Repository navigation
Expand file tree
/
Copy pathimaphelper.sh
More file actions
242 lines (199 loc) Β· 8.3 KB
/
Copy pathimaphelper.sh
File metadata and controls
242 lines (199 loc) Β· 8.3 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
# --- Configuration: Your centralized storage root ---
VAULT="/mnt/models"
# Color Definitions
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m'
WHITE='\033[1;37m'
# Display Banner
show_header() {
echo -e "${BLUE}====================================================${NC}"
echo -e "π§ ${GREEN} AI Model Centralized Management (iMapHelper) ${NC}"
echo -e "π ${CYAN} Global Version | Hardware Optimized ${NC}"
echo -e "${BLUE}====================================================${NC}"
}
# 1. Directory Structure & Health Check
show_tree() {
echo -e "${YELLOW}[Current Vault Physical Structure]${NC}"
if [ ! -d "$VAULT" ]; then
echo -e "${RED}β οΈ WARNING: Root directory $VAULT not found! Please run Option [1] to initialize.${NC}"
return
fi
# Check core subdirectory integrity
local missing=0
for dir in "base/llm" "base/diffusion" "base/vlm" "lora" "tensorrt" "cache"; do
if [ ! -d "$VAULT/$dir" ]; then
echo -e "${RED}[Missing] $dir${NC}"
missing=1
fi
done
if [ $missing -eq 1 ]; then
echo -e "${RED}π Structure incomplete. Run Option [1] to fix.${NC}"
else
echo -e "${GREEN}β
Vault structure is healthy.${NC}"
fi
# Display Tree (requires 'tree' pkg, fallback to 'find')
if command -v tree >/dev/null 2>&1; then
tree -d -L 3 "$VAULT"
else
find "$VAULT" -maxdepth 3 -not -path '*/.*' -type d | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-- \1/"
fi
# Show cache size
if [ -d "$VAULT/cache" ]; then
echo -e "${BLUE}π‘ Cache Usage: $(du -sh "$VAULT/cache" 2>/dev/null | cut -f1)${NC}"
fi
echo -e "${BLUE}----------------------------------------------------${NC}"
}
check_vault_integrity() {
local required_dirs=("base/llm" "base/diffusion" "base/vlm" "lora" "tensorrt" "cache")
if [ ! -d "$VAULT" ]; then
return 1
fi
for dir in "${required_dirs[@]}"; do
if [ ! -d "$VAULT/$dir" ]; then
return 1
fi
done
return 0
}
# 2. Safety Initialization
init_vault() {
echo -e "${YELLOW}[Initializing/Repairing Vault...]${NC}"
# Create directory structure
sudo mkdir -p "$VAULT/base/llm" "$VAULT/base/diffusion" "$VAULT/base/vlm" \
"$VAULT/lora" "$VAULT/tensorrt" "$VAULT/cache"
# Set permissions
sudo chown -R $USER:$USER "$VAULT"
sudo chmod -R 775 "$VAULT"
# Set SGID to ensure group inheritance
sudo find "$VAULT" -type d -exec chmod g+s {} +
# Inject Environment Variables
if ! grep -q "HF_HOME" ~/.bashrc; then
echo -e "\n# AI Model Cache Redirect\nexport HF_HOME=\"$VAULT/cache\"\nexport MODELSCOPE_CACHE=\"$VAULT/cache\"" >> ~/.bashrc
echo -e "${GREEN}β
Environment variables added to ~/.bashrc${NC}"
fi
echo -e "${GREEN}β
Initialization complete. Please run: source ~/.bashrc${NC}"
}
# 3. Path Mapping Helper
generate_mapping() {
if ! check_vault_integrity; then
echo -e "${RED}β οΈ Warning: The repository is not ready, Please execute option [1] first.${NC}"
read -p "Press Enter..."
return
fi
echo -e "\n${YELLOW}[Path Mapping Helper]${NC}"
read -p "Enter Target Path (e.g., /app/models/my_model): " INPUT_PATH
echo -e "Category: [1]LLM [2]Diff [3]VLM [4]LoRA [5]TRT"
read -p "Select [1-5]: " TYPE_NUM
BASE_NAME=$(basename "$INPUT_PATH")
case $TYPE_NUM in
1) SUB="base/llm" ;; 2) SUB="base/diffusion" ;; 3) SUB="base/vlm" ;;
4) SUB="lora" ;; 5) SUB="tensorrt" ;; *) return ;;
esac
TARGET_PATH="$VAULT/$SUB/$BASE_NAME"
echo -e "\n${BLUE}----------------------------------------------------${NC}"
echo -e "${GREEN}π Command Previews (Copy as needed):${NC}"
echo -e "${YELLOW}Option A (Local Symlink):${NC}"
echo "mkdir -p \$(dirname \"$INPUT_PATH\") && ln -s \"$TARGET_PATH\" \"$INPUT_PATH\""
echo -e "\n${YELLOW}Option B (Docker Volume Mount):${NC}"
echo "-v \"$TARGET_PATH\":\"$INPUT_PATH\" \\"
echo "-v \"$VAULT/cache\":\"/root/.cache/huggingface\""
if [ ! -e "$TARGET_PATH" ]; then
echo -e "\n${RED}β οΈ Note: Physical file does not exist yet. Please download to:${NC}"
echo -e "${GREEN}$TARGET_PATH${NC}"
fi
echo -e "${BLUE}----------------------------------------------------${NC}"
}
# 4. Model Import/Migration
import_model() {
if ! check_vault_integrity; then
echo -e "${RED}β Error: The model repository structure is incomplete or has not been initialized.${NC}"
echo -e "${YELLOW}Please return to the main menu and select option [1]${NC}"
read -p "Press Enter..."
return
fi
echo -e "\n${YELLOW}[Model Migration Tool]${NC}"
read -e -p "Enter current source path: " SRC_PATH
[ ! -e "$SRC_PATH" ] && { echo -e "${RED}β Source not found.${NC}"; return; }
echo -e "Target Category: [1]LLM [2]Diff [3]VLM [4]LoRA [5]TRT"
read -p "Select: " TYPE_NUM
case $TYPE_NUM in
1) SUB="base/llm" ;; 2) SUB="base/diffusion" ;; 3) SUB="base/vlm" ;;
4) SUB="lora" ;; 5) SUB="tensorrt" ;; *) return ;;
esac
DEST_DIR="$VAULT/$SUB"
CMD_COPY="rsync -avP \"$SRC_PATH\" \"$DEST_DIR/\""
CMD_MOVE="rsync -avP --remove-source-files \"$SRC_PATH\" \"$DEST_DIR/\""
echo -e "\n${GREEN}π Command Preview:${NC}"
echo -e "Copy Mode: $CMD_COPY"
echo -e "Move Mode: $CMD_MOVE"
read -p "Execute now? (y=Move / c=Copy / n=Manual): " MODE
[ "$MODE" == "y" ] && eval $CMD_MOVE
[ "$MODE" == "c" ] && eval $CMD_COPY
}
# 5. DGX / High Performance Docker Template
generate_docker_template() {
if ! check_vault_integrity; then
echo -e "${RED}β Error: The model repository structure is incomplete or has not been initialized.${NC}"
echo -e "${YELLOW}Please return to the main menu and select option [1]${NC}"
read -p "Press Enter..."
return
fi
echo -e "\n${YELLOW}[DGX / High Performance Docker Template]${NC}"
read -p "Enter Image Name (Default: nvcr.io/nvidia/pytorch:24.01-py3): " DOCKER_IMAGE
DOCKER_IMAGE=${DOCKER_IMAGE:-"nvcr.io/nvidia/pytorch:24.01-py3"}
local CURRENT_UID=$(id -u)
local CURRENT_GID=$(id -g)
echo -e "\n${GREEN}π Optimized Launch Command:${NC}"
echo -e "${BLUE}----------------------------------------------------${NC}"
echo "docker run -it --rm \\"
echo " --gpus all \\"
echo " --shm-size=16g \\" # Prevents Bus Errors in DataLoader
echo " --ulimit memlock=-1 \\"
echo " --ulimit stack=67108864 \\"
echo " --user $CURRENT_UID:$CURRENT_GID \\" # Fixes Permission Issues
echo " -v \"$VAULT/cache\":\"/root/.cache/huggingface\" \\"
echo " -v \"$VAULT\":\"$VAULT\" \\"
echo " -e HF_HOME=\"$VAULT/cache\" \\"
echo " -e HF_ENDPOINT=https://hf-mirror.com \\" # Fast mirror for CN users
echo " $DOCKER_IMAGE /bin/bash"
echo -e "${BLUE}----------------------------------------------------${NC}"
echo -e "${CYAN}π‘ Optimization Notes:${NC}"
echo "1. --shm-size: Essential for DGX/Multi-GPU training."
echo "2. --user: Ensures files created in container are accessible by you on Host."
echo "3. Consistency: Using identical paths in Host/Container prevents config errors."
}
# --- Main Logic ---
while true; do
clear
show_header
show_tree
echo -e "Actions:"
echo "1) π Initialize / Fix Vault"
echo "2) π Generate Mapping Commands"
echo "3) π¦ Migrate Existing Models"
echo "4) π³ DGX Optimized Docker Template"
echo "5) ποΈ Generate Uninstall Command"
echo "q) Quit"
read -p ">>> " MAIN_OPT
case $MAIN_OPT in
1) init_vault; read -p "Press Enter...";;
2) generate_mapping; read -p "Press Enter...";;
3) import_model; read -p "Press Enter...";;
4) generate_docker_template; read -p "Press Enter...";;
5)
echo -e "\n${RED}To uninstall, run these manually:${NC}"
echo "sudo rm -rf \"$VAULT\""
echo "sed -i '/# AI Model Cache Redirect/d' ~/.bashrc"
echo "sed -i '/HF_HOME/d' ~/.bashrc"
echo "sed -i '/MODELSCOPE_CACHE/d' ~/.bashrc"
read -p "Press Enter..."
;;
q) exit 0;;
*) sleep 1;;
esac
done