Repository navigation
Fix kaiser_window NaN for dynamic length 1 and reject odd KBD window lengths - #129242
copybara-service[bot] merged 3 commits into
Conversation
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
There was a problem hiding this comment.
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.
dmiltr3
left a comment
There was a problem hiding this comment.
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 aswindow_fn(2 * half_len, dtype=mdcts.dtype). In graph mode and inside@tf.function,2 * half_lenis a symbolicMultensor (op.type == "Mul"), for whichtensor_util.constant_value(window_length)returnsNoneeven whenmdcts.shape[-1]is statically known. As a result, theelse:branch inkaiser_bessel_derived_windowexecutes on every graph-mode or@tf.functioncall toinverse_mdct(..., window_fn=tf.signal.kaiser_bessel_derived_window), injectingFloorMod,Equal,Assert,control_dependencies, andIdentityops into the runtime graph on valid inputs. Acrosstf.signal(for examplemdctintensorflow/python/ops/signal/spectral_ops.py:375-382and_validate_positive_staticintensorflow/python/ops/signal/spectral_ops.py:310-328), static integer checks usetensor_util.constant_valueto raiseValueErrorwithout adding runtimeAssertops 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_opsimport fromtensorflow/python/ops/signal/window_ops.py,"//tensorflow/python/ops:check_ops",fromtensorflow/python/ops/signal/BUILD, the dynamicInvalidArgumentErrorassertion intest_kaiser_bessel_derived_window_odd_dynamic_lengthand unusedfrom tensorflow.python.framework import errorsimport intensorflow/python/kernel_tests/signal/window_ops_test.py, and"//tensorflow/python/framework:errors",fromtensorflow/python/kernel_tests/signal/BUILD.
Avoid adding runtime assertions for symbolic window lengths to prevent injecting assertion ops into inverse_mdct graph-mode calls.
|
Makes sense, thanks for the explanation. Dropped the dynamic check branch and cleaned up the unused deps. |
Fixes #129234, fixes #129235.
kaiser_windowwith a dynamic length of 1kaiser_windowspecial-cases length 1, but only when the length is a constant. Inside atf.functionthat takes the length as an argument, the half length is 0,arg / halflenis 0/0, and the window comes out as[nan]. Hann and Hamming don't have this problem because_raised_cosine_windowwraps the computation in acond.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 tobetastays finite too, and one of the new tests checks it.kaiser_bessel_derived_windowwith an odd lengthThe window is built from two halves of
window_length // 2samples 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, andscipy.signal.windows.kaiser_bessel_derivedraises for odd ones, so this now does the same. A statically known odd length raisesValueError. A length only known at run time gets anassert_equalonwindow_length % 2.Static even lengths don't pick up any new ops, so
test_constant_foldingstill folds the window to a single node.mdctandinverse_mdctalready require frame lengths divisible by 4, so nothing changes for them.Testing
New tests:
kaiser_windowwith a dynamic length, for every length in_WINDOW_LENGTHSand every dtypeAll of them fail on master. With the patched
window_ops.pydropped into tf-nightly 2.23.0-dev20261014, all 435 cases inwindow_ops_test.pypass in both graph and eager mode, and so do themdcttests inspectral_ops_test.py.