yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f28f67d98
master
1implementing mlp; 2 3// A wrapper of CoopVec<T> to allow it being used in differentiable context. 4// 5public struct MLVec<int N> : IDifferentiable 6{ 7 public CoopVec<NFloat, N> data; 8 public typealias Differential = MLVec<N>; 9 10 public static MLVec<N> fromArray(NFloat[N] values) 11 { 12 MLVec<N> result; 13 [ForceUnroll] 14 for (int i = 0; i < N; i++) 15 result.data[i] = values[i]; 16 return result; 17 } 18 19 internal static NFloat[N] coopVecToArray(CoopVec<NFloat, N> v) 20 { 21 NFloat[N] arr; 22 [ForceUnroll] 23 for (int i = 0; i < N; i++) 24 arr[i] = v[i]; 25 return arr; 26 } 27 28 [BackwardDerivativeOf(fromArray)] 29 internal static void fromArrayBwd(inout DifferentialPair<NFloat[N]> values, MLVec<N> dResult) 30 { 31 values = diffPair(values.p, coopVecToArray(dResult.data)); 32 } 33 34 internal static NFloat[N] toArray(MLVec<N> vec) 35 { 36 return coopVecToArray(vec.data); 37 } 38 39 [BackwardDerivativeOf(toArray)] 40 internal static void toArrayBwd(inout DifferentialPair<MLVec<N>> vec, NFloat[N] dResult) 41 { 42 vec = diffPair(vec.p, MLVec<N>.fromArray(dResult)); 43 } 44 45 [Differentiable] 46 public NFloat[N] toArray() 47 { 48 return toArray(this); 49 } 50 51 public override static Differential dadd(Differential d0, Differential d1) 52 { 53 return {d0.data + d1.data}; 54 } 55 public override static Differential dmul<U:__BuiltinRealType>(U s, Differential d) 56 { 57 return {d.data * __realCast<NFloat>(s)}; 58 } 59 public override static Differential dzero() 60 { 61 return {}; 62 } 63}