MCPcopy Create free account
hub / github.com/ModelTC/LightX2V / _compute_weight_diff

Function _compute_weight_diff

tools/extract/lora_extractor.py:201–229  ·  view source on GitHub ↗

Compute the difference between two weight tensors Args: source_tensor: Source weight tensor target_tensor: Target weight tensor key: Weight key name (for logging) Returns: Difference tensor, returns None if no change

(source_tensor: torch.Tensor, target_tensor: torch.Tensor, key: str)

Source from the content-addressed store, hash-verified

199
200
201def _compute_weight_diff(source_tensor: torch.Tensor, target_tensor: torch.Tensor, key: str) -> Optional[torch.Tensor]:
202 """
203 Compute the difference between two weight tensors
204
205 Args:
206 source_tensor: Source weight tensor
207 target_tensor: Target weight tensor
208 key: Weight key name (for logging)
209
210 Returns:
211 Difference tensor, returns None if no change
212 """
213 # Check if tensor shapes match
214 if source_tensor.shape != target_tensor.shape:
215 return None
216
217 # Check if tensor data types match
218 if source_tensor.dtype != target_tensor.dtype:
219 target_tensor = target_tensor.to(source_tensor.dtype)
220
221 # Compute difference
222 diff = target_tensor - source_tensor
223
224 # Check if there are actual changes
225 if torch.allclose(diff, torch.zeros_like(diff), atol=1e-8):
226 # No change
227 return None
228
229 return diff
230
231
232def _decompose_to_lora(diff: torch.Tensor, key: str, rank: int) -> Dict[str, torch.Tensor]:

Callers 1

extract_lora_from_diffFunction · 0.85

Calls 1

toMethod · 0.45

Tested by

no test coverage detected