Skip to content

Add HannWindow operator to burn-tensor#4631

Open
walkinggo wants to merge 1 commit intotracel-ai:mainfrom
walkinggo:feat/HannWindow
Open

Add HannWindow operator to burn-tensor#4631
walkinggo wants to merge 1 commit intotracel-ai:mainfrom
walkinggo:feat/HannWindow

Conversation

@walkinggo
Copy link

Pull Request Template

Checklist

  • Confirmed that cargo run-checks command has been executed. (Note: Ran targeted checks instead due to local CUDA SDK limitations, see Testing section below for details).
  • Made sure the book is up to date with changes in this PR. (Note: N/A, this is an API addition and the Rustdoc is fully documented).

Related Issues/PRs

Addresses part of #4531 (Specifically the HannWindow signal processing operator).

Changes

Added the HannWindow operator to the Tensor API to support signal processing models and align with ONNX specifications.

Detailed implementation:

  1. crates/burn-tensor/src/tensor/api/float.rs:

    • Added Tensor<B, 1>::hann_window(size, periodic, options) as an associated function under a new impl<B> Tensor<B, 1> block.
    • Implemented the Hann window formula: $w_n = 0.5 - 0.5 \times \cos(2\pi n / N)$ using existing tensor operations (arange, float, cos, mul_scalar, add_scalar, cast).
    • Supported both periodic ($N = size$) and symmetric ($N = size - 1$) modes.
    • Handled edge cases perfectly: size = 0 returns an empty tensor, and size = 1 returns [1.0] (preventing division-by-zero panics and matching PyTorch/NumPy behavior).
    • Used f64 precision for the angular increment calculation to ensure high accuracy before casting to the target dtype.
    • Added dual-state documentation (cfg_attr) for clean KaTeX math rendering in Rustdoc and readable plain-text fallbacks in IDE hover hints.
  2. crates/burn-backend-tests/tests/tensor/float/ops/hann_window.rs & mod.rs:

    • Registered the new test module.
    • Added 6 comprehensive test cases covering periodic mode, symmetric mode, dtype options, empty tensors, and the size=1 boundary for both modes.
    • Used assert_approx_eq with Tolerance for cross-backend floating-point stability.

Testing

Due to my local CUDA SDK being an older version, the cudarc binding in cubecl-cuda failed to find CUtensorMapL2promotion and CUtensorMapFloatOOBfill. Therefore, I had to skip the global cargo run-checks command.

Instead, I manually and successfully verified the code using targeted commands for the active backends:

Check Command Result
Formatting cargo fmt --all -- --check Passed ✅
Linting cargo clippy -p burn-tensor -p burn-backend-tests -- -D warnings Passed ✅
Testing cargo test -p burn-backend-tests --test tensor -- hann_window 6/6 Passed ✅

I had check every line code to make sure the PR is right

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
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 61.83%. Comparing base (4e962f9) to head (4b22def).
⚠️ Report is 4 commits behind head on main.

❌ 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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@walkinggo
Copy link
Author

Hi @antimora , I've submitted this PR to address the HannWindow part of your issue #4531
Whenever you have some free time, I would really appreciate it if you could take a look and review this :)

Copy link
Member

@laggui laggui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@laggui laggui requested a review from antimora March 17, 2026 19:56
@antimora
Copy link
Collaborator

Currently I am traveling; I'll review carefully when I am back this weekend.

@laggui
Copy link
Member

laggui commented Mar 18, 2026

No worries! Safe travels 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants