Serving a 320B MoE Model with 1M Context on a Budget: How a Megakernel-Based Engine Cuts Inference Costs
A technical breakdown of a Megakernel-based inference engine for a 320B-parameter MoE model with 18B active parameters, covering sparse attention for 1M-token contexts, kernel fusion strategies, and how to interpret the reported $0.04 per million token serving cost. Includes a checklist of caveats for anyone benchmarking or deploying similar systems.
From “It Runs” to “It Runs Affordably”: Why MoE Inference Costs Differ
A 320B-parameter MoE model with 18B active parameters only engages 18B of those parameters per token. In rough terms of compute, its per-token workload is about a quarter of a 70B dense model’s. However, the model’s knowledge capacity comes from all 320B parameters, so the full weights must reside in GPU memory or on-chip storage. An inference system therefore has to solve three problems at once: where to place hundreds of gigabytes of weights, how to overlap expert-parallel communication with computation, and how to keep the KV Cache under control for long contexts.
The original report claims that running this model on a single 8-card inference server yields a service cost of roughly $0.04 per million tokens. The exact methodology behind that figure is not fully disclosed; the sections below examine the conditions that need to be verified.
Part 1: Why Doesn’t This Engine Follow the GPU Playbook?
Most mainstream inference frameworks are built around the GPU programming model. The engine described in the original article targets a class of dedicated AI chips featuring large matrix compute units and high-bandwidth memory. The article states the engine is open-sourced under the Apache 2.0 license, permitting commercial use and modification. Teams evaluating it should verify the exact license terms of the specific version, along with its dependencies—don’t rely on the word “open source” alone.
1.1 Megakernel: Fitting an Entire Layer into a Resident Kernel
In traditional inference, each Transformer layer contains multiple operators: multi-head attention, feed-forward networks, normalization, and residual connections. Each operator typically triggers one kernel launch and one round of memory reads and writes. The Megakernel approach merges an entire layer’s computation into a single device-resident kernel, keeping intermediate results in on-chip caches or registers to reduce global memory round trips.
The original article claims this reduces memory access overhead by several orders of magnitude, but provides no test model, batch size, or comparison baseline. Treat that figure as directional guidance only, not a performance guarantee.
1.2 Two Fused Kernels per Layer: How Megakernel Works in Practice
A single fused kernel can exhaust registers or shared memory and fail to compile. This engine instead splits each layer into two resident fused kernels. Kernel A handles the input projection after RMSNorm, RoPE positional encoding, and attention score computation. Kernel B handles the weighted summation of attention outputs and joins with the MoE routing results from the FFN branch. Together, the two kernels form a Megakernel pipeline—they are complementary stages, not alternative approaches.
The original article does not specify whether the split occurs along the sequence dimension or the hidden dimension, nor does it state register and shared memory limits. During deployment, defer to the official documentation and prepare a fallback path: if dual-kernel compilation fails for a layer, you can revert to per-operator execution. This fallback reduces performance but keeps the service available and simplifies debugging. Note that this is an engineering recommendation, not something the original article proposes.
Part 2: How Is the KV Cache Compressed for 1M Contexts While Preserving Long-Range Dependencies?
Standard attention’s compute complexity grows quadratically with context length. At 1M tokens, the KV Cache can balloon to hundreds of gigabytes. A common approximation in the industry is “sliding window plus global tokens.” This engine instead uses sparse attention: a local window covers the most recent N tokens, pre-selected anchor tokens provide global attention, and a learnable routing mechanism decides which historical positions to attend to.
| Approach | Compute growth vs. context | Long-range dependency | Main risk |
|---|---|---|---|
| Full attention | Quadratic | Fully preserved | Highest compute and memory cost |
| Sliding window + global tokens | Near-linear | Relies on fixed global tokens | Key information outside the window is easily lost |
| Sparse attention described in the article | Near-linear (per the article) | Determined by anchors and routing | Routing quality depends on training; requires accuracy regression testing |
2.1 Prerequisite: Does the Model Itself Have This Structure?
An inference engine can only leverage the sparse attention and routing structure learned during pretraining. If the model was trained with standard dense attention, converting the weight format alone won’t enable learnable routing—that would require additional training or adaptation, with results that need re-evaluation. The original article doesn’t specify the model structure version used, so confirming this is the first step in any reproduction effort.
The article claims the KV Cache compression supports 32 concurrent requests. That number depends on model structure, precision, and batch configuration, and cannot be directly applied to other models.
Part 3: How to Read the Benchmark Data
3.1 Test Conditions
The test used replayed Agent requests from real production scenarios, mixing multi-turn tool calls, long-document reading, and code generation. Concurrency was 32 Agent streams, with the context window held at 1M tokens throughout. The original article describes the hardware as a trn2.48xlarge instance with 8 accelerator cards. It does not fully disclose the chip model, memory capacity, or interconnect bandwidth. Before purchasing or comparing, verify the instance name and card count against the cloud or chip vendor’s spec pages.
3.2 Key Metrics
| Metric | Reported value | Interpretation |
|---|---|---|
| Default batch size | 12 | A scheduling parameter, not the same as concurrent Agent count |
| Requests completed per minute | 28.9 | Directly tied to input/output length distribution |
| Tokens generated per minute | 3.57M | Roughly 59,500 tokens/second system-wide, averaging ~7,400 tokens/s per card across 8 cards |
| Decode speed (P50) | 22.3 tokens/s/user | Median only; tail latency not disclosed |
| Price | ~$0.04 per million tokens | Pricing methodology not fully disclosed |
The original article states “each card processes ~74,000 tokens per second,” which is a unit error. Based on 3.57M tokens per minute, the whole system handles ~59,500 tokens per second, or about 7,400 tokens/s per card on average.
3.3 The Fine Print Behind the Cost Figure
For $0.04 per million tokens to be comparable with other solutions, at least the following must be clarified:
- Instance pricing model—on-demand, reserved, or spot;
- Actual utilization—full-load stress testing or real-world workloads;
- The input-to-output token ratio, since prefill and decode have different cost structures;
- Whether hardware depreciation, networking, storage, and operations overhead are included.
Figures with different methodologies cannot be compared directly. The article claims this price is more than an order of magnitude below the industry average but provides no source for that average. This article therefore states it only as “approximately $0.04 under the article’s test conditions.”
On decode speed, the P50 of 22.3 tokens/s/user only reflects the median experience. Interactive use cases also require P95/P99 latency and time-to-first-token figures, none of which the original article provides.
