-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_manager.ts
273 lines (252 loc) · 7.69 KB
/
model_manager.ts
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import type { TypedDLOpenDynamicLib } from "https://deno.land/x/[email protected]/mod.ts";
import type { Model } from "./model.ts";
import { QAModel } from "./models/qa.ts";
import { NERModel } from "./models/ner.ts";
import { SentimentModel } from "./models/sentiment.ts";
import { TranslationModel } from "./models/translation/mod.ts";
import { ConversationModel } from "./models/conversation.ts";
import { ZeroShotClassificationModel } from "./models/zero_shot_classification.ts";
import type { TranslationModelInit } from "./models/translation/mod.ts";
import { POSModel } from "./models/pos.ts";
import { TextGenerationModel } from "./models/text_generation.ts";
import { SummarizationModel } from "./models/summarization.ts";
import { encode } from "./utils/encode.ts";
import { decode } from "./utils/decode.ts";
import { BertMLError } from "./error.ts";
import { Plug } from "https://deno.land/x/[email protected]/mod.ts";
const symbolDefinitions = {
create_qa_model: { parameters: [], result: "isize", nonblocking: true },
qa_query: {
parameters: ["usize", "buffer", "usize", "usize", "usize"],
result: "isize",
nonblocking: true,
},
create_ner_model: { parameters: [], result: "isize", nonblocking: true },
ner_predict: {
parameters: ["usize", "buffer", "usize"],
result: "isize",
nonblocking: true,
},
create_sentiment_model: {
parameters: [],
result: "isize",
nonblocking: true,
},
sentiment_predict: {
parameters: ["usize", "buffer", "usize"],
result: "isize",
nonblocking: true,
},
create_translation_model: {
parameters: ["buffer", "usize"],
result: "isize",
nonblocking: true,
},
translation_translate: {
parameters: ["usize", "buffer", "usize"],
result: "isize",
nonblocking: true,
},
create_conversation_model: {
parameters: [],
result: "isize",
nonblocking: true,
},
create_conversation_manager: {
parameters: [],
result: "isize",
nonblocking: true,
},
create_conversation: {
parameters: ["usize"],
result: "isize",
nonblocking: true,
},
conversation_send: {
parameters: ["usize", "usize", "usize", "buffer", "usize"],
result: "isize",
nonblocking: true,
},
create_pos_model: {
parameters: [],
result: "isize",
nonblocking: true,
},
pos_predict: {
parameters: ["usize", "buffer", "usize"],
result: "isize",
nonblocking: true,
},
create_zero_shot_model: {
parameters: [],
result: "isize",
nonblocking: true,
},
zero_shot_predict: {
parameters: ["usize", "buffer", "usize"],
result: "isize",
nonblocking: true,
},
zero_shot_predict_multilabel: {
parameters: ["usize", "buffer", "usize"],
result: "isize",
nonblocking: true,
},
create_text_generation_model: {
parameters: [],
result: "isize",
nonblocking: true,
},
text_generation_generate: {
parameters: ["usize", "buffer", "usize"],
result: "isize",
nonblocking: true,
},
create_summarization_model: {
parameters: [],
result: "isize",
nonblocking: true,
},
summarization_summarize: {
parameters: ["usize", "buffer", "usize"],
result: "isize",
nonblocking: true,
},
error_len: { parameters: [], result: "usize", nonblocking: true },
fill_result: {
parameters: ["buffer", "usize"],
result: "void",
nonblocking: true,
},
fill_error: { parameters: ["buffer"], result: "void", nonblocking: true },
delete_model: { parameters: ["usize"], result: "isize", nonblocking: true },
} as const;
type FFISymbols = TypedDLOpenDynamicLib<typeof symbolDefinitions>["symbols"];
/** Provides an abstraction for creating models that run on the same native thread (but don't block the JS thread). */
export class ModelManager {
#symbols: FFISymbols;
#close: () => void;
#models: Model[] = [];
#isClosed = false;
isClosed(): boolean {
return this.#isClosed;
}
#assertCode = async (code: number) => {
if (code < 0) {
const { bindings } = this;
const len = await bindings.error_len();
const buf = new Uint8Array(len);
await bindings.fill_error(buf);
throw new BertMLError(decode(buf));
}
return code;
};
/** Asserts a value returned from a FFI function and throws with the error pulled from Rust stack if present. */
get assertCode(): (code: number) => Promise<number> {
return this.#assertCode.bind(this);
}
/** The native FFI functions - only use this if you know what you're doing. */
get bindings(): FFISymbols {
return this.#symbols;
}
#helpers = {
getResult: async (len: number): Promise<Uint8Array> => {
const buf = new Uint8Array(len);
await this.bindings.fill_result(buf, len);
return buf;
},
getResultString: async (len: number): Promise<string> => {
return decode(await this.helpers.getResult(len));
},
} as const;
get helpers() {
return this.#helpers;
}
constructor(lib: Deno.DynamicLibrary<any>) {
this.#symbols = lib.symbols as any;
this.#close = lib.close.bind(lib);
}
static async create(): Promise<ModelManager> {
const lib = await Plug.prepare(
{
name: "bertml",
url: "https://github.com/sno2/bertml/releases/download/v0.1.0-alpha2/",
},
symbolDefinitions as any
);
return new ModelManager(lib);
}
async createQAModel(): Promise<QAModel> {
const rid = await this.bindings.create_qa_model().then(this.assertCode);
const model = new QAModel(this, rid);
this.#models.push(model);
return model;
}
async createNERModel(): Promise<NERModel> {
const rid = await this.bindings.create_ner_model().then(this.assertCode);
const model = new NERModel(this, rid);
this.#models.push(model);
return model;
}
async createSentimentModel(): Promise<SentimentModel> {
const rid = await this.bindings
.create_sentiment_model()
.then(this.assertCode);
const model = new SentimentModel(this, rid);
this.#models.push(model);
return model;
}
async createTranslationModel<T extends TranslationModelInit>(
init: T
): Promise<TranslationModel<T>> {
const bytes = encode(JSON.stringify(init));
const rid = await this.bindings
.create_translation_model(bytes, bytes.length)
.then(this.assertCode);
const model = new TranslationModel(this, rid, init);
this.#models.push(model);
return model;
}
async createConversationModel(): Promise<ConversationModel> {
const rid = await this.bindings
.create_conversation_model()
.then(this.assertCode);
const model = new ConversationModel(this, rid);
this.#models.push(model);
return model;
}
async createPOSModel(): Promise<POSModel> {
const rid = await this.bindings.create_pos_model().then(this.assertCode);
const model = new POSModel(this, rid);
this.#models.push(model);
return model;
}
async createZeroShotClassificationModel(): Promise<ZeroShotClassificationModel> {
const rid = await this.bindings
.create_zero_shot_model()
.then(this.assertCode);
const model = new ZeroShotClassificationModel(this, rid);
this.#models.push(model);
return model;
}
async createTextGenerationModel(): Promise<TextGenerationModel> {
const rid = await this.bindings
.create_text_generation_model()
.then(this.assertCode);
const model = new TextGenerationModel(this, rid);
this.#models.push(model);
return model;
}
async createSummarizationModel(): Promise<SummarizationModel> {
const rid = await this.bindings
.create_summarization_model()
.then(this.assertCode);
const model = new SummarizationModel(this, rid);
this.#models.push(model);
return model;
}
close() {
this.#close();
this.#isClosed = true;
}
}