Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
37 changes: 37 additions & 0 deletions examples/quantization_lutb/llama3_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from compressed_tensors.offload import dispatch_model
from transformers import AutoModelForCausalLM, AutoTokenizer

from llmcompressor import oneshot
from llmcompressor.modifiers.quantization import QuantizationModifier

MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct"

# Load model.
model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)

# Configure the quantization algorithm and scheme.
# In this case, we:
# * quantize only the mlp layer weights
recipe = QuantizationModifier(
targets="re:.*layers.*mlp.*_proj$", scheme="LUTB", ignore=["lm_head"]
)

# Apply quantization.
oneshot(model=model, recipe=recipe)

print("\n\n")
print("========== SAMPLE GENERATION ==============")
dispatch_model(model)
input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to(
model.device
)
output = model.generate(input_ids, max_new_tokens=100)
print(tokenizer.decode(output[0]))
print("==========================================\n\n")


# Save to disk in compressed-tensors format.
SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-LUTB"
model.save_pretrained(SAVE_DIR, save_compressed=True)
tokenizer.save_pretrained(SAVE_DIR)
Comment thread
brian-dellabetta marked this conversation as resolved.
2 changes: 1 addition & 1 deletion src/llmcompressor/modifiers/quantization/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def initialize_observer(
log_once=True,
)

if args is not None and args.dynamic is not True:
if args is not None and args.dynamic is not True and observer is not None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The check args.dynamic is not True does not account for other dynamic quantization types such as DynamicType.LOCAL (which is checked on line 149). If args.dynamic is DynamicType.LOCAL, this check evaluates to True, which incorrectly initializes a static observer for dynamic quantization.

Additionally, note that if args can be None (as implied by the args is not None check), the function would have already raised an AttributeError on line 62 (observer = args.observer) and line 65 (args.observer). It is highly recommended to add an early return if args is None: at the beginning of the function to prevent this potential crash.

Suggested change
if args is not None and args.dynamic is not True and observer is not None:
if args is not None and args.dynamic not in (True, DynamicType.LOCAL) and observer is not None:

observer = Observer.load_from_registry(observer, base_name=base_name, args=args)
module.register_module(f"{base_name}_observer", observer)
observer.attach(module)
Expand Down
Loading