Skip to content

Commit f8a708d

Browse files
committed
Fix serialization doc display
Summary: att Test Plan: checkout rendered doc Reviewers: Subscribers: Tasks: Tags:
1 parent c61aa44 commit f8a708d

File tree

2 files changed

+77
-80
lines changed

2 files changed

+77
-80
lines changed

docs/source/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ with more content coming soon.
7878
quantization
7979
sparsity
8080
performant_kernels
81-
serialization
8281

8382
.. toctree::
8483
:glob:
@@ -97,5 +96,7 @@ with more content coming soon.
9796
api_ref_intro
9897
api_ref_quantization
9998
api_ref_dtypes
99+
serialization
100+
100101
..
101102
api_ref_kernel

docs/source/serialization.rst

Lines changed: 75 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,97 @@ Serialization
33

44
Serialization and deserialization is an important question that people care about especially when we integrate torchao with other libraries. Here we want to describe how serialization and deserialization works for torchao optimized (quantized or sparsified) models.
55

6-
High level serialization and deserialization flow
7-
=================================================
8-
9-
```python
10-
import copy
11-
import tempfile
12-
import torch
13-
from torchao.quantization.quant_api import (
14-
quantize_,
15-
int4_weight_only,
16-
)
17-
18-
class ToyLinearModel(torch.nn.Module):
19-
def __init__(self, m=64, n=32, k=64):
20-
super().__init__()
21-
self.linear1 = torch.nn.Linear(m, n, bias=False)
22-
self.linear2 = torch.nn.Linear(n, k, bias=False)
23-
24-
def example_inputs(self, batch_size=1, dtype=torch.float32, device="cpu"):
25-
return (torch.randn(batch_size, self.linear1.in_features, dtype=dtype, device=device),)
6+
Serialization and deserialization flow
7+
======================================
8+
9+
Here is the serialization and deserialization flow::
10+
import copy
11+
import tempfile
12+
import torch
13+
from torchao.quantization.quant_api import (
14+
quantize_,
15+
int4_weight_only,
16+
)
17+
18+
class ToyLinearModel(torch.nn.Module):
19+
def __init__(self, m=64, n=32, k=64):
20+
super().__init__()
21+
self.linear1 = torch.nn.Linear(m, n, bias=False)
22+
self.linear2 = torch.nn.Linear(n, k, bias=False)
23+
24+
def example_inputs(self, batch_size=1, dtype=torch.float32, device="cpu"):
25+
return (torch.randn(batch_size, self.linear1.in_features, dtype=dtype, device=device),)
26+
27+
def forward(self, x):
28+
x = self.linear1(x)
29+
x = self.linear2(x)
30+
return x
31+
32+
dtype = torch.bfloat16
33+
m = ToyLinearModel(1024, 1024, 1024).eval().to(dtype).to("cuda")
34+
print(f"original model size: {get_model_size_in_bytes(m) / 1024 / 1024} MB")
35+
36+
example_inputs = m.example_inputs(dtype=dtype, device="cuda")
37+
quantize_(m, int4_weight_only())
38+
print(f"quantized model size: {get_model_size_in_bytes(m) / 1024 / 1024} MB")
39+
40+
ref = m(*example_inputs)
41+
with tempfile.NamedTemporaryFile() as f:
42+
torch.save(m.state_dict(), f)
43+
f.seek(0)
44+
state_dict = torch.load(f)
45+
46+
with torch.device("meta"):
47+
m_loaded = ToyLinearModel(1024, 1024, 1024).eval().to(dtype)
48+
49+
# `linear.weight` is nn.Parameter, so we check the type of `linear.weight.data`
50+
print(f"type of weight before loading: {type(m_loaded.linear1.weight.data), type(m_loaded.linear2.weight.data)}")
51+
m_loaded.load_state_dict(state_dict, assign=True)
52+
print(f"type of weight after loading: {type(m_loaded.linear1.weight), type(m_loaded.linear2.weight)}")
53+
54+
res = m_loaded(*example_inputs)
55+
assert torch.equal(res, ref)
2656

27-
def forward(self, x):
28-
x = self.linear1(x)
29-
x = self.linear2(x)
30-
return x
3157

32-
dtype = torch.bfloat16
33-
m = ToyLinearModel(1024, 1024, 1024).eval().to(dtype).to("cuda")
34-
print(f"original model size: {get_model_size_in_bytes(m) / 1024 / 1024} MB")
35-
36-
example_inputs = m.example_inputs(dtype=dtype, device="cuda")
37-
quantize_(m, int4_weight_only())
38-
print(f"quantized model size: {get_model_size_in_bytes(m) / 1024 / 1024} MB")
39-
40-
ref = m(*example_inputs)
41-
with tempfile.NamedTemporaryFile() as f:
42-
torch.save(m.state_dict(), f)
43-
f.seek(0)
44-
state_dict = torch.load(f)
58+
What happens when serializing an optimized model?
59+
=================================================
60+
To serialize an optimized model, we just need to call ``torch.save(m.state_dict(), f)``, because in torchao, we use tensor subclass to represent different dtypes or support different optimization techniques like quantization and sparsity. So after optimization, the only thing change is the weight Tensor is changed to an optimized weight Tensor, and the model structure is not changed at all. For example:
4561

46-
with torch.device("meta"):
47-
m_loaded = ToyLinearModel(1024, 1024, 1024).eval().to(dtype)
62+
original floating point model ``state_dict``::
63+
64+
{"linear1.weight": float_weight1, "linear2.weight": float_weight2}
4865

49-
# `linear.weight` is nn.Parameter, so we check the type of `linear.weight.data`
50-
print(f"type of weight before loading: {type(m_loaded.linear1.weight.data), type(m_loaded.linear2.weight.data)}")
51-
m_loaded.load_state_dict(state_dict, assign=True)
52-
print(f"type of weight after loading: {type(m_loaded.linear1.weight), type(m_loaded.linear2.weight)}")
66+
quantized model ``state_dict``::
5367

54-
res = m_loaded(*example_inputs)
55-
assert torch.equal(res, ref)
68+
{"linear1.weight": quantized_weight1, "linear2.weight": quantized_weight2, ...}
5669

57-
```
5870

59-
What happens when serializing an optimized model?
60-
=================================================
61-
To serialize an optimized model, we just need to call `torch.save(m.state_dict(), f)`, because in torchao, we use tensor subclass to represent different dtypes or support different optimization techniques like quantization and sparsity. So after optimization, the only thing change is the weight Tensor is changed to an optimized weight Tensor, and the model structure is not changed at all. For example:
71+
The size of the quantized model is typically going to be smaller to the original floating point model, but it also depends on the specific techinque and implementation you are using. You can print the model size with ``torchao.utils.get_model_size_in_bytes`` utility function, specifically for the above example using int4_weight_only quantization, we can see the size reduction is around 4x::
6272

63-
original floating point model `state_dict`:
64-
```
65-
{"linear1.weight": float_weight1, "linear2.weight": float_weight2}
66-
```
73+
original model size: 4.0 MB
74+
quantized model size: 1.0625 MB
6775

68-
quantized model `state_dict`:
69-
```
70-
{"linear1.weight": quantized_weight1, "linear2.weight": quantized_weight2, ...}
71-
```
76+
77+
What happens when deserializing an optimized model?
78+
===================================================
79+
To deserialize an optimized model, we can initialize the floating point model in `meta <https://pytorch.org/docs/stable/meta.html>`__ device and then load the optimized ``state_dict`` with ``assign=True`` using `model.load_state_dict <https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.load_state_dict>`__::
7280

73-
The size of the quantized model is typically going to be smaller to the original floating point model, but it also depends on the specific techinque and implementation you are using. You can print the model size with `torchao.utils.get_model_size_in_bytes` utility function, specifically for the above example using int4_weight_only quantization, we can see the size reduction is around 4x:
7481

75-
```
76-
original model size: 4.0 MB
77-
quantized model size: 1.0625 MB
78-
```
82+
with torch.device("meta"):
83+
m_loaded = ToyLinearModel(1024, 1024, 1024).eval().to(dtype)
7984

80-
What happens when deserializing an optimized model?
81-
===================================================
82-
To deserialize an optimized model, we can initialize the floating point model in `meta <https://pytorch.org/docs/stable/meta.html>`__ device and then load the optimized `state_dict` with `assign=True` using `model.load_state_dict <https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.load_state_dict>`__:
85+
print(f"type of weight before loading: {type(m_loaded.linear1.weight), type(m_loaded.linear2.weight)}")
86+
m_loaded.load_state_dict(state_dict, assign=True)
87+
print(f"type of weight after loading: {type(m_loaded.linear1.weight), type(m_loaded.linear2.weight)}")
8388

84-
```
85-
with torch.device("meta"):
86-
m_loaded = ToyLinearModel(1024, 1024, 1024).eval().to(dtype)
8789

88-
print(f"type of weight before loading: {type(m_loaded.linear1.weight), type(m_loaded.linear2.weight)}")
89-
m_loaded.load_state_dict(state_dict, assign=True)
90-
print(f"type of weight after loading: {type(m_loaded.linear1.weight), type(m_loaded.linear2.weight)}")
91-
```
90+
The reason we initialize the model in ``meta`` device is to avoid initializing the original floating point model since original floating point model may not fit into the device that we want to use for inference.
9291

93-
The reason we initialize the model in `meta` device is to avoid initializing the original floating point model since original floating point model may not fit into the device that we want to use for inference.
92+
What happens in ``m_loaded.load_state_dict(state_dict, assign=True)`` is that the corresponding weights (e.g. m_loaded.linear1.weight) are updated with the Tensors in ``state_dict``, which is an optimized tensor subclass instance (e.g. int4 ``AffineQuantizedTensor``). No dependency on torchao is needed for this to work.
9493

95-
What happens in `m_loaded.load_state_dict(state_dict, assign=True)` is that the corresponding weights (e.g. m_loaded.linear1.weight) are updated with the Tensors in `state_dict`, which is an optimized tensor subclass instance (e.g. int4 `AffineQuantizedTensor`). No dependency on torchao is needed for this to work.
94+
We can also verify that the weight is properly loaded by checking the type of weight tensor::
9695

97-
We can also verify that the weight is properly loaded by checking the type of weight tensor:
98-
```
99-
type of weight before loading: (<class 'torch.Tensor'>, <class 'torch.Tensor'>)
100-
type of weight after loading: (<class 'torchao.dtypes.affine_quantized_tensor.AffineQuantizedTensor'>, <class 'torchao.dtypes.affine_quantized_tensor.AffineQuantizedTensor'>)
96+
type of weight before loading: (<class 'torch.Tensor'>, <class 'torch.Tensor'>)
97+
type of weight after loading: (<class 'torchao.dtypes.affine_quantized_tensor.AffineQuantizedTensor'>, <class 'torchao.dtypes.affine_quantized_tensor.AffineQuantizedTensor'>)
10198

102-
```
10399

0 commit comments

Comments
 (0)