You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] RuntimeError: Invalid device string: 'bfloat16' with transformers v4.40.1 and save_strategy="epoch" #404
It appears that during the model's saving process in fine-tuning, the self.config.torch_dtype was incorrectly set as the string "bfloat16" instead of torch.bfloat16. Here's a simple fix I implemented:
# Embed positionsifinputs_embedsisNone:
inputs_embeds=self.embed_tokens(input_ids)
# My Modificationifself.config.torch_dtype=="bfloat16":
self.config.torch_dtype=torch.bfloat16inputs_embeds=inputs_embeds.to(self.config.torch_dtype)
Reacted by Wentao Chen, It doesn't matter, Fangxu Yu and moontidef
@danielhanchen I'm having this same issue as of commit 976d11a. I get this issue every second time that I run my finetuning script. It alternates between running without issue and throwing this error each time.
I saw that this was solved previously by using if statements to check for the name of the dtype as a string and correcting it to the appropriate dtype object.
Why not use a mapping of dtype strings to their dtype objects?
DTYPE_MAP= { # probably good to set as a class attribute"float32": torch.float32,
torch.float32: torch.float32,
"float16": torch.float16,
torch.float16: torch.float16,
"bfloat16": torch.bfloat16,
torch.bfloat16: torch.bfloat16,
}
inputs_embeds=inputs_embeds.to(DTYPE_MAP[self.config.torch_dtype])
An enum might be a better solution:
fromenumimportEnumimporttorchclassDtypeMap(Enum):
float32: torch.dtype=torch.float32fp32: torch.dtype=float32float16: torch.dtype=torch.float16fp16: torch.dtype=float16bfloat16: torch.dtype=torch.bfloat16bf16: torch.dtype=bfloat16@classmethoddefget_dtype(cls, _v) ->torch.dtype:
ifisinstance(_v, str):
returncls[_v].valueelifisinstance(_v, torch.dtype):
return_velse:
raiseTypeError(f"{type(_v).__name__}")
DtypeMap.get_dtype(torch.bfloat16) ==torch.bfloat16# TrueDtypeMap.get_dtype("bfloat16") ==torch.bfloat16# TrueDtypeMap.get_dtype("bf16") ==torch.bfloat16# True; may be useful for `TrainingArguments`# List all non-aliased dtype names:
[dt.namefordtinDtypeMap] # ['float32', 'float16', 'bfloat16']# List all torch.dtype dtypes defined in the enum:
[dt.valuefordtinDtypeMap] # [torch.float32, torch.float16, torch.bfloat16]# `.to()` now works without if/elif/else branches cluttering methods and without mutating `config` stateinputs_embeds=inputs_embeds.to(DtypeMap.get_dtype(self.config.torch_dtype))
Activity
danielhanchen commentedon May 1, 2024
Oh thanks for that!! Will add your fix in! Thanks!
OAHC2022 commentedon May 6, 2024
Thank you!
johnsonice commentedon Jun 23, 2024
is this fixed ? still hitting the same bug
danielhanchen commentedon Jul 1, 2024
@johnsonice Could you try updating Unsloth as in https://github.com/unslothai/unsloth/wiki
fix: fix config.torch_dtype bug (#874)
chawins commentedon Aug 15, 2024
@danielhanchen I can confirm that I'm still experiencing this bug after updating.
SVHawk13 commentedon Sep 1, 2024
@danielhanchen I'm having this same issue as of commit 976d11a. I get this issue every second time that I run my finetuning script. It alternates between running without issue and throwing this error each time.
I saw that this was solved previously by using if statements to check for the name of the dtype as a string and correcting it to the appropriate dtype object.
Why not use a mapping of dtype strings to their dtype objects?
A naive approach could look like the following:
Original:
To this:
An enum might be a better solution:
danielhanchen commentedon Sep 2, 2024
I can add your mapping idea!
llllvvuu commentedon Sep 18, 2024
I hit this same issue just now, but on this line:
https://github.com/unslothai/unsloth/blob/f1951c0f6d3e1f184af93e5d8f5eff6e7834e4b5/unsloth/models/llama.py#L961C9-L961C52
I don't know if this is the best place to fix it, but I changed it to
logits = logits.to(__DTYPE_MAP[self.config.torch_dtype])and it worked.fix: fix config.torch_dtype bug (unslothai#874)