returns dict of tensors and strings to use in serialization via _save_to_state_dict() param: packed -- returns dict[str, torch.Tensor] for state_dict fit for safetensors saving
(self, packed: bool = False)
| 543 | return quant_state |
| 544 | |
| 545 | def as_dict(self, packed: bool = False) -> dict[str, Any]: |
| 546 | """ |
| 547 | returns dict of tensors and strings to use in serialization via _save_to_state_dict() |
| 548 | param: packed -- returns dict[str, torch.Tensor] for state_dict fit for safetensors saving |
| 549 | """ |
| 550 | qs_dict = { |
| 551 | "quant_type": self.quant_type, |
| 552 | "absmax": self.absmax, |
| 553 | "blocksize": self.blocksize, |
| 554 | "quant_map": self.code, |
| 555 | "dtype": str(self.dtype).strip("torch."), |
| 556 | "shape": tuple(self.shape) if self.shape is not None else None, |
| 557 | } |
| 558 | if self.nested: |
| 559 | qs_dict.update( |
| 560 | { |
| 561 | "nested_absmax": self.state2.absmax, |
| 562 | "nested_blocksize": self.state2.blocksize, |
| 563 | "nested_quant_map": self.state2.code.clone(), # un-shared to avoid restoring it after shared tensors are removed by safetensors |
| 564 | "nested_dtype": str(self.state2.dtype).strip("torch."), |
| 565 | "nested_offset": self.offset.item(), |
| 566 | }, |
| 567 | ) |
| 568 | if not packed or self.quant_type is None: |
| 569 | return qs_dict |
| 570 | |
| 571 | # packed format allows serialization of non-tensor components, critical for saving in safetensors format |
| 572 | qs_packed_dict = {k: v for k, v in qs_dict.items() if isinstance(v, torch.Tensor)} |
| 573 | non_tensor_dict = {k: v for k, v in qs_dict.items() if not isinstance(v, torch.Tensor)} |
| 574 | key = "quant_state.bitsandbytes__" |
| 575 | if self.quant_type is not None: |
| 576 | key += self.quant_type |
| 577 | qs_packed_dict[key] = pack_dict_to_tensor(non_tensor_dict) |
| 578 | return qs_packed_dict |
| 579 | |
| 580 | def to(self, device): |
| 581 | # make sure the quantization state is on the right device |