-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodel.ts
More file actions
149 lines (135 loc) · 5.04 KB
/
model.ts
File metadata and controls
149 lines (135 loc) · 5.04 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
// 模型服务下载,删除逻辑
import logger from '../../logger';
import * as $Dev20230714 from '@alicloud/devs20230714';
import { IInputs } from '../../interface';
import {
extractOssMountDir,
initClient,
retryFileManagerRsyncAndCheckStatus,
retryFileManagerRm,
} from './utils';
export class ModelService {
logger = logger;
region: string;
constructor(private inputs: IInputs) {
const { region } = this.inputs.props;
this.region = region;
}
async downloadModel(name, params) {
const devClient = await initClient(this.inputs, this.region, 'fun-model');
const { nasMountPoints, ossMountPoints, role, modelConfig, vpcConfig, region, storage } =
params;
// 判断modelConfig.source是否是modelscope://、oss://或nas://
let source;
const reversion = modelConfig.reversion ? `@${modelConfig.reversion}` : '';
if (
modelConfig.source.startsWith('modelscope') &&
!modelConfig.source.startsWith('modelscope://')
) {
source = `modelscope://${modelConfig.model}${reversion}`;
} else {
source = `${modelConfig.source}${reversion}`;
}
const validSourcePattern = /^(modelscope|oss):\/\//;
if (!validSourcePattern.test(source)) {
throw new Error(
`Invalid source path. Expected a valid URI starting with 'modelscope://', or 'oss://', but got: ${modelConfig.source}`,
);
}
if (modelConfig.mode === 'never') {
logger.info(
'[Download-model] Skipping model download as modelConfig.mode is set to "never".',
);
return;
}
const processedOssMountPoints = extractOssMountDir(ossMountPoints);
// mode 是 once 时候,判断是否已经下载过
const destination =
storage === 'nas'
? `file:/${nasMountPoints[0].mountDir}`
: `file:/${processedOssMountPoints[0].mountDir}`;
if (modelConfig.mode === 'once') {
const ListFileManagerTasksRequest = new $Dev20230714.ListFileManagerTasksRequest({
name,
});
const res = await devClient.listFileManagerTasks(ListFileManagerTasksRequest);
logger.debug('listFileManagerTasks', JSON.stringify(res, null, 2));
const { tasks } = res.body.data;
const needDownload = !tasks.some(
(task) =>
task.finished &&
task.success &&
task.progress.currentBytes === task.progress.totalBytes &&
task.parameters.source === source &&
task.parameters.destination === destination,
);
if (!needDownload) {
logger.info('[Download-model] The model has been downloaded.');
return;
}
}
const fileManagerRsyncRequest = new $Dev20230714.FileManagerRsyncRequest({
mountConfig: new $Dev20230714.FileManagerMountConfig({
name,
nasMountPoints,
ossMountPoints: processedOssMountPoints,
role,
region,
vpcConfig,
timeoutInSecond: modelConfig.timeout,
}),
source,
destination,
conflictHandling: process.env.MODEL_CONFLIC_HANDLING || modelConfig.conflictResolution,
});
logger.debug(JSON.stringify(fileManagerRsyncRequest, null, 2));
// 使用公共方法重试fileManagerRsync + checkModelStatus流程
await retryFileManagerRsyncAndCheckStatus(
devClient,
fileManagerRsyncRequest,
'',
modelConfig.timeout,
2,
30,
);
}
async removeModel(name, params) {
const { nasMountPoints, ossMountPoints, role, region, vpcConfig, storage } = params;
const devClient = await initClient(this.inputs, this.region, 'fun-model');
const processedOssMountPoints = extractOssMountDir(ossMountPoints);
if (
storage === 'oss' &&
processedOssMountPoints[0] &&
(!processedOssMountPoints[0].bucketPath || processedOssMountPoints[0].bucketPath === '/')
) {
throw new Error(
'The current deleted directory is the OSS root directory. To delete the current model, please go to the OSS console to delete the model.',
);
}
const nasPath = nasMountPoints && nasMountPoints[0]?.serverAddr?.split(':')[1];
if (storage === 'nas' && nasMountPoints[0] && nasPath?.trim() === '/') {
throw new Error(
'The current deleted directory is the NAS root directory. To delete the current model, please go to the NAS console to delete the model.',
);
}
const fileManagerRmRequest = new $Dev20230714.FileManagerRmRequest({
filepath:
storage === 'nas' ? nasMountPoints[0]?.mountDir : processedOssMountPoints[0]?.mountDir,
mountConfig: new $Dev20230714.FileManagerMountConfig({
name,
nasMountPoints,
ossMountPoints: processedOssMountPoints,
role,
vpcConfig,
region,
}),
});
await retryFileManagerRm(devClient, fileManagerRmRequest, 'model', 3, 30);
// 清理任务记录
const deleteFileManagerTasks = new $Dev20230714.RemoveFileManagerTasksRequest({
name,
});
const deleteTasks = await devClient.removeFileManagerTasks(deleteFileManagerTasks);
logger.debug('deleteTasks', JSON.stringify(deleteTasks, null, 2));
}
}