Update modeling_tiny_mixtral.py
Browse files- modeling_tiny_mixtral.py +13 -1
modeling_tiny_mixtral.py
CHANGED
|
@@ -582,9 +582,21 @@ class TinyMixtralForCausalLM(PreTrainedModel, GenerationMixin):
|
|
| 582 |
)
|
| 583 |
self.model = tiny_mixtral(args=args)
|
| 584 |
self.config = config
|
| 585 |
-
self.
|
| 586 |
self.post_init()
|
| 587 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 588 |
def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
|
| 589 |
|
| 590 |
outputs, load_balancing_loss = self.model(input_ids, start_pos=0)
|
|
|
|
| 582 |
)
|
| 583 |
self.model = tiny_mixtral(args=args)
|
| 584 |
self.config = config
|
| 585 |
+
self._move_model_to_device(config.device)
|
| 586 |
self.post_init()
|
| 587 |
|
| 588 |
+
def _move_model_to_device_safe(self, target_device):
|
| 589 |
+
"""Safely move model to target device"""
|
| 590 |
+
# Check if any parameter is on meta device
|
| 591 |
+
has_meta_params = any(param.device.type == 'meta' for param in self.model.parameters())
|
| 592 |
+
|
| 593 |
+
if has_meta_params:
|
| 594 |
+
print("Detected meta tensors, using to_empty() to move model")
|
| 595 |
+
self.model.to_empty(device=target_device)
|
| 596 |
+
else:
|
| 597 |
+
print(f"Moving model to {target_device} using standard to()")
|
| 598 |
+
self.model.to(target_device)
|
| 599 |
+
|
| 600 |
def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
|
| 601 |
|
| 602 |
outputs, load_balancing_loss = self.model(input_ids, start_pos=0)
|