Skip to content

Commit

Permalink
Avoid HF race condition (#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
dakinggg authored Jul 4, 2024
1 parent f0535dc commit 59231d3
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions llmfoundry/models/hf/hf_causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,26 @@ def _autoset_attn_implementation_monkeypatch(
if dist.get_local_rank() != 0 and init_device == 'mixed':
pretrained = False

# If the HuggingFace model is coming from a local folder, Hugging Face copies the modules into the
# 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 model (on meta device) on local rank
# zero. This will set up the transformers model cache and avoid the future contention.
if dist.get_local_rank(
) == 0 and os.path.isdir(pretrained_model_name_or_path):
with init_empty_weights(include_buffers=False):
with warnings.catch_warnings():
warnings.simplefilter('ignore', UserWarning)
AutoModelForCausalLM.from_pretrained(
pretrained_model_name_or_path,
if dist.get_local_rank() == 0:
if os.path.isdir(pretrained_model_name_or_path):
with init_empty_weights(include_buffers=False):
with warnings.catch_warnings():
warnings.simplefilter('ignore', UserWarning)
AutoModelForCausalLM.from_pretrained(
pretrained_model_name_or_path,
trust_remote_code=trust_remote_code,
use_auth_token=use_auth_token,
config=config,
)
else:
with init_empty_weights(include_buffers=False):
AutoModelForCausalLM.from_config(
config,
trust_remote_code=trust_remote_code,
use_auth_token=use_auth_token,
config=config,
)

dist.barrier()
Expand Down

0 comments on commit 59231d3

Please sign in to comment.