#!/bin/bash
# Phase 2 — Installation d'Unsloth et dépendances pour le fine-tuning
#
# Prérequis vérifiés :
#   - PyTorch 2.10.0+cu128 ✅
#   - CUDA 12.x ✅
#   - transformers 4.57.6 ✅
#   - accelerate 1.12.0 ✅
#
# Usage : bash scripts/07_setup_unsloth.sh

set -e

VENV="/home/dom/ai/t2a/.venv"
PIP="$VENV/bin/pip"
PYTHON="$VENV/bin/python3"

echo "=== Installation Unsloth + dépendances ==="
echo ""

# 1. bitsandbytes (quantification 4-bit)
echo "[1/4] Installation de bitsandbytes..."
$PIP install --upgrade bitsandbytes

# 2. PEFT (LoRA)
echo ""
echo "[2/4] Installation de PEFT..."
$PIP install --upgrade peft

# 3. TRL (SFTTrainer)
echo ""
echo "[3/4] Installation de TRL..."
$PIP install --upgrade trl

# 4. Unsloth
echo ""
echo "[4/4] Installation d'Unsloth..."
$PIP install --upgrade --no-cache-dir "unsloth[cu128-torch2100] @ git+https://github.com/unslothai/unsloth.git"

# Vérification
echo ""
echo "=== Vérification ==="
$PYTHON -c "
import torch
print(f'PyTorch: {torch.__version__}')
print(f'CUDA: {torch.cuda.is_available()}')
print(f'GPU: {torch.cuda.get_device_name(0)}')

import bitsandbytes
print(f'bitsandbytes: OK')

from peft import LoraConfig
print(f'PEFT: OK')

from trl import SFTTrainer
print(f'TRL: OK')

try:
    from unsloth import FastLanguageModel
    print(f'Unsloth: OK')
except Exception as e:
    print(f'Unsloth: {e}')
    print('Essayez: pip install unsloth')

print()
print('=== Setup prêt pour le fine-tuning ! ===')
"
