Example #1
0
        public bool Compile <TVectorKernel>(int vectorLength, string code, out IRunnable <TVectorKernel> result)
            where TVectorKernel : unmanaged, IEquatable <TVectorKernel>, IComparable <TVectorKernel>, IConvertible
        {
            ITermShape input  = new DeviceTensor(OpenFirstDevice(), CreateShape <TVectorKernel>(vectorLength), "I");
            ITermShape output = new DeviceTensor(OpenFirstDevice(), CreateShape <TVectorKernel>(vectorLength), "O");

            return(Compile(input, output, code, out result));
        }
Example #2
0
        public RunStatus Run(IVariable <T> output, ref IVariable <T> gradient, params IVariable <T>[] input)
        {
            RunStatus s = Run(output, input);

            if (s != RunStatus.Success)
            {
                gradient = null;
                return(s);
            }

            var gc = new Gradient(Context, OutputValue);
            var c  = new Composer(_context);

            GradientTensors = new Dictionary <DeviceTensor, DeviceTensor>();
            for (int i = 0; i < InputTensors.Count; i++)
            {
                var t    = InputTensors[i];
                var grad = gc.ComputeWrt(t);
                if (!grad.IsAllocated)
                {
                    continue;
                }
                c.AddInputPlaceholder(InputTensors[i].Name, InputTensors[i].DimensionCount);
                var gt = new DeviceTensor(t.Device, t.Shape, grad.Name);
                c.AddOutputValue(gt);
                c.AddUpdate(gt, grad);
                GradientTensors.Add(t, gt);
            }

            var gf    = c.BuildFunction();
            var ginv  = new Invoker <int>(_context, gf, InputTensors.ToArray(), GradientTensors.Values.ToArray());
            var ginvc = ginv.Invoke();

            if (!ginvc.IsAllocated)
            {
                gradient = null;
                return(RunStatus.ErrorComputingGradient);
            }

            var gv = GradientTensors.First().Value.CreateView <T>(MemoryMapType.Retain);

            if (!gv.CopyToAndFree(gradient.Span))
            {
                gv.Free();
                gradient = null;
                return(RunStatus.ErrorComputingGradient);
            }

            return(RunStatus.Success);
        }
Example #3
0
        public bool Compile <TVectorKernel>(int vectorCount, int vectorLength, string code,
                                            out IRunnable <TVectorKernel> result)
            where TVectorKernel : unmanaged, IEquatable <TVectorKernel>, IComparable <TVectorKernel>, IConvertible
        {
            var inputs = new ITermShape[vectorCount];

            for (int i = 0; i < inputs.Length; i++)
            {
                inputs[i] = new DeviceTensor(OpenFirstDevice(), CreateShape <TVectorKernel>(vectorLength),
                                             "I" + i);
            }

            ITermShape output = new DeviceTensor(OpenFirstDevice(), CreateShape <TVectorKernel>(vectorLength), "O");

            return(Compile(inputs, output, code, out result));
        }
Example #4
0
 public Gradient CreateGradient(DeviceTensor variable)
 {
     return(new Gradient(_context, variable));
 }
Example #5
0
 public DeviceTensorView(DeviceTensor variable, MemoryMapType mapType)
     : base(variable.DeviceBuffer, mapType == MemoryMapType.Discard ? true : false)
 {
     _Tensor = variable;
 }
Example #6
0
 public Invoker(Context ctx, Function f, DeviceTensor output, params DeviceTensor[] input)
     : this(ctx, f, input, new DeviceTensor[] { output })
 {
 }