Unsloth-Finetune-Template/setup.sh

170 lines
5.2 KiB
Bash
Raw Normal View History

2026-06-02 15:45:59 +02:00
#!/bin/bash
echo "============================================"
echo " Unsloth Fine-Tune Setup"
echo "============================================"
# Create main virtual environment
echo ""
echo "Creating main virtual environment..."
python -m venv venv
source venv/bin/activate
# Select PyTorch version
echo ""
echo "Select PyTorch version:"
echo " 1) CUDA (NVIDIA GPU, default)"
echo " 2) ROCm (AMD GPU)"
echo " 3) Custom version"
echo ""
read -p "Enter choice (1-3): " TORCH_CHOICE
case $TORCH_CHOICE in
1)
echo "Installing PyTorch (CUDA)..."
pip install torch
;;
2)
echo "Installing PyTorch (ROCm)..."
pip install torch --index-url https://download.pytorch.org/whl/nightly/rocm7.2
;;
3)
echo "Checking for custom PyTorch..."
python -c "import torch" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: torch is not available. Please install it before running this script."
exit 1
fi
echo "torch is available, continuing..."
;;
*)
echo "Invalid choice. Installing PyTorch (CUDA) as default."
pip install torch
;;
esac
2026-06-02 15:45:59 +02:00
pip install -r requirements.txt
# Ask if fresh build or existing
2026-06-02 15:45:59 +02:00
echo ""
echo "Would you like to:"
echo " 1) Clone and build a fresh copy of llama.cpp"
echo " 2) Use an existing llama.cpp build"
echo ""
read -p "Enter choice (1-2): " BUILD_CHOICE
LLAMA_CPP_PATH=""
if [ "$BUILD_CHOICE" = "1" ]; then
echo ""
echo "Select llama.cpp backend:"
echo " 1) CUDA (NVIDIA GPU)"
echo " 2) ROCm (AMD GPU)"
echo " 3) Vulkan (Cross-vendor GPU)"
echo " 4) CPU only"
echo ""
read -p "Enter choice (1-4): " BACKEND
echo ""
echo "Cloning llama.cpp..."
2026-06-02 15:45:59 +02:00
git clone https://github.com/ggml-org/llama.cpp.git
# Build llama.cpp with correct flags
echo ""
echo "Building llama.cpp..."
cd llama.cpp
2026-06-02 15:45:59 +02:00
BUILD_FAILED=0
2026-06-02 15:45:59 +02:00
case $BACKEND in
1)
echo "Building with CUDA support..."
cmake -B build -DBUILD_SHARED_LIBS=ON -DGGML_CUDA=ON || BUILD_FAILED=1
[ $BUILD_FAILED -eq 0 ] && cmake --build build --config Release -j$(nproc) || BUILD_FAILED=1
;;
2)
echo "Building with ROCm support..."
read -p "Enter GPU target (e.g., gfx1030, gfx942, gfx1100): " ROCM_TARGET
[ -z "$ROCM_TARGET" ] && ROCM_TARGET="gfx1030"
HIPCXX="$(hipconfig -l)/clang" HIP_PATH="$(hipconfig -R)" \
cmake -S . -B build -DBUILD_SHARED_LIBS=ON -DGGML_HIP=ON -DGPU_TARGETS="$ROCM_TARGET" -DCMAKE_BUILD_TYPE=Release || BUILD_FAILED=1
[ $BUILD_FAILED -eq 0 ] && cmake --build build --config Release -j$(nproc) || BUILD_FAILED=1
;;
3)
echo "Building with Vulkan support..."
cmake -B build -DBUILD_SHARED_LIBS=ON -DGGML_VULKAN=1 || BUILD_FAILED=1
[ $BUILD_FAILED -eq 0 ] && cmake --build build --config Release -j$(nproc) || BUILD_FAILED=1
;;
4)
echo "Building CPU-only..."
cmake -B build -DBUILD_SHARED_LIBS=ON || BUILD_FAILED=1
[ $BUILD_FAILED -eq 0 ] && cmake --build build --config Release -j$(nproc) || BUILD_FAILED=1
;;
*)
echo "Invalid choice. Building CPU-only."
cmake -B build -DBUILD_SHARED_LIBS=ON || BUILD_FAILED=1
[ $BUILD_FAILED -eq 0 ] && cmake --build build --config Release -j$(nproc) || BUILD_FAILED=1
;;
esac
2026-06-02 15:45:59 +02:00
LLAMA_CPP_PATH=$(pwd)
cd ..
else
2026-06-02 17:17:49 +02:00
read -p "Enter path to existing llama.cpp build: " LLAMA_CPP_PATH
# Validate path
if [ ! -d "$LLAMA_CPP_PATH" ]; then
echo "Error: Directory $LLAMA_CPP_PATH does not exist."
exit 1
fi
# Resolve to absolute path
LLAMA_CPP_PATH=$(realpath "$LLAMA_CPP_PATH")
# Check for shared library
2026-06-02 17:17:49 +02:00
if [ ! -f "$LLAMA_CPP_PATH/build/bin/libllama.so" ]; then
echo "Error: Could not find libllama.so at $LLAMA_CPP_PATH/build/bin/libllama.so"
echo "Make sure llama.cpp was built with -DBUILD_SHARED_LIBS=ON"
exit 1
fi
echo ""
echo "Using existing build: $LLAMA_CPP_PATH"
ln -sfn "$LLAMA_CPP_PATH" llama.cpp
fi
2026-06-02 15:45:59 +02:00
# Find the shared library
2026-06-02 17:17:49 +02:00
if [ -f "$LLAMA_CPP_PATH/build/bin/libllama.so" ]; then
LLAMA_CPP_LIB="$LLAMA_CPP_PATH/build/bin/libllama.so"
else
echo "Error: Could not locate libllama.so"
exit 1
fi
2026-06-02 15:45:59 +02:00
echo ""
echo "Using shared library: $LLAMA_CPP_LIB"
2026-06-02 15:45:59 +02:00
# Install llama-cpp-python with existing build
echo ""
echo "Installing llama-cpp-python..."
2026-06-02 15:45:59 +02:00
source venv/bin/activate
LLAMA_CPP_LIB="$LLAMA_CPP_LIB" LLAMA_CPP_LIB_PATH="$LLAMA_CPP_LIB" CMAKE_ARGS="-DLLAMA_BUILD=OFF" pip install llama-cpp-python
2026-06-02 15:45:59 +02:00
2026-06-02 15:55:34 +02:00
# Create convertgguf_venv for llama.cpp Python tools
echo ""
echo "Creating convertgguf_venv..."
python -m venv llama.cpp/convertgguf_venv
source llama.cpp/convertgguf_venv/bin/activate
pip install -r llama.cpp/requirements.txt
2026-06-02 15:45:59 +02:00
echo ""
echo "Setup complete! Configure the scripts and run:"
echo " bash run-pipeline.sh"
if [ -n "$BUILD_FAILED" ] && [ $BUILD_FAILED -ne 0 ]; then
2026-06-02 15:45:59 +02:00
echo ""
echo "Build failed. See the build guide for help:"
echo " https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md"
fi