Added flash_compatible mode to decoder; fixed CUDA Graph graph-break in spectrum encoder#90
Open
GauravSRC wants to merge 1 commit into
Open
Added flash_compatible mode to decoder; fixed CUDA Graph graph-break in spectrum encoder#90GauravSRC wants to merge 1 commit into
GauravSRC wants to merge 1 commit into
Conversation
…Decoder; fixed CUDA Graph graph-break in SpectrumTransformerEncoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Profiling Casanovo's NAR (non-autoregressive) decoder revealed two blockers preventing hardware-accelerated attention and CUDA Graph capture:
AnalyteTransformerDecoder.embed()unconditionally buildstgt_key_padding_maskand generates a causaltgt_mask, both of which cause PyTorch'sMultiheadAttentionto fall back from the FlashAttention SDPA backend to the slower memory-efficient backend.SpectrumTransformerEncoder.forward()constructs the global-token mask viatorch.tensor([[False]] * batch_size)--a data-dependent Python list comprehension that TorchDynamo cannot trace symbolically, fragmenting the CUDA Graph into 4 disconnected sub-graphs.Changes
analytes.py: addflash_compatible: bool = Falsetoembed()andforward(). WhenTrue, skipstgt_key_padding_maskand causaltgt_maskgeneration. Fully backward-compatible --default isFalse, no existing caller is affected.spectra.py: replace list-comprehension mask withnew_zeros()tensor factory. Numerically identical output, no behaviour change.utils.py,__init__.py, all other files.Backward Compatibility --Verified Bit-Exact
A dedicated numerical equivalence test confirmed the
flash_compatible=Falsedefault path (used by every existing casanovo installation) produces zero change in output. Both code paths were run on the same decoder instance (identical weights), so any difference could only come from logic, not weights. The new default-path code was compared against a standalone reimplementation of the originalembed()body across 4 test conditions (auto-generated causal mask, explicit caller-provided mask, fullforward()token scores, plus a 10-trial robustness sweep across varying batch sizes/sequence lengths/padding patterns) -- 13 tests total, all bit-exact withmax_diff = 0.00e+00. This is not merely withintorch.allclosetolerance; outputs are identical to the last bit. Existing casanovo users are completely unaffected by this change.Performance Validation
Profiled on real MS spectra (NVIDIA L4, PyTorch 2.7.1) with the
flash_compatible=Trueopt-in path:torch.compilespectra.pygraph-break fixAs a side investigation, TF32 (
torch.backends.cuda.matmul.allow_tf32) was also tested as an alternative to BF16 to avoid autocast's casting overhead. TF32 is fully compatible withtorch.compile(comparable ~1.5–1.7× gains) but --as expected, since TF32 doesn't change tensor dtype --cannot activate FlashAttention, confirming BF16 remains the better precision choice when flash attention matters.Testing
Existing test suite passes unchanged (8/8). New
test_analyte_decoder_flash_compatibleverifies the opt-in path produces correct output shape and that the AR path remains unaffected.