summaryrefslogtreecommitdiff
path: root/tests/autodiff
diff options
context:
space:
mode:
authorkaizhangNV <149626564+kaizhangNV@users.noreply.github.com>2025-02-05 12:37:03 -0600
committerGitHub <noreply@github.com>2025-02-05 10:37:03 -0800
commit9ec6b91686b651d959fd9ffbec283845bd725dd6 (patch)
tree2c48202cb04b76e5ddcb274be35529378ddf8f31 /tests/autodiff
parent4b350645042b8e8fbdad19784ee745d11c7bc616 (diff)
Feature/initialize list side branch (#6058)
* SP004: implement initialize list translation to ctor - We synthesize a member-wise constructor for each struct follow the rules described in SP004. - Add logic to translate the initialize list to constructor invoke - Add cuda-host decoration for the synthesized constructor - Remove the default constructor when we have a valid member init constructor - Disable -zero-initialize option, will re-implement it in followup (#6109). - Fix the overload lookup issue When creating invoke expression for ctor, we need to call ResolveInvoke() to find us the best candidates, however the existing lookup logic could find us the base constructor for child struct, we should eliminate this case by providing the LookupOptions::IgnoreInheritance to lookup, this requires us to create a subcontext on SemanticsVisitor to indicate that we only want to use this option on looking the constructor. - Do not implicit initialize a struct that doesn't have explicit default constructor. Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Diffstat (limited to 'tests/autodiff')
-rw-r--r--tests/autodiff/differential-type-constructor.slang6
-rw-r--r--tests/autodiff/generic-constructor.slang10
-rw-r--r--tests/autodiff/generic-impl-jvp.slang26
-rw-r--r--tests/autodiff/self-differential-generic-type-synthesis.slang2
-rw-r--r--tests/autodiff/self-differential-type-synthesis.slang2
5 files changed, 39 insertions, 7 deletions
diff --git a/tests/autodiff/differential-type-constructor.slang b/tests/autodiff/differential-type-constructor.slang
index 9afff878d..96cf1272e 100644
--- a/tests/autodiff/differential-type-constructor.slang
+++ b/tests/autodiff/differential-type-constructor.slang
@@ -14,12 +14,12 @@ RWStructuredBuffer<float> outputBuffer;
void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
{
{
- var dp = diffPair(MyStruct(), MyStruct.Differential());
+ var dp = diffPair(MyStruct(0.0, 0), MyStruct.Differential(0.0));
outputBuffer[0] = dp.d.a;
- var dp2 = diffPair<MyStruct>(MyStruct(), MyStruct.Differential(1.f));
+ var dp2 = diffPair<MyStruct>(MyStruct(0.0, 0), MyStruct.Differential(1.f));
outputBuffer[1] = dp2.d.a;
}
-} \ No newline at end of file
+}
diff --git a/tests/autodiff/generic-constructor.slang b/tests/autodiff/generic-constructor.slang
index aad9824ec..a5b098ac2 100644
--- a/tests/autodiff/generic-constructor.slang
+++ b/tests/autodiff/generic-constructor.slang
@@ -19,6 +19,16 @@ struct Impl : IFoo
{
x = v.x;
}
+
+ // We have to add this __init so that the following code can still work:
+ // Impl.Differential v0 = { (float)x };
+ // because when there is a explicit constructor defined, we will not fall back
+ // to legacy constructor. So this construction will fail.
+ [Differentiable]
+ __init(float v)
+ {
+ x = v;
+ }
}
[Differentiable]
diff --git a/tests/autodiff/generic-impl-jvp.slang b/tests/autodiff/generic-impl-jvp.slang
index e5999b4ae..000c14248 100644
--- a/tests/autodiff/generic-impl-jvp.slang
+++ b/tests/autodiff/generic-impl-jvp.slang
@@ -161,6 +161,16 @@ struct lineardvector : IDifferentiable
val.values[i] = a[i];
}
}
+
+ // Add a new constructor for dadd() function.
+ __init(Real a[N])
+ {
+ [ForceUnroll]
+ for (int i = 0; i < N; i++)
+ {
+ val.values[i] = a[i];
+ }
+ }
};
__generic<let N : int>
@@ -204,12 +214,24 @@ struct linearvector : MyLinearArithmeticType, IDifferentiable
static Differential dadd(Differential a, Differential b)
{
- return { myvector<Real, N>.dadd(a.val, b.val) };
+ // return { myvector<Real, N>.dadd(a.val, b.val) };
+ //
+ // Above code will not work because
+ // myvector<Real, N>.dadd will return dvector<T.Differential, N> type
+ // while Differential == lineardvector<N> type
+ // and the constructor of lineardvector<N> requires a vector<Real.Differential, N> type
+ // and dvector<T.Differential, N> != vector<Real.Differential, N>, though they have the
+ // same members.
+ //
+ // In our new design, generic will not be C-Style struct anymore.
+ dvector<Real.Differential, N> d = myvector<Real, N>.dadd(a.val, b.val);
+ return {d.values};
}
static Differential dmul<T: __BuiltinRealType>(T a, Differential b)
{
- return { myvector<Real, N>.dmul<T>(a, b.val) };
+ dvector<Real.Differential, N> d = myvector<Real, N>.dmul<T>(a, b.val);
+ return {d.values};
}
[ForwardDifferentiable]
diff --git a/tests/autodiff/self-differential-generic-type-synthesis.slang b/tests/autodiff/self-differential-generic-type-synthesis.slang
index 8d225dec2..7883188f6 100644
--- a/tests/autodiff/self-differential-generic-type-synthesis.slang
+++ b/tests/autodiff/self-differential-generic-type-synthesis.slang
@@ -17,7 +17,7 @@ struct Ray<let N: int> : IDifferentiable {
[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
- Ray<4> ray = Ray<4>();
+ Ray<4> ray = Ray<4>(0.0, float4(0.0), float4(0.0));;
Ray<4>.Differential ray2;
ray.a = 1.f;
diff --git a/tests/autodiff/self-differential-type-synthesis.slang b/tests/autodiff/self-differential-type-synthesis.slang
index 7f95891c6..6faa2175a 100644
--- a/tests/autodiff/self-differential-type-synthesis.slang
+++ b/tests/autodiff/self-differential-type-synthesis.slang
@@ -17,7 +17,7 @@ struct Ray : IDifferentiable {
[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
- Ray ray = Ray();
+ Ray ray = {};
Ray.Differential ray2;
ray.a = 1.f;