use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class MonitoringSynapse method eval.
@Override
public Result eval(@Nonnull final Result... inObj) {
assert 1 == inObj.length;
final Result input = inObj[0];
final TensorList inputdata = input.getData();
input.addRef();
inputdata.addRef();
System.nanoTime();
System.nanoTime();
totalBatches++;
totalItems += inputdata.length();
forwardStatistics.clear();
inputdata.stream().parallel().forEach(t -> {
forwardStatistics.add(t.getData());
t.freeRef();
});
return new Result(inputdata, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
backpropStatistics.clear();
data.addRef();
input.accumulate(buffer, data);
data.stream().parallel().forEach(t -> {
backpropStatistics.add(t.getData());
t.freeRef();
});
}) {
@Override
public boolean isAlive() {
return input.isAlive();
}
@Override
protected void _free() {
input.freeRef();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class MaxMetaLayer method eval.
@Nonnull
@Override
public Result eval(final Result... inObj) {
final Result input = inObj[0];
input.addRef();
final int itemCnt = input.getData().length();
final Tensor input0Tensor = input.getData().get(0);
final int vectorSize = input0Tensor.length();
@Nonnull final int[] indicies = new int[vectorSize];
for (int i = 0; i < vectorSize; i++) {
final int itemNumber = i;
indicies[i] = IntStream.range(0, itemCnt).mapToObj(x -> x).max(Comparator.comparing(dataIndex -> {
Tensor tensor = input.getData().get(dataIndex);
double v = tensor.getData()[itemNumber];
tensor.freeRef();
return v;
})).get();
}
return new Result(TensorArray.wrap(input0Tensor.mapIndex((v, c) -> {
Tensor tensor = input.getData().get(indicies[c]);
double v1 = tensor.getData()[c];
tensor.freeRef();
return v1;
})), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (input.isAlive()) {
@Nullable final Tensor delta = data.get(0);
@Nonnull final Tensor[] feedback = new Tensor[itemCnt];
Arrays.parallelSetAll(feedback, i -> new Tensor(delta.getDimensions()));
input0Tensor.coordStream(true).forEach((inputCoord) -> {
feedback[indicies[inputCoord.getIndex()]].add(inputCoord, delta.get(inputCoord));
});
@Nonnull TensorArray tensorArray = TensorArray.wrap(feedback);
input.accumulate(buffer, tensorArray);
delta.freeRef();
}
}) {
@Override
public boolean isAlive() {
return input.isAlive();
}
@Override
protected void _free() {
input.freeRef();
input0Tensor.freeRef();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class MeanSqLossLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
if (2 != inObj.length)
throw new IllegalArgumentException();
final int leftLength = inObj[0].getData().length();
final int rightLength = inObj[1].getData().length();
Arrays.stream(inObj).forEach(ReferenceCounting::addRef);
if (leftLength != rightLength && leftLength != 1 && rightLength != 1) {
throw new IllegalArgumentException(leftLength + " != " + rightLength);
}
@Nonnull final Tensor[] diffs = new Tensor[leftLength];
return new Result(TensorArray.wrap(IntStream.range(0, leftLength).mapToObj(dataIndex -> {
@Nullable final Tensor a = inObj[0].getData().get(1 == leftLength ? 0 : dataIndex);
@Nullable final Tensor b = inObj[1].getData().get(1 == rightLength ? 0 : dataIndex);
if (a.length() != b.length()) {
throw new IllegalArgumentException(String.format("%s != %s", Arrays.toString(a.getDimensions()), Arrays.toString(b.getDimensions())));
}
@Nonnull final Tensor r = a.minus(b);
a.freeRef();
b.freeRef();
diffs[dataIndex] = r;
@Nonnull Tensor statsTensor = new Tensor(new double[] { r.sumSq() / r.length() }, 1);
return statsTensor;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (inObj[0].isAlive()) {
Stream<Tensor> tensorStream = IntStream.range(0, data.length()).parallel().mapToObj(dataIndex -> {
@Nullable Tensor tensor = data.get(dataIndex);
Tensor diff = diffs[dataIndex];
@Nullable Tensor scale = diff.scale(tensor.get(0) * 2.0 / diff.length());
tensor.freeRef();
return scale;
}).collect(Collectors.toList()).stream();
if (1 == leftLength) {
tensorStream = Stream.of(tensorStream.reduce((a, b) -> {
@Nullable Tensor c = a.addAndFree(b);
b.freeRef();
return c;
}).get());
}
@Nonnull final TensorList array = TensorArray.wrap(tensorStream.toArray(i -> new Tensor[i]));
inObj[0].accumulate(buffer, array);
}
if (inObj[1].isAlive()) {
Stream<Tensor> tensorStream = IntStream.range(0, data.length()).parallel().mapToObj(dataIndex -> {
@Nullable Tensor tensor = data.get(dataIndex);
@Nullable Tensor scale = diffs[dataIndex].scale(tensor.get(0) * 2.0 / diffs[dataIndex].length());
tensor.freeRef();
return scale;
}).collect(Collectors.toList()).stream();
if (1 == rightLength) {
tensorStream = Stream.of(tensorStream.reduce((a, b) -> {
@Nullable Tensor c = a.addAndFree(b);
b.freeRef();
return c;
}).get());
}
@Nonnull final TensorList array = TensorArray.wrap(tensorStream.map(x -> {
@Nullable Tensor scale = x.scale(-1);
x.freeRef();
return scale;
}).toArray(i -> new Tensor[i]));
inObj[1].accumulate(buffer, array);
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(ReferenceCounting::freeRef);
Arrays.stream(diffs).forEach(ReferenceCounting::freeRef);
}
@Override
public boolean isAlive() {
return inObj[0].isAlive() || inObj[1].isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class MonitoringWrapperLayer method evalAndFree.
@Override
public Result evalAndFree(@Nonnull final Result... inObj) {
@Nonnull final AtomicLong passbackNanos = new AtomicLong(0);
final Result[] wrappedInput = Arrays.stream(inObj).map(result -> {
return new Result(result.getData(), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
data.addRef();
passbackNanos.addAndGet(TimedResult.time(() -> result.accumulate(buffer, data)).timeNanos);
}) {
@Override
protected void _free() {
result.freeRef();
}
@Override
public boolean isAlive() {
return result.isAlive();
}
};
}).toArray(i -> new Result[i]);
@Nonnull TimedResult<Result> timedResult = TimedResult.time(() -> getInner().evalAndFree(wrappedInput));
final Result output = timedResult.result;
forwardPerformance.add((timedResult.timeNanos) / 1000000000.0);
totalBatches++;
final int items = Arrays.stream(inObj).mapToInt(x -> x.getData().length()).max().orElse(1);
totalItems += items;
if (recordSignalMetrics) {
forwardSignal.clear();
output.getData().stream().parallel().forEach(t -> {
forwardSignal.add(t.getData());
t.freeRef();
});
}
return new Result(output.getData(), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (recordSignalMetrics) {
backwardSignal.clear();
data.stream().parallel().forEach(t -> {
backwardSignal.add(t.getData());
t.freeRef();
});
}
data.addRef();
backwardPerformance.add((TimedResult.time(() -> output.accumulate(buffer, data)).timeNanos - passbackNanos.getAndSet(0)) / (items * 1e9));
}) {
@Override
protected void _free() {
output.freeRef();
}
@Override
public boolean isAlive() {
return output.isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class PoolingLayer method evalAndFree.
@Nullable
@Override
public Result evalAndFree(@Nonnull final Result... inObj) {
if (!CudaSystem.isEnabled())
return getCompatibilityLayer().evalAndFree(inObj);
final int poolDims = 2;
@Nonnull final int[] windowSize = { windowX, windowY };
@Nonnull final int[] padding = { paddingX, paddingY };
@Nonnull final int[] stride = { strideX, strideY };
final Result input = inObj[0];
final TensorList inputData = input.getData();
@Nonnull final int[] inputSize = inputData.getDimensions();
final int length = inputData.length();
final int inputDims = Tensor.length(inputSize);
@Nonnull final int[] outputSize = new int[4];
final CudaTensor outputData = CudaSystem.run(gpu -> {
try {
gpu.initThread();
@Nonnull final CudaResource<cudnnPoolingDescriptor> poolingDesc = gpu.createPoolingDescriptor(mode.id, poolDims, windowSize, padding, stride);
@Nullable final CudaTensor inputTensor = gpu.getTensor(inputData, precision, MemoryType.Device, false);
CudaSystem.handle(CudaSystem.cudnnGetPoolingNdForwardOutputDim(poolingDesc.getPtr(), inputTensor.descriptor.getPtr(), 4, outputSize));
assert inputSize[2] == outputSize[1];
@Nonnull final CudaDevice.CudaTensorDescriptor outputDescriptor = gpu.newTensorDescriptor(precision, outputSize[0], outputSize[1], outputSize[2], outputSize[3], outputSize[1] * outputSize[2] * outputSize[3], outputSize[2] * outputSize[3], outputSize[3], 1);
@Nonnull final CudaMemory outputTensor = gpu.allocate((long) precision.size * Tensor.length(outputSize), MemoryType.Managed.normalize(), true);
CudaMemory inputDataMemory = inputTensor.getMemory(gpu);
CudaSystem.handle(gpu.cudnnPoolingForward(poolingDesc.getPtr(), precision.getPointer(alpha), inputTensor.descriptor.getPtr(), inputDataMemory.getPtr(), precision.getPointer(0.0), outputDescriptor.getPtr(), outputTensor.getPtr()));
assert CudaDevice.isThreadDeviceId(gpu.getDeviceId());
inputDataMemory.dirty();
outputTensor.dirty();
Stream.<ReferenceCounting>of(inputTensor, poolingDesc, inputDataMemory).forEach(ReferenceCounting::freeRef);
return CudaTensor.wrap(outputTensor, outputDescriptor, precision);
} catch (@Nonnull final Throwable e) {
throw new ComponentException("Error", e);
}
}, inputData);
return new Result(CudaTensorList.create(outputData, length, new int[] { outputSize[3], outputSize[2], outputSize[1] }, precision), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList error) -> {
assert error.length() == inputData.length();
if (input.isAlive()) {
TensorList data = CudaSystem.run(gpu -> {
@Nonnull final CudaDevice.CudaTensorDescriptor passbackDescriptor = gpu.newTensorDescriptor(precision, length, inputSize[2], inputSize[1], inputSize[0], inputSize[2] * inputSize[1] * inputSize[0], inputSize[1] * inputSize[0], inputSize[0], 1);
@Nonnull final CudaResource<cudnnPoolingDescriptor> poolingDesc = gpu.createPoolingDescriptor(mode.id, poolDims, windowSize, padding, stride);
@Nullable final CudaTensor inputTensor;
synchronized (gpu) {
inputTensor = gpu.getTensor(inputData, precision, MemoryType.Device, true);
}
@Nullable final CudaTensor errorPtr;
synchronized (gpu) {
errorPtr = gpu.getTensor(error, precision, MemoryType.Device, true);
}
@Nonnull final CudaMemory passbackBuffer = gpu.allocate((long) inputDims * precision.size * length, MemoryType.Managed.normalize(), true);
CudaMemory outputDataMemory = outputData.getMemory(gpu);
CudaMemory errorPtrMemory = errorPtr.getMemory(gpu);
CudaMemory inputDataMemory = inputTensor.getMemory(gpu);
CudaSystem.handle(gpu.cudnnPoolingBackward(poolingDesc.getPtr(), precision.getPointer(this.alpha), outputData.descriptor.getPtr(), outputDataMemory.getPtr(), errorPtr.descriptor.getPtr(), errorPtrMemory.getPtr(), inputTensor.descriptor.getPtr(), inputDataMemory.getPtr(), precision.getPointer(0.0), passbackDescriptor.getPtr(), passbackBuffer.getPtr()));
outputDataMemory.dirty();
errorPtrMemory.dirty();
inputDataMemory.dirty();
passbackBuffer.dirty();
Stream.<ReferenceCounting>of(errorPtr, inputTensor, poolingDesc, outputDataMemory, errorPtrMemory, inputDataMemory).forEach(ReferenceCounting::freeRef);
return CudaTensorList.wrap(CudaTensor.wrap(passbackBuffer, passbackDescriptor, precision), length, inputSize, precision);
}, error);
input.accumulate(buffer, data);
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
inputData.freeRef();
outputData.freeRef();
}
@Override
public boolean isAlive() {
return input.isAlive() || !isFrozen();
}
};
}
Aggregations