-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_n0_70b.sh
231 lines (192 loc) · 7.22 KB
/
train_n0_70b.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash
# set -x
TEE_OUTPUT="${TEE_OUTPUT:-0}"
NO_TORCH_COMPILE="${NO_TORCH_COMPILE:-1}"
CWD=`pwd`
GPUS_PER_NODE=`python -c "import torch; print(torch.cuda.device_count())"`
# Change for multinode config
MASTER_ADDR="banff-pl1-u30-05"
MASTER_PORT=6009
NNODES=2
NODE_RANK=0
WORLD_SIZE=$(($GPUS_PER_NODE*$NNODES))
export CUDA_DEVICE_MAX_CONNECTIONS=1
EXPERIMENT_DIR="experiment"
mkdir -p $EXPERIMENT_DIR
CHECKPOINT_PATH=$EXPERIMENT_DIR/ckpts
rm -rf $CHECKPOINT_PATH
mkdir -p $CHECKPOINT_PATH
#DATA_DIR=$EXPERIMENT_DIR/data
#mkdir -p $DATA_DIR
DATA_DIR="/root/.cache/data"
TOKENIZER_MODEL=$EXPERIMENT_DIR/tokenizer.model
# Download the tokenizer model
if ! [ -f "$TOKENIZER_MODEL" ]; then
wget -O $TOKENIZER_MODEL https://huggingface.co/NousResearch/Llama-2-7b-chat-hf/resolve/main/tokenizer.model
fi
# Prepare the dataset
echo 'import argparse
from pathlib import Path
from datasets import load_dataset
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--out-dir", type=str, required=False, default="tmp/data",
help="Path to output JSON")
args = parser.parse_args()
out_dir = Path(args.out_dir)
out_dir.mkdir(exist_ok=True, parents=True)
dataset = load_dataset("bookcorpus", split="train")
dataset.to_json(out_dir / "bookcorpus_megatron.json")' > prepare_bookcorpus_megatron_dataset.py
DATA_PATH=${DATA_DIR}/bookcorpus_text_sentence
if ! [ -f "${DATA_DIR}/bookcorpus_text_sentence.idx" ]; then
echo "Dataset file does not exist, creating..."
python3 prepare_bookcorpus_megatron_dataset.py --out-dir ${DATA_DIR}
python3 tools/preprocess_data.py --input ${DATA_DIR}/bookcorpus_megatron.json --tokenizer-type GPTSentencePieceTokenizer --tokenizer-model ${EXPERIMENT_DIR}/tokenizer.model --output-prefix ${DATA_DIR}/bookcorpus --workers `nproc` --split-sentences
python3 tools/preprocess_data.py --input ${DATA_DIR}/bookcorpus_megatron.json --tokenizer-type GPTSentencePieceTokenizer --tokenizer-model ${EXPERIMENT_DIR}/tokenizer.model --output-prefix ${DATA_DIR}/bookcorpus --workers `nproc` --split-sentences
else
echo "Dataset file already exist."
fi
MODEL_SIZE="${MODEL_SIZE:-7}"
TP="${TP:-1}"
PP="${PP:-1}"
MBS="${MBS:-1}"
BS="${BS:-16}"
SEQ_LENGTH="${SEQ_LENGTH:-2048}"
TOTAL_ITERS="${TOTAL_ITERS:-6}"
MAX_POSITION_EMBEDDINGS=4096
TRAIN_LOG="${EXPERIMENT_DIR}/train_${MODEL_SIZE}B_iter${TOTAL_ITERS}_mbs${MBS}_bs${BS}_tp${TP}_pp${PP}_seq${SEQ_LENGTH}.log"
if [[ $MODEL_SIZE -eq 7 ]]; then
HIDDEN_SIZE=4096 # e.g. llama-13b: 5120
FFN_HIDDEN_SIZE=11008 # e.g. llama-13b: 13824
NUM_LAYERS=32 # e.g. llama-13b: 40
NUM_HEADS=32 # e.g. llama-13b: 40
SEQ_LENGTH=$SEQ_LENGTH
MAX_POSITION_EMBEDDINGS=$MAX_POSITION_EMBEDDINGS
NUM_KV_HEADS=32 # llama2 70B uses GQA
elif [[ $MODEL_SIZE -eq 13 ]]; then
HIDDEN_SIZE=5120 # e.g. llama-13b: 5120
FFN_HIDDEN_SIZE=13824 # e.g. llama-13b: 13824
NUM_LAYERS=40 # e.g. llama-13b: 40
NUM_HEADS=40 # e.g. llama-13b: 40
SEQ_LENGTH=$SEQ_LENGTH
MAX_POSITION_EMBEDDINGS=$MAX_POSITION_EMBEDDINGS
NUM_KV_HEADS=40 # llama2 70B uses GQA
elif [[ $MODEL_SIZE -eq 70 ]]; then
HIDDEN_SIZE=8192 # e.g. llama-13b: 5120
FFN_HIDDEN_SIZE=28672 # e.g. llama-13b: 13824
NUM_LAYERS=80 # e.g. llama-13b: 40
NUM_HEADS=64 # e.g. llama-13b: 40
NUM_KV_HEADS=8 # llama2 70B uses GQA
SEQ_LENGTH=$SEQ_LENGTH
MAX_POSITION_EMBEDDINGS=$MAX_POSITION_EMBEDDINGS
else
echo "Model size not supported."
exit 1
fi
GROUP_SIZE=$(( ${NUM_HEADS} / ${NUM_KV_HEADS} ))
NUM_GROUPS=$(( ${NUM_HEADS} / ${GROUP_SIZE} ))
GPT_ARGS="
--tensor-model-parallel-size ${TP} \
--pipeline-model-parallel-size ${PP} \
--num-layers $NUM_LAYERS \
--hidden-size $HIDDEN_SIZE \
--ffn-hidden-size $FFN_HIDDEN_SIZE \
--num-attention-heads $NUM_HEADS \
--seq-length $SEQ_LENGTH \
--max-position-embeddings $MAX_POSITION_EMBEDDINGS \
--untie-embeddings-and-output-weights \
--position-embedding-type rope \
--no-position-embedding \
--disable-bias-linear \
--swiglu \
--init-method-std 0.02 \
--attention-dropout 0.0 \
--hidden-dropout 0.0 \
--normalization RMSNorm \
--micro-batch-size $MBS \
--global-batch-size $BS \
--lr 3.0e-4 \
--train-iters $TOTAL_ITERS \
--lr-decay-style cosine \
--min-lr 3.0e-5 \
--weight-decay 1e-1 \
--lr-warmup-fraction .01 \
--no-async-tensor-model-parallel-allreduce \
--clip-grad 1.0 \
--bf16
"
# --no-masked-softmax-fusion \
DATA_ARGS="
--data-path $DATA_PATH \
--tokenizer-type Llama2Tokenizer \
--tokenizer-model ${TOKENIZER_MODEL} \
--split 949,50,1
"
OUTPUT_ARGS="
--log-interval 1 \
--save-interval 1000 \
--log-throughput \
--no-save-optim \
--eval-iters -1
"
# --save-interval $TOTAL_ITERS \
# --eval-interval $TOTAL_ITERS \
DISTRIBUTED_ARGS="
--nproc_per_node $GPUS_PER_NODE \
--nnodes $NNODES \
--node_rank $NODE_RANK \
--master_addr $MASTER_ADDR \
--master_port $MASTER_PORT
"
EXTRA_ARGS="
--group-query-attention \
--num-query-groups $NUM_GROUPS \
--no-gradient-accumulation-fusion \
--use-distributed-optimizer
"
if [ "$NO_TORCH_COMPILE" -eq 1 ]; then
EXTRA_ARGS="$EXTRA_ARGS --no-torch-compile"
fi
run_cmd="
torchrun $DISTRIBUTED_ARGS pretrain_gpt.py \
$GPT_ARGS \
$DATA_ARGS \
$OUTPUT_ARGS \
$EXTRA_ARGS \
--save $CHECKPOINT_PATH \
--load $CHECKPOINT_PATH
"
if [ "$TEE_OUTPUT" -eq 0 ]; then
run_cmd="$run_cmd >& $TRAIN_LOG"
else
run_cmd="$run_cmd |& tee $TRAIN_LOG"
fi
eval $run_cmd
echo 'import argparse
import numpy as np
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="Process Log")
parser.add_argument("filename")
args = parser.parse_args()
with open(args.filename) as f:
lines = f.readlines()
lines = lines[3:-1]
lines = [float(a) for a in lines]
mean = np.mean(np.array(lines))
print(mean)' > mean_log_value.py
# echo '============================================================================================================'
grep -Eo 'throughput per GPU [^|]*' $TRAIN_LOG | sed -E 's/.*throughput per GPU \(TFLOP\/s\/GPU\): ([0-9\.]+).*/\1/' > tmp.txt
echo "throughput per GPU: $(python mean_log_value.py tmp.txt)"
TIME_PER_ITER=$(python mean_log_value.py tmp.txt 2>/dev/null | awk '{printf "%.6f", $0}')
PERFORMANCE=$(awk -v bs="$BS" -v sl="$SEQ_LENGTH" -v tpi="$TIME_PER_ITER" -v ws="$WORLD_SIZE" 'BEGIN {printf "%.6f", bs * sl * 1000/ (tpi * ws)}')
echo "tokens/GPU/s: $PERFORMANCE"
#rm tmp.txt
# echo '============================================================================================================'
#grep -Eo 'elapsed time per iteration [^|]*' $TRAIN_LOG | sed -E 's/.*elapsed time per iteration \(ms\): ([0-9\.]+).*/\1/' > tmp.txt
#echo "elapsed time per iteration: $(python mean_log_value.py tmp.txt)"
#rm tmp.txt
#echo '============================================================================================================'
#grep -Eo 'mem usages: [^|]*' $TRAIN_LOG | sed -E 's/.*mem usages: ([0-9\.]+).*/\1/' > tmp.txt
#echo "mem usages: $(python mean_log_value.py tmp.txt)"
#rm tmp.txt