Make bitsandbytes optional on ROCm and add bf16 helper#4211
Make bitsandbytes optional on ROCm and add bf16 helper#4211danielhanchen wants to merge 4 commits into
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances Unsloth's robustness and compatibility, particularly for environments like ROCm where Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request makes bitsandbytes an optional dependency, enhancing compatibility with environments like ROCm. The changes primarily involve adding try-except blocks around bitsandbytes imports and conditionally handling its components. This is a solid approach. My main feedback across several files is to replace broad except Exception: clauses with the more specific except ImportError: for import-related errors. This improves code robustness by not masking other potential issues. The other fixes for GRPO padding and PEFT compatibility appear correct.
| vllm_version = importlib_version("vllm") | ||
| try: | ||
| vllm_version = importlib_version("vllm") | ||
| except Exception: |
There was a problem hiding this comment.
Catching a broad Exception can hide unexpected errors. It's better to catch a more specific exception. importlib.metadata.version raises importlib.metadata.PackageNotFoundError when package metadata is not found, which is a subclass of ImportError. Using except ImportError: would be more specific and safer here, and it doesn't require adding a new import.
| except Exception: | |
| except ImportError: |
| # https://github.com/bitsandbytes-foundation/bitsandbytes/pull/1330/files | ||
| HAS_CUDA_STREAM = Version(bnb.__version__) > Version("0.43.3") | ||
| get_ptr = bnb.functional.get_ptr | ||
| except Exception: |
There was a problem hiding this comment.
| import bitsandbytes as bnb | ||
| try: | ||
| import bitsandbytes as bnb | ||
| except Exception: |
| import peft.tuners.lora.bnb | ||
| try: | ||
| import peft.tuners.lora.bnb | ||
| except Exception as e: |
| try: | ||
| from bitsandbytes.nn import Linear4bit as Bnb_Linear4bit | ||
| except Exception: | ||
| Bnb_Linear4bit = None | ||
|
|
||
| try: | ||
| from peft.tuners.lora import Linear4bit as Peft_Linear4bit | ||
| except Exception: | ||
| Peft_Linear4bit = None |
There was a problem hiding this comment.
It's better to catch a more specific exception than the general Exception. For failed imports, ImportError is the correct exception to catch. This applies to both try-except blocks here.
| try: | |
| from bitsandbytes.nn import Linear4bit as Bnb_Linear4bit | |
| except Exception: | |
| Bnb_Linear4bit = None | |
| try: | |
| from peft.tuners.lora import Linear4bit as Peft_Linear4bit | |
| except Exception: | |
| Peft_Linear4bit = None | |
| try: | |
| from bitsandbytes.nn import Linear4bit as Bnb_Linear4bit | |
| except ImportError: | |
| Bnb_Linear4bit = None | |
| try: | |
| from peft.tuners.lora import Linear4bit as Peft_Linear4bit | |
| except ImportError: | |
| Peft_Linear4bit = None |
| except Exception: | ||
| arguments.pop("ensure_weight_tying", None) |
There was a problem hiding this comment.
Catching a broad Exception can hide unexpected errors. It's better to catch a more specific set of exceptions that you might expect from the introspection logic, such as ImportError, AttributeError, or TypeError. This makes the code more robust against unforeseen issues.
| except Exception: | |
| arguments.pop("ensure_weight_tying", None) | |
| except (ImportError, AttributeError, TypeError, ValueError): | |
| arguments.pop("ensure_weight_tying", None) |
| try: | ||
| from bitsandbytes.nn import Linear4bit as Bnb_Linear4bit | ||
| except Exception: | ||
| Bnb_Linear4bit = None | ||
|
|
||
| try: | ||
| from peft.tuners.lora import Linear4bit as Peft_Linear4bit | ||
| except Exception: | ||
| Peft_Linear4bit = None |
There was a problem hiding this comment.
It's better to catch a more specific exception than the general Exception. For failed imports, ImportError is the correct exception to catch. This applies to both try-except blocks here.
| try: | |
| from bitsandbytes.nn import Linear4bit as Bnb_Linear4bit | |
| except Exception: | |
| Bnb_Linear4bit = None | |
| try: | |
| from peft.tuners.lora import Linear4bit as Peft_Linear4bit | |
| except Exception: | |
| Peft_Linear4bit = None | |
| try: | |
| from bitsandbytes.nn import Linear4bit as Bnb_Linear4bit | |
| except ImportError: | |
| Bnb_Linear4bit = None | |
| try: | |
| from peft.tuners.lora import Linear4bit as Peft_Linear4bit | |
| except ImportError: | |
| Peft_Linear4bit = None |
d59efa4 to
e8d8974
Compare
This comment was marked as low quality.
This comment was marked as low quality.
# Conflicts: # unsloth/__init__.py # unsloth/models/_utils.py # unsloth/save.py
for more information, see https://pre-commit.ci
|
AMD Support has been added into Unsloth with Bitsandbytes support. |
Replacement for #4000 after the original PR was closed during the branch-history incident.
Summary
This makes Unsloth more robust on environments where bitsandbytes is not available (notably ROCm), while keeping CUDA/NVIDIA behavior unchanged when bitsandbytes is installed.
Changes:
Testing