dchenna commited on
Commit
ba0ba68
·
1 Parent(s): 924602d

Adding the quantized model files

Browse files

- including the python script for model quantization and evaluation

.gitattributes CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ model.safetensors filter=lfs diff=lfs merge=lfs -text
37
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
38
+ vocab.json filter=lfs diff=lfs merge=lfs -text
added_tokens.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "</think>": 151668,
3
+ "</tool_call>": 151658,
4
+ "</tool_response>": 151666,
5
+ "<think>": 151667,
6
+ "<tool_call>": 151657,
7
+ "<tool_response>": 151665,
8
+ "<|box_end|>": 151649,
9
+ "<|box_start|>": 151648,
10
+ "<|endoftext|>": 151643,
11
+ "<|file_sep|>": 151664,
12
+ "<|fim_middle|>": 151660,
13
+ "<|fim_pad|>": 151662,
14
+ "<|fim_prefix|>": 151659,
15
+ "<|fim_suffix|>": 151661,
16
+ "<|im_end|>": 151645,
17
+ "<|im_start|>": 151644,
18
+ "<|image_pad|>": 151655,
19
+ "<|object_ref_end|>": 151647,
20
+ "<|object_ref_start|>": 151646,
21
+ "<|quad_end|>": 151651,
22
+ "<|quad_start|>": 151650,
23
+ "<|repo_name|>": 151663,
24
+ "<|video_pad|>": 151656,
25
+ "<|vision_end|>": 151653,
26
+ "<|vision_pad|>": 151654,
27
+ "<|vision_start|>": 151652
28
+ }
chat_template.jinja ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if tools %}
2
+ {{- '<|im_start|>system\n' }}
3
+ {%- if messages[0].role == 'system' %}
4
+ {%- if messages[0].content is string %}
5
+ {{- messages[0].content }}
6
+ {%- else %}
7
+ {%- for content in messages[0].content %}
8
+ {%- if 'text' in content %}
9
+ {{- content.text }}
10
+ {%- endif %}
11
+ {%- endfor %}
12
+ {%- endif %}
13
+ {{- '\n\n' }}
14
+ {%- endif %}
15
+ {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
16
+ {%- for tool in tools %}
17
+ {{- "\n" }}
18
+ {{- tool | tojson }}
19
+ {%- endfor %}
20
+ {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
21
+ {%- else %}
22
+ {%- if messages[0].role == 'system' %}
23
+ {{- '<|im_start|>system\n' }}
24
+ {%- if messages[0].content is string %}
25
+ {{- messages[0].content }}
26
+ {%- else %}
27
+ {%- for content in messages[0].content %}
28
+ {%- if 'text' in content %}
29
+ {{- content.text }}
30
+ {%- endif %}
31
+ {%- endfor %}
32
+ {%- endif %}
33
+ {{- '<|im_end|>\n' }}
34
+ {%- endif %}
35
+ {%- endif %}
36
+ {%- set image_count = namespace(value=0) %}
37
+ {%- set video_count = namespace(value=0) %}
38
+ {%- for message in messages %}
39
+ {%- if message.role == "user" %}
40
+ {{- '<|im_start|>' + message.role + '\n' }}
41
+ {%- if message.content is string %}
42
+ {{- message.content }}
43
+ {%- else %}
44
+ {%- for content in message.content %}
45
+ {%- if content.type == 'image' or 'image' in content or 'image_url' in content %}
46
+ {%- set image_count.value = image_count.value + 1 %}
47
+ {%- if add_vision_id %}Picture {{ image_count.value }}: {% endif -%}
48
+ <|vision_start|><|image_pad|><|vision_end|>
49
+ {%- elif content.type == 'video' or 'video' in content %}
50
+ {%- set video_count.value = video_count.value + 1 %}
51
+ {%- if add_vision_id %}Video {{ video_count.value }}: {% endif -%}
52
+ <|vision_start|><|video_pad|><|vision_end|>
53
+ {%- elif 'text' in content %}
54
+ {{- content.text }}
55
+ {%- endif %}
56
+ {%- endfor %}
57
+ {%- endif %}
58
+ {{- '<|im_end|>\n' }}
59
+ {%- elif message.role == "assistant" %}
60
+ {{- '<|im_start|>' + message.role + '\n' }}
61
+ {%- if message.content is string %}
62
+ {{- message.content }}
63
+ {%- else %}
64
+ {%- for content_item in message.content %}
65
+ {%- if 'text' in content_item %}
66
+ {{- content_item.text }}
67
+ {%- endif %}
68
+ {%- endfor %}
69
+ {%- endif %}
70
+ {%- if message.tool_calls %}
71
+ {%- for tool_call in message.tool_calls %}
72
+ {%- if (loop.first and message.content) or (not loop.first) %}
73
+ {{- '\n' }}
74
+ {%- endif %}
75
+ {%- if tool_call.function %}
76
+ {%- set tool_call = tool_call.function %}
77
+ {%- endif %}
78
+ {{- '<tool_call>\n{"name": "' }}
79
+ {{- tool_call.name }}
80
+ {{- '", "arguments": ' }}
81
+ {%- if tool_call.arguments is string %}
82
+ {{- tool_call.arguments }}
83
+ {%- else %}
84
+ {{- tool_call.arguments | tojson }}
85
+ {%- endif %}
86
+ {{- '}\n</tool_call>' }}
87
+ {%- endfor %}
88
+ {%- endif %}
89
+ {{- '<|im_end|>\n' }}
90
+ {%- elif message.role == "tool" %}
91
+ {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
92
+ {{- '<|im_start|>user' }}
93
+ {%- endif %}
94
+ {{- '\n<tool_response>\n' }}
95
+ {%- if message.content is string %}
96
+ {{- message.content }}
97
+ {%- else %}
98
+ {%- for content in message.content %}
99
+ {%- if content.type == 'image' or 'image' in content or 'image_url' in content %}
100
+ {%- set image_count.value = image_count.value + 1 %}
101
+ {%- if add_vision_id %}Picture {{ image_count.value }}: {% endif -%}
102
+ <|vision_start|><|image_pad|><|vision_end|>
103
+ {%- elif content.type == 'video' or 'video' in content %}
104
+ {%- set video_count.value = video_count.value + 1 %}
105
+ {%- if add_vision_id %}Video {{ video_count.value }}: {% endif -%}
106
+ <|vision_start|><|video_pad|><|vision_end|>
107
+ {%- elif 'text' in content %}
108
+ {{- content.text }}
109
+ {%- endif %}
110
+ {%- endfor %}
111
+ {%- endif %}
112
+ {{- '\n</tool_response>' }}
113
+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
114
+ {{- '<|im_end|>\n' }}
115
+ {%- endif %}
116
+ {%- endif %}
117
+ {%- endfor %}
118
+ {%- if add_generation_prompt %}
119
+ {{- '<|im_start|>assistant\n' }}
120
+ {%- endif %}
config.json ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Qwen3VLForConditionalGeneration"
4
+ ],
5
+ "dtype": "bfloat16",
6
+ "image_token_id": 151655,
7
+ "model_type": "qwen3_vl",
8
+ "quantization_config": {
9
+ "algo_config": null,
10
+ "exclude": [],
11
+ "export": {
12
+ "kv_cache_group": [],
13
+ "min_kv_scale": 0.0,
14
+ "pack_method": "reorder",
15
+ "weight_format": "real_quantized",
16
+ "weight_merge_groups": null
17
+ },
18
+ "global_quant_config": {
19
+ "bias": null,
20
+ "input_tensors": null,
21
+ "output_tensors": null,
22
+ "target_device": null,
23
+ "weight": {
24
+ "ch_axis": -1,
25
+ "dtype": "uint4",
26
+ "group_size": 128,
27
+ "is_dynamic": false,
28
+ "is_scale_quant": false,
29
+ "mx_element_dtype": null,
30
+ "observer_cls": "PerGroupMinMaxObserver",
31
+ "qscheme": "per_group",
32
+ "round_method": "half_even",
33
+ "scale_calculation_mode": null,
34
+ "scale_format": null,
35
+ "scale_type": "float",
36
+ "symmetric": false
37
+ }
38
+ },
39
+ "kv_cache_post_rope": false,
40
+ "kv_cache_quant_config": {},
41
+ "layer_quant_config": {},
42
+ "layer_type_quant_config": {},
43
+ "quant_method": "quark",
44
+ "quant_mode": "eager_mode",
45
+ "softmax_quant_spec": null,
46
+ "version": "0.11"
47
+ },
48
+ "text_config": {
49
+ "attention_bias": false,
50
+ "attention_dropout": 0.0,
51
+ "bos_token_id": 151643,
52
+ "dtype": "bfloat16",
53
+ "eos_token_id": 151645,
54
+ "head_dim": 128,
55
+ "hidden_act": "silu",
56
+ "hidden_size": 2560,
57
+ "initializer_range": 0.02,
58
+ "intermediate_size": 9728,
59
+ "max_position_embeddings": 262144,
60
+ "model_type": "qwen3_vl_text",
61
+ "num_attention_heads": 32,
62
+ "num_hidden_layers": 36,
63
+ "num_key_value_heads": 8,
64
+ "rms_norm_eps": 1e-06,
65
+ "rope_scaling": {
66
+ "mrope_interleaved": true,
67
+ "mrope_section": [
68
+ 24,
69
+ 20,
70
+ 20
71
+ ],
72
+ "rope_type": "default"
73
+ },
74
+ "rope_theta": 5000000,
75
+ "tie_word_embeddings": true,
76
+ "use_cache": true,
77
+ "vocab_size": 151936
78
+ },
79
+ "tie_word_embeddings": true,
80
+ "transformers_version": "4.57.0",
81
+ "video_token_id": 151656,
82
+ "vision_config": {
83
+ "deepstack_visual_indexes": [
84
+ 5,
85
+ 11,
86
+ 17
87
+ ],
88
+ "depth": 24,
89
+ "dtype": "bfloat16",
90
+ "hidden_act": "gelu_pytorch_tanh",
91
+ "hidden_size": 1024,
92
+ "in_channels": 3,
93
+ "initializer_range": 0.02,
94
+ "intermediate_size": 4096,
95
+ "model_type": "qwen3_vl",
96
+ "num_heads": 16,
97
+ "num_position_embeddings": 2304,
98
+ "out_hidden_size": 2560,
99
+ "patch_size": 16,
100
+ "spatial_merge_size": 2,
101
+ "temporal_patch_size": 2
102
+ },
103
+ "vision_end_token_id": 151653,
104
+ "vision_start_token_id": 151652
105
+ }
generation_config.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 151643,
3
+ "do_sample": true,
4
+ "eos_token_id": [
5
+ 151645,
6
+ 151643
7
+ ],
8
+ "pad_token_id": 151643,
9
+ "temperature": 0.7,
10
+ "top_k": 20,
11
+ "top_p": 0.8,
12
+ "transformers_version": "4.57.0"
13
+ }
generation_test_results.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Manual Text Generation Test Results
2
+ ==================================================
3
+
4
+ Input: The capital of France is
5
+ Output: The capital of France is Paris. The capital of Spain is Madrid. The capital of Italy is Rome. The capital of Germany
6
+
7
+ Input: Python is a programming language that
8
+ Output: Python is a programming language that is easy to learn and use. It is a high-level, interpreted language, which means that it
9
+
10
+ Input: Machine learning is
11
+ Output: Machine learning is a subset of artificial intelligence (AI) that enables computers to learn from data and make predictions or decisions
12
+
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b6436c8f69a2827d7f80a906a0bd5c2ac06a27e55f8405156a443b2d9f31c1e2
3
+ size 3090343456
run_qwen3_vl_4b_quant_model.py ADDED
@@ -0,0 +1,281 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import os
3
+ import torch
4
+ import copy
5
+ import argparse
6
+
7
+ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
8
+ from transformers import AutoConfig, AutoTokenizer, AutoProcessor, PreTrainedTokenizer
9
+
10
+ from quark.torch import (
11
+ LLMTemplate,
12
+ ModelQuantizer,
13
+ export_safetensors,
14
+ import_model_from_safetensors,
15
+ )
16
+
17
+ from quark.torch.utils.llm import (
18
+ get_calib_dataloader,
19
+ )
20
+
21
+ # Import the correct model class
22
+ try:
23
+ from transformers import Qwen3VLForConditionalGeneration
24
+ model_class = Qwen3VLForConditionalGeneration
25
+ print("Using Qwen3VLForConditionalGeneration")
26
+ except ImportError:
27
+ print("Failed to load the model using Qwen3_VLForConditionalGeneration")
28
+
29
+
30
+ def parse_args():
31
+ parser = argparse.ArgumentParser(description='Quantize Qwen3-VL model with various configurations')
32
+
33
+ # Model paths
34
+ parser.add_argument('--model_dir', type=str, default='/scratch/dwchenna/github/hf-models/Qwen3-VL-4B-Instruct',
35
+ help='Path to the input model directory')
36
+ parser.add_argument('--output_dir', type=str, default='quantized_models/Qwen3-VL-4B-Instruct-per-grp-quant',
37
+ help='Output directory for quantized model')
38
+
39
+ # Evaluation options
40
+ parser.add_argument('--eval_float', action='store_true', default=False,
41
+ help='Evaluate original float model')
42
+ parser.add_argument('--eval_quantized', action='store_true', default=False,
43
+ help='Evaluate quantized model')
44
+ parser.add_argument('--skip_quantization', action='store_true',
45
+ help='Skip quantization and only evaluate existing models')
46
+
47
+ # Device
48
+ parser.add_argument('--device', type=str, default='cuda' if torch.cuda.is_available() else 'cpu',
49
+ help='Device to use for computation')
50
+
51
+ return parser.parse_args()
52
+
53
+
54
+ args = parse_args()
55
+
56
+ # Setup
57
+ model_dir = args.model_dir
58
+ model_out_dir = args.output_dir
59
+ device = args.device
60
+
61
+ print(f"Device: {device}")
62
+ print("Loading model...")
63
+
64
+ # Load config, tokenizer, processor
65
+ config = AutoConfig.from_pretrained(model_dir, trust_remote_code=True)
66
+ tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
67
+ processor = AutoProcessor.from_pretrained(model_dir, trust_remote_code=True)
68
+
69
+ # Load model
70
+ model = model_class.from_pretrained(
71
+ model_dir,
72
+ trust_remote_code=True,
73
+ torch_dtype=torch.bfloat16,
74
+ device_map="cpu"
75
+ ).to(device)
76
+
77
+ print(f"Model loaded: {model.__class__.__name__}")
78
+ model.eval()
79
+
80
+ # Create a deep copy before quantization to preserve the original float model
81
+ float_model = copy.deepcopy(model)
82
+ # Filter out visual part
83
+ # del model.visual
84
+ print(model)
85
+
86
+ # Create output directory
87
+ os.makedirs(model_out_dir, exist_ok=True)
88
+ tokenizer.save_pretrained(model_out_dir)
89
+
90
+ # Get model type
91
+ model_config_type = (
92
+ model.config.model_type if hasattr(model.config, "model_type") else model.config.architectures[0]
93
+ )
94
+ print(f"Model config type: {model_config_type}")
95
+
96
+ # Register template for qwen3_vl if needed
97
+ if model_config_type == "qwen3_vl":
98
+ qwen3_vl_template = LLMTemplate(
99
+ model_type="qwen3_vl",
100
+ kv_layers_name=["model.layers.*.self_attn.q_proj",
101
+ "model.layers.*.self_attn.k_proj",
102
+ "model.layers.*.self_attn.v_proj"],
103
+ q_layer_name="model.layers.*.self_attn.q_proj",
104
+ exclude_layers_name=["visual*", "*vision*"],
105
+ )
106
+ LLMTemplate.register_template(qwen3_vl_template)
107
+ print(f"Registered template for '{qwen3_vl_template.model_type}'")
108
+
109
+ # Check if model type is supported
110
+ if model_config_type not in LLMTemplate.list_available():
111
+ print(f"Available templates: {LLMTemplate.list_available()}")
112
+ raise ValueError(f"Model type '{model_config_type}' is not supported.")
113
+
114
+ template = LLMTemplate.get(model_config_type)
115
+ print(f"Using template: {model_config_type}")
116
+
117
+ # Quantization configuration
118
+ quant_scheme = "uint4_wo_128"
119
+ quant_algo = None # "awq"
120
+ exclude_layers = ["visual*", "*vision*"]
121
+
122
+ quant_config = template.get_config(
123
+ scheme=quant_scheme,
124
+ algorithm=quant_algo,
125
+ exclude_layers=exclude_layers,
126
+ )
127
+
128
+ print(f"Quantization config: {quant_config}")
129
+
130
+ # Quantization section
131
+ if not args.skip_quantization:
132
+ # Create calibration dataloader
133
+ print("Loading calibration dataset...")
134
+ main_device = model.device
135
+ dataset = "pileval_for_awq_benchmark"
136
+ batch_size = 1
137
+ num_calib_data = 128
138
+ seq_len = 512
139
+
140
+ calib_dataloader = get_calib_dataloader(
141
+ dataset_name=dataset,
142
+ processor=None, # Set to None to avoid multimodal issues
143
+ tokenizer=tokenizer,
144
+ batch_size=batch_size,
145
+ num_calib_data=num_calib_data,
146
+ seqlen=seq_len,
147
+ device=main_device,
148
+ )
149
+
150
+ # Quantize model
151
+ print("Starting quantization...")
152
+ try:
153
+ quantizer = ModelQuantizer(quant_config)
154
+ model = quantizer.quantize_model(model, calib_dataloader)
155
+
156
+ # Freeze model
157
+ model = quantizer.freeze(model)
158
+ print("✓ Quantization completed successfully!")
159
+
160
+ # Export quantized model
161
+ print("Exporting quantized model...")
162
+ with torch.no_grad():
163
+ export_safetensors(
164
+ model=model,
165
+ output_dir=model_out_dir,
166
+ custom_mode="quark",
167
+ weight_format="real_quantized",
168
+ pack_method="reorder",
169
+ )
170
+
171
+ print(f"✓ Model exported to: {model_out_dir}")
172
+
173
+ except Exception as e:
174
+ print(f"✗ Quantization failed: {e}")
175
+ print("This is likely due to AWQ not being compatible with multimodal models.")
176
+ print("Try using a simpler quantization scheme without AWQ.")
177
+
178
+ print("Quantization script completed!")
179
+ else:
180
+ print("[INFO]: Skipping quantization as requested")
181
+
182
+ # Evaluation section
183
+ print("\n" + "="*60)
184
+ print("MODEL EVALUATION")
185
+ print("="*60)
186
+
187
+ if args.eval_float or args.eval_quantized:
188
+ # Load quantized model if needed for evaluation
189
+ model_quant = None
190
+ if args.eval_quantized:
191
+ print("Loading quantized model...")
192
+ try:
193
+ # Load a fresh model instance first
194
+ fresh_model = model_class.from_pretrained(
195
+ model_dir, # Use original model dir for architecture
196
+ trust_remote_code=True,
197
+ torch_dtype=torch.bfloat16,
198
+ device_map="cpu"
199
+ )
200
+ # Load quantized weights using Quark's import function
201
+ model_quant = import_model_from_safetensors(
202
+ model=fresh_model,
203
+ model_dir=model_out_dir # Directory with quantized safetensors
204
+ )
205
+ model_quant = model_quant.to(device)
206
+ print("Successfully loaded quantized model from safetensors")
207
+ print(f"Model type: {type(model_quant)}")
208
+ except Exception as e:
209
+ print(f"✗ Failed to load from {model_out_dir}: {e}")
210
+ if not args.skip_quantization:
211
+ print("Using the quantized model directly from memory...")
212
+ model_quant = model
213
+ else:
214
+ print("[WARNING]: No quantized model available for evaluation")
215
+ args.eval_quantized = False
216
+
217
+ # Load the evaluation dataset
218
+ from datasets import Dataset, load_dataset
219
+ testdata = load_dataset("wikitext", "wikitext-2-raw-v1", split="test")
220
+
221
+ # Use tokenizer from model directory or output directory
222
+ tokenizer_path = model_out_dir if os.path.exists(model_out_dir) else model_dir
223
+ tokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained( # type: ignore
224
+ tokenizer_path,
225
+ trust_remote_code=True,
226
+ )
227
+
228
+ # Load the test dataset
229
+ testenc = tokenizer("\n\n".join(testdata["text"]), return_tensors="pt")
230
+
231
+ # Results storage
232
+ results = {}
233
+
234
+ from quark.contrib.llm_eval import ppl_eval
235
+
236
+ # Evaluate original float model
237
+ if args.eval_float:
238
+ print("\n[INFO]: Evaluating original float model...")
239
+ print("-" * 40)
240
+ try:
241
+ main_device = float_model.device
242
+ ppl_float = ppl_eval(float_model, testenc, main_device)
243
+ ppl_float_value = ppl_float.item()
244
+ results['float_ppl'] = ppl_float_value
245
+ print(f"[RESULT] Float Model Perplexity: {ppl_float_value:.4f}")
246
+ except Exception as e:
247
+ print(f"[ERROR] Float model evaluation failed: {e}")
248
+ results['float_ppl'] = None
249
+
250
+ # Evaluate quantized model
251
+ if args.eval_quantized and model_quant is not None:
252
+ print("\n[INFO]: Evaluating quantized model...")
253
+ print("-" * 40)
254
+ try:
255
+ main_device = model_quant.device
256
+ ppl_quant = ppl_eval(model_quant, testenc, main_device)
257
+ ppl_quant_value = ppl_quant.item()
258
+ results['quantized_ppl'] = ppl_quant_value
259
+ print(f"[RESULT] Quantized Model Perplexity: {ppl_quant_value:.4f}")
260
+ except Exception as e:
261
+ print(f"[ERROR] Quantized model evaluation failed: {e}")
262
+ results['quantized_ppl'] = None
263
+
264
+ # Summary
265
+ print("\n" + "="*60)
266
+ print("EVALUATION SUMMARY")
267
+ print("="*60)
268
+ print(f"Model: {model_dir}")
269
+ print(f"Output: {model_out_dir}")
270
+
271
+ if 'float_ppl' in results and results['float_ppl'] is not None:
272
+ print(f"Float Model Perplexity: {results['float_ppl']:.4f}")
273
+
274
+ if 'quantized_ppl' in results and results['quantized_ppl'] is not None:
275
+ print(f"Quantized Model Perplexity: {results['quantized_ppl']:.4f}")
276
+
277
+ print("="*60)
278
+ else:
279
+ print("\n[INFO]: Skipping evaluation as requested")
280
+
281
+ print("\n[INFO]: Script completed successfully!")
special_tokens_map.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|im_start|>",
4
+ "<|im_end|>",
5
+ "<|object_ref_start|>",
6
+ "<|object_ref_end|>",
7
+ "<|box_start|>",
8
+ "<|box_end|>",
9
+ "<|quad_start|>",
10
+ "<|quad_end|>",
11
+ "<|vision_start|>",
12
+ "<|vision_end|>",
13
+ "<|vision_pad|>",
14
+ "<|image_pad|>",
15
+ "<|video_pad|>"
16
+ ],
17
+ "eos_token": {
18
+ "content": "<|im_end|>",
19
+ "lstrip": false,
20
+ "normalized": false,
21
+ "rstrip": false,
22
+ "single_word": false
23
+ },
24
+ "pad_token": {
25
+ "content": "<|endoftext|>",
26
+ "lstrip": false,
27
+ "normalized": false,
28
+ "rstrip": false,
29
+ "single_word": false
30
+ }
31
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:aeb13307a71acd8fe81861d94ad54ab689df773318809eed3cbe794b4492dae4
3
+ size 11422654
tokenizer_config.json ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_prefix_space": false,
4
+ "added_tokens_decoder": {
5
+ "151643": {
6
+ "content": "<|endoftext|>",
7
+ "lstrip": false,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false,
11
+ "special": true
12
+ },
13
+ "151644": {
14
+ "content": "<|im_start|>",
15
+ "lstrip": false,
16
+ "normalized": false,
17
+ "rstrip": false,
18
+ "single_word": false,
19
+ "special": true
20
+ },
21
+ "151645": {
22
+ "content": "<|im_end|>",
23
+ "lstrip": false,
24
+ "normalized": false,
25
+ "rstrip": false,
26
+ "single_word": false,
27
+ "special": true
28
+ },
29
+ "151646": {
30
+ "content": "<|object_ref_start|>",
31
+ "lstrip": false,
32
+ "normalized": false,
33
+ "rstrip": false,
34
+ "single_word": false,
35
+ "special": true
36
+ },
37
+ "151647": {
38
+ "content": "<|object_ref_end|>",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false,
43
+ "special": true
44
+ },
45
+ "151648": {
46
+ "content": "<|box_start|>",
47
+ "lstrip": false,
48
+ "normalized": false,
49
+ "rstrip": false,
50
+ "single_word": false,
51
+ "special": true
52
+ },
53
+ "151649": {
54
+ "content": "<|box_end|>",
55
+ "lstrip": false,
56
+ "normalized": false,
57
+ "rstrip": false,
58
+ "single_word": false,
59
+ "special": true
60
+ },
61
+ "151650": {
62
+ "content": "<|quad_start|>",
63
+ "lstrip": false,
64
+ "normalized": false,
65
+ "rstrip": false,
66
+ "single_word": false,
67
+ "special": true
68
+ },
69
+ "151651": {
70
+ "content": "<|quad_end|>",
71
+ "lstrip": false,
72
+ "normalized": false,
73
+ "rstrip": false,
74
+ "single_word": false,
75
+ "special": true
76
+ },
77
+ "151652": {
78
+ "content": "<|vision_start|>",
79
+ "lstrip": false,
80
+ "normalized": false,
81
+ "rstrip": false,
82
+ "single_word": false,
83
+ "special": true
84
+ },
85
+ "151653": {
86
+ "content": "<|vision_end|>",
87
+ "lstrip": false,
88
+ "normalized": false,
89
+ "rstrip": false,
90
+ "single_word": false,
91
+ "special": true
92
+ },
93
+ "151654": {
94
+ "content": "<|vision_pad|>",
95
+ "lstrip": false,
96
+ "normalized": false,
97
+ "rstrip": false,
98
+ "single_word": false,
99
+ "special": true
100
+ },
101
+ "151655": {
102
+ "content": "<|image_pad|>",
103
+ "lstrip": false,
104
+ "normalized": false,
105
+ "rstrip": false,
106
+ "single_word": false,
107
+ "special": true
108
+ },
109
+ "151656": {
110
+ "content": "<|video_pad|>",
111
+ "lstrip": false,
112
+ "normalized": false,
113
+ "rstrip": false,
114
+ "single_word": false,
115
+ "special": true
116
+ },
117
+ "151657": {
118
+ "content": "<tool_call>",
119
+ "lstrip": false,
120
+ "normalized": false,
121
+ "rstrip": false,
122
+ "single_word": false,
123
+ "special": false
124
+ },
125
+ "151658": {
126
+ "content": "</tool_call>",
127
+ "lstrip": false,
128
+ "normalized": false,
129
+ "rstrip": false,
130
+ "single_word": false,
131
+ "special": false
132
+ },
133
+ "151659": {
134
+ "content": "<|fim_prefix|>",
135
+ "lstrip": false,
136
+ "normalized": false,
137
+ "rstrip": false,
138
+ "single_word": false,
139
+ "special": false
140
+ },
141
+ "151660": {
142
+ "content": "<|fim_middle|>",
143
+ "lstrip": false,
144
+ "normalized": false,
145
+ "rstrip": false,
146
+ "single_word": false,
147
+ "special": false
148
+ },
149
+ "151661": {
150
+ "content": "<|fim_suffix|>",
151
+ "lstrip": false,
152
+ "normalized": false,
153
+ "rstrip": false,
154
+ "single_word": false,
155
+ "special": false
156
+ },
157
+ "151662": {
158
+ "content": "<|fim_pad|>",
159
+ "lstrip": false,
160
+ "normalized": false,
161
+ "rstrip": false,
162
+ "single_word": false,
163
+ "special": false
164
+ },
165
+ "151663": {
166
+ "content": "<|repo_name|>",
167
+ "lstrip": false,
168
+ "normalized": false,
169
+ "rstrip": false,
170
+ "single_word": false,
171
+ "special": false
172
+ },
173
+ "151664": {
174
+ "content": "<|file_sep|>",
175
+ "lstrip": false,
176
+ "normalized": false,
177
+ "rstrip": false,
178
+ "single_word": false,
179
+ "special": false
180
+ },
181
+ "151665": {
182
+ "content": "<tool_response>",
183
+ "lstrip": false,
184
+ "normalized": false,
185
+ "rstrip": false,
186
+ "single_word": false,
187
+ "special": false
188
+ },
189
+ "151666": {
190
+ "content": "</tool_response>",
191
+ "lstrip": false,
192
+ "normalized": false,
193
+ "rstrip": false,
194
+ "single_word": false,
195
+ "special": false
196
+ },
197
+ "151667": {
198
+ "content": "<think>",
199
+ "lstrip": false,
200
+ "normalized": false,
201
+ "rstrip": false,
202
+ "single_word": false,
203
+ "special": false
204
+ },
205
+ "151668": {
206
+ "content": "</think>",
207
+ "lstrip": false,
208
+ "normalized": false,
209
+ "rstrip": false,
210
+ "single_word": false,
211
+ "special": false
212
+ }
213
+ },
214
+ "additional_special_tokens": [
215
+ "<|im_start|>",
216
+ "<|im_end|>",
217
+ "<|object_ref_start|>",
218
+ "<|object_ref_end|>",
219
+ "<|box_start|>",
220
+ "<|box_end|>",
221
+ "<|quad_start|>",
222
+ "<|quad_end|>",
223
+ "<|vision_start|>",
224
+ "<|vision_end|>",
225
+ "<|vision_pad|>",
226
+ "<|image_pad|>",
227
+ "<|video_pad|>"
228
+ ],
229
+ "bos_token": null,
230
+ "clean_up_tokenization_spaces": false,
231
+ "eos_token": "<|im_end|>",
232
+ "errors": "replace",
233
+ "extra_special_tokens": {},
234
+ "model_max_length": 262144,
235
+ "pad_token": "<|endoftext|>",
236
+ "split_special_tokens": false,
237
+ "tokenizer_class": "Qwen2Tokenizer",
238
+ "unk_token": null
239
+ }
vocab.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ca10d7e9fb3ed18575dd1e277a2579c16d108e32f27439684afa0e10b1440910
3
+ size 2776833