MPS: RoPE and RMSNorm in fp32 take 30-46% of Mel-RoFormer attention time

Abierto
#314 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
42/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python, pytorch

Línea de trabajo

Start by reading uvr_lib_v5/roformer/rotary.py and inspect the RoPE and RMSNorm paths used with native fp16 on MPS. Compare the supplied monkeypatch and its benchmark results, then determine how to preserve the fp32 autocast path while applying the optimization. Done means the change improves performance and maintains output quality across more than the single reported model and device.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

On Apple Silicon, most of the attention time for Mel-Band RoFormer goes to RoPE and RMSNorm instead of the attention itself. I profiled vocals_mel_band_roformer.ckpt with --use_native_fp16 on an M3 MacBook Air (8 GB, audio-separator 0.47.0, torch 2.14.1, MPS):

op in one attention block time block (60 x 1101) freq block (1101 x 60)
whole block 442 ms 366 ms
RoPE on q and k 131 ms (30%) 170 ms (46%)
SDPA 148 ms 11 ms
RMSNorm 33 ms 41 ms

The cause is rotate_queries_or_keys in uvr_lib_v5/roformer/rotary.py. It upcasts q and k to fp32 and computes cos/sin on every call. RMSNorm also goes through fp32 F.normalize.

What I tried, and what it gave:

  • build the cos/sin tables once per sequence length in fp32, cast them to the model dtype, and rotate in that dtype
  • permute the q/k weight rows to the split-half layout so the rotation is two slices and a cat (q·k doesn't change because both get the same permutation)
  • F.rms_norm in the model dtype
  • fold to_gates into the qkv matmul

On a 3 minute clip that took fp16 from 1.35x to 2.19x real time. The output stayed at 66.7 dB SDR against the fp32 output (stock fp16 is 65.9 dB). The code is a small monkeypatch: https://github.com/aipdv/fast-melroformer/blob/main/fastmbr/patch.py

I know the fp32 RoPE path exists for precision under autocast, so maybe keep it for autocast and use precomputed tables for native fp16. I've only tested this one model on one machine. I'm not planning to open a PR, but the patch is MIT, so take whatever is useful.

Lenguaje dominante
Python
Estrellas
1.5k
Forks
216
Merge medio
1 h
PR fusionados (30 d)
2

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de nomadkaraoke/python-audio-separator

Todos los issues de nomadkaraoke/python-audio-separator

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.