-
Notifications
You must be signed in to change notification settings - Fork 176
Support DeepSeekV3-style block FP8 quantization #1607
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
451219a
a21ff60
3bfbcd4
deb4e9b
02615d9
674b76e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from transformers import AutoModelForCausalLM, AutoTokenizer | ||
|
||
from llmcompressor import oneshot | ||
from llmcompressor.modifiers.quantization import QuantizationModifier | ||
|
||
MODEL_ID = "Qwen/Qwen3-0.6B" | ||
|
||
# Load model. | ||
model = AutoModelForCausalLM.from_pretrained( | ||
MODEL_ID, device_map="auto", torch_dtype="auto" | ||
) | ||
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | ||
|
||
# Configure the quantization algorithm and scheme. | ||
# In this case, we: | ||
# * quantize the weights to fp8 with per channel via ptq | ||
# * quantize the activations to fp8 with dynamic per token | ||
mgoin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
recipe = QuantizationModifier( | ||
targets="Linear", scheme="FP8_BLOCK", ignore=["lm_head"] | ||
) | ||
|
||
# Apply quantization. | ||
oneshot(model=model, recipe=recipe) | ||
|
||
# Confirm generations of the quantized model look sane. | ||
print("========== SAMPLE GENERATION ==============") | ||
input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to("cuda") | ||
mgoin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
output = model.generate(input_ids, max_new_tokens=20) | ||
print(tokenizer.decode(output[0])) | ||
print("==========================================") | ||
|
||
# Save to disk in compressed-tensors format. | ||
SAVE_DIR = MODEL_ID.split("/")[1] + "-FP8-BLOCK" | ||
model.save_pretrained(SAVE_DIR) | ||
tokenizer.save_pretrained(SAVE_DIR) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,8 +109,25 @@ def call_observer( | |
updated_scale, updated_zero_point = observer( | ||
value, g_idx=g_idx, global_scale=global_scale | ||
) | ||
update_parameter_data(module, updated_scale, f"{base_name}_scale") | ||
update_parameter_data(module, updated_zero_point, f"{base_name}_zero_point") | ||
# register or update scale & zero_point parameters (supports block shapes) | ||
scale_name = f"{base_name}_scale" | ||
zp_name = f"{base_name}_zero_point" | ||
if not hasattr(module, scale_name) or getattr(module, scale_name).shape != updated_scale.shape: | ||
if hasattr(module, scale_name): | ||
delattr(module, scale_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kylesayrs do we need this and L140 to be |
||
module.register_parameter( | ||
scale_name, torch.nn.Parameter(updated_scale.clone()) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kylesayrs do we need these (this and L141-143) to use |
||
else: | ||
update_parameter_data(module, updated_scale, scale_name) | ||
if not hasattr(module, zp_name) or getattr(module, zp_name).shape != updated_zero_point.shape: | ||
if hasattr(module, zp_name): | ||
delattr(module, zp_name) | ||
module.register_parameter( | ||
zp_name, torch.nn.Parameter(updated_zero_point.clone()) | ||
) | ||
else: | ||
update_parameter_data(module, updated_zero_point, zp_name) | ||
mgoin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def update_weight_global_scale(module: Module): | ||
|
Uh oh!
There was an error while loading. Please reload this page.