Skip to content

Latest commit

 

History

History
244 lines (224 loc) · 15.7 KB

CLAUDE.md

File metadata and controls

244 lines (224 loc) · 15.7 KB

CLAUDE.md — ATOM

ATOM is a lightweight LLM inference engine built on AITER GPU kernels for the AMD ROCm platform.

Build & Test & Lint

pip install -e .                          # editable install
python -m pytest tests/                   # all tests (no GPU needed — mocks AITER and torch.cuda)
black . && ruff check .                   # format + lint (CI enforced)

Running

# OpenAI-compatible serving
python -m atom.entrypoints.openai_server --model <model> --kv-cache-dtype fp8 -tp 8

# Offline inference
python -m atom.examples.simple_inference --model <model> --kv-cache-dtype fp8
  • Accuracy validation: see /ci-pr-guide for lm_eval setup and CI thresholds
  • Performance benchmark: see /benchmark-guide for full parameters

Architecture (Quick Index)

openai_server.py → LLMEngine → CoreManager → EngineCore → Scheduler → ModelRunner
                   (tokenizer)   (ZMQ IPC)    (inference    (batching)  (forward pass,
                                               loop)                    KV cache, CUDAGraphs)

ATOM also has a diffusion subsystem (atom/diffusion/) for video+audio generation, which is a sibling of atom/model_engine/ rather than an extension of it — no KV cache, one fixed N-step denoise loop per job, four heterogeneous networks, and sequence parallelism across a single request:

diffusion_server.py → DiffusionEngine → DiffusionCoreManager → DiffusionEngineCore → PipelineRunner
   (/v1/videos)        (JobScheduler)      (ZMQ, one PUSH/rank)    (per-GPU worker)     (ComposedPipeline)

Key entry points:

  • Server: atom/entrypoints/openai_server.py
  • Engine: atom/model_engine/llm_engine.py → engine_core.py → scheduler.py → model_runner.py
  • Models: atom/models/ — registered in atom/models/registry.py:support_model_arch_dict
  • Verify head and deferred status: ModelRunner.run_model returns the forward's hidden states (a decode graph records the forward alone); the head runs after it, eagerly, on the forward's stream (ModelRunner.run_lm_head -> LmHeadOutput, handed to postprocess): a verify head's token ids where they stand for the step, else ModelRunner.compute_logits (a lossless drafter's sampled step and a masked rollout vocabulary take the logits). The verify head (atom/model_ops/verify_head.py VerifyHead, bound by ModelRunner._bind_verify_head: the model's own verify_head, else for a drafted model whose ParallelLMHead.logits_are_head the shared mono LM head launch (atom/mono/lm_head) in front of ATOM's Triton ArgmaxVerifyHead): sample_token_ids(hidden) a verify's output ids and spec status (verify_ids.VerifyOutput), samples_step(batch, spec, rows) whether they stand for the step of rows forward rows (samples, draws_top_p(rows): the rows it draws), ids_agree whether every TP rank's are alike. The verify from a head's ids, exact match or forced acceptance: atom/model_ops/verify_ids.py (IdVerifiers); a vocab-sharded head's picks (ShardedPicks, ATOM's and the V4.1 mono LM head's): a temperature row's id a Gumbel-max draw (verify_ids.HeadDraw; the mono head's by atom/mono/device/race.py), within its top-p in the mono head (atom/mono/lm_head/top_p.py: the set within each rank's K largest, top_p_candidates, sorted by the wave bitonic networks of atom/mono/device/bitonic.py; past them hi's two-level scan, top_p_scan; the slices both read, top_p_logits); forced acceptance admits every request greedy (LLMEngine); an attention builder may say a step consumes the previous verdict on the device (AttentionMetadataBuilder.consumes_device_verdict: the runner then takes the speculative status after the target's launch) or stages the step before that status and restages what it moves once taken (settles_spec_status / take_spec_status)
  • Peer embedding: atom/model_ops/peer_embedding.py — a vocab-parallel embedding read from each row's owner rank's shard (HIP IPC), one launch; shared_peer_embedding one mapping an embedding and group (a DSpark draft shares its target's)
  • Model hooks: atom/model_engine/model_hooks.py — DecodeRound, the part of a step a model's install hands DeepSeek-V4.1's metadata builder (DeepseekV41MetadataBuilder.use_decode_round); DecodeRound.service is the step's one routing decision (AttentionMetaData.served_by_round, RoundService) that the builder, its cache, Engram staging, the DSpark proposer and the model's forward all read
  • Ops: atom/model_ops/ — AITER kernel wrappers (linear, attention, fused_moe)
  • Structured output: atom/structured_output/ — request validation, schema compilation, grammar state and asynchronous token/mask transfers
  • Config: atom/config.py (Config, KVCacheConfig, CompilationConfig)
  • Env vars: atom/utils/envs.py (all ATOM_* variable definitions)
  • Pipeline parallel: atom/model_engine/pp_engine_core.py (head / downstream stage loops) over atom/distributed/pp_transport.py; dynamic chunked PP prefill in atom/model_engine/dynamic_chunking.py (latency model, pure) and dynamic_chunking_worker.py (startup sweep and runtime timing in the runner), sized by Scheduler._dynamic_chunk_limit
  • Ulysses sequence parallel (-sp, on the PCP rank dimension): atom/distributed/ulysses_sp.py (the SP group, the token shard / gather and the QKV exchange), group_agreement.py (verdicts every rank of a CPU group takes alike); the model runs on a rank's token range through atom/utils/sp_wrapper.py; the attention and its head gather in atom/model_ops/sp_attention.py, and the MXFP4 MoE through AITER MegaMoE in atom/model_ops/fused_moe/sp_mega_moe.py
  • Mono decode (fused per-layer decode kernels): the shared mechanisms in atom/mono/ (runtime/: TP consensus and a runner's bind-then-agree construction (consensus.bind_agreed), peer memory and its owned buffer (lifecycle), the step's mailbox zeroing and peer fence, one FlyDSL launch (mailboxes.StepMailboxes.begin_step -> step_begin), per-width builds (widths), the routing wrapper's lazy runner (route), debug wait reports, kernel argument tables, compile-only builds and their traced hand-offs (compile.trace / compile.check_traced), the refusals every model shares (deployment), check-mode comparison (compare), the dual-copy scratch's calibration (numa: which copy, one per I/O die, each XCD reads; hi's dual runs it on its own probe); plan/: the execution model and task placement, build keys, a TP rank's share of the widths (shard: a model's Dims and its TP refusal), mailbox region layout (layout), int / traced arithmetic (arith) and the hand-off contract a trace is held to (check.check); device/: the tagged mailbox and its publish, the TP group's peer bases and all-reduce sum (ranks), FP8 / MX codes, FP4 tile loads and the scaled MFMA (mx), a CTA's dual copy (numa) and wave / math primitives), a model's kernels in atom/models/<model>/mono/kernels/ (MiniMax-M3; DeepSeek-V4.1 and Kimi-K3, both wrapped after load through models.registry.mono_decode_installers; V4.1's per-step cell decisions live in deepseek_v41/mono/{index,attention,moe}_plan.py, each called by both the kernel and its CPU I3 test; its dual-copy regions in numa_plan.py; K1 and K2 of a layer in one launch (kernels/layer.py), a column-sharded residual's gather (kernels/col_gather.py), and the launches beside the layers, each falling back to the original path where not built (compress_launch, engram_launch, head_launch, draft_ctx_launch; head_launch binds the shared mono LM head over V4.1's norm and head). The shared mono LM head (atom/mono/lm_head/): a vocab-parallel bf16 head's picks in one launch (kernel: an optional final RMSNorm, the shard's dot (dot), each row's greedy id or temperature draw over every TP rank (argmax), a top-p row's draw within its top-p (top_p: top_p_candidates, else top_p_scan)), its hand-offs tagged with the verify launch count so nothing zeroes its own peer buffer between launches; head binds it as V4.1's head and, for any drafted model whose head is a ParallelLMHead (ModelRunner._bind_verify_head, bind_verify_head), in front of ATOM's Triton picks. On gfx1250, V4.1's whole DSpark decode step is one persistent FlyDSL kernel a rank (deepseek_v41/mono/gfx1250/: a target launch through sampling and verification, then a draft launch), which ModelRunner is offered through atom/mono/runtime/step_override.py; it reads the production MoE's experts in place, and tests/test_deepseek_v41_gfx1250.py is its CPU test. Kimi-K3 runs K1 (the attention seam through the KDA recurrence) and K2 (o_proj + the latent MoE) on its KDA layers, on a plain decode or a uniform speculative verify of at most 64 rows (16-row token groups past 16), with or without ReplaySSM. The MLA layers keep their original paged attention but also run K2; an MLA layer that cannot bind K2 stays on the production path alone. Which steps it serves and how a served step is keyed live in kimi_k3/mono/plan.py, apart from runner.py so the CPU test gate can reach them without AITER or FlyDSL. Its DSA layers' indexer (plain decode or verify) runs the shared atom/mono/dsa_indexer/ launch: under DCP over each rank's own keys, without DCP over the replicated cache (a request's rows owned by one rank, a long request's keys split over the ranks) (wq_b through the selected KV slots; any model with the V3.2-style Indexer, keyed and named by the layer's IndexerShape, plan.unsupported the shapes it is not written for); the bf16 GEMV it shares with K1 / K2 is atom/mono/device/gemv.py. GLM-5.2's single kernels/kernel.py covers the whole layer, kernels/op.py is its host wrapper, and kernels/geometry.py / kernels/geometry_config.py hold its launch geometry and its own LayerConfig. What is NOT GLM-5.2's sits in atom/mono/: the storage vocabulary and host-side weight preparation in mono/weights/ (config the enum vocabulary and LayerConfig, formats MX conversion, packing, layer the LayerWeights ABI), ATOM's MXFP4 index arithmetic in plan/tiles.py, and the HIP IPC wrapper in runtime/hip_ipc.py. runtime/peer_memory.py now holds BOTH peer-buffer implementations side by side — PeerBuffer (aiter's heap) and SymmetricPeerBuffer (HIP directly, GLM-5.2's) — because they are different implementations rather than two copies, and merging them is a GPU behaviour change, not a move; both allocate through runtime/uncached.py, as do the other models' step scratch buffers (uncached device memory: a hand-off's polls never hit a stale cache line). DeepSeek-V4.1's high-interactivity monokernels (below) keep a third, their own: the hi programs' peer groups (deepseek_v41/mono/hi/runtime/peer.py, PeerGroup) and uncached scratch arenas (hi/runtime/uncached.py, a block retired and reused, never freed), both over runtime/hip_ipc.py. Qwen3.8-Flash-Next's kernels (qwen38/mono/, beside the single-file qwen4_exp.py) are whole-layer programs composed from FlyDSL stages (kernels/blocks/, kernels/recipe.py); its MTP drafter is wrapped at install like V4.1's (draft_model.MonoDraftModel). Where a model ships a mono/contract.py with a __main__ (DeepSeek-V4.1, MiniMax-M3), check its hand-offs without a GPU with ARCH=gfx950 python -m atom.models.<model>.mono.contract; GLM-5.2's contract.py is the per-step input check only, and Kimi-K3 ships neither it nor a check.py yet). DeepSeek-V4.1 also has the high-interactivity monokernels (ATOM_DSV41_MONO_HI, on by default where deepseek_v41/mono/hi_gate.py says they serve: MI355X, TP2, TP4 or TP8, DSpark; at TP8 the shared expert's K is padded in place, 288 -> 320, until ROCm/aiter#7967): one persistent FlyDSL launch a layer (the runtime in deepseek_v41/mono/hi/), the host hook in hi_runner.py / hi_compose.py / hi_dispatch.py, its verify head (HiVerifyHead: the in-graph verify, round/post_verify.py) and its decode round in round/ (HiRound)
  • Diffusion: atom/diffusion/ — framework at the top level (pipeline.py, attention.py, ulysses.py), engine in engine/, server in entrypoints/, and one package per model in models/<family>/. Extras: pip install -e ".[diffusion]"

Critical Rules

  • A source file's header is these three lines, the ATOM line included (every new file gets all three):
    # SPDX-License-Identifier: MIT
    # Copyright (C) 2026, Advanced Micro Devices, Inc. All rights reserved.
    # Copyright (c) 2026 ATOM Project Contributors
    
  • Change @support_torch_compile decorated model files with care — edits are allowed, but debug/instrumentation code inside traced functions breaks Dynamo tracing even with --enforce-eager. Instrument at call sites instead (e.g., ModelRunner.run_model(), EagleProposer._propose(), DSparkProposer._propose(); Drafter.propose() is only the template that calls them), know which functions are traced before editing, and run the compiled path afterwards. deepseek_v4_dspark.py carries a COMPILE BOUNDARY block at the top naming exactly which functions are traced — read it before touching that file
  • Multiprocessing must use spawn — fork causes CUDA re-initialization crashes
  • Set AITER_LOG_LEVEL=WARNING before starting server — suppresses aiter kernel log flooding
  • Clear compile cache before server restart: rm -rf /root/.cache/atom/* — stale cache causes silent failures after code changes
  • Verify server with GPU, not just HTTP: curl /health can return OK even when model is not loaded. Always check rocm-smi --showmemuse (VRAM% > 0) to confirm
  • On any server/GPU error, run /debug-guide first — do not blindly retry
  • Fix-then-sweep: after fixing a bug, immediately grep for the same pattern across the codebase and fix all occurrences in one pass
  • Name-matches-function: variable, function, and file names must accurately describe what they do. When behavior changes, rename immediately — stale names mislead future readers

Key Development Patterns

  • Adding a model: see /add-model for full guide
  • Model reuse: DeepSeek V3/V3.2/GLM-5 share deepseek_v2.py; MTP models in deepseek_mtp.py, qwen3_next_mtp.py, and qwen3_5_mtp.py
  • Compilation levels: --level 0=eager, 1=torch.compile, 2=dynamo once, 3=piecewise compile (default); CUDA graphs are --cudagraph-mode's, independent of the level

Dependencies

  • AITER (from aiter import ...) — GPU compute kernels
  • MORI — MoE expert-parallel all-to-all communication
  • RCCL — collective communication primitives

Detailed Guides

Topic Source
Architecture docs/architecture_guide.md
Environment variables docs/environment_variables.md
Compilation & CUDAGraph docs/compilation_cudagraph_guide.md
Model support docs/model_support_guide.md
Structured output docs/structured_output_capabilities.md
Model ops (AITER kernels) docs/model_ops_guide.md
Scheduling & KV cache docs/scheduling_kv_cache_guide.md
Serving & benchmarking docs/serving_benchmarking_guide.md
Configuration docs/configuration_guide.md
Distributed docs/distributed_guide.md
Dynamic chunked pipeline parallel docs/dynamic_chunking_guide.md
Metrics & observability docs/metrics_guide.md
CI/PR workflow /ci-pr-guide
Performance benchmark /benchmark-guide
Debugging /debug-guide
Adding a model /add-model
MiniMax-H3 (video+audio diffusion) recipes/MiniMax-H3.md
DeepSeek-V4.1-Flash recipes/DeepSeek-V4.1-Flash.md
Qwen3.8-Flash-Next mono decode recipes/Qwen3.8-Flash-Next-Mono-Program.md