org.nd4j.linalg.factory.Nd4j.createBufferDetached()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(14.1k)|赞(0)|评价(0)|浏览(117)

本文整理了Java中org.nd4j.linalg.factory.Nd4j.createBufferDetached()方法的一些代码示例,展示了Nd4j.createBufferDetached()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Nd4j.createBufferDetached()方法的具体详情如下:
包路径:org.nd4j.linalg.factory.Nd4j
类名称:Nd4j
方法名:createBufferDetached

Nd4j.createBufferDetached介绍

[英]Create a buffer equal of length prod(shape). This method is NOT affected by workspaces
[中]创建一个长度等于prod(shape)的缓冲区。此方法不受工作区的影响

代码示例

代码示例来源:origin: deeplearning4j/nd4j

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;
}

代码示例来源:origin: deeplearning4j/nd4j

/**
 * 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;
}

代码示例来源:origin: deeplearning4j/nd4j

/**
 * Read in an ndarray from a data input stream
 *
 * @param dis the data input stream to read from
 * @return the ndarray
 * @throws IOException
 */
public static INDArray read(DataInputStream dis) throws IOException {
  DataBuffer shapeInformation = Nd4j.createBufferDetached(new long[1], DataBuffer.Type.LONG);
  shapeInformation.read(dis);
  int length = Shape.length(shapeInformation);
  DataBuffer data = CompressedDataBuffer.readUnknown(dis, length);
  return createArrayFromShapeBuffer(data, shapeInformation);
}

代码示例来源:origin: deeplearning4j/nd4j

DataBuffer shapeBuff = Nd4j.createBufferDetached(new int[shapeBufferLength]);

代码示例来源:origin: deeplearning4j/nd4j

@Override
public INDArray subArray(long[] offsets, int[] shape, int[] stride) {
  Nd4j.getCompressor().autoDecompress(this);
  int n = shape.length;
  // FIXME: shapeInfo should be used here
  if (shape.length < 1)
    return create(Nd4j.createBufferDetached(shape));
  if (offsets.length != n)
    throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets));
  if (stride.length != n)
    throw new IllegalArgumentException("Invalid stride " + Arrays.toString(stride));
  if (Shape.contentEquals(shape, shapeOf())) {
    if (ArrayUtil.isZero(offsets)) {
      return this;
    } else {
      throw new IllegalArgumentException("Invalid subArray offsets");
    }
  }
  long[] dotProductOffsets = offsets;
  int[] dotProductStride = stride;
  long offset = Shape.offset(javaShapeInformation) + NDArrayIndex.offset(dotProductStride, dotProductOffsets);
  if (offset >= data().length())
    offset = ArrayUtil.sumLong(offsets);
  return create(data, Arrays.copyOf(shape, shape.length), stride, offset, ordering());
}

代码示例来源:origin: deeplearning4j/nd4j

@Override
public INDArray subArray(ShapeOffsetResolution resolution) {
  Nd4j.getCompressor().autoDecompress(this);
  long[] offsets = resolution.getOffsets();
  int[] shape = LongUtils.toInts(resolution.getShapes());
  int[] stride = LongUtils.toInts(resolution.getStrides());
  //        if (offset() + resolution.getOffset() >= Integer.MAX_VALUE)
  //            throw new IllegalArgumentException("Offset of array can not be >= Integer.MAX_VALUE");
  long offset = (offset() + resolution.getOffset());
  int n = shape.length;
  // FIXME: shapeInfo should be used here
  if (shape.length < 1)
    return create(Nd4j.createBufferDetached(shape));
  if (offsets.length != n)
    throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets));
  if (stride.length != n)
    throw new IllegalArgumentException("Invalid stride " + Arrays.toString(stride));
  if (shape.length == rank() && Shape.contentEquals(shape, shapeOf())) {
    if (ArrayUtil.isZero(offsets)) {
      return this;
    } else {
      throw new IllegalArgumentException("Invalid subArray offsets");
    }
  }
  char newOrder = Shape.getOrder(shape, stride, 1);
  return create(data, Arrays.copyOf(shape, shape.length), stride, offset, newOrder);
}

代码示例来源:origin: org.nd4j/nd4j-api

public static DataBuffer createBufferDetached(int[] shape, DataBuffer.Type type) {
  int length = ArrayUtil.prod(shape);
  if (type == DataBuffer.Type.INT)
    return createBufferDetached(new int[length]);
  else if (type == DataBuffer.Type.HALF)
    return createBufferDetached(new float[length]);
  return type == DataBuffer.Type.DOUBLE ? createBufferDetached(new double[length]) : createBufferDetached(new float[length]);
}

代码示例来源:origin: org.nd4j/nd4j-api

shapeBuffer[count] = (int) order;
DataBuffer ret = Nd4j.createBufferDetached(shapeBuffer);
ret.setConstant(true);

代码示例来源:origin: org.nd4j/nd4j-cuda-10.0

@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);
}

代码示例来源:origin: org.nd4j/nd4j-cuda-7.5

/**
 * 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);
}

代码示例来源:origin: org.nd4j/nd4j-cuda-10.0

/**
 * 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);
}

代码示例来源:origin: org.nd4j/nd4j-cuda-7.5

/**
 * 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);
}

代码示例来源:origin: org.nd4j/nd4j-cuda-7.5

/**
 * 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);
}

代码示例来源:origin: org.nd4j/nd4j-cuda-10.0

/**
 * 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);
}

代码示例来源:origin: org.nd4j/nd4j-cuda-10.0

/**
 * 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);
}

代码示例来源:origin: org.nd4j/nd4j-api

/**
 * Read in an ndarray from a data input stream
 *
 * @param dis the data input stream to read from
 * @return the ndarray
 * @throws IOException
 */
public static INDArray read(DataInputStream dis) throws IOException {
  DataBuffer shapeInformation = Nd4j.createBufferDetached(new int[1], DataBuffer.Type.INT);
  shapeInformation.read(dis);
  int length = Shape.length(shapeInformation);
  DataBuffer data = CompressedDataBuffer.readUnknown(dis, length);
  return createArrayFromShapeBuffer(data, shapeInformation);
}

代码示例来源:origin: org.nd4j/nd4j-api

DataBuffer shapeBuff = Nd4j.createBufferDetached(new int[shapeBufferLength]);

代码示例来源:origin: org.nd4j/nd4j-api

@Override
public INDArray subArray(long[] offsets, int[] shape, int[] stride) {
  Nd4j.getCompressor().autoDecompress(this);
  int n = shape.length;
  // FIXME: shapeInfo should be used here
  if (shape.length < 1)
    return create(Nd4j.createBufferDetached(shape));
  if (offsets.length != n)
    throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets));
  if (stride.length != n)
    throw new IllegalArgumentException("Invalid stride " + Arrays.toString(stride));
  if (Shape.contentEquals(shape, shapeOf())) {
    if (ArrayUtil.isZero(offsets)) {
      return this;
    } else {
      throw new IllegalArgumentException("Invalid subArray offsets");
    }
  }
  long[] dotProductOffsets = offsets;
  int[] dotProductStride = stride;
  long offset = Shape.offset(javaShapeInformation) + NDArrayIndex.offset(dotProductStride, dotProductOffsets);
  if (offset >= data().length())
    offset = ArrayUtil.sumLong(offsets);
  return create(data, Arrays.copyOf(shape, shape.length), stride, offset, ordering());
}

代码示例来源:origin: org.nd4j/nd4j-api

return create(Nd4j.createBufferDetached(shape));
if (offsets.length != n)
  throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets));

相关文章