yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong HeAdd MLP training examples. (#7550)f28f67d98

master
1.7 KiB64 linesraw
1implementing mlp_sw;
2
3public struct MLVec<int N> : IDifferentiable
4{
5    public NFloat data[N];
6    
7    [Differentiable]
8    public NFloat[N] toArray()
9    {
10        return data;
11    }
12
13    [Differentiable]
14    public static MLVec<N> fromArray(NFloat[N] values)
15    {
16        MLVec<N> result;
17        [ForceUnroll]
18        for (int i = 0; i < N; i++)
19            result.data[i] = values[i];
20        return result;
21    }
22}
23
24MLVec<OutputSize> matMulAdd<int OutputSize, int InputSize>(MLVec<InputSize> input, NFloat* matrix, NFloat* bias)
25{
26    let getMatElem = (int row, int col) => matrix[row*InputSize + col];
27    let getBias = (int idx) => bias[idx];
28    MLVec<OutputSize> result = {};
29    for (int i = 0; i < OutputSize; i++)
30    {
31        NFloat r = getBias(i);
32        for (int j = 0; j < InputSize; j++)
33            r += getMatElem(i, j) * input.data[j];
34        result.data[i] = r;
35    }
36    return result;
37}
38
39MLVec<OutputSize> matMulTransposed<int OutputSize, int InputSize>(MLVec<InputSize> input, NFloat* matrix)
40{
41    let getMatElem = (int row, int col) => matrix[col*OutputSize + row];
42    MLVec<OutputSize> result = {};
43    for (int i = 0; i < OutputSize; i++)
44    {
45        NFloat r = {};
46        for (int j = 0; j < InputSize; j++)
47            r += getMatElem(i, j) * input.data[j];
48        result.data[i] = r;
49    }
50    return result;
51}
52
53void outerProductAccumulate<int M, int N>(MLVec<M> v0, MLVec<N> v1, NFloat* matrix)
54{
55    for (int i = 0; i < M; i++)
56    {
57        for (int j = 0; j < N; j++)
58        {
59            let elem = v0.data[i] * v1.data[j];
60            half original;
61            InterlockedAddF16Emulated(matrix + (i*N + j), elem, original);
62        }
63    }
64}