Skip to content

Commit

Permalink
Added support for SFTTrainer checkpoint models and adapter models con…
Browse files Browse the repository at this point in the history
…taining one or more non-LoRA weights

My initial commit was more like a brute force.
The edits suggested by @FirstTimeEZ reduces the complexity.
  • Loading branch information
Victoran0 authored Oct 8, 2024
1 parent c6396aa commit c2c2626
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions convert_lora_to_gguf.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,8 @@ def generate_extra_tensors(self) -> Iterable[tuple[str, Tensor]]:
def get_tensors(self) -> Iterator[tuple[str, Tensor]]:
tensor_map: dict[str, PartialLoraTensor] = {}

# The following edits will enable conversion for: SFTTrainer checkpoint adapter models and other adapter models that contain weights besides LoRA weights

# Here, we first get the items with the 'lora_' substring
lora_model_items_name = [name for name,_ in lora_model.items()]
lora_model_items_with_lora_tensors = [name for name in lora_model_items_name if 'lora_' in name]

for name, tensor in lora_model.items():

# Check for only LoRA finetuned weights and base layer weights
if (name in lora_model_items_with_lora_tensors) or (".base_layer.weight" in name):
if ("lora_" in name) or (".base_layer.weight" in name):
if self.lazy:
tensor = LazyTorchTensor.from_eager(tensor)
base_name = get_base_tensor_name(name)
Expand All @@ -356,11 +348,7 @@ def get_tensors(self) -> Iterator[tuple[str, Tensor]]:
if not is_lora_a and not is_lora_b:
if ".base_layer.weight" in name:
continue

# we will either have a lora weight or a base layer weight, this error becomes trivial
# logger.error(f"Unexpected name '{name}': Not a lora_A or lora_B tensor")
# sys.exit(1)


if base_name in tensor_map:
if is_lora_a:
tensor_map[base_name].A = tensor
Expand All @@ -371,6 +359,8 @@ def get_tensors(self) -> Iterator[tuple[str, Tensor]]:
tensor_map[base_name] = PartialLoraTensor(A=tensor)
else:
tensor_map[base_name] = PartialLoraTensor(B=tensor)
else:
pass

for name, tensor in tensor_map.items():
assert tensor.A is not None
Expand Down

0 comments on commit c2c2626

Please sign in to comment.