From e2f1d8461984e73be5d3035ff821a64f97747550 Mon Sep 17 00:00:00 2001 From: Borch <52267868+Leoleojames1@users.noreply.github.com> Date: Mon, 16 Sep 2024 00:48:34 -0500 Subject: [PATCH 1/6] Add files via upload --- unsloth-cli-2.py | 684 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 684 insertions(+) create mode 100644 unsloth-cli-2.py diff --git a/unsloth-cli-2.py b/unsloth-cli-2.py new file mode 100644 index 00000000..883cfd37 --- /dev/null +++ b/unsloth-cli-2.py @@ -0,0 +1,684 @@ +#!/usr/bin/env python3 + +""" +πŸ¦₯ Enhanced Script for Fine-Tuning, Merging, and Managing Language Models with Unsloth + +This script significantly extends the original unsloth-cli.py with a wide range of advanced features: + +- Comprehensive training pipeline with validation and testing support +- Advanced error handling and fallback options for robust model loading +- Merging functionality for LoRA adapters with dequantization options +- Flexible quantization and precision control (4-bit, 16-bit, 32-bit) +- Support for custom datasets and data formats (JSON, Parquet) +- Integration with Hugging Face models and push-to-hub functionality +- GGUF conversion for optimized model deployment +- Enhanced logging and progress tracking + +Key Features: + +1. Flexible Data Handling: + - Support for Parquet, JSON, and other data formats + - Custom data parsing and processing pipelines + +2. Advanced Model Management: + - Load and save models in various formats (Hugging Face, GGUF) + - Quantization options for memory-efficient training and inference + - Dequantization capabilities for precision-sensitive operations + +3. Comprehensive Training Pipeline: + - Support for train, validation, and test datasets + - Customizable training parameters (batch size, learning rate, etc.) + - Integration with popular optimization techniques (LoRA, gradient checkpointing) + +4. Merging and Adaptation: + - Merge LoRA adapters with base models + - Dequantization options for merging quantized models + +5. Deployment and Sharing: + - GGUF conversion for optimized model deployment + - Direct integration with Hugging Face Hub for easy model sharing + +6. Robust Error Handling and Logging: + - Detailed error messages and logging for easier debugging + - Fallback options for model loading and processing + +Usage example for training: + python unsloth-cli-2.py train --model_name "your_model_path" --train_dataset "train.parquet" \ + --validation_dataset "val.parquet" --test_dataset "test.parquet" \ + --max_seq_length 2048 --load_in_4bit \ + --per_device_train_batch_size 4 --gradient_accumulation_steps 8 \ + --max_steps 1000 --learning_rate 2e-5 --output_dir "outputs" \ + --save_model --save_path "model" --quantization "q4_k_m" \ + --push_to_hub --hub_path "your_hub/model" --hub_token "your_token" + +Usage example for merging: + python unsloth-cli-2.py merge --base_model_path "path/to/base/model" \ + --adapter_path "path/to/adapter" --output_path "path/to/output" \ + --dequantize f16 + +Usage example with dequantization: + python unsloth-cli-2.py train --model_name "your_model_path" --train_dataset "train.parquet" \ + --load_in_4bit --dequantize + +Dequantization Feature: +The --dequantize option allows you to convert quantized weights back to full precision +after loading the model. This can be useful when you want to fine-tune or use a +previously quantized model in full precision. However, please note: + +1. Dequantization does not recover information lost during the initial quantization. + The quality of the model may still be lower than if it was originally trained in + full precision. + +2. Dequantizing increases memory usage significantly, as it converts weights to + full precision (typically float32). + +3. This option is most useful when you need to perform operations that require + full precision weights but want to start from a quantized model. + +To see a full list of configurable options, use: + python unsloth-cli-2.py train --help + python unsloth-cli-2.py merge --help + +Happy fine-tuning, merging, and deploying with Unsloth! πŸ¦₯πŸš€ +""" + +import argparse +import logging +import os +import torch +import json +import struct +from unsloth import FastLanguageModel +from unsloth import is_bfloat16_supported +from datasets import load_dataset, DatasetDict +from peft import PeftModel +from trl import SFTTrainer +from transformers import TrainingArguments, AutoModelForCausalLM, AutoTokenizer +from safetensors import safe_open + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +def analyze_safetensors_header(file_path): + try: + with open(file_path, 'rb') as f: + header_size_bytes = f.read(8) + header_size = struct.unpack(' Date: Mon, 16 Sep 2024 00:48:53 -0500 Subject: [PATCH 2/6] Delete unsloth-cli-2.py --- unsloth-cli-2.py | 684 ----------------------------------------------- 1 file changed, 684 deletions(-) delete mode 100644 unsloth-cli-2.py diff --git a/unsloth-cli-2.py b/unsloth-cli-2.py deleted file mode 100644 index 883cfd37..00000000 --- a/unsloth-cli-2.py +++ /dev/null @@ -1,684 +0,0 @@ -#!/usr/bin/env python3 - -""" -πŸ¦₯ Enhanced Script for Fine-Tuning, Merging, and Managing Language Models with Unsloth - -This script significantly extends the original unsloth-cli.py with a wide range of advanced features: - -- Comprehensive training pipeline with validation and testing support -- Advanced error handling and fallback options for robust model loading -- Merging functionality for LoRA adapters with dequantization options -- Flexible quantization and precision control (4-bit, 16-bit, 32-bit) -- Support for custom datasets and data formats (JSON, Parquet) -- Integration with Hugging Face models and push-to-hub functionality -- GGUF conversion for optimized model deployment -- Enhanced logging and progress tracking - -Key Features: - -1. Flexible Data Handling: - - Support for Parquet, JSON, and other data formats - - Custom data parsing and processing pipelines - -2. Advanced Model Management: - - Load and save models in various formats (Hugging Face, GGUF) - - Quantization options for memory-efficient training and inference - - Dequantization capabilities for precision-sensitive operations - -3. Comprehensive Training Pipeline: - - Support for train, validation, and test datasets - - Customizable training parameters (batch size, learning rate, etc.) - - Integration with popular optimization techniques (LoRA, gradient checkpointing) - -4. Merging and Adaptation: - - Merge LoRA adapters with base models - - Dequantization options for merging quantized models - -5. Deployment and Sharing: - - GGUF conversion for optimized model deployment - - Direct integration with Hugging Face Hub for easy model sharing - -6. Robust Error Handling and Logging: - - Detailed error messages and logging for easier debugging - - Fallback options for model loading and processing - -Usage example for training: - python unsloth-cli-2.py train --model_name "your_model_path" --train_dataset "train.parquet" \ - --validation_dataset "val.parquet" --test_dataset "test.parquet" \ - --max_seq_length 2048 --load_in_4bit \ - --per_device_train_batch_size 4 --gradient_accumulation_steps 8 \ - --max_steps 1000 --learning_rate 2e-5 --output_dir "outputs" \ - --save_model --save_path "model" --quantization "q4_k_m" \ - --push_to_hub --hub_path "your_hub/model" --hub_token "your_token" - -Usage example for merging: - python unsloth-cli-2.py merge --base_model_path "path/to/base/model" \ - --adapter_path "path/to/adapter" --output_path "path/to/output" \ - --dequantize f16 - -Usage example with dequantization: - python unsloth-cli-2.py train --model_name "your_model_path" --train_dataset "train.parquet" \ - --load_in_4bit --dequantize - -Dequantization Feature: -The --dequantize option allows you to convert quantized weights back to full precision -after loading the model. This can be useful when you want to fine-tune or use a -previously quantized model in full precision. However, please note: - -1. Dequantization does not recover information lost during the initial quantization. - The quality of the model may still be lower than if it was originally trained in - full precision. - -2. Dequantizing increases memory usage significantly, as it converts weights to - full precision (typically float32). - -3. This option is most useful when you need to perform operations that require - full precision weights but want to start from a quantized model. - -To see a full list of configurable options, use: - python unsloth-cli-2.py train --help - python unsloth-cli-2.py merge --help - -Happy fine-tuning, merging, and deploying with Unsloth! πŸ¦₯πŸš€ -""" - -import argparse -import logging -import os -import torch -import json -import struct -from unsloth import FastLanguageModel -from unsloth import is_bfloat16_supported -from datasets import load_dataset, DatasetDict -from peft import PeftModel -from trl import SFTTrainer -from transformers import TrainingArguments, AutoModelForCausalLM, AutoTokenizer -from safetensors import safe_open - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - -def analyze_safetensors_header(file_path): - try: - with open(file_path, 'rb') as f: - header_size_bytes = f.read(8) - header_size = struct.unpack(' Date: Mon, 16 Sep 2024 00:51:43 -0500 Subject: [PATCH 3/6] unsloth-cli.py enhancements unsloth-cli.py enhanced feature set, via unsloth-cli-2.py, the scope of the feature set includes: - Comprehensive training pipeline with validation and testing support - Advanced error handling and fallback options for robust model loading - Merging functionality for LoRA adapters with dequantization options - Flexible quantization and precision control (4-bit, 16-bit, 32-bit) - Support for custom datasets and data formats (JSON, Parquet) - Integration with Hugging Face models and push-to-hub functionality - GGUF conversion for optimized model deployment - Enhanced logging and progress tracking --- unsloth-cli-2.py | 684 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 684 insertions(+) create mode 100644 unsloth-cli-2.py diff --git a/unsloth-cli-2.py b/unsloth-cli-2.py new file mode 100644 index 00000000..883cfd37 --- /dev/null +++ b/unsloth-cli-2.py @@ -0,0 +1,684 @@ +#!/usr/bin/env python3 + +""" +πŸ¦₯ Enhanced Script for Fine-Tuning, Merging, and Managing Language Models with Unsloth + +This script significantly extends the original unsloth-cli.py with a wide range of advanced features: + +- Comprehensive training pipeline with validation and testing support +- Advanced error handling and fallback options for robust model loading +- Merging functionality for LoRA adapters with dequantization options +- Flexible quantization and precision control (4-bit, 16-bit, 32-bit) +- Support for custom datasets and data formats (JSON, Parquet) +- Integration with Hugging Face models and push-to-hub functionality +- GGUF conversion for optimized model deployment +- Enhanced logging and progress tracking + +Key Features: + +1. Flexible Data Handling: + - Support for Parquet, JSON, and other data formats + - Custom data parsing and processing pipelines + +2. Advanced Model Management: + - Load and save models in various formats (Hugging Face, GGUF) + - Quantization options for memory-efficient training and inference + - Dequantization capabilities for precision-sensitive operations + +3. Comprehensive Training Pipeline: + - Support for train, validation, and test datasets + - Customizable training parameters (batch size, learning rate, etc.) + - Integration with popular optimization techniques (LoRA, gradient checkpointing) + +4. Merging and Adaptation: + - Merge LoRA adapters with base models + - Dequantization options for merging quantized models + +5. Deployment and Sharing: + - GGUF conversion for optimized model deployment + - Direct integration with Hugging Face Hub for easy model sharing + +6. Robust Error Handling and Logging: + - Detailed error messages and logging for easier debugging + - Fallback options for model loading and processing + +Usage example for training: + python unsloth-cli-2.py train --model_name "your_model_path" --train_dataset "train.parquet" \ + --validation_dataset "val.parquet" --test_dataset "test.parquet" \ + --max_seq_length 2048 --load_in_4bit \ + --per_device_train_batch_size 4 --gradient_accumulation_steps 8 \ + --max_steps 1000 --learning_rate 2e-5 --output_dir "outputs" \ + --save_model --save_path "model" --quantization "q4_k_m" \ + --push_to_hub --hub_path "your_hub/model" --hub_token "your_token" + +Usage example for merging: + python unsloth-cli-2.py merge --base_model_path "path/to/base/model" \ + --adapter_path "path/to/adapter" --output_path "path/to/output" \ + --dequantize f16 + +Usage example with dequantization: + python unsloth-cli-2.py train --model_name "your_model_path" --train_dataset "train.parquet" \ + --load_in_4bit --dequantize + +Dequantization Feature: +The --dequantize option allows you to convert quantized weights back to full precision +after loading the model. This can be useful when you want to fine-tune or use a +previously quantized model in full precision. However, please note: + +1. Dequantization does not recover information lost during the initial quantization. + The quality of the model may still be lower than if it was originally trained in + full precision. + +2. Dequantizing increases memory usage significantly, as it converts weights to + full precision (typically float32). + +3. This option is most useful when you need to perform operations that require + full precision weights but want to start from a quantized model. + +To see a full list of configurable options, use: + python unsloth-cli-2.py train --help + python unsloth-cli-2.py merge --help + +Happy fine-tuning, merging, and deploying with Unsloth! πŸ¦₯πŸš€ +""" + +import argparse +import logging +import os +import torch +import json +import struct +from unsloth import FastLanguageModel +from unsloth import is_bfloat16_supported +from datasets import load_dataset, DatasetDict +from peft import PeftModel +from trl import SFTTrainer +from transformers import TrainingArguments, AutoModelForCausalLM, AutoTokenizer +from safetensors import safe_open + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +def analyze_safetensors_header(file_path): + try: + with open(file_path, 'rb') as f: + header_size_bytes = f.read(8) + header_size = struct.unpack(' Date: Mon, 16 Sep 2024 01:01:26 -0500 Subject: [PATCH 4/6] Update unsloth-cli-2.py tabs/line spacing issue --- unsloth-cli-2.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/unsloth-cli-2.py b/unsloth-cli-2.py index 883cfd37..c5ac9643 100644 --- a/unsloth-cli-2.py +++ b/unsloth-cli-2.py @@ -651,8 +651,8 @@ def formatting_prompts_func(examples, is_test=False): model_dir = args.model_name if os.path.isdir(args.model_name) else os.path.dirname(args.model_name) logger.info(f"Model directory contents: {os.listdir(model_dir)}") - config_file = os.path.join(model_dir, "config.json") + if os.path.exists(config_file): with open(config_file, 'r') as f: config = json.load(f) @@ -670,15 +670,17 @@ def formatting_prompts_func(examples, is_test=False): logger.error(result["error"]) else: logger.info(result["message"]) + elif args.command == "dequantize": success = run_dequantize(args) if not success: logger.error("Dequantization failed") exit(1) + elif args.command == "quantize": success = run_quantize(args) if not success: logger.error("Quantization failed") exit(1) else: - parser.print_help() \ No newline at end of file + parser.print_help() From 69c8febbf6797da14a3f64f8b8322d8dbb20c088 Mon Sep 17 00:00:00 2001 From: Borch <52267868+Leoleojames1@users.noreply.github.com> Date: Mon, 16 Sep 2024 01:10:05 -0500 Subject: [PATCH 5/6] =?UTF-8?q?=E2=98=85=F0=9F=9A=80Introducing=20the=20en?= =?UTF-8?q?hanced=20unsloth-cli-2!=F0=9F=9A=80=E2=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit β˜…πŸš€commit message, looks niceπŸš€β˜… --- unsloth-cli-2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unsloth-cli-2.py b/unsloth-cli-2.py index c5ac9643..a0c3e286 100644 --- a/unsloth-cli-2.py +++ b/unsloth-cli-2.py @@ -396,7 +396,7 @@ def run_quantize(args): args: Command-line arguments Returns: - True if quantization was successful, False otherwise + True if quantization was successful, False otherwise. """ try: logger.info(f"Quantizing model from {args.input_path} to {args.output_path}") From fb1e0437420263a6b2cbc32d9388b0ded8ce6e7d Mon Sep 17 00:00:00 2001 From: Borch <52267868+Leoleojames1@users.noreply.github.com> Date: Tue, 17 Sep 2024 10:18:58 -0500 Subject: [PATCH 6/6] Update unsloth-cli-2.py --- unsloth-cli-2.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/unsloth-cli-2.py b/unsloth-cli-2.py index a0c3e286..fe6acbdc 100644 --- a/unsloth-cli-2.py +++ b/unsloth-cli-2.py @@ -33,6 +33,8 @@ 4. Merging and Adaptation: - Merge LoRA adapters with base models - Dequantization options for merging quantized models + #TODO @9/17/2024 ADD LoRA+LoRA and Model+Model Merging Methods (lazy Merge kit): To merge fully + #TODO @9/17/2024 ADD DIRECT QUANTIZER FOR SAFETENSOR: To quantize safetensors and support both gguf and safetensors 5. Deployment and Sharing: - GGUF conversion for optimized model deployment