yum-mirror/slang

Making it easier to work with shaders

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

kaizhangNVno_diff diagnostics improvement (#7655)a670bafc1

master
1.5 KiB48 linesraw
1//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):
2
3
4// Similar to const-to-nodiff-function-diagnostic-improvement.slang, but with a CoopVec type
5// to reproduce a more realistic scenario.
6extension<T : __BuiltinFloatingPointType, let K : int> CoopVec<T, K> : IDifferentiable
7{
8    typealias Differential = CoopVec<T, K>;
9};
10
11[BackwardDerivativeOf(exp)]
12void exp_BackwardAutoDiff<T : __BuiltinFloatingPointType, let K : int>(inout DifferentialPair<CoopVec<T, K>> p0, CoopVec<T, K>.Differential dResult)
13{
14    p0 = diffPair(p0.p, dResult * exp(p0.p));
15}
16
17[Differentiable]
18CoopVec<T, K> eval<T : __BuiltinFloatingPointType, let K : int>(CoopVec<T, K> x)
19{
20    // CHECK-NOT: ([[# @LINE+1]]): error 41020
21    return exp(x) - CoopVec<T, K>(1.);
22}
23
24[Differentiable]
25CoopVec<T, K> eval1<T : __BuiltinFloatingPointType, let K : int>(CoopVec<T, K> x)
26{
27    // test.slang(25): error 41020: derivative cannot be propagated through call to non-backward-differentiable function `CoopVec.$init`, use 'no_diff' to clarify intention.
28    // CHECK: ([[# @LINE+1]]): error 41020
29    return exp(x) - CoopVec<T, K>(x[0]);
30}
31
32
33RWStructuredBuffer<float> output;
34
35[shader("compute")]
36[numthreads(1,1,1)]
37void computeMain(uint id : SV_DispatchThreadID)
38{
39    var x = diffPair(CoopVec<float, 2>(2.0f), CoopVec<float, 2>(1.0f));
40    bwd_diff(eval)(x, CoopVec<float, 2>(1.0f));
41
42    output[0] = x.d[0];
43
44    var x1 = diffPair(CoopVec<float, 2>(2.0f), CoopVec<float, 2>(1.0f));
45    bwd_diff(eval1)(x1, CoopVec<float, 2>(1.0f));
46    output[1] = x1.d[1];
47}
48