Skip to content

Commit 4d1ec3c

Browse files
committed
fix: expand graph attention masks across heads
1 parent 63e38f2 commit 4d1ec3c

3 files changed

Lines changed: 45 additions & 1 deletion

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/portfolio/components.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ def forward(self, x: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
259259
still_blocked = blocked.all(dim=-1)
260260
if bool(still_blocked.any()):
261261
blocked = blocked & ~still_blocked.unsqueeze(-1)
262+
blocked = blocked.repeat_interleave(self.attention.num_heads, dim=0)
262263
attended, _ = self.attention(query, query, query, attn_mask=blocked)
263264
attended = attended.reshape(batch_size, n_periods, n_assets, d_model)
264265
return self.layer_norm(x + self.dropout(attended))

‎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)