Skip to content

Xformers and other dependency errors #38

Closed

Description

@alvis233

I am attempting to replicate the Mistral 7b model finetuning locally as outlined below Unsloth Open column in the README.md file. I have successfully downloaded the dataset and the model to my local machine, which is a deviation from the original jupyter notebook that used data directly from the cloud.

model_name = "./Mistral-7B-v0.1"
...
dataset = load_dataset(path = "./SlimOrca", split = "train")

The initial stages of the Jupyter notebook, which include multiple code blocks, were executed without any issues. However, I have run into an error during the execution of the trainer_stats = trainer.train() block. The issue arises with a ValueError stating that the Query/Key/Value must have either BMHK or BMK shape.

ValueError: Query/Key/Value should all have BMHK or BMK shape.
  query.shape: torch.Size([4, 2021, 8, 4, 128])
  key.shape  : torch.Size([4, 2021, 8, 4, 128])
  value.shape: torch.Size([4, 2021, 8, 4, 128])

I have even tried reverting back to using the HuggingFace dataset instead of the local one to see if the issue was related to my dataset location. Unfortunately, I encountered the same error.
Here’s a brief overview of my setup:

==((====))==  Unsloth: Fast Mistral patching release 2023.12
   \\   [/](https://file+.vscode-resource.vscode-cdn.net/)|    GPU: NVIDIA GeForce RTX 4090. Max memory: 23.647 GB
O^O/ \_/ \    CUDA compute capability = 8.9
\        [/](https://file+.vscode-resource.vscode-cdn.net/)    Pytorch version: 2.1.0.post300. CUDA Toolkit = 11.8
 "-____-"     bfloat16 support = TRUE

GPU = NVIDIA GeForce RTX 4090. Max memory = 23.647 GB.
4.66 GB of memory reserved.

Additionally, I have addressed an issue with the torch version format. The original Unsloth configuration didn't support version numbers with four segments, so I modified the version parsing by changing from major_torch, minor_torch, _ = torch.version.split(".") to major_torch, minor_torch, _ = torch.__version__.split(".")[0:3] to accommodate the expected format.

Would anyone be able to point me in the right direction, or has anyone experienced a similar issue while working on model finetuning replication? Your help would be greatly appreciated as this error is currently standing in the way of my progress.

Thank you in advance for any assistance!

Activity

  1. danielhanchen commented on Dec 17, 2023

    @danielhanchen
    Member

    @alvis233 Hey! Thanks for trying out Unsloth!

    1. Fair point on the torch.version issue - I'll add a fix
    2. I'm assuming u used pip install "unsloth[cu118] @ git+https://github.com/unslothai/unsloth.git" and not pip install "unsloth[cu118_ampere] @ git+https://github.com/unslothai/unsloth.git". Since you have a RTX 4090, the ampere path can speed things up even more.
    3. I'll run the Mistral notebook again via a A100, and report back if I see if there are any issues.
    4. One possible fix is maybe to try to reinstall Unsloth
  2. changed the title [-]Issues Encountered While Attempting to Replicate Mistral 7b Model Finetuning Locally[/-] [+]ValueError: Query/Key/Value should all have BMHK or BMK shape for Mistral 7b[/+] on Dec 17, 2023
  3. danielhanchen commented on Dec 17, 2023

    @danielhanchen
    Member

    @alvis233 I just ran the Mistral 7b notebook on a A100 and using your install path ie cu118 and not cu118_ampere.

    The trainer seems to be running fine:
    image

    Are you running the A100 Mistral 7b notebook? https://colab.research.google.com/drive/1SKrKGV-BZoU4kv5q3g0jtE_OhRgPtrrQ?usp=sharing

  4. danielhanchen commented on Dec 17, 2023

    @danielhanchen
    Member
  5. alvis233 commented on Dec 17, 2023

    @alvis233
    Author

    @alvis233 I just ran the Mistral 7b notebook on a A100 and using your install path ie cu118 and not cu118_ampere.

    The trainer seems to be running fine: image

    Are you running the A100 Mistral 7b notebook? https://colab.research.google.com/drive/1SKrKGV-BZoU4kv5q3g0jtE_OhRgPtrrQ?usp=sharing

    Yes, I am using the notebook from the exact link that you provided. To rule out any issues with other changes that I might have made, I will reinstall it and try again.

  6. alvis233 commented on Dec 17, 2023

    @alvis233
    Author

    @alvis233 I just ran the Mistral 7b notebook on a A100 and using your install path ie cu118 and not cu118_ampere.
    The trainer seems to be running fine: image
    Are you running the A100 Mistral 7b notebook? https://colab.research.google.com/drive/1SKrKGV-BZoU4kv5q3g0jtE_OhRgPtrrQ?usp=sharing

    Yes, I am using the notebook from the exact link that you provided. To rule out any issues with other changes that I might have made, I will reinstall it and try again.

    By the way, below are the complete error messages:

    ***** Running training *****
      Num examples = 517,982
      Num Epochs = 1
      Instantaneous batch size per device = 4
      Total train batch size (w. parallel, distributed & accumulation) = 16
      Gradient Accumulation steps = 4
      Total optimization steps = 240
      Number of trainable parameters = 41,943,040
    
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    Cell In[8], [line 1](vscode-notebook-cell:?execution_count=8&line=1)
    ----> [1](vscode-notebook-cell:?execution_count=8&line=1) trainer_stats = trainer.train()
    
    File [~/miniconda3/lib/python3.11/site-packages/trl/trainer/sft_trainer.py:280](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/trl/trainer/sft_trainer.py:280), in SFTTrainer.train(self, *args, **kwargs)
        [277](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/trl/trainer/sft_trainer.py:277) if self.neftune_noise_alpha is not None and not self._trainer_supports_neftune:
        [278](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/trl/trainer/sft_trainer.py:278)     self.model = self._trl_activate_neftune(self.model)
    --> [280](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/trl/trainer/sft_trainer.py:280) output = super().train(*args, **kwargs)
        [282](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/trl/trainer/sft_trainer.py:282) # After training we make sure to retrieve back the original forward pass method
        [283](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/trl/trainer/sft_trainer.py:283) # for the embedding layer by removing the forward post hook.
        [284](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/trl/trainer/sft_trainer.py:284) if self.neftune_noise_alpha is not None and not self._trainer_supports_neftune:
    
    File [~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1537](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1537), in Trainer.train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)
       [1535](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1535)         hf_hub_utils.enable_progress_bars()
       [1536](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1536) else:
    -> [1537](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1537)     return inner_training_loop(
       [1538](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1538)         args=args,
       [1539](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1539)         resume_from_checkpoint=resume_from_checkpoint,
       [1540](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1540)         trial=trial,
       [1541](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1541)         ignore_keys_for_eval=ignore_keys_for_eval,
       [1542](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1542)     )
    
    File [~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1854](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1854), in Trainer._inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
       [1851](https://file+.vscode-resource.vscode-cdn.net/home/alvis/Workspace/~/miniconda3/lib/python3.11/site-packages/transformers/trainer.py:1851)     self.control = self.callback_handler.on_step_begin(args, self.state, self.control)
    ...
    
    ValueError: Query/Key/Value should all have BMHK or BMK shape.
      query.shape: torch.Size([4, 2021, 8, 4, 128])
      key.shape  : torch.Size([4, 2021, 8, 4, 128])
      value.shape: torch.Size([4, 2021, 8, 4, 128])
    

    I would appreciate it if you could take a closer look.

  7. danielhanchen commented on Dec 17, 2023

    @danielhanchen
    Member

    @alvis233 Wait I just noticed - this is a Windows machine?

  8. alvis233 commented on Dec 17, 2023

    @alvis233
    Author

    After reinstalling UnSloth, the issue persists. Additionally, I found that NVCC is required for installation when attempting to install UnSloth from the Ampere path, as indicated by pip. However, my CUDA environment was set up through conda, which only provides minimal support without NVCC. Consequently, I followed the 'Installation Instructions' section in the README.md to reinstall UnSloth. I'm uncertain if the absence of NVCC is contributing to this issue.

  9. alvis233 commented on Dec 17, 2023

    @alvis233
    Author

    @alvis233 Wait I just noticed - this is a Windows machine?

    No, this is an Ubuntu system, running kernel version Linux 6.2.0-39-generic 22.04.1-Ubuntu x86_64 GNU/Linux

  10. danielhanchen commented on Dec 17, 2023

    @danielhanchen
    Member

    @alvis233 Ok my bad - I just get confused when I see vscode :))

  11. danielhanchen commented on Dec 17, 2023

    @danielhanchen
    Member

    For Conda - did u follow the conda install path and not the pip?

  12. alvis233 commented on Dec 17, 2023

    @alvis233
    Author

    For Conda - did u follow the conda install path and not the pip?

    I installed the unsloth by pip install "unsloth[kaggle] @ git+https://github.com/unslothai/unsloth.git" as described under Installation Instructions - Conda section in the README.md file. All previous code blocks in the notebook, which involve importing and initializing UnSloth, have executed smoothly. The nvidia-smi command also runs smoothly, indicating that the GPU and drivers are operational.

  13. danielhanchen commented on Dec 17, 2023

    @danielhanchen
    Member

    @alvis233 Including conda install cudatoolkit xformers bitsandbytes pytorch pytorch-cuda=12.1 \ -c pytorch -c nvidia -c xformers -c conda-forge -y right?

    Hmm this sounds like a complex case - your error message is very weird since I can't seem to glean anything out of it

  14. danielhanchen commented on Dec 17, 2023

    @danielhanchen
    Member

    ACTUALLY @alvis233 can you run:
    python -m xformers.info
    in a terminal and print it out here :)

  15. alvis233 commented on Dec 17, 2023

    @alvis233
    Author

    ACTUALLY @alvis233 can you run: python -m xformers.info in a terminal and print it out here :)

    I have executed the command as you suggested, and here is the output:

    xFormers 0.0.21
    memory_efficient_attention.cutlassF:               available
    memory_efficient_attention.cutlassB:               available
    memory_efficient_attention.decoderF:               available
    memory_efficient_attention.flshattF@884e385:       available
    memory_efficient_attention.flshattB@884e385:       available
    memory_efficient_attention.smallkF:                available
    memory_efficient_attention.smallkB:                available
    memory_efficient_attention.tritonflashattF:        unavailable
    memory_efficient_attention.tritonflashattB:        unavailable
    indexing.scaled_index_addF:                        available
    indexing.scaled_index_addB:                        available
    indexing.index_select:                             available
    swiglu.dual_gemm_silu:                             available
    swiglu.gemm_fused_operand_sum:                     available
    swiglu.fused.p.cpp:                                available
    is_triton_available:                               True
    is_functorch_available:                            False
    pytorch.version:                                   2.1.0.post300
    pytorch.cuda:                                      available
    gpu.compute_capability:                            8.9
    gpu.name:                                          NVIDIA GeForce RTX 4090
    build.info:                                        available
    build.cuda_version:                                1102
    build.python_version:                              3.11.6
    build.torch_version:                               2.1.0.post300
    build.env.TORCH_CUDA_ARCH_LIST:                    3.5+PTX;6.0;7.0;8.0+PTX
    build.env.XFORMERS_BUILD_TYPE:                     None
    build.env.XFORMERS_ENABLE_DEBUG_ASSERTIONS:        None
    build.env.NVCC_FLAGS:                              None
    build.env.XFORMERS_PACKAGE_FROM:                   None
    build.nvcc_version:                                11.2.152
    source.privacy:                                    open source
    
    
  16. danielhanchen commented on Dec 17, 2023

    @danielhanchen
    Member

    OHHH ur xformers is an old version!! version 0.22 was when group query attention was added - hence the error message!

  17. danielhanchen commented on Dec 17, 2023

    @danielhanchen
    Member

    I think if you upgrade xformers to the latest version, or any version above 0.0.22, Unsloth should work!

  18. alvis233 commented on Dec 17, 2023

    @alvis233
    Author

    I think if you upgrade xformers to the latest version, or any version above 0.0.22, Unsloth should work!

    I sincerely appreciate the time and effort you have invested in helping me troubleshoot this issue. It has come to my attention that newer versions of xformers do not support Python 3.11. Unfortunately, downgrading the Python version isn't straightforward for me either due to complex dependencies in my current environment.

    I've encountered another issue while trying to set up a new environment with python=3.9; I am getting a SyntaxError complaint from pip during the installation of unsloth: torch_version = = torch.version.split('.') SyntaxError: invalid syntax, which seems like it could result from a WIP commit(but I'm not sure).

    It looks like I need to allocate some time to resolve these cascading issues. I plan to make another attempt when I can find a suitable time window, and I will keep this issue updated with any progress.

    Thank you once again for your kind assistance and patience.

  19. danielhanchen commented on Dec 18, 2023

    @danielhanchen
    Member

    @alvis233 Oops just fixed the == issue - thanks for spotting it. Hmm I can check Python 3.11 support - I'll have to get back to you

  20. danielhanchen commented on Dec 18, 2023

    @danielhanchen
    Member

    @alvis233 I made a new branch just for xformers-0.21! It should now be supported! :)

    pip install "unsloth[cu118] @ git+https://github.com/unslothai/[email protected]"

    Your other choice is to install flash-attn via the ampere install path:
    pip install "unsloth[cu118_ampere] @ git+https://github.com/unslothai/unsloth.git

    Select either - maybe option 1 is easier!!

    Hope they help!

  21. alvis233 commented on Dec 18, 2023

    @alvis233
    Author

    Thank you for your help with the installation issues, I managed to install unsloth successfully now, but then I encountered another issue related to bitsandbytes. I noticed that the version of bitsandbytes was 0.41.0, so I reinstalled it, upgrading to version 0.41.3, which resolved the issue. Now everything is working fine. The Python dependencies are quite complex and frustrating. Why not provide a requirements.txt file to simplify the setup process?

  22. danielhanchen commented on Dec 18, 2023

    @danielhanchen
    Member

    @alvis233 Great you got it to work! So in fact I have an elaborate dispatching mechanism via pyproject.toml which is the "new" requirements.txt". The issue is if and else statements to dispatch to which is not allowed as of yet - that would require a setup.py`.

    Generally the setup process should be smooth, but i guess sometimes not so much.

    But glady it works now!! Hopefully Unsloth will make training and everything faster!! If you need any help - join our Discord!! https://discord.gg/u54VK8m8tk

    Again hope I was helpful!! (Closing this issue now!)

  23. changed the title [-]ValueError: Query/Key/Value should all have BMHK or BMK shape for Mistral 7b[/-] [+]Xformers and other dependency errors[/+] on Dec 18, 2023
  24. added a commit that references this issue on Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions