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

Offloading to cpu and disk #77

Merged
merged 4 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
8 changes: 5 additions & 3 deletions awq/models/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ def from_pretrained(self, model_path, trust_remote_code=True, safetensors=False,
@classmethod
def from_quantized(self, quant_path, quant_filename='', max_new_tokens=None,
trust_remote_code=True, fuse_layers=True,
batch_size=1, safetensors=False) -> BaseAWQForCausalLM:
batch_size=1, safetensors=False,
max_memory=None, offload_folder=None) -> BaseAWQForCausalLM:
os.environ["AWQ_BATCH_SIZE"] = str(batch_size)
model_type = check_and_get_model_type(quant_path, trust_remote_code)

return AWQ_CAUSAL_LM_MODEL_MAP[model_type].from_quantized(
quant_path, model_type, quant_filename, max_new_tokens, trust_remote_code=trust_remote_code,
fuse_layers=fuse_layers, safetensors=safetensors
)
fuse_layers=fuse_layers, safetensors=safetensors,
max_memory=max_memory, offload_folder=offload_folder
)
28 changes: 23 additions & 5 deletions awq/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def from_pretrained(self, model_path, model_type, torch_dtype: torch.dtype = tor
def from_quantized(self, model_path, model_type, model_filename='',
max_new_tokens=None, torch_dtype=torch.float16,
trust_remote_code=True, safetensors=False, is_quantized=True,
fuse_layers=False, version='GEMM'):
fuse_layers=False, version='GEMM',
max_memory=None, offload_folder=None):
# [STEP 1-2] Load weights path and configs
model_weights_path, config, quant_config = self._load_config(
self, model_path, model_filename, safetensors, version,
Expand All @@ -153,21 +154,38 @@ def from_quantized(self, model_path, model_type, model_filename='',
device_map = infer_auto_device_map(
model,
no_split_module_classes=[self.layer_type],
max_memory=max_memory,
dtype=torch_dtype
)

# Load checkpoint
load_checkpoint_in_model(
model,
checkpoint=model_weights_path,
device_map=device_map
device_map=device_map,
offload_folder=offload_folder,
dtype=torch_dtype
)

# Dispath to devices
model = simple_dispatch_model(model, device_map)
if max_memory is None:
# VRAM only
model = simple_dispatch_model(model, device_map)

if fuse_layers:
self.fuse_layers(model, quant_config)
else:
if fuse_layers:
self.fuse_layers(model, quant_config)

# Offloading dispatch
from accelerate import dispatch_model
model = dispatch_model(
model,
device_map=device_map,
offload_dir=offload_folder
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use dispatch_model() instead if it works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. It works just fine. I've removed call to the simple_dispatch(). And fixed a minor bug in fused/attn.py


if fuse_layers:
self.fuse_layers(model, quant_config)

return self(model, model_type, is_quantized=is_quantized, quant_config=quant_config)

Expand Down