Add HannWindow operator to burn-tensor#4631
Conversation
1. crates/burn-tensor/src/tensor/api/float.rs
- Add Tensor<B, 1>::hann_window(size, periodic, options) as an
associated function under a new impl<B> Tensor<B, 1> block.
- Implement the Hann window formula w(n) = 0.5 - 0.5*cos(2*pi*n/N)
using existing ops (arange, float, cos, mul_scalar, add_scalar, cast).
- Support periodic (N=size) and symmetric (N=size-1) modes.
- Handle edge cases: size=0 returns empty tensor, size=1 returns [1.0].
- Use f64 precision for angular increment calculation.
- Include cfg_attr doc with LaTeX formula and a plain-text fallback.
2. crates/burn-backend-tests/tests/tensor/float/ops/hann_window.rs
- Add 6 test cases covering periodic mode, symmetric mode, dtype
options, empty tensor, and size=1 boundary for both modes.
- Use assert_approx_eq with Tolerance for cross-backend float stability.
3. crates/burn-backend-tests/tests/tensor/float/ops/mod.rs
- Register mod hann_window to include the new test module.
Made-with: Cursor
Codecov Report✅ All modified and coverable lines are covered by tests. ❌ Your project check has failed because the head coverage (61.83%) is below the target coverage (80.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #4631 +/- ##
==========================================
+ Coverage 61.71% 61.83% +0.12%
==========================================
Files 1095 1096 +1
Lines 142906 142972 +66
==========================================
+ Hits 88192 88405 +213
+ Misses 54714 54567 -147 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
laggui
left a comment
There was a problem hiding this comment.
Implementation LGTM!
@antimora I'm not super familiar with these signal processing operations, but maybe we should separate them from the tensor API methods? If they're mostly composed of high-level tensor ops, perhaps we could have a signal module (similar to how we have grid operations grouped).
|
Currently I am traveling; I'll review carefully when I am back this weekend. |
|
No worries! Safe travels 🙂 |
Pull Request Template
Checklist
cargo run-checkscommand has been executed. (Note: Ran targeted checks instead due to local CUDA SDK limitations, see Testing section below for details).Related Issues/PRs
Addresses part of #4531 (Specifically the
HannWindowsignal processing operator).Changes
Added the
HannWindowoperator to the Tensor API to support signal processing models and align with ONNX specifications.Detailed implementation:
crates/burn-tensor/src/tensor/api/float.rs:Tensor<B, 1>::hann_window(size, periodic, options)as an associated function under a newimpl<B> Tensor<B, 1>block.arange,float,cos,mul_scalar,add_scalar,cast).periodic(symmetric(size = 0returns an empty tensor, andsize = 1returns[1.0](preventing division-by-zero panics and matching PyTorch/NumPy behavior).f64precision for the angular increment calculation to ensure high accuracy before casting to the target dtype.cfg_attr) for clean KaTeX math rendering in Rustdoc and readable plain-text fallbacks in IDE hover hints.crates/burn-backend-tests/tests/tensor/float/ops/hann_window.rs&mod.rs:periodicmode,symmetricmode, dtype options, empty tensors, and thesize=1boundary for both modes.assert_approx_eqwithTolerancefor cross-backend floating-point stability.Testing
Due to my local CUDA SDK being an older version, the
cudarcbinding incubecl-cudafailed to findCUtensorMapL2promotionandCUtensorMapFloatOOBfill. Therefore, I had to skip the globalcargo run-checkscommand.Instead, I manually and successfully verified the code using targeted commands for the active backends:
cargo fmt --all -- --checkcargo clippy -p burn-tensor -p burn-backend-tests -- -D warningscargo test -p burn-backend-tests --test tensor -- hann_windowI had check every line code to make sure the PR is right