#!/bin/bash

# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

export CUDA_MODULE_LOADING=LAZY
export CUDA_DEVICE_MAX_CONNECTIONS=1
export PYTHONUNBUFFERED=1
unset GLOG_vmodule GLOG_v
export PADDLE_DISABLE_CUDNN_FA=1
export FLAGS_use_auto_growth_pinned_allocator=True
export FLAGS_pipeline_nccl_comm_init_option=1
export FLAGS_sharding_v2_check_zero_padding=1
export FLAGS_use_paddle_recall_error=0
export FLAGS_tcp_max_syn_backlog=16384
export FLAGS_call_stack_level=2

SM=`nvidia-smi --query-gpu=compute_cap --format=csv | tail -n 1 | sed 's/\.//g'`
if [ $SM -eq 90 ]
then
    export FLAGS_flash_attn_version=3
else
    export FLAGS_flash_attn_version=2
fi

export FLAGS_enable_moe_utils=true

export PYTHONPATH=../../:$PYTHONPATH

log_dir=output/paddle_distributed_logs

python -m paddle.distributed.launch \
    --log_dir ${log_dir} \
    --master <master_ip>:<port> \
    --nnodes 7 \
    --rank <current_rank> \
    --run_mode=collective \
    pretrain.py  \
    --config pretrain_4p5_300B_A47B.yaml

# # Here is an example of the configuration and method for running scripts in a 7-machine cluster.
# nnodes=7
# START_RANK=0
# END_RANK=$nnodes
# # The current rank can be obtained through the preset environment variables in the cluster.
# rank=$CURRENT_TRAINER_ID_FLAG

# if [[ $rank -lt $START_RANK ]]; then
#     exit 0
# fi

# if [[ $rank -ge $END_RANK ]]; then
#     exit 0
# fi
# rank=$(($rank-$START_RANK))
# # Typically, rank 0 is selected as the master of the cluster.
# master=$RANK_0_IP
# port=36677

# python -m paddle.distributed.launch \
#     --log_dir ${log_dir} \
#     --master $master:$port \
#     --nnodes $nnodes \
#     --rank $rank \
#     --run_mode=collective \
#     pretrain.py  \
#     --config pretrain_4p5_300B_A47B.yaml

# # Running scripts in a cluster using the mpirun method.
# mpirun bash train_4p5_300B_A47B.sh
