|
| 1 | +//TEST:SIMPLE(filecheck=CHECK): -target cuda -experimental-feature |
| 2 | + |
| 3 | +// Full regression test for https://github.com/shader-slang/slang/issues/10605 |
| 4 | + |
| 5 | +// 4-layer MLP with autodiff backward pass targeting CUDA. Exercises the |
| 6 | +// interaction between `static const` globals computed from macro expansions, |
| 7 | +// generic struct members with constexpr arithmetic (division, ternary), |
| 8 | +// and the autodiff pass. The original crash was a null-operand segfault in |
| 9 | +// AutoDiffPass::processReferencedFunctions. |
| 10 | + |
| 11 | +// CHECK-DAG: __global__ void __kernel__backward |
| 12 | +// CHECK-DAG: s_apply_runWaveMLP |
| 13 | + |
| 14 | +import slang.neural; |
| 15 | + |
| 16 | +#ifndef TCNN_MLP_IN_DIM |
| 17 | +#define TCNN_MLP_IN_DIM 2 |
| 18 | +#endif |
| 19 | + |
| 20 | +#ifndef WT_WAVE_WARPS |
| 21 | +#define WT_WAVE_WARPS 4 |
| 22 | +#endif |
| 23 | + |
| 24 | +static const int IN_DIM = TCNN_MLP_IN_DIM; |
| 25 | +static const int HIDDEN_DIM = 128; |
| 26 | +static const int OUT_DIM = 3; |
| 27 | +static const int SubgroupSize = 32; |
| 28 | +static const int WARPS_PER_BLOCK = WT_WAVE_WARPS; |
| 29 | +static const float LEAKY_ALPHA = 0.01f; |
| 30 | + |
| 31 | +typealias Storage = TorchTensorViewAddress<half>; |
| 32 | +typealias ShMemSize = SharedMemorySize<half, TargetEnum.CUDA, ExecutionMode.Training, SubgroupSize, WARPS_PER_BLOCK>; |
| 33 | +typealias ShMemSizeLayer = ShMemSize.OfLayer4<IN_DIM, HIDDEN_DIM, HIDDEN_DIM, HIDDEN_DIM, OUT_DIM>; |
| 34 | +typealias InVec = WaveTangledVector<half, ShMemSizeLayer, IN_DIM, SubgroupSize>; |
| 35 | +typealias HidVec = WaveTangledVector<half, ShMemSizeLayer, HIDDEN_DIM, SubgroupSize>; |
| 36 | +typealias OutVec = WaveTangledVector<half, ShMemSizeLayer, OUT_DIM, SubgroupSize>; |
| 37 | +typealias Layer1 = FFLayer<half, InVec, HidVec, LinearLayout, LeakyReLU<half>, true>; |
| 38 | +typealias Layer2 = FFLayer<half, HidVec, HidVec, LinearLayout, LeakyReLU<half>, true>; |
| 39 | +typealias Layer3 = FFLayer<half, HidVec, HidVec, LinearLayout, LeakyReLU<half>, true>; |
| 40 | +typealias Layer4 = FFLayer<half, HidVec, OutVec, LinearLayout, Sigmoid<half>, true>; |
| 41 | + |
| 42 | +static const int TotalParamCount = |
| 43 | + Layer1.ParameterCount + Layer2.ParameterCount + Layer3.ParameterCount + Layer4.ParameterCount; |
| 44 | + |
| 45 | +[Differentiable] |
| 46 | +OutVec runWaveMLP(InVec input, Storage params) |
| 47 | +{ |
| 48 | + LeakyReLU<half> leaky = LeakyReLU<half>(half(LEAKY_ALPHA)); |
| 49 | + Layer1 layer1 = Layer1(leaky); |
| 50 | + Layer2 layer2 = Layer2(leaky); |
| 51 | + Layer3 layer3 = Layer3(leaky); |
| 52 | + Layer4 layer4 = Layer4(); |
| 53 | + |
| 54 | + int offset = 0; |
| 55 | + HidVec h1 = layer1.eval<Storage>(input, params.getOffset(offset)); offset += Layer1.ParameterCount; |
| 56 | + HidVec h2 = layer2.eval<Storage>(h1, params.getOffset(offset)); offset += Layer2.ParameterCount; |
| 57 | + HidVec h3 = layer3.eval<Storage>(h2, params.getOffset(offset)); offset += Layer3.ParameterCount; |
| 58 | + return layer4.eval<Storage>(h3, params.getOffset(offset)); |
| 59 | +} |
| 60 | + |
| 61 | +[AutoPyBindCUDA] |
| 62 | +[CUDAKernel] |
| 63 | +void backward( |
| 64 | + DiffTensorView input, |
| 65 | + TensorView<half> params, |
| 66 | + TensorView<half> paramsGrad, |
| 67 | + DiffTensorView output) |
| 68 | +{ |
| 69 | + uint idx = cudaBlockIdx().x * cudaBlockDim().x + cudaThreadIdx().x; |
| 70 | + bool isActive = idx < input.size(0); |
| 71 | + |
| 72 | + InVec x = InVec(half(0)); |
| 73 | + if (isActive) |
| 74 | + { |
| 75 | + for (int i = 0; i < IN_DIM; ++i) x[i] = half(input.primal[idx, i]); |
| 76 | + } |
| 77 | + |
| 78 | + OutVec dL_dOutput = OutVec(half(0)); |
| 79 | + if (isActive) |
| 80 | + { |
| 81 | + for (int i = 0; i < OUT_DIM; ++i) dL_dOutput[i] = half(output.diff.diff[idx, i]); |
| 82 | + } |
| 83 | + |
| 84 | + InVec dx = InVec(half(0)); |
| 85 | + var dpInput = diffPair(x, dx); |
| 86 | + bwd_diff(runWaveMLP)(dpInput, |
| 87 | + DifferentialPtrPair<Storage>(Storage(params), Storage(paramsGrad)), |
| 88 | + dL_dOutput); |
| 89 | + |
| 90 | + if (isActive) |
| 91 | + { |
| 92 | + for (int i = 0; i < IN_DIM; ++i) input.diff.diff[idx, i] = float(dpInput.d[i]); |
| 93 | + } |
| 94 | +} |
0 commit comments