Skip to content

Commit 4cc4ec3

Browse files
committed
fix format
Signed-off-by: yiliu30 <yi4.liu@intel.com>
1 parent 9b8dda6 commit 4cc4ec3

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

examples/autoround/ddp/ddp_qwen3_example.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
#############################################################################
2-
# This script is adapted to use DDP functionality with AutoRound.
3-
# run this with `torchrun --nproc_per_node=2 ddp_qwen3_example.py`
4-
# or change nproc_per_node to your desired configuration
5-
#
6-
# Example usage:
7-
# torchrun --nproc_per_node=2 ddp_qwen3_example.py \
8-
# --model Qwen/Qwen3-8B \
9-
# --nsamples 128 \
10-
# --iters 100 \
11-
# --disable_torch_compile \
12-
# --deterministic
13-
#############################################################################
14-
1+
"""
2+
This script is adapted to use DDP functionality with AutoRound.
3+
run this with `torchrun --nproc_per_node=2 ddp_qwen3_example.py`
4+
or change nproc_per_node to your desired configuration
5+
6+
Example usage:
7+
torchrun --nproc_per_node=2 ddp_qwen3_example.py \
8+
--model Qwen/Qwen3-8B \
9+
--nsamples 128 \
10+
--iters 100 \
11+
--disable_torch_compile \
12+
--deterministic
13+
"""
1514
import argparse
1615
import os
1716

@@ -93,12 +92,12 @@ def config_deterministic():
9392

9493

9594
# Get aligned calibration dataset.
96-
from auto_round.calib_dataset import get_dataset
95+
from auto_round.calib_dataset import get_dataset # noqa: E402
9796

9897
# Note: Make sure model are loaded before importing auto-round related code.
9998
# This requirement will be lifted once switching to new release of auto-round which
10099
# includes below fix:
101-
from llmcompressor.modifiers.autoround import AutoRoundModifier
100+
from llmcompressor.modifiers.autoround import AutoRoundModifier # noqa: E402
102101

103102
ds = get_dataset(
104103
tokenizer=tokenizer,
@@ -131,27 +130,28 @@ def config_deterministic():
131130

132131
rank = dist.get_rank()
133132
logger.info(f"[Rank {rank}] Quantization completed")
134-
# Confirm generations of the quantized model look sane.
135-
logger.info("\n\n")
136-
logger.info("========== SAMPLE GENERATION ==============")
137-
dispatch_model(model)
138-
sample = tokenizer("Hello my name is", return_tensors="pt")
139-
sample = {key: value.to(model.device) for key, value in sample.items()}
140-
output = model.generate(**sample, max_new_tokens=100)
141-
logger.info(tokenizer.decode(output[0]))
142-
logger.info("==========================================\n\n")
143-
144-
logger.info("Saving...")
145-
# Save to disk compressed.
146-
SAVE_DIR = (
147-
model_id.rstrip("/").split("/")[-1]
148-
+ f"-{args.scheme}-AutoRound"
149-
+ f"-iters{args.iters}-nsamples{args.nsamples}"
150-
+ "-DDP"
151-
+ str(dist.get_world_size())
152-
)
153-
model.save_pretrained(SAVE_DIR, save_compressed=True)
154-
tokenizer.save_pretrained(SAVE_DIR)
155-
logger.info(f"Saved to {SAVE_DIR}")
133+
if rank == 0:
134+
# Confirm generations of the quantized model look sane.
135+
logger.info("\n\n")
136+
logger.info("========== SAMPLE GENERATION ==============")
137+
dispatch_model(model)
138+
sample = tokenizer("Hello my name is", return_tensors="pt")
139+
sample = {key: value.to(model.device) for key, value in sample.items()}
140+
output = model.generate(**sample, max_new_tokens=100)
141+
logger.info(tokenizer.decode(output[0]))
142+
logger.info("==========================================\n\n")
143+
144+
logger.info("Saving...")
145+
# Save to disk compressed.
146+
SAVE_DIR = (
147+
model_id.rstrip("/").split("/")[-1]
148+
+ f"-{args.scheme}-AutoRound"
149+
+ f"-iters{args.iters}-nsamples{args.nsamples}"
150+
+ "-DDP"
151+
+ str(dist.get_world_size())
152+
)
153+
model.save_pretrained(SAVE_DIR, save_compressed=True)
154+
tokenizer.save_pretrained(SAVE_DIR)
155+
logger.info(f"Saved to {SAVE_DIR}")
156156

157157
dist.destroy_process_group()

0 commit comments

Comments
 (0)