Skip to content
Open
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
17 changes: 10 additions & 7 deletions src/mistral_inference/lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@ def load_lora(self, lora_path: Union[Path, str], scaling: float = 2.0) -> None:

def _load_lora_state_dict(self, lora_state_dict: Dict[str, torch.Tensor], scaling: float = 2.0) -> None:
"""Loads LoRA state_dict"""
lora_dtypes = set([p.dtype for p in lora_state_dict.values()])
assert (
len(lora_dtypes) == 1
), f"LoRA weights have multiple different dtypes {lora_dtypes}. All weights need to have the same dtype"
lora_dtype = lora_dtypes.pop()
assert lora_dtype == self.dtype, f"LoRA weights dtype differs from model's dtype {lora_dtype} != {self.dtype}" # type: ignore[attr-defined]
lora_dtypes = set(p.dtype for p in lora_state_dict.values())
assert len(lora_dtypes) == 1, (
f"LoRA weights have multiple different dtypes {lora_dtypes}. "
"All weights need to have the same dtype"
)
lora_dtype = next(iter(lora_dtypes))
assert lora_dtype == self.dtype, (
f"LoRA weights dtype ({lora_dtype}) differs from model's dtype ({self.dtype})"
) # type: ignore[attr-defined]
assert all("lora" in key for key in lora_state_dict.keys())

# move tensors to device
Expand Down Expand Up @@ -152,4 +155,4 @@ def _load_lora_state_dict(self, lora_state_dict: Dict[str, torch.Tensor], scalin
self.pipeline_rank, # type: ignore[attr-defined]
)

self.load_state_dict(state_dict, strict=True) # type: ignore[attr-defined]
self.load_state_dict(state_dict, strict=True) # type: ignore[attr-defined]