@automatalabs/react-native-transformers 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/app.plugin.js +1 -1
- package/dist/app.plugin.d.ts +3 -0
- package/dist/app.plugin.d.ts.map +1 -0
- package/dist/app.plugin.js +4 -0
- package/dist/app.plugin.js.map +1 -0
- package/dist/plugin/src/index.d.ts +4 -0
- package/dist/plugin/src/index.d.ts.map +1 -0
- package/dist/plugin/src/index.js +16 -0
- package/dist/plugin/src/index.js.map +1 -0
- package/dist/src/adapter/onnxruntime-web-webgpu.d.ts +47 -0
- package/dist/src/adapter/onnxruntime-web-webgpu.d.ts.map +1 -0
- package/dist/src/adapter/onnxruntime-web-webgpu.js +256 -0
- package/dist/src/adapter/onnxruntime-web-webgpu.js.map +1 -0
- package/dist/src/expoFileSystemCache.d.ts +17 -0
- package/dist/src/expoFileSystemCache.d.ts.map +1 -0
- package/dist/src/expoFileSystemCache.js +240 -0
- package/dist/src/expoFileSystemCache.js.map +1 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +16 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/metro.d.ts +21 -0
- package/dist/src/metro.d.ts.map +1 -0
- package/dist/src/metro.js +90 -0
- package/dist/src/metro.js.map +1 -0
- package/dist/src/runtime.d.ts +17 -0
- package/dist/src/runtime.d.ts.map +1 -0
- package/dist/src/runtime.js +511 -0
- package/dist/src/runtime.js.map +1 -0
- package/dist/src/transformers.d.ts +19 -0
- package/dist/src/transformers.d.ts.map +1 -0
- package/dist/src/transformers.js +224 -0
- package/dist/src/transformers.js.map +1 -0
- package/dist/src/types.d.ts +96 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/package.json +58 -13
- package/plugin/src/index.js +0 -13
- package/src/adapter/onnxruntime-web-webgpu.js +0 -246
- package/src/expoFileSystemCache.js +0 -251
- package/src/index.js +0 -27
- package/src/metro.js +0 -66
- package/src/runtime.js +0 -535
- package/src/transformers.js +0 -225
package/README.md
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
# @automatalabs/react-native-transformers
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@automatalabs/react-native-transformers)
|
|
4
|
+
|
|
3
5
|
Use [`@huggingface/transformers`](https://www.npmjs.com/package/@huggingface/transformers) in Expo / React Native apps through [`onnxruntime-react-native`](https://www.npmjs.com/package/onnxruntime-react-native), without forking Transformers.js.
|
|
4
6
|
|
|
7
|
+
For npm release automation and trusted publisher setup, see [docs/npm-publishing.md](./docs/npm-publishing.md).
|
|
8
|
+
|
|
5
9
|
## What this package does
|
|
6
10
|
|
|
7
11
|
- adds an Expo config plugin that composes `onnxruntime-react-native`
|
|
@@ -301,7 +305,7 @@ But you generally cannot prove exact per-op fallback from JavaScript alone.
|
|
|
301
305
|
|
|
302
306
|
This package takes a no-fork approach:
|
|
303
307
|
|
|
304
|
-
- aliases `@huggingface/transformers` to `src/transformers.
|
|
308
|
+
- aliases `@huggingface/transformers` to the TypeScript-authored wrapper in `src/transformers.ts`
|
|
305
309
|
- aliases `onnxruntime-node`, `onnxruntime-web`, and `onnxruntime-web/webgpu` to a React Native adapter
|
|
306
310
|
- reuses the unified ONNX Runtime JavaScript API shape exposed by `onnxruntime-react-native`
|
|
307
311
|
- patches the create-session path for React Native buffer / external-data model loading
|
package/app.plugin.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require('./plugin
|
|
1
|
+
module.exports = require('./dist/app.plugin.js');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.plugin.d.ts","sourceRoot":"","sources":["../app.plugin.ts"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,GAAG,QAAQ,cAAc,CAAC,CAAC;AAE7D,SAAS,2BAA2B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.plugin.js","sourceRoot":"","sources":["../app.plugin.ts"],"names":[],"mappings":";AAAA,4DAA6D;AAE7D,iBAAS,2BAA2B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../plugin/src/index.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAY1C,iBAAS,2BAA2B,CAAC,OAAO,SAAS,UAAU,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAczF;AAED,SAAS,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
function requireFromProject(moduleId) {
|
|
3
|
+
return require(require.resolve(moduleId, { paths: [process.cwd(), __dirname] }));
|
|
4
|
+
}
|
|
5
|
+
function withReactNativeTransformers(config) {
|
|
6
|
+
const onnxruntimeReactNativePlugin = requireFromProject('onnxruntime-react-native/app.plugin');
|
|
7
|
+
const withOnnxruntimeReactNative = typeof onnxruntimeReactNativePlugin === 'function'
|
|
8
|
+
? onnxruntimeReactNativePlugin
|
|
9
|
+
: onnxruntimeReactNativePlugin.default;
|
|
10
|
+
if (typeof withOnnxruntimeReactNative !== 'function') {
|
|
11
|
+
throw new TypeError('onnxruntime-react-native/app.plugin did not export a config plugin.');
|
|
12
|
+
}
|
|
13
|
+
return withOnnxruntimeReactNative(config);
|
|
14
|
+
}
|
|
15
|
+
module.exports = withReactNativeTransformers;
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../plugin/src/index.ts"],"names":[],"mappings":";AAQA,SAAS,kBAAkB,CAAC,QAAgB;IAC1C,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;AACnF,CAAC;AAED,SAAS,2BAA2B,CAA6B,MAAe;IAC9E,MAAM,4BAA4B,GAAG,kBAAkB,CACrD,qCAAqC,CACH,CAAC;IACrC,MAAM,0BAA0B,GAC9B,OAAO,4BAA4B,KAAK,UAAU;QAChD,CAAC,CAAC,4BAA4B;QAC9B,CAAC,CAAC,4BAA4B,CAAC,OAAO,CAAC;IAE3C,IAAI,OAAO,0BAA0B,KAAK,UAAU,EAAE,CAAC;QACrD,MAAM,IAAI,SAAS,CAAC,qEAAqE,CAAC,CAAC;IAC7F,CAAC;IAED,OAAO,0BAA0B,CAAC,MAAM,CAAC,CAAC;AAC5C,CAAC;AAED,iBAAS,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as ortReactNative from 'onnxruntime-react-native';
|
|
2
|
+
import type { SessionHandler } from 'onnxruntime-react-native';
|
|
3
|
+
import type { RunOptions, SessionOptions } from '../types';
|
|
4
|
+
interface SupportedBackend {
|
|
5
|
+
name: string;
|
|
6
|
+
}
|
|
7
|
+
interface NativeValueMetadata {
|
|
8
|
+
name: string;
|
|
9
|
+
isTensor: boolean;
|
|
10
|
+
type: number;
|
|
11
|
+
shape: number[];
|
|
12
|
+
symbolicDimensions: string[];
|
|
13
|
+
}
|
|
14
|
+
interface NativeSessionOptions extends SessionOptions {
|
|
15
|
+
ortExtLibPath?: string | undefined;
|
|
16
|
+
}
|
|
17
|
+
interface NativeInferenceSessionImpl {
|
|
18
|
+
loadModel(modelPath: string, options: NativeSessionOptions): Promise<void>;
|
|
19
|
+
loadModel(buffer: ArrayBuffer, byteOffset: number, byteLength: number, options: NativeSessionOptions): Promise<void>;
|
|
20
|
+
readonly inputMetadata: NativeValueMetadata[];
|
|
21
|
+
readonly outputMetadata: NativeValueMetadata[];
|
|
22
|
+
run(feeds: SessionHandler.FeedsType, fetches: SessionHandler.FetchesType, options: RunOptions): Promise<SessionHandler.ReturnType>;
|
|
23
|
+
endProfiling(): void;
|
|
24
|
+
dispose(): void;
|
|
25
|
+
}
|
|
26
|
+
interface OrtApiLike {
|
|
27
|
+
createInferenceSession(): NativeInferenceSessionImpl;
|
|
28
|
+
listSupportedBackends?(): SupportedBackend[];
|
|
29
|
+
initOrtOnce(logLevel: number, tensorConstructor: typeof ortReactNative.Tensor): void;
|
|
30
|
+
}
|
|
31
|
+
declare global {
|
|
32
|
+
var OrtApi: OrtApiLike | undefined;
|
|
33
|
+
}
|
|
34
|
+
declare const adapterModule: {
|
|
35
|
+
InferenceSession: typeof ortReactNative.InferenceSession;
|
|
36
|
+
Tensor: ortReactNative.TensorConstructor;
|
|
37
|
+
env: ortReactNative.Env;
|
|
38
|
+
listSupportedBackends: () => import("onnxruntime-react-native/dist/typescript/api").SupportedBackend[];
|
|
39
|
+
registerBackend: (name: string, backend: ortReactNative.Backend, priority: number) => void;
|
|
40
|
+
TRACE: (deviceType: string, label: string) => void;
|
|
41
|
+
TRACE_FUNC_BEGIN: (extraMsg?: string) => void;
|
|
42
|
+
TRACE_FUNC_END: (extraMsg?: string) => void;
|
|
43
|
+
TRACE_EVENT_BEGIN: (extraMsg?: string) => void;
|
|
44
|
+
TRACE_EVENT_END: (extraMsg?: string) => void;
|
|
45
|
+
};
|
|
46
|
+
export = adapterModule;
|
|
47
|
+
//# sourceMappingURL=onnxruntime-web-webgpu.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onnxruntime-web-webgpu.d.ts","sourceRoot":"","sources":["../../../src/adapter/onnxruntime-web-webgpu.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,cAAc,MAAM,0BAA0B,CAAC;AAI3D,OAAO,KAAK,EAGV,cAAc,EAEf,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAiB,MAAM,UAAU,CAAC;AAE1E,UAAU,gBAAgB;IACxB,IAAI,EAAE,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AAgB;;;;;;+BA5VhG,CADA;6BAGuB,CAAC;gCAIhB,CAAC;8BAES,CAAC;CAwVc,CAAC;AAElC,SAAS,aAAa,CAAC"}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
const react_native_1 = require("react-native");
|
|
36
|
+
const ortReactNative = __importStar(require("onnxruntime-react-native"));
|
|
37
|
+
const runtime_1 = require("../runtime");
|
|
38
|
+
const Module = react_native_1.NativeModules.Onnxruntime;
|
|
39
|
+
if (typeof globalThis.OrtApi === 'undefined' && typeof Module?.install === 'function') {
|
|
40
|
+
Module.install();
|
|
41
|
+
}
|
|
42
|
+
const OrtApi = globalThis.OrtApi ??
|
|
43
|
+
new Proxy({}, {
|
|
44
|
+
get() {
|
|
45
|
+
throw new Error('OrtApi is not initialized. Please make sure Onnxruntime installation is successful.');
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
const dataTypeStrings = [
|
|
49
|
+
undefined,
|
|
50
|
+
'float32',
|
|
51
|
+
'uint8',
|
|
52
|
+
'int8',
|
|
53
|
+
'uint16',
|
|
54
|
+
'int16',
|
|
55
|
+
'int32',
|
|
56
|
+
'int64',
|
|
57
|
+
'string',
|
|
58
|
+
'bool',
|
|
59
|
+
'float16',
|
|
60
|
+
'float64',
|
|
61
|
+
'uint32',
|
|
62
|
+
'uint64',
|
|
63
|
+
undefined,
|
|
64
|
+
undefined,
|
|
65
|
+
undefined,
|
|
66
|
+
undefined,
|
|
67
|
+
undefined,
|
|
68
|
+
undefined,
|
|
69
|
+
undefined,
|
|
70
|
+
'uint4',
|
|
71
|
+
'int4',
|
|
72
|
+
];
|
|
73
|
+
function isPlainObject(value) {
|
|
74
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
75
|
+
}
|
|
76
|
+
function fillNamesAndMetadata(rawMetadata = []) {
|
|
77
|
+
const names = [];
|
|
78
|
+
const metadata = [];
|
|
79
|
+
for (const item of rawMetadata) {
|
|
80
|
+
names.push(item.name);
|
|
81
|
+
if (!item.isTensor) {
|
|
82
|
+
metadata.push({
|
|
83
|
+
name: item.name,
|
|
84
|
+
isTensor: false,
|
|
85
|
+
});
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const type = dataTypeStrings[item.type];
|
|
89
|
+
if (type === undefined) {
|
|
90
|
+
throw new Error(`Unsupported data type: ${item.type}`);
|
|
91
|
+
}
|
|
92
|
+
const shape = [];
|
|
93
|
+
for (let index = 0; index < item.shape.length; index += 1) {
|
|
94
|
+
const dim = item.shape[index];
|
|
95
|
+
if (dim === undefined) {
|
|
96
|
+
throw new Error(`Missing dimension at index ${index}.`);
|
|
97
|
+
}
|
|
98
|
+
if (dim === -1) {
|
|
99
|
+
const symbolicDimension = item.symbolicDimensions[index];
|
|
100
|
+
if (typeof symbolicDimension !== 'string') {
|
|
101
|
+
throw new Error(`Missing symbolic dimension at index ${index}.`);
|
|
102
|
+
}
|
|
103
|
+
shape.push(symbolicDimension);
|
|
104
|
+
}
|
|
105
|
+
else if (dim >= 0) {
|
|
106
|
+
shape.push(dim);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
throw new Error(`Invalid dimension: ${dim}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
metadata.push({
|
|
113
|
+
name: item.name,
|
|
114
|
+
isTensor: true,
|
|
115
|
+
type,
|
|
116
|
+
shape,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return [names, metadata];
|
|
120
|
+
}
|
|
121
|
+
function getLogLevelValue(logLevel) {
|
|
122
|
+
switch (logLevel) {
|
|
123
|
+
case 'verbose':
|
|
124
|
+
return 0;
|
|
125
|
+
case 'info':
|
|
126
|
+
return 1;
|
|
127
|
+
case 'warning':
|
|
128
|
+
case undefined:
|
|
129
|
+
return 2;
|
|
130
|
+
case 'error':
|
|
131
|
+
return 3;
|
|
132
|
+
case 'fatal':
|
|
133
|
+
return 4;
|
|
134
|
+
default:
|
|
135
|
+
throw new Error(`Unsupported log level: ${String(logLevel)}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function normalizeCreateArguments(args) {
|
|
139
|
+
const [arg0, arg1, arg2, arg3] = args;
|
|
140
|
+
if (typeof arg0 === 'string') {
|
|
141
|
+
if (arg1 !== undefined && !isPlainObject(arg1)) {
|
|
142
|
+
throw new TypeError("'options' must be an object.");
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
modelPath: arg0,
|
|
146
|
+
modelBytes: null,
|
|
147
|
+
options: arg1 ?? {},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
if (arg0 instanceof Uint8Array) {
|
|
151
|
+
if (arg1 !== undefined && !isPlainObject(arg1)) {
|
|
152
|
+
throw new TypeError("'options' must be an object.");
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
modelPath: null,
|
|
156
|
+
modelBytes: arg0,
|
|
157
|
+
options: arg1 ?? {},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
if (arg0 instanceof ArrayBuffer ||
|
|
161
|
+
(typeof SharedArrayBuffer !== 'undefined' && arg0 instanceof SharedArrayBuffer)) {
|
|
162
|
+
let byteOffset = 0;
|
|
163
|
+
let byteLength = arg0.byteLength;
|
|
164
|
+
let options = {};
|
|
165
|
+
if (isPlainObject(arg1)) {
|
|
166
|
+
options = arg1;
|
|
167
|
+
}
|
|
168
|
+
else if (typeof arg1 === 'number') {
|
|
169
|
+
byteOffset = arg1;
|
|
170
|
+
byteLength = typeof arg2 === 'number' ? arg2 : arg0.byteLength - byteOffset;
|
|
171
|
+
options = isPlainObject(arg3) ? arg3 : {};
|
|
172
|
+
}
|
|
173
|
+
else if (arg1 !== undefined) {
|
|
174
|
+
throw new TypeError("'options' must be an object.");
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
modelPath: null,
|
|
178
|
+
modelBytes: new Uint8Array(arg0, byteOffset, byteLength),
|
|
179
|
+
options,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");
|
|
183
|
+
}
|
|
184
|
+
function toExactArrayBuffer(uint8Array) {
|
|
185
|
+
if (uint8Array.byteOffset === 0 &&
|
|
186
|
+
uint8Array.byteLength === uint8Array.buffer.byteLength &&
|
|
187
|
+
uint8Array.buffer instanceof ArrayBuffer) {
|
|
188
|
+
return uint8Array.buffer;
|
|
189
|
+
}
|
|
190
|
+
return uint8Array.slice().buffer;
|
|
191
|
+
}
|
|
192
|
+
class ReactNativeSessionHandler {
|
|
193
|
+
inputNames;
|
|
194
|
+
outputNames;
|
|
195
|
+
inputMetadata;
|
|
196
|
+
outputMetadata;
|
|
197
|
+
#inferenceSession;
|
|
198
|
+
constructor(session) {
|
|
199
|
+
this.#inferenceSession = session;
|
|
200
|
+
const [inputNames, inputMetadata] = fillNamesAndMetadata(session.inputMetadata);
|
|
201
|
+
const [outputNames, outputMetadata] = fillNamesAndMetadata(session.outputMetadata);
|
|
202
|
+
this.inputNames = inputNames;
|
|
203
|
+
this.outputNames = outputNames;
|
|
204
|
+
this.inputMetadata = inputMetadata;
|
|
205
|
+
this.outputMetadata = outputMetadata;
|
|
206
|
+
}
|
|
207
|
+
async run(feeds, fetches, options) {
|
|
208
|
+
return this.#inferenceSession.run(feeds, fetches, options);
|
|
209
|
+
}
|
|
210
|
+
async dispose() {
|
|
211
|
+
this.#inferenceSession.dispose();
|
|
212
|
+
}
|
|
213
|
+
startProfiling() {
|
|
214
|
+
// no-op; profiling is enabled at load time by session options if requested
|
|
215
|
+
}
|
|
216
|
+
endProfiling() {
|
|
217
|
+
this.#inferenceSession.endProfiling();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const BaseInferenceSession = ortReactNative.InferenceSession;
|
|
221
|
+
class PatchedInferenceSession extends BaseInferenceSession {
|
|
222
|
+
static #initialized = false;
|
|
223
|
+
static async create(...args) {
|
|
224
|
+
const { modelPath, modelBytes, options } = normalizeCreateArguments(args);
|
|
225
|
+
const sessionOptionsInput = {
|
|
226
|
+
...options,
|
|
227
|
+
ortExtLibPath: options.ortExtLibPath ?? Module?.ORT_EXTENSIONS_PATH,
|
|
228
|
+
};
|
|
229
|
+
const sessionOptions = (0, runtime_1.sanitizeSessionOptions)(sessionOptionsInput);
|
|
230
|
+
if (!PatchedInferenceSession.#initialized) {
|
|
231
|
+
PatchedInferenceSession.#initialized = true;
|
|
232
|
+
OrtApi.initOrtOnce(getLogLevelValue(ortReactNative.env.logLevel), ortReactNative.Tensor);
|
|
233
|
+
}
|
|
234
|
+
const session = OrtApi.createInferenceSession();
|
|
235
|
+
if (typeof modelPath === 'string') {
|
|
236
|
+
await session.loadModel(modelPath, sessionOptions);
|
|
237
|
+
}
|
|
238
|
+
else if (modelBytes) {
|
|
239
|
+
const modelBuffer = toExactArrayBuffer(modelBytes);
|
|
240
|
+
await session.loadModel(modelBuffer, 0, modelBuffer.byteLength, sessionOptions);
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
throw new TypeError('Model bytes were not provided.');
|
|
244
|
+
}
|
|
245
|
+
return new PatchedInferenceSession(new ReactNativeSessionHandler(session));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const adapterModule = {
|
|
249
|
+
...ortReactNative,
|
|
250
|
+
InferenceSession: PatchedInferenceSession,
|
|
251
|
+
Tensor: ortReactNative.Tensor,
|
|
252
|
+
env: ortReactNative.env,
|
|
253
|
+
listSupportedBackends: OrtApi.listSupportedBackends ?? ortReactNative.listSupportedBackends,
|
|
254
|
+
};
|
|
255
|
+
module.exports = adapterModule;
|
|
256
|
+
//# sourceMappingURL=onnxruntime-web-webgpu.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onnxruntime-web-webgpu.js","sourceRoot":"","sources":["../../../src/adapter/onnxruntime-web-webgpu.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAA6C;AAC7C,yEAA2D;AAE3D,wCAAoD;AAkEpD,MAAM,MAAM,GAAI,4BAA2D,CAAC,WAAW,CAAC;AAExF,IAAI,OAAO,UAAU,CAAC,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,EAAE,OAAO,KAAK,UAAU,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C1C,uBAAuB,CAAC,YAAY,GAAG,IAAI,CAAC;YAC5C,MAAM,CAAC,WAAW,CAAC,gBAAgB,CAAC,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;QAC3F,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,CAAC,sBAAsB,EAAE,CAAC;QAEhD,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;YAClC,MAAM,OAAO,CAAC,SAAS,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;QACrD,CAAC;aAAM,IAAI,UAAU,EAAE,CAAC;YACtB,MAAM,WAAW,GAAG,kBAAkB,CAAC,UAAU,CAAC,CAAC;YACnD,MAAM,OAAO,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC,EAAE,WAAW,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;QAClF,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,SAAS,CAAC,gCAAgC,CAAC,CAAC;QACxD,CAAC;QAED,OAAO,IAAI,uBAAuB,CAAC,IAAI,yBAAyB,CAAC,OAAO,CAAC,CAAC,CAAC;IAC7E,CAAC;;AAGH,MAAM,aAAa,GAAG;IACpB,GAAG,cAAc;IACjB,gBAAgB,EAAE,uBAA4E;IAC9F,MAAM,EAAE,cAAc,CAAC,MAAM;IAC7B,GAAG,EAAE,cAAc,CAAC,GAAG;IACvB,qBAAqB,EAAE,MAAM,CAAC,qBAAqB,IAAI,cAAc,CAAC,qBAAqB;CAC5D,CAAC;AAElC,iBAAS,aAAa,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Directory } from 'expo-file-system';
|
|
2
|
+
import type { CacheLike } from './types';
|
|
3
|
+
export interface ExpoFileSystemCacheOptions {
|
|
4
|
+
directory?: Directory | string;
|
|
5
|
+
}
|
|
6
|
+
export interface ExpoFileSystemCacheMetadata {
|
|
7
|
+
request: string;
|
|
8
|
+
status: number;
|
|
9
|
+
headers: Record<string, string>;
|
|
10
|
+
cachedAt: number;
|
|
11
|
+
}
|
|
12
|
+
export interface ExpoFileSystemCache extends CacheLike {
|
|
13
|
+
readonly directory: Directory;
|
|
14
|
+
}
|
|
15
|
+
export declare function getDefaultExpoFileSystemModelCacheDirectory(): Directory;
|
|
16
|
+
export declare function createExpoFileSystemCache(options?: ExpoFileSystemCacheOptions): ExpoFileSystemCache;
|
|
17
|
+
//# sourceMappingURL=expoFileSystemCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expoFileSystemCache.d.ts","sourceRoot":"","sources":["../../src/expoFileSystemCache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAe,MAAM,kBAAkB,CAAC;AAE1D,OAAO,KAAK,EACV,SAAS,EAGV,MAAM,SAAS,CAAC;AAOjB,MAAM,WAAW,0BAA0B;IACzC,SAAS,CAAC,EAAE,SAAS,GAAG,MAAM,CAAC;CAChC;AAED,MAAM,WAAW,2BAA2B;IAC1C,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,mBAAoB,SAAQ,SAAS;IACpD,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;CAC/B;AAwOD,wBAAgB,2CAA2C,IAAI,SAAS,CAEvE;AAED,wBAAgB,yBAAyB,CACvC,OAAO,GAAE,0BAA+B,GACvC,mBAAmB,CA4DrB"}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDefaultExpoFileSystemModelCacheDirectory = getDefaultExpoFileSystemModelCacheDirectory;
|
|
4
|
+
exports.createExpoFileSystemCache = createExpoFileSystemCache;
|
|
5
|
+
const expo_file_system_1 = require("expo-file-system");
|
|
6
|
+
const DEFAULT_MODEL_CACHE_PATH_SEGMENTS = Object.freeze([
|
|
7
|
+
'automatalabs-react-native-transformers',
|
|
8
|
+
'models',
|
|
9
|
+
]);
|
|
10
|
+
function hashString(value) {
|
|
11
|
+
let hash = 0x811c9dc5;
|
|
12
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
13
|
+
hash ^= value.charCodeAt(index);
|
|
14
|
+
hash = Math.imul(hash, 0x01000193);
|
|
15
|
+
}
|
|
16
|
+
return (hash >>> 0).toString(16).padStart(8, '0');
|
|
17
|
+
}
|
|
18
|
+
function sanitizeSegment(value) {
|
|
19
|
+
const normalized = encodeURIComponent(String(value))
|
|
20
|
+
.replace(/%/g, '_')
|
|
21
|
+
.replace(/[^A-Za-z0-9._-]+/g, '_')
|
|
22
|
+
.replace(/_+/g, '_')
|
|
23
|
+
.replace(/^_+|_+$/g, '');
|
|
24
|
+
return normalized.slice(0, 120) || 'index';
|
|
25
|
+
}
|
|
26
|
+
function serializeRequest(request) {
|
|
27
|
+
return request instanceof Request ? request.url : String(request);
|
|
28
|
+
}
|
|
29
|
+
function getRequestPathSegments(request) {
|
|
30
|
+
const value = serializeRequest(request);
|
|
31
|
+
try {
|
|
32
|
+
const url = new URL(value);
|
|
33
|
+
const segments = [sanitizeSegment(url.protocol.replace(/:$/, '')), sanitizeSegment(url.host)];
|
|
34
|
+
const pathnameSegments = url.pathname
|
|
35
|
+
.split('/')
|
|
36
|
+
.filter(Boolean)
|
|
37
|
+
.map((segment) => sanitizeSegment(segment));
|
|
38
|
+
segments.push(...pathnameSegments);
|
|
39
|
+
if (segments.length === 2) {
|
|
40
|
+
segments.push('index');
|
|
41
|
+
}
|
|
42
|
+
if (url.search) {
|
|
43
|
+
const lastIndex = segments.length - 1;
|
|
44
|
+
const lastSegment = segments[lastIndex];
|
|
45
|
+
if (lastSegment) {
|
|
46
|
+
segments[lastIndex] = `${lastSegment}__q_${hashString(url.search)}`;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return segments;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
const segments = value
|
|
53
|
+
.split('/')
|
|
54
|
+
.filter(Boolean)
|
|
55
|
+
.map((segment) => sanitizeSegment(segment));
|
|
56
|
+
return segments.length > 0 ? segments : ['index'];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function ensureDirectory(directory) {
|
|
60
|
+
if (!directory.exists) {
|
|
61
|
+
directory.create({
|
|
62
|
+
intermediates: true,
|
|
63
|
+
idempotent: true,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function deleteIfExists(file) {
|
|
68
|
+
if (file?.exists) {
|
|
69
|
+
try {
|
|
70
|
+
file.delete();
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// ignore cleanup failures
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function getCachePaths(rootDirectory, request) {
|
|
78
|
+
const requestPathSegments = getRequestPathSegments(request);
|
|
79
|
+
const directorySegments = requestPathSegments.slice(0, -1);
|
|
80
|
+
const leafSegment = requestPathSegments.at(-1) ?? 'index';
|
|
81
|
+
const requestHash = hashString(serializeRequest(request));
|
|
82
|
+
const baseName = `${leafSegment}__${requestHash}`;
|
|
83
|
+
const directory = new expo_file_system_1.Directory(rootDirectory, ...directorySegments);
|
|
84
|
+
return {
|
|
85
|
+
directory,
|
|
86
|
+
dataFile: new expo_file_system_1.File(directory, `${baseName}.data`),
|
|
87
|
+
metadataFile: new expo_file_system_1.File(directory, `${baseName}.meta.json`),
|
|
88
|
+
tempDataFile: new expo_file_system_1.File(directory, `${baseName}.tmp.data`),
|
|
89
|
+
tempMetadataFile: new expo_file_system_1.File(directory, `${baseName}.tmp.meta.json`),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function isStringRecord(value) {
|
|
93
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
return Object.values(value).every((entry) => typeof entry === 'string');
|
|
97
|
+
}
|
|
98
|
+
function isCacheMetadata(value) {
|
|
99
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
const metadata = value;
|
|
103
|
+
return (typeof metadata.request === 'string' &&
|
|
104
|
+
typeof metadata.status === 'number' &&
|
|
105
|
+
typeof metadata.cachedAt === 'number' &&
|
|
106
|
+
isStringRecord(metadata.headers));
|
|
107
|
+
}
|
|
108
|
+
async function readMetadata(metadataFile) {
|
|
109
|
+
if (!metadataFile.exists) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
const parsed = JSON.parse(await metadataFile.text());
|
|
114
|
+
return isCacheMetadata(parsed) ? parsed : null;
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function createResponseHeaders(dataFile, metadata) {
|
|
121
|
+
const headers = new Headers(metadata?.headers ?? {});
|
|
122
|
+
if (!headers.has('content-length')) {
|
|
123
|
+
headers.set('content-length', String(dataFile.size));
|
|
124
|
+
}
|
|
125
|
+
return headers;
|
|
126
|
+
}
|
|
127
|
+
async function writeMetadata(metadataFile, tempMetadataFile, metadata) {
|
|
128
|
+
deleteIfExists(tempMetadataFile);
|
|
129
|
+
tempMetadataFile.create({
|
|
130
|
+
intermediates: true,
|
|
131
|
+
overwrite: true,
|
|
132
|
+
});
|
|
133
|
+
tempMetadataFile.write(JSON.stringify(metadata));
|
|
134
|
+
deleteIfExists(metadataFile);
|
|
135
|
+
tempMetadataFile.move(metadataFile);
|
|
136
|
+
}
|
|
137
|
+
async function writeResponseToFile(file, response, progressCallback) {
|
|
138
|
+
deleteIfExists(file);
|
|
139
|
+
file.create({
|
|
140
|
+
intermediates: true,
|
|
141
|
+
overwrite: true,
|
|
142
|
+
});
|
|
143
|
+
const total = Number.parseInt(response.headers.get('content-length') ?? '0', 10) || 0;
|
|
144
|
+
let loaded = 0;
|
|
145
|
+
if (response.body?.getReader) {
|
|
146
|
+
const reader = response.body.getReader();
|
|
147
|
+
const handle = file.open();
|
|
148
|
+
try {
|
|
149
|
+
while (true) {
|
|
150
|
+
const { done, value } = await reader.read();
|
|
151
|
+
if (done) {
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
if (value && value.length > 0) {
|
|
155
|
+
handle.writeBytes(value);
|
|
156
|
+
loaded += value.length;
|
|
157
|
+
progressCallback?.({
|
|
158
|
+
progress: total > 0 ? (loaded / total) * 100 : 0,
|
|
159
|
+
loaded,
|
|
160
|
+
total,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
handle.close();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
171
|
+
file.write(bytes);
|
|
172
|
+
loaded = bytes.length;
|
|
173
|
+
progressCallback?.({
|
|
174
|
+
progress: 100,
|
|
175
|
+
loaded,
|
|
176
|
+
total: total || loaded,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function normalizeCacheRootDirectory(directory) {
|
|
181
|
+
if (directory instanceof expo_file_system_1.Directory) {
|
|
182
|
+
return directory;
|
|
183
|
+
}
|
|
184
|
+
if (typeof directory === 'string') {
|
|
185
|
+
return new expo_file_system_1.Directory(directory);
|
|
186
|
+
}
|
|
187
|
+
return new expo_file_system_1.Directory(expo_file_system_1.Paths.cache, ...DEFAULT_MODEL_CACHE_PATH_SEGMENTS);
|
|
188
|
+
}
|
|
189
|
+
function getDefaultExpoFileSystemModelCacheDirectory() {
|
|
190
|
+
return normalizeCacheRootDirectory();
|
|
191
|
+
}
|
|
192
|
+
function createExpoFileSystemCache(options = {}) {
|
|
193
|
+
const rootDirectory = normalizeCacheRootDirectory(options.directory);
|
|
194
|
+
ensureDirectory(rootDirectory);
|
|
195
|
+
return {
|
|
196
|
+
async match(request) {
|
|
197
|
+
const { dataFile, metadataFile } = getCachePaths(rootDirectory, request);
|
|
198
|
+
if (!dataFile.exists) {
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
const metadata = await readMetadata(metadataFile);
|
|
202
|
+
return new Response(dataFile, {
|
|
203
|
+
headers: createResponseHeaders(dataFile, metadata),
|
|
204
|
+
status: metadata?.status ?? 200,
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
async put(request, response, progressCallback) {
|
|
208
|
+
const { directory, dataFile, metadataFile, tempDataFile, tempMetadataFile } = getCachePaths(rootDirectory, request);
|
|
209
|
+
ensureDirectory(directory);
|
|
210
|
+
try {
|
|
211
|
+
await writeResponseToFile(tempDataFile, response, progressCallback);
|
|
212
|
+
deleteIfExists(dataFile);
|
|
213
|
+
tempDataFile.move(dataFile);
|
|
214
|
+
await writeMetadata(metadataFile, tempMetadataFile, {
|
|
215
|
+
request: serializeRequest(request),
|
|
216
|
+
status: response.status,
|
|
217
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
218
|
+
cachedAt: Date.now(),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
deleteIfExists(tempDataFile);
|
|
223
|
+
deleteIfExists(tempMetadataFile);
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
async delete(request) {
|
|
228
|
+
const { dataFile, metadataFile } = getCachePaths(rootDirectory, request);
|
|
229
|
+
const hadData = dataFile.exists;
|
|
230
|
+
const hadMetadata = metadataFile.exists;
|
|
231
|
+
deleteIfExists(dataFile);
|
|
232
|
+
deleteIfExists(metadataFile);
|
|
233
|
+
return hadData || hadMetadata;
|
|
234
|
+
},
|
|
235
|
+
get directory() {
|
|
236
|
+
return rootDirectory;
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
//# sourceMappingURL=expoFileSystemCache.js.map
|