Updates a parameter tensor, handling both regular torch.Tensor and QBytesTensor cases with proper rescaling for quantized tensors. Args: target: The parameter to update (either torch.Tensor or QBytesTensor) result_float: The new values to assign (torch.Tensor)
(target, result_float)
| 91 | |
| 92 | |
| 93 | def update_parameter(target, result_float): |
| 94 | """ |
| 95 | Updates a parameter tensor, handling both regular torch.Tensor and QBytesTensor cases |
| 96 | with proper rescaling for quantized tensors. |
| 97 | |
| 98 | Args: |
| 99 | target: The parameter to update (either torch.Tensor or QBytesTensor) |
| 100 | result_float: The new values to assign (torch.Tensor) |
| 101 | """ |
| 102 | if isinstance(target, QBytesTensor): |
| 103 | # Get the target dtype from the existing quantized tensor |
| 104 | target_dtype = target._data.dtype |
| 105 | |
| 106 | # Handle device placement |
| 107 | device = target._data.device |
| 108 | result_float = result_float.to(device) |
| 109 | |
| 110 | # Compute new quantized values and scale |
| 111 | quantized_data, new_scale = quantize_tensor(result_float, target_dtype) |
| 112 | |
| 113 | # Update the internal tensors with newly computed values |
| 114 | target._data.copy_(quantized_data) |
| 115 | target._scale.copy_(new_scale) |
| 116 | else: |
| 117 | # Regular tensor update |
| 118 | target.copy_(result_float) |
| 119 | |
| 120 | |
| 121 | def get_format_params(dtype: torch.dtype) -> tuple[int, int]: |
no test coverage detected