Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[inference] add smoothquant llama #4861

Merged
merged 7 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
53 changes: 53 additions & 0 deletions colossalai/inference/quant/smoothquant/calibration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Adapted from AutoGPTQ: https://github.com/PanQiWei/AutoGPTQ

import functools

import torch
import torch.nn as nn
from datasets import load_dataset
from tqdm import tqdm


def get_act_scales(model, tokenizer, dataset_path, num_samples=512, seq_len=512):
model.eval()
device = next(model.parameters()).device
act_scales = {}

def stat_tensor(name, tensor):
hidden_dim = tensor.shape[-1]
tensor = tensor.view(-1, hidden_dim).abs().detach()
comming_max = torch.max(tensor, dim=0)[0].float().cpu()
if name in act_scales:
act_scales[name] = torch.max(act_scales[name], comming_max)
else:
act_scales[name] = comming_max

def stat_input_hook(m, x, y, name):
if isinstance(x, tuple):
x = x[0]
stat_tensor(name, x)

hooks = []
for name, m in model.named_modules():
if isinstance(m, nn.Linear):
hooks.append(m.register_forward_hook(functools.partial(stat_input_hook, name=name)))

dataset = load_dataset("json", data_files=dataset_path)

print("text", dataset["train"]["rows"][0][1]["row"]["text"])

dataset = dataset.shuffle(seed=42)

for i in tqdm(range(num_samples)):
input_ids = tokenizer(
dataset["train"]["rows"][0][i]["row"]["text"],
return_tensors="pt",
max_length=seq_len,
truncation=True,
).input_ids.to(device)
model(input_ids)

for h in hooks:
h.remove()

return act_scales
Loading
Loading