mlx-examples icon indicating copy to clipboard operation
mlx-examples copied to clipboard

Quantization causing tensor shape mismatch

Open alphrc opened this issue 2 years ago • 1 comments

After quantization, I can run the model using mlx_lm.generate --model <model> --prompt <prompt>, but I cannot run it with script as there would be an error of tensor shape mismatch:

(base) MBP Dev % python src/test.py
Loading checkpoint shards:   0%|                                                                                                                          | 0/4 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "/Users/rainchong/Desktop/Projects/LiLM/Dev/src/test.py", line 172, in <module>
    test()
  File "/Users/rainchong/Desktop/Projects/LiLM/Dev/src/test.py", line 125, in test
    lilm = LiLM(pretrained_model_name_or_path)
  File "/Users/rainchong/Desktop/Projects/LiLM/Dev/src/test.py", line 23, in __init__
    self.model = AutoModelForCausalLM.from_pretrained(model_name_or_path, torch_dtype="auto", device_map="auto")
  File "/Users/rainchong/Library/Python/3.9/lib/python/site-packages/transformers/models/auto/auto_factory.py", line 563, in from_pretrained
    return model_class.from_pretrained(
  File "/Users/rainchong/Library/Python/3.9/lib/python/site-packages/transformers/modeling_utils.py", line 3754, in from_pretrained
    ) = cls._load_pretrained_model(
  File "/Users/rainchong/Library/Python/3.9/lib/python/site-packages/transformers/modeling_utils.py", line 4214, in _load_pretrained_model
    new_error_msgs, offload_index, state_dict_index = _load_state_dict_into_meta_model(
  File "/Users/rainchong/Library/Python/3.9/lib/python/site-packages/transformers/modeling_utils.py", line 887, in _load_state_dict_into_meta_model
    set_module_tensor_to_device(model, param_name, param_device, **set_module_kwargs)
  File "/Users/rainchong/Library/Python/3.9/lib/python/site-packages/accelerate/utils/modeling.py", line 358, in set_module_tensor_to_device
    raise ValueError(
ValueError: Trying to set a tensor of shape torch.Size([152064, 640]) in "weight" (which has shape torch.Size([152064, 5120])), this look incorrect.

The code runs perfectly fine before quantization. The quantized model is saved at mlx/lora_fused_model-4Bit.

alphrc avatar Jul 06 '24 02:07 alphrc

You can't run an MLX quantized model with Hugging Face Transformers unfortunately.

awni avatar Jul 06 '24 18:07 awni