Java Code Examples for org.nd4j.linalg.factory.Nd4j#createBufferDetached()
The following examples show how to use
org.nd4j.linalg.factory.Nd4j#createBufferDetached() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConstantBuffersCache.java From nd4j with Apache License 2.0 | 6 votes |
@Override public DataBuffer getConstantBuffer(int[] array) { ArrayDescriptor descriptor = new ArrayDescriptor(array); if (!buffersCache.containsKey(descriptor)) { DataBuffer buffer = Nd4j.createBufferDetached(array); // we always allow int arrays with length < 3. 99.9% it's just dimension array. we don't want to recreate them over and over if (counter.get() < MAX_ENTRIES || array.length < 4) { counter.incrementAndGet(); buffersCache.put(descriptor, buffer); bytes.addAndGet(array.length * 4); } return buffer; } return buffersCache.get(descriptor); }
Example 2
Source File: ConstantBuffersCache.java From nd4j with Apache License 2.0 | 6 votes |
@Override public DataBuffer getConstantBuffer(float[] array) { ArrayDescriptor descriptor = new ArrayDescriptor(array); if (!buffersCache.containsKey(descriptor)) { DataBuffer buffer = Nd4j.createBufferDetached(array); if (counter.get() < MAX_ENTRIES) { counter.incrementAndGet(); buffersCache.put(descriptor, buffer); bytes.addAndGet(array.length * Nd4j.sizeOfDataType()); } return buffer; } return buffersCache.get(descriptor); }
Example 3
Source File: ConstantBuffersCache.java From nd4j with Apache License 2.0 | 6 votes |
@Override public DataBuffer getConstantBuffer(double[] array) { ArrayDescriptor descriptor = new ArrayDescriptor(array); if (!buffersCache.containsKey(descriptor)) { DataBuffer buffer = Nd4j.createBufferDetached(array); if (counter.get() < MAX_ENTRIES) { counter.incrementAndGet(); buffersCache.put(descriptor, buffer); bytes.addAndGet(array.length * Nd4j.sizeOfDataType()); } return buffer; } return buffersCache.get(descriptor); }
Example 4
Source File: ConstantBuffersCache.java From nd4j with Apache License 2.0 | 6 votes |
@Override public DataBuffer getConstantBuffer(long[] array) { ArrayDescriptor descriptor = new ArrayDescriptor(array); if (!buffersCache.containsKey(descriptor)) { DataBuffer buffer = Nd4j.createBufferDetached(array); if (counter.get() < MAX_ENTRIES) { counter.incrementAndGet(); buffersCache.put(descriptor, buffer); bytes.addAndGet(array.length * Nd4j.sizeOfDataType()); } return buffer; } return buffersCache.get(descriptor); }
Example 5
Source File: Shape.java From nd4j with Apache License 2.0 | 6 votes |
/** * Creates the shape information buffer * given the shape,stride * @param shape the shape for the buffer * @param stride the stride for the buffer * @param offset the offset for the buffer * @param elementWiseStride the element wise stride for the buffer * @param order the order for the buffer * @return the shape information buffer given the parameters */ public static DataBuffer createShapeInformation(int[] shape, int[] stride, long offset, int elementWiseStride, char order) { if (shape.length != stride.length) throw new IllegalStateException("Shape and stride must be the same length"); int rank = shape.length; int shapeBuffer[] = new int[rank * 2 + 4]; shapeBuffer[0] = rank; int count = 1; for (int e = 0; e < shape.length; e++) shapeBuffer[count++] = shape[e]; for (int e = 0; e < stride.length; e++) shapeBuffer[count++] = stride[e]; shapeBuffer[count++] = (int) offset; shapeBuffer[count++] = elementWiseStride; shapeBuffer[count] = (int) order; DataBuffer ret = Nd4j.createBufferDetached(shapeBuffer); ret.setConstant(true); return ret; }
Example 6
Source File: Shape.java From nd4j with Apache License 2.0 | 6 votes |
public static DataBuffer createShapeInformation(long[] shape, long[] stride, long offset, long elementWiseStride, char order) { offset = 0; if (shape.length != stride.length) throw new IllegalStateException("Shape and stride must be the same length"); int rank = shape.length; long shapeBuffer[] = new long[rank * 2 + 4]; shapeBuffer[0] = rank; int count = 1; for (int e = 0; e < shape.length; e++) shapeBuffer[count++] = shape[e]; for (int e = 0; e < stride.length; e++) shapeBuffer[count++] = stride[e]; shapeBuffer[count++] = (int) offset; shapeBuffer[count++] = elementWiseStride; shapeBuffer[count] = (int) order; DataBuffer ret = Nd4j.createBufferDetached(shapeBuffer); ret.setConstant(true); return ret; }
Example 7
Source File: ConstantBuffersCache.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Override public DataBuffer getConstantBuffer(long[] array, DataType dataType) { ArrayDescriptor descriptor = new ArrayDescriptor(array, dataType); if (!buffersCache.containsKey(descriptor)) { DataBuffer buffer = Nd4j.createBufferDetached(array); if (counter.get() < MAX_ENTRIES) { counter.incrementAndGet(); buffersCache.put(descriptor, buffer); bytes.addAndGet(array.length * Nd4j.sizeOfDataType(dataType)); AllocationsTracker.getInstance().markAllocated(AllocationKind.CONSTANT, 0, array.length * Nd4j.sizeOfDataType(dataType)); } return buffer; } return buffersCache.get(descriptor); }
Example 8
Source File: ProtectedCudaConstantHandler.java From nd4j with Apache License 2.0 | 5 votes |
/** * This method returns DataBuffer with contant equal to input array. * * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer * * @param array * @return */ @Override public DataBuffer getConstantBuffer(int[] array) { // logger.info("getConstantBuffer(int[]) called"); ArrayDescriptor descriptor = new ArrayDescriptor(array); Integer deviceId = AtomicAllocator.getInstance().getDeviceId(); ensureMaps(deviceId); if (!buffersCache.get(deviceId).containsKey(descriptor)) { // we create new databuffer //logger.info("Creating new constant buffer..."); DataBuffer buffer = Nd4j.createBufferDetached(array); if (constantOffsets.get(deviceId).get() + (array.length * 4) < MAX_CONSTANT_LENGTH) { buffer.setConstant(true); // now we move data to constant memory, and keep happy moveToConstantSpace(buffer); buffersCache.get(deviceId).put(descriptor, buffer); bytes.addAndGet(array.length * 4); } return buffer; } //else logger.info("Reusing constant buffer..."); return buffersCache.get(deviceId).get(descriptor); }
Example 9
Source File: ProtectedCudaConstantHandler.java From nd4j with Apache License 2.0 | 5 votes |
@Override public DataBuffer getConstantBuffer(long[] array) { // logger.info("getConstantBuffer(int[]) called"); ArrayDescriptor descriptor = new ArrayDescriptor(array); Integer deviceId = AtomicAllocator.getInstance().getDeviceId(); ensureMaps(deviceId); if (!buffersCache.get(deviceId).containsKey(descriptor)) { // we create new databuffer //logger.info("Creating new constant buffer..."); DataBuffer buffer = Nd4j.createBufferDetached(array); if (constantOffsets.get(deviceId).get() + (array.length * 8) < MAX_CONSTANT_LENGTH) { buffer.setConstant(true); // now we move data to constant memory, and keep happy moveToConstantSpace(buffer); buffersCache.get(deviceId).put(descriptor, buffer); bytes.addAndGet(array.length * 8); } return buffer; } //else logger.info("Reusing constant buffer..."); return buffersCache.get(deviceId).get(descriptor); }
Example 10
Source File: ProtectedCudaConstantHandler.java From nd4j with Apache License 2.0 | 5 votes |
/** * This method returns DataBuffer with contant equal to input array. * * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer * * @param array * @return */ @Override public DataBuffer getConstantBuffer(float[] array) { // logger.info("getConstantBuffer(float[]) called"); ArrayDescriptor descriptor = new ArrayDescriptor(array); Integer deviceId = AtomicAllocator.getInstance().getDeviceId(); ensureMaps(deviceId); if (!buffersCache.get(deviceId).containsKey(descriptor)) { // we create new databuffer //logger.info("Creating new constant buffer..."); DataBuffer buffer = Nd4j.createBufferDetached(array); if (constantOffsets.get(deviceId).get() + (array.length * Nd4j.sizeOfDataType()) < MAX_CONSTANT_LENGTH) { buffer.setConstant(true); // now we move data to constant memory, and keep happy moveToConstantSpace(buffer); buffersCache.get(deviceId).put(descriptor, buffer); bytes.addAndGet(array.length * Nd4j.sizeOfDataType()); } return buffer; } // else logger.info("Reusing constant buffer..."); return buffersCache.get(deviceId).get(descriptor); }
Example 11
Source File: ProtectedCudaConstantHandler.java From nd4j with Apache License 2.0 | 5 votes |
/** * This method returns DataBuffer with contant equal to input array. * * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer * * @param array * @return */ @Override public DataBuffer getConstantBuffer(double[] array) { //logger.info("getConstantBuffer(double[]) called: {}", Arrays.toString(array)); ArrayDescriptor descriptor = new ArrayDescriptor(array); Integer deviceId = AtomicAllocator.getInstance().getDeviceId(); ensureMaps(deviceId); if (!buffersCache.get(deviceId).containsKey(descriptor)) { // we create new databuffer //logger.info("Creating new constant buffer..."); DataBuffer buffer = Nd4j.createBufferDetached(array); if (constantOffsets.get(deviceId).get() + (array.length * Nd4j.sizeOfDataType()) < MAX_CONSTANT_LENGTH) { buffer.setConstant(true); // now we move data to constant memory, and keep happy moveToConstantSpace(buffer); buffersCache.get(deviceId).put(descriptor, buffer); bytes.addAndGet(array.length * Nd4j.sizeOfDataType()); } return buffer; } //else logger.info("Reusing constant buffer..."); return buffersCache.get(deviceId).get(descriptor); }
Example 12
Source File: JCublasNDArrayFactory.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Override public INDArray createUninitializedDetached(DataType dataType, char ordering, long... shape) { return new JCublasNDArray(Nd4j.createBufferDetached(shape, dataType), shape, Nd4j.getStrides(shape, order), order, dataType); }
Example 13
Source File: DataBufferTests.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test @Ignore("AB 2019/06/03 - CI issue: \"CUDA stream synchronization failed\" - see issue 7657") public void testNoArgCreateBufferFromArray() { //Tests here: //1. Create from JVM array //2. Create from JVM array with offset -> does this even make sense? //3. Create detached buffer WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L) .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build(); MemoryWorkspace workspace = Nd4j.getWorkspaceManager().createNewWorkspace(initialConfig, "WorkspaceId"); for (boolean useWs : new boolean[]{false, true}) { try (MemoryWorkspace ws = (useWs ? workspace.notifyScopeEntered() : null)) { //Float DataBuffer f = Nd4j.createBuffer(new float[]{1, 2, 3}); checkTypes(DataType.FLOAT, f, 3); assertEquals(useWs, f.isAttached()); testDBOps(f); f = Nd4j.createBuffer(new float[]{1, 2, 3}, 0); checkTypes(DataType.FLOAT, f, 3); assertEquals(useWs, f.isAttached()); testDBOps(f); f = Nd4j.createBufferDetached(new float[]{1, 2, 3}); checkTypes(DataType.FLOAT, f, 3); assertFalse(f.isAttached()); testDBOps(f); //Double DataBuffer d = Nd4j.createBuffer(new double[]{1, 2, 3}); checkTypes(DataType.DOUBLE, d, 3); assertEquals(useWs, d.isAttached()); testDBOps(d); d = Nd4j.createBuffer(new double[]{1, 2, 3}, 0); checkTypes(DataType.DOUBLE, d, 3); assertEquals(useWs, d.isAttached()); testDBOps(d); d = Nd4j.createBufferDetached(new double[]{1, 2, 3}); checkTypes(DataType.DOUBLE, d, 3); assertFalse(d.isAttached()); testDBOps(d); //Int DataBuffer i = Nd4j.createBuffer(new int[]{1, 2, 3}); checkTypes(DataType.INT, i, 3); assertEquals(useWs, i.isAttached()); testDBOps(i); i = Nd4j.createBuffer(new int[]{1, 2, 3}); checkTypes(DataType.INT, i, 3); assertEquals(useWs, i.isAttached()); testDBOps(i); i = Nd4j.createBufferDetached(new int[]{1, 2, 3}); checkTypes(DataType.INT, i, 3); assertFalse(i.isAttached()); testDBOps(i); //Long DataBuffer l = Nd4j.createBuffer(new long[]{1, 2, 3}); checkTypes(DataType.LONG, l, 3); assertEquals(useWs, l.isAttached()); testDBOps(l); l = Nd4j.createBuffer(new long[]{1, 2, 3}); checkTypes(DataType.LONG, l, 3); assertEquals(useWs, l.isAttached()); testDBOps(l); l = Nd4j.createBufferDetached(new long[]{1, 2, 3}); checkTypes(DataType.LONG, l, 3); assertFalse(l.isAttached()); testDBOps(l); //byte // DataBuffer b = Nd4j.createBuffer(new byte[]{1, 2, 3}); // checkTypes(DataType.BYTE, b, 3); // testDBOps(b); // // b = Nd4j.createBuffer(new byte[]{1, 2, 3}, 0); // checkTypes(DataType.BYTE, b, 3); // testDBOps(b); // // b = Nd4j.createBufferDetached(new byte[]{1,2,3}); // checkTypes(DataType.BYTE, b, 3); // testDBOps(b); //short //TODO } } }