Skip to content

Commit d179690

Browse files
committed
Added tensors and input objects as global objects so they don't need to be created in every forward pass. In this manner, any possible dataleak is reduced
1 parent 7d57548 commit d179690

1 file changed

Lines changed: 21 additions & 24 deletions

File tree

src/features/spam-demo/detector.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import * as ort from 'onnxruntime-web';
2-
ort.env.wasm.wasmPaths = '/public/wasm/onnxruntime/';
3-
console.log(ort.env.wasm.wasmPaths)
42

53
let sessionFull : any = null;
6-
let mergesPath : string = "public/vocab/merges_all_18k.txt";
7-
let vocabPath : string = "public/vocab/vocab_all_18k.json";
8-
let modelPath : string = "public/onnx/mail_180226_02.onnx";
4+
const mergesPath : string = "public/vocab/merges_all_18k.txt";
5+
const vocabPath : string = "public/vocab/vocab_all_18k.json";
6+
const modelPath : string = "public/onnx/mail_180226_02.onnx";
7+
const wasmPath: string = '/public/wasm/onnxruntime/';
98

9+
ort.env.wasm.wasmPaths = wasmPath;
1010
const LENTOKENS = 128;
1111
const finalVocab = await loadVocab(vocabPath);
1212
const vstr = await v2str(vocabPath);
13-
console.log("cpp vocab", vstr);
1413
const mstr = await m2str(mergesPath);
15-
console.log("cpp merges:", mstr);
1614

1715
try {
1816
sessionFull = await ort.InferenceSession.create(modelPath, {
@@ -22,9 +20,14 @@ try {
2220
} catch (e) {
2321
console.error("Failed to load full model:", e);
2422
}
25-
26-
27-
export async function loadTokenizer() {
23+
const tensorial = new ort.Tensor("int64",new BigInt64Array(LENTOKENS),[1,LENTOKENS]);
24+
const atten = new ort.Tensor('float32',new Float32Array(LENTOKENS),[1,LENTOKENS])
25+
const input_tensor = {
26+
input: tensorial,
27+
attention: atten,
28+
};
29+
30+
async function loadTokenizer() {
2831
return new Promise((resolve) => {
2932
const script = document.createElement('script')
3033
script.src = 'public/wasm/tokenizer/tokenizer.js'
@@ -40,7 +43,9 @@ export async function loadTokenizer() {
4043
}
4144

4245
const tokenizer : any = await loadTokenizer()
43-
export async function loadVocab(path: string) {
46+
47+
48+
async function loadVocab(path: string) {
4449
const response = await fetch(path);
4550
let text = await response.text();
4651
text = text.slice(1,-1);
@@ -92,7 +97,7 @@ async function m2str(path: string) {
9297
}
9398

9499

95-
function runTokenizer(text: string) {
100+
function runTokenizer(text: string) :[BigInt64Array, Float32Array] {
96101

97102
const vocabPtr = vstr.map(str => {
98103
const utf8Length = tokenizer.lengthBytesUTF8(str);
@@ -144,7 +149,7 @@ function runTokenizer(text: string) {
144149

145150
function color(v: any) {
146151
const g = Math.round(255 * v);
147-
return `rgb(0,0,${g})`;
152+
return `rgb(${255-g},${255-g},255)`;
148153
}
149154

150155

@@ -161,18 +166,11 @@ export async function spamOrHam(message: string | undefined, result: any, explan
161166
if (message) {
162167
let messagec = message.replaceAll("\n"," ")
163168
console.time("Token");
164-
console.log(messagec);
165-
const [outputTokens, attention] = runTokenizer(messagec);
169+
const [outputTokens, atten] = runTokenizer(messagec);
166170
console.timeEnd("Token")
167-
console.log("Texto tokenizado:",outputTokens);
168-
console.log("Mascara tokens:",attention);
169171
console.time("Tensor");
170-
const tensorial = new ort.Tensor("int64",outputTokens,[1,LENTOKENS]);
171-
const atten = new ort.Tensor('float32',attention,[1,LENTOKENS])
172-
const input_tensor = {
173-
input: tensorial,
174-
attention: atten,
175-
};
172+
input_tensor.input.data.set(outputTokens);
173+
input_tensor.attention.data.set(atten);
176174
console.timeEnd("Tensor")
177175
console.time("Inferencia")
178176
let running = null;
@@ -182,7 +180,6 @@ export async function spamOrHam(message: string | undefined, result: any, explan
182180
let relevancies;
183181
if (Object.values(running).length > 1){
184182
relevancies = running.saliencies.data;
185-
console.log("Relevancies", relevancies);
186183
}
187184
console.log("Salida del modelo: ", spam);
188185
let s_result = "";

0 commit comments

Comments
 (0)