Skip to content

Latest commit

 

History

History
192 lines (179 loc) · 49.2 KB

debug-env.md

File metadata and controls

192 lines (179 loc) · 49.2 KB

Environment variables

Features and tuning values are command-line flags of legion serve (and legion run, which shares the model flags); see legion serve --help and serving.md. New switches are flags too, never environment variables. What the environment still does is listed here. tests/env_reads.rs fails when the code reads a LEGION_* variable this page and its allow-list do not name. LEGION_DEBUG_* switches need no allow-list entry, but each one needs a row of its own in the table of debug hooks below, and a row whose switch nothing reads any more fails the test too.

Variables that mirror a flag

Most legion serve flags take their value from a variable when the flag is not given (--model from LEGION_MODEL, --port from LEGION_PORT, ...). --help shows each one as [env: LEGION_...]. A flag on the command line wins.

Secrets

Variable Flag Use
LEGION_API_KEY legion serve --api-key, legion chat --api-key API keys, kept out of the process list.
LEGION_BENCH_API_KEY legion bench --api-key The key legion bench sends.
LEGION_TLS_CERT, LEGION_TLS_KEY --tls-cert, --tls-key HTTPS certificate and key paths.

Removed

These worked as environment variables before #421 and as deprecated aliases until #737. They are no longer read: setting one does nothing. Use the flag.

Variable Use instead
LEGION_MOE_RAW=0 --no-moe-raw-staging
LEGION_MOE_MMA16=0 none: the fused MoE op runs mma.m16n8k16 wherever the GPU and the binary have it
LEGION_QSA_MMA=0 none: qwen4exp's sparse-attention prompt kernel runs mma.m16n8k16 wherever the GPU and the binary have it
LEGION_CPU_ISA=<tier> none: the CPU kernels take the best tier the CPU has (legion cpu-bench times every tier)
LEGION_GATED_NORM=1 --gated-norm
LEGION_MOE_COMBINE=1 --moe-combine
LEGION_DELTA_PREP=0 --delta-prep-ggml
LEGION_MOE_PAIR_OUT=f16|bf16 --moe-pair-out f16|bf16
LEGION_EXPERT_MV_VRAM=<mode> --expert-mv-vram <mode>
LEGION_EXPERT_MV_DEVICE=1 --expert-mv-device
LEGION_EXPERT_MV_MAX_TOKENS=<n> --expert-mv-max-tokens <n>
LEGION_EXPERT_MV_GLU=0 --expert-mv-split-glu
LEGION_QSA_SCORES_TC=1 --qsa-scores-tc
LEGION_QSA_ATTN_CHAIN=1 --qsa-attn-chain
LEGION_QWEN4_HC=0 --hc-mix-ggml
LEGION_QWEN4_HC_POST_NORM=0 --hc-post-norm-ggml
LEGION_AFTER_OVERLAP=0 --no-after-overlap
LEGION_LOAD_THREADS=<n> --load-threads <n>
LEGION_LOAD_PARALLEL_DEVICE=1 --load-parallel-device
LEGION_NO_MMAP=1 --no-mmap
LEGION_SPEC_DRAFT_WINDOW=<n> --spec-draft-window <n>
LEGION_EXPERT_PROFILE_SEED=<x> --expert-profile-seed <x>
LEGION_MTP_HEAD_TYPE=q8_0 --spec-draft-head-type q8_0

Debug and development hooks

For people working on Legion's kernels and tests. They are not settings: they may change or go without notice. Most LEGION_DEBUG_* switches put back the code from before a change, for A/B runs; unless a row says so, output does not change.

Variable Effect
LEGION_OP_PROFILE=1 Per-op GPU time profile (CUDA): every graph node timed between CUDA events, a table on stderr now and then (=dev: per GPU, every row). Disables ggml's CUDA graphs and fusions. A node that reads a weight of 1 MiB or more is keyed op weight [type] ne0xne1xne2 x<tokens> and shows GB/s: the weight bytes it read per second, its achieved bandwidth (caches and states do not count; an expert stack counts every routed choice as a different expert, so its GB/s above 1 token is an upper bound). External ops take their token count from their input, so a flat output still lands in the right x<tokens> row.
LEGION_DEBUG_VISION_CPU=1 Images (--mmproj or a checkpoint's vision tower, #1195): the vision tower runs on the CPU (ggml's CPU backend, the oracle) instead of on the language model's first GPU, for A/B runs. The image rows change within F16 rounding, so an answer about an image can differ where two words are near a tie; text-only requests give the same output either way.
LEGION_DEBUG_VISION_SHARE=<list> The GPU vision tower (#1195): how much of the GPU an encode's kernels take, a comma-separated list used in turn, one entry per encode: 0 one block per tile (the grids before the cap), or GEMM CTAs per SM (1, the default, or a fraction like 1/2; small kernels twice that). For A/B runs; the image rows are the same bits whatever the share. A list set here holds whether or not anything else runs.
LEGION_DEBUG_VISION_IDLE_FULL=0 The GPU vision tower (#1195): an encode that starts while the engine has no request waiting and no sequence running keeps the default share too. Without it such an encode runs uncapped (one block per tile), since nothing else would use the rest of the GPU. For A/B runs; the image rows are the same bits either way.
LEGION_DEBUG_VISION_REENCODE=1 Images: every image is encoded (the row cache unused) and the vision: image encoded log line names a hash of its rows' bits, for A/B runs of the tower.
LEGION_DEBUG_NGRAM_OLD_POLICY=1 An n-gram table read from the SSD with an --ngram-ram budget: the row cache replaces round-robin, without a hot set (the policy before #1209), for A/B runs. Output does not change.
LEGION_DEBUG_NODE_GROUP_DRAFTS=1 A model over two nodes (--node-rank) with --spec mtp: the depth policy may draft for decoding groups in flight (the behaviour before #560 round 3, which drafts nothing there), for A/B runs. Greedy output does not change.
LEGION_DEBUG_NODE_MTP_BF16=1 A model over two nodes (--node-rank) with --spec mtp: GLM-5.3-Flash's MTP experts stay BF16 as shipped instead of loading as NVFP4 (both ranks), for A/B runs. Only the drafts change; greedy output does not.
LEGION_DEBUG_NODE_SPLIT_HEAD=1 A model over two nodes with --spec mtp and no --node-split: the automatic split counts the MTP head's bytes on rank 1 (the split before #560 round 4, which gives rank 0 more layers); give it to both ranks. Output does not change.
LEGION_DEBUG_NODE_TAKE_IN_NOW=1 A model over two nodes with --spec mtp: rank 1 runs every MTP take-in when it comes, instead of letting decode steps' take-ins wait for the sequence's next draft round (#560), for A/B runs. Set it on rank 1. Greedy output does not change.
LEGION_DEBUG_GLM_KDA_SPLIT=<0-2> GLM-5.3-Flash on a DGX Spark (#1172 round 8): how many blocks the KDA scan's decode kernel (a verify step's scan and every MTP commit's) gives each of the 64 heads, as a log2: 0 one (the kernel before round 8), 1 two, 2 four (the default). Each block computes the token's norms itself from the same values in the same order, so greedy output does not change. For A/B runs and legion lin-bench --kda.
LEGION_DEBUG_HOST_MEM_GIB=<n> The host expert tier's sizing (#37, a discrete GPU): take n GiB as the machine's total and available memory instead of what /proc/meminfo says, to try a smaller PC's RAM slot count on a bigger one. <total>:<available> gives the two apart (32:27: a 32 GB machine with 27 GiB available, which keeps the margin of a 32 GB machine; 27 keeps the smaller margin of a 27 GiB one). The process is not held to it.
LEGION_DEBUG_TIER_FETCH=hostfunc The host expert tier (#37): run each layer's fetch of a step's routed experts as a host function on the CUDA stream (the way before) instead of on the launching thread after a wait for the ids, for A/B runs. Output does not change.
LEGION_DEBUG_TIER_HIT_OVERLAP=<0|1> The bounded host tier (#37) with the exact CPU share: a small step's fetch leaves its checkpoint reads running and the layer's SwiGLU op runs the VRAM experts' pairs, waits for the reads, then runs the host experts' pairs; the share's pool waits only for the experts it reads, and takes the experts already in RAM ahead of those being read. 0 waits for the reads in the fetch (the way before), 1 overlaps without that pool order, for A/B runs. Output does not change.
LEGION_DEBUG_TIER_LAND_LATER=<0-3> The overlap above (#37 round 36): where the wait for a small step's checkpoint reads goes. 0 in the SwiGLU op after its VRAM experts' pairs (round 35); 1 in the down op after its own VRAM experts' pairs, then the SwiGLU's host pairs, the host's SwiGLU rows (activate) and the down's host pairs; 2 as 1 with activate ahead of the wait, so the CPU share's pool starts its down rows sooner; 3 (the default) as 2 with the SwiGLU's pairs of host experts already in RAM ahead of the wait too, only those of the experts being read after it. For A/B runs. Output does not change.
LEGION_DEBUG_TIER_FIT_ONLY=1 The bounded host tier (#37, a discrete GPU): size a long prompt's staged passes to what the lent pass step (measured as lent, for the first long lone prompt) and a pass's 16 MiB pad fit in the VRAM left, even where that takes a different number of passes than the default (which keeps the count the KV tail mapped in full leaves room for, and with it the output). A different pass count changes long prompts' output. For A/B runs.
LEGION_DEBUG_TIER_PASS_RING=0 The bounded host tier (#37, a discrete GPU): a long prompt's staged pass gives each layer's batch of checkpoint reads a staging set of its own, two sets in turn (the way before), instead of taking the places as a ring right behind the batch before, for A/B runs. Output does not change.
LEGION_DEBUG_TIER_PASS_MERGE_KIB=<n> The bounded host tier (#37): a staged pass joins a batch's checkpoint reads that follow one another in the file and in memory into reads of at most n KiB instead of the default 4096; 0 reads every expert matrix on its own (the way before), for A/B runs. Output does not change.
LEGION_DEBUG_TIER_READ_PIECE_KIB=<n> The host expert tier (#37): a small step reads each missed expert's tensors from the checkpoint in pieces of n KiB over the reader threads instead of the default 256; 0 reads every tensor in one read (the way before), for A/B runs. Output does not change.
LEGION_DEBUG_SWAP_COPIES_FIRST=1 The expert cache on a CUDA device (no bounded host tier): start the after-step's swap copies before the expert tables are written (the way before #37's round 11), for A/B runs; the table write then waits on the copy engine behind the swaps' expert bytes. Output does not change.
LEGION_DEBUG_SWAP_PACE=0 The expert cache on a CUDA device with the exact --cpu-expert-share (#37, no bounded host tier): queue the after-step's swap copies all at once (the way before round 14) instead of letting the next step's share layers start them a few at a time, each behind its layer's pool job, for A/B runs. Output does not change.
LEGION_DEBUG_SHARE_ALL_UPTO=<n> --cpu-expert-share auto (#37): the pool computes every host expert of a layer's step with at most n of them, and the race between pool and link only splits bigger steps (0: every step races, the way before round 16). The default is 3 with every routed expert in host memory and 0 on the bounded host tier. Output does not change.
LEGION_DEBUG_REFILL_TAILS_EAGER=1 The expert cache after a lend that kept the slots below keep (#37): the restore's slot thread gives the lent tails their VRAM back at once (the way before round 27) instead of after the layers lent whole and only once a step runs, for A/B runs. Output does not change.
LEGION_DEBUG_RAM_FILL_LAYERS=<n> The bounded host tier (#37): with LEGION_DEBUG_RAM_FILL_SYNC=1, each after-step copies the planned RAM fills of at most n refilled layers (default 8) into their emptied VRAM slots; 0 copies all of them at once (the way before round 27; the restore that leaves the KV tail's share then copies them before the next step). Output does not change.
LEGION_DEBUG_RAM_FILL_SYNC=1 The bounded host tier (#37): the after-steps copy the RAM fill of the refilled layers themselves, a few layers each (LEGION_DEBUG_RAM_FILL_LAYERS, the way of round 27), instead of a thread queuing the copies on a copy stream of their own while the steps run. Output does not change.
LEGION_DEBUG_TAILS_AT_ADMIT=0 The expert cache after a lend that kept the slots below keep (#37): the lent tails take their VRAM back only in the after-step of the first step that does not lend (the way of round 27), not as that step starts. Output does not change.
LEGION_DEBUG_EMBD_DEVICE=1 The bounded host tier's machine and model (#37, one discrete GPU): keep the token embeddings in VRAM (the way before) instead of pinned host memory, for A/B runs. Output does not change.
LEGION_DEBUG_HEAT_SIZES=0 The host expert tier with every routed expert in host memory (#37): give every layer the same number of VRAM expert slots instead of sizing the layers by the saved routing heat (the default). Added by PR #95.
LEGION_DEBUG_WHOLE_KEEP=0 Every routed expert in host memory on one GPU (#37): a long prompt's lend gives up every VRAM expert slot, as before, instead of keeping the hottest cached experts for prompts up to the pass size measured at load (longer ones still lend every slot). For A/B runs. Output does not change.
LEGION_DEBUG_ROUTE_GATE=N Every routed expert in host memory on one GPU (#37): staged prompt steps of up to N tokens (default 8704) copy only the uncached experts their tokens route to. 0 copies every uncached expert, as before. For A/B runs. Output does not change.
LEGION_DEBUG_SHORT_GATE=0 One GPU with routing-gated prompt steps (every routed expert in host memory, or the bounded host tier, #37; with LEGION_DEBUG_SLOT_LEARN=0 only with RAM slots for at least half of each layer's experts): prompts shorter than --long-prompt-tokens read their experts in place, as before, instead of borrowing the cache for staging from 128 tokens (--short-prompt-tokens default). It also makes an explicit --short-prompt-tokens lend stage with the fused expert op again (and from --prefill-stage-min-tokens only), the old path. With the switch on, a short prompt's steps keep their chunks and the ops of an unlent step (the fused op only where that one takes it), so the output is that of an unlent cache. For A/B runs.
LEGION_DEBUG_ROUTE_GATE_AHEAD=KiB With the route gate on: how many KiB per prompt token the copy stream copies ahead into the next layer (its hottest staged experts) while the router runs. Default 96; 0 turns it off.
LEGION_DEBUG_ROUTE_GATE_TIER=N The bounded host expert tier (experts bigger than the RAM, #37) on one GPU: staged prompt steps of up to N tokens (default as LEGION_DEBUG_ROUTE_GATE) that would read at least 4 MiB per token from the GGUF read and copy only the experts their tokens route to. 0 reads and copies every expert of the pass, as before. For A/B runs. Output does not change.
LEGION_DEBUG_ROUTE_GATE_READ_AHEAD=KiB With the tier's route gate on: at most how many KiB per prompt token, per read batch, the tier's reader reads ahead from the GGUF while it waits for the layer's router (the layer's hottest experts, as large a share as the pass's layers before routed); the router's choices drop the unrouted reads not started yet. Default 256; 0 turns it off.
LEGION_DEBUG_SLOT_LEARN=0 The bounded host expert tier (#37): a routing-gated staged pass reads every routed expert without a RAM slot into a staging place and gives up no RAM slot (the way before round 42), instead of reading the hottest of them into the RAM slots of the layer's experts no token routes to (coldest first, as a step's fetch evicts) and copying them from there; short prompts then borrow the cache only with RAM slots for at least half of each layer's experts, as before. For A/B runs. Output does not change.
LEGION_DEBUG_DUP_REFILL=0 The bounded host expert tier (#37), where its RAM slots hold at least half of each layer's experts: after a lend's restore, the RAM slots whose experts the refill copied into VRAM slots keep those copies until decode misses evict them (the way before round 43), instead of taking the hottest experts in neither tier, read in the background behind every other read once the refill is done. For A/B runs. Output does not change.
LEGION_DEBUG_SWAP_OVERLAP=0 The bounded host expert tier (#37): a VRAM swap whose new expert no RAM slot holds reads it from the checkpoint and waits for the read and the copy in the after-step, one swap after another (the way before round 45), instead of reading it into a RAM slot in the background while the next step runs and starting its copy at the start of the next after-step. =1: in the background, but that after-step waits for the copies (the slot fills there, an after-step earlier than by default). For A/B runs. Output does not change.
LEGION_DEBUG_SHARE_PREFETCH=0 The exact --cpu-expert-share (#37): the pool does not pull each thread's first down rows into its cache while it waits for the GPU's SwiGLU blocks (the way before round 19), for A/B runs. Output does not change.
LEGION_DEBUG_SHARE_SHEXP=<0-2> The exact --cpu-expert-share on CUDA (#37): where a share layer runs its shared expert, 0 after the routed experts (the way before round 20), 1 while the pool runs the down rows, 2 (the default) while it runs the gate and up rows, for A/B runs. Output does not change.
LEGION_DEBUG_SHARE_LAPS=<file> The exact --cpu-expert-share (#37): append one line a pool job to file (seq layer experts picked gu_done act_seen down_done, CLOCK_REALTIME nanoseconds, the clock of a CUPTI trace's host stamps), for a decode attribution. Output does not change.
LEGION_DEBUG_SHARE_STREAM_WAIT=1 --cpu-expert-share on CUDA (#37): the stream waits for the host's "gate/up done" and "done" flags before the activate and collect kernels (cuStreamWaitValue32, the way before) instead of those kernels' host-pair blocks polling the flags, for A/B runs. Output does not change.
LEGION_DEBUG_EXPERT_DUMP=<file>[,every=<n>][,full=<n>][,max=<n>] The decode expert matvec (CUDA, #37): write to file what the kernels read and wrote in steps of up to 8 tokens (each op's ids, the first 64 bytes of each picked expert, the Q8_1 activation blocks, a digest of each expert's output rows; a SwiGLU op's raw gate and up sums, from two more launches of the same kernels), for legion expert-cpu-proof check to recompute on the host and compare bit for bit. every: one op in n; full: the first n records carry the rows themselves; max: stop after n records. Stage graphs leave the expert ops out and recorded steps are slower. Output does not change.
LEGION_DEBUG_GDN_PF=<MiB> The DeltaNet z prefetch into L2 on a paced side stream (#689; CUDA, compute capability 12.x, stage graphs, dense models, steps of up to 8 tokens): prefetch this many MiB (up to 64, 0 turns it off) instead of the default 8. Added by PR #105.
LEGION_DEBUG_GDN_PF_FORK=0 The DeltaNet z prefetch: run it on the compute stream, unpaced, instead of on the forked side stream (the default). Added by PR #105.
LEGION_DEBUG_GDN_PF_PACE=<ns> The DeltaNet z prefetch: one step every ns nanoseconds (up to 1 ms, 0: all at once) instead of every 1000. Added by PR #105.
LEGION_DEBUG_GDN_PF_SHAPE=<KiB>x<threads> The DeltaNet z prefetch: bytes per bulk prefetch (a multiple of 4 KiB, up to 1024 KiB) and threads per CTA (1-32) instead of 4 KiB x 32. Added by PR #105.
LEGION_DEBUG_GDN_PF_FAR=1 The DeltaNet z prefetch: prefetch as many bytes of the LM head instead of z's rows (a probe: the DRAM traffic without the data). Off by default. Added by PR #105.
LEGION_DEBUG_GDN_PF_LATE=1 The DeltaNet z prefetch: the prefetch kernel issues its prefetches before it waits for the qkv projection instead of after. Off by default. Added by PR #105.
LEGION_DEBUG_GDN_PF_CONV=1 The DeltaNet z prefetch: no prefetch node; the layer's conv_silu kernel issues the prefetch at its start. Off by default. Added by PR #105.
LEGION_DEBUG_GDN_Z_SIDE=0 A DeltaNet layer with an FP8 z projection where the z prefetch applies (#689): go back to the paced z prefetch instead of running the z projection itself on the side stream, beside the layer's small kernels (the default). Added by PR #107.
LEGION_DEBUG_SIDE_PRIO=0 A stage graph with a kernel on the side stream (the z projection above): leave all its kernels at one priority instead of giving the compute stream's kernels the greatest one (the default). Added by PR #107.
LEGION_DEBUG_MVF_ROWS=<n> The op for small BF16 matmuls of 1-8 tokens (a DeltaNet layer's alpha and beta gates, #689): 1 or 2 output rows a CTA instead of 4. Added by PR #100.
LEGION_DEBUG_MVQ_SWIGLU=0 The NVFP4 decode matvec of a SwiGLU FFN's down projection (#689): run the SwiGLU as a kernel of its own instead of inside the matvec's quantization kernel (the default). Added by PR #103.
LEGION_DEBUG_DRAFT_Q4K_MV=<mode> The DFlash drafter's Q4_K matmuls (--spec-draft-weights q4, --spec-draft-lm-head q4, #689): 0 runs them all on ggml's mul_mat instead of Legion's Q4_K matvec op, head takes the op for the head only, layers for the layers only; by default both. Added by PR #102.
LEGION_DEBUG_DFLASH_KEEP_FEATS=0 The DFlash drafter's take-in of rows read back to the host (prompt chunks, and decode steps whose rows are not left on the device, #1133): a fresh features vector every take-in (the way before), instead of one buffer the drafter keeps and reuses. A prompt's piece of 512 rows of 5 fused layers is about 55 MB on a 5,376-wide target, a new mapping faulted in and unmapped each prompt. For A/B runs. Each rank with a drafter (or a share of one) reads its own. Output does not change.
LEGION_DEBUG_DFLASH_INGEST_PIPE=0 The DFlash drafter's take-in of a sequence's rows in several pieces of 512 (a prompt's last chunks, #1133): waits for each piece's graph before the next piece (the way before), one allocator, instead of queueing every piece but the last on two allocators in turn, so that a piece's graph is built and its input written while the piece before runs. The same graphs in the same order on the same stream; the second allocator is the one DFlash 2's draft pass uses, grown to a piece's take-in when that is bigger. RUST_LOG=legion::spec::dflash=debug logs each take-in of 512 rows or more (dflash take-in: rows, pieces, ms, gather_ms, upload_ms, wait_ms and both allocators' MiB). For A/B runs. Each rank with a drafter (or a share of one) reads its own. Output does not change.
LEGION_DEBUG_DFLASH_INGEST_ROWS=0 The DFlash drafter's take-in of rows read back to the host (prompt chunks, #1133): gathers each piece's rows of every fused layer into one host buffer and uploads that (the way before) instead of copying each layer's rows into the graph's input with one strided host-to-device copy, with no pass over them on the host. The same floats either way. For A/B runs. Each rank with a drafter (or a share of one) reads its own. Output does not change.
LEGION_DEBUG_DRAFT_FUSE=0 The DFlash drafter's quantized weights (#689): store them one tensor each instead of fused (the default). Added by PR #100.
LEGION_DEBUG_TP_DRAFT_HEAD=0 --tp-draft-split on two ranks with a DFlash 2 drafter, greedy drafts and a Q4_K draft head (#666): rank 0 runs the whole draft head, as before, instead of each rank running half the vocabulary and rank 0 merging the two top-k lists (the default), for A/B runs. Every rank must set the same. Same drafts either way. Added by PR #170.
LEGION_DEBUG_TP_DRAFT_HEAD_CHECK=1 The split draft head of --tp-draft-split (#666): rank 0 also runs the whole head every draft pass and logs how many rows' merged candidates or logits differ from it (every 256 passes, and at once for a pass with a difference). Set it on rank 0. Slows drafting; output does not change. Added by PR #170.
LEGION_DEBUG_SPEC_WINDOWS=0 The commit graph of speculative decoding's DeltaNet states (#689): copy each layer's conv window in a launch of its own instead of every layer's in one launch (the default). Added by PR #100.
LEGION_DEBUG_HC_GB10=<mode> Qwen3.8-Flash-Next's hyper-connection mixer on a GB10 (#1176): whole stages the down rows whole and launches the norm plainly (the kernels before), ring or early keeps only one of the two; by default both (the down rows through a ring in shared memory, the norm launched early). Added by PR #82.
LEGION_DEBUG_TP_ROWS=0 Tensor parallelism with an n-gram table read through a store (#1176): the ranks above 0 read the rows their cache misses themselves instead of asking rank 0 for them over the TP link (the default). Added by PR #106.
LEGION_DEBUG_TP_ROWS_SMALL=0 The same (#1176): the ranks above 0 still read the small batches Linux AIO takes (a decode or verify step's rows, a draft's read-ahead) themselves, as before, instead of asking rank 0 for them too (the default). On two DGX Sparks the second rank's reads go over NFS (~0.5 ms a 4 KiB read against ~0.1 ms on the first rank's disk). Same bits either way.
LEGION_DEBUG_NGRAM_PADS=0 An n-gram table read through a store with --tp-mtp-chain (#1176): a draft round does not read the n-gram rows of the verify step's padding ahead, as before. By default, once a sequence's drafts stop (under --spec-p-min or at their cap), the round starts reading the rows of the tokens that pad it to the verify step's width (its bonus token again), which the verify step's fill otherwise read itself (~16 rows a 16-user step on two DGX Sparks). Set it on rank 0. Same bits either way.
LEGION_DEBUG_TP_ROW_HINT=0 Tensor parallelism with an n-gram table (#1176): rank 0 does not send the other ranks a prompt's next chunk to read ahead (the default); they read each chunk's rows in its own step. Added by PR #104.
LEGION_DEBUG_NODE_LINK_SPIN=0 The node link of tensor parallelism over machines and of a model split by layers (#1176): every wait for a frame (rank 0's writer thread and its reads of the replies, rank 1's reader thread and its step loop) blocks at once, as before, instead of spinning for 5 ms and then napping 50 µs at a time for up to a second (the default). Same bits either way. Added by PR #120.
LEGION_DEBUG_NODE_LINK_BELL=0 The node link (#1176): rank 1's reader thread and rank 0's writer thread are not rung, as before. By default rank 1 rings its reader as it answers a request and rank 0 its writer as it queues a frame, so a wait that was napping (a verify step's commit comes 40-100 ms after the step) wakes at once and spins again; without it rank 1 starts the commit up to a nap (~0.1 ms) later. The same switch gives the two link threads a 1 µs timer slack (Linux's default is 50 µs), so a nap lasts ~50 µs instead of ~0.1 ms; =0 keeps the default slack too. Same bits either way.
LEGION_DEBUG_TP_EMPTY_COPIES=1 Tensor parallelism over machines (#1176): rank 0 still sends the other ranks the empty state copy every step ends with, as before, instead of nothing (the default). The next step's send waited for its answer, a round trip every decode round. Same bits either way. Added by PR #120.
LEGION_DEBUG_TP_EARLY_COMMIT=0 Tensor parallelism over machines with recurrent states and drafts (#1176): rank 0 sends the other ranks a verify step's commit after the step's text (detokenizer, stream sends, stop strings), as before, instead of as soon as its accept counts are known (the default, only in steps where no sequence can end: no grammar, no stop strings, no stop token and no length cap among its tokens). Set it on rank 0. Same commit and bits either way.
LEGION_DEBUG_TP_SEND_AHEAD=0 Tensor parallelism over machines (#1176): before a step, MTP pass or draft round goes out, rank 0 reads every answer still owed once two are, as before, instead of only those older than the last step sent (the default). Before, a verify step after a draft round waited for the round's answer, a round trip on the draft-to-verify edge. Set it on rank 0. Same bits either way.
LEGION_DEBUG_TP_RAIL_HALVES=<bytes> Tensor parallelism over machines with --tp-rails 2 (#1176): exchanges to a peer of at least this many bytes and under 1 MiB go in two halves, one RDMA write on each rail (default 131072, 128 KiB: a 16-user decode step's sums); 0 sends every exchange under 1 MiB as one write on the first rail, as before. Exchanges from 1 MiB go in 1 MiB pieces over both rails either way. Every rank must set the same. Same bits either way.
LEGION_DEBUG_GEMMA_TP_HALVES=0 Gemma 4 dense models under tensor parallelism over machines (#619): prompt steps of at least 2,048 tokens whose matmuls all run on the W4A4 kernels (--nvfp4-a4 prefill) run their layers whole again, each sum one all-reduce, instead of in two halves of their tokens whose sums travel while the GPU computes the other half (the default). Every rank must set the same. Same bits either way.
LEGION_DEBUG_GEMMA4_TP_PARTS=<n> The same Gemma 4 halves (#1181): the most parts of a prompt step's attention output projection. 2 runs it in the two halves, as before; 3 (the default) runs half 0's in two parts, a third and two thirds of its tokens (an 8K step: 682 + 1,366 + 2,048), each part's sum started behind its matmul and finished into one tensor, so that half 0's sum mostly travels during the next part's projection instead of waiting in sight before half 0's FFN. A part under 512 tokens is not made (the halves instead), and at most two sums are in flight. Every rank must set the same. Same bits either way.
LEGION_DEBUG_QWEN35_TP_HALVES=0 Dense Qwen3.5+ models (e.g. Qwen3.8-27B) under tensor parallelism over machines (#1133): prompt steps of at least 2,048 tokens sum each layer whole again, one all-reduce per sum, instead of in two halves of their tokens, each half's FFN running while the other half's sum travels (the default, in the layers whose FFN runs on the W4A4 kernels, --nvfp4-a4 prefill, or on the FP8 W8A8 GEMM, --fp8-act a8-dynamic; the others stay whole). Every rank must set the same. Same bits either way.
LEGION_DEBUG_QWEN35_TP_PARTS=<n> The same layers (#1133): the most FFN parts a prompt step's layer takes. 2 runs the FFN in the two halves, as before; 3 runs half 1's FFN in two parts, three quarters and a quarter of its tokens; 4 (the default) in three, 5/8, 1/4 and 1/8 of them (an 8K step: 4,096 + 2,560 + 1,024 + 512), so the layer's last sum, which travels with nothing left to compute, is an eighth of the halves' last one. A part under 512 tokens is not made (fewer parts instead), and the mixer sum stays in two halves. Every rank must set the same. Same bits either way.
LEGION_DEBUG_QWEN35_TP_FP8_PARTS=<n> The same layers (#1133): the layers whose FFN runs on the FP8 W8A8 GEMM with per-token activation scales (--fp8-act a8-dynamic; e.g. the FP8 MLPs of Qwen3.8-27B NVFP4's last 8 layers). 0 runs them whole, one all-reduce per sum, as before; 2 to 4 caps their FFN parts at that many, as LEGION_DEBUG_QWEN35_TP_PARTS does for the others; unset (the default) they take as many parts as the others. Every rank must set the same. Same bits either way.
LEGION_DEBUG_TP_SUM_NORM=0 Dense Qwen3.5+ models (e.g. Qwen3.8-27B) under tensor parallelism over machines with --fp8-act a8-dynamic prefill (#1133): the RMS norm that reads a layer's sum (the next layer's input norm after the FFN sum; the post-attention norm when the FFN reads FP8 activations) runs as its own ops after the sum, as before, instead of in the sum's kernel, which computes the norm's FP8 codes (and BF16 rows for a DeltaNet layer) from the sums it writes, saving a read of them (the default where the projection that reads the norm takes its FP8 codes). Every rank should set the same. Same bits either way.
LEGION_DEBUG_TP_SEND_KERNEL=0 Tensor parallelism over machines on CUDA (#1176): an exchange whose partials travel in F32 (decode steps) copies this rank's partial into the pinned send buffer with cudaMemcpyAsync, as before, instead of a copy kernel (the default). On a GB10 the kernel is ~4 us for a 16-user decode sum against ~12 us on the copy engine, and the sum waits less after it. The same bytes either way.
LEGION_DEBUG_TP_SEND_GEMM=0 Tensor parallelism over machines on CUDA (#1133): a prompt step's micro all-reduce whose partials travel in F32 copies this rank's partial from its matmul's output into the pinned send buffer, as before, instead of running the matmul (an FP8 output projection's columns, a W4A4 FFN down) straight into the send buffer (the default). Qwen3.8-27B NVFP4 over two DGX Sparks drops ~220 copies of an 8K step. Same bits either way.
LEGION_DEBUG_TP_SEND_STAGED=0 Tensor parallelism over machines on CUDA (#1133): the matmuls that write straight into the pinned send buffer (LEGION_DEBUG_TP_SEND_GEMM) store their outputs the usual way, as before, instead of through shared memory in whole runs of a token's rows (the default). On a GB10 the usual 32-byte pieces go out to that mapped host memory about half as fast as whole lines. Same bits either way.
LEGION_DEBUG_NGRAM_FIFO=1 An n-gram table read directly from the SSD (#1176): the I/O threads take every read in asking order (the old queue) instead of a step's reads ahead of queued read-ahead. Off by default. Added by PR #104.
LEGION_DEBUG_TP_EP_GET_ROWS=1 --tp-moe-ep (#1176): remap the skipped experts' ids and weights with ggml's get_rows (the way before) instead of one remap kernel per table. Off by default. Added by PR #109.
LEGION_DEBUG_GLM_BF16_GEMV=<n> GLM-5.3-Flash's BF16 decode GEMV of 8 tokens or fewer where the staged GEMV is on (not V100, #560): 0 linear_mv, 1 linear_mv_bf16, 2 (the default) linear_mv_bf16 at 1 token and linear_mv_bf16x at 2-8; 3 also takes 9-16 tokens on matrices of 2048+-value rows, which moves output. Added by PR #80.
LEGION_DEBUG_GLM_Q6K_MV=<bits> GLM-5.3-Flash's Q6_K decode GEMV of 8 tokens or fewer where the staged GEMV is on (#1172): 0 the kernels before; bit 0 (the default, 1) linear_mv_q6k_rw for MLA's short rows; bit 1 two stages in flight; bit 2 the next kernel launched early; bit 3 (#1172 round 8, opt-in) the draft LM head's rows on linear_mv_q6k_rw too; bits 4-6 log2 of more rows a warp. Every mode gives the same bits; legion lin-bench --q6k times them. Added by PR #108, bit 3 by #118.
LEGION_DEBUG_GLM_SMALL_MV=<bits> GLM-5.3-Flash's small decode products of 1-4 token steps where the staged GEMV is on (#1172), a sum of bits: 1 the router's F32 GEMV with the inputs in shared memory; 2 the 128-value Q8_0 rows (ssm_f_b, ssm_g_b) two a warp; 4 hc_pre's mix weights read once; 8 hc_pre's finish launched early by the mix, its streams and norm weight loaded before the wait; 16 the shared expert's gate, up and GLU in one launch; 32 early triggers in hc_finish_wide, pool_keys and glu_mv_q6k_sp; 64 (opt-in) the kv_a, q_a and shared expert down rows loaded before the wait; 128 the routed experts' decode kernels back to back with PDL. 191 (all but 64) by default, 31 the kernels of PR #111, 7 those of PR #110, 0 the kernels before. Added by PR #110.
LEGION_DEBUG_GLM_SHARE_LEVEL=<0-4> GLM-5.3-Flash with --tp-seq-par (#560): how much of a step's per-token work runs on the token share, each level adding to the ones below; 4 (all) by default. Set it on every rank alike. Added by PR #97.
LEGION_DEBUG_GLM_DRAFT_PREFIX=1 GLM-5.3-Flash with --spec-draft-vocab (#1172): draft over the head's first rows only, without the number tokens. Off by default. Only the drafts change; greedy output does not. Added by PR #84.
LEGION_DEBUG_PAGED_D512_PAIR=0 Gemma 4's global layers (heads of 512, 8 query heads a kv head) on compute capability 8.0+ (CUDA, #1181): small multi-token groups (speculative verify steps whose grid of 4 tokens a CTA fits on the SMs in one wave) run the prompt attention kernel paged_prefill_mma (the way before) instead of the warp-pair kernel, which splits each query column's output over two warps, for A/B runs. Output does not change; legion paged-qm-bench --heads 32,4 --d512 auto,pair checks the bits and times them.
LEGION_DEBUG_PAGED_D512_PREFETCH=0 The warp-pair kernel above (paged_prefill_pair, #689): its CTAs no longer prefetch the first line of every K and V block of their keys into L2 at the start for groups of one or two sequences (the way before). As in the quad verify kernel below, the prefetch translates the keys' pages before the loop reaches them, which a call in a model meets cold after the other layers' traffic. With more sequences it stays off: their pages at once ran slower. For A/B runs. Output does not change; legion paged-qm-bench --heads 32,4 --d512 pair --cold rot --pf 0,1 checks the bits and times both.
LEGION_DEBUG_PAGED_VERIFY_GRID=0 / =1 Attention layers with heads of 256 and 6 query heads a kv head (Qwen3.8-27B) on compute capability 8.0+ (CUDA, #689): small multi-token groups (speculative verify steps of up to 16 tokens a sequence) normally run on paged_prefill_quad (each group of 8 query columns on its own CTA of 4 warps) where that grid is at most half the SMs, else on paged_prefill_grid (two groups a CTA, each on a warp pair) where that grid fits on the SMs in one wave. =1 drops the quad kernel (the pair kernel wherever its grid fits); =0 runs the prompt attention kernel paged_prefill_mma (the way before, one CTA per 64 query columns) for every group. For A/B runs. Output does not change; legion paged-qm-bench --heads 24,4 --grid auto,pair,quad checks the bits and times them.
LEGION_DEBUG_PAGED_VERIFY_PREFETCH=0 The quad verify kernel above (paged_prefill_quad, #689): its CTAs no longer prefetch the first line of every K and V block of their keys into L2 at the start (the way before). The prefetch translates the keys' pages before the loop reaches them, which a call in a model meets cold after the other layers' traffic: on a GB10, Qwen3.8-27B NVFP4's verify attention at 64K went 1.70 -> 1.47 ms a call. For A/B runs. Output does not change; legion paged-qm-bench --heads 24,4 --grid quad,quad0 checks the bits and times both.
LEGION_DEBUG_QM_PINGPONG=0 Prompt attention at heads of 256 and 6 query heads a kv head (Qwen3.8-27B) on compute capability 12.x (CUDA, #1133): chunks of 64 tokens a sequence or more run the queries-as-M kernel paged_prefill_qm with all 8 warps in step (the way before) instead of as a ping-pong of two 4-warp groups, for A/B runs. Output does not change; legion paged-qm-bench --modes 2,258 checks the bits and times both.
LEGION_DEBUG_NVFP4_PF32=1 NVFP4 checkpoints' routed experts at prefill (CUDA): the SwiGLU GEMM's 128- and 64-token tiles prefetch their weights into L2 in runs of 32 blocks (the way before #1176 rounds 35 and 36) instead of 8, for A/B runs. Output does not change.
LEGION_DEBUG_FP8_STREAM_EPI=0 W8A8 FP8 prefill GEMMs on the TMA-fed tiles (--fp8-act a8-dynamic, compute capability 12.x; Qwen3.8-27B's FP8 projections, with the fused residual on attn_output, ssm_out and its FP8 ffn_down): the epilogue writes one row group at a time with all its residuals loaded first (the way before #1133 round 25) instead of two token tiles at a time with streaming stores and residual loads, for A/B runs. Output does not change; legion fp8-gemm-bench checks the bits and times both.
LEGION_DEBUG_NVFP4_VERIFY_TUNE=0 NVFP4 dense matmuls of 33-64 tokens on CUDA (#1181; a verify step of 5-8 users' drafts, a short prompt step) on matrices of 4096-8192 rows (not the fused GLU): the W4A16 GEMM's 128-row CTAs in three stages, one per SM (the way before), instead of 64-row CTAs in four stages, two per SM. For A/B runs. Output does not change; legion nvfp4-mv-bench --dense K:M --tokens 33,64 checks the bits and times the variants.
LEGION_DEBUG_NVFP4_VERIFY_STREAM=0 NVFP4 dense matmuls of 33-64 tokens on CUDA (#1181; a verify step of 5-8 users' drafts, a short prompt step) on matrices of 4096 rows or more and the fused GLU: the W4A16 GEMM's row tiles (the way before, with LEGION_DEBUG_NVFP4_VERIFY_TUNE's tune) instead of the streaming GEMM (one CTA per SM sized to the rows, each warp 16 rows by all 64 tokens over the whole of k, the activations shared by the CTA's warps). For A/B runs. Output does not change; legion nvfp4-mv-bench --dense K:M --tokens 33,64 times the variants and cargo test --test nvfp4_mm -- --ignored cuda_verify_stream checks the bits.
LEGION_DEBUG_NVFP4_A4_GENERIC=1 --nvfp4-a4 dense prefill GEMMs on a GB10 (#1133 round 12): the TMA-fed tile reads its weight fragments and scale words by generic loads and the SwiGLU takes 128 x 96 tiles from 16384 tokens (the kernel before) instead of shared-memory loads and 128 x 128 tiles, for A/B runs. Output does not change.
LEGION_DEBUG_TP_DEVICE_ROWS=0 Qwen3.8-Flash-Next over machines with MTP (CUDA): a prompt chunk's step reads all its captured rows back on every rank and the MTP head's take-in pieces upload them again (the way before #1176 round 39) instead of copying them on the device, for A/B runs. Set it on rank 0. Output does not change.
LEGION_DEBUG_TP_ROWS_FROM=0 Tensor parallelism over machines with --spec dflash (#1133): a prompt chunk's step reads every captured row back on every rank (the way before) instead of only the rows from the first one the drafter takes in (its ring's last rows of the prompt, none for a chunk the ring overwrites before the prompt ends), as one GPU does. With --tp-draft-split the other ranks read those rows for their share of the drafter; without it they capture none. For A/B runs. Set it on rank 0. Output does not change.
LEGION_DEBUG_TP_DRAFT_DEVICE_ROWS=0 Tensor parallelism over machines with --spec dflash --tp-draft-split (CUDA, #1133): a decode or verify step reads its captured rows back on every rank and each rank's share of the drafter uploads the rows it takes in (the way before) instead of leaving them on the device, where the drafter's take-in picks them, as on one GPU. For A/B runs. Set it on rank 0 (the other ranks follow its steps). Output does not change.
LEGION_DEBUG_TP_DRAFT_RECYCLE=0 Tensor parallelism over machines with --spec dflash --tp-draft-split (#1133): a rank above 0 keeps its last two steps' captured rows (the way before) instead of handing the last step's back when the next step starts, as rank 0 does after its draft round; kept, a long prompt's last chunks copied into new vectors more often there (pages faulted in, extra vectors unmapped). For A/B runs. Set it on the rank above 0. Output does not change.
LEGION_DEBUG_DFLASH_DEFER_TAKE_IN=0 --spec dflash (#1133): a draft round of prompt chunks' take-ins only (no sequence decoding, none drafting) runs at once (the way before) instead of waiting for the next round, which runs it first. The drafter takes in a prompt's last 2,064 rows; when they span a chunk boundary (an 8K prompt over 4,096-token steps, a 32K one over 16,384-token steps) the earlier chunk's take-in ran before the last chunk's step, inside the time to the first token, and with --tp-draft-split on every rank. The drafter runs the same take-ins of each sequence in the same order before its first drafts. For A/B runs. Set it on every rank (rank 0 for one machine; with --tp-draft-split each rank decides by itself). Output does not change.
LEGION_DEBUG_TP_AR_PF=<MiB> Gemma 4 dense models over machines (CUDA, compute capability 12.x, #1181): at each sum of a step of 9 to 256 tokens (verify steps of 2+ users), once this rank's partial is on its way, a side stream reads the first MiB of the next matmul's weights (the FFN's gate and up, the next layer's q, k and v) into L2 while the rank waits for the others' partials. 12 by default, at most 20; 0 off (the way before). Set it on every rank. Output does not change.
LEGION_DEBUG_TP_AR_PF_PACE=<ns> That prefetch, paced: one 128 KiB step every ns nanoseconds (up to 1 ms) instead of all at once (the default). Output does not change.
LEGION_DEBUG_QWEN4_QSA_SHARE=0 Qwen3.8-Flash-Next over machines with --tp-seq-par (CUDA): every rank scores the sparse attention's pools and picks the best for every token of a big step (the way before #1176 round 37) instead of for its share of the tokens, then exchanging the picks, for A/B runs. Set it on every rank. Output does not change.
LEGION_DEBUG_QSA_GW_L1=0 Qwen3.8-Flash-Next prompt steps on compute capability 12.x (CUDA, the GB10): the sparse-attention prompt kernel copies K and V rows with cp.async.cg (L2 only, the way before #1176 round 42) instead of cp.async.ca (through L1), for A/B runs. Output does not change.
LEGION_DEBUG_QSA_PROMPT_KERNEL=mma|gb|gw Qwen3.8-Flash-Next prompt steps with an F16 cache on compute capability 8.0 or newer (CUDA): every sparse-attention prompt launch takes that kernel instead of the launcher's choice (the two-CTA kernel on Ampere and Ada, its warp-local variant on compute capability 12.x, the one-CTA kernel elsewhere), for A/B runs; mma on Ada is the way before #1213 round 30. Output does not change.
LEGION_DEBUG_HC_POST_NORM_CS=0 Qwen3.8-Flash-Next prompt steps with BF16 streams (CUDA): the post-norm reads the old streams and writes the new ones and its outputs with plain loads and stores (the way before #1176 round 44) instead of evict-first (ld.global.cs/st.global.cs, the default on compute capability 12.x), for A/B runs; =1 turns the evict-first copies on for other GPUs. Output does not change.
LEGION_DEBUG_CONV_SILU_CS=0 Gated DeltaNet layers (CUDA): the fused conv reads its inputs and writes its outputs with plain loads and stores, an input a thread at a time (the way before #1176 round 44) instead of evict-first with eight in flight (the default on compute capability 12.x), for A/B runs; =1 turns the new way on for other GPUs. Output does not change.
LEGION_DEBUG_GATED_NORM_CS=0 Gated DeltaNet layers (CUDA): the gated output norm reads and writes with plain loads and stores, a block a head and token (the way before #1176 round 44) instead of evict-first with a warp a head and token for heads of 128 (the default on compute capability 12.x), for A/B runs; =1 turns the new way on for other GPUs. Output does not change.
LEGION_DEBUG_GDN_GATED_NORM=0 MoE models' prompt steps of one sequence with BF16 output projections on compute capability 12.x (CUDA, the GB10): the delta rule's whole-head kernel writes its F32 outputs and a separate kernel computes the gated output norm from them (the way before #1176 round 44), instead of the whole-head kernel computing the norm from its outputs in shared memory and writing the BF16 rows the projection reads, for A/B runs. Output does not change.
LEGION_DEBUG_HC_UP_MIX_L1=0 Qwen3.8-Flash-Next prompt steps with BF16 mixer weights on compute capability 12.x (CUDA, the GB10): the mixer's up GEMM copies its low-rank rows with cp.async.cg (L2 only, the way before #1176 round 43) instead of cp.async.ca (through L1), for A/B runs. Output does not change.
LEGION_DEBUG_HC_UP_MIX_L1=1 Qwen3.8-Flash-Next prompt steps with BF16 mixer weights on other sm_80+ GPUs (CUDA): the mixer's up GEMM copies its low-rank rows through L1 (cp.async.ca) as on compute capability 12.x, for A/B runs (#1213 round 30: 0.92-0.96x the time per call on an RTX 4090; GGUF mixers are Q8_0 and don't take this kernel). Output does not change.
LEGION_DEBUG_QSA_SEL=<prefix>[,keep=<call>.<call>...] Qwen3.8-Flash-Next prompt steps (CUDA, #1176): after every sparse-attention call of more than 64 tokens, copy the token selections to the host (a stream wait; measurement only) and print a qsa-sel line on how much groups of 2, 4 and 8 neighbouring tokens' selections overlap; calls numbered in keep are also written to <prefix>-<call>.bin for legion qsa-bench --sel. Output does not change.
LEGION_META_DEBUG=1 Tensor parallelism: print how each tensor is split over the GPUs.
LEGION_DEBUG_FAIL_CUMEMCREATE_AFTER=<n>[,<count>] The expert cache's slot allocations (CUDA): after the first n cuMemCreate calls for VRAM slots, the next count (1 if left out) fail as out of memory, to try the load's shrink to fit (#1208: the cache plans that GPU again in less and the memory plan says how many slots it kept).
LEGION_EXPERT_COUNTS=<file> On exit, write the raw routing counts per layer and expert as JSON, for offline expert-cache studies.
LEGION_SSD_EXPERT_TRACE=<file> The SSD expert tier: write every step's routed expert ids per layer and the router lookahead's predictions of them (GLM-5.3-Flash: the next layer's router on the post-attention and post-MoE states, and on the MTP draft's state under --spec mtp; binary, src/residency/ssd_experts/trace.rs), for legion ssd-replay <file> to compare slot policies, prediction recall and prefetches offline. Small steps run one extra router per layer while it is set.
LEGION_CPU_BENCH=<rows>,<columns> The shape of the ignored CPU kernel micro-benchmark (cpu::bench).
LEGION_MOE_SPEED_TYPES=<gu>,<down> GGML type numbers of the experts in the ignored fused-MoE speed test.
LEGION_TINY=1 The ignored sparse-attention GPU tests: smallest cases only (for compute-sanitizer runs).
LEGION_TEST_DS41_HF=<dir> The ignored released_token_map test: the DeepSeek-V4.1-Flash directory holding the checkpoint's tokenizer.json, whose Engram token map is compared with the released GGUF's.
LEGION_TEST_PAIR_*, LEGION_TEST_HC_* Model, text and sizes of the ignored KL measurements in src/engine/lm/tests/ (see their file headers).

Build time

LEGION_GIT_HASH, LEGION_GGML_HASH and LEGION_CUDA are set by build.rs for legion --version and /health; setting them at run time does nothing.