Views
No views yet
uv run https://huggingface.co/shisa-ai/megablocks-hip/raw/main/readme_example.py1# /// script
2# requires-python = "==3.10"
3# dependencies = [
4# "numpy",
5# "kernels",
6# "torch"
7# ]
8# ///
9
10import torch
11from collections import namedtuple
12
13from kernels import get_kernel
14
15# Make reproducible
16torch.manual_seed(42)
17torch.cuda.manual_seed(42)
18
19# Download optimized kernels from the Hugging Face hub
20megablocks = get_kernel("shisa-ai/megablocks-hip")
21print("MegaBlocks kernel downloaded successfully.")
22
23model = megablocks.layers.MegaBlocksMoeMLP()
24model.experts = namedtuple("Experts", ["gate_up_proj", "gate_down_proj", "down_proj", "hidden_size"])
25print("MegaBlocksMoeMLP instance created successfully.")
26
27# Config
28ne, hs, isz = 128, 1152, 3072
29
30# Router with proper initialization
31model.router = torch.nn.Linear(hs, ne, device="cuda")
32torch.nn.init.kaiming_uniform_(model.router.weight)
33
34# Expert layers with realistic weights
35e = model.experts
36e.gate_up_proj = torch.nn.Parameter(torch.randn(ne, hs, isz, device="cuda") * 0.02)
37e.gate_up_proj_bias = torch.nn.Parameter(torch.zeros(ne, isz, device="cuda"))
38e.down_proj = torch.nn.Parameter(torch.randn(ne, 1536, hs, device="cuda") * 0.02)
39e.down_proj_bias = torch.nn.Parameter(torch.zeros(ne, hs, device="cuda"))
40e.hidden_size = hs
41print("Expert layers initialized successfully.")
42
43# Test with normalized input
44x = torch.randn(1, 1, hs, device="cuda") * 0.1
45output, expert_weights = model(x)
46print("Model forward pass completed successfully.")
47
48print(f"Output shape: {output.shape}")
49print(f"Output range: [{output.min():.3f}, {output.max():.3f}]")
50print(f"Output: {output.flatten()[:10]}")
51print(f"Expert weights sum: {expert_weights.sum():.3f}")