PARAS MADAN
All writing

Latest in AI

Run NVIDIA Dory in Transformers 5.24: Test Loop Count and KV Cache

Load one Dory 4B checkpoint, vary its recurrent passes without changing weights, and compare latency, memory and answer quality.

If your local model takes too long to answer, a faster run is usually a different model, a shorter prompt or a more aggressive quantization. With NVIDIA's Dory 4B, you can change how many times it runs its middle layers while keeping the same weights. That gives you another setting to test before switching checkpoints.

Transformers 5.24.0 shipped native Dory support on November 21. The NVIDIA checkpoint was released in October. What is new is the route through the stable Transformers package, including access to the loop and cache settings from Python.

Start with two loops and a fresh cache

Use the same checkpoint, tokenizer and chat template for every test. The versioned Transformers guide uses the BF16 checkpoint through AutoModelForCausalLM. To set up one baseline, pass the loop and cache settings through the model config:

from transformers import AutoConfig, AutoModelForCausalLM

checkpoint = "nvidia/NVIDIA-Nemotron-Labs-Dory1.0-4B-BF16"
config = AutoConfig.from_pretrained(
    checkpoint,
    n_recurrent_loops=2,
    recurrent_kv_cache_mode="per_loop",
)
model = AutoModelForCausalLM.from_pretrained(
    checkpoint, config=config, device_map="auto"
)

Use the tokenizer's chat template when you call generate. Run the other configurations separately and start with an empty cache each time. Don't recycle cache state from another configuration: the model implementation builds different cache layouts depending on these settings. If you use automatic device placement, also check that every comparison uses the same placement. A run that spills layers onto the CPU isn't a clean measurement of the extra loops.

Compare complete answers at one, two and four loops

Two loops is the released default. One cuts the amount of recurrent computation per generated token; four repeats the shared block more times. That extra work can be worthwhile if you get more correct answers or if the model needs fewer output tokens to finish. Neither outcome follows from the loop count alone.

Pick one task with a grade you can check, such as extracting totals from invoices supplied as text. Lock the prompts, sampling settings and output cap. Score whether the amounts add up and whether the response preserves currency and tax, rather than treating a longer explanation as a better answer.

Keep time to first visible answer, total request time and output length together in the results. A run with faster token generation can still be slower if the answer rambles. A run that repeatedly hits its output cap should count as unfinished, even if the tokens it returned look good.

Use last-loop caching when memory is the constraint

The per_loop baseline keeps separate key/value history for each visit to the recurrent block. With last_loop, only the final visit writes to that history. Earlier visits still do attention work and use the shared past. That avoids growing the recurrent cache with the loop count, but doesn't make the extra computation free or cap memory growth as the prompt length rises.

To isolate the tradeoff, run four loops first with per_loop and then with last_loop. Keep the prompt and placement fixed. Compare peak memory and the same answer checks at both short and long prompt lengths. The cache change affects what earlier visits can attend to, so don't assume their outputs are identical.

I'd keep the two-loop configuration unless another setting wins on the metric that actually matters for the application. If both give the same number of correct answers, lower latency or more room for the next request can decide it.

I'm Paras. I work at the intersection of AI, engineering and distribution.

I help AI teams turn model releases and serving tradeoffs into technical writing developers can use.

Book a 30-minute call