Exemple #1
0
        public SyftController(ComputeShader _shader)
        {
            shader = _shader;

            floatTensorFactory = new FloatTensorFactory(_shader, this);
            intTensorFactory   = new IntTensorFactory(_shader);

            models = new Dictionary <int, Model> ();
        }
Exemple #2
0
        public SyftController(ComputeShader _shader)
        {
            shader = _shader;

            floatTensorFactory = new FloatTensorFactory(_shader, this);
            intTensorFactory   = new IntTensorFactory(_shader);

            models     = new Dictionary <int, Model> ();
            agents     = new Dictionary <int, Syft.NN.RL.Agent>();
            optimizers = new Dictionary <int, Optimizer>();
        }
Exemple #3
0
        public void Init(IntTensorFactory _factory,
                         int[] _shape,
                         int[] _data = null,
                         ComputeBuffer _dataBuffer    = null,
                         ComputeBuffer _shapeBuffer   = null,
                         ComputeBuffer _stridesBuffer = null,
                         ComputeShader _shader        = null,
                         bool _copyData      = true,
                         bool _dataOnGpu     = false,
                         string _creation_op = null)
        {
            factory     = _factory;
            dataOnGpu   = _dataOnGpu;
            creation_op = _creation_op;

            // First: check that shape is valid.
            if (_shape == null || _shape.Length == 0)
            {
                throw new InvalidOperationException("Tensor shape can't be an empty array.");
            }

            // Second: since shape is valid, let's save it
            shape = (int[])_shape.Clone();

            setStridesAndCheckShape();

            // Third: let's see what kind of data we've got. We should either have
            // a GPU ComputeBuffer or a data[] object.
            if (_data != null && _shapeBuffer == null && _stridesBuffer == null && _dataBuffer == null)
            {
                InitCpu(_data: _data, _copyData: _copyData);
            }
            else if (_dataBuffer != null && _shapeBuffer != null && _stridesBuffer != null && SystemInfo.supportsComputeShaders && _data == null)
            {
                // looks like we have GPU data being passed in... initialize a GPU tensor.

                InitGpu(_shader, _dataBuffer, _shapeBuffer, _stridesBuffer, _copyData);
            }
            else
            {
                // no data seems to be passed in... or its got missing stuff

                // if CPU works... go with that
                if (_data != null)
                {
                    InitCpu(_data, _copyData);
                }
                else if (_dataBuffer != null && _shader != null)
                {
                    if (SystemInfo.supportsComputeShaders)
                    {
                        // seems i'm just missing a shape buffer - no biggie
                        shapeBuffer = new ComputeBuffer(shape.Length, sizeof(int));
                        shapeBuffer.SetData(shape);

                        InitGpu(_shader, _dataBuffer, _shapeBuffer, _stridesBuffer, _copyData);
                        InitShaderKernels();
                    }
                    else
                    {
                        throw new InvalidOperationException(
                                  "You seem to be trying to create a GPU tensor without having access to a GPU...");
                    }
                }
                else
                {
                    // nothing else seems to work - i suppose i'm just supposed to initialize an empty tensor.
                    long acc = 1;
                    for (var i = shape.Length - 1; i >= 0; --i)
                    {
                        acc *= shape[i];
                    }

                    if (_dataOnGpu)
                    {
                        _shapeBuffer = new ComputeBuffer(shape.Length, sizeof(int));
                        _shapeBuffer.SetData(shape);
                        _stridesBuffer = new ComputeBuffer(shape.Length, sizeof(int));
                        _stridesBuffer.SetData(strides);
                        _dataBuffer = new ComputeBuffer(size, sizeof(float));

                        InitGpu(_shader: _shader, _dataBuffer: _dataBuffer, _shapeBuffer: _shapeBuffer, _stridesBuffer: _stridesBuffer,
                                _copyData: false);
                        InitShaderKernels();
                        this.Zero_();
                    }
                    else
                    {
                        _data = new int[acc];

                        InitCpu(_data, false);
                    }
                }
            }

            // Lastly: let's set the ID of the tensor.
            // IDEs might show a warning, but ref and volatile seems to be working with Interlocked API.

            #pragma warning disable 420
            id = System.Threading.Interlocked.Increment(ref nCreated);

            if (SystemInfo.supportsComputeShaders && shader == null)
            {
                shader = factory.GetShader();
                InitShaderKernels();
            }
        }