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