torchtrade icon indicating copy to clipboard operation
torchtrade copied to clipboard

Portfolio env: store observation windows once, not per rollout step

Open BY571 opened this issue 2 months ago • 0 comments

Follow-up to #435 (PortfolioTradingEnv). Not a decision — a proposal to weigh once the env and DPM exist, and to discard if it does not pay for itself.

The observation

An observation in the portfolio env is a window of market history per asset: [N assets, W bars, F features]. For a 110-asset universe with DPM's window (W=50, F=3) that is 16,500 floats, about 66 KB as float32.

Consecutive observations overlap almost completely, because the window slides one bar at a time:

step t   : [951 .......... 1000]
step t+1 :     [952 .......... 1001]   49 of 50 bars identical
step t+2 :         [953 .......... 1002]

In an offline env every parallel environment also replays the same history, so the overlap repeats across the batch dimension too.

Why it matters

On-policy algorithms collect a rollout before learning, and off-policy ones keep a replay buffer. If those buffers store materialised observation windows, the same numbers are stored many times over:

rollout stored observations memory (float32)
1 env x 1 step 1 66 KB
64 envs x 512 steps 32,768 2.16 GB
256 envs x 512 steps 131,072 8.65 GB

The unique data behind a 512-step rollout is only (512 + 50) x 110 x 3 x 4 B ≈ 0.74 MB, so a 64-env rollout holds roughly the same market history ~2,900 times.

For scale: the whole hourly cross-asset panel (110 assets, 2020 to 2026) is about 154 MB as a float32 tensor. The dataset is not the problem; the buffers are.

Proposal

  1. The env keeps the panel as one contiguous [T, N, F] tensor, loaded once.
  2. Observations are views into it (narrow/unfold), never copies.
  3. Rollout and replay buffers store the time index t and the portfolio weights (a few hundred bytes) instead of the window; the window is rebuilt on sampling. This is valid because market data is fixed and identical for every environment — an offline env's observation is fully determined by (t, weights).
  4. With worker processes, share_memory_() the panel so N workers share one copy rather than N.

DPM already works this way: its Portfolio-Vector Memory stores weights per period and reads price windows straight from the data, which is part of why it trains on long histories cheaply.

Reasons to discard it

  • It may be unnecessary. With few environments and short rollouts the duplication is affordable, and 2.16 GB only appears at 64 envs x 512 steps.
  • TorchRL may already solve it. LazyMemmapStorage / MemoryMappedTensor spill buffers to disk, which removes the memory pressure without changing the env interface, at the cost of I/O.
  • It adds coupling. A buffer holding indices is only meaningful together with the panel that indices refer to: checkpoints, replay dumps and anything shipped between machines must carry or re-load the panel, and a buffer recorded against one dataset version is meaningless against another.
  • It constrains the observation. Any observation component that is not a pure function of (t, weights) (sampled noise, randomised costs, live features) breaks the reconstruction and would have to be stored anyway.

Scope

Only the env's observation handling and the vectorized variant in #435; the classic baselines (UBAH, UCRP, OLMAR) keep no buffers and are unaffected.

Suggested decision point

After DPM and the first RL training runs: measure the actual rollout memory in a real configuration. If it is comfortable, close this as not needed. The cheap half — panel as one shared tensor, observations as views — is worth doing regardless, because retrofitting it later means changing the env interface.

BY571 avatar Sep 16 '26 16:09 BY571