sunjuice
/

sunjuice commited on
Commit
bc1e698
·
1 Parent(s): 43189e0

project complete

Browse files
Files changed (1) hide show
  1. modeling_molmo2.py +13 -1
modeling_molmo2.py CHANGED
@@ -758,7 +758,7 @@ class Molmo2DecoderLayer(GradientCheckpointingLayer):
758
  ):
759
  super().__init__()
760
  self.config = config
761
-
762
  self.self_attn = Molmo2Attention(config, layer_idx)
763
  self.attn_norm = Molmo2RMSNorm(
764
  config.hidden_size, eps=config.layer_norm_eps, device=device)
@@ -782,6 +782,7 @@ class Molmo2DecoderLayer(GradientCheckpointingLayer):
782
  ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]:
783
 
784
  residual = hidden_states
 
785
  hidden_states = self.attn_norm(hidden_states)
786
 
787
  # Self Attention
@@ -797,12 +798,23 @@ class Molmo2DecoderLayer(GradientCheckpointingLayer):
797
  **kwargs,
798
  )
799
 
 
 
 
 
 
 
 
 
 
 
800
  hidden_states = residual + self.dropout(hidden_states)
801
 
802
  # Fully Connected
803
  residual = hidden_states
804
  hidden_states = self.ff_norm(hidden_states)
805
  hidden_states = self.mlp(hidden_states)
 
806
 
807
  hidden_states = residual + self.dropout(hidden_states)
808
 
 
758
  ):
759
  super().__init__()
760
  self.config = config
761
+ self.device = device
762
  self.self_attn = Molmo2Attention(config, layer_idx)
763
  self.attn_norm = Molmo2RMSNorm(
764
  config.hidden_size, eps=config.layer_norm_eps, device=device)
 
782
  ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]:
783
 
784
  residual = hidden_states
785
+ block_device = self.device
786
  hidden_states = self.attn_norm(hidden_states)
787
 
788
  # Self Attention
 
798
  **kwargs,
799
  )
800
 
801
+ def move_if_needed(x, device):
802
+ if x is None:
803
+ return x
804
+ if isinstance(x, tuple):
805
+ return tuple(move_if_needed(xx, device) for xx in x)
806
+ if hasattr(x, "device") and x.device != device:
807
+ return x.to(device)
808
+ return x
809
+
810
+ residual = move_if_needed(residual, block_device)
811
  hidden_states = residual + self.dropout(hidden_states)
812
 
813
  # Fully Connected
814
  residual = hidden_states
815
  hidden_states = self.ff_norm(hidden_states)
816
  hidden_states = self.mlp(hidden_states)
817
+ residual = move_if_needed(residual, block_device)
818
 
819
  hidden_states = residual + self.dropout(hidden_states)
820