diff options
| author | Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> | 2025-02-04 18:41:11 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-04 21:41:11 -0500 |
| commit | fe77f076b46f45d6c89ab8592d0ff548bda223de (patch) | |
| tree | 53159ad0daa43244142ba3da9d3938b6d3d23e33 | |
| parent | 78a6389aa4b01cfe4f8b4ec34d48b2cbfc092865 (diff) | |
Fix non-square matrix derivatives (#6282)
| -rw-r--r-- | source/slang/slang-ir-autodiff-transpose.h | 7 | ||||
| -rw-r--r-- | tests/autodiff/matrix-arithmetic-non-sqr.slang | 41 |
2 files changed, 44 insertions, 4 deletions
diff --git a/source/slang/slang-ir-autodiff-transpose.h b/source/slang/slang-ir-autodiff-transpose.h index a5ed5814c..5e96c4e0f 100644 --- a/source/slang/slang-ir-autodiff-transpose.h +++ b/source/slang/slang-ir-autodiff-transpose.h @@ -1812,8 +1812,8 @@ struct DiffTransposePass { List<RevGradient> gradients; auto matrixType = as<IRMatrixType>(fwdMakeMatrix->getDataType()); - auto row = as<IRIntLit>(matrixType->getRowCount()); auto colCount = matrixType->getColumnCount(); + auto colCountVal = as<IRIntLit>(matrixType->getColumnCount())->getValue(); IRType* rowVectorType = nullptr; for (UIndex ii = 0; ii < fwdMakeMatrix->getOperandCount(); ii++) { @@ -1828,9 +1828,8 @@ struct DiffTransposePass } else { - SLANG_RELEASE_ASSERT(row); - UInt rowIndex = ii / (UInt)row->getValue(); - UInt colIndex = ii % (UInt)row->getValue(); + UInt rowIndex = ii / (UInt)colCountVal; + UInt colIndex = ii % (UInt)colCountVal; if (!rowVectorType) rowVectorType = builder->getVectorType(matrixType->getElementType(), colCount); auto revRow = builder->emitElementExtract( diff --git a/tests/autodiff/matrix-arithmetic-non-sqr.slang b/tests/autodiff/matrix-arithmetic-non-sqr.slang new file mode 100644 index 000000000..bc42097d5 --- /dev/null +++ b/tests/autodiff/matrix-arithmetic-non-sqr.slang @@ -0,0 +1,41 @@ +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +[Differentiable] +float h_outer(float a, float b, float c) +{ + const float3x2 m2 = float3x2(2 * a, 0.0, + 0.0, 3 * b, + 0.0, 5 * c); + + const float3x3 m1 = float3x3(1.f); + return h(m1, m2); +} + +[Differentiable] +float h(float3x3 x, float3x2 y) +{ + let res = mul(x, y); + return dot(mul(res, float2(1.0)), float3(1.0)); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + // Do a bwd_diff test for h_outer + var dpa = diffPair(1.f, 0.f); + var dpb = diffPair(1.f, 0.f); + var dpc = diffPair(1.f, 0.f); + bwd_diff(h_outer)(dpa, dpb, dpc, 1.f); + + outputBuffer[1] = dpa.d; + outputBuffer[2] = dpb.d; + outputBuffer[3] = dpc.d; + + // CHECK: type: float + // CHECK: 6.0 + // CHECK: 9.0 + // CHECK: 15.0 +} |
