File size: 1,270 Bytes
ac0560a eae79e1 ac0560a eae79e1 ac0560a eae79e1 61fce60 01e7f12 61fce60 80352c6 61fce60 f1d0f63 61fce60 |
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 |
```
model: single_linear
config: Int4WeightOnlyConfig, with preshuffled packing format
config version: 2
torchao version: 0.13.dev
```
```
import torch
import io
model = torch.nn.Sequential(torch.nn.Linear(32, 256, dtype=torch.bfloat16, device="cuda"))
from torchao.quantization import Int4WeightOnlyConfig, quantize_
quant_config = Int4WeightOnlyConfig(group_size=128, int4_packing_format="preshuffled", version=2)
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}-Int4WeightOnlyConfig-preshuffled-v2-0.13.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=True)
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,
)
``` |