Pack a dictionary into a torch tensor for storing quant_state items in state_dict. Parameters: - source_dict: The dictionary to be packed. Returns: A torch tensor containing the packed data.
(source_dict)
| 164 | |
| 165 | |
| 166 | def pack_dict_to_tensor(source_dict): |
| 167 | """ |
| 168 | Pack a dictionary into a torch tensor for storing quant_state items in state_dict. |
| 169 | |
| 170 | Parameters: |
| 171 | - source_dict: The dictionary to be packed. |
| 172 | |
| 173 | Returns: |
| 174 | A torch tensor containing the packed data. |
| 175 | """ |
| 176 | json_str = json.dumps(source_dict) |
| 177 | json_bytes = json_str.encode("utf-8") |
| 178 | tensor_data = torch.tensor(list(json_bytes), dtype=torch.uint8) |
| 179 | |
| 180 | return tensor_data |
| 181 | |
| 182 | |
| 183 | def unpack_tensor_to_dict(tensor_data): |