3131from weathergen .model .layers import MLP
3232from weathergen .model .utils import ActivationFactory
3333from weathergen .utils .utils import get_dtype
34+ from weathergen .datasets .utils import healpix_verts_rots , r3tos2
3435
3536
3637class 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
0 commit comments