mirror of
https://github.com/linkedin/Liger-Kernel.git
synced 2026-10-12 02:17:47 +00:00
## 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
125 lines
3.9 KiB
Python
125 lines
3.9 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 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.jsd import LigerJSD
|
|
from liger_kernel.utils import infer_device
|
|
|
|
device = infer_device()
|
|
|
|
|
|
class TorchJSD(torch.nn.Module):
|
|
def __init__(
|
|
self,
|
|
beta: float = 0.5,
|
|
ignore_index: int = -100,
|
|
dtype: torch.dtype = torch.float,
|
|
):
|
|
super(TorchJSD, self).__init__()
|
|
self.kl = torch.nn.KLDivLoss(reduction="none", log_target=True)
|
|
self.beta = beta
|
|
self.ignore_index = ignore_index
|
|
self.dtype = dtype
|
|
|
|
def forward(
|
|
self,
|
|
log_q: torch.Tensor, # input
|
|
log_p: torch.Tensor, # target
|
|
label=None,
|
|
):
|
|
log_p, log_q = log_p.to(torch.float), log_q.to(torch.float)
|
|
log_p, log_q = log_p.view(-1, log_p.size(-1)), log_q.view(-1, log_q.size(-1))
|
|
m = torch.lerp(torch.exp(log_q), torch.exp(log_p), self.beta)
|
|
loss = self.beta * self.kl(torch.log(m), log_p).sum(dim=-1) + (1 - self.beta) * self.kl(
|
|
torch.log(m), log_q
|
|
).sum(dim=-1)
|
|
|
|
if label is not None:
|
|
loss = torch.where(label != self.ignore_index, loss, 0.0)
|
|
n_non_ignore = (label != self.ignore_index).sum().item()
|
|
if n_non_ignore == 0:
|
|
loss = 0.0
|
|
else:
|
|
loss = (loss / n_non_ignore).sum()
|
|
else:
|
|
loss = (loss / log_q.shape[0]).sum()
|
|
return loss.to(self.dtype)
|
|
|
|
|
|
def setup_jsd(input: SingleBenchmarkRunInput):
|
|
"""Create input tensors and JSD loss from benchmark config."""
|
|
cfg = input.extra_benchmark_config
|
|
if isinstance(input.x, str):
|
|
model_cfg = MODEL_REGISTRY[input.x]
|
|
BT = cfg["seq_len"] * cfg["bsz"]
|
|
V = model_cfg.vocab_size
|
|
else:
|
|
BT = input.x
|
|
V = cfg["vocab_size"]
|
|
|
|
_input = torch.randn(BT, V, requires_grad=True, device=device).log_softmax(dim=-1)
|
|
target = torch.randn(BT, V, device=device).log_softmax(dim=-1)
|
|
|
|
if input.kernel_provider == "liger":
|
|
loss_fn = LigerJSD()
|
|
elif input.kernel_provider == "torch":
|
|
loss_fn = TorchJSD()
|
|
else:
|
|
raise ValueError(f"Invalid provider: {input.kernel_provider} for JSD")
|
|
return _input, lambda _: loss_fn(_input, target)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
args = parse_benchmark_script_args()
|
|
|
|
if args.sweep_mode == "model_config":
|
|
common_configs = build_model_config_sweep(
|
|
kernel_name="jsd",
|
|
setup_fn=setup_jsd,
|
|
model_keys=["vocab_size"],
|
|
probe_provider="torch",
|
|
probe_dim="BT",
|
|
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="jsd",
|
|
probe_x=probe_seq_len,
|
|
model=model,
|
|
setup_fn=setup_jsd,
|
|
model_keys=["vocab_size"],
|
|
scale_dim="BT",
|
|
x_label="total tokens",
|
|
probe_provider="torch",
|
|
overwrite=args.overwrite,
|
|
)
|
|
|
|
common_configs["kernel_providers"] = ["torch", "liger"]
|
|
|
|
run_benchmarks(
|
|
bench_test_fn=build_speed_bench_fn(setup_jsd),
|
|
kernel_operation_modes=["full", "forward", "backward"],
|
|
metric_name="speed",
|
|
metric_unit="ms",
|
|
**common_configs,
|
|
)
|
|
run_benchmarks(
|
|
bench_test_fn=build_memory_bench_fn(setup_jsd),
|
|
kernel_operation_modes=["full"],
|
|
metric_name="memory",
|
|
metric_unit="MB",
|
|
**common_configs,
|
|
)
|