You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[reland] Refactor TorchAOBaseTensor for better BC (#2793) (#2855)
Summary:
After this PR, tensors inheriting from TorchAOBaseTensor will have better support BC, that is if they add some optional tensor data attribute or optional non-tensor attribute, we will still have BC without any additional changes.
More Details: The BC story we are looking at is that, after we land some tensor, e.g. Int4Tensor, Float8Tensor, future changes should only add optional Tensor data attributes and optional non-Tensor attributes to the Tensor (other bigger changes will require a version bump, we need to add that too). The current TorchAOBaseTensor doesn’t support this very well.
also see #2840 for a real test that adds both an optional tensor and optional non-tensor attribute to Float8Tensor, and the BC test in https://github.com/pytorch/ao/blob/main/test/integration/test_load_and_run_checkpoint.py that tests Float8Tensor does not fail.
Docs for current TorchAOBaseTensor: https://github.com/pytorch/ao/blob/e6b38bb0e1477ae6aaca0a3d30de70598be43290/torchao/utils.py#L726-L731
`tensor_data_names` (List[str]): list of names of all requires tensor_data, order should match
the `__init__` list of tensor subclass
`optional_tensor_data_names` (List[str]): it's optional to define this field to have the additional boilerplate functions been implemented for you, but this will be need if there are some optional Tensor attributes, when defined, this will be a list of names of Tensors that can be optional
`tensor_attribute_names` (List[str]): list of names of non-Tensor attributes,
order should match the `__init__` list of tensor subclass, following all the `tensor_data_names` arguments and `optional_tensor_data_names`
Problems: current optional_tensor_data_names is not truly optional, since it is followed by tensor_attribute_names which contains both required and optional attributes. So if we add a tensor data attribute to Tensor, it will break BC.
Here are a few options:
```
class Int4Tensor(TorchAOBaseTensor):
tensor_data_names = ["qdata", "scale", "zero_point"]
optional_tensor_data_names = ["act_scale"]
tensor_attribute_names = ["block_size", "shape", "_demo_only_optional_attr"]
def __init__(self, qdata, scale, zero_point, act_scale=None, block_size=None, shape=None, _demo_only_optional_attr=None):
...
# for BC
def __setstate__(self, state):
torch._utils._set_obj_state(self, state)
if "act_scale" not in self.__dict__:
self.act_scale = None
```
```
class Int4Tensor(TorchAOBaseTensor):
tensor_data_names = ["qdata", "scale", "zero_point"]
optional_tensor_data_names = ["act_scale"]
required_tensor_attribute_names = ["block_size", "shape"]
optional_tensor_attribute_names = ["_demo_only_optional_attr"]
def __init__(self, qdata, scale, zero_point, block_size, shape, act_scale=None, _demo_only_optional_attr = None):
...
# for BC
def __setstate__(self, state):
torch._utils._set_obj_state(self, state)
if "act_scale" not in self.__dict__:
self.act_scale = None
```
```
class Int4Tensor(TorchAOBaseTensor):
tensor_data_names = ["qdata", "scale", "zero_point"]
tensor_attribute_names = ["block_size", "shape", "_demo_only_optional_attr"]
optional_tensor_data_names = ["act_scale"]
def __init__(self, qdata, scale, zero_point, block_size, shape, _demo_only_optional_attr = None, act_scale = None):
...
# for BC
def __setstate__(self, state):
torch._utils._set_obj_state(self, state)
if "act_scale" not in self.__dict__:
self.act_scale = None
```
Test Plan:
python test/integration/test_load_and_run_checkpoint.py
Reviewers:
Subscribers:
Tasks:
Tags:
0 commit comments