@@ -23,26 +23,26 @@ def get_dataloader(tokenizer, seqlen, dataset, nsamples=128, seed=0):
23
23
all_tokens = []
24
24
rng = np .random .default_rng (seed = 42 )
25
25
26
- # --- Step 1: Unify all input types into a single list of tokens ---
26
+ # Unify all input types into a single list of tokens
27
27
if isinstance (dataset , str ):
28
28
logging .info (f"Loading '{ dataset } ' dataset from Hub..." )
29
29
if dataset == "wikitext2" :
30
30
d_name , d_config = "wikitext" , "wikitext-2-raw-v1"
31
31
elif dataset == "ptb" :
32
32
url = "https://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz"
33
33
try :
34
- # 1. Download the archive into memory
34
+ # Download the archive into memory
35
35
response = requests .get (url )
36
36
response .raise_for_status ()
37
37
38
- # 2. Extract only the test file from the in-memory archive
38
+ # Extract only the test file from the in-memory archive
39
39
with tarfile .open (
40
40
fileobj = io .BytesIO (response .content ), mode = "r:gz"
41
41
) as tar :
42
42
train_path = "./simple-examples/data/ptb.train.txt"
43
43
test_bytes = tar .extractfile (train_path ).read ()
44
44
45
- # 3. Decode the bytes and join into a single string
45
+ # Decode the bytes and join into a single string
46
46
test_lines = test_bytes .decode ("utf-8" ).strip ().split ("\n " )
47
47
full_text = "\n \n " .join (test_lines )
48
48
all_tokens = tokenizer .tokenize (full_text )
@@ -51,7 +51,7 @@ def get_dataloader(tokenizer, seqlen, dataset, nsamples=128, seed=0):
51
51
"calibration."
52
52
)
53
53
54
- # 2. Perform sampling and chunking directly inside this block
54
+ # Perform sampling and chunking directly inside this block
55
55
all_tokens = np .array (all_tokens , dtype = np .int32 )
56
56
required_tokens = nsamples * seqlen
57
57
if len (all_tokens ) < required_tokens :
@@ -73,7 +73,7 @@ def get_dataloader(tokenizer, seqlen, dataset, nsamples=128, seed=0):
73
73
74
74
final_array = ops .stack (calibration_samples , axis = 0 )
75
75
76
- # 3. Return the correctly shaped array, isolating the logic
76
+ # Return the correctly shaped array, isolating the logic
77
77
return ops .convert_to_numpy (final_array )
78
78
79
79
except Exception as e :
@@ -115,7 +115,6 @@ def get_dataloader(tokenizer, seqlen, dataset, nsamples=128, seed=0):
115
115
116
116
return np .array (samples , dtype = np .int32 )
117
117
else :
118
- logging .info (f"Warning: No specific alias found for '{ dataset } '." )
119
118
logging .info (
120
119
f"Attempting to load '{ dataset } ' directly with its "
121
120
"default configuration."
@@ -132,7 +131,7 @@ def get_dataloader(tokenizer, seqlen, dataset, nsamples=128, seed=0):
132
131
all_tokens = tokenizer .tokenize (full_text )
133
132
134
133
else :
135
- logging .info ("\n ==> Using pre-made dataset/generator... " )
134
+ logging .info ("Using pre-made dataset/generator" )
136
135
dataset_list = list (dataset )
137
136
138
137
if not dataset_list :
@@ -161,9 +160,6 @@ def get_dataloader(tokenizer, seqlen, dataset, nsamples=128, seed=0):
161
160
repeats = - (- required_tokens // len (all_tokens )) # Ceiling division
162
161
all_tokens = np .tile (all_tokens , repeats )
163
162
164
- # --- Step 3: Chunk the token list into samples ---
165
- # utils.set_random_seed(seed)
166
-
167
163
calibration_samples = []
168
164
for _ in range (nsamples ):
169
165
# Generate a random starting index
@@ -260,7 +256,7 @@ def apply_gptq_layerwise(
260
256
embedding_layer = None
261
257
transformer_blocks = []
262
258
if hasattr (model , "backbone" ):
263
- logging .info (" -> Detected KerasNLP model structure." )
259
+ logging .info ("Detected KerasNLP model structure." )
264
260
backbone = model .backbone
265
261
transformer_blocks = backbone .transformer_layers
266
262
# Find the embedding layer by checking for common names or by type.
@@ -311,7 +307,7 @@ def apply_gptq_layerwise(
311
307
"Skipping."
312
308
)
313
309
else :
314
- logging .info (f" Found layers: { list (sub_layers_map .keys ())} " )
310
+ logging .info (f"Found layers: { list (sub_layers_map .keys ())} " )
315
311
gptq_objects = {
316
312
name : GPTQ (layer ) for name , layer in sub_layers_map .items ()
317
313
}
@@ -397,7 +393,7 @@ def quantize_model(model, config):
397
393
"""
398
394
logging .info ("Starting GPTQ quantization process..." )
399
395
400
- # 1. Load ALL data needed from the generator/source in a single call.
396
+ # Load ALL data needed from the generator/source in a single call.
401
397
total_samples_to_request = config .nsamples
402
398
full_dataloader = get_dataloader (
403
399
config .tokenizer ,
@@ -406,7 +402,7 @@ def quantize_model(model, config):
406
402
nsamples = total_samples_to_request ,
407
403
)
408
404
409
- # 2. Split the materialized data. This works because full_dataloader
405
+ # Split the materialized data. This works because full_dataloader
410
406
# is now a NumPy array, which can be sliced and reused.
411
407
calibration_dataloader = full_dataloader [: config .nsamples ]
412
408
0 commit comments