Model Utilities¶
Type Definitions¶
fusion_bench.utils.type
¶
Parameter Count and Manipulation¶
fusion_bench.utils.parameters
¶
check_parameters_all_equal(list_of_param_names)
¶
Checks if all models have the same parameters.
This function takes a list of parameter names or state dictionaries from different models. It checks if all models have the same parameters by comparing the parameter names. If any model has different parameters, it raises a ValueError with the differing parameters.
Parameters:
-
list_of_param_names
(List[Union[StateDict, List[str]]]
) –A list of parameter names or state dictionaries.
Raises:
-
ValueError
–If any model has different parameters.
Returns:
-
None
–None
Source code in fusion_bench/utils/parameters.py
count_parameters(module, non_zero_only=False)
¶
Counts the number of trainable and total parameters in a PyTorch model.
Parameters:
-
model
(Module
) –The PyTorch model for which to count parameters.
-
non_zero_only
(bool
, default:False
) –If True, only non-zero parameters are counted. If False, all parameters are counted. Defaults to False.
Returns:
-
tuple
(tuple[int, int]
) –A tuple containing the number of trainable parameters and the total number of parameters.
Examples:
Source code in fusion_bench/utils/parameters.py
get_parameter_statistics(module_or_state_dict, model_wise=False)
¶
Get statistics of the parameters in a PyTorch model or state dictionary.
Parameters:
-
module_or_state_dict
(Union[Module, StateDictType]
) –The PyTorch model for which to get parameter statistics.
Returns:
-
dict
(dict
) –A dictionary containing the mean, standard deviation, min, and max of the parameters.
Source code in fusion_bench/utils/parameters.py
get_parameter_summary(module_or_state_dict, non_zero_only=False)
¶
Get a summary of the parameters in a PyTorch model.
Source code in fusion_bench/utils/parameters.py
human_readable(num)
¶
Converts a number into a human-readable string with appropriate magnitude suffix.
Examples:
Parameters:
-
num
(int
) –The number to convert.
Returns:
-
str
(str
) –The human-readable string representation of the number.
Source code in fusion_bench/utils/parameters.py
print_parameters(module, is_human_readable=True, print_fn=print, non_zero_only=False)
¶
Prints the number of trainable and total parameters in a PyTorch model.
Parameters:
-
module
(Module
) –The PyTorch model for which to print parameters.
-
human_readable
(bool
) –If True, the parameter counts are converted to a human-readable format (e.g., '1.5M' instead of '1500000'). Defaults to True.
-
print_fn
(Callable
, default:print
) –Function used to print the message.
-
non_zero_only
(bool
, default:False
) –If True, only non-zero elements are counted. If False, all elements are counted. Defaults to False.
Prints
The number of trainable parameters, the total number of parameters, and the percentage of trainable parameters in the model.
Source code in fusion_bench/utils/parameters.py
state_dict_to_vector(state_dict, remove_keys=None)
¶
Convert a state dictionary to a vector.
Parameters:
-
state_dict
(Union[dict[str, Tensor], Module]
) –The state dictionary to convert.
-
remove_keys
(list
, default:None
) –List of keys to remove from the state dictionary. Defaults to [].
Returns:
-
–
torch.Tensor: The converted vector.
Source code in fusion_bench/utils/parameters.py
trainable_state_dict(module, prefix='', keep_vars=False)
¶
Returns the state dictionary of the module containing only the trainable parameters.
Parameters:
-
module
(Module
) –The neural network module.
-
prefix
(str
, default:''
) –The prefix to add to the parameter names. Defaults to "".
-
keep_vars
(bool
, default:False
) –If True, the parameters are not detached. Defaults to False.
Returns:
-
StateDictType
–Dict[str, Tensor]: A dictionary containing the names and values of the trainable parameters.
Source code in fusion_bench/utils/parameters.py
vector_to_state_dict(vector, state_dict, remove_keys=None)
¶
Convert a vector to a state dictionary.
Parameters:
-
vector
(Tensor
) –The vector to convert.
-
state_dict
(Union[dict[str, Tensor], Module]
) –The reference state dictionary to define the order of the vector.
-
remove_keys
(list
, default:None
) –List of keys to remove from the reference state dictionary. Defaults to [].
Returns:
-
dict
(Dict[str, Tensor]
) –The converted state dictionary.
Source code in fusion_bench/utils/parameters.py
State Dict Arithmetic¶
fusion_bench.utils.state_dict_arithmetic
¶
ArithmeticStateDict
¶
Bases: OrderedDict
An OrderedDict subclass that supports arithmetic operations on state dictionaries.
This class provides convenient operator overloading for common state dict operations like addition, subtraction, multiplication, and division, while maintaining all the functionality of OrderedDict.
Examples:
>>> sd1 = ArithmeticStateDict({'weight': torch.tensor([1.0, 2.0]), 'bias': torch.tensor([0.5])})
>>> sd2 = ArithmeticStateDict({'weight': torch.tensor([2.0, 3.0]), 'bias': torch.tensor([1.0])})
>>> result = sd1 + sd2 # Element-wise addition
>>> result = sd1 - sd2 # Element-wise subtraction
>>> result = sd1 * 2.0 # Scalar multiplication
>>> result = sd1 / 2.0 # Scalar division
>>> result = sd1 @ sd2 # Hadamard product
Source code in fusion_bench/utils/state_dict_arithmetic.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
|
__add__(other)
¶
Element-wise addition with another state dict or scalar.
Parameters:
-
other
(Union[ArithmeticStateDict, StateDictType, Number]
) –Another state dict to add or a scalar to add to all elements.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with the element-wise sum.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__iadd__(other)
¶
In-place addition.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__imul__(scalar)
¶
In-place multiplication.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__init__(*args, **kwargs)
¶
__ipow__(exponent)
¶
In-place power operation.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__isub__(other)
¶
In-place subtraction.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__itruediv__(scalar)
¶
In-place division.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__matmul__(other)
¶
Hadamard product (element-wise multiplication) using @ operator.
Parameters:
-
other
(Union[ArithmeticStateDict, StateDictType]
) –Another state dict for element-wise multiplication.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with the Hadamard product.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__mul__(scalar)
¶
Scalar multiplication or Hadamard product.
Parameters:
-
scalar
(Union[Number, ArithmeticStateDict, StateDictType]
) –A scalar value for element-wise multiplication, or another state dict for Hadamard product.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with the result.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__pow__(exponent)
¶
Element-wise power operation.
Parameters:
-
exponent
(Number
) –The exponent to raise each element to.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with each element raised to the power.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__radd__(other)
¶
Right addition (other + self). Handles the case where sum() starts with 0 and scalar addition.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__rmatmul__(other)
¶
Right matrix multiplication (other @ self).
__rmul__(scalar)
¶
__rsub__(other)
¶
Right subtraction (other - self).
Source code in fusion_bench/utils/state_dict_arithmetic.py
__sub__(other)
¶
Element-wise subtraction with another state dict or scalar.
Parameters:
-
other
(Union[ArithmeticStateDict, StateDictType, Number]
) –Another state dict to subtract or a scalar to subtract from all elements.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with the element-wise difference.
Source code in fusion_bench/utils/state_dict_arithmetic.py
__truediv__(scalar)
¶
Scalar division.
Parameters:
-
scalar
(Number
) –A scalar value to divide by.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with each element divided by scalar.
Raises:
-
ZeroDivisionError
–If scalar is zero.
-
TypeError
–If scalar is not a number.
Source code in fusion_bench/utils/state_dict_arithmetic.py
abs()
¶
Element-wise absolute value.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with absolute values.
Source code in fusion_bench/utils/state_dict_arithmetic.py
average(state_dicts)
classmethod
¶
Compute the average of multiple state dicts.
Parameters:
-
state_dicts
(List[Union[ArithmeticStateDict, StateDictType]]
) –List of state dicts to average.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with the average.
Source code in fusion_bench/utils/state_dict_arithmetic.py
clone()
¶
Create a deep copy with cloned tensors.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with cloned tensors.
Source code in fusion_bench/utils/state_dict_arithmetic.py
detach()
¶
Detach all tensors from the computation graph.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with detached tensors.
Source code in fusion_bench/utils/state_dict_arithmetic.py
from_state_dict(state_dict)
classmethod
¶
Create an ArithmeticStateDict from a regular state dict.
Parameters:
-
state_dict
(StateDictType
) –A regular state dictionary.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with the same data.
Source code in fusion_bench/utils/state_dict_arithmetic.py
num_params()
¶
Calculate the total number of parameters.
Returns:
-
int
–Total number of parameters in all tensors.
sqrt()
¶
Element-wise square root.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with square roots.
Source code in fusion_bench/utils/state_dict_arithmetic.py
sum()
¶
Sum with other ArithmeticStateDicts using the + operator.
Parameters:
-
*others
–Other ArithmeticStateDicts to sum with.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with the sum.
Source code in fusion_bench/utils/state_dict_arithmetic.py
to_device(device, copy=False, inplace=False)
¶
Move all tensors to the specified device.
Parameters:
-
device
(Union[device, str]
) –Target device.
-
copy
(bool
, default:False
) –Whether to force a copy.
-
inplace
(bool
, default:False
) –Whether to modify in place.
Returns:
-
ArithmeticStateDict
–ArithmeticStateDict with tensors on the target device.
Source code in fusion_bench/utils/state_dict_arithmetic.py
weighted_sum(state_dicts, weights)
classmethod
¶
Compute a weighted sum of multiple state dicts.
Parameters:
-
state_dicts
(List[Union[ArithmeticStateDict, StateDictType]]
) –List of state dicts to combine.
-
weights
(List[float]
) –List of weights for the combination.
Returns:
-
ArithmeticStateDict
–A new ArithmeticStateDict with the weighted sum.
Source code in fusion_bench/utils/state_dict_arithmetic.py
num_params_of_state_dict(state_dict)
¶
Calculate the total number of parameters in a state dict.
Parameters:
-
state_dict
(StateDictType
) –The state dict to count parameters in.
Returns:
-
int
–The total number of parameters in the state dict.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_add(a, b, strict=True, device=None, show_pbar=False)
¶
Compute the element-wise sum of two state dicts.
Parameters:
-
a
(StateDictType
) –The first state dict.
-
b
(StateDictType
) –The second state dict.
-
strict
(bool
, default:True
) –Whether to require exact key matching between state dicts.
-
device
(Optional[Union[device, str]]
, default:None
) –Optional device to move the result tensors to.
-
show_pbar
(bool
, default:False
) –Whether to show a progress bar during computation.
Returns:
-
StateDictType
–A state dict containing the element-wise sums.
Raises:
-
ValueError
–If strict=True and the state dicts have different parameters.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_add_scalar(state_dict, scalar)
¶
Add a scalar value to all parameters in a state dict.
Parameters:
-
state_dict
(StateDictType
) –The state dict to modify.
-
scalar
(Number
) –The scalar value to add to each parameter.
Returns:
-
StateDictType
–A new state dict with the scalar added to each parameter.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_avg(state_dicts)
¶
Calculate the element-wise average of a list of state dicts.
Parameters:
-
state_dicts
(List[StateDictType]
) –List of state dicts to average.
Returns:
-
StateDictType
–A state dict containing the averaged parameters.
Raises:
-
ValueError
–If the list is empty or state dicts have different keys.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_binary_mask(a, b, compare_fn='greater', strict=True, show_pbar=False)
¶
Create binary masks by comparing elements in two state dicts.
Parameters:
-
a
(StateDictType
) –The first state dict.
-
b
(StateDictType
) –The second state dict.
-
compare_fn
(Union[Literal['greater', 'less', 'equal', 'not_equal'], Callable[[Tensor, Tensor], BoolTensor]]
, default:'greater'
) –Comparison function to use. Can be a string literal ("greater", "less", "equal", "not_equal") or a callable that takes two tensors and returns a boolean tensor.
-
strict
(bool
, default:True
) –Whether to require exact key matching between state dicts.
-
show_pbar
(bool
, default:False
) –Whether to show a progress bar during computation.
Returns:
-
BoolStateDictType
–A dictionary containing boolean masks based on the comparison.
Raises:
-
ValueError
–If compare_fn is not a valid string or callable, or if strict=True and the state dicts have different keys or incompatible tensor shapes.
-
TypeError
–If tensors have incompatible types.
Source code in fusion_bench/utils/state_dict_arithmetic.py
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 |
|
state_dict_diff_abs(a, b)
¶
Compute the element-wise absolute difference between two state dicts.
Parameters:
-
a
(StateDictType
) –The first state dict.
-
b
(StateDictType
) –The second state dict.
Returns:
-
StateDictType
–A state dict containing the absolute differences.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_div(state_dict, scalar, show_pbar=False)
¶
Divide all parameters in a state dict by a scalar.
Parameters:
-
state_dict
(StateDictType
) –The state dict to divide.
-
scalar
(float
) –The scalar value to divide each parameter by.
-
show_pbar
(bool
, default:False
) –Whether to show a progress bar during computation.
Returns:
-
StateDictType
–A new state dict with each parameter divided by the scalar.
Raises:
-
ZeroDivisionError
–If scalar is zero.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_flatten(state_dict)
¶
Flatten all tensors in a state dict into a single 1D tensor.
Parameters:
-
state_dict
(StateDictType
) –The state dict to flatten.
Returns:
-
Tensor
–A single flattened tensor containing all parameters.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_hadamard_product(a, b)
¶
Compute the Hadamard product (element-wise multiplication) of two state dicts.
Parameters:
-
a
(StateDictType
) –The first state dict.
-
b
(StateDictType
) –The second state dict.
Returns:
-
StateDictType
–A state dict containing the element-wise products.
Raises:
-
ValueError
–If the state dicts have different keys or incompatible tensor shapes.
-
TypeError
–If tensors have incompatible types.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_interpolation(state_dicts, scalars)
¶
Interpolate between multiple state dicts using specified scalar weights.
Parameters:
-
state_dicts
(List[StateDictType]
) –List of state dicts to interpolate between.
-
scalars
(List[float]
) –List of scalar weights for interpolation.
Returns:
-
StateDictType
–A state dict containing the interpolated parameters.
Raises:
-
ValueError
–If the lists have different lengths or are empty, or if state dicts have different keys.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_mul(state_dict, scalar)
¶
Multiply all parameters in a state dict by a scalar.
Parameters:
-
state_dict
(StateDictType
) –The state dict to multiply.
-
scalar
(float
) –The scalar value to multiply each parameter by.
Returns:
-
StateDictType
–A new state dict with each parameter multiplied by the scalar.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_power(state_dict, p)
¶
Raise all parameters in a state dict to a power.
Parameters:
-
state_dict
(StateDictType
) –The state dict to raise to a power.
-
p
(float
) –The exponent to raise each parameter to.
Returns:
-
StateDictType
–A new state dict with each parameter raised to the power p.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_sub(a, b, strict=True, device=None)
¶
Compute the element-wise difference between two state dicts (a - b).
Parameters:
-
a
(StateDictType
) –The first state dict (minuend).
-
b
(StateDictType
) –The second state dict (subtrahend).
-
strict
(bool
, default:True
) –Whether to require exact key matching between state dicts.
-
device
(Optional[Union[device, str]]
, default:None
) –Optional device to move the result tensors to.
Returns:
-
StateDictType
–A state dict containing the element-wise differences.
Raises:
-
ValueError
–If strict=True and the state dicts have different keys or incompatible tensor shapes.
-
TypeError
–If tensors have incompatible types.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_sum(state_dicts)
¶
Compute the element-wise sum of multiple state dicts.
Parameters:
-
state_dicts
(List[StateDictType]
) –List of state dicts to sum.
Returns:
-
StateDictType
–A state dict containing the element-wise sums.
Raises:
-
ValueError
–If the list is empty or state dicts have different keys.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_to_device(state_dict, device, copy=False, inplace=False)
¶
Move state dict tensors to the specified device.
Parameters:
-
state_dict
(StateDictType
) –The state dictionary to move.
-
device
(Union[device, str]
) –Target device for the tensors.
-
copy
(bool
, default:False
) –Whether to force a copy even when the tensor is already on the target device.
-
inplace
(bool
, default:False
) –Whether to modify the input state dict in place.
Returns:
-
StateDictType
–State dict with tensors moved to the specified device.
Source code in fusion_bench/utils/state_dict_arithmetic.py
state_dict_weighted_sum(state_dicts, weights, device=None)
¶
Compute the weighted sum of multiple state dicts.
Parameters:
-
state_dicts
(List[StateDictType]
) –List of state dicts to combine.
-
weights
(List[float]
) –List of weights for the weighted sum.
-
device
(Optional[Union[device, str]]
, default:None
) –Optional device to move the result tensors to.
Returns:
-
StateDictType
–A state dict containing the weighted sum of parameters.
Raises:
-
ValueError
–If the lists have different lengths or are empty, or if state dicts have different keys.
Source code in fusion_bench/utils/state_dict_arithmetic.py
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
|
state_dicts_check_keys(state_dicts)
¶
Check that all state dictionaries have the same keys.
Parameters:
-
state_dicts
(List[StateDictType]
) –A list of state dictionaries to check.
Raises:
-
ValueError
–If the state dictionaries have different keys or the list is empty.
Source code in fusion_bench/utils/state_dict_arithmetic.py
Lazy Model Loading¶
fusion_bench.utils.lazy_state_dict.LazyStateDict
¶
Bases: Mapping[str, Tensor]
, Generic[TorchModelType]
A dictionary-like object that lazily loads tensors from model checkpoints.
Source code in fusion_bench/utils/lazy_state_dict.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
|
dtype
property
¶
torch.dtype
: The dtype of the module (assuming that all the module parameters have the same dtype).
__init__(checkpoint, meta_module_class=None, meta_module=None, cache_state_dict=False, torch_dtype=None, device='cpu', hf_revision=None, hf_cache_dir=None, hf_proxies=None)
¶
Initialize LazyStateDict with a checkpoint path.
Parameters:
-
checkpoint
(str
) –Path to the checkpoint file or directory.
-
meta_module_class
(Type[Module]
, default:None
) –Class of the meta module to instantiate.
-
meta_module
(Module
, default:None
) –Pre-initialized meta module.
-
cache_state_dict
(bool
, default:False
) –Whether to cache the state dict in memory.
-
torch_dtype
(dtype
, default:None
) –The dtype to use for the tensors.
-
device
(str
, default:'cpu'
) –The device to load the tensors onto.
-
hf_revision
(str
, default:None
) –The revision of the model to download from Hugging Face Hub.
-
hf_cache_dir
(str
, default:None
) –The cache directory for Hugging Face models.
-
hf_proxies
(Dict
, default:None
) –Proxies to use for downloading from Hugging Face Hub.
Source code in fusion_bench/utils/lazy_state_dict.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
__setitem__(key, value)
¶
Set a tensor in the LazyStateDict. This will update the state dict cache if it is enabled.
Source code in fusion_bench/utils/lazy_state_dict.py
load_state_dict(state_dict, strict=True)
¶
Load a state dict into this LazyStateDict. This method is only for compatibility with nn.Module and it overrides the cache of LazyStateDict.
Parameters:
-
state_dict
(Dict[str, Tensor]
) –The state dict to load.
-
strict
(bool
, default:True
) –Whether to enforce that all keys in the state dict are present in this LazyStateDict.
Source code in fusion_bench/utils/lazy_state_dict.py
state_dict(keep_vars=False)
¶
Parameters:
-
keep_vars
(bool
, default:False
) –Ignored, as LazyStateDict does not support keep_vars. Just for compatibility.