CPU can incur a slow path on non-contiguous magnitudes
Author: vbayanag
Pull request: yes

<!-- ci-dashboard-badge:start -->
[![CI](https://transformers-ci.lor-e.huggingface.cool/badge/pr?pr=47351)](https://transformers-ci.lor-e.huggingface.cool/d/pytest-observability-by-pr/pytest-observability-branch?var-pr=47351)
<!-- ci-dashboard-badge:end -->

# What does this PR do?
`stft[..., :-1]` produces a non-contiguous view. Asobserved on MI355X ROCm vLLM latest build image, the audio preprocessing function `mel_filters.T @ magnitudes` falls onto a strided-GEMM path that runs ~8x slower (~43 ms vs ~3 ms for a 30 s audio window), dominating Whisper feature extraction.

Forcing `magnitudes` contiguous costs ~0.18 ms but restores the fast matmul path, cutting per-request audio preprocessing substantially.

Fixes # (issue)

Make the CPU op run on contiguous memory layout for ~8x speedup

### Before/after (single 30 s window, ROCm vLLM image)
| stage | non-contiguous | contiguous |
| --- | --- | --- |
| `mel_filters.T @ magnitudes` | ~43 ms | ~3 ms |
| `.contiguous()` overhead | – | ~0.18 ms |

## Code Agent Policy

The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. These often are low-quality, or fix extremely minor issues that occur rarely or never in practice.
As a result, we're instituting a rule that **first-time contributors should not use code agents to submit PRs or issues**.
We'd also ask autonomous "OpenClaw"-like agents not to open any PRs or issues.

Issues/PRs from first-time contributors that violate this rule will probably just be closed without review, and we
might block you, especially if you open more than one or appear to be deliberately ignoring this. We especially do not
want new contributors to jump in on random issues to contribute an agent-written fix. This creates lots of noise
for reviewers and other users and will almost certainly get you blocked.

For more information, please read [`CONTRIBUTING.md`](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md).

- [x] (First-time contributors only): I confirm that this PR description and code is not written by an LLM or code agent

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://huggingface.co/docs/transformers/contributing) and the
      [Pull Request](https://huggingface.co/docs/transformers/pr_checks) checks?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes according to the [guidelines](https://github.com/huggingface/transformers/tree/main/docs)?
- [ ] Did you write any new necessary [tests](https://huggingface.co/docs/transformers/testing)?


## Who can review?

@eustlb @ebezzam @vasqu

Comments:
> Happy to add the fix on 2 conditions
> 
> 1. Shorten the comment --> `cpu can incurr a slow path on non-contiguous magnitutes`
> 2. Add a fast test that can show this or a benchmark with this PR so we can verify as well

Hi @vasqu, the posted benchmarks in the description are from before/after change. Kindly let me know if you need anymore information.

Thank you for your contribution 🤗!

## CI Security Gate — automatic approval blocked

This PR was **not** automatically approved for CI because the security gate failed.

**Possible reasons:**
- The PR touches 50 or more files — only PRs with fewer than 50 changed files are automatically approved
- A changed file is outside the allowed directories (`src/`, `tests/`, `docs/`, `utils/`), has a disallowed extension (only `.py`, `.txt`, `.md` permitted outside `tests/` and `docs/`), or is not `.md`/`.yml` inside `docs/`
- A new high-severity security issue was detected in the changed Python files (Bandit check)

See the [workflow run](https://github.com/huggingface/transformers/actions/runs/29534317417) for the exact violations.

A maintainer can review and manually approve CI if a finding is a false positive.

@vbayanag I meant like a small benchmark script :D the numbers are impressive but imo for anyone stumbling on this, it's always nice to have the exact code that produced the numbers along with it

**[For maintainers]** Suggested jobs to run (before merge)

run-slow: whisper

<!-- ci-dashboard-recap:start -->

---

### CI recap

**Dashboard:** [View test results in Grafana](https://transformers-ci.lor-e.huggingface.cool/d/pytest-observability-by-pr/pytest-observability-branch?var-pr=47351)
**Latest run:** [29617900406:2](https://github.com/huggingface/transformers/actions/runs/29617900406)
**Result:** `failure` | **Jobs:** 2 | **Tests:** 22 | **Failures:** 1 | **Duration:** 1m 56s

> **Code quality check failed**: test jobs were skipped. Fix the code quality issues and push again to run tests.

<!-- ci-dashboard-recap:end -->

> @vbayanag I meant like a small benchmark script :D the numbers are impressive but imo for anyone stumbling on this, it's always nice to have the exact code that produced the numbers along with it

Hi @vasqu, I added a test case in the relevant test location. Please check.

Local testing shows 5x uplift. This number is different from what's in description, likely due to difference in the way metrics were extracted. The initial posted numbers were extracted from feature_extractor API call from the trace file of end-to-end inference testing of whisper model, whereas the uplift observed now is localised only to the matmul operation.

`[whisper mel matmul] non-contiguous=10.269 ms contiguous=1.999 ms speedup=5.14x`

System details:

1. Docker pull and launch `vllm/vllm-openai-rocm:latest` image
```
docker run -it  --group-add=video    --ipc=host    --cap-add=SYS_PTRACE    --security-opt seccomp=unconfined    --device /dev/kfd    --device /dev/dri -v /:/data --entrypoint ''  vllm/vllm-openai-rocm:latest /bin/bash
```
2. Install depenencies (optional, only required for other test cases to run smoothly)
```
apt-get update && apt-get install -y ffmpeg
pip install soundfile librosa torchcodec==0.12
```
3. Run the python unit test inside your clone dir
```
PYTHONPATH=src python3 -m pytest     tests/models/whisper/test_feature_extraction_whisper.py -k contiguous_magnitudes -sv
```