---
title: "Prepare Market Data for Forecasting with NVIDIA Megatron Core"
description: "Market data arrives as a stream of trades and quotes, timestamped to the microsecond, requiring aggregation into bars for AI-based forecasting. NVIDIA Megatron Core, an open-source library of GPU-optimized components, powers transformer model training at scale; this post shows offline preprocessing that turns intraday bars into patches of, for example, eight bars per patch and 32 patches per context window, and then training a small model with Megatron-LM. The preprocessing records configuration like patch length and forecast horizons in metadata and saves outputs as memory-mapped PyTorch tensors. The data loader supplies batches to distributed training while enforcing date-based splits and an embargo to prevent look-ahead bias, with reads shared within tensor-parallel groups. This framework forecasts log returns and trading volume across several horizons from each context position, supporting both point and cumulative targets with the same input representation."
---

# Prepare Market Data for Forecasting with NVIDIA Megatron Core

[NVIDIA](https://yomu.fyi/company/nvidia-developer-blog) · Liana Mikaelyan · Nov 24, 2026

**Type:** Tutorial

## Summary

Market data arrives as a stream of trades and quotes, timestamped to the microsecond, requiring aggregation into bars for AI-based forecasting. NVIDIA Megatron Core, an open-source library of GPU-optimized components, powers transformer model training at scale; this post shows offline preprocessing that turns intraday bars into patches of, for example, eight bars per patch and 32 patches per context window, and then training a small model with Megatron-LM. The preprocessing records configuration like patch length and forecast horizons in metadata and saves outputs as memory-mapped PyTorch tensors. The data loader supplies batches to distributed training while enforcing date-based splits and an embargo to prevent look-ahead bias, with reads shared within tensor-parallel groups. This framework forecasts log returns and trading volume across several horizons from each context position, supporting both point and cumulative targets with the same input representation.

## Context

This guide extends Megatron Core’s application to financial time series, providing a pipeline from raw bar data to transformer-based multi-horizon forecasting.

## Approach / What changed

The preprocessing script (preprocessing.py) converts Parquet bar files into PyTorch tensors grouped by patches, context windows, and forecast targets. Then torchrun launches a Megatron-LM training script that loads the tensors via a memory-mapped dataset provider and trains a transformer model using distributed data and tensor parallelism.

## Takeaways

- Preprocessing groups consecutive bars into patches (model tokens) and slides context windows over them, retaining features like log returns and session position indices per patch.
- Forecast targets can be point (a single future bar’s return and volume) or cumulative (summed log return and total volume over the interval), both computed at every context position for multiple horizons.
- Data is split by date into train/val/test with an embargo to prevent look-ahead bias; target normalization statistics use only the training split, and the loader reconstructs valid-target masks from saved values.

## FAQ

### What input format does preprocessing require?

A directory of Parquet files with columns Symbol, Date, Timestamp, Close, Volume, and optional numeric features. Each instrument's trading day should be in one file, files in chronological order. The script handles per-instrument daily sorting and derives log returns and log volumes internally.

### How are forecast horizons defined?

Horizons are specified in observation steps (e.g., 1,2,5,15 for one-minute bars meaning 1-15 minutes). Targets can be point (value at that future step) or cumulative (aggregate over interval). Volume targets use log scale.

### How does the data loader prevent train/test leakage?

Outputs are split by date into train, validation, and test sets. A window is kept only if its entire context and future observations for the longest horizon fall within the same split. An embargo removes trading days before validation, and context patches across splits never overlap.

**Tags:** [GPU](https://yomu.fyi/topic/gpu), [Megatron Core](https://yomu.fyi/topic/megatron-core), [NVIDIA](https://yomu.fyi/topic/nvidia), [Time Series](https://yomu.fyi/topic/time-series), [Transformer](https://yomu.fyi/topic/transformer)

- Source: [NVIDIA](https://developer.nvidia.com/blog/prepare-market-data-for-forecasting-with-nvidia-megatron-core)
- Source URL: https://developer.nvidia.com/blog/prepare-market-data-for-forecasting-with-nvidia-megatron-core
- Ingested by Yomu: 2026-11-24T18:01:14.257Z

[Read original post](https://developer.nvidia.com/blog/prepare-market-data-for-forecasting-with-nvidia-megatron-core)
