Search in sources :

Example 36 with TensorArray

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;
}
Also used : TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nonnull(javax.annotation.Nonnull)

Example 37 with TensorArray

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]);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Tensor(com.simiacryptus.mindseye.lang.Tensor) ReferenceCountingBase(com.simiacryptus.mindseye.lang.ReferenceCountingBase) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Result(com.simiacryptus.mindseye.lang.Result) StateSet(com.simiacryptus.mindseye.lang.StateSet) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) TimedResult(com.simiacryptus.util.lang.TimedResult) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) Nonnull(javax.annotation.Nonnull) PointSample(com.simiacryptus.mindseye.lang.PointSample) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) Result(com.simiacryptus.mindseye.lang.Result) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) TimedResult(com.simiacryptus.util.lang.TimedResult) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer)

Example 38 with TensorArray

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);
}
Also used : Tensor(com.simiacryptus.mindseye.lang.Tensor) CudaTensor(com.simiacryptus.mindseye.lang.cudnn.CudaTensor) CudaDevice(com.simiacryptus.mindseye.lang.cudnn.CudaDevice) Random(java.util.Random) Nonnull(javax.annotation.Nonnull) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) CudaMemory(com.simiacryptus.mindseye.lang.cudnn.CudaMemory) MemoryType(com.simiacryptus.mindseye.lang.cudnn.MemoryType)

Example 39 with TensorArray

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;
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DoubleBuffer(com.simiacryptus.mindseye.lang.DoubleBuffer) Result(com.simiacryptus.mindseye.lang.Result) Collectors(java.util.stream.Collectors) Delta(com.simiacryptus.mindseye.lang.Delta) List(java.util.List) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) Result(com.simiacryptus.mindseye.lang.Result) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nullable(javax.annotation.Nullable) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Nonnull(javax.annotation.Nonnull)

Example 40 with TensorArray

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();
        }
    });
}
Also used : IntStream(java.util.stream.IntStream) SimpleResult(com.simiacryptus.mindseye.test.SimpleResult) SimpleGpuEval(com.simiacryptus.mindseye.test.SimpleGpuEval) Arrays(java.util.Arrays) CudaMemory(com.simiacryptus.mindseye.lang.cudnn.CudaMemory) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) ReferenceCountingBase(com.simiacryptus.mindseye.lang.ReferenceCountingBase) Random(java.util.Random) Function(java.util.function.Function) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) CudnnHandle(com.simiacryptus.mindseye.lang.cudnn.CudnnHandle) Layer(com.simiacryptus.mindseye.lang.Layer) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) IntFunction(java.util.function.IntFunction) Logger(org.slf4j.Logger) CudaDevice(com.simiacryptus.mindseye.lang.cudnn.CudaDevice) CudaTensor(com.simiacryptus.mindseye.lang.cudnn.CudaTensor) CudaTensorList(com.simiacryptus.mindseye.lang.cudnn.CudaTensorList) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) CudaSystem(com.simiacryptus.mindseye.lang.cudnn.CudaSystem) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) TensorList(com.simiacryptus.mindseye.lang.TensorList) MemoryType(com.simiacryptus.mindseye.lang.cudnn.MemoryType) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) ReferenceCountingBase(com.simiacryptus.mindseye.lang.ReferenceCountingBase) Tensor(com.simiacryptus.mindseye.lang.Tensor) CudaTensor(com.simiacryptus.mindseye.lang.cudnn.CudaTensor) Nonnull(javax.annotation.Nonnull) CudaTensorList(com.simiacryptus.mindseye.lang.cudnn.CudaTensorList) TensorList(com.simiacryptus.mindseye.lang.TensorList) SimpleResult(com.simiacryptus.mindseye.test.SimpleResult) CudaTensorList(com.simiacryptus.mindseye.lang.cudnn.CudaTensorList) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nonnull(javax.annotation.Nonnull)

Aggregations

TensorArray (com.simiacryptus.mindseye.lang.TensorArray)54 Nonnull (javax.annotation.Nonnull)52 Tensor (com.simiacryptus.mindseye.lang.Tensor)51 Nullable (javax.annotation.Nullable)49 DeltaSet (com.simiacryptus.mindseye.lang.DeltaSet)47 Layer (com.simiacryptus.mindseye.lang.Layer)47 Result (com.simiacryptus.mindseye.lang.Result)47 TensorList (com.simiacryptus.mindseye.lang.TensorList)47 Arrays (java.util.Arrays)46 IntStream (java.util.stream.IntStream)46 List (java.util.List)43 JsonObject (com.google.gson.JsonObject)36 LayerBase (com.simiacryptus.mindseye.lang.LayerBase)36 Map (java.util.Map)36 DataSerializer (com.simiacryptus.mindseye.lang.DataSerializer)35 Logger (org.slf4j.Logger)34 LoggerFactory (org.slf4j.LoggerFactory)34 Collectors (java.util.stream.Collectors)11 ConstantResult (com.simiacryptus.mindseye.lang.ConstantResult)8 Coordinate (com.simiacryptus.mindseye.lang.Coordinate)8