File size: 3,033 Bytes
8e72e9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (C) 2024 Habana Labs, Ltd. an Intel Company.
#
# 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.

#!/bin/bash

echo "**************************************************************************"
echo "Script to convert Megatron-DeepSpeed Checkpoint into a Universal Checkpoint"
echo "**************************************************************************"

###### INPUTS : START #####
# here for testing only
# MEGATRON_DEEPSPEED_ROOT
# DEEPSPEED_ROOT
# HL_NUM_NODES
# HL_LATEST_CHECKPOINT=<>/checkpoints/global_step48600
# HL_UNIV_CP_EXTRACT_WORKERS
# HL_UNIV_CP_MERGE_WORKERS
# HL_DEVICES_PER_NODE
###### INPUTS : END  ######

NUM_NODES=${HL_NUM_NODES:-1}
LATEST_CHECKPOINT=${HL_LATEST_CHECKPOINT:-}
EXTRACT_WORKERS=${HL_UNIV_CP_EXTRACT_WORKERS:-}
MERGE_WORKERS=${HL_UNIV_CP_MERGE_WORKERS:-}
DEVICES_PER_NODE=${HL_DEVICES_PER_NODE:-8}

if [[ -z "$MEGATRON_DEEPSPEED_ROOT" ]]; then
    MEGATRON_DEEPSPEED_ROOT=$(realpath $(dirname $0)/../)
fi

if [[ -z "$DEEPSPEED_ROOT" ]]; then
    res=$(deepspeed --help)
    if [ $? -ne 0 ]; then
        echo "please install deepspeed or set DEEPSPEED_ROOT"
    fi
    DEEPSPEED_ROOT=$(pip show deepspeed | grep -i "^Location:" | cut -d" " -f 2)
fi

if [[ -z "$LATEST_CHECKPOINT" ]]; then
    echo "please set HL_LATEST_CHECKPOINT"
    exit 1
else
    LATEST_CHECKPOINT=${LATEST_CHECKPOINT%/}
fi

echo ${DEEPSPEED_ROOT}

OLD_CARDS=$(ls $LATEST_CHECKPOINT/bf16_zero_pp_rank_*_mp_rank_*_optim_states.pt|wc -l)
NEW_CARDS=$(($NUM_NODES*$DEVICES_PER_NODE))

if [ $OLD_CARDS -ne $NEW_CARDS ]; then
    echo "Conversion required as accelerators count has changed from ${OLD_CARDS} to ${NEW_CARDS}"
    export PYTHONPATH=$MEGATRON_DEEPSPEED_ROOT:$PYTHONPATH
    UNIV_CP_PATH=${LATEST_CHECKPOINT}_universal
    mkdir -p $UNIV_CP_PATH
    PYTHON_CMD="python ${DEEPSPEED_ROOT}/deepspeed/checkpoint/ds_to_universal.py --input_folder ${LATEST_CHECKPOINT} --output_folder ${UNIV_CP_PATH}"

    if [ -n "$EXTRACT_WORKERS" ]; then
      PYTHON_CMD="${PYTHON_CMD} --num_extract_workers ${EXTRACT_WORKERS}"
    fi

    if [ -n "$MERGE_WORKERS" ]; then
      PYTHON_CMD="${PYTHON_CMD} --num_merge_workers ${MERGE_WORKERS}"
    fi

    echo $PYTHON_CMD
    eval $PYTHON_CMD
    if [ $? -ne 0 ]; then
        echo 'Failed to run ds_to_universal.py '
        exit 1
    else
      echo "Conversion to universal checkpoint finished. Converted checkpoint available at ${UNIV_CP_PATH} "
      exit 0
    fi
else
    echo 'Conversion not required as number of accelerators count has not changed'
    exit 0
fi