(model, compute_dtype, state_dict, prefix="", patches=None, scale_weights=None, compile_args=None, modules_to_not_convert=[])
| 42 | |
| 43 | #based on https://github.com/huggingface/diffusers/blob/main/src/diffusers/quantizers/gguf/utils.py |
| 44 | def _replace_linear(model, compute_dtype, state_dict, prefix="", patches=None, scale_weights=None, compile_args=None, modules_to_not_convert=[]): |
| 45 | |
| 46 | has_children = list(model.children()) |
| 47 | if not has_children: |
| 48 | return |
| 49 | |
| 50 | allow_compile = False |
| 51 | |
| 52 | for name, module in model.named_children(): |
| 53 | if compile_args is not None: |
| 54 | allow_compile = compile_args.get("allow_unmerged_lora_compile", False) |
| 55 | module_prefix = prefix + name + "." |
| 56 | module_prefix = module_prefix.replace("_orig_mod.", "") |
| 57 | _replace_linear(module, compute_dtype, state_dict, module_prefix, patches, scale_weights, compile_args, modules_to_not_convert) |
| 58 | |
| 59 | if isinstance(module, nn.Linear) and "loras" not in module_prefix and "dual_controller" not in module_prefix and name not in modules_to_not_convert: |
| 60 | weight_key = module_prefix + "weight" |
| 61 | if weight_key not in state_dict: |
| 62 | continue |
| 63 | |
| 64 | in_features = state_dict[weight_key].shape[1] |
| 65 | out_features = state_dict[weight_key].shape[0] |
| 66 | |
| 67 | is_gguf = isinstance(state_dict[weight_key], GGUFParameter) |
| 68 | |
| 69 | scale_weight = None |
| 70 | if not is_gguf and scale_weights is not None: |
| 71 | scale_key = f"{module_prefix}scale_weight" |
| 72 | scale_weight = scale_weights.get(scale_key) |
| 73 | |
| 74 | with init_empty_weights(): |
| 75 | model._modules[name] = CustomLinear( |
| 76 | in_features, |
| 77 | out_features, |
| 78 | module.bias is not None, |
| 79 | compute_dtype=compute_dtype, |
| 80 | scale_weight=scale_weight, |
| 81 | allow_compile=allow_compile, |
| 82 | is_gguf=is_gguf |
| 83 | ) |
| 84 | model._modules[name].source_cls = type(module) |
| 85 | model._modules[name].requires_grad_(False) |
| 86 | |
| 87 | return model |
| 88 | |
| 89 | def set_lora_params(module, patches, module_prefix="", device=torch.device("cpu")): |
| 90 | remove_lora_from_module(module) |
no test coverage detected