Search in sources :

Example 6 with TimedResult

use of com.simiacryptus.util.lang.TimedResult in project MindsEye by SimiaCryptus.

the class TensorListTrainable method eval.

/**
 * Eval point sample.
 *
 * @param list    the list
 * @param monitor the monitor
 * @return the point sample
 */
@Nonnull
protected PointSample eval(@Nonnull final TensorList[] list, @Nullable final TrainingMonitor monitor) {
    int inputs = data.length;
    assert 0 < inputs;
    int items = data[0].length();
    assert 0 < items;
    @Nonnull final TimedResult<PointSample> timedResult = TimedResult.time(() -> {
        final Result[] nnContext = TensorListTrainable.getNNContext(list, mask);
        final Result result = network.eval(nnContext);
        for (@Nonnull Result nnResult : nnContext) {
            nnResult.getData().freeRef();
            nnResult.freeRef();
        }
        final TensorList resultData = result.getData();
        final DoubleSummaryStatistics statistics = resultData.stream().flatMapToDouble(x -> {
            double[] array = Arrays.stream(x.getData()).toArray();
            x.freeRef();
            return Arrays.stream(array);
        }).summaryStatistics();
        final double sum = statistics.getSum();
        @Nonnull final DeltaSet<Layer> deltaSet = new DeltaSet<Layer>();
        @Nonnull PointSample pointSample;
        try {
            result.accumulate(deltaSet, 1.0);
            // log.info(String.format("Evaluated to %s delta buffers, %s mag", DeltaSet<LayerBase>.getMap().size(), DeltaSet<LayerBase>.getMagnitude()));
            @Nonnull StateSet<Layer> stateSet = new StateSet<>(deltaSet);
            pointSample = new PointSample(deltaSet, stateSet, sum, 0.0, items);
            stateSet.freeRef();
        } finally {
            resultData.freeRef();
            result.freeRef();
            deltaSet.freeRef();
        }
        return pointSample;
    });
    if (null != monitor && verbosity() > 0) {
        monitor.log(String.format("Device completed %s items in %.3f sec", items, timedResult.timeNanos / 1e9));
    }
    @Nonnull PointSample normalize = timedResult.result.normalize();
    timedResult.result.freeRef();
    return normalize;
}
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) Nonnull(javax.annotation.Nonnull) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) 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) PointSample(com.simiacryptus.mindseye.lang.PointSample) StateSet(com.simiacryptus.mindseye.lang.StateSet) Nonnull(javax.annotation.Nonnull)

Example 7 with TimedResult

use of com.simiacryptus.util.lang.TimedResult in project MindsEye by SimiaCryptus.

the class PerformanceTester method testPerformance.

/**
 * Test learning performance double statistics.
 *
 * @param component      the component
 * @param inputPrototype the input prototype
 * @return the double statistics
 */
@Nonnull
protected Tuple2<Double, Double> testPerformance(@Nonnull final Layer component, final Tensor... inputPrototype) {
    final Tensor[][] data = IntStream.range(0, batches).mapToObj(x -> x).flatMap(x -> Stream.<Tensor[]>of(inputPrototype)).toArray(i -> new Tensor[i][]);
    @Nonnull TimedResult<Result> timedEval = TimedResult.time(() -> {
        Result[] input = ConstantResult.batchResultArray(data);
        @Nullable Result result;
        try {
            result = component.eval(input);
        } finally {
            for (@Nonnull Result nnResult : input) {
                nnResult.freeRef();
                nnResult.getData().freeRef();
            }
        }
        return result;
    });
    final Result result = timedEval.result;
    @Nonnull final DeltaSet<Layer> buffer = new DeltaSet<Layer>();
    try {
        long timedBackprop = TimedResult.time(() -> {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(result.getData().stream().map(x -> {
                return x.mapAndFree(v -> 1.0);
            }).toArray(i -> new Tensor[i]));
            result.accumulate(buffer, tensorArray);
            assert tensorArray.currentRefCount() == 0;
            return buffer;
        }).timeNanos;
        return new Tuple2<>(timedEval.timeNanos / 1e9, timedBackprop / 1e9);
    } finally {
        buffer.freeRef();
        result.freeRef();
        result.getData().freeRef();
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) TestUtil(com.simiacryptus.mindseye.test.TestUtil) Result(com.simiacryptus.mindseye.lang.Result) Collectors(java.util.stream.Collectors) Tuple2(com.simiacryptus.util.lang.Tuple2) List(java.util.List) Stream(java.util.stream.Stream) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) TimedResult(com.simiacryptus.util.lang.TimedResult) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) Layer(com.simiacryptus.mindseye.lang.Layer) Result(com.simiacryptus.mindseye.lang.Result) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) TimedResult(com.simiacryptus.util.lang.TimedResult) Tuple2(com.simiacryptus.util.lang.Tuple2) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 8 with TimedResult

use of com.simiacryptus.util.lang.TimedResult in project MindsEye by SimiaCryptus.

the class CudaTensorList method toHeap.

private TensorArray toHeap(final boolean avoidAllocations) {
    CudaTensor gpuCopy = this.gpuCopy;
    TimedResult<TensorArray> timedResult = TimedResult.time(() -> CudaDevice.run(gpu -> {
        assert CudaDevice.isThreadDeviceId(gpu.getDeviceId());
        if (null == gpuCopy) {
            if (null == heapCopy) {
                throw new IllegalStateException("No data");
            } else if (heapCopy.isFinalized()) {
                throw new IllegalStateException("Local data has been freed");
            }
        }
        gpuCopy.addRef();
        assert CudaDevice.isThreadDeviceId(gpu.getDeviceId());
        try {
            assert getPrecision() == gpuCopy.getPrecision();
            assert getPrecision() == gpuCopy.descriptor.dataType;
            assert CudaDevice.isThreadDeviceId(gpu.getDeviceId());
            final Tensor[] output = IntStream.range(0, getLength()).mapToObj(dataIndex -> new Tensor(getDimensions())).toArray(i -> new Tensor[i]);
            assert CudaDevice.isThreadDeviceId(gpu.getDeviceId());
            for (int i = 0; i < getLength(); i++) {
                assert CudaDevice.isThreadDeviceId(gpu.getDeviceId());
                gpuCopy.read(gpu, i, output[i], avoidAllocations);
                assert CudaDevice.isThreadDeviceId(gpu.getDeviceId());
            }
            return TensorArray.wrap(output);
        } finally {
            gpuCopy.freeRef();
        }
    }, this));
    CudaTensorList.logger.debug(String.format("Read %s bytes in %.4f from Tensor %s on GPU at %s, created by %s", gpuCopy.size(), timedResult.seconds(), Integer.toHexString(System.identityHashCode(timedResult.result)), TestUtil.toString(TestUtil.getStackTrace()).replaceAll("\n", "\n\t"), TestUtil.toString(createdBy).replaceAll("\n", "\n\t")));
    return timedResult.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) TestUtil(com.simiacryptus.mindseye.test.TestUtil) ReshapedTensorList(com.simiacryptus.mindseye.lang.ReshapedTensorList) Stream(java.util.stream.Stream) RegisteredObjectBase(com.simiacryptus.mindseye.lang.RegisteredObjectBase) TensorList(com.simiacryptus.mindseye.lang.TensorList) TimedResult(com.simiacryptus.util.lang.TimedResult) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) TensorArray(com.simiacryptus.mindseye.lang.TensorArray)

Aggregations

TimedResult (com.simiacryptus.util.lang.TimedResult)8 Arrays (java.util.Arrays)8 Nonnull (javax.annotation.Nonnull)8 Nullable (javax.annotation.Nullable)8 Tensor (com.simiacryptus.mindseye.lang.Tensor)7 TensorArray (com.simiacryptus.mindseye.lang.TensorArray)7 TensorList (com.simiacryptus.mindseye.lang.TensorList)7 IntStream (java.util.stream.IntStream)7 List (java.util.List)6 DeltaSet (com.simiacryptus.mindseye.lang.DeltaSet)4 Layer (com.simiacryptus.mindseye.lang.Layer)4 Result (com.simiacryptus.mindseye.lang.Result)4 NotebookOutput (com.simiacryptus.util.io.NotebookOutput)4 Collectors (java.util.stream.Collectors)4 Futures (com.google.common.util.concurrent.Futures)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)3 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)3 ConstantResult (com.simiacryptus.mindseye.lang.ConstantResult)3 NotebookReportBase (com.simiacryptus.mindseye.test.NotebookReportBase)3