File size: 1,201 Bytes
b40ac55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
```python
model_id = "mgoin/starcoderbase-1b-pruned50-quant"

# Load model with SparseAutoModel
from sparseml.transformers.utils import SparseAutoModel
from transformers import AutoConfig

config = AutoConfig.from_pretrained(model_id)  # Why does SparseAutoModel need config?
model = SparseAutoModel.text_generation_from_pretrained(model_id, config=config)

# Apply recipe to model
# Note: Really annoying we can't grab the recipe.yaml present in the uploaded model
# and you need this separate apply_recipe_structure_to_model function
from sparseml.pytorch.model_load.helpers import apply_recipe_structure_to_model
from huggingface_hub import hf_hub_download
import os

recipe_path = hf_hub_download(repo_id=model_id, filename="recipe.yaml")
apply_recipe_structure_to_model(
    model=model, recipe_path=recipe_path, model_path=os.path.dirname(recipe_path)
)

# Regular HF inference
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(model_id)
inputs = tokenizer.encode("def print_hello_world():", return_tensors="pt")
outputs = model.generate(inputs)
print(tokenizer.decode(outputs[0]))
"""
def print_hello_world():
    print("Hello World!")

print_hello_world
"""
```