A trading desk looking to forecast returns or trading volume from raw market data faces the classic pain: every bid, ask, and execution lands as a microsecond-stamped stream. Before an AI model can learn from it, you have to batch those ticks into something consistent, leak-proof, and GPU-friendly. NVIDIA's Megatron Core, the open-source library behind Megatron-LM, now includes a preprocessing pipeline that does exactly that for intraday market bars.
The pipeline turns a directory of Parquet files—one per instrument trading day, with columns like Symbol, Date, Timestamp, Close, and Volume—into ready-to-train PyTorch tensors. The process groups consecutive bars into patches (think: tokens for a transformer), slides context windows over them, and computes return and volume targets at multiple horizons. The result: a set of memory-mapped tensor splits (train.pt, val.pt, test.pt, meta.pt) that keep all the heavy parsing and target construction out of the training loop.
How the pipeline shapes your data
You start with Parquet files, one row per market bar, in chronological order. The script sorts and groups bars for each instrument and day, then forms patches—say, eight bars per patch, thirty-two patches per window. Each patch keeps all its features, so you can feed longer contexts or extra numeric columns without changing the interface. Instrument IDs and patch positions track everything for the model.
Targets (returns and log-volumes) are computed for each context window, at user-chosen horizons (like 1, 2, 5, or 15 bars ahead). The pipeline records patch size, context length, and forecast horizons in metadata, making experiments reproducible and explicit.
Guarding against data leakage
Temporal splits are handled carefully: data is divided by date, with embargo periods to prevent cross-split leakage. Target statistics (means, scales) are computed only from the training split, so there's no forward-looking bias. Overnight gaps are excluded by default (), and the script treats missing bars as-is—if you want a regular time grid, you have to align upstream.




