Skip to content

Commit a95bc9e

Browse files
author
Tharmeekan
committed
Add ROPE coords to ForecastingEngine and update reset_parameters method
1 parent fb9588b commit a95bc9e

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

src/weathergen/model/engines.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from weathergen.model.layers import MLP
3232
from weathergen.model.utils import ActivationFactory
3333
from weathergen.utils.utils import get_dtype
34+
from weathergen.datasets.utils import healpix_verts_rots, r3tos2
3435

3536

3637
class EmbeddingEngine(torch.nn.Module):
@@ -555,6 +556,27 @@ def __init__(self, cf: Config, mode_cfg, num_healpix_cells: int, dim_aux: int =
555556
self.cf = cf
556557
self.num_healpix_cells = num_healpix_cells
557558
self.fe_blocks = torch.nn.ModuleList()
559+
self.rope_2D = cf.get("rope_2D", False)
560+
self.healpix_level = cf.healpix_level
561+
self.dtype = get_dtype(cf.attention_dtype)
562+
563+
564+
if self.rope_2D:
565+
num_extra_tokens = cf.num_register_tokens + cf.num_class_tokens
566+
total_tokens = (
567+
self.num_healpix_cells + num_extra_tokens
568+
) * cf.ae_local_num_queries
569+
self.register_buffer(
570+
"rope_coords",
571+
torch.zeros(
572+
1,
573+
total_tokens,
574+
2,
575+
dtype=self.dtype
576+
),
577+
)
578+
else:
579+
self.rope_coords = None
558580

559581
global_rate = int(1 / self.cf.forecast_att_dense_rate)
560582
if mode_cfg.get("forecast", {}).get("policy") is not None:
@@ -621,7 +643,24 @@ def init_weights_final(m):
621643
for block in self.fe_blocks:
622644
block.apply(init_weights_final)
623645

624-
def forward(self, tokens, fstep, coords=None):
646+
647+
def reset_parameters(self) -> None:
648+
"""HEALPix neighbourhood based parameter initializing for target prediction."""
649+
650+
cf = self.cf
651+
652+
if self.rope_2D:
653+
verts, _ = healpix_verts_rots(self.healpix_level, 0.5, 0.5)
654+
coords = r3tos2(verts.to(self.rope_coords.device)).to(self.rope_coords.dtype)
655+
coords = coords.unsqueeze(1).repeat(1, cf.ae_local_num_queries, 1)
656+
coords_flat = coords.flatten(0, 1).unsqueeze(0)
657+
num_extra_tokens = cf.num_register_tokens + cf.num_class_tokens
658+
offset = num_extra_tokens * cf.ae_local_num_queries
659+
self.rope_coords.data.fill_(0.0)
660+
self.rope_coords.data[:, offset : offset + coords_flat.shape[1], :].copy_(coords_flat)
661+
662+
663+
def forward(self, tokens, fstep):
625664
if self.training:
626665
# Impute noise to the latent state
627666
noise_std = self.cf.get("fe_impute_latent_noise_std", 0.0)
@@ -633,7 +672,7 @@ def forward(self, tokens, fstep, coords=None):
633672
if isinstance(block, torch.nn.modules.normalization.LayerNorm):
634673
tokens = checkpoint(block, tokens, use_reentrant=False)
635674
else:
636-
tokens = checkpoint(block, tokens, coords, aux_info, use_reentrant=False)
675+
tokens = checkpoint(block, tokens, self.rope_coords, aux_info, use_reentrant=False)
637676
return tokens
638677

639678

src/weathergen/model/model.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ def _reset_params(module):
467467
self.apply(_reset_params)
468468
if self.encoder is not None:
469469
self.encoder.reset_parameters()
470+
if self.forecast_engine is not None:
471+
self.forecast_engine.reset_parameters()
470472

471473
def print_num_parameters(self) -> None:
472474
"""Print number of parameters for entire model and each module used to build the model"""
@@ -578,10 +580,10 @@ def forward(self, model_params: ModelParams, batch: ModelBatch) -> ModelOutput:
578580
without_grad = p_fwd and self.training and step != max(batch.get_output_idxs())
579581
if without_grad:
580582
# Pushforward mode: advance tokens without grad; no decoding with torch.no_grad():
581-
tokens = self.forecast_engine(tokens, step, self.encoder.rope_coords)
583+
tokens = self.forecast_engine(tokens, step)
582584
continue
583585

584-
tokens = self.forecast_engine(tokens, step, self.encoder.rope_coords)
586+
tokens = self.forecast_engine(tokens, step)
585587
# decoder predictions
586588
output = self.predict_decoders(model_params, step, tokens, batch, output)
587589
# latent predictions (raw and with SSL heads)

0 commit comments

Comments
 (0)