Files
lowdy1 ace042ec55 [Benchmark] Refactor benchmark for linear kernels (#1223)
## Summary
This PR is a descendant of [[PR
#1195](https://github.com/linkedin/Liger-Kernel/pull/1195)] and
continues the benchmark refactor effort by migrating
linear-space-complexity benchmark scripts to the new reusable `setup`
helper infrastructure.



### Benefits

* Significantly reduces benchmark script complexity
* Makes new benchmark additions easier and more consistent
* Centralizes probing and sweep logic
* Improves maintainability of benchmark infrastructure


### Notes

This PR mainly focuses on kernels with approximately linear memory
complexity and migrates them to the new benchmark helper APIs introduced
in the parent refactor.

- [ ] run `make test` to ensure correctness
- [x] run `make checkstyle` to ensure code style
- [ ] run `make test-convergence` to ensure convergence
2026-05-15 18:22:54 +00:00

111 lines
3.5 KiB
Python

import torch
from benchmark_model_configs import MODEL_REGISTRY
from benchmark_model_configs import build_model_config_sweep
from benchmark_model_configs import build_token_length_sweep
from benchmark_model_configs import get_benchmark_model_config
from transformers.models.llama.configuration_llama import LlamaConfig
from transformers.models.llama.modeling_llama import LlamaMLP
from utils import SingleBenchmarkRunInput
from utils import build_memory_bench_fn
from utils import build_speed_bench_fn
from utils import parse_benchmark_script_args
from utils import run_benchmarks
from liger_kernel.transformers.swiglu import LigerSwiGLUMLP
from liger_kernel.utils import infer_device
device = infer_device()
def setup_swiglu(input: SingleBenchmarkRunInput):
"""Create input tensor and SwiGLU layer from benchmark config."""
cfg = input.extra_benchmark_config
if isinstance(input.x, str):
model_cfg = MODEL_REGISTRY[input.x]
seq_len = cfg["seq_len"]
hidden_size = model_cfg.hidden_size
intermediate_size = model_cfg.intermediate_size
dtype = model_cfg.dtype
else:
seq_len = input.x
hidden_size = cfg["hidden_size"]
intermediate_size = cfg["intermediate_size"]
dtype = cfg["dtype"]
llama_config = LlamaConfig(
hidden_size=hidden_size,
intermediate_size=intermediate_size,
hidden_act=cfg["hidden_act"],
)
x = torch.randn(
cfg["bsz"],
seq_len,
hidden_size,
device=device,
dtype=dtype,
requires_grad=True,
)
if input.kernel_provider == "liger":
layer = LigerSwiGLUMLP(config=llama_config).to(device).to(dtype)
elif input.kernel_provider == "huggingface":
layer = LlamaMLP(config=llama_config).to(device).to(dtype)
else:
raise ValueError(f"Invalid provider: {input.kernel_provider} for SwiGLU")
return x, layer
if __name__ == "__main__":
args = parse_benchmark_script_args()
if args.sweep_mode == "model_config":
common_configs = build_model_config_sweep(
kernel_name="swiglu",
setup_fn=setup_swiglu,
model_keys=["hidden_size", "intermediate_size", "dtype"],
probe_provider="huggingface",
extra_configs={
"bsz": 1,
"hidden_act": "silu",
},
probe_dim="T",
bt=args.bt,
overwrite=args.overwrite,
)
else:
model = get_benchmark_model_config(args.model)
probe_seq_len = 1024
common_configs = build_token_length_sweep(
kernel_name="swiglu",
probe_x=probe_seq_len,
model=model,
setup_fn=setup_swiglu,
model_keys=["hidden_size", "intermediate_size", "dtype"],
extra_configs={
"bsz": 1,
"hidden_act": "silu",
},
scale_dim="T",
x_label="total tokens",
probe_provider="huggingface",
overwrite=args.overwrite,
)
common_configs["kernel_providers"] = ["huggingface", "liger"]
run_benchmarks(
bench_test_fn=build_speed_bench_fn(setup_swiglu),
kernel_operation_modes=["forward", "backward", "full"],
metric_name="speed",
metric_unit="ms",
**common_configs,
)
run_benchmarks(
bench_test_fn=build_memory_bench_fn(setup_swiglu),
kernel_operation_modes=["full", "forward", "backward"],
metric_name="memory",
metric_unit="MB",
**common_configs,
)