Skip to content

Commit 17727e8

Browse files
authored
[0029] Update example code to use real API (microsoft#472)
* [0029] Update example code to use real API This attempts to replicate the spirit of the original sample, but using the actual API as described in proposal 0031.
1 parent 731c6cb commit 17727e8

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

proposals/0029-cooperative-vector.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ void ps_main(args) // args: texture, normal, position
7777
7878
**Neural Network based shader**
7979
80-
Below shader is in HLSL-like psuedocode, to highlight the idea of what replacing physical computations with a neural network based evaluation looks like. The exact syntax for the new intrinsics is intentionally skipped to keep it simple, later sections contain examples with the correct syntax and sample descriptors.
80+
The shader below shows the idea of what replacing physical computations with a
81+
neural network based evaluation looks like. Some details have been omitted, but
82+
this should give a sense of how these new operations can be used.
8183
82-
> NOTE: see proposal [0031] for the latest on the HLSL API.
84+
> NOTE: see proposal [0031] for full details on the HLSL API.
8385
8486
```c++
8587
ByteAddressBuffer inputMatrix0;
@@ -89,29 +91,38 @@ ByteAddressBuffer biasVector1;
8991
9092
void ps_main(args) // args: texture, normal, position
9193
{
94+
using namespace dx::linalg;
95+
9296
PreProcessing(args);
9397
// Neural Network computes the output vector
9498
// using the same input args and trained data
9599
// in the form of matrices and bias vectors.
96100
97101
// The input vector is computed from the shader input
98-
vector<uint32_t, M> inputVector = SomeFunction(args);
102+
vector<uint32_t, INPUT_SIZE> inputVector = SomeFunction(args);
99103
100104
// Below the physical calculations are replaced by NN evaluation
101105
// the Matrix and Bias are trained offline and loaded to memory
102106
103107
// layer0 = inputVector*inputMatrix + biasVector0
104108
// The matrix and bias are loaded from memory at offsets : moffset0 and boffset0
105-
vector<uint32_t, K> layer0 = MatrixVectorMulAdd(inputVector, inputMatrix0, moffset0, biasVector0, boffset0);
109+
MatrixRef<DATA_TYPE_UINT32, N, INPUT_SIZE, MATRIX_LAYOUT_MUL_OPTIMAL> M0 = { inputMatrix0, moffset0, 0 };
110+
VectorRef<DATA_TYPE_UINT32> B0 = { biasVector0, boffset0 };
111+
112+
vector<uint32_t, N> layer0 = MulAdd<uint32_t>(M0, MakeInterpretedVector<DATA_TYPE_UINT32>(inputVector), B0);
106113
layer0 = max(layer0,0); // Apply activation function
107114
108-
// layer0 = inputVector*inputMatrix0 + biasVector0
115+
// layer1 = inputVector*inputMatrix0 + biasVector0
109116
// The matrix and bias are loaded from memory at offsets : moffset1 and boffset1
110-
vector<uint32_t, K> layer1 = MatrixVectorMulAdd(layer0, inputMatrix0, moffset1, biasVector0, boffset1);
117+
MatrixRef<DATA_TYPE_UINT32, N, N, MATRIX_LAYOUT_MUL_OPTIMAL> M1 = { inputMatrix0, moffset1, 0 };
118+
VectorRef<DATA_TYPE_UINT32> B1 = { biasVector0, boffset1 };
119+
vector<uint32_t, K> layer1 = MulAdd<uint32_t>(M1, MakeInterpretedVector<DATA_TYPE_UINT32>(layer0), B1);
111120
layer1 = max(layer1,0); // Apply activation function
112121
113122
// output = layer1*inputMatrix1 + biasVector1
114-
vector<uint32_t, N> output = MatrixVectorMulAdd(layer1, inputMatrix1, biasVector1);
123+
MatrixRef<DATA_TYPE_UINT32, OUTPUT_SIZE, N, MATRIX_LAYOUT_MUL_OPTIMAL> M2 = { inputMatrix1, 0, 0 };
124+
VectorRef<DATA_TYPE_UIN32> B2 = { biasVector1, 0 };
125+
vector<uint32_t, OUTPUT_SIZE> output = MulAdd<uint32_t>(M2, MakeInterpretedVector<DATA_TYPE_UINT32>(layer1), B2);
115126
116127
output = exp(output);
117128

0 commit comments

Comments
 (0)