Conversation
for more information, see https://pre-commit.ci
Summary of ChangesHello @JINO-ROHIT, 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 expands Unsloth's hardware compatibility by integrating MLX support, primarily benefiting users on Apple Silicon devices. The changes enable the loading and fine-tuning of models using the MLX framework, providing an optimized experience for Mac users. This integration is achieved through new dedicated MLX model and trainer classes, alongside conditional logic to ensure seamless operation across different hardware platforms. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. 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 introduces MLX support to unsloth, enabling training on Apple Silicon. The changes are well-structured and mostly confined to conditional imports and a new mlx_model.py module. I've identified a few areas for improvement to enhance code clarity and reduce redundancy. Specifically, I've suggested removing a redundant conditional block in device_type.py, simplifying a dataclass-to-dictionary conversion, and removing a duplicate check for lora_config in the new MLX model file. Overall, this is a great addition to the library.
| elif DEVICE_TYPE_TORCH == "mps": | ||
| DEVICE_TYPE_TORCH = "mps" |
There was a problem hiding this comment.
| def to_dict(self) -> Dict[str, Any]: | ||
| return { | ||
| "adapter_file": self.adapter_file, | ||
| "max_seq_length": self.max_seq_length, | ||
| "grad_checkpoint": self.grad_checkpoint, | ||
| "grad_accumulation_steps": self.grad_accumulation_steps, | ||
| "iters": self.iters, | ||
| "batch_size": self.batch_size, | ||
| "val_batches": self.val_batches, | ||
| } |
There was a problem hiding this comment.
The to_dict method can be simplified by using dataclasses.asdict. This will make the code more concise and less prone to errors if new fields are added to the dataclass, as you won't need to update this method manually.
To use it, you'll also need to update the import on line 4 to:
from dataclasses import dataclass, asdict def to_dict(self) -> Dict[str, Any]:
return asdict(self)| trainer.prepare_model_for_training(model, lora_config) | ||
|
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c623f4cd3d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| elif DEVICE_TYPE != "mps": | ||
| from .models import * | ||
| from .models import __version__ |
There was a problem hiding this comment.
Base modules never imported on CUDA/HIP/XPU
The core imports are now guarded by elif DEVICE_TYPE != "mps":, but that condition is part of the same if/elif chain as the preceding CUDA/HIP/XPU branches. Because one of those earlier branches always matches on supported GPUs, this block never executes, so import unsloth no longer brings in FastLanguageModel, __version__, trainer/chat helpers, or the TRL patch on any CUDA/ROCm/Intel system—the chain exits before reaching these imports—leading to AttributeError/missing functionality for all existing users.
Useful? React with 👍 / 👎.
|
Thank you so much for your PR we'll take a look asap! :) |
| ) | ||
| raise NotImplementedError( | ||
| "Unsloth currently only works on NVIDIA, AMD and Intel GPUs." | ||
| "Unsloth currently only works on NVIDIA, AMD, Intel GPUs, MAC Silicon and MLX." |
There was a problem hiding this comment.
Shouldn't that be M-Series Apple Silicon, Apple Macintosh Silicon or Apple Mac Silicon? I don't think it's ever branded "MAC".
|
hello @shimmyshimmer sorry did you get some time to review this? id love to iterate on feedbacks and push this PR forward |
While the PR itself is fantastic (thank you!!), there arent any optimizations at the moment, were discussing whether we should proceed as is or, spend more time on optimizations |
|
Being only an interested consumer of Unsloth models I might be wrong, but to my knowledge the fine-tuning performance gains are also heavily related to the custom Triton kernels. Shouldn’t the optimization strategies behind those kernels be reimplemented for MLX when possible? (I understand Triton kernels can’t be directly ported since they compile to CUDA PTX, but the underlying approaches like fused attention and memory-efficient backward passes could potentially be implemented using MLX’s Metal primitives.) |
of course, ill wait to hear on further updates |
i think mlx should already have its own set of pre-existing optimizations, but sure we could also possibly look into further improvements, although im not quite sure how difficult/easy itd be to make new metal kernels and integrate within. |
At least should be easier now https://ml-explore.github.io/mlx/build/html/dev/custom_metal_kernels.html |
|
sure, i meant the overall complexity of metal programming + wrtiting optimized kernels on top of it |
|
@JINO-ROHIT can you please upload the changes you made in unsloth_zoo in order to run this. |
|
sure ill do it |
hello everyone!
this PR aims to integrate mlx support in unsloth with minimal changes.
im attaching below a sample alpaca training run script to get this working.