Inspiration:

We wanted to build an optimization that delivered measurable improvements while remaining numerically trustworthy. The largest test case motivated us to address both execution speed and memory feasibility.

What it does:

MegaMind accelerates Transformer inference using shape-aware execution while preserving the model structure, weights and full causal context. It evaluates numerical correctness and performance across the published shapes, saving logs and results for reproducibility.

How we built it:

We implemented a custom Transformer that retains the supplied model's layer normalization, residual connections, feed-forward layers and causal attention.

We packed the query, key and value weights into one larger projection, replacing three separate linear operations. We reshaped the output for PyTorch's scaled-dot-product attention and evaluated both eager execution and torch.compile with the reduce-overhead preset.

For long sequences, we implemented a separate path combining fused attention with batch microbatching. It processes one complete 100,000-token sequence at a time and assembles the full batch output without truncating context.

We built an independent reference using separate projections and query-tiled attention. Each query tile still considers every permitted causal key, allowing full-size validation without constructing the complete attention-score matrix.

We also added parameter-cache invalidation, correctness-oriented fallback paths for padded inputs, and automated recording of accuracy checks, raw timings, environment details and source snapshots.

Challenges we ran into:

Shape 14's explicit float32 attention-score tensor alone would require 20.48 TB. Avoiding this allocation was only part of the challenge: we also needed an independent way to validate the full-size output.

Other challenges included keeping packed parameters consistent after weight changes, maintaining masking correctness, and interpreting variable baseline timings. We progressed from small proxy tests to full-size comparisons and preserved raw measurements for inspection.

Accomplishments that we're proud of:

Our completed single-H100 run passed all 27 cases: eager and compiled execution for shapes 1 through 13, plus the dedicated shape-14 test.

Across shapes 1 through 13, we recorded geometric-mean speedups of 2.39x for eager execution and 4.98x for compiled execution against the supplied eager baseline.

Shape 14 passed a full-size accuracy trial covering 3.2768 billion output values, with zero tolerance failures and a maximum absolute error of approximately 0.000705.

Its optimized median execution time was 53.47 seconds, compared with 101.24 seconds for our independent tiled reference. This 1.89x ratio is separate from the original-baseline comparisons.

These are measurements from our recorded run, using float32 tensors with TF32 permitted and warmed-up inference timing.

What we learned:

Different input shapes require different execution strategies. Memory scheduling can make a workload feasible without reducing its mathematical complexity.

We also learned that small proxy tests cannot establish full-size correctness, and that performance ratios must be interpreted alongside their reference implementation, runtime settings and measurement variability.

What's next for MegaMind:

Repeat noisy measurements under controlled conditions, profile kernel selection, optimize wide-dimension workloads, and expand validation across GPUs and input patterns.

Built With

Share this project:

Updates

Submission history