Skip to content
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

Fixing quantization in eval recipe #1777

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions recipes/eleuther_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from torchtune.modules.tokenizers import ModelTokenizer
from torchtune.modules.transforms import Transform
from torchtune.recipe_interfaces import EvalRecipeInterface
from torchtune.training import FullModelTorchTuneCheckpointer

try:
import lm_eval
Expand Down Expand Up @@ -475,28 +476,34 @@ def setup(self, cfg: DictConfig) -> None:

# Load checkpoint
checkpointer = config.instantiate(cfg.checkpointer)
if quantization_mode is None:
ckpt_dict = checkpointer.load_checkpoint()
else:
# weights_only needs to be False when loading a quantized model
# currently loading a quantized model is only supported with the
# FullModelTorchTuneCheckpointer
ckpt_dict = checkpointer.load_checkpoint(weights_only=False)

# Initialize model
with training.set_default_dtype(self.dtype), self.device:
model = config.instantiate(cfg.model)

# Quantize model if requested
if quantization_mode is not None:
if not isinstance(checkpointer, FullModelTorchTuneCheckpointer):
raise ValueError(
"Quantization is only supported for models quantized and saved with the "
"FullModelTorchTuneCheckpointer - please ensure you have quantized your "
"model and are using the quantized weights!"
)
if "qat" in quantization_mode:
model = quantizer.prepare(model)
model = quantizer.quantize(model)
model = model.to(device=self.device, dtype=self.dtype)
for k, v in model_state_dict.items():
model_state_dict[k] = v.to(self._device)
model.load_state_dict(model_state_dict, assign=True)
ckpt_dict = checkpointer.load_checkpoint(weights_only=False)[
training.MODEL_KEY
]
for k, v in ckpt_dict.items():
ckpt_dict[k] = v.to(self.device)
model.load_state_dict(ckpt_dict, assign=True)
else:
ckpt_dict = checkpointer.load_checkpoint()[training.MODEL_KEY]
model.load_state_dict(ckpt_dict)

# Load model weights into initialized model
model.load_state_dict(ckpt_dict[training.MODEL_KEY])
self.logger.info(f"Model is initialized with precision {self.dtype}.")

# Put model in eval mode.
Expand Down
Loading