Example #1
0
        public static FloatTensor Ones(FloatTensorFactory factory, int[] dims)
        {
            FloatTensor result = factory.ctrl.floatTensorFactory.Create(dims);

            result.Add(1.0F, inline: true);
            return(result);
        }
Example #2
0
        public SyftController(ComputeShader _shader)
        {
            shader = _shader;

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

            models = new Dictionary <int, Model> ();
        }
Example #3
0
        // opposite of batchify
        public static FloatTensor Concatenate(FloatTensorFactory factory, List <int> tensor_ids, int axis, FloatTensor result = null)
        {
            FloatTensor[] tensors = new FloatTensor[tensor_ids.Count];
            for (int i = 0; i < tensor_ids.Count; i++)
            {
                tensors[i] = factory.Get(tensor_ids[i]);
            }

            return(Concatenate(factory, tensors, axis, result));
        }
Example #4
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>();
        }
Example #5
0
        public static FloatTensor Random(FloatTensorFactory factory, int[] dims, int random_seed = 0)
        {
            int dims_prod = 1;

            foreach (int dim in dims)
            {
                dims_prod *= dim;
            }
            FloatTensor result = factory.ctrl.floatTensorFactory.Create(dims);

            if (random_seed > 0)
            {
                UnityEngine.Random.InitState(random_seed);
            }
            for (int i = 0; i < dims_prod; i++)
            {
                result.Data[i] = UnityEngine.Random.value;
            }
            return(result.View(dims));
        }
Example #6
0
        public static FloatTensor Randn(FloatTensorFactory factory, int[] dims, int random_seed = 0)
        {
            int dims_prod = 1;

            foreach (int dim in dims)
            {
                dims_prod *= dim;
            }
            FloatTensor result = factory.ctrl.floatTensorFactory.Create(dims);

            if (random_seed > 0)
            {
                UnityEngine.Random.InitState(random_seed);
            }
            for (int i = 0; i < dims_prod; i++)
            {
                // Reference: https://stackoverflow.com/questions/218060/random-gaussian-variables

                double u1 = 1.0F - UnityEngine.Random.value;
                double u2 = 1.0F - UnityEngine.Random.value;
                result.Data[i] = Convert.ToSingle(Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2));
            }
            return(result.View(dims));
        }
Example #7
0
        public void init(FloatTensorFactory _factory,
                         int[] _shape,
                         float[] _data              = null,
                         ComputeBuffer _dataBuffer  = null,
                         ComputeBuffer _shapeBuffer = null,
                         ComputeShader _shader      = null,
                         bool _copyData             = true,
                         bool _dataOnGpu            = false,
                         bool _autograd             = false,
                         bool _keepgrads            = false,
                         string _creation_op        = null)
        {
            factory     = _factory;
            dataOnGpu   = _dataOnGpu;
            autograd    = _autograd;
            keepgrads   = _keepgrads;
            creation_op = _creation_op;

            InitGraph();

            if (autograd)
            {
                InitAutograd();
            }

            // 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 && _dataBuffer == null)
            {
                InitCpu(_data: _data, _copyData: _copyData);
            }
            else if (_dataBuffer != null && _shapeBuffer != null && SystemInfo.supportsComputeShaders && _data == null)
            {
                // looks like we have GPU data being passed in... initialize a GPU tensor.

                InitGpu(_shader, _dataBuffer, _shapeBuffer, _copyData);
                initShaderKernels();
            }
            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, _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);

                        _dataBuffer = new ComputeBuffer(size, sizeof(float));

                        InitGpu(_shader: _shader, _dataBuffer: _dataBuffer, _shapeBuffer: _shapeBuffer,
                                _copyData: false);
                        initShaderKernels();
                        this.Zero_();
                    }
                    else
                    {
                        _data = new float[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);

            //controller.addTensor(this);
            if (SystemInfo.supportsComputeShaders && shader == null)
            {
                shader = factory.GetShader();
            }
        }
Example #8
0
        public static FloatTensor Concatenate(FloatTensorFactory factory, FloatTensor[] tensors_inp, int axis, FloatTensor result = null)
        {
            FloatTensor first = tensors_inp[0];

            FloatTensor[] tensors = new FloatTensor[tensors_inp.Length - 1];
            for (int i = 0; i < tensors_inp.Length - 1; i++)
            {
                tensors[i] = tensors_inp[i + 1];
            }

            if (first.DataOnGpu)
            {
                throw new NotImplementedException("Can't concatenate GPU tensors yet");
            }

            int num_new_rows = 0;

            List <IntTensor> int_tensors_for_index_add = new List <IntTensor>();

            int[] first_indices = new int[first.Shape[axis]];
            for (int i = 0; i < first.Shape[axis]; i++)
            {
                first_indices[i] = i + num_new_rows;
            }
            int_tensors_for_index_add.Add(
                factory.ctrl.intTensorFactory.Create(_shape: new int[1] {
                first.Shape[axis]
            }, _data: first_indices));

            num_new_rows += first.Shape[axis];

            foreach (FloatTensor tensor in tensors)
            {
                if (tensor.Shape.Length != first.Shape.Length)
                {
                    throw new InvalidOperationException("Tensors do not have the same number of dimensions.");
                }

                for (int i = 0; i < tensor.Shape.Length; i++)
                {
                    if (i != axis)
                    {
                        if (tensor.Shape[i] != first.Shape[i])
                        {
                            throw new InvalidOperationException("Tensors do not have the same shape.");
                        }
                    }
                }

                if (tensor.DataOnGpu != first.DataOnGpu)
                {
                    throw new InvalidOperationException("All tensors must be on the same device...");
                }

                int[] indices = new int[tensor.Shape[axis]];
                for (int i = 0; i < tensor.Shape[axis]; i++)
                {
                    indices[i] = i + num_new_rows;
                }
                int_tensors_for_index_add.Add(
                    factory.ctrl.intTensorFactory.Create(_shape: new int[1] {
                    tensor.Shape[axis]
                }, _data: indices));


                num_new_rows += tensor.Shape[axis];
            }

            Debug.Log("Num new Rows:" + num_new_rows);

            int[] concat_shape = new int[first.Shape.Length];

            for (int i = 0; i < first.Shape.Length; i++)
            {
                if (i == axis)
                {
                    concat_shape[i] = num_new_rows;
                }
                else
                {
                    concat_shape[i] = first.Shape[i];
                }
            }

            result = first.HookGraph(ref result, tensor_inputs: tensors, creation_op: "concatenate_" + axis, inline: false, resultShape: concat_shape, indices: int_tensors_for_index_add.ToArray());

            if (axis != 0)
            {
                result.IndexAdd(int_tensors_for_index_add[0], axis, first, inline: true);

                for (int i = 0; i < tensors.Length; i++)
                {
                    result.IndexAdd(int_tensors_for_index_add[i + 1], axis, tensors[i], inline: true);
                }
            }
            else
            {
                //Debug.Log("Result Size:" + result.Size);
                //Debug.Log("First Size:" + first.Size);

                int result_i = 0;

                for (int i = 0; i < first.Data.Length; i++)
                {
                    result.Data[result_i] = first.Data[i];
                    result_i += 1;
                }

                foreach (FloatTensor tensor in tensors)
                {
                    //Debug.Log("Tensor Size:" + tensor.Size);
                    for (int i = 0; i < tensor.Data.Length; i++)
                    {
                        result.Data[result_i] = tensor.Data[i];
                        result_i += 1;
                    }
                }
            }
            return(result);
        }
Example #9
0
        public static FloatTensor Zeros(FloatTensorFactory factory, int[] dims)
        {
            FloatTensor result = factory.ctrl.floatTensorFactory.Create(dims);

            return(result);
        }
Example #10
0
 // opposite of batchify
 public static FloatTensor Concatenate(FloatTensorFactory factory, List <FloatTensor> tensors, int axis, FloatTensor result = null)
 {
     return(Concatenate(factory, tensors.ToArray(), axis, result));
 }