implementing neural; /** Concrete implementation of IVector storing elements inline (on stack/registers). InlineVector stores all elements in a fixed-size array, making it suitable for small vectors that can fit in registers or stack memory. Supports automatic differentiation for gradient computation in neural networks. @param T The element type @param N The vector size (compile-time constant). @remarks Type constraints: - `T` must conform to `__BuiltinFloatingPointType` (float, double, half, etc.) - `T.Differential` must conform to `__BuiltinFloatingPointType` for automatic differentiation @category neural */ public struct InlineVector : IVector where T : __BuiltinFloatingPointType where T.Differential == T { /// The differential type for automatic differentiation. public typealias Differential = InlineVector; /// The compile-time size of the vector. public static const int Size = N; /** Internal storage for vector elements. @remarks Marked as derivative member to enable automatic differentiation. */ [DerivativeMember(Differential.data)] internal T[N] data; /// Default constructor - initializes all elements to zero. public __init() { data = {}; } /** Scalar broadcast constructor - fills all elements with the same value. @param[in] value The value to broadcast to all elements. */ public __init(T value) { [ForceUnroll] for (int i = 0; i < N; i++) data[i] = value; } /** Array constructor - initializes from an array. @param[in] data Array of N elements to initialize the vector. */ public __init(T[N] data) { this.data = data; } /** Copy constructor. @param[in] other The vector to copy from. */ public __init(This other) { this.data = other.data; } /** Element access operator. @param[in] index The element index (0-based). @return Reference to the element at the given index. */ public __subscript(int index) -> T { get() { return this.data[index]; } set() { this.data[index] = newValue; } } // Linear transformation without bias [BackwardDerivative(linearTransformBwd)] public OutputVector linearTransform( Storage weightStorage, no_diff Storage.Address weightAddress) where Storage : IStorage where Storage.Differential : IStorage where Storage.Address == Storage.Differential.Address where OutputVector : IVector { OutputVector output = OutputVector(); [MaxIters(OutputSize)] for (int row = 0; row < OutputSize; row++) { // get the address of each row of the weight matrix let rowOffset = Storage.getOffset(weightAddress, row * N); [ForceUnroll] for (int col = 0; col < N; col++) { let elementOffset = Storage.getOffset(rowOffset, col); output[row] += data[col] * weightStorage.read(elementOffset); } } return output; } // Linear transformation with bias [BackwardDerivative(linearTransformBwd)] public OutputVector linearTransform( Storage weightStorage, Storage biasStorage, no_diff Storage.Address weightAddress, no_diff Storage.Address biasAddress) where Storage : IStorage where Storage.Differential : IStorage where Storage.Address == Storage.Differential.Address where OutputVector : IVector { // Reuse the unbias matmul method OutputVector output = this.linearTransform(weightStorage, weightAddress); // apply the bias [ForceUnroll] for (int i = 0; i < OutputSize; i++) { let elementOffset = Storage.getOffset(biasAddress, i); output[i] = output[i] + biasStorage.read(elementOffset); } return output; } // Backward of linear transformation without bias static void linearTransformBwd( inout DifferentialPair dthis, DifferentialPtrPair dWeightStorage, no_diff Storage.Address dWeightAddress, OutputVector.Differential doutput) where Storage : IStorage where Storage.Differential : IStorage where Storage.Address == Storage.Differential.Address where OutputVector : IVector { // Derivative of the input is transposed weight matrix times the output differential var d = dthis.d; [MaxIters(OutputSize)] for (int j = 0; j < OutputSize; j++) { T.Differential dy = doutput[j]; [ForceUnroll] for (int i = 0; i < N; i++) { Storage.Address elementOffset = Storage.getOffset(dWeightAddress, i * OutputSize + j); T.Differential prod = T.Differential.dmul(dWeightStorage.p.read(elementOffset), dy); d[i] = T.Differential.dadd(d[i], prod); } } // Derivative of the weights is the outer product of the input and the output differential // dW = dOutput * dInput^T [MaxIters(OutputSize)] for (int row = 0; row < OutputSize; row++) { let rowOffset = Storage.getOffset(dWeightAddress, row * N); T.Differential dy = doutput[row]; [ForceUnroll] for (int col = 0; col < N; col++) { let x = dthis.p[col]; let elementOffset = Storage.getOffset(rowOffset, col); T.Differential prod = T.Differential.dmul(x, dy); dWeightStorage.d.atomicAdd(elementOffset, prod); } } dthis = DifferentialPair(dthis.p, d); } // Backward of linear transformation with bias static void linearTransformBwd( inout DifferentialPair dthis, DifferentialPtrPair dWeightStorage, DifferentialPtrPair dBiasStorage, no_diff Storage.Address dWeightAddress, no_diff Storage.Address dBiasAddress, OutputVector.Differential doutput) where Storage : IStorage where Storage.Differential : IStorage where Storage.Address == Storage.Differential.Address where OutputVector : IVector { // Reuse the unbias backward method linearTransformBwd(dthis, dWeightStorage, dWeightAddress, doutput); // Derivative of the bias is the same as the output differential [ForceUnroll] for (int i = 0; i < OutputSize; i++) { let biasOffset = Storage.getOffset(dBiasAddress, i); dBiasStorage.d.atomicAdd(biasOffset, doutput[i]); } } // Linear transformation without bias (Bindless storage) [BackwardDerivative(linearTransformBwd)] public OutputVector linearTransform( Address weightAddress) where Address : IPointerLikeAddress where Address.Differential : IPointerLikeAddress where OutputVector : IVector { var output = OutputVector(); // output = W * input [MaxIters(OutputSize)] for (int row = 0; row < OutputSize; row++) { // get the address of each row of the weight matrix let rowAddr = weightAddress.getOffset(row * N); [ForceUnroll] for (int col = 0; col < N; col++) { output[row] += data[col] * rowAddr[col]; } } return output; } // Linear transformation with bias (Bindless storage) [BackwardDerivative(linearTransformBwd)] public OutputVector linearTransform( Address weightAddress, Address biasAddress) where Address : IPointerLikeAddress where Address.Differential : IPointerLikeAddress where OutputVector : IVector { // Reuse the unbias matmul method OutputVector output = this.linearTransform(weightAddress); [ForceUnroll] for (int i = 0; i < OutputSize; i++) output[i] = output[i] + biasAddress[i]; return output; } // Backward of linear transformation without bias (Bindless storage) static public void linearTransformBwd( inout DifferentialPair dthis, DifferentialPtrPair
dparameters, OutputVector.Differential doutput) where Address : IPointerLikeAddress where Address.Differential : IPointerLikeAddress where OutputVector : IVector where OutputVector.Differential : IVector { // dInput = dW^T * dOutput var d = dthis.d; [MaxIters(OutputSize)] for (int j = 0; j < OutputSize; j++) { let dy = doutput[j]; [ForceUnroll] for (int i = 0; i < N; i++) { T.Differential prod = T.Differential.dmul(dparameters.p[i * OutputSize + j], dy); d[i] = T.Differential.dadd(d[i], prod); } } // Derivative of the weights is the outer product of the input and the output differential // dW = dOutput * dInput^T [MaxIters(OutputSize)] for (int row = 0; row < OutputSize; row++) { let rowAddr = dparameters.d.getOffset(row * N); T.Differential dy = doutput[row]; [ForceUnroll] for (int col = 0; col < N; col++) { let x = dthis.p[col]; T.Differential prod = T.Differential.dmul(x, dy); rowAddr.atomicAdd(col, prod); } } dthis = DifferentialPair(dthis.p, d); } // Backward of linear transformation with bias (Bindless storage) static public void linearTransformBwd( inout DifferentialPair dthis, DifferentialPtrPair
dWeightAddress, DifferentialPtrPair
dBiasAddress, OutputVector.Differential doutput) where Address : IPointerLikeAddress where Address.Differential : IPointerLikeAddress where OutputVector : IVector { // Reuse the unbias backward method linearTransformBwd(dthis, dWeightAddress, doutput); let biasOffset = dBiasAddress.d.getOffset(0); // dBias = dOutput [ForceUnroll] for (int i = 0; i < OutputSize; i++) { biasOffset.atomicAdd(i, doutput[i]); } } }