Repository navigation
Reduce memory usage by computing SPLADE activations in-place - #15
Merged
Merged
Conversation
Rewrite the activation formula relu -> log1p -> mask multiplication to run in-place when gradients are not required (inference or frozen encoder), halving peak memory for the (b, N, V) intermediates. When gradients are required, keep out-of-place ops but switch log(1 + x) to log1p(x) so both paths produce identical values (log1p is also more accurate in fp16). Apply the same in-place formula to the README quickstart snippet. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
|
||
| # Eq. (1) in [2] | ||
| if logits.requires_grad: | ||
| activations = torch.log1p(torch.relu(logits)) * attention_mask.unsqueeze(-1) # (b, N, V) |
Collaborator
Author
There was a problem hiding this comment.
Unified both branches to log1p(x) instead of log(1 + x), since log1p is more accurate for small values — this prevents any discrepancy between training and inference outputs.
https://docs.pytorch.org/docs/2.14/generated/torch.log1p.html
| # Compute in-place to reduce memory usage when gradients are not required | ||
| activations = torch.relu_(logits).log1p_().mul_(attention_mask.unsqueeze(-1)) # (b, N, V) | ||
| vecs, indices_ = self.agg_func( | ||
| torch.log(1 + torch.relu(logits)) * attention_mask.unsqueeze(-1), # (b, N, 1) |
Collaborator
Author
There was a problem hiding this comment.
Regarding the comment: apologies if I'm misreading this, but wasn't the actual shape here (b, N, V) all along?
takashi-wada-1322
marked this pull request as ready for review
September 11, 2026 02:13
This branch was successfully deployed
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.
Summary
Compute the SPLADE activation (
relu→log1p→ mask multiplication) in-place when gradients are not required, roughly halving peak memory for the(b, N, V)intermediate tensors