-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
195 lines (174 loc) · 5.5 KB
/
util.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
191
192
193
194
195
// Get model via Origin Private File System
async function getModelOPFS(name, url) {
const root = await navigator.storage.getDirectory();
let fileHandle;
let buffer;
try {
fileHandle = await root.getFileHandle(name);
const blob = await fileHandle.getFile();
buffer = await blob.arrayBuffer();
} catch(e) {
const response = await fetch(url);
buffer = await readResponse(response);
fileHandle = await root.getFileHandle(name, {create: true});
const writable = await fileHandle.createWritable();
await writable.write(buffer);
await writable.close();
}
return buffer;
}
// Get model via Cache API
async function getModelCache(name, url) {
const cache = await caches.open(name);
let response = await cache.match(url);
if (!response) {
await cache.add(url);
response = await cache.match(url);
}
const buffer = await readResponse(response);
return buffer;
}
async function readResponse(response) {
const contentLength = response.headers.get('Content-Length');
let total = parseInt(contentLength ?? '0');
let buffer = new Uint8Array(total);
let loaded = 0;
const reader = response.body.getReader();
async function read() {
const { done, value } = await reader.read();
if (done) return;
let newLoaded = loaded + value.length;
if (newLoaded > total) {
total = newLoaded;
let newBuffer = new Uint8Array(total);
newBuffer.set(buffer);
buffer = newBuffer;
}
buffer.set(value, loaded);
loaded = newLoaded;
return read();
}
await read();
return buffer;
}
function reportStatus(status) {
document.getElementById('status').innerHTML = status;
}
function getSum(data) {
return data.reduce((accumulator, currentValue) => {
return accumulator + currentValue
}, 0);
}
function toggleClass(el, className) {
if (el.className.indexOf(className) >= 0) {
el.className = el.className.replace(className, '');
} else {
el.className += className;
}
}
function compare(actual, expected, epsilon = 1e-3) {
try {
areCloseObjects(actual, expected, epsilon);
} catch (e) {
return false;
}
return true;
}
function areCloseObjects(actual, expected, epsilon) {
let actualKeys = Object.getOwnPropertyNames(actual);
let expectedKeys = Object.getOwnPropertyNames(expected);
if (actualKeys.length != expectedKeys.length) {
throw new Error(`Actual length ${actualKeys.length} not equal Expected length ${expectedKeys.length}`);
}
for (let i = 0; i < actualKeys.length; i++) {
let key = actualKeys[i];
let isArray = isTypedArray(actual[key]) && isTypedArray(expected[key]);
let isObject = typeof (actual[key]) === 'object' && typeof (expected[key]) === 'object';
if (isArray) {
areCloseArrays(actual[key], expected[key], epsilon);
} else if (isObject) {
areCloseObjects(actual[key], expected[key], epsilon);
} else {
if (!areClosePrimitives(actual[key], expected[key])) {
throw new Error(`Objects differ: actual[${key}] = ${JSON.stringify(actual[key])}, expected[${key}] = ${JSON.stringify(expected[key])}!`);
}
}
}
return true;
}
function areCloseArrays(actual, expected, epsilon) {
let checkClassType = true;
if (isTypedArray(actual) || isTypedArray(expected)) {
checkClassType = false;
}
if (isTypedArray(actual) && isTypedArray(expected)) {
checkClassType = true;
}
if (checkClassType) {
const aType = actual.constructor.name;
const bType = expected.constructor.name;
if (aType !== bType) {
throw new Error(`Arrays are of different type. Actual: ${aType}. Expected: ${bType}`);
}
}
const actualFlat = isTypedArray(actual) ? actual : flatten(actual);
const expectedFlat = isTypedArray(expected) ? expected : flatten(expected);
if (actualFlat.length !== expectedFlat.length) {
throw new Error(
`Arrays have different lengths actual: ${actualFlat.length} vs ` +
`expected: ${expectedFlat.length}.\n` +
`Actual: ${actualFlat}.\n` +
`Expected: ${expectedFlat}.`);
}
for (let i = 0; i < expectedFlat.length; ++i) {
const a = actualFlat[i];
const e = expectedFlat[i];
if (!areClosePrimitives(a, e)) {
throw new Error(
`Arrays differ: actual[${i}] = ${a}, expected[${i}] = ${e}.\n` +
`Actual: ${actualFlat}.\n` +
`Expected: ${expectedFlat}.`);
}
}
}
function areClosePrimitives(actual, expected, epsilon) {
if (!isFinite(actual) && !isFinite(expected)) {
return true;
} else if (isNaN(actual) || isNaN(expected)) {
return false;
}
const error = Math.abs(actual - expected);
if (Math.abs(actual) >= 1) {
if ((error > 1e-1) || error / Math.min(Math.abs(actual), Math.abs(expected)) > epsilon) {
return false;
}
} else {
if (error > epsilon) {
return false;
}
}
return true;
}
function isTypedArray(object) {
return ArrayBuffer.isView(object) && !(object instanceof DataView);
}
const type_to_func = {
float32: Float32Array,
uint16: Uint16Array,
float16: Uint16Array,
int32: Int32Array,
BigInt64Array: BigInt64Array,
};
function clone(x) {
let feed = {};
for (const [key, value] of Object.entries(x)) {
let func = type_to_func[value.type];
let arrayType = func.from(value.data);
feed[key] = new ort.Tensor(
value.type,
arrayType.slice(0),
value.dims
);
}
return feed;
}