Triton Seeded Fused Dropout
Problem Statement
Implement inverted dropout in a single kernel using Triton's counter-based RNG: drop each element with probability p, scale survivors by 1/(1-p). Use a runtime seed so the result is reproducible.
Background
tl.rand(seed, offsets) produces deterministic uniform values in [0, 1) per offset. Keep elements where rand > p. Because the RNG is seeded, two launches with the same seed must be identical, and survivors must be exactly x/(1-p).
Your Task
Implement dropout_kernel and run(n=4096, p=0.5, seed=123) that returns True when (a) two same-seed launches match and (b) surviving values equal x/(1-p).
How it is tested
Your solution must define a top-level function run(...) that allocates inputs on the GPU, launches your Triton kernel, and returns a boolean from torch.allclose(triton_out, torch_reference, ...). The grader prints run(...); the expected output is True.
Example
n = 4096, p = 0.5, seed = 123
True
- The input values are
n = 4096,p = 0.5, andseed = 123, which are used to allocate inputs on the GPU and launch thedropout_kernel. - The
dropout_kerneluses Triton's counter-based RNG to generate uniform values in [0, 1) per offset, and drops each element with probabilityp = 0.5, scaling survivors by . - The kernel is launched twice with the same seed, ensuring that the results are identical due to the deterministic nature of the RNG.
- The final output is
Truebecause the two same-seed launches match and the surviving values equalx/(1-p), as verified bytorch.allclose(triton_out, torch_reference, ...).
Constraints
- Use tl.rand(seed, offsets) for per-element randomness
- keep = rand > p; out = where(keep, x/(1-p), 0)
- Same seed must give identical output (verified in run)