Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
```
|
2 |
+
model: single_linear
|
3 |
+
config: Int8DynamicActivationIntxWeightConfig
|
4 |
+
config version: 1
|
5 |
+
torchao version: 0.14.dev
|
6 |
+
```
|
7 |
+
|
8 |
+
```
|
9 |
+
import torch
|
10 |
+
import io
|
11 |
+
|
12 |
+
model = torch.nn.Sequential(torch.nn.Linear(32, 256, dtype=torch.bfloat16, device="cuda"))
|
13 |
+
|
14 |
+
from torchao.quantization import Int8DynamicActivationIntxWeightConfig, quantize_
|
15 |
+
from torchao.quantization.granularity import PerGroup
|
16 |
+
|
17 |
+
version=1
|
18 |
+
|
19 |
+
quant_config = Int8DynamicActivationIntxWeightConfig(
|
20 |
+
weight_dtype=torch.int4,
|
21 |
+
weight_granularity=PerGroup(32),
|
22 |
+
version=version
|
23 |
+
)
|
24 |
+
quantize_(model, quant_config)
|
25 |
+
example_inputs = (torch.randn(2, 32, dtype=torch.bfloat16, device="cuda"),)
|
26 |
+
output = model(*example_inputs)
|
27 |
+
|
28 |
+
# Push to hub
|
29 |
+
USER_ID = "torchao-testing"
|
30 |
+
MODEL_NAME = "single-linear"
|
31 |
+
save_to = f"{USER_ID}/{MODEL_NAME}-Int8DynamicActivationIntxWeightConfig-v{version}-0.14.dev"
|
32 |
+
|
33 |
+
from huggingface_hub import HfApi
|
34 |
+
api = HfApi()
|
35 |
+
|
36 |
+
buf = io.BytesIO()
|
37 |
+
torch.save(model.state_dict(), buf)
|
38 |
+
api.create_repo(save_to, repo_type="model", exist_ok=False)
|
39 |
+
api.upload_file(
|
40 |
+
path_or_fileobj=buf,
|
41 |
+
path_in_repo="model.pt",
|
42 |
+
repo_id=save_to,
|
43 |
+
)
|
44 |
+
|
45 |
+
buf = io.BytesIO()
|
46 |
+
torch.save(example_inputs, buf)
|
47 |
+
api.upload_file(
|
48 |
+
path_or_fileobj=buf,
|
49 |
+
path_in_repo="model_inputs.pt",
|
50 |
+
repo_id=save_to,
|
51 |
+
)
|
52 |
+
|
53 |
+
buf = io.BytesIO()
|
54 |
+
torch.save(output, buf)
|
55 |
+
api.upload_file(
|
56 |
+
path_or_fileobj=buf,
|
57 |
+
path_in_repo="model_output.pt",
|
58 |
+
repo_id=save_to,
|
59 |
+
)
|
60 |
+
```
|