Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement Kaiser window #87330

Closed
wants to merge 9 commits into from
Closed

Conversation

alvgaona
Copy link
Contributor

Relates to #85366

@pytorch-bot
Copy link

pytorch-bot bot commented Oct 19, 2022

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/87330

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 38550c5:
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@alvgaona
Copy link
Contributor Author

alvgaona commented Oct 19, 2022

@mruberry @lezcano I'm getting this lint error.

>>> Lint for torch/__init__.py:

  Error (MYPY) [no-redef]
    Name "kaiser_window" already defined (possibly by an import)

        874  |
        875  |# Importing alias functions due to backward compatibility.
        876  |# Link to a detailed conversation: https://github.com/pytorch/pytorch/pull/87082
    >>> 877  |from torch._windows import kaiser_window
        878  |
        879  |# Quantized, sparse, AO, etc. should be last to get imported, as nothing
        880  |# is expected to depend on them.

I feel like we need to somehow stop generating the Python bindings for the C++ implementation for this warning to get solved. Additionally, I also feel like we should remove this block as it has old documentation and doesn't really add anything. We're doing the alias to a different implementation; at the moment it gets overridden by the alias function's docstring.

pytorch/torch/_torch_docs.py

Lines 12723 to 12765 in c9b6184

add_docstr(
torch.kaiser_window,
"""
kaiser_window(window_length, periodic=True, beta=12.0, *, dtype=None, \
layout=torch.strided, device=None, requires_grad=False) -> Tensor
"""
+ r"""
Computes the Kaiser window with window length :attr:`window_length` and shape parameter :attr:`beta`.
Let I_0 be the zeroth order modified Bessel function of the first kind (see :func:`torch.i0`) and
``N = L - 1`` if :attr:`periodic` is False and ``L`` if :attr:`periodic` is True,
where ``L`` is the :attr:`window_length`. This function computes:
.. math::
out_i = I_0 \left( \beta \sqrt{1 - \left( {\frac{i - N/2}{N/2}} \right) ^2 } \right) / I_0( \beta )
Calling ``torch.kaiser_window(L, B, periodic=True)`` is equivalent to calling
``torch.kaiser_window(L + 1, B, periodic=False)[:-1])``.
The :attr:`periodic` argument is intended as a helpful shorthand
to produce a periodic window as input to functions like :func:`torch.stft`.
.. note::
If :attr:`window_length` is one, then the returned window is a single element tensor containing a one.
"""
+ r"""
Args:
window_length (int): length of the window.
periodic (bool, optional): If True, returns a periodic window suitable for use in spectral analysis.
If False, returns a symmetric window suitable for use in filter design.
beta (float, optional): shape parameter for the window.
Keyword args:
{dtype}
layout (:class:`torch.layout`, optional): the desired layout of returned window tensor. Only
``torch.strided`` (dense layout) is supported.
{device}
{requires_grad}
""".format(
**factory_common_args
),
)

One last question. What should I do next? I got lost a little bit in the previous conversation about what to do with the C++ implementations, the PrimTorch, and the _refs directory (still learning about this kind of module).

@mruberry mruberry self-requested a review October 20, 2022 00:01
@lezcano
Copy link
Collaborator

lezcano commented Oct 20, 2022

You can stop PyTorch from generating bindings by adding a manual_cpp_binding: True to its corresponding entry in native_functions.yaml.
About the docs, agree. You should replace those docs with docs of other functions that are alias, as Mike suggested in the previous PR.
About the refs, it may be fine not to do anything on that end really (unless @mruberry thinks otherwise). Already having this implemented in Python is a massive step forward, and adding these to the _refs folder (which, as you may have already seen, is a folder with Python implementations of many PyTorch functions) should be fairly straightforward and can be done at a later time.

torch/_windows.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

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

This looks great; I would just leave torch.kaiser_window as-is for the moment and focus on the development of torch.signal.windows. (That thought goes for the development of additional windows, too). I made some small inline comments about torch.signal.windows.kaiser for your review.

We can review aligning the existing torch windowing operations with torch.signal.windows later.

Copy link
Contributor Author

@alvgaona alvgaona left a comment

Choose a reason for hiding this comment

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

@mruberry addressed a couple of your comments with additional comments 😛

torch/_windows.py Outdated Show resolved Hide resolved
torch/signal/windows/windows.py Show resolved Hide resolved
@alvgaona

This comment was marked as resolved.

@lezcano
Copy link
Collaborator

lezcano commented Oct 21, 2022

I did mention we may want to update the C++ implementations, but I think it's alright to leave them as they are for now, so that we don't generate unnecessary extra work.

@albanD albanD added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Oct 21, 2022
torch/__init__.py Outdated Show resolved Hide resolved
@alvgaona
Copy link
Contributor Author

You can stop PyTorch from generating bindings by adding a manual_cpp_binding: True to its corresponding entry in native_functions.yaml. About the docs, agree. You should replace those docs with docs of other functions that are alias, as Mike suggested in the previous PR. About the refs, it may be fine not to do anything on that end really (unless @mruberry thinks otherwise). Already having this implemented in Python is a massive step forward, and adding these to the _refs folder (which, as you may have already seen, is a folder with Python implementations of many PyTorch functions) should be fairly straightforward and can be done at a later time.

@lezcano I tried using manual_cpp_binding: True but it fails. I guess we can have a look at the follow-up PR where we try to do the aliasing work.

@alvgaona alvgaona force-pushed the kaiser-window branch 2 times, most recently from 8254bc7 to 9a9a231 Compare October 24, 2022 01:53
@lezcano
Copy link
Collaborator

lezcano commented Oct 24, 2022

follow up PR LGTM!

@alvgaona alvgaona requested review from lezcano and removed request for lezcano October 25, 2022 03:41
@alvgaona
Copy link
Contributor Author

@lezcano @mruberry I think we can merge it and go back to #87082, right?

if M == 1:
return torch.ones((1,), dtype=dtype, layout=layout, device=device, requires_grad=requires_grad)

if beta <= 0:
Copy link
Collaborator

Choose a reason for hiding this comment

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

check for beta should go before returning the wrong value for M=1

Copy link
Collaborator

Choose a reason for hiding this comment

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

What's interesting is that SciPy does not require a positive beta:

scipy.signal.windows.kaiser(1, beta=-5)
: array([1.])

scipy.signal.windows.kaiser(10, beta=-5.)
: array([0.03671089, 0.20127873, 0.47552746, 0.7753221 , 0.97273069,
       0.97273069, 0.7753221 , 0.47552746, 0.20127873, 0.03671089])

The wikipedia entry stipulates that beta should be non-negative, but not strictly positive: https://en.wikipedia.org/wiki/Kaiser_window

So, follow-up questions:

  • why are we requiring a positive beta?
  • should the documentation discuss this requirement?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure, let's make it non-negative. Definition includes I0(beta) and I0 is not defined for negative args (and there's no alternative definition for 1), so looks like scipy is wrong.

Copy link
Contributor Author

@alvgaona alvgaona Oct 26, 2022

Choose a reason for hiding this comment

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

Agreed with @ngimel. I'll turn that precheck into a non-negative one, and I'll move it up as well.

Copy link
Contributor Author

@alvgaona alvgaona Oct 26, 2022

Choose a reason for hiding this comment

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

@lezcano @mruberry @ngimel I guess for the gaussian and exponential windows we should move the std and tau check up a few lines too (will do in another PR).

Copy link
Collaborator

Choose a reason for hiding this comment

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

yup. Feel free to add it to this PR if oyu want.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll do it in #87082

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed with @ngimel. I'll turn that precheck into a non-negative one, and I'll move it up as well.

But why not just allow negative betas, too?

Copy link
Contributor Author

@alvgaona alvgaona Oct 26, 2022

Choose a reason for hiding this comment

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

@mruberry in the literature beta is always non-negative.

image

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

I understand, just interesting that SciPy allows them

Copy link
Collaborator

@lezcano lezcano left a comment

Choose a reason for hiding this comment

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

Leaving aside a small point I left and Natalia's valid point, this LGTM. Let's see what Mike has to say though.

As always, thank you for doing all this ground work!

torch/signal/windows/windows.py Outdated Show resolved Hide resolved
@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 25, 2022
- Make the precheck non-negative.
- Call fewer kernels by adding beta in the construction of k
@alvgaona alvgaona requested review from ngimel and removed request for mruberry October 26, 2022 17:28
@alvgaona alvgaona requested review from mruberry and removed request for ngimel October 27, 2022 00:28
Copy link
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

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

See one suggestion about mentioning non-negative requirement for beta in the documentation, otherwise LGTM! Thanks @alvgaona

@alvgaona
Copy link
Contributor Author

@mruberry @lezcano ready to get merged. Is it possible to do a squash merge?

@lezcano
Copy link
Collaborator

lezcano commented Oct 27, 2022

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@github-actions
Copy link

Hey @alvgaona.
You've committed this PR, but it does not have both a 'release notes: ...' and 'topics: ...' label. Please add one of each to the PR. The 'release notes: ...' label should represent the part of PyTorch that this PR changes (fx, autograd, distributed, etc) and the 'topics: ...' label should represent the kind of PR it is (not user facing, new feature, bug fix, perf improvement, etc). The list of valid labels can be found here for the 'release notes: ...' and here for the 'topics: ...'.
For changes that are 'topic: not user facing' there is no need for a release notes label.

sgrigory pushed a commit to sgrigory/pytorch that referenced this pull request Oct 28, 2022
kulinseth pushed a commit to kulinseth/pytorch that referenced this pull request Nov 5, 2022
kulinseth pushed a commit to kulinseth/pytorch that referenced this pull request Dec 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/trunk Trigger trunk jobs on your pull request Merged open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants