Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/megatron/bridge/models/conversion/auto_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import dataclasses
from functools import cached_property, partial
from pathlib import Path
from typing import Any, Generic, Iterable, List, Optional, Type, TypeVar, Union
from typing import Any, Generic, Iterable, List, Literal, Optional, Type, TypeVar, Union

import torch.distributed as dist
import transformers
Expand Down Expand Up @@ -101,6 +101,8 @@ def __init__(self, hf_pretrained: PreTrainedCausalLM | PretrainedConfig):
if not isinstance(hf_pretrained, (PreTrainedCausalLM, PretrainedConfig)):
raise ValueError("hf_pretrained must be a PreTrainedCausalLM or PretrainedConfig instance")
self.hf_pretrained: PreTrainedCausalLM | PretrainedConfig = hf_pretrained
# Data type for exporting weights
self.export_weight_dtype: Literal["bf16", "fp16", "fp8"] = "bf16"

@classmethod
def list_supported_models(cls) -> list[str]:
Expand Down Expand Up @@ -319,10 +321,10 @@ def load_hf_weights(
# Preserve trust_remote_code setting from the original bridge instance
trust_remote_code = getattr(self.hf_pretrained, "trust_remote_code", False)
pre_trained = PreTrainedCausalLM.from_pretrained(hf_path, trust_remote_code=trust_remote_code)
self._model_bridge.load_weights_hf_to_megatron(
pre_trained, model, allowed_mismatched_params=allowed_mismatched_params
)

bridge = self._model_bridge
bridge.load_weights_hf_to_megatron(pre_trained, model, allowed_mismatched_params=allowed_mismatched_params)
# Get unquantized_state_dict from the bridge instance that was used for optimizer reload
self.unquantized_state_dict = getattr(bridge, "unquantized_state_dict", None)
return model

def export_hf_weights(
Expand Down Expand Up @@ -371,6 +373,13 @@ def export_hf_weights(
... cpu=True
... ))
"""
# Build conversion tasks based on export_weight_dtype configuration
if conversion_tasks is None and self.export_weight_dtype == "fp8":
if not isinstance(model, list):
model = [model]
# Use FP8 export tasks for blockwise FP8 weights
conversion_tasks = self._model_bridge.build_export_fp8_tasks(self.hf_pretrained, model)

dispatch_instance = (self._causal_lm_architecture, self._get_model_instance(model))
return model_bridge.stream_weights_megatron_to_hf(
dispatch_instance,
Expand Down Expand Up @@ -1051,7 +1060,9 @@ def _model_bridge(self) -> "MegatronModelBridge":
else:
hf_config = self.hf_pretrained

return model_bridge.get_model_bridge(self._causal_lm_architecture, hf_config=hf_config)
bridge = model_bridge.get_model_bridge(self._causal_lm_architecture, hf_config=hf_config)
bridge.export_weight_dtype = self.export_weight_dtype
return bridge

@property
def _provider_bridge_input(self) -> PreTrainedCausalLM | _ConfigOnlyPretrainedShim:
Expand Down
Loading