Skip to content

How to perform real quantization model inference with SmoothQuant? #106

Description

@wanto-rest

I want to know how to perform inference. I have made several attempts, including using the Transformers framework, but there are always issues.
here is my code. i always meet problem like 'Int8OPTDecoder' object has no attribute 'dropout'...if i change the version of transformers ,missing attribute will also change.
import torch
from transformers import AutoTokenizer
from smoothquant.opt import Int8OPTForCausalLM
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

def load_pretrained_int8_model():
# 官方提供的INT8模型
model_name = "/data/zyh/models/mit-han-lab/opt-125m-smoothquant"
tokenizer = AutoTokenizer.from_pretrained("/data/zyh/models/opt-125m")

# 直接加载INT8模型
model = Int8OPTForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.float16,
    device_map="auto"
)

return model, tokenizer

def int8_inference_demo():
model, tokenizer = load_pretrained_int8_model()

# 推理
prompt = "The future of artificial intelligence is"
inputs = tokenizer(prompt, return_tensors="pt")
input_ids = inputs.input_ids.cuda()

with torch.no_grad():
    outputs = model.generate(
        input_ids,
        max_new_tokens=50,
        temperature=0.7,
        do_sample=True,
        pad_token_id=tokenizer.eos_token_id
    )

generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Generated: {generated_text}")

return generated_text

if name == "main":
int8_inference_demo()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions