Skip to content

Commit

Permalink
LLM: Check model device type (#9092)
Browse files Browse the repository at this point in the history
* check model device
  • Loading branch information
leonardozcm authored Oct 9, 2023
1 parent 2c0c9fe commit edccfb2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/llm/src/bigdl/llm/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ def load_low_bit(model, model_path):
"model should be a instance of "
f"`torch.nn.Module`, but got {type(model)} at last.")
if low_bit:
invalidInputError(isinstance(model, torch.nn.Module),
"model should be an instance of `torch.nn.Module`, "
f"but got {type(model)} at last.")
invalidInputError(model.device.type in ('cpu', 'meta'),
"Expect model on device `cpu` or `meta`, "
f"but got device type {model.device.type}")
qtype = ggml_tensor_qtype[low_bit]
model = ggml_convert_low_bit(model, qtype=qtype, convert_shape_only=True)

Expand Down Expand Up @@ -173,6 +179,12 @@ def optimize_model(model, low_bit='sym_int4', optimize_llm=True):
invalidInputError(low_bit in ggml_tensor_qtype,
f"Unknown load_in_low_bit value: {low_bit}, expected:"
f" sym_int4, asym_int4, sym_int5, asym_int5 or sym_int8.")
invalidInputError(isinstance(model, torch.nn.Module),
"model should be an instance of "
f"`torch.nn.Module`, but got {type(model)} at last.")
invalidInputError(model.device.type == 'cpu',
"Expect model on device `cpu`, "
f"but got device type {model.device.type}")
qtype = ggml_tensor_qtype[low_bit]
model = ggml_convert_low_bit(model, qtype=qtype, optimize_model=optimize_llm)
# add save_low_bit to pretrained model dynamically
Expand Down

0 comments on commit edccfb2

Please sign in to comment.