MCPcopy Create free account
hub / github.com/tdrussell/diffusion-pipe / update_parameter

Function update_parameter

optimizers/optimizer_utils.py:93–118  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

91
92
93def 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
121def get_format_params(dtype: torch.dtype) -> tuple[int, int]:

Callers 1

copy_stochasticFunction · 0.85

Calls 2

quantize_tensorFunction · 0.85
toMethod · 0.45

Tested by

no test coverage detected