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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type
//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<float> outputBuffer;
typedef DifferentialPair<float> dpfloat;
interface IFoo
{
// Since IFoo is not inheriting from IDifferentiable,
// The `this` parameter should be considered as `no_diff` when `getVal`
// is called through this interface.
[ForwardDifferentiable]
float getVal(float y);
}
struct A : IDifferentiable, IFoo
{
float x;
// This `getVal` implementation will have `this` parameter treated as
// differentiable. In order for this method to satisfy the `IFoo.getVal`
// requirement, we need to synthesize a method with `[NoDiffThis]` attribute
// that calls this.
[ForwardDifferentiable]
float getVal(float y){ return x * x + y * y; }
}
[ForwardDifferentiable]
static float f<T:IFoo>(T obj, float y)
{
return obj.getVal(y);
}
[ForwardDifferentiable]
static float f2(IFoo obj, float y)
{
return obj.getVal(y);
}
[ForwardDifferentiable]
float f3(A obj, float y)
{
return obj.getVal(y);
}
[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
A a;
a.x = 2.0;
A.Differential ad;
ad.x = 1.0;
let rs = __fwd_diff(f)(a, dpfloat(3.0, 1.0));
outputBuffer[0] = rs.p; // Expect: 13.0
outputBuffer[1] = rs.d; // Expect: 6.0
let rs2 = __fwd_diff(f2)(a, dpfloat(3.0, 1.0));
outputBuffer[2] = rs2.p; // Expect: 13.0
outputBuffer[3] = rs2.d; // Expect: 6.0
// By calling A.getVal directly, we will invoke the implementation
// that differentiates the `this` argument.
let rs3 = __fwd_diff(f3)(DifferentialPair<A>(a, ad), dpfloat(3.0, 1.0));
outputBuffer[4] = rs3.p; // Expect: 13.0
outputBuffer[5] = rs3.d; // Expect: 10.0
}
|