use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class CudnnHandle method getTensor.
/**
* The Ptr.
*
* @param data the data
* @param memoryType the memory type
* @param dense the dense
* @return the ptr
*/
@Nonnull
public CudaTensor getTensor(@Nonnull final CudaTensorList data, @Nonnull final MemoryType memoryType, final boolean dense) {
assert CudaDevice.isThreadDeviceId(getDeviceId());
CudaTensor gpuCopy = data.gpuCopy;
CudaTensor result = gpuCopy;
TensorArray heapCopy = data.heapCopy;
if ((null == result || result.isFinalized()) && null != heapCopy && !heapCopy.isFinalized()) {
result = getTensor(heapCopy, data.getPrecision(), memoryType, dense);
} else {
result.addRef();
}
if (dense)
result = result.getDenseAndFree(this);
if (null == result) {
throw new IllegalStateException("No data");
}
CudaTensor prev = null;
synchronized (data) {
if (result != gpuCopy) {
if (null != data.gpuCopy) {
prev = data.gpuCopy;
}
data.gpuCopy = result;
data.gpuCopy.addRef();
}
}
if (null != prev)
prev.freeRef();
return result;
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class TensorListTrainable method getNNContext.
/**
* Get nn context nn result [ ].
*
* @param data the data
* @param mask the mask
* @return the nn result [ ]
*/
public static Result[] getNNContext(@Nullable final TensorList[] data, @Nullable final boolean[] mask) {
if (null == data)
throw new IllegalArgumentException();
int inputs = data.length;
assert 0 < inputs;
int items = data[0].length();
assert 0 < items;
return IntStream.range(0, inputs).mapToObj(col -> {
final Tensor[] tensors = IntStream.range(0, items).mapToObj(row -> data[col].get(row)).toArray(i -> new Tensor[i]);
@Nonnull TensorArray tensorArray = TensorArray.create(tensors);
if (null == mask || col >= mask.length || !mask[col]) {
return new ConstantResult(tensorArray);
} else {
return new Result(tensorArray, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
for (int index = 0; index < delta.length(); index++) {
final Tensor dt = delta.get(index);
@Nullable final double[] d = dt.getData();
final Tensor t = tensors[index];
@Nullable final double[] p = t.getData();
@Nonnull PlaceholderLayer<double[]> layer = new PlaceholderLayer<>(p);
buffer.get(layer, p).addInPlace(d).freeRef();
dt.freeRef();
layer.freeRef();
}
}) {
@Override
public boolean isAlive() {
return true;
}
};
}
}).toArray(x1 -> new Result[x1]);
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class CudaLayerTester method buildIrregularCudaTensor.
/**
* Build irregular cuda tensor cuda tensor list.
*
* @param gpu the gpu
* @param precision the precision
* @param original the original
* @return the cuda tensor list
*/
public CudaTensorList buildIrregularCudaTensor(final CudnnHandle gpu, final Precision precision, final Tensor original) {
TensorArray data = TensorArray.create(original);
int[] inputSize = original.getDimensions();
int channels = inputSize.length < 3 ? 1 : inputSize[2];
int height = inputSize.length < 2 ? 1 : inputSize[1];
int width = inputSize.length < 1 ? 1 : inputSize[0];
final int listLength = 1;
final int elementLength = data.getElements();
MemoryType memoryType = MemoryType.Managed;
@Nonnull final CudaMemory ptr0 = gpu.allocate((long) elementLength * listLength * precision.size, memoryType, false);
@Nonnull final CudaDevice.CudaTensorDescriptor descriptor0 = gpu.newTensorDescriptor(precision, listLength, channels, height, width, channels * height * width, height * width, width, 1);
for (int i = 0; i < listLength; i++) {
Tensor tensor = data.get(i);
assert null != data;
assert null != tensor;
assert Arrays.equals(tensor.getDimensions(), data.getDimensions()) : Arrays.toString(tensor.getDimensions()) + " != " + Arrays.toString(data.getDimensions());
ptr0.write(precision, tensor.getData(), (long) i * elementLength);
tensor.freeRef();
}
data.freeRef();
Random r = new Random();
int c = r.nextInt(5);
int v = r.nextInt(5);
int h = r.nextInt(5);
@Nonnull final CudaMemory ptr1 = gpu.allocate((long) (channels + c) * (height + v) * (width + h) * listLength * precision.size, memoryType, false);
@Nonnull final CudaDevice.CudaTensorDescriptor descriptor1 = gpu.newTensorDescriptor(precision, listLength, channels, height, width, (height + v) * (width + h) * (channels + c), (height + v) * (width + h), width + h, 1);
gpu.cudnnTransformTensor(precision.getPointer(1.0), descriptor0.getPtr(), ptr0.getPtr(), precision.getPointer(0.0), descriptor1.getPtr(), ptr1.getPtr());
assert CudaDevice.isThreadDeviceId(gpu.getDeviceId());
ptr0.dirty();
ptr1.dirty();
descriptor0.freeRef();
ptr0.freeRef();
return CudaTensorList.wrap(CudaTensor.wrap(ptr1, descriptor1, precision), 1, original.getDimensions(), precision);
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class BatchDerivativeTester method getFeedbackGradient.
@Nonnull
private Tensor getFeedbackGradient(@Nonnull final Layer component, final int inputIndex, @Nonnull final Tensor outputPrototype, final Tensor... inputPrototype) {
final Tensor inputTensor = inputPrototype[inputIndex];
final int inputDims = inputTensor.length();
@Nonnull final Tensor result = new Tensor(inputDims, outputPrototype.length());
for (int j = 0; j < outputPrototype.length(); j++) {
final int j_ = j;
@Nonnull final PlaceholderLayer<Tensor> inputKey = new PlaceholderLayer<Tensor>(new Tensor());
@Nonnull final Result copyInput = new Result(TensorArray.create(inputPrototype), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
@Nonnull final Tensor gradientBuffer = new Tensor(inputDims, outputPrototype.length());
if (!Arrays.equals(inputTensor.getDimensions(), data.get(inputIndex).getDimensions())) {
throw new AssertionError();
}
for (int i = 0; i < inputDims; i++) {
gradientBuffer.set(new int[] { i, j_ }, data.get(inputIndex).getData()[i]);
}
buffer.get(inputKey, new double[gradientBuffer.length()]).addInPlace(gradientBuffer.getData());
}) {
@Override
public boolean isAlive() {
return true;
}
};
@Nullable final Result eval = component.eval(copyInput);
@Nonnull final DeltaSet<Layer> xxx = new DeltaSet<Layer>();
@Nonnull TensorArray tensorArray = TensorArray.wrap(eval.getData().stream().map(x -> {
@Nonnull Tensor set = x.set(j_, 1);
x.freeRef();
return set;
}).toArray(i -> new Tensor[i]));
eval.accumulate(xxx, tensorArray);
final Delta<Layer> inputDelta = xxx.getMap().get(inputKey);
if (null != inputDelta) {
result.addInPlace(new Tensor(inputDelta.getDelta(), result.getDimensions()));
}
}
return result;
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class CudaLayerTester method testNonstandardBounds.
/**
* Test nonstandard bounds tolerance statistics.
*
* @param log the log
* @param reference the reference
* @param inputPrototype the input prototype
* @return the tolerance statistics
*/
@Nonnull
public ToleranceStatistics testNonstandardBounds(final NotebookOutput log, @Nullable final Layer reference, @Nonnull final Tensor[] inputPrototype) {
log.h2("Irregular Input");
log.p("This layer should be able to accept non-dense inputs.");
return log.code(() -> {
Tensor[] randomized = Arrays.stream(inputPrototype).map(x -> x.map(v -> getRandom())).toArray(i -> new Tensor[i]);
logger.info("Input: " + Arrays.stream(randomized).map(Tensor::prettyPrint).collect(Collectors.toList()));
Precision precision = Precision.Double;
TensorList[] controlInput = CudaSystem.run(gpu -> {
return Arrays.stream(randomized).map(original -> {
TensorArray data = TensorArray.create(original);
CudaTensorList wrap = CudaTensorList.wrap(gpu.getTensor(data, precision, MemoryType.Managed, false), 1, original.getDimensions(), precision);
data.freeRef();
return wrap;
}).toArray(i -> new TensorList[i]);
}, 0);
@Nonnull final SimpleResult controlResult = CudaSystem.run(gpu -> {
return SimpleGpuEval.run(reference, gpu, controlInput);
}, 1);
final TensorList[] irregularInput = CudaSystem.run(gpu -> {
return Arrays.stream(randomized).map(original -> {
return buildIrregularCudaTensor(gpu, precision, original);
}).toArray(i -> new TensorList[i]);
}, 0);
@Nonnull final SimpleResult testResult = CudaSystem.run(gpu -> {
return SimpleGpuEval.run(reference, gpu, irregularInput);
}, 1);
try {
ToleranceStatistics compareOutput = compareOutput(controlResult, testResult);
ToleranceStatistics compareDerivatives = compareDerivatives(controlResult, testResult);
return compareDerivatives.combine(compareOutput);
} finally {
Arrays.stream(randomized).forEach(ReferenceCountingBase::freeRef);
Arrays.stream(controlInput).forEach(ReferenceCounting::freeRef);
Arrays.stream(irregularInput).forEach(x -> x.freeRef());
controlResult.freeRef();
testResult.freeRef();
}
});
}
Aggregations