Basculer le menu
Changer de menu des préférences
Basculer le menu personnel
Non connecté(e)
Votre adresse IP sera visible au public si vous faites des modifications.

« Llama-Factory » : différence entre les versions

De Le Wiki de Lug
Balise : Révocation manuelle
Ligne 87 : Ligne 87 :
==== Carte NVIDIA ====
==== Carte NVIDIA ====
=== Installation de LLaMA-Factory ===
=== Installation de LLaMA-Factory ===
# cd /opt/llamafactory/LlamaFactory/
  # pip install -e .
  # pip install -e .
  # pip install -r requirements/metrics.txt
  # pip install -r requirements/metrics.txt

Version du 16 août 2026 à 13:33

Prérequis

  • Disposer d’un environnement GPU fonctionnel avec CUDA Toolkit pour une carte NVIDIA ou ROCm pour une carte AMD, voir cette page.
  • Distribution Ubuntu recommandée.

Installation

# apt update && apt upgrade
# apt install -y python3 python3-venv python3-pip

Créer un environnement virtuel dédié :

# mkdir -p /opt/llamafactory
# python3 -m venv /opt/llamafactory/venv
# source /opt/llamafactory/venv/bin/activate

Mettre à jour les outils Python :

# python -m pip install --upgrade pip setuptools wheel

Télécharger LLaMA-Factory :

# cd /opt/llamafactory
# git clone --depth 1 https://github.com/hiyouga/LlamaFactory.git
# cd LlamaFactory

Installation de PyTorch

Carte AMD

Installer PyTorch avec le support ROCm (exemple avec ROCm 7.2) :

# pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm7.2

Vérifier que PyTorch détecte le GPU :

# python -c "import torch; print(torch.__version__); print(torch.version.hip); print(torch.cuda.is_available()); print(torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'GPU non détecté')"
Fix pour PyTorch sous Windows WSL

Supprimer l'environnement virtuel existant :

# deactivate 2>/dev/null || true
# rm -rf /opt/llamafactory/venv

Installer uv et Python 3.12 :

# curl -LsSf https://astral.sh/uv/install.sh | sh
# source ~/.bashrc
# uv python install 3.12

Recréer l'environnement virtuel :

# uv venv --python 3.12 /opt/llamafactory/venv
# uv pip install --python /opt/llamafactory/venv/bin/python pip setuptools wheel
# source /opt/llamafactory/venv/bin/activate

Créer les répertoires temporaires et de téléchargement :

# mkdir -p /opt/pip-tmp
# mkdir -p /opt/pytorch-rocm
# cd /opt/pytorch-rocm

Télécharger les wheels AMD compatibles ROCm 7.2 :

# wget 'https://repo.radeon.com/rocm/manylinux/rocm-rel-7.2/torch-2.9.1%2Brocm7.2.0.lw.git7e1940d4-cp312-cp312-linux_x86_64.whl'
# wget 'https://repo.radeon.com/rocm/manylinux/rocm-rel-7.2/torchvision-0.24.0%2Brocm7.2.0.gitb919bd0c-cp312-cp312-linux_x86_64.whl'
# wget 'https://repo.radeon.com/rocm/manylinux/rocm-rel-7.2/torchaudio-2.9.0%2Brocm7.2.0.gite3c6ee2b-cp312-cp312-linux_x86_64.whl'
# wget 'https://repo.radeon.com/rocm/manylinux/rocm-rel-7.2/triton-3.5.1%2Brocm7.2.0.gita272dfa8-cp312-cp312-linux_x86_64.whl'

Installer les wheels PyTorch AMD :

# TMPDIR=/opt/pip-tmp uv pip install \
  --python /opt/llamafactory/venv/bin/python \
  --no-cache \
  ./torch-2.9.1+rocm7.2.0.lw.git7e1940d4-cp312-cp312-linux_x86_64.whl \
  ./torchvision-0.24.0+rocm7.2.0.gitb919bd0c-cp312-cp312-linux_x86_64.whl \
  ./torchaudio-2.9.0+rocm7.2.0.gite3c6ee2b-cp312-cp312-linux_x86_64.whl \
  ./triton-3.5.1+rocm7.2.0.gita272dfa8-cp312-cp312-linux_x86_64.whl

Sous WSL avec ROCDXG, supprimer le runtime HSA inclus dans la wheel PyTorch afin d'utiliser le runtime HSA système compatible WSL :

# location=$(pip show torch | awk -F ': ' '/Location/{print $2}')
# rm -f "$location/torch/lib/libhsa-runtime64.so"*
# ldconfig

Vérifier que la variable ROCDXG est active :

# echo $HSA_ENABLE_DXG_DETECTION

La commande doit retourner :

1

Vérifier que PyTorch détecte le GPU :

# python -c "import torch; print(torch.__version__); print(torch.version.hip); print(torch.cuda.is_available()); print(torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'GPU non détecté')"

Exemple de résultat attendu :

2.9.1+rocm7.2.0.git7e1940d4
7.2.x
True
AMD Radeon RX 9070 XT

Carte NVIDIA

Installation de LLaMA-Factory

# cd /opt/llamafactory/LlamaFactory/
# pip install -e .
# pip install -r requirements/metrics.txt

Vérification

# llamafactory-cli version
# llamafactory-cli env

Interface Web

# llamafactory-cli webui

Exemples

hy_mt2 (QLoRA)

Avec pour exemple le dataset paradox_sft_eu5.jsonl

# source /opt/llamafactory/venv/bin/activate
# cd /opt/llamafactory/LlamaFactory
# mkdir paradox_data

On place le fichier paradox_sft_eu5.jsonl dans /opt/llamafactory/LlamaFactory/paradox_data

# vi paradox_data/dataset_info.json
{
  "paradox_eu5": {
    "file_name": "paradox_sft_eu5.jsonl",
    "formatting": "sharegpt",
    "columns": {
      "messages": "messages"
    },
     "tags": {
     "role_tag": "role",
      "content_tag": "content",
      "user_tag": "user",
      "assistant_tag": "assistant",
      "system_tag": "system"
    }
  }
}

On installe bitsandbytes si nécessaire (indispensable pour QLoRA) :

# pip install -U bitsandbytes
# python -m bitsandbytes

Ensuite on récupère les 2 petits fichiers officiels Tencent nécessaires au wrapper :

# mkdir -p hy_mt2_support hy_mt2_support_tmp
# hf download tencent/Hy-MT2-7B-GGUF \
  train/llama_factory_support/hy_dense_template.py \
  train/llama_factory_support/hy_v3_patches.py \
  --local-dir hy_mt2_support_tmp
# cp hy_mt2_support_tmp/train/llama_factory_support/train_hy_dense.py hy_mt2_support/
# cp hy_mt2_support_tmp/train/llama_factory_support/hy_v3_patches.py hy_mt2_support/
# rm -rf hy_mt2_support_tmp

Le ficher hy_dense_template.py étant déjà intégré à Llama-Factory on supprime son importation dans train_hy_dense.py :

# sed -i 's/^import hy_dense_template/# import hy_dense_template  # already built into LLaMA-Factory/' \
  hy_mt2_support/train_hy_dense.py

Ensuite on peut créer un fichier de test :

# vi hy_mt2_7b_eu5_qlora_smoke.yaml
### model
model_name_or_path: tencent/Hy-MT2-7B
trust_remote_code: true

# QLoRA 4-bit for 16 GB VRAM
quantization_method: bitsandbytes
quantization_bit: 4
quantization_type: nf4
double_quantization: true

### method
stage: sft
do_train: true
finetuning_type: lora
lora_rank: 64
lora_alpha: 128
lora_dropout: 0.05
lora_target: q_proj,k_proj,v_proj,o_proj

### dataset
dataset_dir: ./paradox_data
dataset: paradox_eu5
template: hy_dense_7b
cutoff_len: 4096
overwrite_cache: false
preprocessing_num_workers: 8

### output
logging_steps: 5
plot_loss: true
report_to: none
save_only_model: false

### train
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
learning_rate: 2.0e-4
lr_scheduler_type: cosine_with_min_lr
lr_scheduler_kwargs:
  min_lr_rate: 0.1
warmup_ratio: 0.05
bf16: true
gradient_checkpointing: true
gradient_checkpointing_kwargs:
  use_reentrant: true
flash_attn: sdpa
ddp_timeout: 180000000

output_dir: ./saves/hy_mt2_7b/eu5_qlora_smoke
overwrite_output_dir: true
save_steps: 100
max_steps: 200

Pour lancer ce test :

# torchrun --nproc_per_node 1 hy_mt2_support/train_hy_dense.py hy_mt2_7b_eu5_qlora_smoke.yaml