Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for ort-1.17 #596

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 26 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
},
"homepage": "https://github.com/xenova/transformers.js#readme",
"dependencies": {
"onnxruntime-web": "1.14.0",
"onnxruntime-web": "1.17.1",
"sharp": "^0.32.0",
"@huggingface/jinja": "^0.2.1"
},
"optionalDependencies": {
"onnxruntime-node": "1.14.0"
"onnxruntime-node": "1.17.1"
},
"devDependencies": {
"@types/jest": "^29.5.1",
Expand Down
11 changes: 9 additions & 2 deletions src/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,16 @@ function validateInputs(session, inputs) {
async function sessionRun(session, inputs) {
const checkedInputs = validateInputs(session, inputs);
try {
// @ts-ignore
let output = await session.run(checkedInputs);
// pass the original ort tensor
const ortFeed = Object.fromEntries(Object.entries(checkedInputs).map(([k, v]) => [k, v.ort_tensor]));
let output = await session.run(ortFeed);
output = replaceTensors(output);
for (const [name, t] of Object.entries(checkedInputs)) {
// if we use gpu buffers for kv_caches, we own them and need to dispose()
if (name.startsWith('past_key_values')) {
t.dispose();
};
}
return output;
} catch (e) {
// This usually occurs when the inputs are of the wrong type.
Expand Down
39 changes: 30 additions & 9 deletions src/utils/tensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {

const DataTypeMap = Object.freeze({
float32: Float32Array,
float16: Uint16Array,
float64: Float64Array,
string: Array, // string[]
int8: Int8Array,
Expand All @@ -39,33 +40,48 @@ const ONNXTensor = ONNX.Tensor;

export class Tensor {
/** @type {number[]} Dimensions of the tensor. */
dims;
get dims() {
// @ts-ignore
return this.ort_tensor.dims;
}
set dims(value) {
// FIXME: ONNXTensor declares dims as readonly so one needs to use the constructor() if dims change.
// @ts-ignore
this.ort_tensor.dims = value;
}

/** @type {DataType} Type of the tensor. */
type;
get type() {
return this.ort_tensor.type;
};

/** @type {DataArray} The data stored in the tensor. */
data;
get data() {
return this.ort_tensor.data;
}

/** @type {number} The number of elements in the tensor. */
size;
get size() {
return this.ort_tensor.size;
};

ort_tensor;

/**
* Create a new Tensor or copy an existing Tensor.
* @param {[DataType, DataArray, number[]]|[import('onnxruntime-common').Tensor]} args
*/
constructor(...args) {
if (args[0] instanceof ONNXTensor) {
// Create shallow copy
Object.assign(this, args[0]);

this.ort_tensor = args[0];
} else {
// Create new tensor
Object.assign(this, new ONNXTensor(
const t = new ONNXTensor(
/** @type {DataType} */(args[0]),
/** @type {Exclude<import('./maths.js').AnyTypedArray, Uint8ClampedArray>} */(args[1]),
args[2]
));
);
this.ort_tensor = t;
}

return new Proxy(this, {
Expand All @@ -89,6 +105,11 @@ export class Tensor {
});
}

dispose() {
this.ort_tensor.dispose();
// this.ort_tensor = undefined;
}

/**
* Returns an iterator object for iterating over the tensor data in row-major order.
* If the tensor has more than one dimension, the iterator will yield subarrays.
Expand Down