Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled generalizing build_inner_model in ComposerHFCausalLM #1450

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Changes from 3 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
20 changes: 14 additions & 6 deletions llmfoundry/models/hf/hf_causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from torchmetrics import Metric
from transformers import (
AutoConfig,
AutoModel,
AutoModelForCausalLM,
GenerationConfig,
PreTrainedModel,
Expand Down Expand Up @@ -193,6 +194,7 @@ def build_inner_model(
config_overrides: Dict[str, Any],
load_in_8bit: bool,
pretrained: bool,
model_cls: Union[AutoModel, PreTrainedModel] = AutoModelForCausalLM,
gupta-abhay marked this conversation as resolved.
Show resolved Hide resolved
prepare_for_fsdp: bool = False,
) -> Union[PreTrainedModel, 'PeftModel']:
"""Builds the inner model for the ComposerHFCausalLM.
Expand All @@ -207,7 +209,8 @@ def build_inner_model(
config_overrides (Dict[str, Any]): The configuration overrides.
load_in_8bit (bool): Whether to load in 8-bit.
pretrained (bool): Whether the model is pretrained.
prepare_for_fsdp (bool, optional): Whether to prepare the model for FSDP wrapping. Default: False.
model_cls (Union[AutoModel, PreTrainedModel]): HF class for models. Default: ``AutoModelForCausalLM``.
prepare_for_fsdp (bool, optional): Whether to prepare the model for FSDP wrapping. Default: ``False``.

Returns:
Union[PreTrainedModel, 'PeftModel']: The built inner model.
Expand All @@ -231,6 +234,11 @@ def build_inner_model(
+ 'Please `pip install llm-foundry[gpu]`.',
)

assert hasattr(
gupta-abhay marked this conversation as resolved.
Show resolved Hide resolved
model_cls,
'from_pretrained',
), 'HF Model class is not supported, check arguments to function call!'

# Hugging Face copies the modules into the
# transformers modules cache. On particular systems, this operation seems to cause contention between
# the different processes. To avoid this contention, we first create the config and generation config on local rank
Expand Down Expand Up @@ -280,7 +288,7 @@ def build_inner_model(
with init_empty_weights(include_buffers=False):
with warnings.catch_warnings():
warnings.simplefilter('ignore', UserWarning)
AutoModelForCausalLM.from_pretrained(
model_cls.from_pretrained(
pretrained_model_name_or_path,
trust_remote_code=trust_remote_code,
use_auth_token=use_auth_token,
Expand All @@ -290,7 +298,7 @@ def build_inner_model(
)
else:
with init_empty_weights(include_buffers=False):
AutoModelForCausalLM.from_config(
model_cls.from_config(
config,
trust_remote_code=trust_remote_code,
attn_implementation=requested_attention_implementation,
Expand All @@ -301,7 +309,7 @@ def build_inner_model(
# initialize the model on the correct device
if resolved_init_device == 'cpu':
if pretrained:
model = AutoModelForCausalLM.from_pretrained(
model = model_cls.from_pretrained(
pretrained_model_name_or_path,
trust_remote_code=trust_remote_code,
use_auth_token=use_auth_token,
Expand All @@ -310,7 +318,7 @@ def build_inner_model(
config=config,
)
else:
model = AutoModelForCausalLM.from_config(
model = model_cls.from_config(
config,
trust_remote_code=trust_remote_code,
attn_implementation=requested_attention_implementation,
Expand All @@ -321,7 +329,7 @@ def build_inner_model(
'Setting cfg.pretrained=True is not supported when init_device="meta".',
)
with init_empty_weights(include_buffers=False):
model = AutoModelForCausalLM.from_config(
model = model_cls.from_config(
config,
trust_remote_code=trust_remote_code,
attn_implementation=requested_attention_implementation,
Expand Down
Loading