Implement HardShrink, SoftShrink and Shrink Activations#4556
Implement HardShrink, SoftShrink and Shrink Activations#4556laggui merged 7 commits intotracel-ai:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4556 +/- ##
==========================================
+ Coverage 61.21% 61.32% +0.11%
==========================================
Files 1062 1066 +4
Lines 136012 136410 +398
==========================================
+ Hits 83258 83655 +397
- Misses 52754 52755 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@antimora, Please review. |
There was a problem hiding this comment.
Pull request overview
This PR implements HardShrink and SoftShrink activation functions to support the ONNX Shrink operator. The implementation includes base tensor operations, neural network modules, configuration structs, display implementations, comprehensive tests, and documentation updates.
Changes:
- Added
hard_shrinkandsoft_shrinkactivation functions in burn-tensor - Created
HardShrinkandSoftShrinkneural network modules with configuration support - Integrated both activations into the activation wrapper system
- Updated documentation in the Burn book
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/burn-tensor/src/tensor/activation/base.rs | Implements base hard_shrink and soft_shrink activation functions with LaTeX documentation |
| crates/burn-nn/src/activation/hardshrink.rs | Defines HardShrink module, configuration, display, and tests |
| crates/burn-nn/src/activation/softshrink.rs | Defines SoftShrink module with separate lambd/bias parameters for ONNX compatibility |
| crates/burn-nn/src/activation/mod.rs | Exports the new activation modules |
| crates/burn-nn/src/lib.rs | Adds public exports for hardshrink and softshrink |
| crates/burn-nn/src/activation/activation_wrapper.rs | Integrates both activations into the activation enum with From implementations and tests |
| burn-book/src/building-blocks/tensor.md | Documents the new activation functions in the API reference |
| burn-book/src/building-blocks/module.md | Lists the new modules in the built-in modules table |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
antimora
left a comment
There was a problem hiding this comment.
Nice work adding these activations - the implementations are correct and the integration into the Activation/ActivationConfig wrapper is clean. A few things to address though, see inline comments.
antimora
left a comment
There was a problem hiding this comment.
Minor fixes are required. Please see inlined comments.
laggui
left a comment
There was a problem hiding this comment.
Thanks for contributing!
If I understand correctly, I think ONNX Shrink is just a generalized implementation that handles both soft and hard shrink.
For bias = 0 it acts like HardShrink, and bias = lambda, it is like SoftShrink.
So if we add both hard_shrink and soft_shrink, we don't need soft shrink to have a separate bias no?
And for any other bias values, we could have a general shrink (reused in soft_shrink possibly).
|
@antimora looks like I started the review at the same time 😄 comments are pretty similar, except for the |
If I understand it correctly, we agree to add another ActivationConfig/Activation for general For Soft shink, we remove the |
|
@aditya0by0 Yeah that's exactly right. To spell it out:
At the tensor activation level, |
|
Yep, agreed with the above.
Yeah we should probably rename to just |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| | `SoftShrink` | `nn.Softshrink` | | ||
| | `Softsign` | `nn.Softsign` | | ||
| | `Shrink` | _No direct equivalent_ | |
There was a problem hiding this comment.
The table entries are not in alphabetical order. "Shrink" should be placed before "SoftShrink" to maintain alphabetical ordering in the table. The correct order should be: Selu, Shrink, SoftShrink, Softsign.
| | `SoftShrink` | `nn.Softshrink` | | |
| | `Softsign` | `nn.Softsign` | | |
| | `Shrink` | _No direct equivalent_ | | |
| | `Shrink` | _No direct equivalent_ | | |
| | `SoftShrink` | `nn.Softshrink` | | |
| | `Softsign` | `nn.Softsign` | |
There was a problem hiding this comment.
I think the activations which don't have a pytorch equivalent goes in the bottom irrespective of alphabetical order.
antimora
left a comment
There was a problem hiding this comment.
All feedback addressed, looks good now. The three-tier split (HardShrink / SoftShrink / Shrink) is clean, soft_shrink properly delegates to shrink(tensor, lambda, lambda), LaTeX is fixed, unnecessary clone removed, file naming is consistent. Thanks for the quick turnaround!
Pull Request Template
Checklist
cargo run-checkscommand has been executed.Related Issues/PRs
These activations are needed to implement Shrink ONNX operator tracel-ai/burn-onnx#158
Changes
Soft and Hard Shrink Activations
Testing