summaryrefslogtreecommitdiffstats
path: root/tests/autodiff-dstdlib/dstdlib-inverse-hyperbolic.slang
blob: e71de962c96f7db70aa6746099be1f59324027e5 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-vk -compute -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -dx12 -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-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;

[BackwardDifferentiable]
float diffAsinh(float x)
{
    return asinh(x);
}

[BackwardDifferentiable]
float diffAcosh(float x)
{
    return acosh(x);
}

[BackwardDifferentiable]
float diffAtanh(float x)
{
    return atanh(x);
}

[numthreads(1, 1, 1)]
[shader("compute")]
void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
{
    var index = 0U;

    let sinhValue = 2;
    {
        // Expected: 1 / sqrt(x^2 + 1) = 1 / sqrt(4 + 1) = 0.447214
        // CHECK: 0.447214
        dpfloat dpx = dpfloat(sinhValue, 1.0);
        dpfloat res = __fwd_diff(diffAsinh)(dpx);
        outputBuffer[index++] = res.d;
    }
    {
        // Check backward mode agrees with forward
        // CHECK: 0.447214
        dpfloat dpx = diffPair(sinhValue);
        __bwd_diff(diffAsinh)(dpx, 1.0);
        outputBuffer[index++] = dpx.d;
    }

    let coshValue = 4;
    {
        // Expected: 1 / sqrt(x^2 + 1) = 1 / sqrt(16 - 1) = 0.258199
        // CHECK: 0.258199
        dpfloat dpx = dpfloat(coshValue, 1.0);
        dpfloat res = __fwd_diff(diffAcosh)(dpx);
        outputBuffer[index++] = res.d;
    }
    {
        // Check backward mode agrees with forward
        // CHECK: 0.258199
        dpfloat dpx = diffPair(coshValue);
        __bwd_diff(diffAcosh)(dpx, 1.0);
        outputBuffer[index++] = dpx.d;
    }


    let tanhValue = 0.5;
    {
        // Expected: 1 / (1 - x^2) = 1 / (1 - 0.25) = 1.333...
        // CHECK: 1.3333
        dpfloat dpx = dpfloat(tanhValue, 1.0);
        dpfloat res = __fwd_diff(diffAtanh)(dpx);
        outputBuffer[index++] = res.d;
    }
    {
        // Check backward mode agrees with forward
        // CHECK: 1.3333
        dpfloat dpx = diffPair(tanhValue);
        __bwd_diff(diffAtanh)(dpx, 1.0);
        outputBuffer[index++] = dpx.d;
    }
}