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