-
Notifications
You must be signed in to change notification settings - Fork 19.7k
Closed
Labels
Description
Description
The keras.ops.stft operation lacks proper input validation for the sequence_stride parameter. This leads to two specific issues across all backends:
- Negative Stride Failure: A negative
sequence_stridecauses the operation to return an unexpected object type (tuple) instead of a tensor, leading to'tuple' object has no attribute 'shape'errors during downstream processing. This indicates a silent internal failure. - Zero Stride Crash: A zero
sequence_strideleaks to the backend and causes a rawInvalidArgumentError(Integer division by zero) instead of a unified KerasValueError.
Minimal Reproducible Example
import os
import numpy as np
import keras
import keras.ops as ops
# Set backend before running
# os.environ["KERAS_BACKEND"] = "tensorflow"
def reproduce_stft_bug():
print(f"Testing Backend: {keras.backend.backend()}")
x = ops.convert_to_tensor(np.random.rand(1, 1024).astype("float32"))
print("\n--- Test 1: Negative Stride ---")
try:
# Expected: ValueError | Observed: Returns a tuple, causing AttributeError on .shape
res_neg = ops.stft(x, sequence_length=256, sequence_stride=-5, fft_length=256)
print(f"Result shape: {res_neg.shape}")
except Exception as e:
print(f"Caught negative stride issue: {e}")
print("\n--- Test 2: Zero Stride ---")
try:
# Expected: ValueError | Observed: Raw Backend Crash
res_zero = ops.stft(x, sequence_length=256, sequence_stride=0, fft_length=256)
except Exception as e:
print(f"Observed: Caught by backend with {type(e).__name__}: {e}")
if __name__ == "__main__":
reproduce_stft_bug()Observed Output
Testing Backend: tensorflow
--- Test 1: Negative Stride ---
SUCCESS: Caught negative stride: 'tuple' object has no attribute 'shape'
--- Test 2: Zero Stride ---
OBSERVED: Caught by backend with InvalidArgumentError: {{function_node __wrapped__FloorDiv_device_/job:localhost/replica:0/task:0/device:GPU:0}} Integer division by zero [Op:FloorDiv] name:
NOTE: This should be caught by Keras validation as a ValueError, not a raw backend error.
Observed Behavior
- Negative Stride: All backends proceed without validating the stride. In TensorFlow, the operation fails internally and returns a tuple, which breaks any subsequent tensor operations.
- Zero Stride: Triggers
InvalidArgumentError: Integer division by zero [Op:FloorDiv].
Expected Behavior
keras.ops.stft should validate that sequence_stride > 0 and raise a ValueError consistently across all backends.
System Information (Google Colab)
==============================
System Info
==============================
OS : Linux 6.6.105+
Python version : 3.12.12
Keras version : 3.10.0
Keras Backend : tensorflow
==============================
GPU Info
==============================
GPU Model : Tesla T4
CUDA Available : Yes
CUDA Version : 12.8
==============================
Library Versions
==============================
jax 0.7.2
jax-cuda12-pjrt 0.7.2
jax-cuda12-plugin 0.7.2
jaxlib 0.7.2
numpy 2.0.2
tensorflow 2.19.0
tensorflow-datasets 4.9.9
tensorflow_decision_forests 1.12.0
tensorflow-hub 0.16.1
tensorflow-metadata 1.17.3
tensorflow-probability 0.25.0
tensorflow-text 2.19.0
torch 2.9.0+cu128
torchao 0.10.0
torchaudio 2.9.0+cu128
torchcodec 0.8.0+cu128
torchdata 0.11.0
torchsummary 1.5.1
torchtune 0.6.1
torchvision 0.24.0+cu128
Conclusion
The current implementation of keras.ops.stft relies on backend-specific behavior for input validation instead of a unified Keras validation layer. This results in:
- Silent Logic Errors: Negative strides are accepted, causing internal failures and returning inconsistent object types (tuples).
- Process Crashes: Zero strides leak to the backend kernels, triggering raw
InvalidArgumentErroror C++ level division-by-zero instead of a high-levelValueError.
A centralized validation check ensuring sequence_stride > 0 is necessary to maintain cross-backend consistency and framework stability.
Reactions are currently unavailable