{"id":45220326,"url":"https://github.com/ssmall256/mps-spectro","last_synced_at":"2026-02-23T21:01:38.033Z","repository":{"id":339809986,"uuid":"1160304117","full_name":"ssmall256/mps-spectro","owner":"ssmall256","description":"Fast STFT/ISTFT for PyTorch MPS with fused Metal kernels and autograd support","archived":false,"fork":false,"pushed_at":"2026-02-20T16:27:34.000Z","size":46,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-02-21T23:20:08.492Z","etag":null,"topics":["apple-silicon","audio","autograd","gpu","istft","metal","mps","pytorch","signal-processing","stft"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ssmall256.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-02-17T19:31:02.000Z","updated_at":"2026-02-19T22:00:38.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/ssmall256/mps-spectro","commit_stats":null,"previous_names":["ssmall256/mps-spectro"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/ssmall256/mps-spectro","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssmall256%2Fmps-spectro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssmall256%2Fmps-spectro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssmall256%2Fmps-spectro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssmall256%2Fmps-spectro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ssmall256","download_url":"https://codeload.github.com/ssmall256/mps-spectro/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssmall256%2Fmps-spectro/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29725287,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-22T19:57:12.410Z","status":"ssl_error","status_checked_at":"2026-02-22T19:54:50.710Z","response_time":110,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apple-silicon","audio","autograd","gpu","istft","metal","mps","pytorch","signal-processing","stft"],"created_at":"2026-02-20T18:15:39.507Z","updated_at":"2026-02-22T20:00:44.222Z","avatar_url":"https://github.com/ssmall256.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mps-spectro\n\nFast `torch.stft` / `torch.istft` replacements for Apple MPS, powered by custom Metal kernels.\n\n- Fused overlap-add with optimized Metal kernels\n- PyTorch-compatible STFT/ISTFT semantics\n- Autograd support with custom Metal backward kernels\n- Drop-in replacement for `torch.stft` / `torch.istft`\n\n## Install\n\n```bash\npip install -e .\n```\n\n## Quick Start\n\n```python\nimport torch\nfrom mps_spectro import stft, istft\n\nx = torch.randn(1, 16000, device=\"mps\")\n\nspec = stft(x, n_fft=1024, hop_length=256)\ny = istft(spec, n_fft=1024, hop_length=256, center=True)\n```\n\n`stft` and `istft` accept the same parameters as `torch.stft` / `torch.istft` (n_fft, hop_length, win_length, window, center, normalized, onesided, length).\n\n### Autograd\n\nBoth `stft` and `istft` support PyTorch autograd when inputs have `requires_grad=True`:\n\n```python\nx = torch.randn(4, 16000, device=\"mps\", requires_grad=True)\n\nspec = stft(x, n_fft=1024, hop_length=256)\ny = istft(spec, n_fft=1024, hop_length=256, center=True, length=16000)\n\nloss = y.pow(2).sum()\nloss.backward()\nprint(x.grad.shape)  # torch.Size([4, 16000])\n```\n\nWhen `requires_grad=False` (the default), zero overhead -- the original Metal kernel path is used directly. Backward passes use custom Metal kernels for GPU-accelerated gradient computation. Window gradients are not computed (returns `None`) since windows are almost always frozen in practice.\n\n### ISTFT extras\n\n`istft` also supports:\n\n- `torch_like=True` -- raise on NOLA violations like `torch.istft`\n- `safety=\"auto\"|\"always\"|\"off\"` -- NOLA envelope safety checking\n- `kernel_dtype=\"float32\"|\"float16\"|\"mixed\"` -- Metal kernel precision\n- `kernel_layout=\"auto\"|\"native\"|\"transposed\"` -- memory layout selection\n\n## Benchmarks\n\nApple M4 Max, macOS 26.3, PyTorch 2.10.0, 20 iterations (5 warmup).\n\n### STFT Forward\n\n| Config | torch MPS | mps_spectro | Speedup |\n|---|--:|--:|--:|\n| B=4 T=160k nfft=1024 | 1.08 ms | 0.29 ms | 3.7x |\n| B=4 T=160k nfft=2048 | 1.06 ms | 0.29 ms | 3.6x |\n| B=8 T=160k nfft=1024 | 0.55 ms | 0.41 ms | 1.4x |\n| B=4 T=1.3M nfft=1024 | 1.80 ms | 1.37 ms | 1.3x |\n\n### ISTFT Forward\n\n| Config | torch MPS | mps_spectro | Speedup |\n|---|--:|--:|--:|\n| B=4 T=160k nfft=1024 | 1.11 ms | 0.50 ms | 2.2x |\n| B=8 T=160k nfft=1024 | 1.66 ms | 0.74 ms | 2.3x |\n| B=4 T=1.3M nfft=1024 | 5.65 ms | 2.36 ms | 2.4x |\n| B=1 T=1.3M nfft=1024 | 1.74 ms | 0.75 ms | 2.3x |\n\n### Differentiable STFT + ISTFT (Forward + Backward)\n\n| Config | torch MPS | mps_spectro | Speedup |\n|---|--:|--:|--:|\n| B=4 T=160k nfft=1024 | 1.37 ms | 1.04 ms | 1.3x |\n| B=8 T=160k nfft=1024 | 2.72 ms | 1.81 ms | 1.5x |\n| B=4 T=1.3M nfft=1024 | 12.20 ms | 8.88 ms | 1.4x |\n| B=1 T=1.3M nfft=1024 | 2.66 ms | 1.78 ms | 1.5x |\n\n### Roundtrip (STFT -\u003e ISTFT) Forward + Backward\n\n| Config | torch MPS | mps_spectro | Speedup |\n|---|--:|--:|--:|\n| B=4 T=160k nfft=1024 | 2.33 ms | 1.27 ms | 1.8x |\n| B=8 T=160k nfft=1024 | 4.28 ms | 2.38 ms | 1.8x |\n| B=4 T=1.3M nfft=1024 | 17.56 ms | 10.43 ms | 1.7x |\n| B=1 T=1.3M nfft=1024 | 4.25 ms | 2.36 ms | 1.8x |\n\nTo reproduce: `python scripts/benchmark.py`\n\n## How it works\n\n1. **STFT**: a tiled Metal kernel loads overlapping signal chunks into threadgroup shared memory (~3x data reuse for typical n_fft/hop ratios), applies reflect-padding and windowing in one pass, then `torch.fft.rfft` for the FFT\n2. **ISTFT**: `torch.fft.irfft` on MPS, then a fused Metal kernel for synthesis-window multiply + overlap-add + envelope normalization\n\n## Requirements\n\n- macOS with Apple Silicon (MPS)\n- Python 3.12+\n- PyTorch 2.10+\n- Xcode command-line tools (for JIT Metal kernel compilation)\n\n## Tests\n\n```bash\npip install -e \".[dev]\"\npytest\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssmall256%2Fmps-spectro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fssmall256%2Fmps-spectro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssmall256%2Fmps-spectro/lists"}