-
Notifications
You must be signed in to change notification settings - Fork 1
Dev i #4
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?
Dev i #4
Changes from all commits
7b67045
23087ce
55dafc4
12ab1b1
a278a81
ab51b7f
a268d2e
7c54f6a
e7452e2
b48cc49
f056558
f950fb7
cf9843b
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,133 @@ | ||||||||||||
| import torch | ||||||||||||
| from datasets import load_dataset | ||||||||||||
| from packaging.version import Version | ||||||||||||
| from transformers import AutoModelForCausalLM, AutoTokenizer, __version__ | ||||||||||||
|
|
||||||||||||
| from llmcompressor import oneshot | ||||||||||||
| from llmcompressor.utils import dispatch_for_generation | ||||||||||||
|
|
||||||||||||
| # NOTE: transformers 4.49.0 has an attribute error with DeepSeek. | ||||||||||||
| # Please consider either downgrading your transformers version to a | ||||||||||||
| # previous version or upgrading to a version where this bug is fixed | ||||||||||||
|
|
||||||||||||
| # select a Mixture of Experts model for quantization | ||||||||||||
| MODEL_ID = "deepseek-ai/DeepSeek-V2.5" | ||||||||||||
| MODEL_ID = "/data0/deepseek-ai/DeepSeek-V2-Lite" | ||||||||||||
| MODEL_ID = "/data0/deepseek-ai/DeepSeek-R1" | ||||||||||||
| MODEL_ID = "/data1/DeepSeek-R1-bf16" | ||||||||||||
|
Comment on lines
+14
to
+17
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. This example script hardcodes multiple
Suggested change
|
||||||||||||
|
|
||||||||||||
| model = AutoModelForCausalLM.from_pretrained( | ||||||||||||
| MODEL_ID, torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto" | ||||||||||||
| ) | ||||||||||||
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | ||||||||||||
|
|
||||||||||||
| # Select calibration dataset. | ||||||||||||
| DATASET_ID = "HuggingFaceH4/ultrachat_200k" | ||||||||||||
| DATASET_SPLIT = "train_sft" | ||||||||||||
| NUM_CALIBRATION_SAMPLES = 2 | ||||||||||||
| MAX_SEQUENCE_LENGTH = 2048 | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| # Load dataset and preprocess. | ||||||||||||
| ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]") | ||||||||||||
| ds = ds.shuffle(seed=42) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def preprocess(example): | ||||||||||||
| return { | ||||||||||||
| "text": tokenizer.apply_chat_template( | ||||||||||||
| example["messages"], | ||||||||||||
| tokenize=False, | ||||||||||||
| ) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| ds = ds.map(preprocess) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| # Tokenize inputs. | ||||||||||||
| def tokenize(sample): | ||||||||||||
| return tokenizer( | ||||||||||||
| sample["text"], | ||||||||||||
| padding=False, | ||||||||||||
| max_length=MAX_SEQUENCE_LENGTH, | ||||||||||||
| truncation=True, | ||||||||||||
| add_special_tokens=False, | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| ds = ds.map(tokenize, remove_columns=ds.column_names) | ||||||||||||
|
|
||||||||||||
| # define a llmcompressor recipe for W416 quantization | ||||||||||||
| # since the MoE gate layers are sensitive to quantization, we add them to the ignore | ||||||||||||
| # list so they remain at full precision | ||||||||||||
| # recipe = "deepseek_recipe_w4a16.yaml" | ||||||||||||
|
|
||||||||||||
| from llmcompressor.modifiers.quantization import QuantizationModifier | ||||||||||||
| from llmcompressor.utils import dispatch_for_generation | ||||||||||||
|
Comment on lines
+66
to
+67
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. |
||||||||||||
|
|
||||||||||||
| recipe = QuantizationModifier(targets="Linear", scheme="NVFP4", ignore=["lm_head"]) | ||||||||||||
|
|
||||||||||||
| oneshot( | ||||||||||||
| model=model, | ||||||||||||
| dataset=ds, | ||||||||||||
| recipe=recipe, | ||||||||||||
| max_seq_length=MAX_SEQUENCE_LENGTH, | ||||||||||||
| num_calibration_samples=NUM_CALIBRATION_SAMPLES, | ||||||||||||
| save_compressed=True, | ||||||||||||
| trust_remote_code_model=True, | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| # Confirm generations of the quantized model look sane. | ||||||||||||
| # Generation is broken for deepseek models when using the latest transformers package | ||||||||||||
| if Version(__version__) < Version("4.48"): | ||||||||||||
| print("========== SAMPLE GENERATION ==============") | ||||||||||||
| dispatch_for_generation(model) | ||||||||||||
| input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to("cuda") | ||||||||||||
| output = model.generate(input_ids, max_new_tokens=20) | ||||||||||||
| print(tokenizer.decode(output[0])) | ||||||||||||
| print("==========================================") | ||||||||||||
| else: | ||||||||||||
| print( | ||||||||||||
| "WARNING: cannot perform sample generation of " | ||||||||||||
| "deepseek models with transformers >= 4.48" | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| # Save to disk in compressed-tensors format. | ||||||||||||
| SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4" | ||||||||||||
| model.save_pretrained(SAVE_DIR, save_compressed=True) | ||||||||||||
| tokenizer.save_pretrained(SAVE_DIR) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| # # Run the model on vLLM | ||||||||||||
| # try: | ||||||||||||
| # from vllm import LLM, SamplingParams | ||||||||||||
|
|
||||||||||||
| # vllm_installed = True | ||||||||||||
| # except ImportError: | ||||||||||||
| # vllm_installed = False | ||||||||||||
|
|
||||||||||||
| # if vllm_installed: | ||||||||||||
| # print("vLLM installed, running using vLLM") | ||||||||||||
| # sampling_params = SamplingParams(temperature=0.80, top_p=0.95) | ||||||||||||
| # llm = LLM( | ||||||||||||
| # model=SAVE_DIR, | ||||||||||||
| # tensor_parallel_size=2, | ||||||||||||
| # trust_remote_code=True, | ||||||||||||
| # max_model_len=1042, | ||||||||||||
| # dtype=torch.half, | ||||||||||||
| # ) | ||||||||||||
| # prompts = [ | ||||||||||||
| # "The capital of France is", | ||||||||||||
| # "The president of the US is", | ||||||||||||
| # "My name is", | ||||||||||||
| # ] | ||||||||||||
|
|
||||||||||||
| # outputs = llm.generate(prompts, sampling_params) | ||||||||||||
| # print("================= vLLM GENERATION ======================") | ||||||||||||
| # for output in outputs: | ||||||||||||
| # assert output | ||||||||||||
| # prompt = output.prompt | ||||||||||||
| # generated_text = output.outputs[0].text | ||||||||||||
| # print("PROMPT", prompt) | ||||||||||||
| # print("GENERATED TEXT", generated_text) | ||||||||||||
|
Comment on lines
+102
to
+133
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. |
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,13 @@ | |||||||||||||||||||||||||||||||||
| QuantizationStrategy, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| from compressed_tensors.quantization.lifecycle.forward import forward_quantize | ||||||||||||||||||||||||||||||||||
| from compressed_tensors.quantization.utils import ( | ||||||||||||||||||||||||||||||||||
| is_fp4, | ||||||||||||||||||||||||||||||||||
| is_kv_cache_quant_scheme, | ||||||||||||||||||||||||||||||||||
| is_mx, | ||||||||||||||||||||||||||||||||||
| is_mxfp4, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| from compressed_tensors.utils import align_module_device, update_parameter_data | ||||||||||||||||||||||||||||||||||
| from compressed_tensors.quantization.utils import is_kv_cache_quant_scheme | ||||||||||||||||||||||||||||||||||
| from compressed_tensors.utils import align_module_device, update_offload_parameter | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+12
to
20
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. There are duplicate imports in this block.
Suggested change
|
||||||||||||||||||||||||||||||||||
| from loguru import logger | ||||||||||||||||||||||||||||||||||
|
|
@@ -140,6 +147,10 @@ def update_weight_global_scale(module: Module): | |||||||||||||||||||||||||||||||||
| != QuantizationStrategy.TENSOR_GROUP | ||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||
| weight_quant_args = getattr_chain(module, "quantization_scheme.weights") | ||||||||||||||||||||||||||||||||||
| if is_mx(quantization_args=weight_quant_args): | ||||||||||||||||||||||||||||||||||
| # MX schemes do not use global scale | ||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| call_observer( | ||||||||||||||||||||||||||||||||||
| module, | ||||||||||||||||||||||||||||||||||
|
|
@@ -196,7 +207,7 @@ def calibrate_activations(module: Module, value: torch.Tensor, base_name: str): | |||||||||||||||||||||||||||||||||
| if quantization_args is not None: | ||||||||||||||||||||||||||||||||||
| if quantization_args.dynamic in (True, DynamicType.LOCAL): | ||||||||||||||||||||||||||||||||||
| calculate_qparams = False | ||||||||||||||||||||||||||||||||||
| if quantization_args.strategy == QuantizationStrategy.TENSOR_GROUP: | ||||||||||||||||||||||||||||||||||
| if is_fp4(quantization_args=quantization_args): | ||||||||||||||||||||||||||||||||||
| calculate_gparam = True | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| call_observer( | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block contains hardcoded, user-specific paths, multiple re-definitions of variables, and a debug print statement. This appears to be temporary development code that should be removed before merging, as it makes the example non-runnable for other users.