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)); }
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); }
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)); }
public DeviceTensorView(DeviceTensor variable, MemoryMapType mapType) : base(variable.DeviceBuffer, mapType == MemoryMapType.Discard ? true : false) { _Tensor = variable; }
public Invoker(Context ctx, Function f, DeviceTensor output, params DeviceTensor[] input) : this(ctx, f, input, new DeviceTensor[] { output }) { }
public Gradient CreateGradient(DeviceTensor variable) { return(new Gradient(_context, variable)); }