Distributed Training: LLaMA-Factory on Managed Slurm

Distributed Training: LLaMA-Factory on Managed Slurm

1. Overview

This guide walks you through implementing distributed training with LLaMA-Factory on a Managed Slurm cluster. The documentation covers all essential aspects of the workflow, including environment configuration, efficient job scheduling via Slurm, and critical performance optimizations.


The guide features a practical training example that demonstrates fine-tuning a Llama-3.2-1B model using the alpaca dataset - a collection of instruction-tuning samples designed to enhance model capabilities. Using full fine-tuning settings, we provide detailed, hands-on instructions for executing this real-world training scenario in a distributed computing environment.


Our step-by-step approach ensures you can successfully replicate and adapt this training pipeline for your specific requirements, leveraging the power of Slurm's resource management capabilities.

Key advantages:
  1. Performance Scaling: Slurm intelligently distributes workloads across GPUs/nodes, cutting training time significantly.
  2. Cost Efficiency: Smart scheduling eliminates idle GPU time, maximizing resource utilization and reducing expenses.
  3. Operational Stability: Automated recovery, error management, and monitoring ensure uninterrupted training sessions.
  4. Practical Implementation: Complete fine-tuning workflow includes dataset preparation, YAML configuration, and ready-to-use Slurm scripts.
This guide enables direct replication of optimized LLM training workflows on GreenNode AI Platform infrastructure.

2. Use case

Use case: Training Large Language Models with LLaMA-factory

The GreenNode AI Platform's Managed Slurm service delivers performance for training large language models using LLaMA-factory. This optimized solution distributes processing across multiple GPUs and compute nodes, significantly reducing training time while maintaining operational stability.

Key Advantages
  1. Enterprise Scalability: Seamlessly handles large-scale models with intelligent GPU resource allocation
  2. Cost Efficiency: Minimizes cloud computing expenses by eliminating idle resources through dynamic scheduling
  3. Operational Reliability: Ensures uninterrupted training with automated job recovery and comprehensive error handling
Experience the power of distributed computing without the complexity of manual configuration.

3. Prerequisites

  1. GreenNode GPU Instances
  2. Managed Slurm cluster

4. Environment Setup

    With Head node login information, log in to Head node via SSH and run the following on Head node. This will install all required packages for running LlaMA-Factory
    1. python3 –m venv venv 

    2. source venv/bin/activate
    3. git clone https://github.com/hiyouga/LLaMA-Factory.git
    4. cd LLaMA-Factory
    5. pip install -e ".[torch,metrics,deepspeed]" 

    5. Sample Training Task: Fine-tuning LLaMA

    To demonstrate a real-world scenario, we will full fine-tune a Llama-3.2-1B model using the Alpaca dataset for instruction-following tasks.

    5.1. Dataset: Alpaca

    The Alpaca dataset is a collection of instruction-response pairs created by Stanford researchers to improve language model instruction-following capabilities. Generated using LLM-based techniques, it covers diverse tasks from creative writing to problem-solving. The dataset has become a foundation for fine-tuning language models in the open-source community, with variants like Open Instruct Uncensored Alpaca extending its capabilities for more comprehensive training scenarios.

    5.2. Model: LLaMA 3.2-1B

    Meta AI's LLaMA 3.2 1B delivers impressive performance within a compact 1 billion parameter footprint, balancing strong reasoning capabilities with efficient resource utilization. Despite its small size, the model benefits significantly from distributed fine-tuning across multiple GPUs, where frameworks like LLaMA-Factory with Slurm orchestration can parallelize the training process, dramatically reducing fine-tuning time while maintaining optimization quality. 
    If you want to download the model directly from Huggingface, you can use the below command:

    1. hugging face-cli download meta-llama/Llama-3.2-1B --local-dir=Llama-3.2-1B

    6. Preparing training configuration

    LLaMA-Factory leverages YAML files for structured training parameter management, simplifying hyperparameter adjustments while ensuring experiment reproducibility. 
    We will walk you through the process for creating an optimized YAML configuration specifically tailored for fine-tuning the LLaMA 3.2 1B model.

    6.1. Sample YAML Configuration for Fine-Tuning LLaMA 3.2 1B

    LLaMA-Factory provides various predefined YAML training configuration files, located at LLaMA-Factory/examples. Here is a YAML file for full fine-tuning LLaMA 3.2 1B with Open Instruct Uncensored Alpaca dataset:
    1. model_name_or_path: Llama-3.2-1B
    2. trust_remote_code: true                                                                                                                                                                                                                    
    3. stage: sft                                                                                                                                                                                                                                 
    4. do_train: true                                                                                                                                                                                                                             
    5. finetuning_type: lora                                                                                                                                                                                                                      
    6. deepspeed: examples/deepspeed/ds_z2_config.json                                                                                                                                                                                            
    7. dataset: alpaca_en                                                                                                                                                                                                                 
    8. template: llama3                                                                                                                                                                                                                           
    9. cutoff_len: 512                                                                                                                                                                                                                            
    10. max_samples: 1000                                                                                                                                                                                                                          
    11. overwrite_cache: true                                                                                                                                                                                                                      
    12. preprocessing_num_workers: 1                                                                                                                                                                                                               
    13. output_dir: saves/llama3.2-1b/full/sft                                                                                                                                                                                                     
    14. logging_steps: 10                                                                                                                                                                                                                          
    15. save_strategy: "steps"                                                                                                                                                                                                                     
    16. save_steps: 100                                                                                                                                                                                                                            
    17. save_total_limit: 2                                                                                                                                                                                                                        
    18. plot_loss: true                                                                                                                                                                                                                            
    19. overwrite_output_dir: true                                                                                                                                                                                                                 
    20. per_device_train_batch_size: 2                                                                                                                                                                                                             
    21. gradient_checkpointing: true                                                                                                                                                                                                               
    22. gradient_accumulation_steps: 4                                                                                                                                                                                                             
    23. learning_rate: 2.0e
    Assuming you have downloaded the model as suggested above, we change the model path to local by:
    1. model_name_or_path: Llama-3.2-1B

    We adjust the number of training samples to 1000 samples by:

    1. max_samples: 1000                                                                                                                                                                                                                          
    You can adjust all other options if necessary

    7. Configuring Slurm for Multi-Node Training

    Assume you have a YAML training configuration file named llama31_training.yaml, create a Slurm script train_llama.sbatch for training on 2 nodes, 1 GPUs per node:

    1. #!/bin/bash                                                                                                                                                                                                                                
    2. #SBATCH --job-name=llm-multinode-training                                                                                                                                                                                                      
    3. #SBATCH --nodes=2                                                                                                                                                                                                                          
    4. #SBATCH --time=4-00:00:00                                                                                                                                                                                                                  
    5. #SBATCH --gres=gpu:1                                                                                                                                                                                                                       
    6. #SBATCH -o log/training_%N.out                                                                                                                                                                                                             
    7. #SBATCH -e log/training_%N.err                                                                                                                                                                                                             
    8.                                                                                                                                                                                                                                            
    9. # Get node information and master node                                                                                                                                                                                                     
    10. nodes=$(scontrol show hostnames $SLURM_JOB_NODELIST)                                                                                                                                                                                       
    11. nodes_array=($nodes)                                                                                                                                                                                                                       
    12. head_node=${nodes_array[0]}                                                                                                                                                                                                                
    13. head_node_ip=$(srun --nodes=1 --ntasks=1 -w "$head_node" hostname --ip-address | cut -d" " -f2)                                                                                                                                            
    14. echo "Master Node IP: $head_node"                                                                                                                                                                                                          
    15.                                                                                                                                                                                                                                            
    16. # Create a script for each node to execute                                                                                                                                                                                                 
    17. cat > node_script.sh << 'EOF'                                                                                                                                                                                                              
    18. #!/bin/bash                                                                                                                                                                                                                                
    19. # Each node will get its proper SLURM_NODEID in this context                                                                                                                                                                               
    20. export NNODES=2                                                                                                                                                                                                                            
    21. export NPROC_PER_NODE=1                                                                                                                                                                                                                    
    22. export MASTER_ADDR=PLACEHOLDER_MASTER   
    23. export MASTER_PORT=29500                                                                                                                                                                                                                   
    24. export NODE_RANK=$SLURM_NODEID                                                                                                                                                                                                             
    25. export NCCL_IB_DISABLE=0                                                                                                                                                                                                                   
    26. export NCCL_SOCKET_IFNAME=^lo,docker0                                                                                                                                                                                                      
    27. export NCCL_TIMEOUT=180000000                                                                                                                                                                                                              
    28. export NCCL_BLOCKING_WAIT=1                                                                                                                                                                                                                
    29. export NCCL_ASYNC_ERROR_HANDLING=1                                                                                                                                                                                                         
    30. export FORCE_TORCHRUN=1                                                                                                                                                                                                                    
    31.                                                                                                                                                                                                                                            
    32. echo "NODE: $(hostname) with RANK: $NODE_RANK starting training"                                                                                                                                                                           
    33. source venv/bin/activate                                                                                                                                                                                                             
    34. llamafactory-cli train llama32_training.yaml                                                                                                                                                                                               
    35. EOF                                                                                                                                                                                                                                        
    36.                                                                                                                                                                                                                                            
    37. # Replace placeholder with actual master node                                                                                                                                                                                              
    38. sed -i "s/PLACEHOLDER_MASTER/$head_node/" node_script.sh                                                                                                                                                                                   
    39. chmod +x node_script.sh                                                                                                                                                                                                                    
    40.                                                                                                                                                                                                                                            
    41. # Run the training script on all nodes                                                                                                                                                                                                     
    42. srun ./node_script.sh 
    Use sbatch to submit the training job:

    1. sbatch train_llama.sbatch


    View the queue job with squeue



    Inspect the training.out and training.err file to see the training progress




    The final result is shown below:



    8. Monitoring CPU and GPU Usage During Training

    Effective resource monitoring is essential when training LLaMA 3.2 1B on GreenNode AI Platform to ensure optimal performance and stability. Comprehensive system monitoring enables:
    1. Real-time identification of performance bottlenecks including GPU underutilization and CPU processing constraints
    2. Dynamic resource optimization through batch size adjustments and memory allocation refinements
    3. Proactive prevention of training interruptions caused by memory limitations and system failures
    GreenNode AI Platform features an integrated monitoring dashboard providing live hardware performance metrics:
    1. Per-GPU utilization rates and processing efficiency
    2. VRAM consumption patterns across distributed nodes
    3. CPU load distribution and thread utilization
    4. I/O performance including disk throughput and network transfer rates
    Access these critical metrics directly through your GreenNode AI Platform control panel to maintain peak training efficiency throughout your model development lifecycle.






      • Related Articles

      • Managed SLURM service

        1. Introduction to SLURM SLURM (Simple Linux Utility for Resource Management) is an open-source job scheduler designed for Linux high-performance computing clusters. It efficiently allocates compute resources to user jobs, provides tools for job ...
      • Training Mode

        Training Moe Definition Characteristics Use Cases Single training Single training refers to training a machine learning model using a single instance or node. Utilizes a single compute instance for model training. Suitable for small to medium-sized ...
      • Start your Model Training Job

        Model training job involves using datasets to create and optimize machine learning models. This process occurs in the cloud environment, where data scientists run code to build models and tune hyperparameters. Training uses computational resources ...
      • Local Storage Limits for Notebook, Model Training, and Online Prediction

        To ensure optimal performance and cost-efficiency, our platform provides a certain amount of local storage included with each compute instance you create. However, exceeding this storage limit can impact your workflow and results. This guide will ...
      • Foundation Models

        We're excited to offer a diverse selection of powerful base models to fuel your AI development. This list represents a range of cutting-edge language models from leading AI research organizations, each with unique strengths and characteristics. ...