-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.js
190 lines (170 loc) ยท 5.05 KB
/
inference.js
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
/** */
/*global BigInt */
/*global BigInt64Array */
import { loadTokenizer } from './bert_tokenizer.ts';
// import * as wasmFeatureDetect from 'wasm-feature-detect';
//Setup onnxruntime
const ort = require('onnxruntime-web');
//requires Cross-Origin-*-policy headers https://web.dev/coop-coep/
/**
const simdResolver = wasmFeatureDetect.simd().then(simdSupported => {
console.log("simd is supported? "+ simdSupported);
if (simdSupported) {
ort.env.wasm.numThreads = 3;
ort.env.wasm.simd = true;
} else {
ort.env.wasm.numThreads = 1;
ort.env.wasm.simd = false;
}
});
*/
const options = {
executionProviders: ['wasm'],
graphOptimizationLevel: 'all'
};
var downLoadingModel = true;
const model = "./xtremedistill-go-emotion-int8.onnx";
// const gruModel = "./gru_embedder(1,21,80).onnx";
const gruModel = "./gru_embedder(1,40,80).onnx";
const gruSession = ort.InferenceSession.create(gruModel, options);
const session = ort.InferenceSession.create(model, options);
session.then(t => {
downLoadingModel = false;
//warmup the VM
for(var i = 0; i < 10; i++) {
console.log("Inference warmup " + i);
lm_inference("this is a warmup inference");
}
});
const tokenizer = loadTokenizer()
const EMOJI_DEFAULT_DISPLAY = [
["Emotion", "Score"],
['admiration ๐',0],
['amusement ๐', 0],
['neutral ๐',0],
['approval ๐',0],
['joy ๐',0],
['gratitude ๐',0],
];
const EMOJIS = [
'admiration ๐',
'amusement ๐',
'anger ๐ก',
'annoyance ๐',
'approval ๐',
'caring ๐ค',
'confusion ๐',
'curiosity ๐ค',
'desire ๐',
'disappointment ๐',
'disapproval ๐',
'disgust ๐คฎ',
'embarrassment ๐ณ',
'excitement ๐คฉ',
'fear ๐จ',
'gratitude ๐',
'grief ๐ข',
'joy ๐',
'love โค๏ธ',
'nervousness ๐ฌ',
'optimism ๐ค',
'pride ๐',
'realization ๐ก',
'relief๐
',
'remorse ๐',
'sadness ๐',
'surprise ๐ฒ',
'neutral ๐'
];
function isDownloading() {
return downLoadingModel;
}
function sortResult(a, b) {
if (a[1] === b[1]) {
return 0;
}
else {
return (a[1] < b[1]) ? 1 : -1;
}
}
function sigmoid(t) {
return 1/(1+Math.pow(Math.E, -t));
}
function create_model_input(encoded) {
var input_ids = new Array(encoded.length+2);
var attention_mask = new Array(encoded.length+2);
var token_type_ids = new Array(encoded.length+2);
input_ids[0]ย = BigInt(101);
attention_mask[0]ย = BigInt(1);
token_type_ids[0]ย = BigInt(0);
var i = 0;
for(; i < encoded.length; i++) {
input_ids[i+1] = BigInt(encoded[i]);
attention_mask[i+1] = BigInt(1);
token_type_ids[i+1] = BigInt(0);
}
input_ids[i+1]ย = BigInt(102);
attention_mask[i+1]ย = BigInt(1);
token_type_ids[i+1]ย = BigInt(0);
const sequence_length = input_ids.length;
input_ids = new ort.Tensor('int64', BigInt64Array.from(input_ids), [1,sequence_length]);
attention_mask = new ort.Tensor('int64', BigInt64Array.from(attention_mask), [1,sequence_length]);
token_type_ids = new ort.Tensor('int64', BigInt64Array.from(token_type_ids), [1,sequence_length]);
return {
input_ids: input_ids,
attention_mask: attention_mask,
token_type_ids:token_type_ids
}
}
async function lm_inference(text) {
try {
const encoded_ids = await tokenizer.then(t => {
return t.tokenize(text);
});
if(encoded_ids.length === 0) {
return [0.0, EMOJI_DEFAULT_DISPLAY];
}
const start = performance.now();
const model_input = create_model_input(encoded_ids);
const output = await session.then(s => { return s.run(model_input,['output_0'])});
const duration = (performance.now() - start).toFixed(1);
const probs = output['output_0'].data.map(sigmoid).map( t => Math.floor(t*100));
const result = [];
for(var i = 0; i < EMOJIS.length;i++) {
const t = [EMOJIS[i], probs[i]];
result[i] = t;
}
result.sort(sortResult);
const result_list = [];
result_list[0] = ["Emotion", "Score"];
for(i = 0; i < 6; i++) {
result_list[i+1] = result[i];
}
return [duration,result_list];
} catch (e) {
return [0.0,EMOJI_DEFAULT_DISPLAY];
}
}
async function gru_inference(melLogSpectrogram) {
try {
const inputs = ort.InferenceSession.FeedsType = {
// input: new ort.Tensor('float32', melLogSpectrogram.flat(), [1, 21, 80])
input: new ort.Tensor('float32', melLogSpectrogram.flat(), [1, 40, 80])
};
// console.log(171, 'input: ', inputs.input.data);
// console.log('input sum: ', inputs.inpumot.data.reduce((a, b) => a + b, 0));
// console.log(inputs.input);
const output = await gruSession.then(s => { return s.run(inputs)});
// console.log(175, 'output: ', output.output.data);
// sum all data
// const sum = output.output.data.reduce((a, b) => a + b, 0);
// console.log('output sum: ', sum);
return output;
} catch (e) {
console.log(e);
}
}
export let inference = lm_inference
export let columnNames = EMOJI_DEFAULT_DISPLAY
export let modelDownloadInProgress = isDownloading
export let gruInference = gru_inference