Skip to content

Commit bbe6e87

Browse files
honor deterministic path
Signed-off-by: MahmoudAshraf97 <hassouna97.ma@gmail.com>
1 parent edc8778 commit bbe6e87

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

nemo/collections/asr/parts/submodules/subsampling.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,12 +716,14 @@ def forward(self, x, lengths):
716716
x = x.unsqueeze(1) # (batch, 1, time, features)
717717
current_lengths = lengths
718718

719-
# Tracing and export cannot capture a Triton launch, and the fused kernel returns no
720-
# input gradient.
719+
# Tracing and export cannot capture a Triton launch, the fused kernel returns no input
720+
# gradient, and its weight gradients accumulate through atomics, so their summation order
721+
# varies between runs.
721722
if (
722723
self.fuse_triton
723724
and x.is_cuda
724725
and not x.requires_grad
726+
and not torch.are_deterministic_algorithms_enabled()
725727
and not (torch.jit.is_tracing() or torch.compiler.is_exporting())
726728
):
727729
x, current_lengths, mask = self._forward_fused(x, current_lengths)

tests/collections/asr/test_fast_subsampling.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,27 @@ def test_export_takes_the_pytorch_path(tmp_path, dynamo):
324324
assert len(convolutions) == 5
325325

326326

327+
@pytest.mark.unit
328+
@pytest.mark.skipif(not CUDA_TRITON_AVAILABLE, reason="CUDA and Triton are required")
329+
def test_deterministic_algorithms_take_the_pytorch_path(monkeypatch):
330+
"""The weight gradients accumulate through atomics, whose summation order varies per run."""
331+
module = _build()
332+
assert module.conv.fuse_triton
333+
x, lengths = _inputs(batch=2, time=520)
334+
335+
def unreachable(*args, **kwargs):
336+
raise AssertionError("the fused path ran under deterministic algorithms")
337+
338+
monkeypatch.setattr("nemo.collections.asr.parts.submodules.subsampling.fused_conv_relu_dw", unreachable)
339+
torch.use_deterministic_algorithms(True)
340+
try:
341+
out, _ = module.conv(x, lengths)
342+
finally:
343+
torch.use_deterministic_algorithms(False)
344+
345+
assert torch.isfinite(out).all()
346+
347+
327348
@pytest.mark.unit
328349
@pytest.mark.skipif(not CUDA_TRITON_AVAILABLE, reason="CUDA and Triton are required")
329350
def test_cpu_input_uses_the_pytorch_path():

0 commit comments

Comments
 (0)