| implementing neural; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public struct InlineVector<T, int N> : IVector<T, N> |
| where T : __BuiltinFloatingPointType |
| where T.Differential == T |
| { |
| |
| public typealias Differential = InlineVector<T.Differential, N>; |
|
|
| |
| public static const int Size = N; |
|
|
| |
| |
| |
| |
| [DerivativeMember(Differential.data)] |
| internal T[N] data; |
|
|
| |
| public __init() { data = {}; } |
|
|
| |
| |
| |
| |
| public __init(T value) { |
| [ForceUnroll] |
| for (int i = 0; i < N; i++) |
| data[i] = value; |
| } |
|
|
| |
| |
| |
| |
| public __init(T[N] data) { this.data = data; } |
|
|
| |
| |
| |
| |
| public __init(This other) { this.data = other.data; } |
|
|
| |
| |
| |
| |
| |
| public __subscript(int index) -> T |
| { |
| get() { return this.data[index]; } |
| set() { this.data[index] = newValue; } |
| } |
|
|
| |
| [BackwardDerivative(linearTransformBwd)] |
| public OutputVector linearTransform<int OutputSize, Storage, OutputVector>( |
| Storage weightStorage, |
| no_diff Storage.Address weightAddress) |
| where Storage : IStorage<T> |
| where Storage.Differential : IStorage<T.Differential> |
| where Storage.Address == Storage.Differential.Address |
| where OutputVector : IVector<T, OutputSize> |
| { |
| OutputVector output = OutputVector(); |
|
|
| [MaxIters(OutputSize)] |
| for (int row = 0; row < OutputSize; row++) |
| { |
| |
| 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; |
| } |
|
|
| |
| [BackwardDerivative(linearTransformBwd)] |
| public OutputVector linearTransform<int OutputSize, Storage, OutputVector>( |
| Storage weightStorage, |
| Storage biasStorage, |
| no_diff Storage.Address weightAddress, |
| no_diff Storage.Address biasAddress) |
| where Storage : IStorage<T> |
| where Storage.Differential : IStorage<T.Differential> |
| where Storage.Address == Storage.Differential.Address |
| where OutputVector : IVector<T, OutputSize> |
| { |
| |
| OutputVector output = this.linearTransform<OutputSize, Storage, OutputVector>(weightStorage, weightAddress); |
|
|
| |
| [ForceUnroll] |
| for (int i = 0; i < OutputSize; i++) |
| { |
| let elementOffset = Storage.getOffset(biasAddress, i); |
| output[i] = output[i] + biasStorage.read(elementOffset); |
| } |
|
|
| return output; |
| } |
|
|
| |
| static void linearTransformBwd<int OutputSize, Storage, OutputVector>( |
| inout DifferentialPair<This> dthis, |
| DifferentialPtrPair<Storage> dWeightStorage, |
| no_diff Storage.Address dWeightAddress, |
| OutputVector.Differential doutput) |
| where Storage : IStorage<T> |
| where Storage.Differential : IStorage<T.Differential> |
| where Storage.Address == Storage.Differential.Address |
| where OutputVector : IVector<T, OutputSize> |
| { |
| |
| 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); |
| } |
| } |
|
|
| |
| |
| [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<This>(dthis.p, d); |
| } |
|
|
| |
| static void linearTransformBwd<int OutputSize, Storage, OutputVector>( |
| inout DifferentialPair<This> dthis, |
| DifferentialPtrPair<Storage> dWeightStorage, |
| DifferentialPtrPair<Storage> dBiasStorage, |
| no_diff Storage.Address dWeightAddress, |
| no_diff Storage.Address dBiasAddress, |
| OutputVector.Differential doutput) |
| where Storage : IStorage<T> |
| where Storage.Differential : IStorage<T.Differential> |
| where Storage.Address == Storage.Differential.Address |
| where OutputVector : IVector<T, OutputSize> |
| { |
| |
| linearTransformBwd<OutputSize, Storage, OutputVector>(dthis, dWeightStorage, dWeightAddress, doutput); |
|
|
| |
| [ForceUnroll] |
| for (int i = 0; i < OutputSize; i++) |
| { |
| let biasOffset = Storage.getOffset(dBiasAddress, i); |
| dBiasStorage.d.atomicAdd(biasOffset, doutput[i]); |
| } |
| } |
|
|
| |
| [BackwardDerivative(linearTransformBwd)] |
| public OutputVector linearTransform<int OutputSize, Address, OutputVector>( |
| Address weightAddress) |
| where Address : IPointerLikeAddress<T> |
| where Address.Differential : IPointerLikeAddress<T.Differential> |
| where OutputVector : IVector<T, OutputSize> |
| { |
| var output = OutputVector(); |
|
|
| |
| [MaxIters(OutputSize)] |
| for (int row = 0; row < OutputSize; row++) |
| { |
| |
| let rowAddr = weightAddress.getOffset(row * N); |
| [ForceUnroll] |
| for (int col = 0; col < N; col++) |
| { |
| output[row] += data[col] * rowAddr[col]; |
| } |
| } |
| return output; |
| } |
|
|
| |
| [BackwardDerivative(linearTransformBwd)] |
| public OutputVector linearTransform<int OutputSize, Address, OutputVector>( |
| Address weightAddress, |
| Address biasAddress) |
| where Address : IPointerLikeAddress<T> |
| where Address.Differential : IPointerLikeAddress<T.Differential> |
| where OutputVector : IVector<T, OutputSize> |
| { |
| |
| OutputVector output = this.linearTransform<OutputSize, Address, OutputVector>(weightAddress); |
|
|
| [ForceUnroll] |
| for (int i = 0; i < OutputSize; i++) |
| output[i] = output[i] + biasAddress[i]; |
|
|
| return output; |
| } |
|
|
| |
| static public void linearTransformBwd<int OutputSize, Address, OutputVector>( |
| inout DifferentialPair<This> dthis, |
| DifferentialPtrPair<Address> dparameters, |
| OutputVector.Differential doutput) |
| where Address : IPointerLikeAddress<T> |
| where Address.Differential : IPointerLikeAddress<T.Differential> |
| where OutputVector : IVector<T, OutputSize> |
| where OutputVector.Differential : IVector<T.Differential, OutputSize> |
| { |
| |
| 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); |
| } |
| } |
|
|
| |
| |
| [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<This>(dthis.p, d); |
| } |
|
|
| |
| static public void linearTransformBwd<int OutputSize, Address, OutputVector>( |
| inout DifferentialPair<This> dthis, |
| DifferentialPtrPair<Address> dWeightAddress, |
| DifferentialPtrPair<Address> dBiasAddress, |
| OutputVector.Differential doutput) |
| where Address : IPointerLikeAddress<T> |
| where Address.Differential : IPointerLikeAddress<T.Differential> |
| where OutputVector : IVector<T, OutputSize> |
| { |
| |
| linearTransformBwd<OutputSize, Address, OutputVector>(dthis, dWeightAddress, doutput); |
|
|
| let biasOffset = dBiasAddress.d.getOffset(0); |
| |
| [ForceUnroll] |
| for (int i = 0; i < OutputSize; i++) |
| { |
| biasOffset.atomicAdd(i, doutput[i]); |
| } |
| } |
| } |
|
|