Search in sources :

Example 1 with SimpleGpuEval

use of com.simiacryptus.mindseye.test.SimpleGpuEval in project MindsEye by SimiaCryptus.

the class CudaLayerTester method testNonstandardBoundsBackprop.

/**
 * Test nonstandard bounds backprop tolerance statistics.
 *
 * @param log            the log
 * @param layer          the layer
 * @param inputPrototype the input prototype
 * @return the tolerance statistics
 */
@Nonnull
public ToleranceStatistics testNonstandardBoundsBackprop(final NotebookOutput log, @Nullable final Layer layer, @Nonnull final Tensor[] inputPrototype) {
    log.h2("Irregular Backprop");
    log.p("This layer should accept non-dense tensors as delta input.");
    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 = Arrays.stream(randomized).map(original -> {
            return TensorArray.wrap(original);
        }).toArray(i -> new TensorList[i]);
        @Nonnull final SimpleResult testResult = CudaSystem.run(gpu -> {
            TensorList[] copy = copy(controlInput);
            SimpleResult result = new SimpleGpuEval(layer, gpu, copy) {

                @Nonnull
                @Override
                public TensorList getFeedback(@Nonnull final TensorList original) {
                    Tensor originalTensor = original.get(0).mapAndFree(x -> 1);
                    CudaTensorList cudaTensorList = buildIrregularCudaTensor(gpu, precision, originalTensor);
                    originalTensor.freeRef();
                    return cudaTensorList;
                }
            }.call();
            Arrays.stream(copy).forEach(ReferenceCounting::freeRef);
            return result;
        });
        @Nonnull final SimpleResult controlResult = CudaSystem.run(gpu -> {
            TensorList[] copy = copy(controlInput);
            SimpleResult result = SimpleGpuEval.run(layer, gpu, copy);
            Arrays.stream(copy).forEach(ReferenceCounting::freeRef);
            return result;
        }, 1);
        try {
            ToleranceStatistics compareOutput = compareOutput(controlResult, testResult);
            ToleranceStatistics compareDerivatives = compareDerivatives(controlResult, testResult);
            return compareDerivatives.combine(compareOutput);
        } finally {
            Arrays.stream(controlInput).forEach(ReferenceCounting::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) 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) SimpleGpuEval(com.simiacryptus.mindseye.test.SimpleGpuEval) 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) Nonnull(javax.annotation.Nonnull)

Aggregations

Layer (com.simiacryptus.mindseye.lang.Layer)1 ReferenceCounting (com.simiacryptus.mindseye.lang.ReferenceCounting)1 ReferenceCountingBase (com.simiacryptus.mindseye.lang.ReferenceCountingBase)1 Tensor (com.simiacryptus.mindseye.lang.Tensor)1 TensorArray (com.simiacryptus.mindseye.lang.TensorArray)1 TensorList (com.simiacryptus.mindseye.lang.TensorList)1 CudaDevice (com.simiacryptus.mindseye.lang.cudnn.CudaDevice)1 CudaMemory (com.simiacryptus.mindseye.lang.cudnn.CudaMemory)1 CudaSystem (com.simiacryptus.mindseye.lang.cudnn.CudaSystem)1 CudaTensor (com.simiacryptus.mindseye.lang.cudnn.CudaTensor)1 CudaTensorList (com.simiacryptus.mindseye.lang.cudnn.CudaTensorList)1 CudnnHandle (com.simiacryptus.mindseye.lang.cudnn.CudnnHandle)1 MemoryType (com.simiacryptus.mindseye.lang.cudnn.MemoryType)1 Precision (com.simiacryptus.mindseye.lang.cudnn.Precision)1 SimpleGpuEval (com.simiacryptus.mindseye.test.SimpleGpuEval)1 SimpleResult (com.simiacryptus.mindseye.test.SimpleResult)1 ToleranceStatistics (com.simiacryptus.mindseye.test.ToleranceStatistics)1 NotebookOutput (com.simiacryptus.util.io.NotebookOutput)1 Arrays (java.util.Arrays)1 Random (java.util.Random)1