summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDarren Wihandi <65404740+fairywreath@users.noreply.github.com>2025-05-21 01:48:19 -0400
committerGitHub <noreply@github.com>2025-05-21 05:48:19 +0000
commitc4b6ef0bd6a7b7de131786c661c7e3423e318d7e (patch)
treec4b3b2b0cab861bb1b7cd182c797e9ce5c47bf1f
parent9059093bc764e901a9c4aaeb12471bf32028874f (diff)
Add inverse hyperbolic derivatives (#7173)
* Add inverse hyperbolic derivatives * Add test
-rw-r--r--source/slang/diff.meta.slang6
-rw-r--r--source/slang/hlsl.meta.slang38
-rw-r--r--tests/autodiff-dstdlib/dstdlib-inverse-hyperbolic.slang83
3 files changed, 127 insertions, 0 deletions
diff --git a/source/slang/diff.meta.slang b/source/slang/diff.meta.slang
index d32584db0..c24a8b11a 100644
--- a/source/slang/diff.meta.slang
+++ b/source/slang/diff.meta.slang
@@ -1970,6 +1970,12 @@ SIMPLE_UNARY_DERIVATIVE_IMPL(asin, T(1.0) / sqrt(T(1.0) - dpx.p * dpx.p))
SIMPLE_UNARY_DERIVATIVE_IMPL(acos, T(-1.0) / sqrt(T(1.0) - dpx.p * dpx.p))
// Arc-tan
SIMPLE_UNARY_DERIVATIVE_IMPL(atan, T(1.0) / (T(1.0) + dpx.p * dpx.p))
+// Arc-sinh
+SIMPLE_UNARY_DERIVATIVE_IMPL(asinh, T(1.0) / sqrt(dpx.p * dpx.p + T(1.0)))
+// Arc-cosh
+SIMPLE_UNARY_DERIVATIVE_IMPL(acosh, T(1.0) / sqrt(dpx.p * dpx.p - T(1.0)))
+// Arc-tanh
+SIMPLE_UNARY_DERIVATIVE_IMPL(atanh, T(1.0) / (T(1.0) - dpx.p * dpx.p))
// Atan2
__generic<T : __BuiltinFloatingPointType>
diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang
index 57cb188bb..e3d2d9dd2 100644
--- a/source/slang/hlsl.meta.slang
+++ b/source/slang/hlsl.meta.slang
@@ -6452,6 +6452,18 @@ vector<T,N> acosh(vector<T,N> x)
}
}
+__generic<T : __BuiltinFloatingPointType, let N : int, let M : int>
+[__readNone]
+[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, sm_4_0_version)]
+matrix<T, N, M> acosh(matrix<T, N, M> x)
+{
+ __target_switch
+ {
+ case hlsl: __intrinsic_asm "acosh";
+ default:
+ MATRIX_MAP_UNARY(T, N, M, acosh, x);
+ }
+}
// Test if all components are non-zero.
__generic<T : __BuiltinType>
@@ -6955,6 +6967,19 @@ vector<T,N> asinh(vector<T,N> x)
}
}
+__generic<T : __BuiltinFloatingPointType, let N : int, let M : int>
+[__readNone]
+[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, sm_4_0_version)]
+matrix<T, N, M> asinh(matrix<T, N, M> x)
+{
+ __target_switch
+ {
+ case hlsl: __intrinsic_asm "asinh";
+ default:
+ MATRIX_MAP_UNARY(T, N, M, asinh, x);
+ }
+}
+
/// Reinterpret bits as an int.
/// @category conversion
[__readNone]
@@ -7577,6 +7602,19 @@ vector<T,N> atanh(vector<T,N> x)
}
}
+__generic<T : __BuiltinFloatingPointType, let N : int, let M : int>
+[__readNone]
+[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, sm_4_0_version)]
+matrix<T, N, M> atanh(matrix<T, N, M> x)
+{
+ __target_switch
+ {
+ case hlsl: __intrinsic_asm "atanh";
+ default:
+ MATRIX_MAP_UNARY(T, N, M, atanh, x);
+ }
+}
+
/// Ceiling. Returns the smallest integer that is greater than or equal to the specified value.
/// @param x The value.
/// @return The smallest integer that is greater than or equal to the specified value.
diff --git a/tests/autodiff-dstdlib/dstdlib-inverse-hyperbolic.slang b/tests/autodiff-dstdlib/dstdlib-inverse-hyperbolic.slang
new file mode 100644
index 000000000..c7c59ff49
--- /dev/null
+++ b/tests/autodiff-dstdlib/dstdlib-inverse-hyperbolic.slang
@@ -0,0 +1,83 @@
+//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 -use-dxil -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;
+ }
+}
+
+