Update README.md
Browse files
README.md
CHANGED
|
@@ -1,199 +1,109 @@
|
|
| 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 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
-
|
| 113 |
-
[More Information Needed]
|
| 114 |
-
|
| 115 |
-
#### Factors
|
| 116 |
-
|
| 117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
-
|
| 119 |
-
[More Information Needed]
|
| 120 |
-
|
| 121 |
-
#### Metrics
|
| 122 |
-
|
| 123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
-
|
| 125 |
-
[More Information Needed]
|
| 126 |
-
|
| 127 |
-
### Results
|
| 128 |
-
|
| 129 |
-
[More Information Needed]
|
| 130 |
-
|
| 131 |
-
#### Summary
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
## Model Examination [optional]
|
| 136 |
-
|
| 137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
-
|
| 139 |
-
[More Information Needed]
|
| 140 |
-
|
| 141 |
-
## Environmental Impact
|
| 142 |
-
|
| 143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
-
|
| 145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
-
|
| 147 |
-
- **Hardware Type:** [More Information Needed]
|
| 148 |
-
- **Hours used:** [More Information Needed]
|
| 149 |
-
- **Cloud Provider:** [More Information Needed]
|
| 150 |
-
- **Compute Region:** [More Information Needed]
|
| 151 |
-
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
-
|
| 153 |
-
## Technical Specifications [optional]
|
| 154 |
-
|
| 155 |
-
### Model Architecture and Objective
|
| 156 |
-
|
| 157 |
-
[More Information Needed]
|
| 158 |
-
|
| 159 |
-
### Compute Infrastructure
|
| 160 |
-
|
| 161 |
-
[More Information Needed]
|
| 162 |
-
|
| 163 |
-
#### Hardware
|
| 164 |
-
|
| 165 |
-
[More Information Needed]
|
| 166 |
-
|
| 167 |
-
#### Software
|
| 168 |
-
|
| 169 |
-
[More Information Needed]
|
| 170 |
-
|
| 171 |
-
## Citation [optional]
|
| 172 |
-
|
| 173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
-
|
| 175 |
-
**BibTeX:**
|
| 176 |
-
|
| 177 |
-
[More Information Needed]
|
| 178 |
-
|
| 179 |
-
**APA:**
|
| 180 |
-
|
| 181 |
-
[More Information Needed]
|
| 182 |
-
|
| 183 |
-
## Glossary [optional]
|
| 184 |
-
|
| 185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
-
|
| 187 |
-
[More Information Needed]
|
| 188 |
-
|
| 189 |
-
## More Information [optional]
|
| 190 |
-
|
| 191 |
-
[More Information Needed]
|
| 192 |
-
|
| 193 |
-
## Model Card Authors [optional]
|
| 194 |
-
|
| 195 |
-
[More Information Needed]
|
| 196 |
-
|
| 197 |
-
## Model Card Contact
|
| 198 |
-
|
| 199 |
-
[More Information Needed]
|
|
|
|
| 1 |
+
```
|
| 2 |
+
model: opt-125m
|
| 3 |
+
config: ModuleFqnToConfig
|
| 4 |
+
with Float8DynamicActivationFloat8WeightConfig, Int4WeightOnlyConfig and IntxWeightOnlyConfig
|
| 5 |
+
config version: 1
|
| 6 |
+
torchao version: 0.14.dev
|
| 7 |
+
```
|
| 8 |
+
|
| 9 |
+
```
|
| 10 |
+
import logging
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
|
| 14 |
+
|
| 15 |
+
# Configure logging to see warnings and debug information
|
| 16 |
+
logging.basicConfig(
|
| 17 |
+
level=logging.INFO, format="%(name)s - %(levelname)s - %(message)s"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Enable specific loggers that might contain the serialization warnings
|
| 21 |
+
logging.getLogger("transformers").setLevel(logging.INFO)
|
| 22 |
+
logging.getLogger("torchao").setLevel(logging.INFO)
|
| 23 |
+
logging.getLogger("safetensors").setLevel(logging.INFO)
|
| 24 |
+
logging.getLogger("huggingface_hub").setLevel(logging.INFO)
|
| 25 |
+
|
| 26 |
+
model_id = "facebook/opt-125m"
|
| 27 |
+
|
| 28 |
+
from torchao.quantization import (
|
| 29 |
+
Float8DynamicActivationFloat8WeightConfig,
|
| 30 |
+
Int4WeightOnlyConfig,
|
| 31 |
+
IntxWeightOnlyConfig,
|
| 32 |
+
PerRow,
|
| 33 |
+
PerAxis,
|
| 34 |
+
ModuleFqnToConfig,
|
| 35 |
+
Float8Tensor,
|
| 36 |
+
Int4TilePackedTo4dTensor,
|
| 37 |
+
IntxUnpackedToInt8Tensor,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
float8dyn = Float8DynamicActivationFloat8WeightConfig(granularity=PerRow())
|
| 41 |
+
int4wo = Int4WeightOnlyConfig(int4_packing_format="tile_packed_to_4d")
|
| 42 |
+
intxwo = IntxWeightOnlyConfig(weight_dtype=torch.int8, granularity=PerAxis(0))
|
| 43 |
+
|
| 44 |
+
qconfig_dict = {
|
| 45 |
+
# highest priority
|
| 46 |
+
"model.decoder.layers.3.self_attn.q_proj": int4wo,
|
| 47 |
+
"model.decoder.layers.*.self_attn.q_proj": float8dyn,
|
| 48 |
+
"model.decoder.layers.*.self_attn.k_proj": float8dyn,
|
| 49 |
+
"model.decoder.layers.*.self_attn.v_proj": None,
|
| 50 |
+
"_default": intxwo,
|
| 51 |
+
}
|
| 52 |
+
quant_config = ModuleFqnToConfig(qconfig_dict)
|
| 53 |
+
quantization_config = TorchAoConfig(quant_type=quant_config)
|
| 54 |
+
quantized_model = AutoModelForCausalLM.from_pretrained(
|
| 55 |
+
model_id,
|
| 56 |
+
device_map="auto",
|
| 57 |
+
torch_dtype=torch.bfloat16,
|
| 58 |
+
quantization_config=quantization_config,
|
| 59 |
+
)
|
| 60 |
+
print("quantized model:", quantized_model)
|
| 61 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 62 |
+
for i in range(12):
|
| 63 |
+
if i == 3:
|
| 64 |
+
print("type:", quantized_model.model.decoder.layers[i].self_attn.q_proj.weight)
|
| 65 |
+
assert isinstance(quantized_model.model.decoder.layers[i].self_attn.q_proj.weight, Int4TilePackedTo4dTensor)
|
| 66 |
+
else:
|
| 67 |
+
assert isinstance(quantized_model.model.decoder.layers[i].self_attn.q_proj.weight, Float8Tensor)
|
| 68 |
+
assert isinstance(quantized_model.model.decoder.layers[i].self_attn.k_proj.weight, Float8Tensor)
|
| 69 |
+
assert not isinstance(quantized_model.model.decoder.layers[i].self_attn.v_proj.weight, Float8Tensor)
|
| 70 |
+
assert isinstance(quantized_model.model.decoder.layers[i].self_attn.out_proj.weight, IntxUnpackedToInt8Tensor)
|
| 71 |
+
|
| 72 |
+
# Push to hub
|
| 73 |
+
MODEL_NAME = model_id.split("/")[-1]
|
| 74 |
+
save_to = f"torchao-testing/{MODEL_NAME}-ModuleFqnToConfig-v1-regex-0.14.0.dev"
|
| 75 |
+
quantized_model.push_to_hub(save_to, safe_serialization=False)
|
| 76 |
+
tokenizer.push_to_hub(save_to)
|
| 77 |
+
|
| 78 |
+
# Manual Testing
|
| 79 |
+
prompt = "What are we having for dinner?"
|
| 80 |
+
print("Prompt:", prompt)
|
| 81 |
+
inputs = tokenizer(
|
| 82 |
+
prompt,
|
| 83 |
+
return_tensors="pt",
|
| 84 |
+
).to("cuda")
|
| 85 |
+
# setting temperature to 0 to make sure result deterministic
|
| 86 |
+
generated_ids = quantized_model.generate(**inputs, max_new_tokens=128, temperature=0)
|
| 87 |
+
|
| 88 |
+
correct_output_text = tokenizer.batch_decode(
|
| 89 |
+
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 90 |
+
)
|
| 91 |
+
print("Response:", correct_output_text[0][len(prompt) :])
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# # # Load model from saved checkpoint
|
| 95 |
+
reloaded_model = AutoModelForCausalLM.from_pretrained(
|
| 96 |
+
save_to,
|
| 97 |
+
device_map="cuda:0",
|
| 98 |
+
torch_dtype=torch.bfloat16,
|
| 99 |
+
# quantization_config=quantization_config,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
generated_ids = reloaded_model.generate(**inputs, max_new_tokens=128, temperature=0)
|
| 103 |
+
output_text = tokenizer.batch_decode(
|
| 104 |
+
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 105 |
+
)
|
| 106 |
+
print("Response:", output_text[0][len(prompt) :])
|
| 107 |
+
|
| 108 |
+
assert(correct_output_text == output_text)
|
| 109 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|