MCPcopy Create free account
hub / github.com/kijai/ComfyUI-WanVideoWrapper / _replace_linear

Function _replace_linear

custom_linear.py:44–87  ·  view source on GitHub ↗
(model, compute_dtype, state_dict, prefix="", patches=None, scale_weights=None, compile_args=None, modules_to_not_convert=[])

Source from the content-addressed store, hash-verified

42
43#based on https://github.com/huggingface/diffusers/blob/main/src/diffusers/quantizers/gguf/utils.py
44def _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
89def set_lora_params(module, patches, module_prefix="", device=torch.device("cpu")):
90 remove_lora_from_module(module)

Callers 4

loadmodelMethod · 0.85
processMethod · 0.85
processMethod · 0.85

Calls 2

CustomLinearClass · 0.85
getMethod · 0.45

Tested by

no test coverage detected