harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0083harbor download openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0083Medium difficulty redesign
Summary
Create a self-contained, PyTorch-based STFT/Inverse-STFT utility inside /app/openunmix.py that exposes a small transforms submodule. The tests will import as:
from openunmix import transforms
Your task is to implement a single-file solution that provides a differentiable STFT/ISTFT pair via a factory function:
- transforms.make_filterbanks(n_fft: int, n_hop: int, center: bool = True, method: str = "torch") -> (stft, istft)
The function should return two callables (stft, istft) that operate on batched audio tensors with shapes and behaviors described below. The implementation must be self-contained (no external dependencies beyond PyTorch).
What to implement
1) Location and API
- File: /app/openunmix.py
- Expose a transforms object/module-like attribute with a function:
- make_filterbanks(n_fft: int, n_hop: int, center: bool = True, method: str = "torch") -> (stft, istft)
- The two returned callables:
- stft(audio: torch.Tensor) -> torch.Tensor
- istft(X: torch.Tensor, length: int) -> torch.Tensor
2) Behavior and shapes
- Input/Output shapes for stft
- Input audio: shape (B, C, T) where B = nb_samples, C = nb_channels, T = nb_timesteps
- Processing: flatten the first two dimensions to (N, T) with N = B * C, perform STFT in batch, then reshape results back to (B, C, F, T_out)
- STFT output X should have shape (B, C, F, T_out) with:
- F = n_fft // 2 + 1
- T_out is determined by PyTorch’s STFT settings
- Dtype: complex with the same precision as input (complex64 or complex128)
- Input/Output shapes for istft
- Input X: shape (B, C, F, T_out)
- Parameters: length is the target time-domain length to reconstruct
- Processing: flatten X to (N, F, T_out), perform ISTFT, then reshape back to (B, C, length)
- Output: real tensor with shape (B, C, length)
- Implementation details
- Use a Hann window of length n_fft
- Use PyTorch’s STFT/ISTFT with return_complex=True
- Use the same window parameters for stft and istft
- Centering behavior is controlled by the center parameter, and must be passed to both stft and istft
- The implementation must be differentiable (autograd-friendly) and work with autodiff, so no .detach() in the core path
3) Method parameter mapping
- method must be either "torch" or "asteroid"
- For both values, implement the same underlying STFT/ISTFT logic
- If any other value is provided, raise a clear ValueError with a helpful message
4) Validation and robustness
- Validate inputs:
- n_fft and n_hop should be positive integers
- n_fft should be even (informative but not strictly required; if you want, you can enforce)
- audio for stft must be 3D (B, C, T)
- X for istft must be 4D (B, C, F, T_out)
- length for istft must be a non-negative integer
- method must be one of the accepted values
- Provide helpful error messages for invalid inputs
- Do not rely on environment-specific features beyond PyTorch
- Include docstrings for both stft and istft detailing input/output shapes and behavior
5) Documentation and examples
- Include docstrings directly in the stft and istft callables describing:
- Input shapes
- Output shapes
- What the functions do (differentiable STFT/ISTFT)
- Ensure the implementation is fully self-contained in /app/openunmix.py
6) Edge cases (moderate)
- center toggles between True/False to verify padding behavior
- Non-trivial batch sizes, e.g., B=1 with multiple channels, and B>1
- Very short sequences where T < n_fft (verify PyTorch’s STFT behavior with center/padding)
- Ensure exact reconstruction when length matches the original signal length and parameters are consistent
7) Acceptance criteria mapping
- Import path: from openunmix import transforms should work and transforms.make_filterbanks should exist
- The returned stft and istft are callable and support PyTorch autograd
- For a variety of n_fft, n_hop, nb_timesteps, nb_samples, nb_channels, and method values ("torch"/"asteroid"):
- stft(audio) followed by istft(X, length=audio.shape[-1]) reconstructs the original audio with mean-squared error < 1e-6
- Output shapes:
- stft: (B, C, F, T_out) with F = n_fft//2 + 1
- istft input: (B, C, F, T_out)
- istft output: (B, C, length)
8) Hints and best practices
- Build window dynamically inside each closure using the input’s device and dtype:
- window = torch.hann_window(n_fft, device=audio.device, dtype=audio.dtype)
- Flatten and unflatten carefully to preserve the (B, C) pairing
- Keep the interface clean and well-documented, including type hints and robust error messages
Notes
- The solution should be a single-file, self-contained implementation placed at /app/openunmix.py
- Tests will import as: from openunmix import transforms
- The tests will parametrize method with "torch" and "asteroid" and require identical behavior