Skip to content

Fix kaiser_window NaN for dynamic length 1 and reject odd KBD window lengths - #129242

Merged
copybara-service[bot] merged 3 commits into
tensorflow:masterfrom
bodapatisaikrishna:fix-signal-kaiser-windows
Oct 22, 2026
Merged

copybara-service[bot] merged 3 commits into
tensorflow:masterfrom
bodapatisaikrishna:fix-signal-kaiser-windows

Conversation

@bodapatisaikrishna

Copy link
Copy Markdown
Contributor

Fixes #129234, fixes #129235.

kaiser_window with a dynamic length of 1

kaiser_window special-cases length 1, but only when the length is a constant. Inside a tf.function that takes the length as an argument, the half length is 0, arg / halflen is 0/0, and the window comes out as [nan]. Hann and Hamming don't have this problem because _raised_cosine_window wraps the computation in a cond.

I went with something smaller than a cond: when the half length is 0, the code divides by 1 instead. The window's only sample is at the center, so that gives exactly [1.], and the rest of the computation doesn't change. The gradient with respect to beta stays finite too, and one of the new tests checks it.

kaiser_bessel_derived_window with an odd length

The window is built from two halves of window_length // 2 samples each. For an odd length that quietly returns one sample fewer than requested (an empty tensor for length 1), even though the docstring promises shape [window_length]. KBD windows are only defined for even lengths, and scipy.signal.windows.kaiser_bessel_derived raises for odd ones, so this now does the same. A statically known odd length raises ValueError. A length only known at run time gets an assert_equal on window_length % 2.

Static even lengths don't pick up any new ops, so test_constant_folding still folds the window to a single node. mdct and inverse_mdct already require frame lengths divisible by 4, so nothing changes for them.

Testing

New tests:

  • kaiser_window with a dynamic length, for every length in _WINDOW_LENGTHS and every dtype
  • the length-1 gradient
  • KBD output length for even lengths
  • KBD rejecting odd lengths, both static and dynamic

All of them fail on master. With the patched window_ops.py dropped into tf-nightly 2.23.0-dev20261014, all 435 cases in window_ops_test.py pass in both graph and eager mode, and so do the mdct tests in spectral_ops_test.py.

kaiser_window only special-cased a length of 1 when the length was a
constant. With a length known only at run time (e.g. a tf.function
argument) the half length is 0 and `arg / halflen` became 0/0, so the
window was [nan]. Divide by 1 in that case instead, which yields the
expected [1.] and keeps the gradient with respect to beta finite.

kaiser_bessel_derived_window built two halves of window_length // 2
samples, so an odd length silently produced window_length - 1 samples
(and an empty window for length 1), contradicting the documented
[window_length] shape. KBD windows are only defined for even lengths
(SciPy raises as well), so raise a ValueError for a static odd length
and add a runtime assertion when the length is dynamic.

Fixes tensorflow#129234
Fixes tensorflow#129235
@google-ml-butler google-ml-butler Bot added the size:M CL Change Size: Medium label Oct 17, 2026
@google-ml-butler
google-ml-butler Bot requested a review from cantonios October 17, 2026 17:36
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Oct 17, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds support and validation for dynamic window lengths in kaiser_window and kaiser_bessel_derived_window. Specifically, it handles the division-by-zero edge case for length-1 Kaiser windows under dynamic execution and enforces that the window length for Kaiser-Bessel derived windows is even, both statically and dynamically. Unit tests have been added to cover these scenarios. Feedback on the changes suggests avoiding direct floating-point equality comparison when checking for zero-length halves in kaiser_window, recommending instead to compare the integer window_length with 1 to prevent potential precision issues.

Same result as comparing halflen_float with 0, which is exact, but it reads more directly and avoids a float comparison.
@nithyak0204 nithyak0204 self-assigned this Oct 19, 2026
@nithyak0204
nithyak0204 requested a review from a team October 19, 2026 05:16
@nithyak0204 nithyak0204 added python Pull requests that update Python code prtype:bugfix PR to fix a bug labels Oct 19, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Oct 19, 2026

@dmiltr3 dmiltr3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1. Keep only the static ValueError check in kaiser_bessel_derived_window and remove the dynamic check_ops.assert_equal branch

  • Location: tensorflow/python/ops/signal/window_ops.py:24,135-165, tensorflow/python/ops/signal/BUILD:192, tensorflow/python/kernel_tests/signal/window_ops_test.py:28,306-317, tensorflow/python/kernel_tests/signal/BUILD:185
  • Why: In tf.signal.inverse_mdct (tensorflow/python/ops/signal/spectral_ops.py:468,484), the window function is called as window_fn(2 * half_len, dtype=mdcts.dtype). In graph mode and inside @tf.function, 2 * half_len is a symbolic Mul tensor (op.type == "Mul"), for which tensor_util.constant_value(window_length) returns None even when mdcts.shape[-1] is statically known. As a result, the else: branch in kaiser_bessel_derived_window executes on every graph-mode or @tf.function call to inverse_mdct(..., window_fn=tf.signal.kaiser_bessel_derived_window), injecting FloorMod, Equal, Assert, control_dependencies, and Identity ops into the runtime graph on valid inputs. Across tf.signal (for example mdct in tensorflow/python/ops/signal/spectral_ops.py:375-382 and _validate_positive_static in tensorflow/python/ops/signal/spectral_ops.py:310-328), static integer checks use tensor_util.constant_value to raise ValueError without adding runtime Assert ops when the tensor is symbolic.
  • Required update in tensorflow/python/ops/signal/window_ops.py:
  Raises:
    ValueError: If `dtype` is not a floating point type, or if `window_length`
      is statically known to be odd.
    window_length = _check_params(window_length, dtype)
    # Each half has `window_length // 2` samples, so an odd length would
    # silently come out one sample short.
    window_length_const = tensor_util.constant_value(window_length)
    if window_length_const is not None and window_length_const % 2:
      raise ValueError(
          '`window_length` must be even for a Kaiser-Bessel derived window. '
          f'Received: window_length={window_length_const}'
      )
    halflen = window_length // 2
  • Please also remove the unused from tensorflow.python.ops import check_ops import from tensorflow/python/ops/signal/window_ops.py, "//tensorflow/python/ops:check_ops", from tensorflow/python/ops/signal/BUILD, the dynamic InvalidArgumentError assertion in test_kaiser_bessel_derived_window_odd_dynamic_length and unused from tensorflow.python.framework import errors import in tensorflow/python/kernel_tests/signal/window_ops_test.py, and "//tensorflow/python/framework:errors", from tensorflow/python/kernel_tests/signal/BUILD.

@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Reviewer Requested Changes in PR Queue Oct 19, 2026
@nithyak0204 nithyak0204 added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review labels Oct 19, 2026
Avoid adding runtime assertions for symbolic window lengths to prevent
injecting assertion ops into inverse_mdct graph-mode calls.
@bodapatisaikrishna

Copy link
Copy Markdown
Contributor Author

Makes sense, thanks for the explanation. Dropped the dynamic check branch and cleaned up the unused deps.

@google-ml-butler google-ml-butler Bot removed the stat:awaiting response Status - Awaiting response from author label Oct 19, 2026
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Oct 19, 2026
@github-project-automation github-project-automation Bot moved this from Reviewer Requested Changes to Approved by Reviewer in PR Queue Oct 19, 2026
@copybara-service
copybara-service Bot merged commit f168c48 into tensorflow:master Oct 22, 2026
17 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kokoro:force-run Tests on submitted change prtype:bugfix PR to fix a bug python Pull requests that update Python code ready to pull PR ready for merge process size:M CL Change Size: Medium

Projects

Status: Merged

3 participants