mirror of
https://github.com/linkedin/Liger-Kernel.git
synced 2026-10-12 02:17:47 +00:00
## Motivation This pr ports `fused_moe.py` and `fused_moe_kernels.py` to an NPU-affine implementation while preserving the original math. The computational definition is unchanged: forward remains `W1 (gate/up) -> SwiGLU -> W2 -> token-weighted gather`, and backward still follows `dA' = dO @ W2^T` to produce `d_pre_act / dS / dW2 / dX / dW1`. The main changes are execution-strategy optimizations for NPU. ## Note: Use the Skill For this fused_moe kernel migration, we followed the skill document from https://github.com/linkedin/Liger-Kernel/issues/1197. ## Testing Done - Hardware Type: Ascend 910B2 - [x] run `make test` to ensure correctness - [x] run `make checkstyle` to ensure code style - [x] run `make test-convergence` to ensure convergence 🤖 Generated with: [cursor](https://cursor.com/).
Benchmarking Liger Kernels
Follow these steps to benchmark and visualize kernel performance:
-
Create a benchmark script
- Add your script under
benchmark/scripts/ - Name it according to the kernel (e.g.,
benchmark_<kernel_name>.py)
- Add your script under
-
Run the benchmark
- Results will be saved to
benchmark/data/all_benchmark_data.csv
Example: Benchmarking KTO Loss
cd benchmark python scripts/benchmark_kto_loss.py - Results will be saved to
-
Visualize results
-
Use the visualization script with optional modes:
--sweep-mode: Select which sweep data to plot.token_length(default): plots where x-axis is sequence length.model_config: plots where x-axis is model configuration.
- To target specific operation mode(s), pass
--kernel-operation-modeone or more values. - If you omit
--kernel-operation-mode, the script will:- For
speedmetrics: generate plots for all available modes (forward/backward/full). - For
memorymetrics: generate only thefullplot.
- For
Examples:
- Token-length sweep, specific modes (speed):
python benchmarks_visualizer.py \ --kernel-name kto_loss \ --metric-name speed \ --kernel-operation-mode forward backward- Token-length sweep, all modes (speed):
python benchmarks_visualizer.py \ --kernel-name kto_loss \ --metric-name speed- Model-config sweep (speed):
python benchmarks_visualizer.py \ --kernel-name geglu \ --metric-name speed \ --sweep-mode model_config- Memory (always full):
python benchmarks_visualizer.py \ --kernel-name kto_loss \ --metric-name memory -
-
View results
- Generated plots will be saved in
benchmark/visualizations/ - Filenames include the sweep mode when specified (e.g.
geglu_speed_full_model_config.png)
- Generated plots will be saved in