Skip to content

Commit f9b5001

Browse files
Merge branch 'main' into dependabot/github_actions/github-actions-f538117abd
2 parents a892c52 + 4d1ec3c commit f9b5001

11 files changed

Lines changed: 287 additions & 46 deletions

File tree

‎scripts/ci/hardware_qualification.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import tempfile
1212
import time
1313
from collections.abc import Callable
14+
from dataclasses import replace
1415
from pathlib import Path
1516
from typing import Any
1617

@@ -299,11 +300,14 @@ def predict(value: LSTMPortfolioModel) -> np.ndarray:
299300

300301

301302
def _deep(batch: PortfolioSequenceBatch, device: str) -> FitOnce:
303+
adjacency = np.zeros((batch.features.shape[2], batch.features.shape[2]), dtype=bool)
304+
adjacency[0, :] = True
305+
batch = replace(batch, adjacency_mask=adjacency)
302306
config = DeepPortfolioConfig(
303307
d_model=4,
304308
n_heads=1,
305309
cross_attention_heads=1,
306-
macro_gnn_heads=1,
310+
macro_gnn_heads=2,
307311
max_iters=1,
308312
eval_every=1,
309313
checkpoint_every=1,

‎src/ml4t/models/asset_prediction/sae.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,27 @@ def _iterate_batches(
327327
seed: int,
328328
):
329329
n_obs = int(features.shape[0])
330+
if n_obs < 2:
331+
raise ValueError(
332+
"SAE training requires at least 2 valid observations; "
333+
f"got {n_obs}. BatchNorm cannot update on a single row."
334+
)
330335
if batch_size is None or batch_size <= 0 or batch_size >= n_obs:
331336
yield features, targets
332337
return
338+
# BatchNorm1d rejects a one-row training batch. Keep the configured size
339+
# when it is already safe, and fold a trailing singleton into the previous
340+
# batch so every observation is still used.
341+
step = max(int(batch_size), 2)
333342
generator = torch.Generator(device=features.device)
334343
generator.manual_seed(seed)
335344
order = torch.randperm(n_obs, generator=generator, device=features.device)
336-
for start in range(0, n_obs, batch_size):
337-
idx = order[start : start + batch_size]
345+
starts = list(range(0, n_obs, step))
346+
if len(starts) > 1 and n_obs - starts[-1] == 1:
347+
starts.pop()
348+
for index, start in enumerate(starts):
349+
end = starts[index + 1] if index + 1 < len(starts) else n_obs
350+
idx = order[start:end]
338351
yield features[idx], targets[idx]
339352

340353

‎src/ml4t/models/integration/backtest.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,9 @@ def backtest_inputs_from_weights(
405405
def _coerce_feed_spec_mapping(schema: Any | None) -> dict[str, Any]:
406406
feed_spec_cls = _load_feed_spec_class()
407407
if feed_spec_cls is not None:
408-
return {
409-
key: value
410-
for key, value in asdict(feed_spec_cls.from_any(schema)).items()
411-
if value is not None
412-
}
408+
spec = feed_spec_cls.from_any(schema)
409+
data = spec.to_dict() if hasattr(spec, "to_dict") else asdict(spec)
410+
return {key: value for key, value in data.items() if value is not None}
413411
return _fallback_feed_spec_mapping(schema)
414412

415413

‎src/ml4t/models/latent_factors/cae.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def fit(
146146
loss_sums: dict[int, float] = dict.fromkeys(checkpoint_epochs, 0.0)
147147
val_best_losses: list[float] = []
148148
skipped_updates = 0
149+
applied_updates = 0
149150

150151
for ensemble_idx in range(self.config.n_ensemble):
151152
seed = self.config.seed + ensemble_idx
@@ -192,6 +193,7 @@ def fit(
192193

193194
epoch_loss += float(loss.item())
194195
n_batches += 1
196+
applied_updates += 1
195197

196198
mean_loss = epoch_loss / max(n_batches, 1)
197199
if epoch in checkpoint_epochs:
@@ -218,6 +220,13 @@ def fit(
218220
self._checkpoint_states[0].append(best_state)
219221
val_best_losses.append(best_val_loss)
220222

223+
if self.config.hidden_units and applied_updates == 0:
224+
raise ValueError(
225+
"CAE training updated no parameters because every minibatch had one row. "
226+
"Beta BatchNorm cannot train on a single observation; provide at least two "
227+
"valid rows and set batch_size to at least 2."
228+
)
229+
221230
history: list[dict[str, float | str]] = []
222231
for epoch in checkpoint_epochs:
223232
if epoch in self._checkpoint_states:

‎src/ml4t/models/portfolio/components.py‎

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,24 @@ def __init__(self, *, d_model: int, n_heads: int, dropout: float, lag: int) -> N
179179
def forward(self, x: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
180180
batch_size, n_periods, n_assets, d_model = x.shape
181181
if self.lag > 0:
182-
pad = torch.zeros(
183-
(batch_size, self.lag, n_assets, d_model),
184-
device=x.device,
185-
dtype=x.dtype,
186-
)
187-
kv = torch.cat([pad, x[:, : n_periods - self.lag]], dim=1)
188-
pad_mask = torch.zeros(
189-
(batch_size, self.lag, n_assets),
190-
device=mask.device,
191-
dtype=mask.dtype,
192-
)
193-
kv_mask = torch.cat([pad_mask, mask[:, : n_periods - self.lag]], dim=1)
182+
history = n_periods - self.lag
183+
if history <= 0:
184+
# A negative slice would wrap to the end of the window.
185+
kv = torch.zeros_like(x)
186+
kv_mask = torch.zeros_like(mask)
187+
else:
188+
pad = torch.zeros(
189+
(batch_size, self.lag, n_assets, d_model),
190+
device=x.device,
191+
dtype=x.dtype,
192+
)
193+
kv = torch.cat([pad, x[:, :history]], dim=1)
194+
pad_mask = torch.zeros(
195+
(batch_size, self.lag, n_assets),
196+
device=mask.device,
197+
dtype=mask.dtype,
198+
)
199+
kv_mask = torch.cat([pad_mask, mask[:, :history]], dim=1)
194200
else:
195201
kv = x
196202
kv_mask = mask
@@ -239,16 +245,21 @@ def forward(self, x: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
239245
query = x.reshape(batch_size * n_periods, n_assets, d_model)
240246
key_padding_mask = mask.reshape(batch_size * n_periods, n_assets) < 0.5
241247
adjacency_mask = self.get_buffer("_adjacency_mask_buffer")
242-
combined = adjacency_mask.unsqueeze(0) | key_padding_mask.unsqueeze(1)
243-
all_blocked = combined.all(dim=-1)
244-
if all_blocked.any():
245-
key_padding_mask = key_padding_mask & ~all_blocked
246-
attended, _ = self.attention(
247-
query,
248-
query,
249-
query,
250-
attn_mask=adjacency_mask,
251-
key_padding_mask=key_padding_mask,
252-
)
248+
# True blocks attention. A query with no legal key makes softmax NaN,
249+
# so fall back to self when that asset is present and otherwise allow
250+
# a finite dummy attention that the caller mask zeros out.
251+
blocked = adjacency_mask.unsqueeze(0) | key_padding_mask.unsqueeze(1)
252+
fully_blocked = blocked.all(dim=-1)
253+
if bool(fully_blocked.any()):
254+
eye = torch.eye(n_assets, device=blocked.device, dtype=torch.bool)
255+
allow_self = (
256+
fully_blocked.unsqueeze(-1) & eye.unsqueeze(0) & (~key_padding_mask).unsqueeze(1)
257+
)
258+
blocked = blocked & ~allow_self
259+
still_blocked = blocked.all(dim=-1)
260+
if bool(still_blocked.any()):
261+
blocked = blocked & ~still_blocked.unsqueeze(-1)
262+
blocked = blocked.repeat_interleave(self.attention.num_heads, dim=0)
263+
attended, _ = self.attention(query, query, query, attn_mask=blocked)
253264
attended = attended.reshape(batch_size, n_periods, n_assets, d_model)
254265
return self.layer_norm(x + self.dropout(attended))

‎src/ml4t/models/portfolio/postprocessors.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,11 @@ def apply_turnover_limit(
217217
) -> np.ndarray:
218218
"""Scale cross-sectional weight changes to satisfy an L1 turnover cap."""
219219

220+
if not np.isfinite(turnover_limit) or turnover_limit < 0.0:
221+
raise ValueError(f"turnover_limit must be finite and non-negative; got {turnover_limit}")
222+
220223
constrained = np.asarray(weights, dtype=np.float64).copy()
221224
batch_size, n_periods, _ = constrained.shape
222-
if turnover_limit <= 0:
223-
return np.zeros_like(constrained)
224225

225226
previous = (
226227
np.asarray(previous_weights, dtype=np.float64).copy()

‎tests/test_cae.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,33 @@ def test_cae_pipeline_forecasts_returns_through_checkpointed_two_step_path() ->
189189
assert prediction.factor_forecast.factor_premia.shape == (3, 1)
190190
assert prediction.asset_forecast.expected_returns.shape == (3, n_assets)
191191
assert np.isfinite(prediction.asset_forecast.expected_returns).all()
192+
193+
194+
@pytest.mark.parametrize(
195+
"batch",
196+
[
197+
CrossSectionBatch(
198+
characteristics=np.ones((1, 1, 2), dtype=np.float64),
199+
returns=np.ones((1, 1), dtype=np.float64),
200+
),
201+
CrossSectionBatch(
202+
characteristics=np.ones((2, 3, 2), dtype=np.float64),
203+
returns=np.ones((2, 3), dtype=np.float64),
204+
),
205+
],
206+
)
207+
def test_cae_rejects_fits_that_never_update_parameters(batch: CrossSectionBatch) -> None:
208+
model = CAEModel(
209+
CAEConfig(
210+
n_factors=1,
211+
hidden_units=(4,),
212+
n_epochs=2,
213+
checkpoint_interval=2,
214+
batch_size=1,
215+
)
216+
)
217+
218+
with pytest.raises(ValueError, match="updated no parameters"):
219+
model.fit(batch)
220+
221+
assert not model.is_fitted

‎tests/test_financial_correctness.py‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
from ml4t.models.latent_factors.ipca import _normalize_theta_y
1717
from ml4t.models.latent_factors.rp_pca import _risk_premium_matrix
1818
from ml4t.models.portfolio import runtime as portfolio_runtime
19+
from ml4t.models.portfolio.components import CrossSectionalAttention, MacroGraphAttention
1920
from ml4t.models.portfolio.losses import compute_net_portfolio_returns, softmin_sharpe
20-
from ml4t.models.portfolio.postprocessors import normalize_cross_sectional_weights
21+
from ml4t.models.portfolio.postprocessors import (
22+
apply_turnover_limit,
23+
normalize_cross_sectional_weights,
24+
)
2125

2226

2327
def test_rp_pca_default_matches_lettau_pelger_signal_matrix() -> None:
@@ -326,6 +330,91 @@ def fake_validation(
326330
)
327331

328332

333+
def test_cross_attention_lag_longer_than_the_window_does_not_wrap() -> None:
334+
torch.manual_seed(0)
335+
window = torch.randn(1, 3, 2, 4)
336+
mask = torch.ones(1, 3, 2)
337+
matched = CrossSectionalAttention(d_model=4, n_heads=1, dropout=0.0, lag=3)
338+
longer = CrossSectionalAttention(d_model=4, n_heads=1, dropout=0.0, lag=8)
339+
longer.load_state_dict(matched.state_dict())
340+
matched.eval()
341+
longer.eval()
342+
343+
matched_output = matched(window, mask)
344+
longer_output = longer(window, mask)
345+
346+
assert matched_output.shape == window.shape
347+
assert torch.isfinite(longer_output).all()
348+
torch.testing.assert_close(longer_output, matched_output)
349+
350+
351+
def test_graph_attention_stays_finite_when_a_node_has_no_neighbors() -> None:
352+
torch.manual_seed(0)
353+
layer = MacroGraphAttention(
354+
d_model=4,
355+
n_heads=1,
356+
dropout=0.0,
357+
adjacency_mask=torch.ones(2, 2, dtype=torch.bool),
358+
)
359+
layer.eval()
360+
features = torch.randn(1, 2, 2, 4, requires_grad=True)
361+
mask = torch.ones(1, 2, 2)
362+
363+
output = layer(features, mask)
364+
output.sum().backward()
365+
366+
assert torch.isfinite(output).all()
367+
assert features.grad is not None
368+
assert torch.isfinite(features.grad).all()
369+
370+
371+
def test_graph_attention_ignores_a_masked_only_neighbor() -> None:
372+
layer = MacroGraphAttention(
373+
d_model=4,
374+
n_heads=1,
375+
dropout=0.0,
376+
adjacency_mask=torch.tensor([[True, False], [True, True]]),
377+
)
378+
layer.eval()
379+
base = torch.randn(1, 1, 2, 4)
380+
shifted = base.clone()
381+
shifted[..., 1, :] += 10.0
382+
mask = torch.tensor([[[1.0, 0.0]]])
383+
384+
base_output = layer(base, mask)
385+
shifted_output = layer(shifted, mask)
386+
387+
assert torch.isfinite(base_output).all()
388+
torch.testing.assert_close(base_output[:, :, 0, :], shifted_output[:, :, 0, :])
389+
390+
391+
def test_zero_turnover_limit_holds_the_previous_book() -> None:
392+
weights = np.array([[[0.4, -0.4], [0.2, -0.1]]], dtype=np.float64)
393+
previous = np.array([[0.5, -0.5]], dtype=np.float64)
394+
395+
actual = apply_turnover_limit(
396+
weights,
397+
previous_weights=previous,
398+
mask=np.ones_like(weights, dtype=bool),
399+
turnover_limit=0.0,
400+
)
401+
402+
np.testing.assert_allclose(actual[0, 0], previous[0])
403+
np.testing.assert_allclose(actual[0, 1], previous[0])
404+
405+
406+
def test_negative_turnover_limit_is_rejected() -> None:
407+
weights = np.ones((1, 1, 2), dtype=np.float64)
408+
409+
with pytest.raises(ValueError, match="turnover_limit"):
410+
apply_turnover_limit(
411+
weights,
412+
previous_weights=np.zeros((1, 2)),
413+
mask=np.ones_like(weights, dtype=bool),
414+
turnover_limit=-0.1,
415+
)
416+
417+
329418
def test_portfolio_constraints_are_satisfied_jointly() -> None:
330419
raw = np.array([[[10.0, 1.0, -1.0, -2.0]]])
331420
mask = np.ones_like(raw, dtype=bool)

‎tests/test_portfolio.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,42 @@ def test_deep_portfolio_reuses_fitted_topology_and_rejects_changes() -> None:
316316
)
317317
with pytest.raises(ValueError, match="adjacency_mask"):
318318
model.predict(changed)
319+
320+
321+
@pytest.mark.parametrize("heads", [1, 2])
322+
@pytest.mark.parametrize("isolated_asset", [False, True])
323+
def test_deep_portfolio_graph_masks_train_with_multiple_heads(
324+
heads: int, isolated_asset: bool
325+
) -> None:
326+
rng = np.random.default_rng(71)
327+
adjacency = np.zeros((2, 2), dtype=bool)
328+
if isolated_asset:
329+
adjacency[0, :] = True
330+
train = PortfolioSequenceBatch(
331+
features=rng.normal(size=(2, 3, 2, 2)),
332+
returns=0.01 * rng.normal(size=(2, 3, 2)),
333+
adjacency_mask=adjacency,
334+
asset_ids=("A", "B"),
335+
)
336+
model = DeepPortfolioModel(
337+
DeepPortfolioConfig(
338+
d_model=4,
339+
n_heads=1,
340+
cross_attention_heads=1,
341+
macro_gnn_heads=heads,
342+
dropout=0.0,
343+
max_iters=1,
344+
eval_every=1,
345+
checkpoint_every=1,
346+
default_checkpoint=1,
347+
batch_size=2,
348+
device="cpu",
349+
)
350+
)
351+
352+
fit = model.fit(train)
353+
prediction = model.predict(train)
354+
355+
assert fit.converged
356+
assert prediction.weights.shape == (2, 3, 2)
357+
assert np.isfinite(prediction.weights).all()

0 commit comments

Comments
 (0)