Discussing the article: "Foundation Models for Trading (Part II): Decoding, Autoregression, and an Exact KV-Cache"

 

Check out the new article: Foundation Models for Trading (Part II): Decoding, Autoregression, and an Exact KV-Cache.

We complete the native MQL5 port of Kronos: the decoder, the predictor's decode_s1 and decode_s2 stages with their cross-attention traps, and the autoregressive loop that produces a multi-bar forecast. Then we profile and make it roughly 4.5x faster with an exact KV-cache and pre-transposed weights, verifying every stage against PyTorch.

This part moves to the other half of the model. We build the decoder that turns tokens back into candles, the predictor that generates new tokens one step at a time, and the autoregressive loop that ties them together into a multi-bar forecast. Along the way we meet the subtle traps that cost real debugging time: a cross-attention head that uses a different number of heads than the rest of the model, a rotary embedding that rotates every key at the same position, and an embedding that is scaled in one place but left raw in another. Each is the kind of mistake that runs without error and quietly corrupts the output.

Then we make it fast. A first working port generated a forecast slowly enough to be impractical for any real study. Two fixes together cut the per-step time by roughly four and a half times: a key/value cache that skips the work the loop was repeating, and a weight-transpose fix that the profiler surfaced in a place intuition would not have looked. By the end of this part, the full pipeline runs end to end, verified stage by stage against the PyTorch reference: a window of candles goes in, and a multi-bar forecast in real price units comes out.

Foundation Models for Trading (Part II): Decoding, Autoregression, and an Exact KV-Cache"

Author: Muhammad Minhas Qamar