Skip to content

Commit d596983

Browse files
committed
Add option for qcfg None
Signed-off-by: Andrea Fasoli <[email protected]>
1 parent ebdec64 commit d596983

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

fms_mo/utils/aiu_utils.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def recompute_weight_with_sawb(
188188
f"(std_min={weight_int_std_min:.1f} "
189189
f"-> {weight_int_sawb_std_min:.1f}) "
190190
f"and clips of {layer_name + '.weight'}"
191-
)
191+
)
192192
else:
193193
weight_int_sawb_as_fp_std = weight_int_sawb_as_fp.std()
194194
if verbose:
@@ -410,6 +410,13 @@ def save_sd_for_aiu(
410410
) -> None:
411411
"""Save model state dictionary after conversion for AIU compatibility."""
412412

413+
if qcfg is None:
414+
logger.info(
415+
"Attention: saving state dictionary without specifying a quantization "
416+
"configuration (qcfg) performs no recomputation for narrow weight "
417+
"distributions and assumes the weight quantizer used was per-tensor."
418+
)
419+
413420
converted_sd = convert_sd_for_aiu(
414421
model=model,
415422
recompute_narrow_weights=(
@@ -426,7 +433,7 @@ def save_sd_for_aiu(
426433

427434
def save_for_aiu(
428435
model: PreTrainedModel,
429-
qcfg: dict,
436+
qcfg: dict | None = None,
430437
output_dir: str | Path = "./",
431438
file_name: str | Path = "qmodel_for_aiu.pt",
432439
cfg_name: str | Path = "qcfg.json",
@@ -440,11 +447,18 @@ def save_for_aiu(
440447
weights presenting narrow distributions in the integer domain.
441448
The general qconfig_save function is used to save the quantization configuration.
442449
443-
Required arguments: model (quantized), qcfg
450+
Required arguments: model (quantized)
444451
"""
445452

446453
save_sd_for_aiu(model, qcfg, output_dir, file_name, verbose)
447454

455+
if qcfg is None:
456+
logger.info(
457+
"Quantization configuration was not provided. Only converted checkpoint is "
458+
"saved."
459+
)
460+
return
461+
448462
# enforce specific keys needed when reloading model for AIU
449463
qcfg["keys_to_save"] = [
450464
"qa_mode",
@@ -455,4 +469,9 @@ def save_for_aiu(
455469
"qskip_large_mag_layers",
456470
"recompute_narrow_weights",
457471
]
458-
qconfig_save(qcfg, recipe=recipe, minimal=True, fname=Path(output_dir) / cfg_name)
472+
qconfig_save(
473+
qcfg=qcfg,
474+
recipe=recipe,
475+
minimal=True,
476+
fname=str(Path(output_dir) / cfg_name), # only str is fname accepted type
477+
)

fms_mo/utils/qconfig_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,9 @@ def add_wanted_defaults_to_config(config: dict, minimal: bool = True) -> None:
539539

540540
def qconfig_save(
541541
qcfg: dict,
542-
recipe: str = None,
542+
recipe: str | None = None,
543543
minimal: bool = True,
544-
fname="qcfg.json",
544+
fname: str = "qcfg.json",
545545
) -> None:
546546
"""
547547
Try to save qcfg into a JSON file (or use .pt format if something really can't be text-only).

0 commit comments

Comments
 (0)