yum-mirror/slang

Making it easier to work with shaders

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

Harsh Aggarwal (NVIDIA)Fix 7723 - Add autodiff tests (#7919)d10732742

master
1.5 KiB63 linesraw
1//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK): -slang -compute -shaderobj -output-using-type
2//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-cuda -compute -shaderobj -output-using-type
3//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-vk -compute -shaderobj -output-using-type
4
5#define DO_FLOOR
6#define MANUAL_DERIVATIVE
7
8#ifndef MANUAL_DERIVATIVE
9[BackwardDifferentiable]
10#endif
11float unusual_norm<let N : uint>(Array<float, N> x)
12{
13  float result = 0.f;
14  #ifndef MANUAL_DERIVATIVE
15  [ForceUnroll]
16  #endif
17  for(uint i = 0; i < N; i++)
18  {
19  #ifdef DO_FLOOR
20    result += pow(floor(x[i]), 4);
21  #else
22    result += pow(x[i], 4);
23  #endif
24  }
25  return result;
26}
27
28#ifdef MANUAL_DERIVATIVE
29[BackwardDerivativeOf(unusual_norm)]
30void unusual_norm_bwd<let N : uint>(inout DifferentialPair<Array<float, N>> x, float dResult)
31{
32  Array<float, N> derivatives;
33  for(uint i = 0; i < N; i++)
34  {
35    derivatives[i] = 4.f * dResult * pow(x.p[i], 3);
36  }
37  x = diffPair(x.p, derivatives);
38}
39#endif
40
41//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=g_out
42
43RWStructuredBuffer<float> g_out;
44[shader("compute")]
45[numthreads(1, 1, 1)]
46void computeMain(uint3 dtid : SV_DispatchThreadID)
47{
48  Array<float, 5> x;
49  for(uint i = 0; i < 5; i++)
50  {
51    x[i] = float(i + dtid.x);
52  }
53  
54  DifferentialPair<Array<float, 5>> x_pd = diffPair(x, {});
55  bwd_diff(unusual_norm)(x_pd, 1.0f);
56  for (int i = 0; i < 5; i++)
57    g_out[i] = x_pd.d[i];
58    // CHECK: 0.0
59    // CHECK: 4.0
60    // CHECK: 32.0
61    // CHECK: 108.0
62    // CHECK: 256.0
63}