|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using JetBrains.Annotations; |
| 4 | +using NeuralNetworkNET.APIs.Structs; |
| 5 | +using NeuralNetworkNET.Extensions; |
| 6 | +using NeuralNetworkNET.Networks.Activations.Delegates; |
| 7 | + |
| 8 | +namespace NeuralNetworkNET.cpuDNN |
| 9 | +{ |
| 10 | + public static class CpuDnn |
| 11 | + { |
| 12 | + #region Activation |
| 13 | + |
| 14 | + public static unsafe void ActivationForward(in Tensor x, [NotNull] ActivationFunction f, in Tensor y) |
| 15 | + { |
| 16 | + // Setup |
| 17 | + int n = x.Entities, l = x.Length; |
| 18 | + if (!y.MatchShape(x)) throw new ArgumentException("The target tensor must have the same shape as the input"); |
| 19 | + float* py = y, px = x; |
| 20 | + |
| 21 | + // Execute the activation in parallel |
| 22 | + void Kernel(int i) |
| 23 | + { |
| 24 | + int offset = i * l; |
| 25 | + for (int j = 0; j < l; j++) |
| 26 | + { |
| 27 | + int target = offset + j; |
| 28 | + py[target] = f(px[target]); |
| 29 | + } |
| 30 | + } |
| 31 | + Parallel.For(0, n, Kernel).AssertCompleted(); |
| 32 | + } |
| 33 | + |
| 34 | + public static unsafe void ActivationBackward(in Tensor x, in Tensor y, [NotNull] ActivationFunction f_, in Tensor dx) |
| 35 | + { |
| 36 | + // Check |
| 37 | + if (!y.MatchShape(x)) throw new ArgumentException(nameof(y), "The input tensors must have the same shape"); |
| 38 | + if (!dx.MatchShape(x)) throw new ArgumentException(nameof(y), "The output tensor must have the same shape as the input"); |
| 39 | + int |
| 40 | + n = x.Entities, |
| 41 | + l = x.Length; |
| 42 | + float* px = x, py = y, pdx = dx; |
| 43 | + |
| 44 | + // Loop in parallel |
| 45 | + void Kernel(int i) |
| 46 | + { |
| 47 | + int offset = i * l; |
| 48 | + for (int j = 0; j < l; j++) |
| 49 | + { |
| 50 | + int target = offset + j; |
| 51 | + pdx[target] = f_(px[target]) * py[target]; |
| 52 | + } |
| 53 | + } |
| 54 | + Parallel.For(0, n, Kernel).AssertCompleted(); |
| 55 | + } |
| 56 | + |
| 57 | + #endregion |
| 58 | + } |
| 59 | +} |
0 commit comments