Skip to content

Add hamming/blackman/bartlett window op converters#2744

Open
hemanth1999k wants to merge 2 commits into
apple:mainfrom
hemanth1999k:add-window-functions
Open

Add hamming/blackman/bartlett window op converters#2744
hemanth1999k wants to merge 2 commits into
apple:mainfrom
hemanth1999k:add-window-functions

Conversation

@hemanth1999k

Copy link
Copy Markdown
Contributor

Summary

torch.hamming_window, torch.blackman_window, and torch.bartlett_window have no converter. On the torch.export path they fail with:

NotImplementedError: Unsupported fx node hamming_window, kind hamming_window

Only hann_window was supported, so any audio/DSP model that uses one of the other standard windows can't convert. I found these while bringing up torch 2.9 (#2743) and they reproduce on 2.8 too — they're a plain missing-op gap, not a version regression.

Fix

window_length, periodic, and (for Hamming) alpha/beta are all known at conversion time, so the data-independent window is materialized as a constant using the exact torch formulas (matching torch's symmetric-of-length-N+1-then-drop-last behavior for the periodic case).

A shared _parse_window_inputs helper handles the input-count difference between frontends — TorchScript carries the trailing dtype/layout/device/pin_memory graph inputs, while torch.export/ExecuTorch pass only the positional params. The periodic and Hamming alpha/beta overloads (*.periodic, hamming_window.periodic_alpha[_beta]) are wired up with torch_alias, since sanitize_op_kind doesn't strip those suffixes.

Test

Adds TestWindowFunctions — parametrized over frontends, window lengths [1, 3, 6, 10, 12], and periodic True/False for all three windows, plus a Hamming custom-alpha/beta case.

Verification

Built against coremltools 9.0 + torch 2.9.0 on macOS (arm64). Convert + predict matches PyTorch within fp16 tolerance for every combination:

hamming/blackman/bartlett, N in {6,7,16}, periodic in {True,False}  -> max|torch-coreml| ~ 5e-5 .. 4e-4
hamming alpha=0.5 beta=0.4                                          -> 2.4e-4

Not included

kaiser_window — its definition needs a modified Bessel function I0, which has no direct MIL op, so it warrants a separate change (polynomial approximation). Left out here to keep this focused.

torch.hamming_window, torch.blackman_window and torch.bartlett_window had no
converter — they failed with 'Unsupported fx node ... hamming_window' on the
torch.export path (only hann_window was supported).

Since window_length/periodic/alpha/beta are all known at conversion time, the
(data-independent) window is materialized as a constant computed with the exact
torch formulas. Handles the TorchScript and torch.export/ExecuTorch frontends
(input-count differs), the periodic variants, and Hamming's alpha/beta overloads
(hamming_window.periodic_alpha[_beta]) via torch_alias.

Adds TestWindowFunctions. Verified against torch 2.9.0: convert + predict match
PyTorch within fp16 tolerance across sizes, periodic settings, and custom
alpha/beta. (kaiser_window is left out — it needs a Bessel I0 approximation.)
compute_units,
backends,
frontends,
["hamming", "blackman", "bartlett"],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not just have this line be:

 [torch.hamming_window, torch.blackman_window,torch.bartlett_window]

and get rid of the dictionary lookup.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done — parametrized directly on [torch.hamming_window, torch.blackman_window, torch.bartlett_window] and removed the dict lookup.

@TobyRoseman

Copy link
Copy Markdown
Collaborator

@hemanth1999k

Copy link
Copy Markdown
Contributor Author

Addressed the review comment — parametrized directly on the window callables and dropped the dict lookup. Ready for re-review / CI whenever you get a chance. Thanks!

@TobyRoseman

Copy link
Copy Markdown
Collaborator

@hemanth1999k - the change looks good. There are however CI failures which need to be addressed:
https://gitlab.com/coremltools1/coremltools/-/jobs/14980780467

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.

2 participants