-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve documentation on how to export model from Colab #1657
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,39 @@ All notebooks are **beginner friendly**! Add your dataset, click "Run All", and | |
- This [continued pretraining notebook](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Mistral_v0.3_(7B)-CPT.ipynb) is for learning another language | ||
- Click [here](https://docs.unsloth.ai/) for detailed documentation for Unsloth. | ||
|
||
## Exporting Models from Colab to Local Machine | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we move this maybe to the docs or wiki instead? :) @shimmyshimmer can help on that! |
||
|
||
If you have fine-tuned a model in Colab and want to use it locally on your machine, follow these steps: | ||
|
||
1. **Save the Model in Colab**: Ensure you have saved the model in a format that can be easily downloaded. You can use the `unsloth_save_model` function to save the model in the desired format. | ||
|
||
2. **Connect to Google Drive**: Mount your Google Drive in Colab to save the model files there. This allows you to download the files to your local machine later. | ||
|
||
```python | ||
from google.colab import drive | ||
drive.mount('/content/drive') | ||
``` | ||
|
||
3. **Save Model to Google Drive**: Save the model files to a directory in your Google Drive. | ||
|
||
```python | ||
model.save_pretrained('/content/drive/MyDrive/your_model_directory') | ||
tokenizer.save_pretrained('/content/drive/MyDrive/your_model_directory') | ||
``` | ||
|
||
4. **Download Model Files**: After saving the model files to Google Drive, you can download them to your local machine. Go to your Google Drive, locate the model directory, and download the files. | ||
|
||
5. **Load Model Locally**: Once you have downloaded the model files to your local machine, you can load the model using the `from_pretrained` method. | ||
|
||
```python | ||
from transformers import AutoModel, AutoTokenizer | ||
|
||
model = AutoModel.from_pretrained('path_to_your_model_directory') | ||
tokenizer = AutoTokenizer.from_pretrained('path_to_your_model_directory') | ||
``` | ||
|
||
By following these steps, you can easily export a fine-tuned model from Colab and use it locally on your machine. | ||
|
||
## 🦥 Unsloth.ai News | ||
- 📣 NEW! Introducing [Reasoning](https://unsloth.ai/blog/r1-reasoning) in Unsloth. You can now reproduce DeepSeek-R1's "aha" moment with just 7GB VRAM. Transform Llama, Phi, Mistral etc. into reasoning LLMs! | ||
- 📣 NEW! [DeepSeek-R1](https://unsloth.ai/blog/deepseek-r1) - the most powerful open reasoning models with Llama & Qwen distillations. Run or fine-tune them now! More details: [unsloth.ai/blog/deepseek-r1](https://unsloth.ai/blog/deepseek-r1). All model uploads: [here](https://huggingface.co/collections/unsloth/deepseek-r1-all-versions-678e1c48f5d2fce87892ace5). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
"save_to_gguf", | ||
"patch_saving_functions", | ||
"create_huggingface_repo", | ||
"export_model_to_local", | ||
] | ||
|
||
# llama.cpp specific targets - all takes 90s. Below takes 60s | ||
|
@@ -1025,69 +1026,6 @@ def save_to_gguf( | |
) | ||
pass | ||
|
||
# Determine whether the system already has llama.cpp installed and the scripts are executable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsure why this is removed? |
||
quantize_location = get_executable(["llama-quantize", "quantize"]) | ||
convert_location = get_executable(["convert-hf-to-gguf.py", "convert_hf_to_gguf.py"]) | ||
|
||
error = 0 | ||
if quantize_location is not None and convert_location is not None: | ||
print("Unsloth: llama.cpp found in the system. We shall skip installation.") | ||
else: | ||
print("Unsloth: Installing llama.cpp. This might take 3 minutes...") | ||
if _run_installer is not None: | ||
_run_installer, IS_CMAKE = _run_installer | ||
|
||
error = _run_installer.wait() | ||
# Check if successful | ||
if error != 0: | ||
print(f"Unsloth: llama.cpp error code = {error}.") | ||
install_llama_cpp_old(-10) | ||
pass | ||
|
||
if IS_CMAKE: | ||
# CMAKE needs to do some extra steps | ||
print("Unsloth: CMAKE detected. Finalizing some steps for installation.") | ||
|
||
check = os.system("cp llama.cpp/build/bin/llama-* llama.cpp") | ||
if check != 0: raise RuntimeError("Failed compiling llama.cpp. Please report this ASAP!") | ||
check = os.system("rm -rf llama.cpp/build") | ||
if check != 0: raise RuntimeError("Failed compiling llama.cpp. Please report this ASAP!") | ||
pass | ||
else: | ||
error = 0 | ||
install_llama_cpp_blocking() | ||
pass | ||
|
||
# Careful llama.cpp/quantize changed to llama.cpp/llama-quantize | ||
# and llama.cpp/main changed to llama.cpp/llama-cli | ||
# See https://github.com/ggerganov/llama.cpp/pull/7809 | ||
quantize_location = None | ||
if os.path.exists("llama.cpp/quantize"): | ||
quantize_location = "llama.cpp/quantize" | ||
elif os.path.exists("llama.cpp/llama-quantize"): | ||
quantize_location = "llama.cpp/llama-quantize" | ||
else: | ||
raise RuntimeError( | ||
"Unsloth: The file 'llama.cpp/llama-quantize' or 'llama.cpp/quantize' does not exist.\n"\ | ||
"But we expect this file to exist! Maybe the llama.cpp developers changed the name?" | ||
) | ||
pass | ||
|
||
# See https://github.com/unslothai/unsloth/pull/730 | ||
# Filenames changed again! | ||
convert_location = None | ||
if os.path.exists("llama.cpp/convert-hf-to-gguf.py"): | ||
convert_location = "llama.cpp/convert-hf-to-gguf.py" | ||
elif os.path.exists("llama.cpp/convert_hf_to_gguf.py"): | ||
convert_location = "llama.cpp/convert_hf_to_gguf.py" | ||
else: | ||
raise RuntimeError( | ||
"Unsloth: The file 'llama.cpp/convert-hf-to-gguf.py' or 'llama.cpp/convert_hf_to_gguf.py' does not exist.\n"\ | ||
"But we expect this file to exist! Maybe the llama.cpp developers changed the name?" | ||
) | ||
pass | ||
pass | ||
|
||
# Determine maximum first_conversion state | ||
if first_conversion == "f32" : strength = 3 | ||
elif first_conversion == "f16" : strength = 2 | ||
|
@@ -1315,7 +1253,7 @@ def unsloth_push_to_hub_merged( | |
safe_serialization : bool = True, | ||
revision : str = None, | ||
commit_description : str = "Upload model trained with Unsloth 2x faster", | ||
tags : Optional[List[str]] = None, | ||
tags : List[str] = None, | ||
temporary_location : str = "_unsloth_temporary_saved_buffers", | ||
maximum_memory_usage : float = 0.75, | ||
): | ||
|
@@ -1823,6 +1761,8 @@ def unsloth_push_to_hub_gguf( | |
"q4_0" : "Original quant method, 4-bit.", | ||
"q4_1" : "Higher accuracy than q4_0 but not as high as q5_0. However has quicker inference than q5 models.", | ||
"q4_k_s" : "Uses Q4_K for all tensors", | ||
"q4_k" : "alias for q4_k_m", | ||
"q5_k" : "alias for q5_k_m", | ||
"q5_0" : "Higher accuracy, higher resource usage and slower inference.", | ||
"q5_1" : "Even higher accuracy, resource usage and slower inference.", | ||
"q5_k_s" : "Uses Q5_K for all tensors", | ||
|
@@ -2347,3 +2287,27 @@ def patch_saving_functions(model, vision = False): | |
pass | ||
return model | ||
pass | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok this is a reasonable function - we can probably just include this if that works :) |
||
def export_model_to_local(model, tokenizer, save_directory, drive_directory): | ||
""" | ||
Export a fine-tuned model from Colab to your local machine. | ||
|
||
Args: | ||
model: The fine-tuned model to be exported. | ||
tokenizer: The tokenizer associated with the model. | ||
save_directory: The directory where the model will be saved in Colab. | ||
drive_directory: The directory in Google Drive where the model will be saved. | ||
""" | ||
# Save the model in Colab | ||
model.save_pretrained(save_directory) | ||
tokenizer.save_pretrained(save_directory) | ||
|
||
# Mount Google Drive | ||
from google.colab import drive | ||
drive.mount('/content/drive') | ||
|
||
# Copy the model files to Google Drive | ||
import shutil | ||
shutil.copytree(save_directory, drive_directory) | ||
|
||
print(f"Model saved to {drive_directory} in Google Drive. You can now download it to your local machine.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably not necessary!