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)
| 199 | |
| 200 | |
| 201 | def _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 | |
| 232 | def _decompose_to_lora(diff: torch.Tensor, key: str, rank: int) -> Dict[str, torch.Tensor]: |
no test coverage detected