/******/ var __webpack_modules__ = ({ /***/ "?2ce3": /*!**********************************!*\ !*** onnxruntime-node (ignored) ***! \**********************************/ /***/ (() => { /* (ignored) */ /***/ }), /***/ "?7a2c": /*!********************!*\ !*** fs (ignored) ***! \********************/ /***/ (() => { /* (ignored) */ /***/ }), /***/ "?a42a": /*!**********************!*\ !*** path (ignored) ***! \**********************/ /***/ (() => { /* (ignored) */ /***/ }), /***/ "?2b25": /*!***********************!*\ !*** sharp (ignored) ***! \***********************/ /***/ (() => { /* (ignored) */ /***/ }), /***/ "?569f": /*!********************!*\ !*** fs (ignored) ***! \********************/ /***/ (() => { /* (ignored) */ /***/ }), /***/ "?3f59": /*!**********************!*\ !*** path (ignored) ***! \**********************/ /***/ (() => { /* (ignored) */ /***/ }), /***/ "?154a": /*!*********************!*\ !*** url (ignored) ***! \*********************/ /***/ (() => { /* (ignored) */ /***/ }), /***/ "./node_modules/@huggingface/jinja/dist/index.js": /*!*******************************************************!*\ !*** ./node_modules/@huggingface/jinja/dist/index.js ***! \*******************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "Environment": () => (/* binding */ Environment), /* harmony export */ "Interpreter": () => (/* binding */ Interpreter), /* harmony export */ "Template": () => (/* binding */ Template), /* harmony export */ "parse": () => (/* binding */ parse), /* harmony export */ "tokenize": () => (/* binding */ tokenize) /* harmony export */ }); // src/lexer.ts var TOKEN_TYPES = Object.freeze({ Text: "Text", // The text between Jinja statements or expressions NumericLiteral: "NumericLiteral", // e.g., 123 BooleanLiteral: "BooleanLiteral", // true or false StringLiteral: "StringLiteral", // 'string' Identifier: "Identifier", // Variables, functions, etc. Equals: "Equals", // = OpenParen: "OpenParen", // ( CloseParen: "CloseParen", // ) OpenStatement: "OpenStatement", // {% CloseStatement: "CloseStatement", // %} OpenExpression: "OpenExpression", // {{ CloseExpression: "CloseExpression", // }} OpenSquareBracket: "OpenSquareBracket", // [ CloseSquareBracket: "CloseSquareBracket", // ] OpenCurlyBracket: "OpenCurlyBracket", // { CloseCurlyBracket: "CloseCurlyBracket", // } Comma: "Comma", // , Dot: "Dot", // . Colon: "Colon", // : Pipe: "Pipe", // | CallOperator: "CallOperator", // () AdditiveBinaryOperator: "AdditiveBinaryOperator", // + - MultiplicativeBinaryOperator: "MultiplicativeBinaryOperator", // * / % ComparisonBinaryOperator: "ComparisonBinaryOperator", // < > <= >= == != UnaryOperator: "UnaryOperator", // ! - + // Keywords Set: "Set", If: "If", For: "For", In: "In", Is: "Is", NotIn: "NotIn", Else: "Else", EndIf: "EndIf", ElseIf: "ElseIf", EndFor: "EndFor", And: "And", Or: "Or", Not: "UnaryOperator" }); var KEYWORDS = Object.freeze({ set: TOKEN_TYPES.Set, for: TOKEN_TYPES.For, in: TOKEN_TYPES.In, is: TOKEN_TYPES.Is, if: TOKEN_TYPES.If, else: TOKEN_TYPES.Else, endif: TOKEN_TYPES.EndIf, elif: TOKEN_TYPES.ElseIf, endfor: TOKEN_TYPES.EndFor, and: TOKEN_TYPES.And, or: TOKEN_TYPES.Or, not: TOKEN_TYPES.Not, "not in": TOKEN_TYPES.NotIn, // Literals true: TOKEN_TYPES.BooleanLiteral, false: TOKEN_TYPES.BooleanLiteral }); var Token = class { /** * Constructs a new Token. * @param {string} value The raw value as seen inside the source code. * @param {TokenType} type The type of token. */ constructor(value, type) { this.value = value; this.type = type; } }; function isWord(char) { return /\w/.test(char); } function isInteger(char) { return /[0-9]/.test(char); } var ORDERED_MAPPING_TABLE = [ // Control sequences ["{%", TOKEN_TYPES.OpenStatement], ["%}", TOKEN_TYPES.CloseStatement], ["{{", TOKEN_TYPES.OpenExpression], ["}}", TOKEN_TYPES.CloseExpression], // Single character tokens ["(", TOKEN_TYPES.OpenParen], [")", TOKEN_TYPES.CloseParen], ["{", TOKEN_TYPES.OpenCurlyBracket], ["}", TOKEN_TYPES.CloseCurlyBracket], ["[", TOKEN_TYPES.OpenSquareBracket], ["]", TOKEN_TYPES.CloseSquareBracket], [",", TOKEN_TYPES.Comma], [".", TOKEN_TYPES.Dot], [":", TOKEN_TYPES.Colon], ["|", TOKEN_TYPES.Pipe], // Comparison operators ["<=", TOKEN_TYPES.ComparisonBinaryOperator], [">=", TOKEN_TYPES.ComparisonBinaryOperator], ["==", TOKEN_TYPES.ComparisonBinaryOperator], ["!=", TOKEN_TYPES.ComparisonBinaryOperator], ["<", TOKEN_TYPES.ComparisonBinaryOperator], [">", TOKEN_TYPES.ComparisonBinaryOperator], // Arithmetic operators ["+", TOKEN_TYPES.AdditiveBinaryOperator], ["-", TOKEN_TYPES.AdditiveBinaryOperator], ["*", TOKEN_TYPES.MultiplicativeBinaryOperator], ["/", TOKEN_TYPES.MultiplicativeBinaryOperator], ["%", TOKEN_TYPES.MultiplicativeBinaryOperator], // Assignment operator ["=", TOKEN_TYPES.Equals] ]; var ESCAPE_CHARACTERS = /* @__PURE__ */ new Map([ ["n", "\n"], // New line ["t", " "], // Horizontal tab ["r", "\r"], // Carriage return ["b", "\b"], // Backspace ["f", "\f"], // Form feed ["v", "\v"], // Vertical tab ["'", "'"], // Single quote ['"', '"'], // Double quote ["\\", "\\"] // Backslash ]); function preprocess(template, options = {}) { if (template.endsWith("\n")) { template = template.slice(0, -1); } template = template.replace(/{#.*?#}/gs, "{##}"); if (options.lstrip_blocks) { template = template.replace(/^[ \t]*({[#%])/gm, "$1"); } if (options.trim_blocks) { template = template.replace(/([#%]})\n/g, "$1"); } return template.replace(/{##}/g, "").replace(/-%}\s*/g, "%}").replace(/\s*{%-/g, "{%").replace(/-}}\s*/g, "}}").replace(/\s*{{-/g, "{{"); } function tokenize(source, options = {}) { const tokens = []; const src = preprocess(source, options); let cursorPosition = 0; const consumeWhile = (predicate) => { let str = ""; while (predicate(src[cursorPosition])) { if (src[cursorPosition] === "\\") { ++cursorPosition; if (cursorPosition >= src.length) throw new SyntaxError("Unexpected end of input"); const escaped = src[cursorPosition++]; const unescaped = ESCAPE_CHARACTERS.get(escaped); if (unescaped === void 0) { throw new SyntaxError(`Unexpected escaped character: ${escaped}`); } str += unescaped; continue; } str += src[cursorPosition++]; if (cursorPosition >= src.length) throw new SyntaxError("Unexpected end of input"); } return str; }; main: while (cursorPosition < src.length) { const lastTokenType = tokens.at(-1)?.type; if (lastTokenType === void 0 || lastTokenType === TOKEN_TYPES.CloseStatement || lastTokenType === TOKEN_TYPES.CloseExpression) { let text = ""; while (cursorPosition < src.length && // Keep going until we hit the next Jinja statement or expression !(src[cursorPosition] === "{" && (src[cursorPosition + 1] === "%" || src[cursorPosition + 1] === "{"))) { text += src[cursorPosition++]; } if (text.length > 0) { tokens.push(new Token(text, TOKEN_TYPES.Text)); continue; } } consumeWhile((char2) => /\s/.test(char2)); const char = src[cursorPosition]; if (char === "-" || char === "+") { const lastTokenType2 = tokens.at(-1)?.type; if (lastTokenType2 === TOKEN_TYPES.Text || lastTokenType2 === void 0) { throw new SyntaxError(`Unexpected character: ${char}`); } switch (lastTokenType2) { case TOKEN_TYPES.Identifier: case TOKEN_TYPES.NumericLiteral: case TOKEN_TYPES.BooleanLiteral: case TOKEN_TYPES.StringLiteral: case TOKEN_TYPES.CloseParen: case TOKEN_TYPES.CloseSquareBracket: break; default: { ++cursorPosition; const num = consumeWhile(isInteger); tokens.push( new Token(`${char}${num}`, num.length > 0 ? TOKEN_TYPES.NumericLiteral : TOKEN_TYPES.UnaryOperator) ); continue; } } } for (const [char2, token] of ORDERED_MAPPING_TABLE) { const slice2 = src.slice(cursorPosition, cursorPosition + char2.length); if (slice2 === char2) { tokens.push(new Token(char2, token)); cursorPosition += char2.length; continue main; } } if (char === "'" || char === '"') { ++cursorPosition; const str = consumeWhile((c) => c !== char); tokens.push(new Token(str, TOKEN_TYPES.StringLiteral)); ++cursorPosition; continue; } if (isInteger(char)) { const num = consumeWhile(isInteger); tokens.push(new Token(num, TOKEN_TYPES.NumericLiteral)); continue; } if (isWord(char)) { const word = consumeWhile(isWord); const type = Object.hasOwn(KEYWORDS, word) ? KEYWORDS[word] : TOKEN_TYPES.Identifier; if (type === TOKEN_TYPES.In && tokens.at(-1)?.type === TOKEN_TYPES.Not) { tokens.pop(); tokens.push(new Token("not in", TOKEN_TYPES.NotIn)); } else { tokens.push(new Token(word, type)); } continue; } throw new SyntaxError(`Unexpected character: ${char}`); } return tokens; } // src/ast.ts var Statement = class { type = "Statement"; }; var Program = class extends Statement { constructor(body) { super(); this.body = body; } type = "Program"; }; var If = class extends Statement { constructor(test, body, alternate) { super(); this.test = test; this.body = body; this.alternate = alternate; } type = "If"; }; var For = class extends Statement { constructor(loopvar, iterable, body) { super(); this.loopvar = loopvar; this.iterable = iterable; this.body = body; } type = "For"; }; var SetStatement = class extends Statement { constructor(assignee, value) { super(); this.assignee = assignee; this.value = value; } type = "Set"; }; var Expression = class extends Statement { type = "Expression"; }; var MemberExpression = class extends Expression { constructor(object, property, computed) { super(); this.object = object; this.property = property; this.computed = computed; } type = "MemberExpression"; }; var CallExpression = class extends Expression { constructor(callee, args) { super(); this.callee = callee; this.args = args; } type = "CallExpression"; }; var Identifier = class extends Expression { /** * @param {string} value The name of the identifier */ constructor(value) { super(); this.value = value; } type = "Identifier"; }; var Literal = class extends Expression { constructor(value) { super(); this.value = value; } type = "Literal"; }; var NumericLiteral = class extends Literal { type = "NumericLiteral"; }; var StringLiteral = class extends Literal { type = "StringLiteral"; }; var BooleanLiteral = class extends Literal { type = "BooleanLiteral"; }; var ArrayLiteral = class extends Literal { type = "ArrayLiteral"; }; var TupleLiteral = class extends Literal { type = "TupleLiteral"; }; var ObjectLiteral = class extends Literal { type = "ObjectLiteral"; }; var BinaryExpression = class extends Expression { constructor(operator, left, right) { super(); this.operator = operator; this.left = left; this.right = right; } type = "BinaryExpression"; }; var FilterExpression = class extends Expression { constructor(operand, filter) { super(); this.operand = operand; this.filter = filter; } type = "FilterExpression"; }; var TestExpression = class extends Expression { constructor(operand, negate, test) { super(); this.operand = operand; this.negate = negate; this.test = test; } type = "TestExpression"; }; var UnaryExpression = class extends Expression { constructor(operator, argument) { super(); this.operator = operator; this.argument = argument; } type = "UnaryExpression"; }; var SliceExpression = class extends Expression { constructor(start = void 0, stop = void 0, step = void 0) { super(); this.start = start; this.stop = stop; this.step = step; } type = "SliceExpression"; }; var KeywordArgumentExpression = class extends Expression { constructor(key, value) { super(); this.key = key; this.value = value; } type = "KeywordArgumentExpression"; }; // src/parser.ts function parse(tokens) { const program = new Program([]); let current = 0; function expect(type, error) { const prev = tokens[current++]; if (!prev || prev.type !== type) { throw new Error(`Parser Error: ${error}. ${prev.type} !== ${type}.`); } return prev; } function parseAny() { switch (tokens[current].type) { case TOKEN_TYPES.Text: return parseText(); case TOKEN_TYPES.OpenStatement: return parseJinjaStatement(); case TOKEN_TYPES.OpenExpression: return parseJinjaExpression(); default: throw new SyntaxError(`Unexpected token type: ${tokens[current].type}`); } } function not(...types) { return current + types.length <= tokens.length && types.some((type, i) => type !== tokens[current + i].type); } function is(...types) { return current + types.length <= tokens.length && types.every((type, i) => type === tokens[current + i].type); } function parseText() { return new StringLiteral(expect(TOKEN_TYPES.Text, "Expected text token").value); } function parseJinjaStatement() { expect(TOKEN_TYPES.OpenStatement, "Expected opening statement token"); let result; switch (tokens[current].type) { case TOKEN_TYPES.Set: ++current; result = parseSetStatement(); expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); break; case TOKEN_TYPES.If: ++current; result = parseIfStatement(); expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); expect(TOKEN_TYPES.EndIf, "Expected endif token"); expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); break; case TOKEN_TYPES.For: ++current; result = parseForStatement(); expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); expect(TOKEN_TYPES.EndFor, "Expected endfor token"); expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); break; default: throw new SyntaxError(`Unknown statement type: ${tokens[current].type}`); } return result; } function parseJinjaExpression() { expect(TOKEN_TYPES.OpenExpression, "Expected opening expression token"); const result = parseExpression(); expect(TOKEN_TYPES.CloseExpression, "Expected closing expression token"); return result; } function parseSetStatement() { const left = parseExpression(); if (is(TOKEN_TYPES.Equals)) { ++current; const value = parseSetStatement(); return new SetStatement(left, value); } return left; } function parseIfStatement() { const test = parseExpression(); expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); const body = []; const alternate = []; while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && (tokens[current + 1]?.type === TOKEN_TYPES.ElseIf || tokens[current + 1]?.type === TOKEN_TYPES.Else || tokens[current + 1]?.type === TOKEN_TYPES.EndIf))) { body.push(parseAny()); } if (tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type !== TOKEN_TYPES.EndIf) { ++current; if (is(TOKEN_TYPES.ElseIf)) { expect(TOKEN_TYPES.ElseIf, "Expected elseif token"); alternate.push(parseIfStatement()); } else { expect(TOKEN_TYPES.Else, "Expected else token"); expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type === TOKEN_TYPES.EndIf)) { alternate.push(parseAny()); } } } return new If(test, body, alternate); } function parseExpressionSequence(primary = false) { const fn = primary ? parsePrimaryExpression : parseExpression; const expressions = [fn()]; const isTuple = is(TOKEN_TYPES.Comma); while (isTuple) { ++current; expressions.push(fn()); if (!is(TOKEN_TYPES.Comma)) { break; } } return isTuple ? new TupleLiteral(expressions) : expressions[0]; } function parseForStatement() { const loopVariable = parseExpressionSequence(true); if (!(loopVariable instanceof Identifier || loopVariable instanceof TupleLiteral)) { throw new SyntaxError(`Expected identifier/tuple for the loop variable, got ${loopVariable.type} instead`); } expect(TOKEN_TYPES.In, "Expected `in` keyword following loop variable"); const iterable = parseExpression(); expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); const body = []; while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor)) { body.push(parseAny()); } return new For(loopVariable, iterable, body); } function parseExpression() { return parseTernaryExpression(); } function parseTernaryExpression() { const a = parseLogicalOrExpression(); if (is(TOKEN_TYPES.If)) { ++current; const predicate = parseLogicalOrExpression(); expect(TOKEN_TYPES.Else, "Expected else token"); const b = parseLogicalOrExpression(); return new If(predicate, [a], [b]); } return a; } function parseLogicalOrExpression() { let left = parseLogicalAndExpression(); while (is(TOKEN_TYPES.Or)) { const operator = tokens[current]; ++current; const right = parseLogicalAndExpression(); left = new BinaryExpression(operator, left, right); } return left; } function parseLogicalAndExpression() { let left = parseLogicalNegationExpression(); while (is(TOKEN_TYPES.And)) { const operator = tokens[current]; ++current; const right = parseLogicalNegationExpression(); left = new BinaryExpression(operator, left, right); } return left; } function parseLogicalNegationExpression() { let right; while (is(TOKEN_TYPES.Not)) { const operator = tokens[current]; ++current; const arg = parseLogicalNegationExpression(); right = new UnaryExpression(operator, arg); } return right ?? parseComparisonExpression(); } function parseComparisonExpression() { let left = parseAdditiveExpression(); while (is(TOKEN_TYPES.ComparisonBinaryOperator) || is(TOKEN_TYPES.In) || is(TOKEN_TYPES.NotIn)) { const operator = tokens[current]; ++current; const right = parseAdditiveExpression(); left = new BinaryExpression(operator, left, right); } return left; } function parseAdditiveExpression() { let left = parseMultiplicativeExpression(); while (is(TOKEN_TYPES.AdditiveBinaryOperator)) { const operator = tokens[current]; ++current; const right = parseMultiplicativeExpression(); left = new BinaryExpression(operator, left, right); } return left; } function parseCallMemberExpression() { const member = parseMemberExpression(); if (is(TOKEN_TYPES.OpenParen)) { return parseCallExpression(member); } return member; } function parseCallExpression(callee) { let callExpression = new CallExpression(callee, parseArgs()); if (is(TOKEN_TYPES.OpenParen)) { callExpression = parseCallExpression(callExpression); } return callExpression; } function parseArgs() { expect(TOKEN_TYPES.OpenParen, "Expected opening parenthesis for arguments list"); const args = parseArgumentsList(); expect(TOKEN_TYPES.CloseParen, "Expected closing parenthesis for arguments list"); return args; } function parseArgumentsList() { const args = []; while (!is(TOKEN_TYPES.CloseParen)) { let argument = parseExpression(); if (is(TOKEN_TYPES.Equals)) { ++current; if (!(argument instanceof Identifier)) { throw new SyntaxError(`Expected identifier for keyword argument`); } const value = parseExpression(); argument = new KeywordArgumentExpression(argument, value); } args.push(argument); if (is(TOKEN_TYPES.Comma)) { ++current; } } return args; } function parseMemberExpressionArgumentsList() { const slices = []; let isSlice = false; while (!is(TOKEN_TYPES.CloseSquareBracket)) { if (is(TOKEN_TYPES.Colon)) { slices.push(void 0); ++current; isSlice = true; } else { slices.push(parseExpression()); if (is(TOKEN_TYPES.Colon)) { ++current; isSlice = true; } } } if (slices.length === 0) { throw new SyntaxError(`Expected at least one argument for member/slice expression`); } if (isSlice) { if (slices.length > 3) { throw new SyntaxError(`Expected 0-3 arguments for slice expression`); } return new SliceExpression(...slices); } return slices[0]; } function parseMemberExpression() { let object = parsePrimaryExpression(); while (is(TOKEN_TYPES.Dot) || is(TOKEN_TYPES.OpenSquareBracket)) { const operator = tokens[current]; ++current; let property; const computed = operator.type !== TOKEN_TYPES.Dot; if (computed) { property = parseMemberExpressionArgumentsList(); expect(TOKEN_TYPES.CloseSquareBracket, "Expected closing square bracket"); } else { property = parsePrimaryExpression(); if (property.type !== "Identifier") { throw new SyntaxError(`Expected identifier following dot operator`); } } object = new MemberExpression(object, property, computed); } return object; } function parseMultiplicativeExpression() { let left = parseTestExpression(); while (is(TOKEN_TYPES.MultiplicativeBinaryOperator)) { const operator = tokens[current]; ++current; const right = parseTestExpression(); left = new BinaryExpression(operator, left, right); } return left; } function parseTestExpression() { let operand = parseFilterExpression(); while (is(TOKEN_TYPES.Is)) { ++current; const negate = is(TOKEN_TYPES.Not); if (negate) { ++current; } let filter = parsePrimaryExpression(); if (filter instanceof BooleanLiteral) { filter = new Identifier(filter.value.toString()); } if (!(filter instanceof Identifier)) { throw new SyntaxError(`Expected identifier for the test`); } operand = new TestExpression(operand, negate, filter); } return operand; } function parseFilterExpression() { let operand = parseCallMemberExpression(); while (is(TOKEN_TYPES.Pipe)) { ++current; let filter = parsePrimaryExpression(); if (!(filter instanceof Identifier)) { throw new SyntaxError(`Expected identifier for the filter`); } if (is(TOKEN_TYPES.OpenParen)) { filter = parseCallExpression(filter); } operand = new FilterExpression(operand, filter); } return operand; } function parsePrimaryExpression() { const token = tokens[current]; switch (token.type) { case TOKEN_TYPES.NumericLiteral: ++current; return new NumericLiteral(Number(token.value)); case TOKEN_TYPES.StringLiteral: ++current; return new StringLiteral(token.value); case TOKEN_TYPES.BooleanLiteral: ++current; return new BooleanLiteral(token.value === "true"); case TOKEN_TYPES.Identifier: ++current; return new Identifier(token.value); case TOKEN_TYPES.OpenParen: { ++current; const expression = parseExpressionSequence(); if (tokens[current].type !== TOKEN_TYPES.CloseParen) { throw new SyntaxError(`Expected closing parenthesis, got ${tokens[current].type} instead`); } ++current; return expression; } case TOKEN_TYPES.OpenSquareBracket: { ++current; const values = []; while (!is(TOKEN_TYPES.CloseSquareBracket)) { values.push(parseExpression()); if (is(TOKEN_TYPES.Comma)) { ++current; } } ++current; return new ArrayLiteral(values); } case TOKEN_TYPES.OpenCurlyBracket: { ++current; const values = /* @__PURE__ */ new Map(); while (!is(TOKEN_TYPES.CloseCurlyBracket)) { const key = parseExpression(); expect(TOKEN_TYPES.Colon, "Expected colon between key and value in object literal"); const value = parseExpression(); values.set(key, value); if (is(TOKEN_TYPES.Comma)) { ++current; } } ++current; return new ObjectLiteral(values); } default: throw new SyntaxError(`Unexpected token: ${token.type}`); } } while (current < tokens.length) { program.body.push(parseAny()); } return program; } // src/utils.ts function range(start, stop, step = 1) { if (stop === void 0) { stop = start; start = 0; } const result = []; for (let i = start; i < stop; i += step) { result.push(i); } return result; } function slice(array, start, stop, step = 1) { const direction = Math.sign(step); if (direction >= 0) { start = (start ??= 0) < 0 ? Math.max(array.length + start, 0) : Math.min(start, array.length); stop = (stop ??= array.length) < 0 ? Math.max(array.length + stop, 0) : Math.min(stop, array.length); } else { start = (start ??= array.length - 1) < 0 ? Math.max(array.length + start, -1) : Math.min(start, array.length - 1); stop = (stop ??= -1) < -1 ? Math.max(array.length + stop, -1) : Math.min(stop, array.length - 1); } const result = []; for (let i = start; direction * i < direction * stop; i += step) { result.push(array[i]); } return result; } function titleCase(value) { return value.replace(/\b\w/g, (c) => c.toUpperCase()); } // src/runtime.ts var RuntimeValue = class { type = "RuntimeValue"; value; /** * A collection of built-in functions for this type. */ builtins = /* @__PURE__ */ new Map(); /** * Creates a new RuntimeValue. */ constructor(value = void 0) { this.value = value; } /** * Determines truthiness or falsiness of the runtime value. * This function should be overridden by subclasses if it has custom truthiness criteria. * @returns {BooleanValue} BooleanValue(true) if the value is truthy, BooleanValue(false) otherwise. */ __bool__() { return new BooleanValue(!!this.value); } }; var NumericValue = class extends RuntimeValue { type = "NumericValue"; }; var StringValue = class extends RuntimeValue { type = "StringValue"; builtins = /* @__PURE__ */ new Map([ [ "upper", new FunctionValue(() => { return new StringValue(this.value.toUpperCase()); }) ], [ "lower", new FunctionValue(() => { return new StringValue(this.value.toLowerCase()); }) ], [ "strip", new FunctionValue(() => { return new StringValue(this.value.trim()); }) ], [ "title", new FunctionValue(() => { return new StringValue(titleCase(this.value)); }) ], ["length", new NumericValue(this.value.length)] ]); }; var BooleanValue = class extends RuntimeValue { type = "BooleanValue"; }; var ObjectValue = class extends RuntimeValue { type = "ObjectValue"; /** * NOTE: necessary to override since all JavaScript arrays are considered truthy, * while only non-empty Python arrays are consider truthy. * * e.g., * - JavaScript: {} && 5 -> 5 * - Python: {} and 5 -> {} */ __bool__() { return new BooleanValue(this.value.size > 0); } builtins = /* @__PURE__ */ new Map([ [ "get", new FunctionValue(([key, defaultValue]) => { if (!(key instanceof StringValue)) { throw new Error(`Object key must be a string: got ${key.type}`); } return this.value.get(key.value) ?? defaultValue ?? new NullValue(); }) ], [ "items", new FunctionValue(() => { return new ArrayValue( Array.from(this.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) ); }) ] ]); }; var ArrayValue = class extends RuntimeValue { type = "ArrayValue"; builtins = /* @__PURE__ */ new Map([["length", new NumericValue(this.value.length)]]); /** * NOTE: necessary to override since all JavaScript arrays are considered truthy, * while only non-empty Python arrays are consider truthy. * * e.g., * - JavaScript: [] && 5 -> 5 * - Python: [] and 5 -> [] */ __bool__() { return new BooleanValue(this.value.length > 0); } }; var TupleValue = class extends ArrayValue { type = "TupleValue"; }; var FunctionValue = class extends RuntimeValue { type = "FunctionValue"; }; var NullValue = class extends RuntimeValue { type = "NullValue"; }; var UndefinedValue = class extends RuntimeValue { type = "UndefinedValue"; }; var Environment = class { constructor(parent) { this.parent = parent; } /** * The variables declared in this environment. */ variables = /* @__PURE__ */ new Map([ [ "namespace", new FunctionValue((args) => { if (args.length === 0) { return new ObjectValue(/* @__PURE__ */ new Map()); } if (args.length !== 1 || !(args[0] instanceof ObjectValue)) { throw new Error("`namespace` expects either zero arguments or a single object argument"); } return args[0]; }) ] ]); /** * The tests available in this environment. */ tests = /* @__PURE__ */ new Map([ ["boolean", (operand) => operand.type === "BooleanValue"], ["callable", (operand) => operand instanceof FunctionValue], [ "odd", (operand) => { if (operand.type !== "NumericValue") { throw new Error(`Cannot apply test "odd" to type: ${operand.type}`); } return operand.value % 2 !== 0; } ], [ "even", (operand) => { if (operand.type !== "NumericValue") { throw new Error(`Cannot apply test "even" to type: ${operand.type}`); } return operand.value % 2 === 0; } ], ["false", (operand) => operand.type === "BooleanValue" && !operand.value], ["true", (operand) => operand.type === "BooleanValue" && operand.value], ["number", (operand) => operand.type === "NumericValue"], ["integer", (operand) => operand.type === "NumericValue" && Number.isInteger(operand.value)], ["iterable", (operand) => operand instanceof ArrayValue || operand instanceof StringValue], [ "lower", (operand) => { const str = operand.value; return operand.type === "StringValue" && str === str.toLowerCase(); } ], [ "upper", (operand) => { const str = operand.value; return operand.type === "StringValue" && str === str.toUpperCase(); } ], ["none", (operand) => operand.type === "NullValue"], ["defined", (operand) => operand.type !== "UndefinedValue"], ["undefined", (operand) => operand.type === "UndefinedValue"], ["equalto", (a, b) => a.value === b.value] ]); /** * Set the value of a variable in the current environment. */ set(name, value) { return this.declareVariable(name, convertToRuntimeValues(value)); } declareVariable(name, value) { if (this.variables.has(name)) { throw new SyntaxError(`Variable already declared: ${name}`); } this.variables.set(name, value); return value; } // private assignVariable(name: string, value: AnyRuntimeValue): AnyRuntimeValue { // const env = this.resolve(name); // env.variables.set(name, value); // return value; // } /** * Set variable in the current scope. * See https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for more information. */ setVariable(name, value) { this.variables.set(name, value); return value; } /** * Resolve the environment in which the variable is declared. * @param {string} name The name of the variable. * @returns {Environment} The environment in which the variable is declared. */ resolve(name) { if (this.variables.has(name)) { return this; } if (this.parent) { return this.parent.resolve(name); } throw new Error(`Unknown variable: ${name}`); } lookupVariable(name) { try { return this.resolve(name).variables.get(name) ?? new UndefinedValue(); } catch { return new UndefinedValue(); } } }; var Interpreter = class { global; constructor(env) { this.global = env ?? new Environment(); } /** * Run the program. */ run(program) { return this.evaluate(program, this.global); } /** * Evaluates expressions following the binary operation type. */ evaluateBinaryExpression(node, environment) { const left = this.evaluate(node.left, environment); switch (node.operator.value) { case "and": return left.__bool__().value ? this.evaluate(node.right, environment) : left; case "or": return left.__bool__().value ? left : this.evaluate(node.right, environment); } const right = this.evaluate(node.right, environment); switch (node.operator.value) { case "==": return new BooleanValue(left.value == right.value); case "!=": return new BooleanValue(left.value != right.value); } if (left instanceof UndefinedValue || right instanceof UndefinedValue) { throw new Error("Cannot perform operation on undefined values"); } else if (left instanceof NullValue || right instanceof NullValue) { throw new Error("Cannot perform operation on null values"); } else if (left instanceof NumericValue && right instanceof NumericValue) { switch (node.operator.value) { case "+": return new NumericValue(left.value + right.value); case "-": return new NumericValue(left.value - right.value); case "*": return new NumericValue(left.value * right.value); case "/": return new NumericValue(left.value / right.value); case "%": return new NumericValue(left.value % right.value); case "<": return new BooleanValue(left.value < right.value); case ">": return new BooleanValue(left.value > right.value); case ">=": return new BooleanValue(left.value >= right.value); case "<=": return new BooleanValue(left.value <= right.value); } } else if (left instanceof ArrayValue && right instanceof ArrayValue) { switch (node.operator.value) { case "+": return new ArrayValue(left.value.concat(right.value)); } } else if (right instanceof ArrayValue) { const member = right.value.find((x) => x.value === left.value) !== void 0; switch (node.operator.value) { case "in": return new BooleanValue(member); case "not in": return new BooleanValue(!member); } } if (left instanceof StringValue || right instanceof StringValue) { switch (node.operator.value) { case "+": return new StringValue(left.value.toString() + right.value.toString()); } } if (left instanceof StringValue && right instanceof StringValue) { switch (node.operator.value) { case "in": return new BooleanValue(right.value.includes(left.value)); case "not in": return new BooleanValue(!right.value.includes(left.value)); } } if (left instanceof StringValue && right instanceof ObjectValue) { switch (node.operator.value) { case "in": return new BooleanValue(right.value.has(left.value)); case "not in": return new BooleanValue(!right.value.has(left.value)); } } throw new SyntaxError(`Unknown operator "${node.operator.value}" between ${left.type} and ${right.type}`); } /** * Evaluates expressions following the filter operation type. */ evaluateFilterExpression(node, environment) { const operand = this.evaluate(node.operand, environment); if (node.filter.type === "Identifier") { const filter = node.filter; if (operand instanceof ArrayValue) { switch (filter.value) { case "list": return operand; case "first": return operand.value[0]; case "last": return operand.value[operand.value.length - 1]; case "length": return new NumericValue(operand.value.length); case "reverse": return new ArrayValue(operand.value.reverse()); case "sort": return new ArrayValue( operand.value.sort((a, b) => { if (a.type !== b.type) { throw new Error(`Cannot compare different types: ${a.type} and ${b.type}`); } switch (a.type) { case "NumericValue": return a.value - b.value; case "StringValue": return a.value.localeCompare(b.value); default: throw new Error(`Cannot compare type: ${a.type}`); } }) ); default: throw new Error(`Unknown ArrayValue filter: ${filter.value}`); } } else if (operand instanceof StringValue) { switch (filter.value) { case "length": return new NumericValue(operand.value.length); case "upper": return new StringValue(operand.value.toUpperCase()); case "lower": return new StringValue(operand.value.toLowerCase()); case "title": return new StringValue(titleCase(operand.value)); case "capitalize": return new StringValue(operand.value.charAt(0).toUpperCase() + operand.value.slice(1)); case "trim": return new StringValue(operand.value.trim()); default: throw new Error(`Unknown StringValue filter: ${filter.value}`); } } else if (operand instanceof NumericValue) { switch (filter.value) { case "abs": return new NumericValue(Math.abs(operand.value)); default: throw new Error(`Unknown NumericValue filter: ${filter.value}`); } } else if (operand instanceof ObjectValue) { switch (filter.value) { case "items": return new ArrayValue( Array.from(operand.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) ); case "length": return new NumericValue(operand.value.size); default: throw new Error(`Unknown ObjectValue filter: ${filter.value}`); } } throw new Error(`Cannot apply filter "${filter.value}" to type: ${operand.type}`); } else if (node.filter.type === "CallExpression") { const filter = node.filter; if (filter.callee.type !== "Identifier") { throw new Error(`Unknown filter: ${filter.callee.type}`); } const filterName = filter.callee.value; if (operand instanceof ArrayValue) { switch (filterName) { case "selectattr": { if (operand.value.some((x) => !(x instanceof ObjectValue))) { throw new Error("`selectattr` can only be applied to array of objects"); } if (filter.args.some((x) => x.type !== "StringLiteral")) { throw new Error("arguments of `selectattr` must be strings"); } const [attr, testName, value] = filter.args.map((x) => this.evaluate(x, environment)); let testFunction; if (testName) { const test = environment.tests.get(testName.value); if (!test) { throw new Error(`Unknown test: ${testName.value}`); } testFunction = test; } else { testFunction = (...x) => x[0].__bool__().value; } const filtered = operand.value.filter((item) => { const a = item.value.get(attr.value); if (a) { return testFunction(a, value); } return false; }); return new ArrayValue(filtered); } } throw new Error(`Unknown ArrayValue filter: ${filterName}`); } else { throw new Error(`Cannot apply filter "${filterName}" to type: ${operand.type}`); } } throw new Error(`Unknown filter: ${node.filter.type}`); } /** * Evaluates expressions following the test operation type. */ evaluateTestExpression(node, environment) { const operand = this.evaluate(node.operand, environment); const test = environment.tests.get(node.test.value); if (!test) { throw new Error(`Unknown test: ${node.test.value}`); } const result = test(operand); return new BooleanValue(node.negate ? !result : result); } /** * Evaluates expressions following the unary operation type. */ evaluateUnaryExpression(node, environment) { const argument = this.evaluate(node.argument, environment); switch (node.operator.value) { case "not": return new BooleanValue(!argument.value); default: throw new SyntaxError(`Unknown operator: ${node.operator.value}`); } } evalProgram(program, environment) { return this.evaluateBlock(program.body, environment); } evaluateBlock(statements, environment) { let result = ""; for (const statement of statements) { const lastEvaluated = this.evaluate(statement, environment); if (lastEvaluated.type !== "NullValue" && lastEvaluated.type !== "UndefinedValue") { result += lastEvaluated.value; } } return new StringValue(result); } evaluateIdentifier(node, environment) { return environment.lookupVariable(node.value); } evaluateCallExpression(expr, environment) { const args = []; const kwargs = /* @__PURE__ */ new Map(); for (const argument of expr.args) { if (argument.type === "KeywordArgumentExpression") { const kwarg = argument; kwargs.set(kwarg.key.value, this.evaluate(kwarg.value, environment)); } else { args.push(this.evaluate(argument, environment)); } } if (kwargs.size > 0) { args.push(new ObjectValue(kwargs)); } const fn = this.evaluate(expr.callee, environment); if (fn.type !== "FunctionValue") { throw new Error(`Cannot call something that is not a function: got ${fn.type}`); } return fn.value(args, environment); } evaluateSliceExpression(object, expr, environment) { if (!(object instanceof ArrayValue || object instanceof StringValue)) { throw new Error("Slice object must be an array or string"); } const start = this.evaluate(expr.start, environment); const stop = this.evaluate(expr.stop, environment); const step = this.evaluate(expr.step, environment); if (!(start instanceof NumericValue || start instanceof UndefinedValue)) { throw new Error("Slice start must be numeric or undefined"); } if (!(stop instanceof NumericValue || stop instanceof UndefinedValue)) { throw new Error("Slice stop must be numeric or undefined"); } if (!(step instanceof NumericValue || step instanceof UndefinedValue)) { throw new Error("Slice step must be numeric or undefined"); } if (object instanceof ArrayValue) { return new ArrayValue(slice(object.value, start.value, stop.value, step.value)); } else { return new StringValue(slice(Array.from(object.value), start.value, stop.value, step.value).join("")); } } evaluateMemberExpression(expr, environment) { const object = this.evaluate(expr.object, environment); let property; if (expr.computed) { if (expr.property.type === "SliceExpression") { return this.evaluateSliceExpression(object, expr.property, environment); } else { property = this.evaluate(expr.property, environment); } } else { property = new StringValue(expr.property.value); } let value; if (object instanceof ObjectValue) { if (!(property instanceof StringValue)) { throw new Error(`Cannot access property with non-string: got ${property.type}`); } value = object.value.get(property.value) ?? object.builtins.get(property.value); } else if (object instanceof ArrayValue || object instanceof StringValue) { if (property instanceof NumericValue) { value = object.value.at(property.value); if (object instanceof StringValue) { value = new StringValue(object.value.at(property.value)); } } else if (property instanceof StringValue) { value = object.builtins.get(property.value); } else { throw new Error(`Cannot access property with non-string/non-number: got ${property.type}`); } } else { if (!(property instanceof StringValue)) { throw new Error(`Cannot access property with non-string: got ${property.type}`); } value = object.builtins.get(property.value); } return value instanceof RuntimeValue ? value : new UndefinedValue(); } evaluateSet(node, environment) { const rhs = this.evaluate(node.value, environment); if (node.assignee.type === "Identifier") { const variableName = node.assignee.value; environment.setVariable(variableName, rhs); } else if (node.assignee.type === "MemberExpression") { const member = node.assignee; const object = this.evaluate(member.object, environment); if (!(object instanceof ObjectValue)) { throw new Error("Cannot assign to member of non-object"); } if (member.property.type !== "Identifier") { throw new Error("Cannot assign to member with non-identifier property"); } object.value.set(member.property.value, rhs); } else { throw new Error(`Invalid LHS inside assignment expression: ${JSON.stringify(node.assignee)}`); } return new NullValue(); } evaluateIf(node, environment) { const test = this.evaluate(node.test, environment); return this.evaluateBlock(test.__bool__().value ? node.body : node.alternate, environment); } evaluateFor(node, environment) { const scope = new Environment(environment); const iterable = this.evaluate(node.iterable, scope); if (!(iterable instanceof ArrayValue)) { throw new Error(`Expected iterable type in for loop: got ${iterable.type}`); } let result = ""; for (let i = 0; i < iterable.value.length; ++i) { const loop = /* @__PURE__ */ new Map([ ["index", new NumericValue(i + 1)], ["index0", new NumericValue(i)], ["revindex", new NumericValue(iterable.value.length - i)], ["revindex0", new NumericValue(iterable.value.length - i - 1)], ["first", new BooleanValue(i === 0)], ["last", new BooleanValue(i === iterable.value.length - 1)], ["length", new NumericValue(iterable.value.length)], ["previtem", i > 0 ? iterable.value[i - 1] : new UndefinedValue()], ["nextitem", i < iterable.value.length - 1 ? iterable.value[i + 1] : new UndefinedValue()] ]); scope.setVariable("loop", new ObjectValue(loop)); const current = iterable.value[i]; if (node.loopvar.type === "Identifier") { scope.setVariable(node.loopvar.value, current); } else if (node.loopvar.type === "TupleLiteral") { const loopvar = node.loopvar; if (current.type !== "ArrayValue") { throw new Error(`Cannot unpack non-iterable type: ${current.type}`); } const c = current; if (loopvar.value.length !== c.value.length) { throw new Error(`Too ${loopvar.value.length > c.value.length ? "few" : "many"} items to unpack`); } for (let j = 0; j < loopvar.value.length; ++j) { if (loopvar.value[j].type !== "Identifier") { throw new Error(`Cannot unpack non-identifier type: ${loopvar.value[j].type}`); } scope.setVariable(loopvar.value[j].value, c.value[j]); } } const evaluated = this.evaluateBlock(node.body, scope); result += evaluated.value; } return new StringValue(result); } evaluate(statement, environment) { if (statement === void 0) return new UndefinedValue(); switch (statement.type) { case "Program": return this.evalProgram(statement, environment); case "Set": return this.evaluateSet(statement, environment); case "If": return this.evaluateIf(statement, environment); case "For": return this.evaluateFor(statement, environment); case "NumericLiteral": return new NumericValue(Number(statement.value)); case "StringLiteral": return new StringValue(statement.value); case "BooleanLiteral": return new BooleanValue(statement.value); case "ArrayLiteral": return new ArrayValue(statement.value.map((x) => this.evaluate(x, environment))); case "TupleLiteral": return new TupleValue(statement.value.map((x) => this.evaluate(x, environment))); case "ObjectLiteral": { const mapping = /* @__PURE__ */ new Map(); for (const [key, value] of statement.value) { const evaluatedKey = this.evaluate(key, environment); if (!(evaluatedKey instanceof StringValue)) { throw new Error(`Object keys must be strings: got ${evaluatedKey.type}`); } mapping.set(evaluatedKey.value, this.evaluate(value, environment)); } return new ObjectValue(mapping); } case "Identifier": return this.evaluateIdentifier(statement, environment); case "CallExpression": return this.evaluateCallExpression(statement, environment); case "MemberExpression": return this.evaluateMemberExpression(statement, environment); case "UnaryExpression": return this.evaluateUnaryExpression(statement, environment); case "BinaryExpression": return this.evaluateBinaryExpression(statement, environment); case "FilterExpression": return this.evaluateFilterExpression(statement, environment); case "TestExpression": return this.evaluateTestExpression(statement, environment); default: throw new SyntaxError(`Unknown node type: ${statement.type}`); } } }; function convertToRuntimeValues(input) { switch (typeof input) { case "number": return new NumericValue(input); case "string": return new StringValue(input); case "boolean": return new BooleanValue(input); case "object": if (input === null) { return new NullValue(); } else if (Array.isArray(input)) { return new ArrayValue(input.map(convertToRuntimeValues)); } else { return new ObjectValue( new Map(Object.entries(input).map(([key, value]) => [key, convertToRuntimeValues(value)])) ); } case "function": return new FunctionValue((args, _scope) => { const result = input(...args.map((x) => x.value)) ?? null; return convertToRuntimeValues(result); }); default: throw new Error(`Cannot convert to runtime value: ${input}`); } } // src/index.ts var Template = class { parsed; /** * @param {string} template The template string */ constructor(template) { const tokens = tokenize(template, { lstrip_blocks: true, trim_blocks: true }); this.parsed = parse(tokens); } render(items) { const env = new Environment(); env.set("false", false); env.set("true", true); env.set("raise_exception", (args) => { throw new Error(args); }); env.set("range", range); for (const [key, value] of Object.entries(items)) { env.set(key, value); } const interpreter = new Interpreter(env); const result = interpreter.run(this.parsed); return result.value; } }; /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js": /*!******************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/backend-impl.js ***! \******************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "registerBackend": () => (/* binding */ registerBackend), /* harmony export */ "resolveBackend": () => (/* binding */ resolveBackend) /* harmony export */ }); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. const backends = new Map(); const backendsSortedByPriority = []; /** * Register a backend. * * @param name - the name as a key to lookup as an execution provider. * @param backend - the backend object. * @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority * < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default. * * @ignore */ const registerBackend = (name, backend, priority) => { if (backend && typeof backend.init === 'function' && typeof backend.createInferenceSessionHandler === 'function') { const currentBackend = backends.get(name); if (currentBackend === undefined) { backends.set(name, { backend, priority }); } else if (currentBackend.priority > priority) { // same name is already registered with a higher priority. skip registeration. return; } else if (currentBackend.priority === priority) { if (currentBackend.backend !== backend) { throw new Error(`cannot register backend "${name}" using priority ${priority}`); } } if (priority >= 0) { const i = backendsSortedByPriority.indexOf(name); if (i !== -1) { backendsSortedByPriority.splice(i, 1); } for (let i = 0; i < backendsSortedByPriority.length; i++) { if (backends.get(backendsSortedByPriority[i]).priority <= priority) { backendsSortedByPriority.splice(i, 0, name); return; } } backendsSortedByPriority.push(name); } return; } throw new TypeError('not a valid backend'); }; /** * Resolve backend by specified hints. * * @param backendHints - a list of execution provider names to lookup. If omitted use registered backends as list. * @returns a promise that resolves to the backend. * * @ignore */ const resolveBackend = async (backendHints) => { const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints; const errors = []; for (const backendName of backendNames) { const backendInfo = backends.get(backendName); if (backendInfo) { if (backendInfo.initialized) { return backendInfo.backend; } else if (backendInfo.aborted) { continue; // current backend is unavailable; try next } const isInitializing = !!backendInfo.initPromise; try { if (!isInitializing) { backendInfo.initPromise = backendInfo.backend.init(backendName); } await backendInfo.initPromise; backendInfo.initialized = true; return backendInfo.backend; } catch (e) { if (!isInitializing) { errors.push({ name: backendName, err: e }); } backendInfo.aborted = true; } finally { delete backendInfo.initPromise; } } } throw new Error(`no available backend found. ERR: ${errors.map(e => `[${e.name}] ${e.err}`).join(', ')}`); }; //# sourceMappingURL=backend-impl.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/backend.js": /*!*************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/backend.js ***! \*************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "registerBackend": () => (/* reexport safe */ _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend) /* harmony export */ }); /* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. //# sourceMappingURL=backend.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/env-impl.js": /*!**************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/env-impl.js ***! \**************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "env": () => (/* binding */ env) /* harmony export */ }); /* harmony import */ var _version_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./version.js */ "./node_modules/onnxruntime-common/dist/esm/version.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. let logLevelValue = 'warning'; const env = { wasm: {}, webgl: {}, webgpu: {}, versions: { common: _version_js__WEBPACK_IMPORTED_MODULE_0__.version }, set logLevel(value) { if (value === undefined) { return; } if (typeof value !== 'string' || ['verbose', 'info', 'warning', 'error', 'fatal'].indexOf(value) === -1) { throw new Error(`Unsupported logging level: ${value}`); } logLevelValue = value; }, get logLevel() { return logLevelValue; }, }; // set property 'logLevel' so that they can be correctly transferred to worker by `postMessage()`. Object.defineProperty(env, 'logLevel', { enumerable: true }); //# sourceMappingURL=env-impl.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/env.js": /*!*********************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/env.js ***! \*********************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "env": () => (/* binding */ env) /* harmony export */ }); /* harmony import */ var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env-impl.js */ "./node_modules/onnxruntime-common/dist/esm/env-impl.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. /** * Represent a set of flags as a global singleton. */ const env = _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env; //# sourceMappingURL=env.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/index.js": /*!***********************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/index.js ***! \***********************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "InferenceSession": () => (/* reexport safe */ _inference_session_js__WEBPACK_IMPORTED_MODULE_2__.InferenceSession), /* harmony export */ "TRACE": () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_4__.TRACE), /* harmony export */ "TRACE_FUNC_BEGIN": () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_4__.TRACE_FUNC_BEGIN), /* harmony export */ "TRACE_FUNC_END": () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_4__.TRACE_FUNC_END), /* harmony export */ "Tensor": () => (/* reexport safe */ _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor), /* harmony export */ "TrainingSession": () => (/* reexport safe */ _training_session_js__WEBPACK_IMPORTED_MODULE_6__.TrainingSession), /* harmony export */ "env": () => (/* reexport safe */ _env_js__WEBPACK_IMPORTED_MODULE_1__.env), /* harmony export */ "registerBackend": () => (/* reexport safe */ _backend_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend) /* harmony export */ }); /* harmony import */ var _backend_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend.js */ "./node_modules/onnxruntime-common/dist/esm/backend.js"); /* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./env.js */ "./node_modules/onnxruntime-common/dist/esm/env.js"); /* harmony import */ var _inference_session_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./inference-session.js */ "./node_modules/onnxruntime-common/dist/esm/inference-session.js"); /* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); /* harmony import */ var _trace_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./trace.js */ "./node_modules/onnxruntime-common/dist/esm/trace.js"); /* harmony import */ var _onnx_value_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./onnx-value.js */ "./node_modules/onnxruntime-common/dist/esm/onnx-value.js"); /* harmony import */ var _training_session_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./training-session.js */ "./node_modules/onnxruntime-common/dist/esm/training-session.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. /** * # ONNX Runtime JavaScript API * * ONNX Runtime JavaScript API is a unified API for all JavaScript usages, including the following NPM packages: * * - [onnxruntime-node](https://www.npmjs.com/package/onnxruntime-node) * - [onnxruntime-web](https://www.npmjs.com/package/onnxruntime-web) * - [onnxruntime-react-native](https://www.npmjs.com/package/onnxruntime-react-native) * * See also: * - [Get Started](https://onnxruntime.ai/docs/get-started/with-javascript.html) * - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js) * * @packageDocumentation */ //# sourceMappingURL=index.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js": /*!****************************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js ***! \****************************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "InferenceSession": () => (/* binding */ InferenceSession) /* harmony export */ }); /* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); /* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); /* harmony import */ var _trace_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./trace.js */ "./node_modules/onnxruntime-common/dist/esm/trace.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. class InferenceSession { constructor(handler) { this.handler = handler; } async run(feeds, arg1, arg2) { (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); const fetches = {}; let options = {}; // check inputs if (typeof feeds !== 'object' || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { throw new TypeError('\'feeds\' must be an object that use input names as keys and OnnxValue as corresponding values.'); } let isFetchesEmpty = true; // determine which override is being used if (typeof arg1 === 'object') { if (arg1 === null) { throw new TypeError('Unexpected argument[1]: cannot be null.'); } if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { throw new TypeError('\'fetches\' cannot be a Tensor'); } if (Array.isArray(arg1)) { if (arg1.length === 0) { throw new TypeError('\'fetches\' cannot be an empty array.'); } isFetchesEmpty = false; // output names for (const name of arg1) { if (typeof name !== 'string') { throw new TypeError('\'fetches\' must be a string array or an object.'); } if (this.outputNames.indexOf(name) === -1) { throw new RangeError(`'fetches' contains invalid output name: ${name}.`); } fetches[name] = null; } if (typeof arg2 === 'object' && arg2 !== null) { options = arg2; } else if (typeof arg2 !== 'undefined') { throw new TypeError('\'options\' must be an object.'); } } else { // decide whether arg1 is fetches or options // if any output name is present and its value is valid OnnxValue, we consider it fetches let isFetches = false; const arg1Keys = Object.getOwnPropertyNames(arg1); for (const name of this.outputNames) { if (arg1Keys.indexOf(name) !== -1) { const v = arg1[name]; if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { isFetches = true; isFetchesEmpty = false; fetches[name] = v; } } } if (isFetches) { if (typeof arg2 === 'object' && arg2 !== null) { options = arg2; } else if (typeof arg2 !== 'undefined') { throw new TypeError('\'options\' must be an object.'); } } else { options = arg1; } } } else if (typeof arg1 !== 'undefined') { throw new TypeError('Unexpected argument[1]: must be \'fetches\' or \'options\'.'); } // check if all inputs are in feed for (const name of this.inputNames) { if (typeof feeds[name] === 'undefined') { throw new Error(`input '${name}' is missing in 'feeds'.`); } } // if no fetches is specified, we use the full output names list if (isFetchesEmpty) { for (const name of this.outputNames) { fetches[name] = null; } } // feeds, fetches and options are prepared const results = await this.handler.run(feeds, fetches, options); const returnValue = {}; for (const key in results) { if (Object.hasOwnProperty.call(results, key)) { const result = results[key]; if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { returnValue[key] = result; } else { returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); } } } (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); return returnValue; } async release() { return this.handler.dispose(); } static async create(arg0, arg1, arg2, arg3) { (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); // either load from a file or buffer let filePathOrUint8Array; let options = {}; if (typeof arg0 === 'string') { filePathOrUint8Array = arg0; if (typeof arg1 === 'object' && arg1 !== null) { options = arg1; } else if (typeof arg1 !== 'undefined') { throw new TypeError('\'options\' must be an object.'); } } else if (arg0 instanceof Uint8Array) { filePathOrUint8Array = arg0; if (typeof arg1 === 'object' && arg1 !== null) { options = arg1; } else if (typeof arg1 !== 'undefined') { throw new TypeError('\'options\' must be an object.'); } } else if (arg0 instanceof ArrayBuffer || (typeof SharedArrayBuffer !== 'undefined' && arg0 instanceof SharedArrayBuffer)) { const buffer = arg0; let byteOffset = 0; let byteLength = arg0.byteLength; if (typeof arg1 === 'object' && arg1 !== null) { options = arg1; } else if (typeof arg1 === 'number') { byteOffset = arg1; if (!Number.isSafeInteger(byteOffset)) { throw new RangeError('\'byteOffset\' must be an integer.'); } if (byteOffset < 0 || byteOffset >= buffer.byteLength) { throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`); } byteLength = arg0.byteLength - byteOffset; if (typeof arg2 === 'number') { byteLength = arg2; if (!Number.isSafeInteger(byteLength)) { throw new RangeError('\'byteLength\' must be an integer.'); } if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) { throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`); } if (typeof arg3 === 'object' && arg3 !== null) { options = arg3; } else if (typeof arg3 !== 'undefined') { throw new TypeError('\'options\' must be an object.'); } } else if (typeof arg2 !== 'undefined') { throw new TypeError('\'byteLength\' must be a number.'); } } else if (typeof arg1 !== 'undefined') { throw new TypeError('\'options\' must be an object.'); } filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength); } else { throw new TypeError('Unexpected argument[0]: must be \'path\' or \'buffer\'.'); } // get backend hints const eps = options.executionProviders || []; const backendHints = eps.map(i => typeof i === 'string' ? i : i.name); const backend = await (0,_backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackend)(backendHints); const handler = await backend.createInferenceSessionHandler(filePathOrUint8Array, options); (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); return new InferenceSession(handler); } startProfiling() { this.handler.startProfiling(); } endProfiling() { this.handler.endProfiling(); } get inputNames() { return this.handler.inputNames; } get outputNames() { return this.handler.outputNames; } } //# sourceMappingURL=inference-session-impl.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/inference-session.js": /*!***********************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/inference-session.js ***! \***********************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "InferenceSession": () => (/* binding */ InferenceSession) /* harmony export */ }); /* harmony import */ var _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./inference-session-impl.js */ "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // eslint-disable-next-line @typescript-eslint/naming-convention const InferenceSession = _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.InferenceSession; //# sourceMappingURL=inference-session.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/onnx-value.js": /*!****************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/onnx-value.js ***! \****************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. //# sourceMappingURL=onnx-value.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js": /*!****************************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js ***! \****************************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "tensorToDataURL": () => (/* binding */ tensorToDataURL), /* harmony export */ "tensorToImageData": () => (/* binding */ tensorToImageData) /* harmony export */ }); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. /** * implementation of Tensor.toDataURL() */ const tensorToDataURL = (tensor, options) => { const canvas = typeof document !== 'undefined' ? document.createElement('canvas') : (new OffscreenCanvas(1, 1)); canvas.width = tensor.dims[3]; canvas.height = tensor.dims[2]; const pixels2DContext = canvas.getContext('2d'); if (pixels2DContext != null) { // Default values for height and width & format let width; let height; if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') { width = tensor.dims[2]; height = tensor.dims[3]; } else { // Default layout is NCWH width = tensor.dims[3]; height = tensor.dims[2]; } const inputformat = options?.format !== undefined ? options.format : 'RGB'; const norm = options?.norm; let normMean; let normBias; if (norm === undefined || norm.mean === undefined) { normMean = [255, 255, 255, 255]; } else { if (typeof (norm.mean) === 'number') { normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; } else { normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 0]; if (norm.mean[3] !== undefined) { normMean[3] = norm.mean[3]; } } } if (norm === undefined || norm.bias === undefined) { normBias = [0, 0, 0, 0]; } else { if (typeof (norm.bias) === 'number') { normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; } else { normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; if (norm.bias[3] !== undefined) { normBias[3] = norm.bias[3]; } } } const stride = height * width; // Default pointer assignments let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; // Updating the pointer assignments based on the input image format if (inputformat === 'RGBA') { rTensorPointer = 0; gTensorPointer = stride; bTensorPointer = stride * 2; aTensorPointer = stride * 3; } else if (inputformat === 'RGB') { rTensorPointer = 0; gTensorPointer = stride; bTensorPointer = stride * 2; } else if (inputformat === 'RBG') { rTensorPointer = 0; bTensorPointer = stride; gTensorPointer = stride * 2; } for (let i = 0; i < height; i++) { for (let j = 0; j < width; j++) { const R = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value const G = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value const B = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value const A = aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value // eslint-disable-next-line @typescript-eslint/restrict-plus-operands pixels2DContext.fillStyle = 'rgba(' + R + ',' + G + ',' + B + ',' + A + ')'; pixels2DContext.fillRect(j, i, 1, 1); } } if ('toDataURL' in canvas) { return canvas.toDataURL(); } else { throw new Error('toDataURL is not supported'); } } else { throw new Error('Can not access image data'); } }; /** * implementation of Tensor.toImageData() */ const tensorToImageData = (tensor, options) => { const pixels2DContext = typeof document !== 'undefined' ? document.createElement('canvas').getContext('2d') : new OffscreenCanvas(1, 1).getContext('2d'); let image; if (pixels2DContext != null) { // Default values for height and width & format let width; let height; let channels; if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') { width = tensor.dims[2]; height = tensor.dims[1]; channels = tensor.dims[3]; } else { // Default layout is NCWH width = tensor.dims[3]; height = tensor.dims[2]; channels = tensor.dims[1]; } const inputformat = options !== undefined ? (options.format !== undefined ? options.format : 'RGB') : 'RGB'; const norm = options?.norm; let normMean; let normBias; if (norm === undefined || norm.mean === undefined) { normMean = [255, 255, 255, 255]; } else { if (typeof (norm.mean) === 'number') { normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; } else { normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 255]; if (norm.mean[3] !== undefined) { normMean[3] = norm.mean[3]; } } } if (norm === undefined || norm.bias === undefined) { normBias = [0, 0, 0, 0]; } else { if (typeof (norm.bias) === 'number') { normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; } else { normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; if (norm.bias[3] !== undefined) { normBias[3] = norm.bias[3]; } } } const stride = height * width; if (options !== undefined) { if (options.format !== undefined && (channels === 4 && options.format !== 'RGBA') || (channels === 3 && (options.format !== 'RGB' && options.format !== 'BGR'))) { throw new Error('Tensor format doesn\'t match input tensor dims'); } } // Default pointer assignments const step = 4; let rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; // Updating the pointer assignments based on the input image format if (inputformat === 'RGBA') { rTensorPointer = 0; gTensorPointer = stride; bTensorPointer = stride * 2; aTensorPointer = stride * 3; } else if (inputformat === 'RGB') { rTensorPointer = 0; gTensorPointer = stride; bTensorPointer = stride * 2; } else if (inputformat === 'RBG') { rTensorPointer = 0; bTensorPointer = stride; gTensorPointer = stride * 2; } image = pixels2DContext.createImageData(width, height); for (let i = 0; i < height * width; rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++) { image.data[rImagePointer] = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value image.data[gImagePointer] = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value image.data[bImagePointer] = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value image.data[aImagePointer] = aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value } } else { throw new Error('Can not access image data'); } return image; }; //# sourceMappingURL=tensor-conversion-impl.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js": /*!*************************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js ***! \*************************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "bufferToTensor": () => (/* binding */ bufferToTensor), /* harmony export */ "tensorFromGpuBuffer": () => (/* binding */ tensorFromGpuBuffer), /* harmony export */ "tensorFromImage": () => (/* binding */ tensorFromImage), /* harmony export */ "tensorFromPinnedBuffer": () => (/* binding */ tensorFromPinnedBuffer), /* harmony export */ "tensorFromTexture": () => (/* binding */ tensorFromTexture) /* harmony export */ }); /* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. /** * Create a new tensor object from image object * * @param buffer - Extracted image buffer data - assuming RGBA format * @param imageFormat - input image configuration - required configurations height, width, format * @param tensorFormat - output tensor configuration - Default is RGB format */ const bufferToTensor = (buffer, options) => { if (buffer === undefined) { throw new Error('Image buffer must be defined'); } if (options.height === undefined || options.width === undefined) { throw new Error('Image height and width must be defined'); } if (options.tensorLayout === 'NHWC') { throw new Error('NHWC Tensor layout is not supported yet'); } const { height, width } = options; const norm = options.norm ?? { mean: 255, bias: 0 }; let normMean; let normBias; if (typeof (norm.mean) === 'number') { normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; } else { normMean = [norm.mean[0], norm.mean[1], norm.mean[2], norm.mean[3] ?? 255]; } if (typeof (norm.bias) === 'number') { normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; } else { normBias = [norm.bias[0], norm.bias[1], norm.bias[2], norm.bias[3] ?? 0]; } const inputformat = options.format !== undefined ? options.format : 'RGBA'; // default value is RGBA since imagedata and HTMLImageElement uses it const outputformat = options.tensorFormat !== undefined ? (options.tensorFormat !== undefined ? options.tensorFormat : 'RGB') : 'RGB'; const stride = height * width; const float32Data = outputformat === 'RGBA' ? new Float32Array(stride * 4) : new Float32Array(stride * 3); // Default pointer assignments let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; // Updating the pointer assignments based on the input image format if (inputformat === 'RGB') { step = 3; rImagePointer = 0; gImagePointer = 1; bImagePointer = 2; aImagePointer = -1; } // Updating the pointer assignments based on the output tensor format if (outputformat === 'RGBA') { aTensorPointer = stride * 3; } else if (outputformat === 'RBG') { rTensorPointer = 0; bTensorPointer = stride; gTensorPointer = stride * 2; } else if (outputformat === 'BGR') { bTensorPointer = 0; gTensorPointer = stride; rTensorPointer = stride * 2; } for (let i = 0; i < stride; i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) { float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias[0]) / normMean[0]; float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias[1]) / normMean[1]; float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias[2]) / normMean[2]; if (aTensorPointer !== -1 && aImagePointer !== -1) { float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias[3]) / normMean[3]; } } // Float32Array -> ort.Tensor const outputTensor = outputformat === 'RGBA' ? new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor('float32', float32Data, [1, 4, height, width]) : new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor('float32', float32Data, [1, 3, height, width]); return outputTensor; }; /** * implementation of Tensor.fromImage(). */ const tensorFromImage = async (image, options) => { // checking the type of image object const isHTMLImageEle = typeof (HTMLImageElement) !== 'undefined' && image instanceof HTMLImageElement; const isImageDataEle = typeof (ImageData) !== 'undefined' && image instanceof ImageData; const isImageBitmap = typeof (ImageBitmap) !== 'undefined' && image instanceof ImageBitmap; const isString = typeof image === 'string'; let data; let bufferToTensorOptions = options ?? {}; const createCanvas = () => { if (typeof document !== 'undefined') { return document.createElement('canvas'); } else if (typeof OffscreenCanvas !== 'undefined') { return new OffscreenCanvas(1, 1); } else { throw new Error('Canvas is not supported'); } }; const createCanvasContext = (canvas) => { if (canvas instanceof HTMLCanvasElement) { return canvas.getContext('2d'); } else if (canvas instanceof OffscreenCanvas) { return canvas.getContext('2d'); } else { return null; } }; // filling and checking image configuration options if (isHTMLImageEle) { // HTMLImageElement - image object - format is RGBA by default const canvas = createCanvas(); canvas.width = image.width; canvas.height = image.height; const pixels2DContext = createCanvasContext(canvas); if (pixels2DContext != null) { let height = image.height; let width = image.width; if (options !== undefined && options.resizedHeight !== undefined && options.resizedWidth !== undefined) { height = options.resizedHeight; width = options.resizedWidth; } if (options !== undefined) { bufferToTensorOptions = options; if (options.tensorFormat !== undefined) { throw new Error('Image input config format must be RGBA for HTMLImageElement'); } else { bufferToTensorOptions.tensorFormat = 'RGBA'; } bufferToTensorOptions.height = height; bufferToTensorOptions.width = width; } else { bufferToTensorOptions.tensorFormat = 'RGBA'; bufferToTensorOptions.height = height; bufferToTensorOptions.width = width; } pixels2DContext.drawImage(image, 0, 0); data = pixels2DContext.getImageData(0, 0, width, height).data; } else { throw new Error('Can not access image data'); } } else if (isImageDataEle) { let height; let width; if (options !== undefined && options.resizedWidth !== undefined && options.resizedHeight !== undefined) { height = options.resizedHeight; width = options.resizedWidth; } else { height = image.height; width = image.width; } if (options !== undefined) { bufferToTensorOptions = options; } bufferToTensorOptions.format = 'RGBA'; bufferToTensorOptions.height = height; bufferToTensorOptions.width = width; if (options !== undefined) { const tempCanvas = createCanvas(); tempCanvas.width = width; tempCanvas.height = height; const pixels2DContext = createCanvasContext(tempCanvas); if (pixels2DContext != null) { pixels2DContext.putImageData(image, 0, 0); data = pixels2DContext.getImageData(0, 0, width, height).data; } else { throw new Error('Can not access image data'); } } else { data = image.data; } } else if (isImageBitmap) { // ImageBitmap - image object - format must be provided by user if (options === undefined) { throw new Error('Please provide image config with format for Imagebitmap'); } const canvas = createCanvas(); canvas.width = image.width; canvas.height = image.height; const pixels2DContext = createCanvasContext(canvas); if (pixels2DContext != null) { const height = image.height; const width = image.width; pixels2DContext.drawImage(image, 0, 0, width, height); data = pixels2DContext.getImageData(0, 0, width, height).data; bufferToTensorOptions.height = height; bufferToTensorOptions.width = width; return bufferToTensor(data, bufferToTensorOptions); } else { throw new Error('Can not access image data'); } } else if (isString) { return new Promise((resolve, reject) => { const canvas = createCanvas(); const context = createCanvasContext(canvas); if (!image || !context) { return reject(); } const newImage = new Image(); newImage.crossOrigin = 'Anonymous'; newImage.src = image; newImage.onload = () => { canvas.width = newImage.width; canvas.height = newImage.height; context.drawImage(newImage, 0, 0, canvas.width, canvas.height); const img = context.getImageData(0, 0, canvas.width, canvas.height); bufferToTensorOptions.height = canvas.height; bufferToTensorOptions.width = canvas.width; resolve(bufferToTensor(img.data, bufferToTensorOptions)); }; }); } else { throw new Error('Input data provided is not supported - aborted tensor creation'); } if (data !== undefined) { return bufferToTensor(data, bufferToTensorOptions); } else { throw new Error('Input data provided is not supported - aborted tensor creation'); } }; /** * implementation of Tensor.fromTexture(). */ const tensorFromTexture = (texture, options) => { const { width, height, download, dispose } = options; // Always assume RGBAF32. TODO: support different texture format const dims = [1, height, width, 4]; return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'texture', type: 'float32', texture, dims, download, dispose }); }; /** * implementation of Tensor.fromGpuBuffer(). */ const tensorFromGpuBuffer = (gpuBuffer, options) => { const { dataType, dims, download, dispose } = options; return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'gpu-buffer', type: dataType ?? 'float32', gpuBuffer, dims, download, dispose }); }; /** * implementation of Tensor.fromPinnedBuffer(). */ const tensorFromPinnedBuffer = (type, buffer, dims) => new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'cpu-pinned', type, data: buffer, dims: dims ?? [buffer.length] }); //# sourceMappingURL=tensor-factory-impl.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js": /*!******************************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js ***! \******************************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP": () => (/* binding */ NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP), /* harmony export */ "NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP": () => (/* binding */ NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP), /* harmony export */ "checkBigInt": () => (/* binding */ checkBigInt) /* harmony export */ }); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap. const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = new Map([ ['float32', Float32Array], ['uint8', Uint8Array], ['int8', Int8Array], ['uint16', Uint16Array], ['float16', Uint16Array], ['int16', Int16Array], ['int32', Int32Array], ['bool', Uint8Array], ['float64', Float64Array], ['uint32', Uint32Array], ]); // a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap. const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = new Map([ [Float32Array, 'float32'], [Uint8Array, 'uint8'], [Int8Array, 'int8'], [Uint16Array, 'uint16'], [Int16Array, 'int16'], [Int32Array, 'int32'], [Float64Array, 'float64'], [Uint32Array, 'uint32'], ]); // the following code allows delaying execution of BigInt checking. This allows lazy initialization for // NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP and NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP, which allows BigInt polyfill // if available. let isBigIntChecked = false; const checkBigInt = () => { if (!isBigIntChecked) { isBigIntChecked = true; const isBigInt64ArrayAvailable = typeof BigInt64Array !== 'undefined' && typeof BigInt64Array.from === 'function'; const isBigUint64ArrayAvailable = typeof BigUint64Array !== 'undefined' && typeof BigUint64Array.from === 'function'; if (isBigInt64ArrayAvailable) { NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('int64', BigInt64Array); NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, 'int64'); } if (isBigUint64ArrayAvailable) { NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('uint64', BigUint64Array); NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, 'uint64'); } } }; //# sourceMappingURL=tensor-impl-type-mapping.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js": /*!*****************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl.js ***! \*****************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "Tensor": () => (/* binding */ Tensor) /* harmony export */ }); /* harmony import */ var _tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-conversion-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js"); /* harmony import */ var _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor-factory-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js"); /* harmony import */ var _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./tensor-impl-type-mapping.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js"); /* harmony import */ var _tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor-utils-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. /** * the implementation of Tensor interface. * * @ignore */ class Tensor { /** * implementation. */ constructor(arg0, arg1, arg2) { // perform one-time check for BigInt support (0,_tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.checkBigInt)(); let type; let dims; if (typeof arg0 === 'object' && 'location' in arg0) { // // constructing tensor from specific location // this.dataLocation = arg0.location; type = arg0.type; dims = arg0.dims; switch (arg0.location) { case 'cpu-pinned': { const expectedTypedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(type); if (!expectedTypedArrayConstructor) { throw new TypeError(`unsupported type "${type}" to create tensor from pinned buffer`); } if (!(arg0.data instanceof expectedTypedArrayConstructor)) { throw new TypeError(`buffer should be of type ${expectedTypedArrayConstructor.name}`); } this.cpuData = arg0.data; break; } case 'texture': { if (type !== 'float32') { throw new TypeError(`unsupported type "${type}" to create tensor from texture`); } this.gpuTextureData = arg0.texture; this.downloader = arg0.download; this.disposer = arg0.dispose; break; } case 'gpu-buffer': { if ((type !== 'float32' && type !== 'float16' && type !== 'int32' && type !== 'int64' && type !== 'uint32' && type !== 'bool')) { throw new TypeError(`unsupported type "${type}" to create tensor from gpu buffer`); } this.gpuBufferData = arg0.gpuBuffer; this.downloader = arg0.download; this.disposer = arg0.dispose; break; } default: throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`); } } else { // // constructing tensor of location 'cpu' // let data; let maybeDims; // check whether arg0 is type or data if (typeof arg0 === 'string') { // // Override: constructor(type, data, ...) // type = arg0; maybeDims = arg2; if (arg0 === 'string') { // string tensor if (!Array.isArray(arg1)) { throw new TypeError('A string tensor\'s data must be a string array.'); } // we don't check whether every element in the array is string; this is too slow. we assume it's correct and // error will be populated at inference data = arg1; } else { // numeric tensor const typedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0); if (typedArrayConstructor === undefined) { throw new TypeError(`Unsupported tensor type: ${arg0}.`); } if (Array.isArray(arg1)) { if (arg0 === 'float16') { // Throw error here because when user try to use number array as data, // e.g. new Tensor('float16', [1, 2, 3, 4], dims)), it will actually call // Uint16Array.from(arg1) which generates wrong data. throw new TypeError('Creating a float16 tensor from number array is not supported. Please use Uint16Array as data.'); } else if (arg0 === 'uint64' || arg0 === 'int64') { // use 'as any' here because: // 1. TypeScript's check on type of 'Array.isArray()' does not work with readonly arrays. // see https://github.com/microsoft/TypeScript/issues/17002 // 2. TypeScript's check on union type of '(BigInt64ArrayConstructor|BigUint64ArrayConstructor).from()' // does not accept parameter mapFn. // 3. parameters of 'SupportedTypedArrayConstructors.from()' does not match the requirement of the union // type. // assume 'arg1' is of type "readonly number[]|readonly bigint[]" here. // eslint-disable-next-line @typescript-eslint/no-explicit-any data = typedArrayConstructor.from(arg1, BigInt); } else { // assume 'arg1' is of type "readonly number[]" here. // eslint-disable-next-line @typescript-eslint/no-explicit-any data = typedArrayConstructor.from(arg1); } } else if (arg1 instanceof typedArrayConstructor) { data = arg1; } else { throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`); } } } else { // // Override: constructor(data, ...) // maybeDims = arg1; if (Array.isArray(arg0)) { // only boolean[] and string[] is supported if (arg0.length === 0) { throw new TypeError('Tensor type cannot be inferred from an empty array.'); } const firstElementType = typeof arg0[0]; if (firstElementType === 'string') { type = 'string'; data = arg0; } else if (firstElementType === 'boolean') { type = 'bool'; // 'arg0' is of type 'boolean[]'. Uint8Array.from(boolean[]) actually works, but typescript thinks this is // wrong type. We use 'as any' to make it happy. // eslint-disable-next-line @typescript-eslint/no-explicit-any data = Uint8Array.from(arg0); } else { throw new TypeError(`Invalid element type of data array: ${firstElementType}.`); } } else { // get tensor type from TypedArray const mappedType = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(arg0.constructor); if (mappedType === undefined) { throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`); } type = mappedType; data = arg0; } } // type and data is processed, now processing dims if (maybeDims === undefined) { // assume 1-D tensor if dims omitted maybeDims = [data.length]; } else if (!Array.isArray(maybeDims)) { throw new TypeError('A tensor\'s dims must be a number array'); } dims = maybeDims; this.cpuData = data; this.dataLocation = 'cpu'; } // perform check on dims const size = (0,_tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.calculateSize)(dims); // if data is on CPU, check whether data length matches tensor size if (this.cpuData && size !== this.cpuData.length) { throw new Error(`Tensor's size(${size}) does not match data length(${this.cpuData.length}).`); } this.type = type; this.dims = dims; this.size = size; } // #endregion // #region factory static async fromImage(image, options) { return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromImage)(image, options); } static fromTexture(texture, options) { return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromTexture)(texture, options); } static fromGpuBuffer(gpuBuffer, options) { return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromGpuBuffer)(gpuBuffer, options); } static fromPinnedBuffer(type, buffer, dims) { return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromPinnedBuffer)(type, buffer, dims); } // #endregion // #region conversions toDataURL(options) { return (0,_tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToDataURL)(this, options); } toImageData(options) { return (0,_tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToImageData)(this, options); } // #endregion // #region properties get data() { this.ensureValid(); if (!this.cpuData) { throw new Error('The data is not on CPU. Use `getData()` to download GPU data to CPU, ' + 'or use `texture` or `gpuBuffer` property to access the GPU data directly.'); } return this.cpuData; } get location() { return this.dataLocation; } get texture() { this.ensureValid(); if (!this.gpuTextureData) { throw new Error('The data is not stored as a WebGL texture.'); } return this.gpuTextureData; } get gpuBuffer() { this.ensureValid(); if (!this.gpuBufferData) { throw new Error('The data is not stored as a WebGPU buffer.'); } return this.gpuBufferData; } // #endregion // #region methods async getData(releaseData) { this.ensureValid(); switch (this.dataLocation) { case 'cpu': case 'cpu-pinned': return this.data; case 'texture': case 'gpu-buffer': { if (!this.downloader) { throw new Error('The current tensor is not created with a specified data downloader.'); } if (this.isDownloading) { throw new Error('The current tensor is being downloaded.'); } try { this.isDownloading = true; const data = await this.downloader(); this.downloader = undefined; this.dataLocation = 'cpu'; this.cpuData = data; if (releaseData && this.disposer) { this.disposer(); this.disposer = undefined; } return data; } finally { this.isDownloading = false; } } default: throw new Error(`cannot get data from location: ${this.dataLocation}`); } } dispose() { if (this.isDownloading) { throw new Error('The current tensor is being downloaded.'); } if (this.disposer) { this.disposer(); this.disposer = undefined; } this.cpuData = undefined; this.gpuTextureData = undefined; this.gpuBufferData = undefined; this.downloader = undefined; this.isDownloading = undefined; this.dataLocation = 'none'; } // #endregion // #region tensor utilities ensureValid() { if (this.dataLocation === 'none') { throw new Error('The tensor is disposed.'); } } reshape(dims) { this.ensureValid(); if (this.downloader || this.disposer) { throw new Error('Cannot reshape a tensor that owns GPU resource.'); } return (0,_tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.tensorReshape)(this, dims); } } //# sourceMappingURL=tensor-impl.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js": /*!***********************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js ***! \***********************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "calculateSize": () => (/* binding */ calculateSize), /* harmony export */ "tensorReshape": () => (/* binding */ tensorReshape) /* harmony export */ }); /* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. /** * calculate size from dims. * * @param dims the dims array. May be an illegal input. */ const calculateSize = (dims) => { let size = 1; for (let i = 0; i < dims.length; i++) { const dim = dims[i]; if (typeof dim !== 'number' || !Number.isSafeInteger(dim)) { throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`); } if (dim < 0) { throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`); } size *= dim; } return size; }; /** * implementation of Tensor.reshape() */ const tensorReshape = (tensor, dims) => { switch (tensor.location) { case 'cpu': return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor(tensor.type, tensor.data, dims); case 'cpu-pinned': return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'cpu-pinned', data: tensor.data, type: tensor.type, dims, }); case 'texture': return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'texture', texture: tensor.texture, type: tensor.type, dims, }); case 'gpu-buffer': return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'gpu-buffer', gpuBuffer: tensor.gpuBuffer, type: tensor.type, dims, }); default: throw new Error(`tensorReshape: tensor location ${tensor.location} is not supported`); } }; //# sourceMappingURL=tensor-utils-impl.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/tensor.js": /*!************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/tensor.js ***! \************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "Tensor": () => (/* binding */ Tensor) /* harmony export */ }); /* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // eslint-disable-next-line @typescript-eslint/naming-convention const Tensor = _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor; //# sourceMappingURL=tensor.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/trace.js": /*!***********************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/trace.js ***! \***********************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "TRACE": () => (/* binding */ TRACE), /* harmony export */ "TRACE_FUNC_BEGIN": () => (/* binding */ TRACE_FUNC_BEGIN), /* harmony export */ "TRACE_FUNC_END": () => (/* binding */ TRACE_FUNC_END) /* harmony export */ }); /* harmony import */ var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env-impl.js */ "./node_modules/onnxruntime-common/dist/esm/env-impl.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. const TRACE = (deviceType, label) => { if (!_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace) { return; } // eslint-disable-next-line no-console console.timeStamp(`${deviceType}::ORT::${label}`); }; const TRACE_FUNC = (msg, extraMsg) => { const stack = new Error().stack?.split(/\r\n|\r|\n/g) || []; let hasTraceFunc = false; for (let i = 0; i < stack.length; i++) { if (hasTraceFunc && !stack[i].includes('TRACE_FUNC')) { let label = `FUNC_${msg}::${stack[i].trim().split(' ')[1]}`; if (extraMsg) { label += `::${extraMsg}`; } TRACE('CPU', label); return; } if (stack[i].includes('TRACE_FUNC')) { hasTraceFunc = true; } } }; const TRACE_FUNC_BEGIN = (extraMsg) => { if (!_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace) { return; } TRACE_FUNC('BEGIN', extraMsg); }; const TRACE_FUNC_END = (extraMsg) => { if (!_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace) { return; } TRACE_FUNC('END', extraMsg); }; //# sourceMappingURL=trace.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/training-session-impl.js": /*!***************************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/training-session-impl.js ***! \***************************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "TrainingSession": () => (/* binding */ TrainingSession) /* harmony export */ }); /* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); /* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. const noBackendErrMsg = 'Training backend could not be resolved. ' + 'Make sure you\'re using the correct configuration & WebAssembly files.'; class TrainingSession { constructor(handler, hasOptimizerModel, hasEvalModel) { this.handler = handler; this.hasOptimizerModel = hasOptimizerModel; this.hasEvalModel = hasEvalModel; } get trainingInputNames() { return this.handler.inputNames; } get trainingOutputNames() { return this.handler.outputNames; } get evalInputNames() { if (this.hasEvalModel) { return this.handler.evalInputNames; } else { throw new Error('This training session has no evalModel loaded.'); } } get evalOutputNames() { if (this.hasEvalModel) { return this.handler.evalOutputNames; } else { throw new Error('This training session has no evalModel loaded.'); } } static async create(trainingOptions, sessionOptions) { const evalModel = trainingOptions.evalModel || ''; const optimizerModel = trainingOptions.optimizerModel || ''; const options = sessionOptions || {}; // get backend hints const eps = options.executionProviders || []; const backendHints = eps.map(i => typeof i === 'string' ? i : i.name); const backend = await (0,_backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackend)(backendHints); if (backend.createTrainingSessionHandler) { const handler = await backend.createTrainingSessionHandler(trainingOptions.checkpointState, trainingOptions.trainModel, evalModel, optimizerModel, options); return new TrainingSession(handler, !!trainingOptions.optimizerModel, !!trainingOptions.evalModel); } else { throw new Error(noBackendErrMsg); } } /** * Helper function for runTrainStep and future runStep methods that handles the type-narrowing conversion from * the given parameters to SessionHandler.FetchesType and RunOptions. * * @param inputNames the feeds object is checked that they contain all input names in the provided list of input * names. * @param outputNames the fetches object is checked that their keys match up with valid names in the list of output * names. * @param feeds the required input * @param arg1 narrowed & converted into the SessionHandler.FetchesType or RunOptions object * @param arg2 optional RunOptions object. * @returns */ typeNarrowingForRunStep(inputNames, outputNames, feeds, arg1, arg2) { const fetches = {}; let options = {}; // check inputs if (typeof feeds !== 'object' || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { throw new TypeError('\'feeds\' must be an object that use input names as keys and OnnxValue as corresponding values.'); } let isFetchesEmpty = true; // determine which override is being used if (typeof arg1 === 'object') { if (arg1 === null) { throw new TypeError('Unexpected argument[1]: cannot be null.'); } if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { throw new TypeError('\'fetches\' cannot be a Tensor'); } if (Array.isArray(arg1)) { if (arg1.length === 0) { throw new TypeError('\'fetches\' cannot be an empty array.'); } isFetchesEmpty = false; // output names for (const name of arg1) { if (typeof name !== 'string') { throw new TypeError('\'fetches\' must be a string array or an object.'); } if (outputNames.indexOf(name) === -1) { throw new RangeError(`'fetches' contains invalid output name: ${name}.`); } fetches[name] = null; } if (typeof arg2 === 'object' && arg2 !== null) { options = arg2; } else if (typeof arg2 !== 'undefined') { throw new TypeError('\'options\' must be an object.'); } } else { // decide whether arg1 is fetches or options // if any output name is present and its value is valid OnnxValue, we consider it fetches let isFetches = false; const arg1Keys = Object.getOwnPropertyNames(arg1); for (const name of outputNames) { if (arg1Keys.indexOf(name) !== -1) { const v = arg1[name]; if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { isFetches = true; isFetchesEmpty = false; fetches[name] = v; } } } if (isFetches) { if (typeof arg2 === 'object' && arg2 !== null) { options = arg2; } else if (typeof arg2 !== 'undefined') { throw new TypeError('\'options\' must be an object.'); } } else { options = arg1; } } } else if (typeof arg1 !== 'undefined') { throw new TypeError('Unexpected argument[1]: must be \'fetches\' or \'options\'.'); } // check if all inputs are in feed for (const name of inputNames) { if (typeof feeds[name] === 'undefined') { throw new Error(`input '${name}' is missing in 'feeds'.`); } } // if no fetches is specified, we use the full output names list if (isFetchesEmpty) { for (const name of outputNames) { fetches[name] = null; } } return [fetches, options]; } /** * Helper method for runTrainStep and any other runStep methods. Takes the ReturnType result from the SessionHandler * and changes it into a map of Tensors. * * @param results * @returns */ convertHandlerReturnTypeToMapOfTensors(results) { const returnValue = {}; for (const key in results) { if (Object.hasOwnProperty.call(results, key)) { const result = results[key]; if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { returnValue[key] = result; } else { returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); } } } return returnValue; } async lazyResetGrad() { await this.handler.lazyResetGrad(); } async runTrainStep(feeds, arg1, arg2) { const [fetches, options] = this.typeNarrowingForRunStep(this.trainingInputNames, this.trainingOutputNames, feeds, arg1, arg2); const results = await this.handler.runTrainStep(feeds, fetches, options); return this.convertHandlerReturnTypeToMapOfTensors(results); } async runOptimizerStep(options) { if (this.hasOptimizerModel) { await this.handler.runOptimizerStep(options || {}); } else { throw new Error('This TrainingSession has no OptimizerModel loaded.'); } } async runEvalStep(feeds, arg1, arg2) { if (this.hasEvalModel) { const [fetches, options] = this.typeNarrowingForRunStep(this.evalInputNames, this.evalOutputNames, feeds, arg1, arg2); const results = await this.handler.runEvalStep(feeds, fetches, options); return this.convertHandlerReturnTypeToMapOfTensors(results); } else { throw new Error('This TrainingSession has no EvalModel loaded.'); } } async getParametersSize(trainableOnly = true) { return this.handler.getParametersSize(trainableOnly); } async loadParametersBuffer(array, trainableOnly = true) { const paramsSize = await this.getParametersSize(trainableOnly); // checking that the size of the Uint8Array is equivalent to the byte length of a Float32Array of the number // of parameters if (array.length !== 4 * paramsSize) { throw new Error('Size of the buffer passed into loadParametersBuffer must match the number of parameters in ' + 'the model. Please use getParametersSize method to check.'); } return this.handler.loadParametersBuffer(array, trainableOnly); } async getContiguousParameters(trainableOnly = true) { return this.handler.getContiguousParameters(trainableOnly); } async release() { return this.handler.dispose(); } } //# sourceMappingURL=training-session-impl.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/training-session.js": /*!**********************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/training-session.js ***! \**********************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "TrainingSession": () => (/* binding */ TrainingSession) /* harmony export */ }); /* harmony import */ var _training_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./training-session-impl.js */ "./node_modules/onnxruntime-common/dist/esm/training-session-impl.js"); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // eslint-disable-next-line @typescript-eslint/naming-convention const TrainingSession = _training_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.TrainingSession; //# sourceMappingURL=training-session.js.map /***/ }), /***/ "./node_modules/onnxruntime-common/dist/esm/version.js": /*!*************************************************************!*\ !*** ./node_modules/onnxruntime-common/dist/esm/version.js ***! \*************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "version": () => (/* binding */ version) /* harmony export */ }); // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // This file is generated by /js/scripts/update-version.ts // Do not modify file content manually. const version = '1.17.0'; //# sourceMappingURL=version.js.map /***/ }), /***/ "./node_modules/onnxruntime-web/dist/esm/ort.webgpu.min.js": /*!*****************************************************************!*\ !*** ./node_modules/onnxruntime-web/dist/esm/ort.webgpu.min.js ***! \*****************************************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "InferenceSession": () => (/* binding */ md), /* harmony export */ "TRACE": () => (/* binding */ tn), /* harmony export */ "TRACE_FUNC_BEGIN": () => (/* binding */ st), /* harmony export */ "TRACE_FUNC_END": () => (/* binding */ nt), /* harmony export */ "Tensor": () => (/* binding */ Ze), /* harmony export */ "TrainingSession": () => (/* binding */ gd), /* harmony export */ "default": () => (/* binding */ Kv), /* harmony export */ "env": () => (/* binding */ Ae), /* harmony export */ "registerBackend": () => (/* binding */ Lt) /* harmony export */ }); /*! * ONNX Runtime Web v1.17.3 * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var Fn=Object.defineProperty;var ld=Object.getOwnPropertyDescriptor;var dd=Object.getOwnPropertyNames;var cd=Object.prototype.hasOwnProperty;var F=(e,t)=>()=>(e&&(t=e(e=0)),t);var tr=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),rr=(e,t)=>{for(var r in t)Fn(e,r,{get:t[r],enumerable:!0})},pd=(e,t,r,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of dd(t))!cd.call(e,n)&&n!==r&&Fn(e,n,{get:()=>t[n],enumerable:!(a=ld(t,n))||a.enumerable});return e};var Gt=e=>pd(Fn({},"__esModule",{value:!0}),e);var Zr,Ht,Lt,fd,Xr,Qr=F(()=>{"use strict";Zr=new Map,Ht=[],Lt=(e,t,r)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let a=Zr.get(e);if(a===void 0)Zr.set(e,{backend:t,priority:r});else{if(a.priority>r)return;if(a.priority===r&&a.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${r}`)}if(r>=0){let n=Ht.indexOf(e);n!==-1&&Ht.splice(n,1);for(let o=0;o{let t=Zr.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let r=!!t.initPromise;try{return r||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(a){return r||(t.error=`${a}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},Xr=async e=>{let t=e.executionProviders||[],r=t.map(i=>typeof i=="string"?i:i.name),a=r.length===0?Ht:r,n,o=[],s=new Set;for(let i of a){let c=await fd(i);typeof c=="string"?o.push({name:i,err:c}):(n||(n=c),n===c&&s.add(i))}if(!n)throw new Error(`no available backend found. ERR: ${o.map(i=>`[${i.name}] ${i.err}`).join(", ")}`);for(let{name:i,err:c}of o)r.includes(i)&&console.warn(`removing requested execution provider "${i}" from session options because it is not available: ${c}`);let l=t.filter(i=>s.has(typeof i=="string"?i:i.name));return[n,new Proxy(e,{get:(i,c)=>c==="executionProviders"?l:Reflect.get(i,c)})]}});var ii=F(()=>{"use strict";Qr()});var oi,si=F(()=>{"use strict";oi="1.17.3"});var ui,ot,qn=F(()=>{"use strict";si();ui="warning",ot={wasm:{},webgl:{},webgpu:{},versions:{common:oi},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);ui=e}},get logLevel(){return ui}};Object.defineProperty(ot,"logLevel",{enumerable:!0})});var Ae,li=F(()=>{"use strict";qn();Ae=ot});var di,ci,pi=F(()=>{"use strict";di=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);r.width=e.dims[3],r.height=e.dims[2];let a=r.getContext("2d");if(a!=null){let n,o;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(n=e.dims[2],o=e.dims[3]):(n=e.dims[3],o=e.dims[2]);let s=t?.format!==void 0?t.format:"RGB",l=t?.norm,i,c;l===void 0||l.mean===void 0?i=[255,255,255,255]:typeof l.mean=="number"?i=[l.mean,l.mean,l.mean,l.mean]:(i=[l.mean[0],l.mean[1],l.mean[2],0],l.mean[3]!==void 0&&(i[3]=l.mean[3])),l===void 0||l.bias===void 0?c=[0,0,0,0]:typeof l.bias=="number"?c=[l.bias,l.bias,l.bias,l.bias]:(c=[l.bias[0],l.bias[1],l.bias[2],0],l.bias[3]!==void 0&&(c[3]=l.bias[3]));let f=o*n,m=0,h=f,w=f*2,b=-1;s==="RGBA"?(m=0,h=f,w=f*2,b=f*3):s==="RGB"?(m=0,h=f,w=f*2):s==="RBG"&&(m=0,w=f,h=f*2);for(let $=0;${let r=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),a;if(r!=null){let n,o,s;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(n=e.dims[2],o=e.dims[1],s=e.dims[3]):(n=e.dims[3],o=e.dims[2],s=e.dims[1]);let l=t!==void 0&&t.format!==void 0?t.format:"RGB",i=t?.norm,c,f;i===void 0||i.mean===void 0?c=[255,255,255,255]:typeof i.mean=="number"?c=[i.mean,i.mean,i.mean,i.mean]:(c=[i.mean[0],i.mean[1],i.mean[2],255],i.mean[3]!==void 0&&(c[3]=i.mean[3])),i===void 0||i.bias===void 0?f=[0,0,0,0]:typeof i.bias=="number"?f=[i.bias,i.bias,i.bias,i.bias]:(f=[i.bias[0],i.bias[1],i.bias[2],0],i.bias[3]!==void 0&&(f[3]=i.bias[3]));let m=o*n;if(t!==void 0&&(t.format!==void 0&&s===4&&t.format!=="RGBA"||s===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let h=4,w=0,b=1,$=2,I=3,S=0,v=m,E=m*2,A=-1;l==="RGBA"?(S=0,v=m,E=m*2,A=m*3):l==="RGB"?(S=0,v=m,E=m*2):l==="RBG"&&(S=0,E=m,v=m*2),a=r.createImageData(n,o);for(let k=0;k{"use strict";Jr();jn=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:r,width:a}=t,n=t.norm??{mean:255,bias:0},o,s;typeof n.mean=="number"?o=[n.mean,n.mean,n.mean,n.mean]:o=[n.mean[0],n.mean[1],n.mean[2],n.mean[3]??255],typeof n.bias=="number"?s=[n.bias,n.bias,n.bias,n.bias]:s=[n.bias[0],n.bias[1],n.bias[2],n.bias[3]??0];let l=t.format!==void 0?t.format:"RGBA",i=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",c=r*a,f=i==="RGBA"?new Float32Array(c*4):new Float32Array(c*3),m=4,h=0,w=1,b=2,$=3,I=0,S=c,v=c*2,E=-1;l==="RGB"&&(m=3,h=0,w=1,b=2,$=-1),i==="RGBA"?E=c*3:i==="RBG"?(I=0,v=c,S=c*2):i==="BGR"&&(v=0,S=c,I=c*2);for(let k=0;k{let r=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,a=typeof ImageData<"u"&&e instanceof ImageData,n=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,o=typeof e=="string",s,l=t??{},i=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},c=f=>f instanceof HTMLCanvasElement||f instanceof OffscreenCanvas?f.getContext("2d"):null;if(r){let f=i();f.width=e.width,f.height=e.height;let m=c(f);if(m!=null){let h=e.height,w=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(h=t.resizedHeight,w=t.resizedWidth),t!==void 0){if(l=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");l.tensorFormat="RGBA",l.height=h,l.width=w}else l.tensorFormat="RGBA",l.height=h,l.width=w;m.drawImage(e,0,0),s=m.getImageData(0,0,w,h).data}else throw new Error("Can not access image data")}else if(a){let f,m;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(f=t.resizedHeight,m=t.resizedWidth):(f=e.height,m=e.width),t!==void 0&&(l=t),l.format="RGBA",l.height=f,l.width=m,t!==void 0){let h=i();h.width=m,h.height=f;let w=c(h);if(w!=null)w.putImageData(e,0,0),s=w.getImageData(0,0,m,f).data;else throw new Error("Can not access image data")}else s=e.data}else if(n){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let f=i();f.width=e.width,f.height=e.height;let m=c(f);if(m!=null){let h=e.height,w=e.width;return m.drawImage(e,0,0,w,h),s=m.getImageData(0,0,w,h).data,l.height=h,l.width=w,jn(s,l)}else throw new Error("Can not access image data")}else{if(o)return new Promise((f,m)=>{let h=i(),w=c(h);if(!e||!w)return m();let b=new Image;b.crossOrigin="Anonymous",b.src=e,b.onload=()=>{h.width=b.width,h.height=b.height,w.drawImage(b,0,0,h.width,h.height);let $=w.getImageData(0,0,h.width,h.height);l.height=h.height,l.width=h.width,f(jn($.data,l))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(s!==void 0)return jn(s,l);throw new Error("Input data provided is not supported - aborted tensor creation")},mi=(e,t)=>{let{width:r,height:a,download:n,dispose:o}=t,s=[1,a,r,4];return new rt({location:"texture",type:"float32",texture:e,dims:s,download:n,dispose:o})},hi=(e,t)=>{let{dataType:r,dims:a,download:n,dispose:o}=t;return new rt({location:"gpu-buffer",type:r??"float32",gpuBuffer:e,dims:a,download:n,dispose:o})},gi=(e,t,r)=>new rt({location:"cpu-pinned",type:e,data:t,dims:r??[t.length]})});var Ft,Sr,bi,wi,vi=F(()=>{"use strict";Ft=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array]]),Sr=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),bi=!1,wi=()=>{if(!bi){bi=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,r=typeof Float16Array<"u"&&Float16Array.from;e&&(Ft.set("int64",BigInt64Array),Sr.set(BigInt64Array,"int64")),t&&(Ft.set("uint64",BigUint64Array),Sr.set(BigUint64Array,"uint64")),r?(Ft.set("float16",Float16Array),Sr.set(Float16Array,"float16")):Ft.set("float16",Uint16Array)}}});var $i,_i,xi=F(()=>{"use strict";Jr();$i=e=>{let t=1;for(let r=0;r{switch(e.location){case"cpu":return new rt(e.type,e.data,t);case"cpu-pinned":return new rt({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new rt({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new rt({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}});var rt,Jr=F(()=>{"use strict";pi();yi();vi();xi();rt=class{constructor(t,r,a){wi();let n,o;if(typeof t=="object"&&"location"in t)switch(this.dataLocation=t.location,n=t.type,o=t.dims,t.location){case"cpu-pinned":{let l=Ft.get(n);if(!l)throw new TypeError(`unsupported type "${n}" to create tensor from pinned buffer`);if(!(t.data instanceof l))throw new TypeError(`buffer should be of type ${l.name}`);this.cpuData=t.data;break}case"texture":{if(n!=="float32")throw new TypeError(`unsupported type "${n}" to create tensor from texture`);this.gpuTextureData=t.texture,this.downloader=t.download,this.disposer=t.dispose;break}case"gpu-buffer":{if(n!=="float32"&&n!=="float16"&&n!=="int32"&&n!=="int64"&&n!=="uint32"&&n!=="uint8"&&n!=="bool")throw new TypeError(`unsupported type "${n}" to create tensor from gpu buffer`);this.gpuBufferData=t.gpuBuffer,this.downloader=t.download,this.disposer=t.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let l,i;if(typeof t=="string")if(n=t,i=a,t==="string"){if(!Array.isArray(r))throw new TypeError("A string tensor's data must be a string array.");l=r}else{let c=Ft.get(t);if(c===void 0)throw new TypeError(`Unsupported tensor type: ${t}.`);if(Array.isArray(r)){if(t==="float16"&&c===Uint16Array)throw new TypeError("Creating a float16 tensor from number array is not supported. Please use Uint16Array as data.");t==="uint64"||t==="int64"?l=c.from(r,BigInt):l=c.from(r)}else if(r instanceof c)l=r;else throw new TypeError(`A ${n} tensor's data must be type of ${c}`)}else if(i=r,Array.isArray(t)){if(t.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let c=typeof t[0];if(c==="string")n="string",l=t;else if(c==="boolean")n="bool",l=Uint8Array.from(t);else throw new TypeError(`Invalid element type of data array: ${c}.`)}else{let c=Sr.get(t.constructor);if(c===void 0)throw new TypeError(`Unsupported type for tensor data: ${t.constructor}.`);n=c,l=t}if(i===void 0)i=[l.length];else if(!Array.isArray(i))throw new TypeError("A tensor's dims must be a number array");o=i,this.cpuData=l,this.dataLocation="cpu"}let s=$i(o);if(this.cpuData&&s!==this.cpuData.length)throw new Error(`Tensor's size(${s}) does not match data length(${this.cpuData.length}).`);this.type=n,this.dims=o,this.size=s}static async fromImage(t,r){return fi(t,r)}static fromTexture(t,r){return mi(t,r)}static fromGpuBuffer(t,r){return hi(t,r)}static fromPinnedBuffer(t,r,a){return gi(t,r,a)}toDataURL(t){return di(this,t)}toImageData(t){return ci(this,t)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}async getData(t){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let r=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=r,t&&this.disposer&&(this.disposer(),this.disposer=void 0),r}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(t){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return _i(this,t)}}});var Ze,en=F(()=>{"use strict";Jr();Ze=rt});var tn,Si,st,nt,Kn=F(()=>{"use strict";qn();tn=(e,t)=>{(typeof ot.trace>"u"?!ot.wasm.trace:!ot.trace)||console.timeStamp(`${e}::ORT::${t}`)},Si=(e,t)=>{let r=new Error().stack?.split(/\r\n|\r|\n/g)||[],a=!1;for(let n=0;n{(typeof ot.trace>"u"?!ot.wasm.trace:!ot.trace)||Si("BEGIN",e)},nt=e=>{(typeof ot.trace>"u"?!ot.wasm.trace:!ot.trace)||Si("END",e)}});var rn,Ci=F(()=>{"use strict";Qr();en();Kn();rn=class e{constructor(t){this.handler=t}async run(t,r,a){st();let n={},o={};if(typeof t!="object"||t===null||t instanceof Ze||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let s=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof Ze)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(r)){if(r.length===0)throw new TypeError("'fetches' cannot be an empty array.");s=!1;for(let c of r){if(typeof c!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(c)===-1)throw new RangeError(`'fetches' contains invalid output name: ${c}.`);n[c]=null}if(typeof a=="object"&&a!==null)o=a;else if(typeof a<"u")throw new TypeError("'options' must be an object.")}else{let c=!1,f=Object.getOwnPropertyNames(r);for(let m of this.outputNames)if(f.indexOf(m)!==-1){let h=r[m];(h===null||h instanceof Ze)&&(c=!0,s=!1,n[m]=h)}if(c){if(typeof a=="object"&&a!==null)o=a;else if(typeof a<"u")throw new TypeError("'options' must be an object.")}else o=r}}else if(typeof r<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let c of this.inputNames)if(typeof t[c]>"u")throw new Error(`input '${c}' is missing in 'feeds'.`);if(s)for(let c of this.outputNames)n[c]=null;let l=await this.handler.run(t,n,o),i={};for(let c in l)if(Object.hasOwnProperty.call(l,c)){let f=l[c];f instanceof Ze?i[c]=f:i[c]=new Ze(f.type,f.data,f.dims)}return nt(),i}async release(){return this.handler.dispose()}static async create(t,r,a,n){st();let o,s={};if(typeof t=="string"){if(o=t,typeof r=="object"&&r!==null)s=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(o=t,typeof r=="object"&&r!==null)s=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let f=t,m=0,h=t.byteLength;if(typeof r=="object"&&r!==null)s=r;else if(typeof r=="number"){if(m=r,!Number.isSafeInteger(m))throw new RangeError("'byteOffset' must be an integer.");if(m<0||m>=f.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${f.byteLength}).`);if(h=t.byteLength-m,typeof a=="number"){if(h=a,!Number.isSafeInteger(h))throw new RangeError("'byteLength' must be an integer.");if(h<=0||m+h>f.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${f.byteLength-m}].`);if(typeof n=="object"&&n!==null)s=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(typeof a<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof r<"u")throw new TypeError("'options' must be an object.");o=new Uint8Array(f,m,h)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[l,i]=await Xr(s),c=await l.createInferenceSessionHandler(o,i);return nt(),new e(c)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}}});var md,Ii=F(()=>{"use strict";Ci();md=rn});var Ei=F(()=>{"use strict"});var Ai=F(()=>{"use strict"});var Ti=F(()=>{"use strict"});var Oi=F(()=>{"use strict"});var hd,nn,ki=F(()=>{"use strict";Qr();en();hd="Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files.",nn=class e{constructor(t,r,a){this.handler=t,this.hasOptimizerModel=r,this.hasEvalModel=a}get trainingInputNames(){return this.handler.inputNames}get trainingOutputNames(){return this.handler.outputNames}get evalInputNames(){if(this.hasEvalModel)return this.handler.evalInputNames;throw new Error("This training session has no evalModel loaded.")}get evalOutputNames(){if(this.hasEvalModel)return this.handler.evalOutputNames;throw new Error("This training session has no evalModel loaded.")}static async create(t,r){let a=t.evalModel||"",n=t.optimizerModel||"",o=r||{},[s,l]=await Xr(o);if(s.createTrainingSessionHandler){let i=await s.createTrainingSessionHandler(t.checkpointState,t.trainModel,a,n,l);return new e(i,!!t.optimizerModel,!!t.evalModel)}else throw new Error(hd)}typeNarrowingForRunStep(t,r,a,n,o){let s={},l={};if(typeof a!="object"||a===null||a instanceof Ze||Array.isArray(a))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let i=!0;if(typeof n=="object"){if(n===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(n instanceof Ze)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(n)){if(n.length===0)throw new TypeError("'fetches' cannot be an empty array.");i=!1;for(let c of n){if(typeof c!="string")throw new TypeError("'fetches' must be a string array or an object.");if(r.indexOf(c)===-1)throw new RangeError(`'fetches' contains invalid output name: ${c}.`);s[c]=null}if(typeof o=="object"&&o!==null)l=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else{let c=!1,f=Object.getOwnPropertyNames(n);for(let m of r)if(f.indexOf(m)!==-1){let h=n[m];(h===null||h instanceof Ze)&&(c=!0,i=!1,s[m]=h)}if(c){if(typeof o=="object"&&o!==null)l=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else l=n}}else if(typeof n<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let c of t)if(typeof a[c]>"u")throw new Error(`input '${c}' is missing in 'feeds'.`);if(i)for(let c of r)s[c]=null;return[s,l]}convertHandlerReturnTypeToMapOfTensors(t){let r={};for(let a in t)if(Object.hasOwnProperty.call(t,a)){let n=t[a];n instanceof Ze?r[a]=n:r[a]=new Ze(n.type,n.data,n.dims)}return r}async lazyResetGrad(){await this.handler.lazyResetGrad()}async runTrainStep(t,r,a){let[n,o]=this.typeNarrowingForRunStep(this.trainingInputNames,this.trainingOutputNames,t,r,a),s=await this.handler.runTrainStep(t,n,o);return this.convertHandlerReturnTypeToMapOfTensors(s)}async runOptimizerStep(t){if(this.hasOptimizerModel)await this.handler.runOptimizerStep(t||{});else throw new Error("This TrainingSession has no OptimizerModel loaded.")}async runEvalStep(t,r,a){if(this.hasEvalModel){let[n,o]=this.typeNarrowingForRunStep(this.evalInputNames,this.evalOutputNames,t,r,a),s=await this.handler.runEvalStep(t,n,o);return this.convertHandlerReturnTypeToMapOfTensors(s)}else throw new Error("This TrainingSession has no EvalModel loaded.")}async getParametersSize(t=!0){return this.handler.getParametersSize(t)}async loadParametersBuffer(t,r=!0){let a=await this.getParametersSize(r);if(t.length!==4*a)throw new Error("Size of the buffer passed into loadParametersBuffer must match the number of parameters in the model. Please use getParametersSize method to check.");return this.handler.loadParametersBuffer(t,r)}async getContiguousParameters(t=!0){return this.handler.getContiguousParameters(t)}async release(){return this.handler.dispose()}}});var gd,Pi=F(()=>{"use strict";ki();gd=nn});var Yn={};rr(Yn,{InferenceSession:()=>md,TRACE:()=>tn,TRACE_FUNC_BEGIN:()=>st,TRACE_FUNC_END:()=>nt,Tensor:()=>Ze,TrainingSession:()=>gd,env:()=>Ae,registerBackend:()=>Lt});var lt=F(()=>{"use strict";ii();li();Ii();en();Ei();Ai();Kn();Ti();Oi();Pi()});var Zn={};rr(Zn,{createReadStream:()=>zi,readFile:()=>yd,readFileSync:()=>bd});var yd,bd,zi,Xn=F(()=>{yd=void 0,bd=void 0,zi=void 0});var Qn={};rr(Qn,{join:()=>wd});var wd,Jn=F(()=>{wd=void 0});var Mi=tr((Di,ea)=>{"use strict";var Bi=(()=>{var e=typeof document<"u"&&document.currentScript?document.currentScript.src:void 0;return typeof __filename<"u"&&(e=e||__filename),function(t={}){var r=t,a,n;r.ready=new Promise((d,g)=>{a=d,n=g}),r.mountExternalData=(d,g)=>{(r.Fa||(r.Fa=new Map)).set(d,g)},r.unmountExternalData=()=>{delete r.Fa};let o=()=>{let d=(_,C,O)=>(...W)=>{let j=ut,Q=C?.();W=_(...W);let oe=C?.();return Q!==oe&&(_=oe,O(Q),C=O=null),ut!=j?Nr():W},g=_=>async(...C)=>{try{if(r.Ea)throw Error("Session already started");let O=r.Ea={Za:C[0],errors:[]},W=await _(...C);if(r.Ea!==O)throw Error("Session mismatch");r.La?.flush();let j=O.errors;if(0oe),0r._OrtCreateSession,_=>r._OrtCreateSession=_),r._OrtRun=g(d(r._OrtRun,()=>r._OrtRun,_=>r._OrtRun=_)),r._OrtRunWithBinding=g(d(r._OrtRunWithBinding,()=>r._OrtRunWithBinding,_=>r._OrtRunWithBinding=_)),r._OrtBindInput=d(r._OrtBindInput,()=>r._OrtBindInput,_=>r._OrtBindInput=_),o=void 0};r.jsepInit=(d,g)=>{if(o?.(),d==="webgpu"){[r.La,r.Ra,r.Va,r.Ma,r.Ua,r.sa,r.Wa,r.Ya,r.Sa,r.Ta,r.Xa]=g;let _=r.La;r.jsepRegisterBuffer=(C,O,W,j)=>_.registerBuffer(C,O,W,j),r.jsepGetBuffer=C=>_.getBuffer(C),r.jsepCreateDownloader=(C,O,W)=>_.createDownloader(C,O,W),r.jsepOnReleaseSession=C=>{_.onReleaseSession(C)},r.jsepOnRunStart=C=>_.onRunStart(C)}};var s=Object.assign({},r),l="./this.program",i=(d,g)=>{throw g},c=typeof window=="object",f=typeof importScripts=="function",m=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string",h="",w,b,$;if(m){var I=(Xn(),Gt(Zn)),S=(Jn(),Gt(Qn));h=f?S.dirname(h)+"/":__dirname+"/",w=(d,g)=>(d=Ee(d)?new URL(d):S.normalize(d),I.readFileSync(d,g?void 0:"utf8")),$=d=>(d=w(d,!0),d.buffer||(d=new Uint8Array(d)),d),b=(d,g,_,C=!0)=>{d=Ee(d)?new URL(d):S.normalize(d),I.readFile(d,C?void 0:"utf8",(O,W)=>{O?_(O):g(C?W.buffer:W)})},!r.thisProgram&&1{throw process.exitCode=d,g},r.inspect=()=>"[Emscripten Module object]"}else(c||f)&&(f?h=self.location.href:typeof document<"u"&&document.currentScript&&(h=document.currentScript.src),e&&(h=e),h.indexOf("blob:")!==0?h=h.substr(0,h.replace(/[?#].*/,"").lastIndexOf("/")+1):h="",w=d=>{var g=new XMLHttpRequest;return g.open("GET",d,!1),g.send(null),g.responseText},f&&($=d=>{var g=new XMLHttpRequest;return g.open("GET",d,!1),g.responseType="arraybuffer",g.send(null),new Uint8Array(g.response)}),b=(d,g,_)=>{var C=new XMLHttpRequest;C.open("GET",d,!0),C.responseType="arraybuffer",C.onload=()=>{C.status==200||C.status==0&&C.response?g(C.response):_()},C.onerror=_,C.send(null)});var v=console.log.bind(console),E=console.error.bind(console);Object.assign(r,s),s=null,typeof WebAssembly!="object"&&se("no native wasm support detected");var A,k=!1,U,N,H,z,L,pe,Ie;function we(){var d=A.buffer;r.HEAP8=N=new Int8Array(d),r.HEAP16=new Int16Array(d),r.HEAPU8=H=new Uint8Array(d),r.HEAPU16=new Uint16Array(d),r.HEAP32=z=new Int32Array(d),r.HEAPU32=L=new Uint32Array(d),r.HEAPF32=pe=new Float32Array(d),r.HEAPF64=Ie=new Float64Array(d)}var ne=[],Ue=[],X=[],xe=0,fe=null,ue=null;function se(d){throw d="Aborted("+d+")",E(d),k=!0,U=1,d=new WebAssembly.RuntimeError(d+". Build with -sASSERTIONS for more info."),n(d),d}var he=d=>d.startsWith("data:application/octet-stream;base64,"),Ee=d=>d.startsWith("file://"),Le;if(Le="ort-wasm-simd.wasm",!he(Le)){var G=Le;Le=r.locateFile?r.locateFile(G,h):h+G}function J(d){if($)return $(d);throw"both async and sync fetching of the wasm failed"}function Se(d){if(c||f){if(typeof fetch=="function"&&!Ee(d))return fetch(d,{credentials:"same-origin"}).then(g=>{if(!g.ok)throw"failed to load wasm binary file at '"+d+"'";return g.arrayBuffer()}).catch(()=>J(d));if(b)return new Promise((g,_)=>{b(d,C=>g(new Uint8Array(C)),_)})}return Promise.resolve().then(()=>J(d))}function Je(d,g,_){return Se(d).then(C=>WebAssembly.instantiate(C,g)).then(C=>C).then(_,C=>{E(`failed to asynchronously prepare wasm: ${C}`),se(C)})}function et(d,g){var _=Le;return typeof WebAssembly.instantiateStreaming!="function"||he(_)||Ee(_)||m||typeof fetch!="function"?Je(_,d,g):fetch(_,{credentials:"same-origin"}).then(C=>WebAssembly.instantiateStreaming(C,d).then(g,function(O){return E(`wasm streaming compile failed: ${O}`),E("falling back to ArrayBuffer instantiation"),Je(_,d,g)}))}var Fe,It={931760:(d,g,_,C)=>{if(typeof r>"u"||!r.Fa)return 1;if(d=je(d>>>0),d.startsWith("./")&&(d=d.substring(2)),d=r.Fa.get(d),!d)return 2;if(g>>>=0,_>>>=0,g+_>d.byteLength)return 3;try{return H.set(d.subarray(g,g+_),C>>>0>>>0),0}catch{return 4}},932261:()=>{r.Sa()},932292:()=>{r.Ta()},932321:()=>{r.Xa()},932346:d=>r.Ra(d),932379:d=>r.Va(d),932411:(d,g,_)=>{r.Ma(d,g,_,!0)},932450:(d,g,_)=>{r.Ma(d,g,_)},932483:d=>{r.sa("Abs",d,void 0)},932534:d=>{r.sa("Neg",d,void 0)},932585:d=>{r.sa("Floor",d,void 0)},932638:d=>{r.sa("Ceil",d,void 0)},932690:d=>{r.sa("Reciprocal",d,void 0)},932748:d=>{r.sa("Sqrt",d,void 0)},932800:d=>{r.sa("Exp",d,void 0)},932851:d=>{r.sa("Erf",d,void 0)},932902:d=>{r.sa("Sigmoid",d,void 0)},932957:(d,g,_)=>{r.sa("HardSigmoid",d,{alpha:g,beta:_})},933036:d=>{r.sa("Log",d,void 0)},933087:d=>{r.sa("Sin",d,void 0)},933138:d=>{r.sa("Cos",d,void 0)},933189:d=>{r.sa("Tan",d,void 0)},933240:d=>{r.sa("Asin",d,void 0)},933292:d=>{r.sa("Acos",d,void 0)},933344:d=>{r.sa("Atan",d,void 0)},933396:d=>{r.sa("Sinh",d,void 0)},933448:d=>{r.sa("Cosh",d,void 0)},933500:d=>{r.sa("Asinh",d,void 0)},933553:d=>{r.sa("Acosh",d,void 0)},933606:d=>{r.sa("Atanh",d,void 0)},933659:d=>{r.sa("Tanh",d,void 0)},933711:d=>{r.sa("Not",d,void 0)},933762:(d,g,_)=>{r.sa("Clip",d,{min:g,max:_})},933831:d=>{r.sa("Clip",d,void 0)},933883:(d,g)=>{r.sa("Elu",d,{alpha:g})},933941:d=>{r.sa("Relu",d,void 0)},933993:(d,g)=>{r.sa("LeakyRelu",d,{alpha:g})},934057:(d,g)=>{r.sa("ThresholdedRelu",d,{alpha:g})},934127:(d,g)=>{r.sa("Cast",d,{to:g})},934185:d=>{r.sa("Add",d,void 0)},934236:d=>{r.sa("Sub",d,void 0)},934287:d=>{r.sa("Mul",d,void 0)},934338:d=>{r.sa("Div",d,void 0)},934389:d=>{r.sa("Pow",d,void 0)},934440:d=>{r.sa("Equal",d,void 0)},934493:d=>{r.sa("Greater",d,void 0)},934548:d=>{r.sa("GreaterOrEqual",d,void 0)},934610:d=>{r.sa("Less",d,void 0)},934662:d=>{r.sa("LessOrEqual",d,void 0)},934721:(d,g,_,C,O)=>{r.sa("ReduceMean",d,{keepDims:!!g,noopWithEmptyAxes:!!_,axes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},934880:(d,g,_,C,O)=>{r.sa("ReduceMax",d,{keepDims:!!g,noopWithEmptyAxes:!!_,axes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},935038:(d,g,_,C,O)=>{r.sa("ReduceMin",d,{keepDims:!!g,noopWithEmptyAxes:!!_,axes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},935196:(d,g,_,C,O)=>{r.sa("ReduceProd",d,{keepDims:!!g,noopWithEmptyAxes:!!_,axes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},935355:(d,g,_,C,O)=>{r.sa("ReduceSum",d,{keepDims:!!g,noopWithEmptyAxes:!!_,axes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},935513:(d,g,_,C,O)=>{r.sa("ReduceL1",d,{keepDims:!!g,noopWithEmptyAxes:!!_,axes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},935670:(d,g,_,C,O)=>{r.sa("ReduceL2",d,{keepDims:!!g,noopWithEmptyAxes:!!_,axes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},935827:(d,g,_,C,O)=>{r.sa("ReduceLogSum",d,{keepDims:!!g,noopWithEmptyAxes:!!_,axes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},935988:(d,g,_,C,O)=>{r.sa("ReduceSumSquare",d,{keepDims:!!g,noopWithEmptyAxes:!!_,axes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},936152:(d,g,_,C,O)=>{r.sa("ReduceLogSumExp",d,{keepDims:!!g,noopWithEmptyAxes:!!_,axes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},936316:d=>{r.sa("Where",d,void 0)},936369:(d,g,_)=>{r.sa("Transpose",d,{perm:g?Array.from(z.subarray(g>>>0,_>>>0)):[]})},936477:(d,g,_,C,O,W,j,Q,oe,le,me,Pe,ze,P,ae)=>{r.sa("ConvTranspose",d,{format:oe?"NHWC":"NCHW",autoPad:g,dilations:[_],group:C,kernel_shape:[O],pads:[W,j],strides:[Q],wIsConst:()=>!!N[le>>>0],outputPadding:me?Array.from(z.subarray(me>>>0,Pe>>>0)):[],outputShape:ze?Array.from(z.subarray(ze>>>0,P>>>0)):[],activation:je(ae)})},936879:(d,g,_,C,O,W,j,Q,oe,le,me,Pe,ze,P)=>{r.sa("ConvTranspose",d,{format:Q?"NHWC":"NCHW",autoPad:g,dilations:Array.from(z.subarray(_>>>0,(_>>>0)+2>>>0)),group:C,kernelShape:Array.from(z.subarray(O>>>0,(O>>>0)+2>>>0)),pads:Array.from(z.subarray(W>>>0,(W>>>0)+4>>>0)),strides:Array.from(z.subarray(j>>>0,(j>>>0)+2>>>0)),wIsConst:()=>!!N[oe>>>0],outputPadding:le?Array.from(z.subarray(le>>>0,me>>>0)):[],outputShape:Pe?Array.from(z.subarray(Pe>>>0,ze>>>0)):[],activation:je(P)})},937444:(d,g,_,C,O,W,j,Q,oe,le,me,Pe,ze,P,ae)=>{r.sa("ConvTranspose",d,{format:oe?"NHWC":"NCHW",autoPad:g,dilations:[_],group:C,kernel_shape:[O],pads:[W,j],strides:[Q],wIsConst:()=>!!N[le>>>0],outputPadding:me?Array.from(z.subarray(me>>>0,Pe>>>0)):[],outputShape:ze?Array.from(z.subarray(ze>>>0,P>>>0)):[],activation:je(ae)})},937846:(d,g,_,C,O,W,j,Q,oe,le,me,Pe,ze,P)=>{r.sa("ConvTranspose",d,{format:Q?"NHWC":"NCHW",autoPad:g,dilations:Array.from(z.subarray(_>>>0,(_>>>0)+2>>>0)),group:C,kernelShape:Array.from(z.subarray(O>>>0,(O>>>0)+2>>>0)),pads:Array.from(z.subarray(W>>>0,(W>>>0)+4>>>0)),strides:Array.from(z.subarray(j>>>0,(j>>>0)+2>>>0)),wIsConst:()=>!!N[oe>>>0],outputPadding:le?Array.from(z.subarray(le>>>0,me>>>0)):[],outputShape:Pe?Array.from(z.subarray(Pe>>>0,ze>>>0)):[],activation:je(P)})},938411:(d,g)=>{r.sa("GlobalAveragePool",d,{format:g?"NHWC":"NCHW"})},938502:(d,g,_,C,O,W,j,Q,oe,le,me,Pe,ze,P,ae,Ce)=>{r.sa("AveragePool",d,{format:Ce?"NHWC":"NCHW",auto_pad:g,ceil_mode:_,count_include_pad:C,storage_order:O,dilations:[W,j],kernel_shape:[Q,oe],pads:[le,me,Pe,ze],strides:[P,ae]})},938786:(d,g)=>{r.sa("GlobalAveragePool",d,{format:g?"NHWC":"NCHW"})},938877:(d,g,_,C,O,W,j,Q,oe,le,me,Pe,ze,P,ae,Ce)=>{r.sa("AveragePool",d,{format:Ce?"NHWC":"NCHW",auto_pad:g,ceil_mode:_,count_include_pad:C,storage_order:O,dilations:[W,j],kernel_shape:[Q,oe],pads:[le,me,Pe,ze],strides:[P,ae]})},939161:(d,g)=>{r.sa("GlobalMaxPool",d,{format:g?"NHWC":"NCHW"})},939248:(d,g,_,C,O,W,j,Q,oe,le,me,Pe,ze,P,ae,Ce)=>{r.sa("MaxPool",d,{format:Ce?"NHWC":"NCHW",auto_pad:g,ceil_mode:_,count_include_pad:C,storage_order:O,dilations:[W,j],kernel_shape:[Q,oe],pads:[le,me,Pe,ze],strides:[P,ae]})},939528:(d,g)=>{r.sa("GlobalMaxPool",d,{format:g?"NHWC":"NCHW"})},939615:(d,g,_,C,O,W,j,Q,oe,le,me,Pe,ze,P,ae,Ce)=>{r.sa("MaxPool",d,{format:Ce?"NHWC":"NCHW",auto_pad:g,ceil_mode:_,count_include_pad:C,storage_order:O,dilations:[W,j],kernel_shape:[Q,oe],pads:[le,me,Pe,ze],strides:[P,ae]})},939895:(d,g,_,C,O)=>{r.sa("Gemm",d,{alpha:g,beta:_,transA:C,transB:O})},939999:d=>{r.sa("MatMul",d,void 0)},940053:(d,g,_,C)=>{r.sa("ArgMax",d,{keepDims:!!g,selectLastIndex:!!_,axis:C})},940161:(d,g,_,C)=>{r.sa("ArgMin",d,{keepDims:!!g,selectLastIndex:!!_,axis:C})},940269:(d,g)=>{r.sa("Softmax",d,{axis:g})},940332:(d,g)=>{r.sa("Concat",d,{axis:g})},940392:(d,g,_,C,O)=>{r.sa("Split",d,{axis:g,numOutputs:_,splitSizes:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},940532:d=>{r.sa("Expand",d,void 0)},940586:(d,g)=>{r.sa("Gather",d,{axis:Number(g)})},940657:(d,g)=>{r.sa("GatherElements",d,{axis:Number(g)})},940736:(d,g,_,C,O,W,j,Q,oe,le,me)=>{r.sa("Resize",d,{antialias:g,axes:_?Array.from(z.subarray(_>>>0,C>>>0)):[],coordinateTransformMode:je(O),cubicCoeffA:W,excludeOutside:j,extrapolationValue:Q,keepAspectRatioPolicy:je(oe),mode:je(le),nearestMode:je(me)})},941082:(d,g,_,C,O,W,j)=>{r.sa("Slice",d,{starts:g?Array.from(z.subarray(g>>>0,_>>>0)):[],ends:C?Array.from(z.subarray(C>>>0,O>>>0)):[],axes:W?Array.from(z.subarray(W>>>0,j>>>0)):[]})},941298:d=>{r.sa("Tile",d,void 0)},941350:(d,g,_)=>{r.sa("LayerNormalization",d,{axis:Number(g),epsilon:Number(_)})},941457:(d,g,_)=>{r.sa("InstanceNormalization",d,{epsilon:g,format:_?"NHWC":"NCHW"})},941571:(d,g,_)=>{r.sa("InstanceNormalization",d,{epsilon:g,format:_?"NHWC":"NCHW"})},941685:d=>{r.sa("Range",d,void 0)},941738:(d,g)=>{r.sa("Einsum",d,{equation:je(g)})},941819:(d,g,_,C,O)=>{r.sa("Pad",d,{mode:g,value:_,pads:C?Array.from(z.subarray(C>>>0,O>>>0)):[]})},941946:(d,g,_,C,O,W)=>{r.sa("BatchNormalization",d,{epsilon:g,momentum:_,spatial:!!O,trainingMode:!!C,format:W?"NHWC":"NCHW"})},942115:(d,g,_,C,O,W)=>{r.sa("BatchNormalization",d,{epsilon:g,momentum:_,spatial:!!O,trainingMode:!!C,format:W?"NHWC":"NCHW"})},942284:(d,g,_)=>{r.sa("CumSum",d,{exclusive:Number(g),reverse:Number(_)})},942381:(d,g,_,C,O,W,j,Q,oe)=>{r.sa("Attention",d,{numHeads:g,isUnidirectional:_,maskFilterValue:C,scale:O,doRotary:W,qkvHiddenSizes:j?Array.from(z.subarray(Number(Q)>>>0,Number(Q)+j>>>0)):[],pastPresentShareBuffer:!!oe})},942653:d=>{r.sa("BiasAdd",d,void 0)},942708:d=>{r.sa("BiasSplitGelu",d,void 0)},942769:d=>{r.sa("FastGelu",d,void 0)},942825:(d,g,_,C,O,W,j,Q,oe,le,me,Pe,ze)=>{r.sa("Conv",d,{format:oe?"NHWC":"NCHW",auto_pad:g,dilations:[_],group:C,kernel_shape:[O],pads:W?Array.from(z.subarray(W>>>0,j>>>0)):[],strides:[Q],w_is_const:()=>!!N[le>>>0],activation:je(me),activation_params:Pe?Array.from(pe.subarray(Pe>>>0,ze>>>0)):[]})},943195:(d,g,_,C,O,W,j,Q,oe,le,me,Pe,ze,P,ae,Ce)=>{r.sa("Conv",d,{format:Pe?"NHWC":"NCHW",auto_pad:g,dilations:[_,C],group:O,kernel_shape:[W,j],pads:Q?Array.from(z.subarray(Q>>>0,oe>>>0)):[],strides:[le,me],w_is_const:()=>!!N[ze>>>0],activation:je(P),activation_params:ae?Array.from(pe.subarray(ae>>>0,Ce>>>0)):[]})},943586:d=>{r.sa("Gelu",d,void 0)},943638:(d,g,_,C,O,W)=>{r.sa("MatMulNBits",d,{k:g,n:_,accuracyLevel:C,bits:O,blockSize:W})},943765:(d,g,_,C,O,W)=>{r.sa("MultiHeadAttention",d,{numHeads:g,isUnidirectional:_,maskFilterValue:C,scale:O,doRotary:W})},943924:(d,g)=>{r.sa("SkipLayerNormalization",d,{epsilon:g})},944005:d=>{r.Wa(d)},944039:(d,g)=>r.Ya(d,g,r.Ea.Za,r.Ea.errors)};function Ve(d){this.name="ExitStatus",this.message=`Program terminated with exit(${d})`,this.status=d}function qe(d){this.Ja=d-24,this.Pa=function(g){L[this.Ja+4>>>2>>>0]=g},this.Oa=function(g){L[this.Ja+8>>>2>>>0]=g},this.eb=function(g,_){this.Na(),this.Pa(g),this.Oa(_)},this.Na=function(){L[this.Ja+16>>>2>>>0]=0}}var tt=0,Ut=0,Et=typeof TextDecoder<"u"?new TextDecoder("utf8"):void 0,Ot=(d,g,_)=>{g>>>=0;var C=g+_;for(_=g;d[_]&&!(_>=C);)++_;if(16<_-g&&d.buffer&&Et)return Et.decode(d.subarray(g,_));for(C="";g<_;){var O=d[g++];if(O&128){var W=d[g++]&63;if((O&224)==192)C+=String.fromCharCode((O&31)<<6|W);else{var j=d[g++]&63;O=(O&240)==224?(O&15)<<12|W<<6|j:(O&7)<<18|W<<12|j<<6|d[g++]&63,65536>O?C+=String.fromCharCode(O):(O-=65536,C+=String.fromCharCode(55296|O>>10,56320|O&1023))}}else C+=String.fromCharCode(O)}return C},je=(d,g)=>(d>>>=0)?Ot(H,d,g):"",ur=d=>{for(var g=0,_=0;_=C?g++:2047>=C?g+=2:55296<=C&&57343>=C?(g+=4,++_):g+=3}return g},Yt=(d,g,_,C)=>{if(_>>>=0,!(0=j){var Q=d.charCodeAt(++W);j=65536+((j&1023)<<10)|Q&1023}if(127>=j){if(_>=C)break;g[_++>>>0]=j}else{if(2047>=j){if(_+1>=C)break;g[_++>>>0]=192|j>>6}else{if(65535>=j){if(_+2>=C)break;g[_++>>>0]=224|j>>12}else{if(_+3>=C)break;g[_++>>>0]=240|j>>18,g[_++>>>0]=128|j>>12&63}g[_++>>>0]=128|j>>6&63}g[_++>>>0]=128|j&63}}return g[_>>>0]=0,_-O},kt=d=>d%4===0&&(d%100!==0||d%400===0),At=[0,31,60,91,121,152,182,213,244,274,305,335],Zt=[0,31,59,90,120,151,181,212,243,273,304,334],Xt=d=>{var g=ur(d)+1,_=er(g);return _&&Yt(d,H,_,g),_},Nt=[],lr=(d,g)=>{Nt.length=0;for(var _;_=H[d++>>>0];){var C=_!=105;C&=_!=112,g+=C&&g%8?4:0,Nt.push(_==112?L[g>>>2>>>0]:_==105?z[g>>>2>>>0]:Ie[g>>>3>>>0]),g+=C?8:4}return Nt},pt={},dr=()=>{if(!Qt){var d={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:l||"./this.program"},g;for(g in pt)pt[g]===void 0?delete d[g]:d[g]=pt[g];var _=[];for(g in d)_.push(`${g}=${d[g]}`);Qt=_}return Qt},Qt,Ke=[null,[],[]],zr=[31,29,31,30,31,30,31,31,30,31,30,31],Re=[31,28,31,30,31,30,31,31,30,31,30,31];function Br(d){var g=Array(ur(d)+1);return Yt(d,g,0,g.length),g}function Jt(d,g,_,C){function O(P,ae,Ce){for(P=typeof P=="number"?P.toString():P||"";P.lengthLr?-1:0bt-P.getDate())ae-=bt-P.getDate()+1,P.setDate(1),11>Ce?P.setMonth(Ce+1):(P.setMonth(0),P.setFullYear(P.getFullYear()+1));else{P.setDate(P.getDate()+ae);break}}return Ce=new Date(P.getFullYear()+1,0,4),ae=Q(new Date(P.getFullYear(),0,4)),Ce=Q(Ce),0>=j(ae,P)?0>=j(Ce,P)?P.getFullYear()+1:P.getFullYear():P.getFullYear()-1}d>>>=0,g>>>=0,_>>>=0,C>>>=0;var le=L[C+40>>>2>>>0];C={bb:z[C>>>2>>>0],ab:z[C+4>>>2>>>0],Ga:z[C+8>>>2>>>0],Ka:z[C+12>>>2>>>0],Ha:z[C+16>>>2>>>0],Da:z[C+20>>>2>>>0],xa:z[C+24>>>2>>>0],Ca:z[C+28>>>2>>>0],fb:z[C+32>>>2>>>0],$a:z[C+36>>>2>>>0],cb:le?je(le):""},_=je(_),le={"%c":"%a %b %d %H:%M:%S %Y","%D":"%m/%d/%y","%F":"%Y-%m-%d","%h":"%b","%r":"%I:%M:%S %p","%R":"%H:%M","%T":"%H:%M:%S","%x":"%m/%d/%y","%X":"%H:%M:%S","%Ec":"%c","%EC":"%C","%Ex":"%m/%d/%y","%EX":"%H:%M:%S","%Ey":"%y","%EY":"%Y","%Od":"%d","%Oe":"%e","%OH":"%H","%OI":"%I","%Om":"%m","%OM":"%M","%OS":"%S","%Ou":"%u","%OU":"%U","%OV":"%V","%Ow":"%w","%OW":"%W","%Oy":"%y"};for(var me in le)_=_.replace(new RegExp(me,"g"),le[me]);var Pe="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),ze="January February March April May June July August September October November December".split(" ");le={"%a":P=>Pe[P.xa].substring(0,3),"%A":P=>Pe[P.xa],"%b":P=>ze[P.Ha].substring(0,3),"%B":P=>ze[P.Ha],"%C":P=>W((P.Da+1900)/100|0,2),"%d":P=>W(P.Ka,2),"%e":P=>O(P.Ka,2," "),"%g":P=>oe(P).toString().substring(2),"%G":P=>oe(P),"%H":P=>W(P.Ga,2),"%I":P=>(P=P.Ga,P==0?P=12:12{for(var ae=0,Ce=0;Ce<=P.Ha-1;ae+=(kt(P.Da+1900)?zr:Re)[Ce++]);return W(P.Ka+ae,3)},"%m":P=>W(P.Ha+1,2),"%M":P=>W(P.ab,2),"%n":()=>` `,"%p":P=>0<=P.Ga&&12>P.Ga?"AM":"PM","%S":P=>W(P.bb,2),"%t":()=>" ","%u":P=>P.xa||7,"%U":P=>W(Math.floor((P.Ca+7-P.xa)/7),2),"%V":P=>{var ae=Math.floor((P.Ca+7-(P.xa+6)%7)/7);if(2>=(P.xa+371-P.Ca-2)%7&&ae++,ae)ae==53&&(Ce=(P.xa+371-P.Ca)%7,Ce==4||Ce==3&&kt(P.Da)||(ae=1));else{ae=52;var Ce=(P.xa+7-P.Ca-1)%7;(Ce==4||Ce==5&&kt(P.Da%400-1))&&ae++}return W(ae,2)},"%w":P=>P.xa,"%W":P=>W(Math.floor((P.Ca+7-(P.xa+6)%7)/7),2),"%y":P=>(P.Da+1900).toString().substring(2),"%Y":P=>P.Da+1900,"%z":P=>{P=P.$a;var ae=0<=P;return P=Math.abs(P)/60,(ae?"+":"-")+("0000"+(P/60*100+P%60)).slice(-4)},"%Z":P=>P.cb,"%%":()=>"%"},_=_.replace(/%%/g,"\0\0");for(me in le)_.includes(me)&&(_=_.replace(new RegExp(me,"g"),le[me](C)));return _=_.replace(/\0\0/g,"%"),me=Br(_),me.length>g?0:(N.set(me,d>>>0),me.length-1)}var Pt=d=>{try{d()}catch(g){se(g)}};function Un(){var d=ee,g={};for(let[_,C]of Object.entries(d))g[_]=typeof C=="function"?function(){Tt.push(_);try{return C.apply(null,arguments)}finally{k||(Tt.pop(),ut&&ft===1&&Tt.length===0&&(ft=0,Pt($r),typeof Fibers<"u"&&Fibers.gb()))}}:C;return g}var ft=0,ut=null,te=0,Tt=[],cr={},Dr={},Mr=0,pr=null,Ur=[];function Nr(){return new Promise((d,g)=>{pr={resolve:d,reject:g}})}function Wr(){var d=er(65548),g=d+12;L[d>>>2>>>0]=g,L[d+4>>>2>>>0]=g+65536,g=Tt[0];var _=cr[g];return _===void 0&&(_=Mr++,cr[g]=_,Dr[_]=g),z[d+8>>>2>>>0]=_,d}function Vr(d){if(!k){if(ft===0){var g=!1,_=!1;d((C=0)=>{if(!k&&(te=C,g=!0,_)){ft=2,Pt(()=>_r(ut)),typeof Browser<"u"&&Browser.Ia.Qa&&Browser.Ia.resume(),C=!1;try{var O=(0,ee[Dr[z[ut+8>>>2>>>0]]])()}catch(Q){O=Q,C=!0}var W=!1;if(!ut){var j=pr;j&&(pr=null,(C?j.reject:j.resolve)(O),W=!0)}if(C&&!W)throw O}}),_=!0,g||(ft=1,ut=Wr(),typeof Browser<"u"&&Browser.Ia.Qa&&Browser.Ia.pause(),Pt(()=>vr(ut)))}else ft===2?(ft=0,Pt(xr),hr(ut),ut=null,Ur.forEach(C=>{if(!k)try{C();try{U=U=C=U,r.onExit?.(C),k=!0,i(C,new Ve(C))}catch(O){O instanceof Ve||O=="unwind"||i(1,O)}}catch(O){O instanceof Ve||O=="unwind"||i(1,O)}})):se(`invalid state: ${ft}`);return te}}function fr(d){return Vr(g=>{d().then(g)})}var Gr={n:function(d,g,_){return fr(async()=>{await r.Ua(d,g,_)})},a:function(d,g,_){throw d>>>=0,new qe(d).eb(g>>>0,_>>>0),tt=d,Ut++,tt},g:function(){return 0},J:function(){},A:function(){},C:function(){},L:function(){return 0},H:function(){},D:function(){},G:function(){},l:function(){},B:function(){},y:function(){},I:function(){},z:function(){},m:()=>1,q:function(d,g,_){d=g+2097152>>>0<4194305-!!d?(d>>>0)+4294967296*g:NaN,_>>>=0,d=new Date(1e3*d),z[_>>>2>>>0]=d.getUTCSeconds(),z[_+4>>>2>>>0]=d.getUTCMinutes(),z[_+8>>>2>>>0]=d.getUTCHours(),z[_+12>>>2>>>0]=d.getUTCDate(),z[_+16>>>2>>>0]=d.getUTCMonth(),z[_+20>>>2>>>0]=d.getUTCFullYear()-1900,z[_+24>>>2>>>0]=d.getUTCDay(),z[_+28>>>2>>>0]=(d.getTime()-Date.UTC(d.getUTCFullYear(),0,1,0,0,0,0))/864e5|0},r:function(d,g,_){d=g+2097152>>>0<4194305-!!d?(d>>>0)+4294967296*g:NaN,_>>>=0,d=new Date(1e3*d),z[_>>>2>>>0]=d.getSeconds(),z[_+4>>>2>>>0]=d.getMinutes(),z[_+8>>>2>>>0]=d.getHours(),z[_+12>>>2>>>0]=d.getDate(),z[_+16>>>2>>>0]=d.getMonth(),z[_+20>>>2>>>0]=d.getFullYear()-1900,z[_+24>>>2>>>0]=d.getDay(),z[_+28>>>2>>>0]=(kt(d.getFullYear())?At:Zt)[d.getMonth()]+d.getDate()-1|0,z[_+36>>>2>>>0]=-(60*d.getTimezoneOffset()),g=new Date(d.getFullYear(),6,1).getTimezoneOffset();var C=new Date(d.getFullYear(),0,1).getTimezoneOffset();z[_+32>>>2>>>0]=(g!=C&&d.getTimezoneOffset()==Math.min(C,g))|0},s:function(d){d>>>=0;var g=new Date(z[d+20>>>2>>>0]+1900,z[d+16>>>2>>>0],z[d+12>>>2>>>0],z[d+8>>>2>>>0],z[d+4>>>2>>>0],z[d>>>2>>>0],0),_=z[d+32>>>2>>>0],C=g.getTimezoneOffset(),O=new Date(g.getFullYear(),6,1).getTimezoneOffset(),W=new Date(g.getFullYear(),0,1).getTimezoneOffset(),j=Math.min(W,O);return 0>_?z[d+32>>>2>>>0]=+(O!=W&&j==C):0<_!=(j==C)&&(O=Math.max(W,O),g.setTime(g.getTime()+6e4*((0<_?j:O)-C))),z[d+24>>>2>>>0]=g.getDay(),z[d+28>>>2>>>0]=(kt(g.getFullYear())?At:Zt)[g.getMonth()]+g.getDate()-1|0,z[d>>>2>>>0]=g.getSeconds(),z[d+4>>>2>>>0]=g.getMinutes(),z[d+8>>>2>>>0]=g.getHours(),z[d+12>>>2>>>0]=g.getDate(),z[d+16>>>2>>>0]=g.getMonth(),z[d+20>>>2>>>0]=g.getYear(),d=g.getTime(),isNaN(d)?(z[mr()>>>2>>>0]=61,d=-1):d/=1e3,gr((Fe=d,1<=+Math.abs(Fe)?0>>0:~~+Math.ceil((Fe-+(~~Fe>>>0))/4294967296)>>>0:0)),d>>>0},o:function(){return-52},p:function(){},w:function(d,g,_){function C(oe){return(oe=oe.toTimeString().match(/\(([A-Za-z ]+)\)$/))?oe[1]:"GMT"}_>>>=0;var O=new Date().getFullYear(),W=new Date(O,0,1),j=new Date(O,6,1);O=W.getTimezoneOffset();var Q=j.getTimezoneOffset();L[d>>>0>>>2>>>0]=60*Math.max(O,Q),z[g>>>0>>>2>>>0]=+(O!=Q),d=C(W),g=C(j),d=Xt(d),g=Xt(g),Q>>2>>>0]=d,L[_+4>>>2>>>0]=g):(L[_>>>2>>>0]=g,L[_+4>>>2>>>0]=d)},e:()=>{se("")},b:function(d,g,_){return d>>>=0,g=lr(g>>>0,_>>>0),It[d].apply(null,g)},i:function(d,g,_){return d>>>=0,g=lr(g>>>0,_>>>0),It[d].apply(null,g)},h:()=>Date.now(),x:function(){return 4294901760},c:()=>performance.now(),K:function(d,g,_){return g>>>=0,H.copyWithin(d>>>0>>>0,g>>>0,g+(_>>>0)>>>0)},u:function(d){d>>>=0;var g=H.length;if(4294901760=_;_*=2){var C=g*(1+.2/_);C=Math.min(C,d+100663296);var O=Math;C=Math.max(d,C);e:{O=(O.min.call(O,4294901760,C+(65536-C%65536)%65536)-A.buffer.byteLength+65535)/65536;try{A.grow(O),we();var W=1;break e}catch{}W=void 0}if(W)return!0}return!1},E:function(d,g){d>>>=0,g>>>=0;var _=0;return dr().forEach((C,O)=>{var W=g+_;for(O=L[d+4*O>>>2>>>0]=W,W=0;W>>0>>>0]=C.charCodeAt(W);N[O>>>0>>>0]=0,_+=C.length+1}),0},F:function(d,g){d>>>=0,g>>>=0;var _=dr();L[d>>>2>>>0]=_.length;var C=0;return _.forEach(O=>C+=O.length+1),L[g>>>2>>>0]=C,0},f:()=>52,k:function(){return 52},t:function(){return 70},j:function(d,g,_,C){g>>>=0,_>>>=0,C>>>=0;for(var O=0,W=0;W<_;W++){var j=L[g>>>2>>>0],Q=L[g+4>>>2>>>0];g+=8;for(var oe=0;oe>>0],me=Ke[d];le===0||le===10?((d===1?v:E)(Ot(me,0)),me.length=0):me.push(le)}O+=Q}return L[C>>>2>>>0]=O,0},v:Jt,d:function(d,g,_,C){return Jt(d>>>0,g>>>0,_>>>0,C>>>0)}},ee=function(){function d(_){return ee=_.exports,ee=Un(),ee=Hr(),A=ee.M,we(),Ue.unshift(ee.N),xe--,xe==0&&(fe!==null&&(clearInterval(fe),fe=null),ue&&(_=ue,ue=null,_())),ee}var g={a:Gr};if(xe++,r.instantiateWasm)try{return r.instantiateWasm(g,d)}catch(_){E(`Module.instantiateWasm callback failed with error: ${_}`),n(_)}return et(g,function(_){d(_.instance)}).catch(n),{}}();r._OrtInit=(d,g)=>(r._OrtInit=ee.O)(d,g),r._OrtGetLastError=(d,g)=>(r._OrtGetLastError=ee.P)(d,g),r._OrtCreateSessionOptions=(d,g,_,C,O,W,j,Q,oe,le)=>(r._OrtCreateSessionOptions=ee.Q)(d,g,_,C,O,W,j,Q,oe,le),r._OrtAppendExecutionProvider=(d,g)=>(r._OrtAppendExecutionProvider=ee.R)(d,g),r._OrtAddFreeDimensionOverride=(d,g,_)=>(r._OrtAddFreeDimensionOverride=ee.S)(d,g,_),r._OrtAddSessionConfigEntry=(d,g,_)=>(r._OrtAddSessionConfigEntry=ee.T)(d,g,_),r._OrtReleaseSessionOptions=d=>(r._OrtReleaseSessionOptions=ee.U)(d),r._OrtCreateSession=(d,g,_)=>(r._OrtCreateSession=ee.V)(d,g,_),r._OrtReleaseSession=d=>(r._OrtReleaseSession=ee.W)(d),r._OrtGetInputOutputCount=(d,g,_)=>(r._OrtGetInputOutputCount=ee.X)(d,g,_),r._OrtGetInputName=(d,g)=>(r._OrtGetInputName=ee.Y)(d,g),r._OrtGetOutputName=(d,g)=>(r._OrtGetOutputName=ee.Z)(d,g),r._OrtFree=d=>(r._OrtFree=ee._)(d),r._OrtCreateTensor=(d,g,_,C,O,W)=>(r._OrtCreateTensor=ee.$)(d,g,_,C,O,W),r._OrtGetTensorData=(d,g,_,C,O)=>(r._OrtGetTensorData=ee.aa)(d,g,_,C,O),r._OrtReleaseTensor=d=>(r._OrtReleaseTensor=ee.ba)(d),r._OrtCreateRunOptions=(d,g,_,C)=>(r._OrtCreateRunOptions=ee.ca)(d,g,_,C),r._OrtAddRunConfigEntry=(d,g,_)=>(r._OrtAddRunConfigEntry=ee.da)(d,g,_),r._OrtReleaseRunOptions=d=>(r._OrtReleaseRunOptions=ee.ea)(d),r._OrtCreateBinding=d=>(r._OrtCreateBinding=ee.fa)(d),r._OrtBindInput=(d,g,_)=>(r._OrtBindInput=ee.ga)(d,g,_),r._OrtBindOutput=(d,g,_,C)=>(r._OrtBindOutput=ee.ha)(d,g,_,C),r._OrtClearBoundOutputs=d=>(r._OrtClearBoundOutputs=ee.ia)(d),r._OrtReleaseBinding=d=>(r._OrtReleaseBinding=ee.ja)(d),r._OrtRunWithBinding=(d,g,_,C,O)=>(r._OrtRunWithBinding=ee.ka)(d,g,_,C,O),r._OrtRun=(d,g,_,C,O,W,j,Q)=>(r._OrtRun=ee.la)(d,g,_,C,O,W,j,Q),r._OrtEndProfiling=d=>(r._OrtEndProfiling=ee.ma)(d),r._JsepOutput=(d,g,_)=>(r._JsepOutput=ee.na)(d,g,_),r._JsepGetNodeName=d=>(r._JsepGetNodeName=ee.oa)(d);var mr=()=>(mr=ee.pa)(),er=r._malloc=d=>(er=r._malloc=ee.qa)(d),hr=r._free=d=>(hr=r._free=ee.ra)(d),gr=d=>(gr=ee.ta)(d),yr=()=>(yr=ee.ua)(),br=d=>(br=ee.va)(d),wr=d=>(wr=ee.wa)(d),vr=d=>(vr=ee.ya)(d),$r=()=>($r=ee.za)(),_r=d=>(_r=ee.Aa)(d),xr=()=>(xr=ee.Ba)();r.___start_em_js=944151,r.___stop_em_js=944312;function Hr(){var d=ee;d=Object.assign({},d);var g=C=>()=>C()>>>0,_=C=>O=>C(O)>>>0;return d.pa=g(d.pa),d.qa=_(d.qa),d.ua=g(d.ua),d.wa=_(d.wa),d}r.stackAlloc=wr,r.stackSave=yr,r.stackRestore=br,r.UTF8ToString=je,r.stringToUTF8=(d,g,_)=>Yt(d,H,g,_),r.lengthBytesUTF8=ur;var Rt;ue=function d(){Rt||Wt(),Rt||(ue=d)};function Wt(){if(!(0Bi)});var Ui=tr(()=>{});var Ni=tr(()=>{});var Wi={};rr(Wi,{cpus:()=>vd});var vd,Vi=F(()=>{vd=void 0});var Li=tr((Hi,ta)=>{"use strict";var Gi=(()=>{var e=typeof document<"u"&&document.currentScript?document.currentScript.src:void 0;return typeof __filename<"u"&&(e=e||__filename),function(t={}){function r(){return ne.buffer!=fe.buffer&&G(),fe}function a(){return ne.buffer!=fe.buffer&&G(),ue}function n(){return ne.buffer!=fe.buffer&&G(),se}function o(){return ne.buffer!=fe.buffer&&G(),he}function s(){return ne.buffer!=fe.buffer&&G(),Ee}function l(){return ne.buffer!=fe.buffer&&G(),Le}var i=t,c,f;i.ready=new Promise((u,p)=>{c=u,f=p}),i.mountExternalData=(u,p)=>{(i.cb||(i.cb=new Map)).set(u,p)},i.unmountExternalData=()=>{delete i.cb};let m=()=>{let u=(y,x,T)=>(...R)=>{let V=wt,Z=x?.();R=y(...R);let ge=x?.();return Z!==ge&&(y=ge,T(Z),x=T=null),wt!=V?td():R},p=y=>async(...x)=>{try{if(i.bb)throw Error("Session already started");let T=i.bb={Gb:x[0],errors:[]},R=await y(...x);if(i.bb!==T)throw Error("Session mismatch");i.kb?.flush();let V=T.errors;if(0ge),0i._OrtCreateSession,y=>i._OrtCreateSession=y),i._OrtRun=p(u(i._OrtRun,()=>i._OrtRun,y=>i._OrtRun=y)),i._OrtRunWithBinding=p(u(i._OrtRunWithBinding,()=>i._OrtRunWithBinding,y=>i._OrtRunWithBinding=y)),i._OrtBindInput=u(i._OrtBindInput,()=>i._OrtBindInput,y=>i._OrtBindInput=y),m=void 0};i.jsepInit=(u,p)=>{if(m?.(),u==="webgpu"){[i.kb,i.xb,i.Bb,i.lb,i.Ab,i.Ea,i.Cb,i.Eb,i.yb,i.zb,i.Db]=p;let y=i.kb;i.jsepRegisterBuffer=(x,T,R,V)=>y.registerBuffer(x,T,R,V),i.jsepGetBuffer=x=>y.getBuffer(x),i.jsepCreateDownloader=(x,T,R)=>y.createDownloader(x,T,R),i.jsepOnReleaseSession=x=>{y.onReleaseSession(x)},i.jsepOnRunStart=x=>y.onRunStart(x)}};var h=Object.assign({},i),w="./this.program",b=(u,p)=>{throw p},$=typeof window=="object",I=typeof importScripts=="function",S=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string",v=i.ENVIRONMENT_IS_PTHREAD||!1,E="";function A(u){return i.locateFile?i.locateFile(u,E):E+u}var k,U,N;if(S){var H=(Xn(),Gt(Zn)),z=(Jn(),Gt(Qn));E=I?z.dirname(E)+"/":__dirname+"/",k=(p,y)=>(p=Et(p)?new URL(p):z.normalize(p),H.readFileSync(p,y?void 0:"utf8")),N=p=>(p=k(p,!0),p.buffer||(p=new Uint8Array(p)),p),U=(p,y,x,T=!0)=>{p=Et(p)?new URL(p):z.normalize(p),H.readFile(p,T?void 0:"utf8",(R,V)=>{R?x(R):y(T?V.buffer:V)})},!i.thisProgram&&1{throw process.exitCode=p,y},i.inspect=()=>"[Emscripten Module object]";let u;try{u=Ui()}catch(p){throw console.error('The "worker_threads" module is not supported in this node.js build - perhaps a newer version is needed?'),p}global.Worker=u.Worker}else($||I)&&(I?E=self.location.href:typeof document<"u"&&document.currentScript&&(E=document.currentScript.src),typeof e<"u"&&e&&(E=e),E.indexOf("blob:")!==0?E=E.substr(0,E.replace(/[?#].*/,"").lastIndexOf("/")+1):E="",S||(k=u=>{var p=new XMLHttpRequest;return p.open("GET",u,!1),p.send(null),p.responseText},I&&(N=u=>{var p=new XMLHttpRequest;return p.open("GET",u,!1),p.responseType="arraybuffer",p.send(null),new Uint8Array(p.response)}),U=(u,p,y)=>{var x=new XMLHttpRequest;x.open("GET",u,!0),x.responseType="arraybuffer",x.onload=()=>{x.status==200||x.status==0&&x.response?p(x.response):y()},x.onerror=y,x.send(null)}));S&&typeof performance>"u"&&(global.performance=Ni().performance);var L=console.log.bind(console),pe=console.error.bind(console);S&&(L=(...u)=>H.writeSync(1,u.join(" ")+` `),pe=(...u)=>H.writeSync(2,u.join(" ")+` `));var Ie=L,we=pe;Object.assign(i,h),h=null,typeof WebAssembly!="object"&&tt("no native wasm support detected");var ne,Ue,X=!1,xe,fe,ue,se,he,Ee,Le;function G(){var u=ne.buffer;i.HEAP8=fe=new Int8Array(u),i.HEAP16=new Int16Array(u),i.HEAPU8=ue=new Uint8Array(u),i.HEAPU16=new Uint16Array(u),i.HEAP32=se=new Int32Array(u),i.HEAPU32=he=new Uint32Array(u),i.HEAPF32=Ee=new Float32Array(u),i.HEAPF64=Le=new Float64Array(u)}var J=16777216;if(v)ne=i.wasmMemory;else if(i.wasmMemory)ne=i.wasmMemory;else if(ne=new WebAssembly.Memory({initial:J/65536,maximum:65536,shared:!0}),!(ne.buffer instanceof SharedArrayBuffer))throw we("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"),S&&we("(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and/or recent version)"),Error("bad memory");G(),J=ne.buffer.byteLength;var Se=[],Je=[],et=[],Fe=0,It=null,Ve=null;function qe(){if(Fe--,Fe==0&&(It!==null&&(clearInterval(It),It=null),Ve)){var u=Ve;Ve=null,u()}}function tt(u){throw u="Aborted("+u+")",we(u),X=!0,xe=1,u=new WebAssembly.RuntimeError(u+". Build with -sASSERTIONS for more info."),f(u),u}var Ut=u=>u.startsWith("data:application/octet-stream;base64,"),Et=u=>u.startsWith("file://"),Ot;Ot="ort-wasm-simd-threaded.wasm",Ut(Ot)||(Ot=A(Ot));function je(u){if(N)return N(u);throw"both async and sync fetching of the wasm failed"}function ur(u){if($||I){if(typeof fetch=="function"&&!Et(u))return fetch(u,{credentials:"same-origin"}).then(p=>{if(!p.ok)throw"failed to load wasm binary file at '"+u+"'";return p.arrayBuffer()}).catch(()=>je(u));if(U)return new Promise((p,y)=>{U(u,x=>p(new Uint8Array(x)),y)})}return Promise.resolve().then(()=>je(u))}function Yt(u,p,y){return ur(u).then(x=>WebAssembly.instantiate(x,p)).then(x=>x).then(y,x=>{we(`failed to asynchronously prepare wasm: ${x}`),tt(x)})}function kt(u,p){var y=Ot;return typeof WebAssembly.instantiateStreaming!="function"||Ut(y)||Et(y)||S||typeof fetch!="function"?Yt(y,u,p):fetch(y,{credentials:"same-origin"}).then(x=>WebAssembly.instantiateStreaming(x,u).then(p,function(T){return we(`wasm streaming compile failed: ${T}`),we("falling back to ArrayBuffer instantiation"),Yt(y,u,p)}))}var At,Zt={933148:(u,p,y,x)=>{if(typeof i>"u"||!i.cb)return 1;if(u=Ke(u>>>0),u.startsWith("./")&&(u=u.substring(2)),u=i.cb.get(u),!u)return 2;if(p>>>=0,y>>>=0,x>>>=0,p+y>u.byteLength)return 3;try{return a().set(u.subarray(p,p+y),x>>>0),0}catch{return 4}},933649:()=>{i.yb()},933680:()=>{i.zb()},933709:()=>{i.Db()},933734:u=>i.xb(u),933767:u=>i.Bb(u),933799:(u,p,y)=>{i.lb(u,p,y,!0)},933838:(u,p,y)=>{i.lb(u,p,y)},933871:u=>{i.Ea("Abs",u,void 0)},933922:u=>{i.Ea("Neg",u,void 0)},933973:u=>{i.Ea("Floor",u,void 0)},934026:u=>{i.Ea("Ceil",u,void 0)},934078:u=>{i.Ea("Reciprocal",u,void 0)},934136:u=>{i.Ea("Sqrt",u,void 0)},934188:u=>{i.Ea("Exp",u,void 0)},934239:u=>{i.Ea("Erf",u,void 0)},934290:u=>{i.Ea("Sigmoid",u,void 0)},934345:(u,p,y)=>{i.Ea("HardSigmoid",u,{alpha:p,beta:y})},934424:u=>{i.Ea("Log",u,void 0)},934475:u=>{i.Ea("Sin",u,void 0)},934526:u=>{i.Ea("Cos",u,void 0)},934577:u=>{i.Ea("Tan",u,void 0)},934628:u=>{i.Ea("Asin",u,void 0)},934680:u=>{i.Ea("Acos",u,void 0)},934732:u=>{i.Ea("Atan",u,void 0)},934784:u=>{i.Ea("Sinh",u,void 0)},934836:u=>{i.Ea("Cosh",u,void 0)},934888:u=>{i.Ea("Asinh",u,void 0)},934941:u=>{i.Ea("Acosh",u,void 0)},934994:u=>{i.Ea("Atanh",u,void 0)},935047:u=>{i.Ea("Tanh",u,void 0)},935099:u=>{i.Ea("Not",u,void 0)},935150:(u,p,y)=>{i.Ea("Clip",u,{min:p,max:y})},935219:u=>{i.Ea("Clip",u,void 0)},935271:(u,p)=>{i.Ea("Elu",u,{alpha:p})},935329:u=>{i.Ea("Relu",u,void 0)},935381:(u,p)=>{i.Ea("LeakyRelu",u,{alpha:p})},935445:(u,p)=>{i.Ea("ThresholdedRelu",u,{alpha:p})},935515:(u,p)=>{i.Ea("Cast",u,{to:p})},935573:u=>{i.Ea("Add",u,void 0)},935624:u=>{i.Ea("Sub",u,void 0)},935675:u=>{i.Ea("Mul",u,void 0)},935726:u=>{i.Ea("Div",u,void 0)},935777:u=>{i.Ea("Pow",u,void 0)},935828:u=>{i.Ea("Equal",u,void 0)},935881:u=>{i.Ea("Greater",u,void 0)},935936:u=>{i.Ea("GreaterOrEqual",u,void 0)},935998:u=>{i.Ea("Less",u,void 0)},936050:u=>{i.Ea("LessOrEqual",u,void 0)},936109:(u,p,y,x,T)=>{i.Ea("ReduceMean",u,{keepDims:!!p,noopWithEmptyAxes:!!y,axes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},936268:(u,p,y,x,T)=>{i.Ea("ReduceMax",u,{keepDims:!!p,noopWithEmptyAxes:!!y,axes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},936426:(u,p,y,x,T)=>{i.Ea("ReduceMin",u,{keepDims:!!p,noopWithEmptyAxes:!!y,axes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},936584:(u,p,y,x,T)=>{i.Ea("ReduceProd",u,{keepDims:!!p,noopWithEmptyAxes:!!y,axes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},936743:(u,p,y,x,T)=>{i.Ea("ReduceSum",u,{keepDims:!!p,noopWithEmptyAxes:!!y,axes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},936901:(u,p,y,x,T)=>{i.Ea("ReduceL1",u,{keepDims:!!p,noopWithEmptyAxes:!!y,axes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},937058:(u,p,y,x,T)=>{i.Ea("ReduceL2",u,{keepDims:!!p,noopWithEmptyAxes:!!y,axes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},937215:(u,p,y,x,T)=>{i.Ea("ReduceLogSum",u,{keepDims:!!p,noopWithEmptyAxes:!!y,axes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},937376:(u,p,y,x,T)=>{i.Ea("ReduceSumSquare",u,{keepDims:!!p,noopWithEmptyAxes:!!y,axes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},937540:(u,p,y,x,T)=>{i.Ea("ReduceLogSumExp",u,{keepDims:!!p,noopWithEmptyAxes:!!y,axes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},937704:u=>{i.Ea("Where",u,void 0)},937757:(u,p,y)=>{i.Ea("Transpose",u,{perm:p?Array.from(n().subarray(p>>>0,y>>>0)):[]})},937865:(u,p,y,x,T,R,V,Z,ge,de,ve,Ne,We,B,ce)=>{i.Ea("ConvTranspose",u,{format:ge?"NHWC":"NCHW",autoPad:p,dilations:[y],group:x,kernel_shape:[T],pads:[R,V],strides:[Z],wIsConst:()=>!!r()[de>>>0],outputPadding:ve?Array.from(n().subarray(ve>>>0,Ne>>>0)):[],outputShape:We?Array.from(n().subarray(We>>>0,B>>>0)):[],activation:Ke(ce)})},938267:(u,p,y,x,T,R,V,Z,ge,de,ve,Ne,We,B)=>{i.Ea("ConvTranspose",u,{format:Z?"NHWC":"NCHW",autoPad:p,dilations:Array.from(n().subarray(y>>>0,(y>>>0)+2>>>0)),group:x,kernelShape:Array.from(n().subarray(T>>>0,(T>>>0)+2>>>0)),pads:Array.from(n().subarray(R>>>0,(R>>>0)+4>>>0)),strides:Array.from(n().subarray(V>>>0,(V>>>0)+2>>>0)),wIsConst:()=>!!r()[ge>>>0],outputPadding:de?Array.from(n().subarray(de>>>0,ve>>>0)):[],outputShape:Ne?Array.from(n().subarray(Ne>>>0,We>>>0)):[],activation:Ke(B)})},938832:(u,p,y,x,T,R,V,Z,ge,de,ve,Ne,We,B,ce)=>{i.Ea("ConvTranspose",u,{format:ge?"NHWC":"NCHW",autoPad:p,dilations:[y],group:x,kernel_shape:[T],pads:[R,V],strides:[Z],wIsConst:()=>!!r()[de>>>0],outputPadding:ve?Array.from(n().subarray(ve>>>0,Ne>>>0)):[],outputShape:We?Array.from(n().subarray(We>>>0,B>>>0)):[],activation:Ke(ce)})},939234:(u,p,y,x,T,R,V,Z,ge,de,ve,Ne,We,B)=>{i.Ea("ConvTranspose",u,{format:Z?"NHWC":"NCHW",autoPad:p,dilations:Array.from(n().subarray(y>>>0,(y>>>0)+2>>>0)),group:x,kernelShape:Array.from(n().subarray(T>>>0,(T>>>0)+2>>>0)),pads:Array.from(n().subarray(R>>>0,(R>>>0)+4>>>0)),strides:Array.from(n().subarray(V>>>0,(V>>>0)+2>>>0)),wIsConst:()=>!!r()[ge>>>0],outputPadding:de?Array.from(n().subarray(de>>>0,ve>>>0)):[],outputShape:Ne?Array.from(n().subarray(Ne>>>0,We>>>0)):[],activation:Ke(B)})},939799:(u,p)=>{i.Ea("GlobalAveragePool",u,{format:p?"NHWC":"NCHW"})},939890:(u,p,y,x,T,R,V,Z,ge,de,ve,Ne,We,B,ce,Te)=>{i.Ea("AveragePool",u,{format:Te?"NHWC":"NCHW",auto_pad:p,ceil_mode:y,count_include_pad:x,storage_order:T,dilations:[R,V],kernel_shape:[Z,ge],pads:[de,ve,Ne,We],strides:[B,ce]})},940174:(u,p)=>{i.Ea("GlobalAveragePool",u,{format:p?"NHWC":"NCHW"})},940265:(u,p,y,x,T,R,V,Z,ge,de,ve,Ne,We,B,ce,Te)=>{i.Ea("AveragePool",u,{format:Te?"NHWC":"NCHW",auto_pad:p,ceil_mode:y,count_include_pad:x,storage_order:T,dilations:[R,V],kernel_shape:[Z,ge],pads:[de,ve,Ne,We],strides:[B,ce]})},940549:(u,p)=>{i.Ea("GlobalMaxPool",u,{format:p?"NHWC":"NCHW"})},940636:(u,p,y,x,T,R,V,Z,ge,de,ve,Ne,We,B,ce,Te)=>{i.Ea("MaxPool",u,{format:Te?"NHWC":"NCHW",auto_pad:p,ceil_mode:y,count_include_pad:x,storage_order:T,dilations:[R,V],kernel_shape:[Z,ge],pads:[de,ve,Ne,We],strides:[B,ce]})},940916:(u,p)=>{i.Ea("GlobalMaxPool",u,{format:p?"NHWC":"NCHW"})},941003:(u,p,y,x,T,R,V,Z,ge,de,ve,Ne,We,B,ce,Te)=>{i.Ea("MaxPool",u,{format:Te?"NHWC":"NCHW",auto_pad:p,ceil_mode:y,count_include_pad:x,storage_order:T,dilations:[R,V],kernel_shape:[Z,ge],pads:[de,ve,Ne,We],strides:[B,ce]})},941283:(u,p,y,x,T)=>{i.Ea("Gemm",u,{alpha:p,beta:y,transA:x,transB:T})},941387:u=>{i.Ea("MatMul",u,void 0)},941441:(u,p,y,x)=>{i.Ea("ArgMax",u,{keepDims:!!p,selectLastIndex:!!y,axis:x})},941549:(u,p,y,x)=>{i.Ea("ArgMin",u,{keepDims:!!p,selectLastIndex:!!y,axis:x})},941657:(u,p)=>{i.Ea("Softmax",u,{axis:p})},941720:(u,p)=>{i.Ea("Concat",u,{axis:p})},941780:(u,p,y,x,T)=>{i.Ea("Split",u,{axis:p,numOutputs:y,splitSizes:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},941920:u=>{i.Ea("Expand",u,void 0)},941974:(u,p)=>{i.Ea("Gather",u,{axis:Number(p)})},942045:(u,p)=>{i.Ea("GatherElements",u,{axis:Number(p)})},942124:(u,p,y,x,T,R,V,Z,ge,de,ve)=>{i.Ea("Resize",u,{antialias:p,axes:y?Array.from(n().subarray(y>>>0,x>>>0)):[],coordinateTransformMode:Ke(T),cubicCoeffA:R,excludeOutside:V,extrapolationValue:Z,keepAspectRatioPolicy:Ke(ge),mode:Ke(de),nearestMode:Ke(ve)})},942470:(u,p,y,x,T,R,V)=>{i.Ea("Slice",u,{starts:p?Array.from(n().subarray(p>>>0,y>>>0)):[],ends:x?Array.from(n().subarray(x>>>0,T>>>0)):[],axes:R?Array.from(n().subarray(R>>>0,V>>>0)):[]})},942686:u=>{i.Ea("Tile",u,void 0)},942738:(u,p,y)=>{i.Ea("LayerNormalization",u,{axis:Number(p),epsilon:Number(y)})},942845:(u,p,y)=>{i.Ea("InstanceNormalization",u,{epsilon:p,format:y?"NHWC":"NCHW"})},942959:(u,p,y)=>{i.Ea("InstanceNormalization",u,{epsilon:p,format:y?"NHWC":"NCHW"})},943073:u=>{i.Ea("Range",u,void 0)},943126:(u,p)=>{i.Ea("Einsum",u,{equation:Ke(p)})},943207:(u,p,y,x,T)=>{i.Ea("Pad",u,{mode:p,value:y,pads:x?Array.from(n().subarray(x>>>0,T>>>0)):[]})},943334:(u,p,y,x,T,R)=>{i.Ea("BatchNormalization",u,{epsilon:p,momentum:y,spatial:!!T,trainingMode:!!x,format:R?"NHWC":"NCHW"})},943503:(u,p,y,x,T,R)=>{i.Ea("BatchNormalization",u,{epsilon:p,momentum:y,spatial:!!T,trainingMode:!!x,format:R?"NHWC":"NCHW"})},943672:(u,p,y)=>{i.Ea("CumSum",u,{exclusive:Number(p),reverse:Number(y)})},943769:(u,p,y,x,T,R,V,Z,ge)=>{i.Ea("Attention",u,{numHeads:p,isUnidirectional:y,maskFilterValue:x,scale:T,doRotary:R,qkvHiddenSizes:V?Array.from(n().subarray(Number(Z)>>>0,Number(Z)+V>>>0)):[],pastPresentShareBuffer:!!ge})},944041:u=>{i.Ea("BiasAdd",u,void 0)},944096:u=>{i.Ea("BiasSplitGelu",u,void 0)},944157:u=>{i.Ea("FastGelu",u,void 0)},944213:(u,p,y,x,T,R,V,Z,ge,de,ve,Ne,We)=>{i.Ea("Conv",u,{format:ge?"NHWC":"NCHW",auto_pad:p,dilations:[y],group:x,kernel_shape:[T],pads:R?Array.from(n().subarray(R>>>0,V>>>0)):[],strides:[Z],w_is_const:()=>!!r()[de>>>0],activation:Ke(ve),activation_params:Ne?Array.from(s().subarray(Ne>>>0,We>>>0)):[]})},944583:(u,p,y,x,T,R,V,Z,ge,de,ve,Ne,We,B,ce,Te)=>{i.Ea("Conv",u,{format:Ne?"NHWC":"NCHW",auto_pad:p,dilations:[y,x],group:T,kernel_shape:[R,V],pads:Z?Array.from(n().subarray(Z>>>0,ge>>>0)):[],strides:[de,ve],w_is_const:()=>!!r()[We>>>0],activation:Ke(B),activation_params:ce?Array.from(s().subarray(ce>>>0,Te>>>0)):[]})},944974:u=>{i.Ea("Gelu",u,void 0)},945026:(u,p,y,x,T,R)=>{i.Ea("MatMulNBits",u,{k:p,n:y,accuracyLevel:x,bits:T,blockSize:R})},945153:(u,p,y,x,T,R)=>{i.Ea("MultiHeadAttention",u,{numHeads:p,isUnidirectional:y,maskFilterValue:x,scale:T,doRotary:R})},945312:(u,p)=>{i.Ea("SkipLayerNormalization",u,{epsilon:p})},945393:u=>{i.Cb(u)},945427:(u,p)=>i.Eb(u,p,i.bb.Gb,i.bb.errors)};function Xt(u){this.name="ExitStatus",this.message=`Program terminated with exit(${u})`,this.status=u}var Nt=u=>{u.terminate(),u.onmessage=()=>{}},lr=u=>{te.Ya.length==0&&(ft(),te.mb(te.Ya[0]));var p=te.Ya.pop();if(!p)return 6;te.Za.push(p),te.Qa[u.Xa]=p,p.Xa=u.Xa;var y={cmd:"run",start_routine:u.Hb,arg:u.ub,pthread_ptr:u.Xa};return S&&p.unref(),p.postMessage(y,u.Nb),0},pt=0,dr=typeof TextDecoder<"u"?new TextDecoder("utf8"):void 0,Qt=(u,p,y)=>{p>>>=0;var x=p+y;for(y=p;u[y]&&!(y>=x);)++y;if(16T?x+=String.fromCharCode(T):(T-=65536,x+=String.fromCharCode(55296|T>>10,56320|T&1023))}}else x+=String.fromCharCode(T)}return x},Ke=(u,p)=>(u>>>=0)?Qt(a(),u,p):"",zr=u=>{var p=Hn();return u=u(),Kr(p),u};function Re(u,p){var y=arguments.length-2,x=arguments;return zr(()=>{for(var T=Ln(8*y),R=T>>>3,V=0;V>>0]=Z}return Ka(u,y,T,p)})}function Br(u){if(v)return Re(0,1,u);xe=u,0{if(xe=u,v)throw cr(u),"unwind";Br(u)},Pt=u=>{u instanceof Xt||u=="unwind"||b(1,u)};function Un(){for(var u=i.numThreads;u--;)ft();Se.unshift(()=>{Fe++,ut(()=>qe())})}function ft(){var u=A("ort-wasm-simd-threaded.worker.js");u=new Worker(u),te.Ya.push(u)}function ut(u){v?u():Promise.all(te.Ya.map(te.mb)).then(u)}var te={Ya:[],Za:[],qb:[],Qa:{},hb(){v?(te.receiveObjectTransfer=te.Fb,te.threadInitTLS=te.pb,te.setExitStatus=te.ob):Un()},ob:u=>xe=u,Qb:["$terminateWorker"],Ib:()=>{for(var u of te.Za)Nt(u);for(u of te.Ya)Nt(u);te.Ya=[],te.Za=[],te.Qa=[]},nb:u=>{var p=u.Xa;delete te.Qa[p],te.Ya.push(u),te.Za.splice(te.Za.indexOf(u),1),u.Xa=0,Vn(p)},Fb(){},pb(){te.qb.forEach(u=>u())},mb:u=>new Promise(p=>{u.onmessage=R=>{R=R.data;var V=R.cmd;if(R.targetThread&&R.targetThread!=jr()){var Z=te.Qa[R.targetThread];Z?Z.postMessage(R,R.transferList):we(`Internal error! Worker sent a message "${V}" to target pthread ${R.targetThread}, but that thread no longer exists!`)}else V==="checkMailbox"?Wt():V==="spawnThread"?lr(R):V==="cleanupThread"?te.nb(te.Qa[R.thread]):V==="killThread"?(R=R.thread,V=te.Qa[R],delete te.Qa[R],Nt(V),Vn(R),te.Za.splice(te.Za.indexOf(V),1),V.Xa=0):V==="cancelThread"?te.Qa[R.thread].postMessage({cmd:"cancel"}):V==="loaded"?(u.loaded=!0,S&&!u.Xa&&u.unref(),p(u)):V==="alert"?alert(`Thread ${R.threadId}: ${R.text}`):R.target==="setimmediate"?u.postMessage(R):V==="callHandler"?i[R.handler](...R.args):V&&we(`worker sent an unknown command ${V}`)},u.onerror=R=>{throw we(`worker sent an error! ${R.filename}:${R.lineno}: ${R.message}`),R},S&&(u.on("message",R=>u.onmessage({data:R})),u.on("error",R=>u.onerror(R)));var y=[],x=["onExit"],T;for(T of x)i.hasOwnProperty(T)&&y.push(T);u.postMessage({cmd:"load",handlers:y,urlOrBlob:i.mainScriptUrlOrBlob||e,wasmMemory:ne,wasmModule:Ue})})};i.PThread=te;var Tt=u=>{for(;0{var u=jr(),p=o()[u+52>>>2>>>0];u=o()[u+56>>>2>>>0],Xa(p,p-u),Kr(p)};function cr(u){if(v)return Re(1,0,u);Jt(u)}i.invokeEntryPoint=(u,p)=>{u=Qa.apply(null,[u,p]),0>>2>>>0]=p},this.sb=function(p){o()[this.gb+8>>>2>>>0]=p},this.hb=function(p,y){this.rb(),this.tb(p),this.sb(y)},this.rb=function(){o()[this.gb+16>>>2>>>0]=0}}var Mr=0,pr=0;function Ur(u,p,y,x){return v?Re(2,1,u,p,y,x):Nr(u,p,y,x)}function Nr(u,p,y,x){if(u>>>=0,p>>>=0,y>>>=0,x>>>=0,typeof SharedArrayBuffer>"u")return we("Current environment does not support SharedArrayBuffer, pthreads are not available!"),6;var T=[];return v&&T.length===0?Ur(u,p,y,x):(u={Hb:y,Xa:u,ub:x,Nb:T},v?(u.Pb="spawnThread",postMessage(u,T),0):lr(u))}function Wr(u,p,y){return v?Re(3,1,u,p,y):0}function Vr(u,p){if(v)return Re(4,1,u,p)}var fr=u=>{for(var p=0,y=0;y=x?p++:2047>=x?p+=2:55296<=x&&57343>=x?(p+=4,++y):p+=3}return p},Gr=(u,p,y,x)=>{if(y>>>=0,!(0=V){var Z=u.charCodeAt(++R);V=65536+((V&1023)<<10)|Z&1023}if(127>=V){if(y>=x)break;p[y++>>>0]=V}else{if(2047>=V){if(y+1>=x)break;p[y++>>>0]=192|V>>6}else{if(65535>=V){if(y+2>=x)break;p[y++>>>0]=224|V>>12}else{if(y+3>=x)break;p[y++>>>0]=240|V>>18,p[y++>>>0]=128|V>>12&63}p[y++>>>0]=128|V>>6&63}p[y++>>>0]=128|V&63}}return p[y>>>0]=0,y-T},ee=(u,p,y)=>Gr(u,a(),p,y);function mr(u,p){if(v)return Re(5,1,u,p)}function er(u,p,y){if(v)return Re(6,1,u,p,y)}function hr(u,p,y){return v?Re(7,1,u,p,y):0}function gr(u,p){if(v)return Re(8,1,u,p)}function yr(u,p,y){if(v)return Re(9,1,u,p,y)}function br(u,p,y,x){if(v)return Re(10,1,u,p,y,x)}function wr(u,p,y,x){if(v)return Re(11,1,u,p,y,x)}function vr(u,p,y,x){if(v)return Re(12,1,u,p,y,x)}function $r(u){if(v)return Re(13,1,u)}function _r(u,p){if(v)return Re(14,1,u,p)}function xr(u,p,y){if(v)return Re(15,1,u,p,y)}var Hr=()=>{if(!(0>>=0,typeof Atomics.Ob=="function"&&(Atomics.Ob(n(),u>>>2,u).value.then(Wt),u+=128,Atomics.store(n(),u>>>2,1))}i.__emscripten_thread_mailbox_await=Rt;var Wt=()=>{var u=jr();if(u&&(Rt(u),!X))try{Ya(),Hr()}catch(p){Pt(p)}};i.checkMailbox=Wt;var d=[],g=u=>u%4===0&&(u%100!==0||u%400===0),_=[0,31,60,91,121,152,182,213,244,274,305,335],C=[0,31,59,90,120,151,181,212,243,273,304,334];function O(u,p,y,x,T,R,V,Z){return v?Re(16,1,u,p,y,x,T,R,V,Z):-52}function W(u,p,y,x,T,R,V){if(v)return Re(17,1,u,p,y,x,T,R,V)}var j=u=>{var p=fr(u)+1,y=Wn(p);return y&&ee(u,y,p),y},Q=[],oe=(u,p)=>{Q.length=0;for(var y;y=a()[u++>>>0];){var x=y!=105;x&=y!=112,p+=x&&p%8?4:0,Q.push(y==112?o()[p>>>2>>>0]:y==105?n()[p>>>2>>>0]:l()[p>>>3>>>0]),p+=x?8:4}return Q},le={},me=()=>{if(!Pe){var u={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:w||"./this.program"},p;for(p in le)le[p]===void 0?delete u[p]:u[p]=le[p];var y=[];for(p in u)y.push(`${p}=${u[p]}`);Pe=y}return Pe},Pe;function ze(u,p){if(v)return Re(18,1,u,p);u>>>=0,p>>>=0;var y=0;return me().forEach((x,T)=>{var R=p+y;for(T=o()[u+4*T>>>2>>>0]=R,R=0;R>>0>>>0]=x.charCodeAt(R);r()[T>>>0>>>0]=0,y+=x.length+1}),0}function P(u,p){if(v)return Re(19,1,u,p);u>>>=0,p>>>=0;var y=me();o()[u>>>2>>>0]=y.length;var x=0;return y.forEach(T=>x+=T.length+1),o()[p>>>2>>>0]=x,0}function ae(u){return v?Re(20,1,u):52}function Ce(u,p,y,x){return v?Re(21,1,u,p,y,x):52}function bt(u,p,y,x,T){return v?Re(22,1,u,p,y,x,T):70}var Lr=[null,[],[]];function Ua(u,p,y,x){if(v)return Re(23,1,u,p,y,x);p>>>=0,y>>>=0,x>>>=0;for(var T=0,R=0;R>>2>>>0],Z=o()[p+4>>>2>>>0];p+=8;for(var ge=0;ge>>0],ve=Lr[u];de===0||de===10?((u===1?Ie:we)(Qt(ve,0)),ve.length=0):ve.push(de)}T+=Z}return o()[x>>>2>>>0]=T,0}var Na=[31,29,31,30,31,30,31,31,30,31,30,31],Wa=[31,28,31,30,31,30,31,31,30,31,30,31];function Zl(u){var p=Array(fr(u)+1);return Gr(u,p,0,p.length),p}var Xl=(u,p)=>{r().set(u,p>>>0)};function Va(u,p,y,x){function T(B,ce,Te){for(B=typeof B=="number"?B.toString():B||"";B.lengthai?-1:0Vt-B.getDate())ce-=Vt-B.getDate()+1,B.setDate(1),11>Te?B.setMonth(Te+1):(B.setMonth(0),B.setFullYear(B.getFullYear()+1));else{B.setDate(B.getDate()+ce);break}}return Te=new Date(B.getFullYear()+1,0,4),ce=Z(new Date(B.getFullYear(),0,4)),Te=Z(Te),0>=V(ce,B)?0>=V(Te,B)?B.getFullYear()+1:B.getFullYear():B.getFullYear()-1}u>>>=0,p>>>=0,y>>>=0,x>>>=0;var de=o()[x+40>>>2>>>0];x={Lb:n()[x>>>2>>>0],Kb:n()[x+4>>>2>>>0],eb:n()[x+8>>>2>>>0],jb:n()[x+12>>>2>>>0],fb:n()[x+16>>>2>>>0],ab:n()[x+20>>>2>>>0],Wa:n()[x+24>>>2>>>0],$a:n()[x+28>>>2>>>0],Rb:n()[x+32>>>2>>>0],Jb:n()[x+36>>>2>>>0],Mb:de?Ke(de):""},y=Ke(y),de={"%c":"%a %b %d %H:%M:%S %Y","%D":"%m/%d/%y","%F":"%Y-%m-%d","%h":"%b","%r":"%I:%M:%S %p","%R":"%H:%M","%T":"%H:%M:%S","%x":"%m/%d/%y","%X":"%H:%M:%S","%Ec":"%c","%EC":"%C","%Ex":"%m/%d/%y","%EX":"%H:%M:%S","%Ey":"%y","%EY":"%Y","%Od":"%d","%Oe":"%e","%OH":"%H","%OI":"%I","%Om":"%m","%OM":"%M","%OS":"%S","%Ou":"%u","%OU":"%U","%OV":"%V","%Ow":"%w","%OW":"%W","%Oy":"%y"};for(var ve in de)y=y.replace(new RegExp(ve,"g"),de[ve]);var Ne="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),We="January February March April May June July August September October November December".split(" ");de={"%a":B=>Ne[B.Wa].substring(0,3),"%A":B=>Ne[B.Wa],"%b":B=>We[B.fb].substring(0,3),"%B":B=>We[B.fb],"%C":B=>R((B.ab+1900)/100|0,2),"%d":B=>R(B.jb,2),"%e":B=>T(B.jb,2," "),"%g":B=>ge(B).toString().substring(2),"%G":B=>ge(B),"%H":B=>R(B.eb,2),"%I":B=>(B=B.eb,B==0?B=12:12{for(var ce=0,Te=0;Te<=B.fb-1;ce+=(g(B.ab+1900)?Na:Wa)[Te++]);return R(B.jb+ce,3)},"%m":B=>R(B.fb+1,2),"%M":B=>R(B.Kb,2),"%n":()=>` `,"%p":B=>0<=B.eb&&12>B.eb?"AM":"PM","%S":B=>R(B.Lb,2),"%t":()=>" ","%u":B=>B.Wa||7,"%U":B=>R(Math.floor((B.$a+7-B.Wa)/7),2),"%V":B=>{var ce=Math.floor((B.$a+7-(B.Wa+6)%7)/7);if(2>=(B.Wa+371-B.$a-2)%7&&ce++,ce)ce==53&&(Te=(B.Wa+371-B.$a)%7,Te==4||Te==3&&g(B.ab)||(ce=1));else{ce=52;var Te=(B.Wa+7-B.$a-1)%7;(Te==4||Te==5&&g(B.ab%400-1))&&ce++}return R(ce,2)},"%w":B=>B.Wa,"%W":B=>R(Math.floor((B.$a+7-(B.Wa+6)%7)/7),2),"%y":B=>(B.ab+1900).toString().substring(2),"%Y":B=>B.ab+1900,"%z":B=>{B=B.Jb;var ce=0<=B;return B=Math.abs(B)/60,(ce?"+":"-")+("0000"+(B/60*100+B%60)).slice(-4)},"%Z":B=>B.Mb,"%%":()=>"%"},y=y.replace(/%%/g,"\0\0");for(ve in de)y.includes(ve)&&(y=y.replace(new RegExp(ve,"g"),de[ve](x)));return y=y.replace(/\0\0/g,"%"),ve=Zl(y),ve.length>p?0:(Xl(ve,u),ve.length-1)}var Fr=u=>{try{u()}catch(p){tt(p)}};function Ql(){var u=Y,p={};for(let[y,x]of Object.entries(u))p[y]=typeof x=="function"?function(){qr.push(y);try{return x.apply(null,arguments)}finally{X||(qr.pop(),wt&&zt===1&&qr.length===0&&(zt=0,pt+=1,Fr(ei),typeof Fibers<"u"&&Fibers.Sb()))}}:x;return p}var zt=0,wt=null,Ga=0,qr=[],Ha={},La={},Jl=0,Nn=null,ed=[];function td(){return new Promise((u,p)=>{Nn={resolve:u,reject:p}})}function rd(){var u=Wn(65548),p=u+12;o()[u>>>2>>>0]=p,o()[u+4>>>2>>>0]=p+65536,p=qr[0];var y=Ha[p];return y===void 0&&(y=Jl++,Ha[p]=y,La[y]=p),p=y,n()[u+8>>>2>>>0]=p,u}function nd(){var u=n()[wt+8>>>2>>>0];return u=Y[La[u]],--pt,u()}function ad(u){if(!X){if(zt===0){var p=!1,y=!1;u((x=0)=>{if(!X&&(Ga=x,p=!0,y)){zt=2,Fr(()=>ti(wt)),typeof Browser<"u"&&Browser.ib.wb&&Browser.ib.resume(),x=!1;try{var T=nd()}catch(Z){T=Z,x=!0}var R=!1;if(!wt){var V=Nn;V&&(Nn=null,(x?V.reject:V.resolve)(T),R=!0)}if(x&&!R)throw T}}),y=!0,p||(zt=1,wt=rd(),typeof Browser<"u"&&Browser.ib.wb&&Browser.ib.pause(),Fr(()=>Ja(wt)))}else zt===2?(zt=0,Fr(ri),qa(wt),wt=null,ed.forEach(x=>{if(!X)try{x(),Hr()}catch(T){Pt(T)}})):tt(`invalid state: ${zt}`);return Ga}}function id(u){return ad(p=>{u().then(p)})}te.hb();var od=[Br,cr,Ur,Wr,Vr,mr,er,hr,gr,yr,br,wr,vr,$r,_r,xr,O,W,ze,P,ae,Ce,bt,Ua],sd={r:function(u,p,y){return id(async()=>{await i.Ab(u,p,y)})},b:function(u,p,y){throw u>>>=0,new Dr(u).hb(p>>>0,y>>>0),Mr=u,pr++,Mr},N:function(u){ja(u>>>0,!I,1,!$,131072,!1),te.pb()},l:function(u){u>>>=0,v?postMessage({cmd:"cleanupThread",thread:u}):te.nb(te.Qa[u])},J:Nr,i:Wr,T:Vr,F:mr,H:er,U:hr,R:gr,L:yr,Q:br,p:wr,G:vr,D:$r,S:_r,E:xr,q:()=>1,B:function(u,p){u>>>=0,u==p>>>0?setTimeout(()=>Wt()):v?postMessage({targetThread:u,cmd:"checkMailbox"}):(u=te.Qa[u])&&u.postMessage({cmd:"checkMailbox"})},K:function(u,p,y,x){p>>>=0,d.length=y,x=x>>>0>>>3;for(var T=0;T>>0];return u=0>u?Zt[-u-1]:od[u],te.vb=p,p=u.apply(null,d),te.vb=0,p},M:Rt,W:function(u){S&&te.Qa[u>>>0].ref()},u:function(u,p,y){u=p+2097152>>>0<4194305-!!u?(u>>>0)+4294967296*p:NaN,y>>>=0,u=new Date(1e3*u),n()[y>>>2>>>0]=u.getUTCSeconds(),n()[y+4>>>2>>>0]=u.getUTCMinutes(),n()[y+8>>>2>>>0]=u.getUTCHours(),n()[y+12>>>2>>>0]=u.getUTCDate(),n()[y+16>>>2>>>0]=u.getUTCMonth(),n()[y+20>>>2>>>0]=u.getUTCFullYear()-1900,n()[y+24>>>2>>>0]=u.getUTCDay(),u=(u.getTime()-Date.UTC(u.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,n()[y+28>>>2>>>0]=u},v:function(u,p,y){u=p+2097152>>>0<4194305-!!u?(u>>>0)+4294967296*p:NaN,y>>>=0,u=new Date(1e3*u),n()[y>>>2>>>0]=u.getSeconds(),n()[y+4>>>2>>>0]=u.getMinutes(),n()[y+8>>>2>>>0]=u.getHours(),n()[y+12>>>2>>>0]=u.getDate(),n()[y+16>>>2>>>0]=u.getMonth(),n()[y+20>>>2>>>0]=u.getFullYear()-1900,n()[y+24>>>2>>>0]=u.getDay(),p=(g(u.getFullYear())?_:C)[u.getMonth()]+u.getDate()-1|0,n()[y+28>>>2>>>0]=p,n()[y+36>>>2>>>0]=-(60*u.getTimezoneOffset()),p=new Date(u.getFullYear(),6,1).getTimezoneOffset();var x=new Date(u.getFullYear(),0,1).getTimezoneOffset();u=(p!=x&&u.getTimezoneOffset()==Math.min(x,p))|0,n()[y+32>>>2>>>0]=u},w:function(u){u>>>=0;var p=new Date(n()[u+20>>>2>>>0]+1900,n()[u+16>>>2>>>0],n()[u+12>>>2>>>0],n()[u+8>>>2>>>0],n()[u+4>>>2>>>0],n()[u>>>2>>>0],0),y=n()[u+32>>>2>>>0],x=p.getTimezoneOffset(),T=new Date(p.getFullYear(),6,1).getTimezoneOffset(),R=new Date(p.getFullYear(),0,1).getTimezoneOffset(),V=Math.min(R,T);return 0>y?n()[u+32>>>2>>>0]=+(T!=R&&V==x):0>>2>>>0]=p.getDay(),y=(g(p.getFullYear())?_:C)[p.getMonth()]+p.getDate()-1|0,n()[u+28>>>2>>>0]=y,n()[u>>>2>>>0]=p.getSeconds(),n()[u+4>>>2>>>0]=p.getMinutes(),n()[u+8>>>2>>>0]=p.getHours(),n()[u+12>>>2>>>0]=p.getDate(),n()[u+16>>>2>>>0]=p.getMonth(),n()[u+20>>>2>>>0]=p.getYear(),u=p.getTime(),isNaN(u)?(n()[Fa()>>>2>>>0]=61,u=-1):u/=1e3,Za((At=u,1<=+Math.abs(At)?0>>0:~~+Math.ceil((At-+(~~At>>>0))/4294967296)>>>0:0)),u>>>0},s:O,t:W,A:function(u,p,y){function x(de){return(de=de.toTimeString().match(/\(([A-Za-z ]+)\)$/))?de[1]:"GMT"}u>>>=0,p>>>=0,y>>>=0;var T=new Date().getFullYear(),R=new Date(T,0,1),V=new Date(T,6,1);T=R.getTimezoneOffset();var Z=V.getTimezoneOffset(),ge=Math.max(T,Z);o()[u>>>2>>>0]=60*ge,n()[p>>>2>>>0]=+(T!=Z),u=x(R),p=x(V),u=j(u),p=j(p),Z>>2>>>0]=u,o()[y+4>>>2>>>0]=p):(o()[y>>>2>>>0]=p,o()[y+4>>>2>>>0]=u)},d:()=>{tt("")},c:function(u,p,y){return u>>>=0,p=oe(p>>>0,y>>>0),Zt[u].apply(null,p)},k:function(u,p,y){return u>>>=0,p=oe(p>>>0,y>>>0),Zt[u].apply(null,p)},m:()=>{},j:()=>Date.now(),V:()=>{throw pt+=1,"unwind"},C:function(){return 4294901760},f:()=>performance.timeOrigin+performance.now(),g:()=>S?(Vi(),Gt(Wi)).cpus().length:navigator.hardwareConcurrency,y:function(u){u>>>=0;var p=a().length;if(u<=p||4294901760=y;y*=2){var x=p*(1+.2/y);x=Math.min(x,u+100663296);var T=Math;x=Math.max(u,x);e:{T=(T.min.call(T,4294901760,x+(65536-x%65536)%65536)-ne.buffer.byteLength+65535)/65536;try{ne.grow(T),G();var R=1;break e}catch{}R=void 0}if(R)return!0}return!1},O:ze,P,I:Jt,h:ae,o:Ce,x:bt,n:Ua,a:ne||i.wasmMemory,z:Va,e:function(u,p,y,x){return Va(u>>>0,p>>>0,y>>>0,x>>>0)}},Y=function(){function u(y,x){return Y=y.exports,Y=Ql(),Y=ud(),te.qb.push(Y.Da),Je.unshift(Y.X),Ue=x,qe(),Y}var p={a:sd};if(Fe++,i.instantiateWasm)try{return i.instantiateWasm(p,u)}catch(y){we(`Module.instantiateWasm callback failed with error: ${y}`),f(y)}return kt(p,function(y){u(y.instance,y.module)}).catch(f),{}}();i._OrtInit=(u,p)=>(i._OrtInit=Y.Y)(u,p),i._OrtGetLastError=(u,p)=>(i._OrtGetLastError=Y.Z)(u,p),i._OrtCreateSessionOptions=(u,p,y,x,T,R,V,Z,ge,de)=>(i._OrtCreateSessionOptions=Y._)(u,p,y,x,T,R,V,Z,ge,de),i._OrtAppendExecutionProvider=(u,p)=>(i._OrtAppendExecutionProvider=Y.$)(u,p),i._OrtAddFreeDimensionOverride=(u,p,y)=>(i._OrtAddFreeDimensionOverride=Y.aa)(u,p,y),i._OrtAddSessionConfigEntry=(u,p,y)=>(i._OrtAddSessionConfigEntry=Y.ba)(u,p,y),i._OrtReleaseSessionOptions=u=>(i._OrtReleaseSessionOptions=Y.ca)(u),i._OrtCreateSession=(u,p,y)=>(i._OrtCreateSession=Y.da)(u,p,y),i._OrtReleaseSession=u=>(i._OrtReleaseSession=Y.ea)(u),i._OrtGetInputOutputCount=(u,p,y)=>(i._OrtGetInputOutputCount=Y.fa)(u,p,y),i._OrtGetInputName=(u,p)=>(i._OrtGetInputName=Y.ga)(u,p),i._OrtGetOutputName=(u,p)=>(i._OrtGetOutputName=Y.ha)(u,p),i._OrtFree=u=>(i._OrtFree=Y.ia)(u),i._OrtCreateTensor=(u,p,y,x,T,R)=>(i._OrtCreateTensor=Y.ja)(u,p,y,x,T,R),i._OrtGetTensorData=(u,p,y,x,T)=>(i._OrtGetTensorData=Y.ka)(u,p,y,x,T),i._OrtReleaseTensor=u=>(i._OrtReleaseTensor=Y.la)(u),i._OrtCreateRunOptions=(u,p,y,x)=>(i._OrtCreateRunOptions=Y.ma)(u,p,y,x),i._OrtAddRunConfigEntry=(u,p,y)=>(i._OrtAddRunConfigEntry=Y.na)(u,p,y),i._OrtReleaseRunOptions=u=>(i._OrtReleaseRunOptions=Y.oa)(u),i._OrtCreateBinding=u=>(i._OrtCreateBinding=Y.pa)(u),i._OrtBindInput=(u,p,y)=>(i._OrtBindInput=Y.qa)(u,p,y),i._OrtBindOutput=(u,p,y,x)=>(i._OrtBindOutput=Y.ra)(u,p,y,x),i._OrtClearBoundOutputs=u=>(i._OrtClearBoundOutputs=Y.sa)(u),i._OrtReleaseBinding=u=>(i._OrtReleaseBinding=Y.ta)(u),i._OrtRunWithBinding=(u,p,y,x,T)=>(i._OrtRunWithBinding=Y.ua)(u,p,y,x,T),i._OrtRun=(u,p,y,x,T,R,V,Z)=>(i._OrtRun=Y.va)(u,p,y,x,T,R,V,Z),i._OrtEndProfiling=u=>(i._OrtEndProfiling=Y.wa)(u),i._JsepOutput=(u,p,y)=>(i._JsepOutput=Y.xa)(u,p,y),i._JsepGetNodeName=u=>(i._JsepGetNodeName=Y.ya)(u);var Fa=()=>(Fa=Y.za)(),jr=i._pthread_self=()=>(jr=i._pthread_self=Y.Aa)(),Wn=i._malloc=u=>(Wn=i._malloc=Y.Ba)(u),qa=i._free=u=>(qa=i._free=Y.Ca)(u);i.__emscripten_tls_init=()=>(i.__emscripten_tls_init=Y.Da)();var ja=i.__emscripten_thread_init=(u,p,y,x,T,R)=>(ja=i.__emscripten_thread_init=Y.Fa)(u,p,y,x,T,R);i.__emscripten_thread_crashed=()=>(i.__emscripten_thread_crashed=Y.Ga)();var Ka=(u,p,y,x)=>(Ka=Y.Ha)(u,p,y,x),Vn=u=>(Vn=Y.Ia)(u),Gn=i.__emscripten_thread_exit=u=>(Gn=i.__emscripten_thread_exit=Y.Ja)(u),Ya=()=>(Ya=Y.Ka)(),Za=u=>(Za=Y.La)(u),Xa=(u,p)=>(Xa=Y.Ma)(u,p),Hn=()=>(Hn=Y.Na)(),Kr=u=>(Kr=Y.Oa)(u),Ln=u=>(Ln=Y.Pa)(u),Qa=i.dynCall_ii=(u,p)=>(Qa=i.dynCall_ii=Y.Ra)(u,p),Ja=u=>(Ja=Y.Sa)(u),ei=()=>(ei=Y.Ta)(),ti=u=>(ti=Y.Ua)(u),ri=()=>(ri=Y.Va)();i.___start_em_js=945539,i.___stop_em_js=945700;function ud(){var u=Y;u=Object.assign({},u);var p=x=>()=>x()>>>0,y=x=>T=>x(T)>>>0;return u.za=p(u.za),u.Aa=p(u.Aa),u.Ba=y(u.Ba),u.emscripten_main_runtime_thread_id=p(u.emscripten_main_runtime_thread_id),u.Na=p(u.Na),u.Pa=y(u.Pa),u}i.wasmMemory=ne,i.stackAlloc=Ln,i.stackSave=Hn,i.stackRestore=Kr,i.keepRuntimeAlive=()=>0Gi)});var Fi=tr((fm,$d)=>{$d.exports='"use strict";var Module={},ENVIRONMENT_IS_NODE=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string";if(ENVIRONMENT_IS_NODE){var nodeWorkerThreads=require("worker_threads"),parentPort=nodeWorkerThreads.parentPort;parentPort.on("message",e=>onmessage({data:e}));var fs=require("fs"),vm=require("vm");Object.assign(global,{self:global,require,Module,location:{href:__filename},Worker:nodeWorkerThreads.Worker,importScripts:e=>vm.runInThisContext(fs.readFileSync(e,"utf8"),{filename:e}),postMessage:e=>parentPort.postMessage(e),performance:global.performance||{now:Date.now}})}var initializedJS=!1;function threadPrintErr(){var e=Array.prototype.slice.call(arguments).join(" ");if(ENVIRONMENT_IS_NODE){fs.writeSync(2,e+`\n`);return}console.error(e)}function threadAlert(){var e=Array.prototype.slice.call(arguments).join(" ");postMessage({cmd:"alert",text:e,threadId:Module._pthread_self()})}var err=threadPrintErr;self.alert=threadAlert,Module.instantiateWasm=(e,t)=>{var a=Module.wasmModule;Module.wasmModule=null;var r=new WebAssembly.Instance(a,e);return t(r)},self.onunhandledrejection=e=>{throw e.reason||e};function handleMessage(e){try{if(e.data.cmd==="load"){let a=[];self.onmessage=r=>a.push(r),self.startWorker=r=>{Module=r,postMessage({cmd:"loaded"});for(let s of a)handleMessage(s);self.onmessage=handleMessage},Module.wasmModule=e.data.wasmModule;for(const r of e.data.handlers)Module[r]=(...s)=>{postMessage({cmd:"callHandler",handler:r,args:s})};if(Module.wasmMemory=e.data.wasmMemory,Module.buffer=Module.wasmMemory.buffer,Module.ENVIRONMENT_IS_PTHREAD=!0,typeof e.data.urlOrBlob=="string")importScripts(e.data.urlOrBlob);else{var t=URL.createObjectURL(e.data.urlOrBlob);importScripts(t),URL.revokeObjectURL(t)}ortWasmThreaded(Module)}else if(e.data.cmd==="run"){Module.__emscripten_thread_init(e.data.pthread_ptr,0,0,1),Module.__emscripten_thread_mailbox_await(e.data.pthread_ptr),Module.establishStackSpace(),Module.PThread.receiveObjectTransfer(e.data),Module.PThread.threadInitTLS(),initializedJS||(initializedJS=!0);try{Module.invokeEntryPoint(e.data.start_routine,e.data.arg)}catch(a){if(a!="unwind")throw a}}else e.data.cmd==="cancel"?Module._pthread_self()&&Module.__emscripten_thread_exit(-1):e.data.target==="setimmediate"||(e.data.cmd==="checkMailbox"?initializedJS&&Module.checkMailbox():e.data.cmd&&(err(`worker.js received unknown command ${e.data.cmd}`),err(e.data)))}catch(a){throw Module.__emscripten_thread_crashed?.(),a}}self.onmessage=handleMessage;\n'});var ji,_d,ra,na,an,qi,xd,Sd,Cd,Ki,Ge,nr=F(()=>{"use strict";ji=Mi();_d=Li(),na=!1,an=!1,qi=!1,xd=e=>{if(e===1)return!1;if(typeof SharedArrayBuffer>"u")return typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+e+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),!1;typeof process<"u"&&process.versions&&process.versions.node&&console.warn("env.wasm.numThreads is set to "+e+", however, currently onnxruntime-web does not support multi-threads in Node.js. Please consider using onnxruntime-node for performance critical scenarios.");try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},Sd=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},Cd=(e,t)=>e?t?"ort-wasm-simd-threaded.wasm":"ort-wasm-simd.wasm":t?"ort-wasm-threaded.wasm":"ort-wasm.wasm",Ki=async e=>{if(na)return Promise.resolve();if(an)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(qi)throw new Error("previous call to 'initializeWebAssembly()' failed.");an=!0;let t=e.initTimeout,r=e.numThreads,a=e.simd,n=xd(r),o=a&&Sd(),s=e.wasmPaths,l=typeof s=="string"?s:void 0,i=Cd(o,n),c=typeof s=="object"?s[i]:void 0,f=!1,m=[];if(t>0&&m.push(new Promise(h=>{setTimeout(()=>{f=!0,h()},t)})),m.push(new Promise((h,w)=>{let b=n?_d:ji,$={locateFile:(I,S)=>{if(n&&I.endsWith(".worker.js")&&typeof Blob<"u")return URL.createObjectURL(new Blob([Fi()],{type:"text/javascript"}));if(I.endsWith(".wasm")){if(c)return c;let v=l??S;return i==="ort-wasm-simd.wasm"?v+"ort-wasm-simd.jsep.wasm":i==="ort-wasm-simd-threaded.wasm"?v+"ort-wasm-simd-threaded.jsep.wasm":v+i}return S+I}};if(n)if($.numThreads=r,typeof Blob>"u")$.mainScriptUrlOrBlob=(void 0)(__dirname,"ort-wasm-threaded.js");else{let I=`var ortWasmThreaded=${b.toString()};`;$.mainScriptUrlOrBlob=new Blob([I],{type:"text/javascript"})}b($).then(I=>{an=!1,na=!0,ra=I,h()},I=>{an=!1,qi=!0,w(I)})})),await Promise.race(m),f)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},Ge=()=>{if(na&&ra)return ra;throw new Error("WebAssembly is not initialized yet.")}});var He,Cr,Be,on=F(()=>{"use strict";nr();He=(e,t)=>{let r=Ge(),a=r.lengthBytesUTF8(e)+1,n=r._malloc(a);return r.stringToUTF8(e,n,a),t.push(n),n},Cr=(e,t,r,a)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([n,o])=>{let s=t?t+n:n;if(typeof o=="object")Cr(o,s+".",r,a);else if(typeof o=="string"||typeof o=="number")a(s,o.toString());else if(typeof o=="boolean")a(s,o?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof o}`)})},Be=e=>{let t=Ge(),r=t.stackSave();try{let a=t.stackAlloc(8);t._OrtGetLastError(a,a+4);let n=t.HEAP32[a/4],o=t.HEAPU32[a/4+1],s=o?t.UTF8ToString(o):"";throw new Error(`${e} ERROR_CODE: ${n}, ERROR_MESSAGE: ${s}`)}finally{t.stackRestore(r)}}});var Yi,Zi=F(()=>{"use strict";nr();on();Yi=e=>{let t=Ge(),r=0,a=[],n=e||{};try{if(e?.logSeverityLevel===void 0)n.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log serverity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)n.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(n.terminate=!1);let o=0;return e?.tag!==void 0&&(o=He(e.tag,a)),r=t._OrtCreateRunOptions(n.logSeverityLevel,n.logVerbosityLevel,!!n.terminate,o),r===0&&Be("Can't create run options."),e?.extra!==void 0&&Cr(e.extra,"",new WeakSet,(s,l)=>{let i=He(s,a),c=He(l,a);t._OrtAddRunConfigEntry(r,i,c)!==0&&Be(`Can't set a run config entry: ${s} - ${l}.`)}),[r,a]}catch(o){throw r!==0&&t._OrtReleaseRunOptions(r),a.forEach(s=>t._free(s)),o}}});var Id,Ed,Ad,Td,Xi,Qi=F(()=>{"use strict";nr();on();Id=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},Ed=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},Ad=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(r=>(typeof r=="string"?r:r.name)==="webgpu")&&(e.enableMemPattern=!1)},Td=(e,t,r)=>{for(let a of t){let n=typeof a=="string"?a:a.name;switch(n){case"webnn":if(n="WEBNN",typeof a!="string"){let s=a;if(s?.deviceType){let l=He("deviceType",r),i=He(s.deviceType,r);Ge()._OrtAddSessionConfigEntry(e,l,i)!==0&&Be(`Can't set a session config entry: 'deviceType' - ${s.deviceType}.`)}if(s?.numThreads){let l=s.numThreads;(typeof l!="number"||!Number.isInteger(l)||l<0)&&(l=0);let i=He("numThreads",r),c=He(l.toString(),r);Ge()._OrtAddSessionConfigEntry(e,i,c)!==0&&Be(`Can't set a session config entry: 'numThreads' - ${s.numThreads}.`)}if(s?.powerPreference){let l=He("powerPreference",r),i=He(s.powerPreference,r);Ge()._OrtAddSessionConfigEntry(e,l,i)!==0&&Be(`Can't set a session config entry: 'powerPreference' - ${s.powerPreference}.`)}}break;case"webgpu":if(n="JS",typeof a!="string"){let s=a;if(s?.preferredLayout){if(s.preferredLayout!=="NCHW"&&s.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${s.preferredLayout}`);let l=He("preferredLayout",r),i=He(s.preferredLayout,r);Ge()._OrtAddSessionConfigEntry(e,l,i)!==0&&Be(`Can't set a session config entry: 'preferredLayout' - ${s.preferredLayout}.`)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${n}`)}let o=He(n,r);Ge()._OrtAppendExecutionProvider(e,o)!==0&&Be(`Can't append execution provider: ${n}.`)}},Xi=e=>{let t=Ge(),r=0,a=[],n=e||{};Ad(n);try{let o=Id(n.graphOptimizationLevel??"all"),s=Ed(n.executionMode??"sequential"),l=typeof n.logId=="string"?He(n.logId,a):0,i=n.logSeverityLevel??2;if(!Number.isInteger(i)||i<0||i>4)throw new Error(`log serverity level is not valid: ${i}`);let c=n.logVerbosityLevel??0;if(!Number.isInteger(c)||c<0||c>4)throw new Error(`log verbosity level is not valid: ${c}`);let f=typeof n.optimizedModelFilePath=="string"?He(n.optimizedModelFilePath,a):0;if(r=t._OrtCreateSessionOptions(o,!!n.enableCpuMemArena,!!n.enableMemPattern,s,!!n.enableProfiling,0,l,i,c,f),r===0&&Be("Can't create session options."),n.executionProviders&&Td(r,n.executionProviders,a),n.enableGraphCapture!==void 0){if(typeof n.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${n.enableGraphCapture}`);let m=He("enableGraphCapture",a),h=He(n.enableGraphCapture.toString(),a);t._OrtAddSessionConfigEntry(r,m,h)!==0&&Be(`Can't set a session config entry: 'enableGraphCapture' - ${n.enableGraphCapture}.`)}if(n.freeDimensionOverrides)for(let[m,h]of Object.entries(n.freeDimensionOverrides)){if(typeof m!="string")throw new Error(`free dimension override name must be a string: ${m}`);if(typeof h!="number"||!Number.isInteger(h)||h<0)throw new Error(`free dimension override value must be a non-negative integer: ${h}`);let w=He(m,a);t._OrtAddFreeDimensionOverride(r,w,h)!==0&&Be(`Can't set a free dimension override: ${m} - ${h}.`)}return n.extra!==void 0&&Cr(n.extra,"",new WeakSet,(m,h)=>{let w=He(m,a),b=He(h,a);t._OrtAddSessionConfigEntry(r,w,b)!==0&&Be(`Can't set a session config entry: ${m} - ${h}.`)}),[r,a]}catch(o){throw r!==0&&t._OrtReleaseSessionOptions(r),a.forEach(s=>t._free(s)),o}}});var aa,Bt,Ir,sn,Er,un,ia,ie=F(()=>{"use strict";aa=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;default:throw new Error(`unsupported data type: ${e}`)}},Bt=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";default:throw new Error(`unsupported data type: ${e}`)}},Ir=e=>[void 0,4,1,1,2,2,4,8,void 0,1,2,8,4,8,void 0,void 0,void 0][e],sn=e=>{switch(e){case"float16":return typeof Float16Array<"u"&&Float16Array.from?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},Er=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},un=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool",ia=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;default:throw new Error(`unsupported data location: ${e}`)}}});var Ar,oa=F(()=>{"use strict";Ar=async e=>{if(typeof e=="string")if(typeof process<"u"&&process.versions&&process.versions.node)try{return new Uint8Array(await(void 0)(e))}catch(t){if(t.code==="ERR_FS_FILE_TOO_LARGE"){let r=(void 0)(e),a=[];for await(let n of r)a.push(n);return new Uint8Array(Buffer.concat(a))}throw t}else{let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),a=r?parseInt(r,10):0;if(a<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let n=t.body.getReader(),o;try{o=new ArrayBuffer(a)}catch(l){if(l instanceof RangeError){let i=Math.ceil(a/65536);o=new WebAssembly.Memory({initial:i,maximum:i}).buffer}else throw l}let s=0;for(;;){let{done:l,value:i}=await n.read();if(l)break;let c=i.byteLength;new Uint8Array(o,s,c).set(i),s+=c}return new Uint8Array(o,0,a)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}});var Od,kd,Ji,eo,to,Pd,De,Dt=F(()=>{"use strict";ie();Od=["V","I","W","E","F"],kd=(e,t)=>{console.log(`[${Od[e]},${new Date().toISOString()}]${t}`)},to=(e,t)=>{Ji=e,eo=t},Pd=(e,t)=>{let r=Er(e),a=Er(Ji);r>=a&&kd(r,typeof t=="function"?t():t)},De=(...e)=>{eo&&Pd(...e)}});var ro,no=F(()=>{"use strict";ie();ro=(e,t)=>new(sn(t))(e)});var ln=F(()=>{"use strict"});var dn,Rd,ao,ua,sa,oo,so=F(()=>{"use strict";Dt();ln();dn=e=>Math.ceil(e/16)*16,Rd=1,ao=()=>Rd++,ua=async(e,t,r,a)=>{let n=dn(r),o=e.device.createBuffer({size:n,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let s=e.getCommandEncoder();e.endComputePass(),s.copyBufferToBuffer(t,0,o,0,n),e.flush(),await o.mapAsync(GPUMapMode.READ);let l=o.getMappedRange();if(a){let i=a();return i.set(new Uint8Array(l,0,r)),i}else return new Uint8Array(l.slice(0,r))}finally{o.destroy()}},sa=class{constructor(t){this.backend=t;this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersForUploadingPending=[],this.buffersPending=[],this.externalBuffers=new Map,this.capturedPendingBuffers=new Map}upload(t,r){let a=r.buffer,n=r.byteOffset,o=r.byteLength,s=dn(o),l=this.storageCache.get(t);if(!l)throw new Error("gpu data for uploading does not exist");if(l.originalSize!==o)throw new Error(`inconsistent data size. gpu data size=${l.originalSize}, data size=${o}`);let i=this.backend.device.createBuffer({mappedAtCreation:!0,size:s,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),c=i.getMappedRange();new Uint8Array(c).set(new Uint8Array(a,n,o)),i.unmap();let f=this.backend.getCommandEncoder();this.backend.endComputePass(),f.copyBufferToBuffer(i,0,l.gpuData.buffer,0,s),De("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${t})`),this.buffersForUploadingPending.push(i)}memcpy(t,r){let a=this.storageCache.get(t);if(!a)throw new Error("source gpu data for memcpy does not exist");let n=this.storageCache.get(r);if(!n)throw new Error("destination gpu data for memcpy does not exist");if(a.originalSize!==n.originalSize)throw new Error("inconsistent source and destination gpu data size");let o=dn(a.originalSize),s=this.backend.getCommandEncoder();this.backend.endComputePass(),s.copyBufferToBuffer(a.gpuData.buffer,0,n.gpuData.buffer,0,o)}registerExternalBuffer(t,r,a){let n;if(a){if(n=this.externalBuffers.get(a),n===void 0)throw new Error("previous buffer is not registered");if(t===a)return De("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${n}, buffer is the same, skip.`),n;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. Please use the previous external buffer!`);this.externalBuffers.delete(a)}else n=ao();return this.storageCache.set(n,{gpuData:{id:n,type:0,buffer:t},originalSize:r}),this.externalBuffers.set(t,n),De("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${n}, registered.`),n}unregisterExternalBuffer(t){let r=this.externalBuffers.get(t);r!==void 0&&(this.storageCache.delete(r),this.externalBuffers.delete(t),De("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${r}`))}create(t,r=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let a=dn(t),n,o=(r&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,s=(r&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(o||s){let i=o?this.freeBuffers:this.freeUniformBuffers,c=i.get(a);c||(c=[],i.set(a,c)),c.length>0?n=c.pop():n=this.backend.device.createBuffer({size:a,usage:r})}else n=this.backend.device.createBuffer({size:a,usage:r});let l={id:ao(),type:0,buffer:n};return this.storageCache.set(l.id,{gpuData:l,originalSize:t}),De("verbose",()=>`[WebGPU] GpuDataManager.create(size=${t}) => id=${l.id}`),l}get(t){return this.storageCache.get(t)?.gpuData}release(t){let r=this.storageCache.get(t);if(!r)throw new Error("releasing data does not exist");return De("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(r.gpuData.buffer),r.originalSize}async download(t,r){let a=this.storageCache.get(t);if(!a)throw new Error("data does not exist");await ua(this.backend,a.gpuData.buffer,a.originalSize,r)}refreshPendingBuffers(){for(let t of this.buffersForUploadingPending)t.destroy();if(this.buffersForUploadingPending=[],this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let t of this.buffersPending)(t.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE?this.freeBuffers.get(t.size).push(t):(t.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM?this.freeUniformBuffers.get(t.size).push(t):t.destroy();this.buffersPending=[]}else{let t=this.capturedPendingBuffers.get(this.backend.currentSessionId);t||(t=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,t));for(let r of this.buffersPending)t.push(r);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.freeUniformBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onReleaseSession(t){let r=this.capturedPendingBuffers.get(t);r&&(r.forEach(a=>{a.destroy()}),this.capturedPendingBuffers.delete(t))}},oo=(...e)=>new sa(...e)});var la,$e,Ye=F(()=>{"use strict";la=class{constructor(t){Object.assign(this,t)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(t=>`${this[t]}`).join(";")),this.key}},$e=e=>new la(e)});var da,vt,M,qt,cn,pn,fn,_e=F(()=>{"use strict";da=class{static calcMatMulShape(t,r){return t[1]!==r[0]?void 0:[t[0],r[1]]}},vt=class{static calcShape(t,r,a=!1){let n=t.length,o=r.length;if(n===0)return r;if(o===0)return t;let s=Math.max(t.length,r.length),l=new Array(s);if(a){if(n<2||o<2)return;let i=da.calcMatMulShape([t[n-2],t[n-1]],[r[o-2],r[o-1]]);if(i===void 0)return;[l[s-2],l[s-1]]=i}for(let i=a?3:1;i<=s;i++){let c=n-i<0?1:t[n-i],f=o-i<0?1:r[o-i];if(c!==f&&c>1&&f>1)return;let m=Math.max(c,f);if(c&&f)l[s-i]=Math.max(c,f);else{if(m>1)return;l[s-i]=0}}return l}static isValidBroadcast(t,r){let a=t.length,n=r.length;if(a>n)return!1;for(let o=1;o<=a;o++)if(t[a-o]!==1&&t[a-o]!==r[n-o])return!1;return!0}},M=class e{static size(t){return e.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let a=t.length;if(a===0)return[];let n=new Array(a),o=a-1;for(;o>=0;){if(t[o]%r===0){n[o]=t[o]/r;break}if(r%t[o]!==0)throw new Error("cannot convert shape");n[o]=1,r/=t[o],o--}for(o--;o>=0;o--)n[o]=t[o];return n}static sizeFromDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,r,t.length)}static sizeToDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,a){let n=1;for(let o=r;o=0;--n)a[n]=a[n+1]*t[n+1];return a}static normalizeAxis(t,r){if(t<-r&&t>=r)throw new Error("unsupported axis for this operation.");return t<0?t+r:t}static normalizeAxes(t,r){return t.map(a=>this.normalizeAxis(a,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(a=>t[a]):t.slice().reverse()}static padShape(t,r){let a=t.length;return t.map((n,o)=>n+r[o]+r[o+a])}static areEqual(t,r){return t.length!==r.length?!1:t.every((a,n)=>a===r[n])}},qt=class e{static adjustPoolAttributes(t,r,a,n,o,s){if(!t&&a.length!==r.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let l=0;l=a.length?a.push(r[l+2]):a[l]=r[l+2];for(let l=0;l=a[l]||s[l+a.length]>=a[l])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,a,n,o,s,l){if(l){if(o.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(r.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let i=0;i{"use strict";ie();_e();mn=64,pa=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(e){case 10:return t>1?`vec${t}`:"f16";case 1:return t>1?`vec${t}`:"f32";case 6:return t>1?`vec${t}`:"i32";case 12:return t>1?`vec${t}`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4"];default:throw new Error(`Unknown data type: ${e}`)}},ke=(e,t=1)=>{let r=pa(e,t);return typeof r=="string"?r:r[0]},at=(e,t=1)=>{let r=pa(e,t);return typeof r=="string"?r:r[1]},K=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:M.computeStrides(r)})}),t},Me=e=>e%4===0?4:e%2===0?2:1,Xe=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,mt=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}f(${r})`,it=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,re=(e,t,r,a)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?a==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:a==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,fa=(e,t,r,a,n)=>{let o=typeof r=="number",s=o?r:r.length,l=[...new Array(s).keys()],i=s<2?"u32":s<=4?`vec${s}`:`array`,c=pa(t,n),f=typeof c=="string"?c:c[1],m=typeof c=="string"?c:c[0],h={indices:i,value:f,storage:m,tensor:t},w=G=>typeof G=="string"?G:`${G}u`,b={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},$=o?"uniforms.":"",I=`${$}${e}_shape`,S=`${$}${e}_strides`,v="";for(let G=0;G ${h.indices} { var indices: ${h.indices}; var current = offset; ${v} return indices; }`,A=G=>(b.offsetToIndices=!0,s<2?G:`o2i_${e}(${G})`),k=[];if(s>=2)for(let G=s-1;G>=0;G--)k.push(`${re(S,G,s)} * (indices[${G}])`);let U=s<2?"":` fn i2o_${e}(indices: ${h.indices}) -> u32 { return ${k.join("+")}; }`,N=G=>(b.indicesToOffset=!0,s<2?G:`i2o_${e}(${G})`),H=(...G)=>s===0?"0u":`${h.indices}(${G.map(w).join(",")})`,z=(G,J)=>s<2?`${G}`:`${re(G,J,s)}`,L=(G,J,Se)=>s<2?`${G}=${Se};`:`${re(G,J,s)}=${Se};`,pe={},Ie=(G,J)=>{b.broadcastedIndicesToOffset=!0;let Se=`${J.name}broadcastedIndicesTo${e}Offset`;if(Se in pe)return`${Se}(${G})`;let Je=[];for(let et=s-1;et>=0;et--){let Fe=J.indicesGet("outputIndices",et+J.rank-s);Je.push(`${z(S,et)} * (${Fe} % ${z(I,et)})`)}return pe[Se]=`fn ${Se}(outputIndices: ${J.type.indices}) -> u32 { return ${Je.length>0?Je.join("+"):"0u"}; }`,`${Se}(${G})`},we=(G,J)=>(()=>{if(h.storage===h.value)return`${e}[${G}]=${J};`;if(h.storage==="vec2"&&h.value==="i32")return`${e}[${G}]=vec2(u32(${J}), select(0u, 0xFFFFFFFFu, ${J} < 0));`;if(h.storage==="vec2"&&h.value==="u32")return`${e}[${G}]=vec2(u32(${J}), 0u);`;if(h.storage==="u32"&&h.value==="vec4")return`${e}[${G}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${J}));`;throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`)})(),ne=G=>(()=>{if(h.storage===h.value)return`${e}[${G}]`;if(h.storage==="vec2"&&h.value==="i32")return`i32(${e}[${G}].x)`;if(h.storage==="vec2"&&h.value==="u32")return`u32(${e}[${G}].x)`;if(h.storage==="u32"&&h.value==="vec4")return`vec4(bool(${e}[${G}] & 0xFFu), bool(${e}[${G}] & 0xFF00u), bool(${e}[${G}] & 0xFF0000u), bool(${e}[${G}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`)})(),Ue=s<2?"":` fn get_${e}ByIndices(indices: ${h.indices}) -> ${f} { return ${ne(`i2o_${e}(indices)`)}; }`,X=s<2?"":(()=>{let G=l.map(Se=>`d${Se}: u32`).join(", "),J=l.map(Se=>`d${Se}`).join(", ");return` fn get_${e}(${G}) -> ${f} { return get_${e}ByIndices(${H(J)}); }`})(),xe=(...G)=>{if(G.length!==s)throw new Error(`indices length must be ${s}`);let J=G.map(w).join(",");return s===0?ne("0u"):s===1?ne(J[0]):(b.get=!0,b.getByIndices=!0,b.indicesToOffset=!0,`get_${e}(${J})`)},fe=G=>s<2?ne(G):(b.getByIndices=!0,b.indicesToOffset=!0,`get_${e}ByIndices(${G})`),ue=s<2?"":` fn set_${e}ByIndices(indices: ${h.indices}, value: ${f}) { ${we(`i2o_${e}(indices)`,"value")} }`,se=s<2?"":(()=>{let G=l.map(Se=>`d${Se}: u32`).join(", "),J=l.map(Se=>`d${Se}`).join(", ");return` fn set_${e}(${G}, value: ${f}) { set_${e}ByIndices(${H(J)}, value); }`})();return{impl:()=>{let G=[],J=!1;return b.offsetToIndices&&(G.push(E),J=!0),b.indicesToOffset&&(G.push(U),J=!0),b.broadcastedIndicesToOffset&&(Object.values(pe).forEach(Se=>G.push(Se)),J=!0),b.set&&(G.push(se),J=!0),b.setByIndices&&(G.push(ue),J=!0),b.get&&(G.push(X),J=!0),b.getByIndices&&(G.push(Ue),J=!0),!o&&J&&G.unshift(`const ${I} = ${h.indices}(${r.join(",")});`,`const ${S} = ${h.indices}(${M.computeStrides(r).join(",")});`),G.join(` `)},type:h,offsetToIndices:A,indicesToOffset:N,broadcastedIndicesToOffset:Ie,indices:H,indicesGet:z,indicesSet:L,set:(...G)=>{if(G.length!==s+1)throw new Error(`indices length must be ${s}`);let J=G[s];if(typeof J!="string")throw new Error("value must be string");let Se=G.slice(0,s).map(w).join(",");return s===0?we("0u",J):s===1?we(Se[0],J):(b.set=!0,b.setByIndices=!0,b.indicesToOffset=!0,`set_${e}(${Se}, ${J})`)},setByOffset:we,setByIndices:(G,J)=>s<2?we(G,J):(b.setByIndices=!0,b.indicesToOffset=!0,`set_${e}ByIndices(${G}, ${J});`),get:xe,getByOffset:ne,getByIndices:fe,usage:a,name:e,strides:S,shape:I,rank:s}},D=(e,t,r,a=1)=>fa(e,t,r,"input",a),q=(e,t,r,a=1)=>fa(e,t,r,"output",a),hn=(e,t,r,a=1)=>fa(e,t,r,"internal",a),ca=class{constructor(t){this.normalizedDispatchGroup=t;this.internalVariables=[];this.variables=[];this.uniforms=[];this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(t){return`if (global_idx >= ${typeof t=="number"?`${t}u`:t}) { return; }`}mainStart(t=mn){let r=typeof t=="number"?t:t[0],a=typeof t=="number"?1:t[1],n=typeof t=="number"?1:t[2],o=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,s=o?`@builtin(global_invocation_id) global_id : vec3, @builtin(workgroup_id) workgroup_id : vec3, @builtin(local_invocation_id) local_id : vec3`:`@builtin(local_invocation_id) local_id : vec3, @builtin(local_invocation_index) local_idx : u32, @builtin(workgroup_id) workgroup_id : vec3, @builtin(num_workgroups) num_workgroups : vec3`,l=o?"let global_idx = global_id.x; let local_idx = local_id.x;":`let global_idx = (workgroup_id.z * num_workgroups[0] * num_workgroups[1] + workgroup_id.y * num_workgroups[0] + workgroup_id.x) * ${r*a*n}u + local_idx;`;return`@compute @workgroup_size(${r}, ${a}, ${n}) fn main(${s}) { ${l} `}appendVariableUniforms(t){t.rank!==0&&(t.shape.startsWith("uniforms.")&&this.uniforms.push({name:t.shape.replace("uniforms.",""),type:"u32",length:t.rank}),t.strides.startsWith("uniforms.")&&this.uniforms.push({name:t.strides.replace("uniforms.",""),type:"u32",length:t.rank}))}declareVariable(t,r){if(t.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(t),this.appendVariableUniforms(t);let a=t.usage==="input"?"read":"read_write",n=t.type.storage;return`@group(0) @binding(${r}) var ${t.name}: array<${n}>;`}declareVariables(...t){return t.map(r=>this.declareVariable(r,this.variableIndex++)).join(` `)}registerInternalVariable(t){if(t.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(t),this.appendVariableUniforms(t)}registerInternalVariables(...t){return t.forEach(r=>this.registerInternalVariable(r)),this}registerUniform(t,r,a=1){return this.uniforms.push({name:t,type:r,length:a}),this}registerUniforms(t){return this.uniforms=this.uniforms.concat(t),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let t=[];for(let{name:r,type:a,length:n}of this.uniforms)if(n&&n>4)a==="f16"?t.push(`@align(16) ${r}:array, ${Math.ceil(n/8)}>`):t.push(`${r}:array, ${Math.ceil(n/4)}>`);else{let o=n==null||n===1?a:`vec${n}<${a}>`;t.push(`${r}:${o}`)}return` struct Uniforms { ${t.join(", ")} }; @group(0) @binding(${this.variableIndex}) var uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(t=>t.impl()).join(` `)+this.internalVariables.map(t=>t.impl()).join(` `)}},uo=e=>new ca(e),ar=(e,t)=>{let r=e.length,a=[];for(let n=0;n1&&s===1&&a.unshift(o)}return a}});var zd,lo,Bd,Dd,dt,co,po,ir=F(()=>{"use strict";ie();_e();Ye();be();zd=e=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.")},lo=(e,t)=>t&&t.length!==e?[...new Array(e).keys()].reverse():t,Bd=(e,t)=>M.sortBasedOnPerm(e,lo(e.length,t)),Dd=(e,t,r,a)=>{let n=[];n.push(`fn perm(i: ${a.type.indices}) -> ${r.type.indices} { var a: ${r.type.indices};`);for(let o=0;o{let r=e.dataType,a=e.dims.length,n=lo(a,t),o=Bd(e.dims,n),s=q("output",r,o.length),l=D("a",r,a),i=c=>` ${c.registerUniform("output_size","u32").declareVariables(l,s)} ${Dd(n,a,l,s)} ${c.mainStart()} ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let indices = ${s.offsetToIndices("global_idx")}; let aIndices = perm(indices); ${s.setByOffset("global_idx",l.getByIndices("aIndices"))} }`;return{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:c=>{let f=M.size(o);return{outputs:[{dims:o,dataType:c[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:[{type:12,data:f},...K(c[0].dims,o)]}},getShaderSource:i}},co=(e,t)=>{zd(e.inputs),e.compute(dt(e.inputs[0],t.perm))},po=e=>$e({perm:e.perm})});var Md,Ud,Nd,Wd,Vd,Gd,Hd,Ld,Fd,qd,$t,fo,mo,ho,go,yo,bo,wo,vo,$o,_o,xo=F(()=>{"use strict";ie();_e();be();gn();ir();Md={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},Ud={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},Nd={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Wd={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},Vd=(e,t)=>{let r=[];for(let a=t-e;a{let r=[],a=e.length;for(let o=0;oe[o]);return[r,n]},Hd=(e,t)=>{let r=e.length+t.length,a=[],n=0;for(let o=0;o{for(let r=0;r{let r=[];if(!Ld(e,t)){for(let a=0;ar.push(a))}return r},qd=(e,t,r,a,n,o,s)=>{let l=r[0].dims,i=M.size(o),c=M.size(s),f=D("_A",r[0].dataType,l),m=q("output",n,o),h=32,w=` var aBestValues : array; `;return{name:e,shaderCache:t,getShaderSource:$=>` ${$.registerUniform("reduceSize","u32").declareVariables(f,m)} ${w} fn DIV_CEIL(a : u32, b : u32) -> u32 { return ((a - 1u) / b + 1u); } ${$.mainStart(h)} let outputIndex = global_idx / ${h}; let offset = outputIndex * uniforms.reduceSize; var bestValue = f32(${Nd[a]}); let Length = uniforms.reduceSize; for (var k = local_idx; k < Length; k = k + ${h}) { let candidate = f32(${f.getByOffset("offset + k")}); bestValue = ${Md[a]}; } aBestValues[local_idx] = bestValue; workgroupBarrier(); var reduceSize = min(Length, ${h}u); for (var currentSize = reduceSize / 2u; reduceSize > 1u; currentSize = reduceSize / 2u) { let interval = DIV_CEIL(reduceSize, 2u); if (local_idx < currentSize) { let candidate = aBestValues[local_idx + interval]; bestValue = ${Ud[a]}; aBestValues[local_idx] = bestValue; } reduceSize = interval; workgroupBarrier(); } if (local_idx == 0u) { ${m.setByOffset("outputIndex",`${a==="mean"?`${m.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${m.type.storage}(${Wd[a]})`}`)}; } }`,getRunData:()=>({outputs:[{dims:o,dataType:n}],dispatchGroup:{x:i},programUniforms:[{type:12,data:c}]})}},$t=(e,t,r,a)=>{let n=e.inputs.length===1?r:ma(e.inputs,r),o=n.axes;o.length===0&&!n.noopWithEmptyAxes&&(o=e.inputs[0].dims.map((w,b)=>b));let s=M.normalizeAxes(o,e.inputs[0].dims.length),l=s,i=e.inputs[0],c=Fd(l,e.inputs[0].dims.length);c.length>0&&(i=e.compute(dt(e.inputs[0],c),{inputs:[0],outputs:[-1]})[0],l=Vd(l.length,i.dims.length));let[f,m]=Gd(i.dims,l),h=f;n.keepDims&&(h=Hd(f,s)),e.compute(qd(t,{hint:n.cacheKey,inputDependencies:["type"]},[i],a,e.inputs[0].dataType,h,m),{inputs:[i]})},fo=(e,t)=>{$t(e,"ReduceMeanShared",t,"mean")},mo=(e,t)=>{$t(e,"ReduceL1Shared",t,"l1")},ho=(e,t)=>{$t(e,"ReduceL2Shared",t,"l2")},go=(e,t)=>{$t(e,"ReduceLogSumExpShared",t,"logSumExp")},yo=(e,t)=>{$t(e,"ReduceMaxShared",t,"max")},bo=(e,t)=>{$t(e,"ReduceMinShared",t,"min")},wo=(e,t)=>{$t(e,"ReduceProdShared",t,"prod")},vo=(e,t)=>{$t(e,"ReduceSumShared",t,"sum")},$o=(e,t)=>{$t(e,"ReduceSumSquareShared",t,"sumSquare")},_o=(e,t)=>{$t(e,"ReduceLogSumShared",t,"logSum")}});var _t,jd,yn,ma,xt,Kd,Yd,Zd,Xd,Qd,Jd,ec,tc,rc,nc,St,So,Co,Io,Eo,Ao,To,Oo,ko,Po,Ro,gn=F(()=>{"use strict";ie();_e();Ye();be();xo();_t=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},jd=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],yn=(e,t,r,a,n,o,s=!1,l=!1)=>{let i=[],c=r[0].dims,f=c.length,m=M.normalizeAxes(n,f),h=!l&&m.length===0;c.forEach((I,S)=>{h||m.indexOf(S)>=0?s&&i.push(1):i.push(I)});let w=i.length,b=M.size(i);return{name:e,shaderCache:t,getShaderSource:I=>{let S=[],v=D("_A",r[0].dataType,f),E=q("output",o,w),A=a(v,E,m),k=A[2];for(let U=0,N=0;U=0?(s&&N++,k=`for(var j${U}: u32 = 0; j${U} < ${c[U]}; j${U}++) { ${A[2].includes("last_index")?`let last_index = j${U};`:""} ${v.indicesSet("input_indices",U,`j${U}`)} ${k} }`):(S.push(`${v.indicesSet("input_indices",U,E.indicesGet("output_indices",N))};`),N++);return` ${I.registerUniform("output_size","u32").declareVariables(v,E)} ${I.mainStart()} ${I.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} var input_indices: ${v.type.indices}; let output_indices = ${E.offsetToIndices("global_idx")}; ${S.join(` `)} ${A[0]} // init ops for reduce max/min ${A[1]} ${k} ${A[3]} ${A.length===4?E.setByOffset("global_idx","value"):A.slice(4).join(` `)} }`},getRunData:()=>({outputs:[{dims:i,dataType:o}],dispatchGroup:{x:Math.ceil(b/64)},programUniforms:[{type:12,data:b},...K(c,i)]})}},ma=(e,t)=>{let r=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(a=>r.push(Number(a))),$e({axes:r,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},xt=(e,t,r,a)=>{let n=e.inputs,o=n.length===1?r:ma(n,r);e.compute(yn(t,{hint:o.cacheKey,inputDependencies:["rank"]},[n[0]],o.noopWithEmptyAxes&&o.axes.length===0?jd:a,o.axes,n[0].dataType,o.keepDims,o.noopWithEmptyAxes),{inputs:[0]})},Kd=(e,t)=>{_t(e.inputs),xt(e,"ReduceLogSum",t,(a,n)=>[`var value = ${n.type.storage}(0);`,"",`value += ${a.getByIndices("input_indices")};`,"value = log(value);"])},Yd=(e,t)=>{_t(e.inputs),xt(e,"ReduceL1",t,(a,n)=>[`var value = ${n.type.storage}(0);`,"",`value += abs(${a.getByIndices("input_indices")});`,""])},Zd=(e,t)=>{_t(e.inputs),xt(e,"ReduceL2",t,(a,n)=>[`var t = ${n.type.value}(0); var value = ${n.type.value}(0);`,"",`t = ${a.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},Xd=(e,t)=>{_t(e.inputs),xt(e,"ReduceLogSumExp",t,(a,n)=>[`var value = ${n.type.storage}(0);`,"",`value += exp(${a.getByIndices("input_indices")});`,"value = log(value);"])},Qd=(e,t)=>{_t(e.inputs),xt(e,"ReduceMax",t,(a,n,o)=>{let s=[];for(let l=0;l=0||o.length===0)&&s.push(a.indicesSet("input_indices",l,0));return[`${s.join(` `)}`,`var value = ${a.getByIndices("input_indices")};`,`value = max(value, ${a.getByIndices("input_indices")});`,""]})},Jd=(e,t)=>{_t(e.inputs),xt(e,"ReduceMean",t,(a,n,o)=>{let s=1;for(let l=0;l=0||o.length===0)&&(s*=e.inputs[0].dims[l]);return["var sum = f32(0);","",`sum += f32(${a.getByIndices("input_indices")});`,`let value = ${n.type.value}(sum / ${s});`]})},ec=(e,t)=>{_t(e.inputs),xt(e,"ReduceMin",t,(a,n,o)=>{let s=[];for(let l=0;l=0||o.length===0)&&s.push(`input_indices[${l}] = 0;`);return[`${s.join(` `)}`,`var value = ${a.getByIndices("input_indices")};`,`value = min(value, ${a.getByIndices("input_indices")});`,""]})},tc=(e,t)=>{_t(e.inputs),xt(e,"ReduceProd",t,(a,n)=>[`var value = ${n.type.storage}(1);`,"",`value *= ${a.getByIndices("input_indices")};`,""])},rc=(e,t)=>{_t(e.inputs),xt(e,"ReduceSum",t,(a,n)=>[`var value = ${n.type.storage}(0);`,"",`value += ${a.getByIndices("input_indices")};`,""])},nc=(e,t)=>{_t(e.inputs),xt(e,"ReduceSumSquare",t,(a,n)=>[`var t = ${n.type.value}(0); var value = ${n.type.value}(0);`,"",`t = ${a.getByIndices("input_indices")}; value += t * t;`,""])},St=(e,t,r)=>{if(t.length===0)return r;let a=1,n=1;for(let o=0;o1024},So=(e,t)=>{St(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Jd(e,t):fo(e,t)},Co=(e,t)=>{St(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Yd(e,t):mo(e,t)},Io=(e,t)=>{St(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Zd(e,t):ho(e,t)},Eo=(e,t)=>{St(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Xd(e,t):go(e,t)},Ao=(e,t)=>{St(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Qd(e,t):yo(e,t)},To=(e,t)=>{St(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?ec(e,t):bo(e,t)},Oo=(e,t)=>{St(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?tc(e,t):wo(e,t)},ko=(e,t)=>{St(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?rc(e,t):vo(e,t)},Po=(e,t)=>{St(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?nc(e,t):$o(e,t)},Ro=(e,t)=>{St(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Kd(e,t):_o(e,t)}});var zo,Bo,Do,ha,Mo=F(()=>{"use strict";ie();Ye();gn();zo=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},Bo=(e,t)=>{zo(e.inputs);let r=(a,n,o)=>{let s=[];for(let l=0;l=0||o.length===0)&&s.push(`input_indices[${l}] = 0;`);return[`${s.join(` `)}`,`var value = ${a.getByIndices("input_indices")}; var best_index : i32 = 0;`,`if (${a.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) { value = ${a.getByIndices("input_indices")}; best_index = i32(last_index); }`,"",n.setByOffset("global_idx","best_index")]};e.compute(yn("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},Do=(e,t)=>{zo(e.inputs);let r=(a,n,o)=>{let s=[];for(let l=0;l=0||o.length===0)&&s.push(`input_indices[${l}] = 0;`);return[`${s.join(` `)}`,`var value = ${a.getByIndices("input_indices")}; var best_index : i32 = 0;`,`if (${a.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) { value = ${a.getByIndices("input_indices")}; best_index = i32(last_index); }`,"",n.setByOffset("global_idx","best_index")]};e.compute(yn("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},ha=e=>$e(e)});var ac,ic,oc,sc,bn,uc,Uo,ga=F(()=>{"use strict";ie();ln();be();ac=(e,t)=>{let r=e[0],a=e[1],n=e[2],o=e[3],s=e[4],l=e[5];if(s&&l)throw new Error("Attention cannot have both past and relative_position_bias");if(r.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let i=r.dims[0],c=r.dims[1],f=r.dims[2];if(n.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(a.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(a.dims[0]!==f)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(n.dims[0]!==a.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let m=n.dims[0]/3,h=m,w=h;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let E of t.qkvHiddenSizes)if(E%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");m=t.qkvHiddenSizes[0],h=t.qkvHiddenSizes[1],w=t.qkvHiddenSizes[2]}let b=c;if(m!==h)throw new Error("qkv_hidden_sizes first element should be same as the second");if(n.dims[0]!==m+h+w)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let $=0;if(s){if(h!==w)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(s.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(s.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(s.dims[1]!==i)throw new Error('Input "past" second dimension must be batch_size');if(s.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(s.dims[4]!==h/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||($=s.dims[3])}let I=b+$,S=-1,v=0;if(o)throw new Error("Mask not supported");if(s)throw new Error("past is not supported");if(l)throw new Error("relativePositionBias is not supported");return{batchSize:i,sequenceLength:c,pastSequenceLength:$,kvSequenceLength:b,totalSequenceLength:I,maxSequenceLength:S,inputHiddenSize:f,hiddenSize:m,vHiddenSize:w,headSize:Math.floor(m/t.numHeads),vHeadSize:Math.floor(w/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:v,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},ic=(e,t,r,a)=>{let n=Me(a),o=64,s=a/n;s{let h=q("x",t.dataType,t.dims,n),w="thread_max_vector";n===2?w="max(thread_max_vector.x, thread_max_vector.y)":n===4&&(w="max(max(thread_max_vector.x, thread_max_vector.y), max(thread_max_vector.z, thread_max_vector.w))");let b=at(t.dataType),$=[{name:"d_inv",type:b},{name:"d_comp",type:"u32"},{name:"elements_per_wg",type:"u32"}];return` var wgMax: array; var wgSum: array; ${m.registerUniforms($).declareVariables(h)} ${m.mainStart([o,1,1])} let localOffset = local_idx * uniforms.elements_per_wg; let offset: u32 = workgroup_id.x * uniforms.d_comp + localOffset; var thread_max_vector = ${Xe("f32",n,"-3.402823e+38f")}; for (var i: u32 = 0; i < uniforms.elements_per_wg && i + localOffset < uniforms.d_comp; i++) { thread_max_vector = max(${mt(b,n,"x[offset + i]")}, thread_max_vector); } wgMax[local_idx] = ${w}; workgroupBarrier(); var maxValue = -3.402823e+38f; for (var i = 0u; i < ${o}; i++) { maxValue = max(wgMax[i], maxValue); } var sumVector = ${Xe("f32",n,"0")}; for (var i: u32 = 0; i < uniforms.elements_per_wg && i + localOffset < uniforms.d_comp; i++) { sumVector += exp(${mt(b,n,"x[offset + i]")} - maxValue); } wgSum[local_idx] = ${it("sumVector",n)}; workgroupBarrier(); var sum: f32 = 0; for (var i = 0u; i < ${o}; i++) { sum += wgSum[i]; } if (sum == 0) { for (var i: u32 = 0; i < uniforms.elements_per_wg && i + localOffset < uniforms.d_comp; i++) { x[offset + i] = ${Xe(b,n,"uniforms.d_inv")}; } } else { for (var i: u32 = 0; i < uniforms.elements_per_wg && i + localOffset < uniforms.d_comp; i++) { let f32input = ${mt(b,n,"x[offset + i]")}; x[offset + i] = ${h.type.value}(exp(f32input - maxValue) / sum); } } }`};e.compute({name:"AttentionProbsSoftmax",shaderCache:{hint:`${o};${c};${n}`},getShaderSource:f,getRunData:()=>({outputs:[],dispatchGroup:{x:r},programUniforms:i})},{inputs:[t],outputs:[]})},oc=(e,t,r,a,n,o)=>{let s=[n.batchSize,n.numHeads,n.sequenceLength,n.kvSequenceLength+n.pastSequenceLength],l=o.scale===0?1/Math.sqrt(n.headSize):o.scale,i=Me(n.headSize),c=n.headSize/i,f=12,m={x:Math.ceil(n.totalSequenceLength/f),y:Math.ceil(n.sequenceLength/f),z:n.batchSize*n.numHeads},h=[{type:12,data:n.sequenceLength},{type:12,data:c},{type:12,data:n.totalSequenceLength},{type:12,data:n.kvSequenceLength},{type:t.dataType,data:l}],w=[t,r],b=I=>{let S=D("q",t.dataType,t.dims,i),v=D("key",r.dataType,r.dims,i),E=q("output",t.dataType,s),A=ke(t.dataType),k=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"alpha",type:A}];return` const beta: ${A} = 1.0; const TILE_SIZE = ${f}u; var tileQ: array<${S.type.storage}, ${f*f}>; var tileK: array<${S.type.storage}, ${f*f}>; ${I.registerUniforms(k).declareVariables(S,v,E)} ${I.mainStart([f,f,1])} // x holds the N and y holds the M let headIdx = workgroup_id.z; let m = workgroup_id.y * TILE_SIZE; let n = workgroup_id.x * TILE_SIZE; let lm = m + local_id.y; let ln = n + local_id.x; let qOffset = uniforms.M * uniforms.K * headIdx + m * uniforms.K; let kOffset = uniforms.kv_sequence_length * uniforms.K * headIdx + n * uniforms.K; var value = ${Xe(A,i)}; for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { if (m + local_id.y < uniforms.M && w + local_id.x < uniforms.K) { tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x]; } if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) { tileK[TILE_SIZE * local_id.y + local_id.x] = key[kOffset + local_id.y * uniforms.K + w + local_id.x]; } workgroupBarrier(); for (var k: u32 = 0u; k({outputs:[{dims:s,dataType:t.dataType,gpuDataType:0}],dispatchGroup:m,programUniforms:h}),getShaderSource:b},{inputs:w,outputs:[-1]})[0];return ic(e,$,n.batchSize*n.numHeads*n.sequenceLength,n.totalSequenceLength),$},sc=(e,t,r,a)=>{let n=[a.batchSize,a.sequenceLength,a.vHiddenSize],o=12,s={x:Math.ceil(a.vHeadSize/o),y:Math.ceil(a.sequenceLength/o),z:a.batchSize*a.numHeads},l=[{type:12,data:a.sequenceLength},{type:12,data:a.totalSequenceLength},{type:12,data:a.vHeadSize},{type:12,data:a.numHeads},{type:12,data:a.vHiddenSize}],i=c=>{let f=D("probs",t.dataType,t.dims),m=D("v",r.dataType,r.dims),h=q("output",t.dataType,n),w=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"v_hidden_size",type:"u32"}];return` const TILE_SIZE = ${o}u; var tileQ: array<${f.type.value}, ${o*o}>; var tileK: array<${f.type.value}, ${o*o}>; ${c.registerUniforms(w).declareVariables(f,m,h)} ${c.mainStart([o,o,1])} let headIdx = workgroup_id.z; let m = workgroup_id.y * TILE_SIZE + local_id.y; let n = workgroup_id.x * TILE_SIZE + local_id.x; let offsetA = headIdx * (uniforms.M * uniforms.K) + m * uniforms.K; let offsetB = headIdx * (uniforms.N * uniforms.K) + n; var value = ${f.type.storage}(0); for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { if (m < uniforms.M && w + local_id.x < uniforms.K) { tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x]; } if (n < uniforms.N && w + local_id.y < uniforms.K) { tileK[TILE_SIZE * local_id.y + local_id.x] = v[offsetB + (w + local_id.y) * uniforms.N]; } workgroupBarrier(); for (var k: u32 = 0u; k({outputs:[{dims:n,dataType:t.dataType,gpuDataType:0}],dispatchGroup:s,programUniforms:l}),getShaderSource:i},{inputs:[t,r],outputs:[0]})[0]},bn=(e,t,r,a,n,o,s,l,i,c,f)=>{let m=oc(e,t,r,i,c,f);sc(e,m,a,c)},uc=(e,t)=>{let r=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],a=t.sequenceLength,n=t.inputHiddenSize,o=t.headSize,s=12,l={x:Math.ceil(t.headSize/s),y:Math.ceil(t.sequenceLength/s),z:t.batchSize*t.numHeads},i=[e.inputs[0],e.inputs[1],e.inputs[2]],c=[{type:12,data:a},{type:12,data:n},{type:12,data:o},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],f=m=>{let h=q("output_q",i[0].dataType,r),w=q("output_k",i[0].dataType,r),b=q("output_v",i[0].dataType,r),$=D("input",i[0].dataType,i[0].dims),I=D("weight",i[1].dataType,i[1].dims),S=D("bias",i[2].dataType,i[2].dims),v=$.type.storage,E=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return` const TILE_SIZE = ${s}u; var tileInput: array<${v}, ${s*s}>; var tileWeightQ: array<${v}, ${s*s}>; var tileWeightK: array<${v}, ${s*s}>; var tileWeightV: array<${v}, ${s*s}>; ${m.registerUniforms(E).declareVariables($,I,S,h,w,b)} ${m.mainStart([s,s,1])} let batchIndex = workgroup_id.z / uniforms.num_heads; let headNumber = workgroup_id.z % uniforms.num_heads; let m = workgroup_id.y * TILE_SIZE + local_id.y; let n = workgroup_id.x * TILE_SIZE + local_id.x; let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K; let biasOffsetQ = headNumber * uniforms.head_size; let biasOffsetK = uniforms.hidden_size + biasOffsetQ; let biasOffsetV = uniforms.hidden_size + biasOffsetK; var valueQ = ${v}(0); var valueK = ${v}(0); var valueV = ${v}(0); for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { if (m < uniforms.M && w + local_id.x < uniforms.K) { tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x]; } if (n < uniforms.N && w + local_id.y < uniforms.K) { let offset = n + (w + local_id.y) * uniforms.ldb; tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset]; tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset]; tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset]; } workgroupBarrier(); for (var k: u32 = 0u; k({outputs:[{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:l,programUniforms:c}),getShaderSource:f},{inputs:i,outputs:[-1,-1,-1]})},Uo=(e,t)=>{let r=ac(e.inputs,t),[a,n,o]=uc(e,r);return bn(e,a,n,o,e.inputs[4],void 0,void 0,void 0,e.inputs[5],r,t)}});var lc,dc,cc,No,Wo=F(()=>{"use strict";lt();ie();_e();Ye();be();lc=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let r=(a,n,o)=>{let s=n.length;if(s!==a.length)throw new Error(`${o}: num dimensions != ${s}`);n.forEach((l,i)=>{if(l!==a[i])throw new Error(`${o}: dim[${i}] do not match`)})};if(e[0].dims.length>1){let a=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);r(e[1].dims,a,"Invalid input scale"),r(e[2].dims,a,"Invalid input B"),r(e[3].dims,a,"Invalid input mean"),r(e[4].dims,a,"Invalid input var")}else r(e[1].dims,[1],"Invalid input scale"),r(e[2].dims,[1],"Invalid input B"),r(e[3].dims,[1],"Invalid input mean"),r(e[4].dims,[1],"Invalid input var")},dc=(e,t)=>{let{epsilon:r,spatial:a,format:n}=t,o=e[0].dims,s=a?Me(o[o.length-1]):1,l=n==="NHWC"&&o.length>1?s:1,i=M.size(o)/s,c=a,f=c?o.length:o,m=D("x",e[0].dataType,e[0].dims,s),h=D("scale",e[1].dataType,e[1].dims,l),w=D("bias",e[2].dataType,e[2].dims,l),b=D("inputMean",e[3].dataType,e[3].dims,l),$=D("inputVar",e[4].dataType,e[4].dims,l),I=q("y",e[0].dataType,f,s),S=()=>{let E="";if(a)E=`let cOffset = ${o.length===1?"0u":n==="NHWC"?`outputIndices[${o.length-1}] / ${s}`:"outputIndices[1]"};`;else if(n==="NCHW")E=` ${I.indicesSet("outputIndices","0","0")} let cOffset = ${I.indicesToOffset("outputIndices")};`;else{E=`var cIndices = ${h.type.indices}(0); cIndices[0] = outputIndices[${o.length-1}];`;for(let A=1;A` const epsilon = ${r}; ${E.registerUniform("outputSize","u32").declareVariables(m,h,w,b,$,I)} ${E.mainStart()} ${E.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} var outputIndices = ${I.offsetToIndices(`global_idx * ${s}`)}; ${S()} let scale = ${h.getByOffset("cOffset")}; let bias = ${w.getByOffset("cOffset")}; let inputMean = ${b.getByOffset("cOffset")}; let inputVar = ${$.getByOffset("cOffset")}; let x = ${m.getByOffset("global_idx")}; let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias; ${I.setByOffset("global_idx","value")} }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${a}_${s}`,inputDependencies:c?["rank","type","type","type","type"]:void 0},getShaderSource:v,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:c?[{type:12,data:i},...K(o)]:[{type:12,data:i}]})}},cc=e=>$e(e),No=(e,t)=>{let{inputs:r,outputCount:a}=e,n=cc({...t,outputCount:a});if(Ae.webgpu.validateInputContent&&lc(r,n),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(dc(r,n))}});var pc,fc,Vo,Go=F(()=>{"use strict";_e();be();pc=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},fc=e=>{let t=e[0].dims,r=e[0].dims[2],a=M.size(t)/4,n=e[0].dataType,o=D("input",n,t,4),s=D("bias",n,[r],4),l=D("residual",n,t,4),i=q("output",n,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)}}),getShaderSource:f=>` const channels = ${r}u / 4; ${f.declareVariables(o,s,l,i)} ${f.mainStart()} ${f.guardAgainstOutOfBoundsWorkgroupSizes(a)} let value = ${o.getByOffset("global_idx")} + ${s.getByOffset("global_idx % channels")} + ${l.getByOffset("global_idx")}; ${i.setByOffset("global_idx","value")} }`}},Vo=e=>{pc(e.inputs),e.compute(fc(e.inputs))}});var mc,Oe,Ho,Lo,Fo,qo,jo,Ko,Yo,Zo,Xo,hc,Qo,Jo,es,ts,wn,rs,vn,ns,as,is,os,ss,us,ls,ds,cs,ps,fs,ms,hs,gs,ys,bs,ws,vs,ya,ba,$s,_s,xs,$n=F(()=>{"use strict";ie();_e();Ye();be();mc=(e,t,r,a,n,o)=>{let s=Math.ceil(t/4),l="";typeof n=="string"?l=`${n}(a)`:l=n("a");let i=D("inputData",r,[s],4),c=q("outputData",a,[s],4);return` ${e.registerUniform("vec_size","u32").declareVariables(i,c)} ${o??""} ${e.mainStart()} ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} let a = ${i.getByOffset("global_idx")}; ${c.setByOffset("global_idx",l)} }`},Oe=(e,t,r,a,n,o=e.dataType)=>({name:t,shaderCache:{hint:n,inputDependencies:["type"]},getShaderSource:s=>mc(s,M.size(e.dims),e.dataType,o,r,a),getRunData:s=>({outputs:[{dims:e.dims,dataType:o}],dispatchGroup:{x:Math.ceil(M.size(s[0].dims)/64/4)},programUniforms:[{type:12,data:Math.ceil(M.size(e.dims)/4)}]})}),Ho=e=>{e.compute(Oe(e.inputs[0],"Abs","abs"))},Lo=e=>{e.compute(Oe(e.inputs[0],"Acos","acos"))},Fo=e=>{e.compute(Oe(e.inputs[0],"Acosh","acosh"))},qo=e=>{e.compute(Oe(e.inputs[0],"Asin","asin"))},jo=e=>{e.compute(Oe(e.inputs[0],"Asinh","asinh"))},Ko=e=>{e.compute(Oe(e.inputs[0],"Atan","atan"))},Yo=e=>{e.compute(Oe(e.inputs[0],"Atanh","atanh"))},Zo=e=>$e(e),Xo=(e,t)=>{let r;switch(t.to){case 10:r="vec4";break;case 1:r="vec4";break;case 12:r="vec4";break;case 6:r="vec4";break;case 9:r="vec4";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(Oe(e.inputs[0],"Cast",r,void 0,t.cacheKey,t.to))},hc=e=>{let t=e.length>=2&&e[1].data!==0?e[1].getFloat32Array()[0]:pn,r=e.length>=3&&e[2].data!==0?e[2].getFloat32Array()[0]:fn;return $e({min:t,max:r})},Qo=(e,t)=>{let r=e.inputs.length===1?t:hc(e.inputs),a=at(e.inputs[0].dataType);e.compute(Oe(e.inputs[0],"Clip",n=>`clamp(${n}, clip_min_, clip_max_)`,` const clip_min_: vec4<${a}> = vec4(${a}(${r.min})); const clip_max_: vec4<${a}> = vec4(${a}(${r.max})); `,r.cacheKey),{inputs:[0]})},Jo=e=>{e.compute(Oe(e.inputs[0],"Ceil","ceil"))},es=e=>{e.compute(Oe(e.inputs[0],"Cos","cos"))},ts=e=>{e.compute(Oe(e.inputs[0],"Cosh","cosh"))},wn=e=>$e(e),rs=(e,t)=>{let r=at(e.inputs[0].dataType);e.compute(Oe(e.inputs[0],"Elu",a=>`elu_vf32(${a})`,` const elu_alpha_ = ${r}(${t.alpha}); fn elu_f32(a: ${r}) -> ${r} { return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0); } fn elu_vf32(v: vec4<${r}>) -> vec4<${r}> { return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w)); }`,t.cacheKey))},vn=(e="f32")=>` const r0: ${e} = 0.3275911; const r1: ${e} = 0.254829592; const r2: ${e} = -0.284496736; const r3: ${e} = 1.421413741; const r4: ${e} = -1.453152027; const r5: ${e} = 1.061405429; fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> { let absv = abs(v); let x = 1.0 / (1.0 + r0 * absv); return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv)); }`,ns=e=>{let t=at(e.inputs[0].dataType);e.compute(Oe(e.inputs[0],"Erf",r=>`erf_vf32(${r})`,vn(t)))},as=e=>{e.compute(Oe(e.inputs[0],"Exp","exp"))},is=e=>{e.compute(Oe(e.inputs[0],"Floor","floor"))},os=e=>{let t=at(e.inputs[0].dataType);e.compute(Oe(e.inputs[0],"Gelu",r=>`0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`,vn(t)))},ss=(e,t)=>{let r=at(e.inputs[0].dataType);e.compute(Oe(e.inputs[0],"LeakyRelu",a=>`select(leaky_relu_alpha_ * ${a}, ${a}, ${a} >= vec4<${r}>(0.0))`,`const leaky_relu_alpha_ = ${r}(${t.alpha});`,t.cacheKey))},us=e=>{e.compute(Oe(e.inputs[0],"Not",t=>`!${t}`))},ls=e=>{e.compute(Oe(e.inputs[0],"Neg",t=>`-${t}`))},ds=e=>{e.compute(Oe(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},cs=e=>{let t=at(e.inputs[0].dataType);e.compute(Oe(e.inputs[0],"Relu",r=>`select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`))},ps=e=>{e.compute(Oe(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},fs=e=>$e(e),ms=(e,t)=>{let r=at(e.inputs[0].dataType);e.compute(Oe(e.inputs[0],"HardSigmoid",a=>`max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${a} + vec4<${r}>(${t.beta})))`,void 0,t.cacheKey))},hs=e=>{e.compute(Oe(e.inputs[0],"Sin","sin"))},gs=e=>{e.compute(Oe(e.inputs[0],"Sinh","sinh"))},ys=e=>{e.compute(Oe(e.inputs[0],"Sqrt","sqrt"))},bs=e=>{e.compute(Oe(e.inputs[0],"Tan","tan"))},ws=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,vs=e=>{e.compute(Oe(e.inputs[0],"Tanh",ws))},ya=(e="f32")=>` const fast_gelu_a: ${e} = 0.5; const fast_gelu_b: ${e} = 0.7978845608028654; const fast_gelu_c: ${e} = 0.035677408136300125; fn tanh_v(v: vec4<${e}>) -> vec4<${e}> { return ${ws("v")}; } `,ba=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,$s=e=>{let t=at(e.inputs[0].dataType);e.compute(Oe(e.inputs[0],"FastGelu",ba,ya(t),void 0,e.inputs[0].dataType))},_s=(e,t)=>{let r=at(e.inputs[0].dataType);return e.compute(Oe(e.inputs[0],"ThresholdedRelu",a=>`select(vec4<${r}>(0.0), ${a}, ${a} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`,t.cacheKey)),0},xs=e=>{e.compute(Oe(e.inputs[0],"Log","log"))}});var gc,yc,Cs,Is=F(()=>{"use strict";_e();be();$n();gc=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},yc=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let r=D("input",e[0].dataType,e[0].dims,4),a=D("bias",e[0].dataType,[e[0].dims[2]],4),n=q("output",e[0].dataType,t,4),o=M.size(t)/4,s=ke(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(o/64)}}),getShaderSource:i=>` const M_SQRT2 = sqrt(2.0); const halfChannels = ${e[0].dims[2]/4/2}u; ${i.declareVariables(r,a,n)} ${vn(s)} ${i.mainStart()} ${i.guardAgainstOutOfBoundsWorkgroupSizes(o)} let biasIdx = global_idx % halfChannels; let batchIndex = global_idx / halfChannels; let inputOffset = biasIdx + batchIndex * halfChannels * 2; let valueLeft = input[inputOffset] + bias[biasIdx]; let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels]; let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1); ${n.setByOffset("global_idx","valueLeft * geluRight")} }`}},Cs=e=>{gc(e.inputs),e.compute(yc(e.inputs))}});var bc,wc,Ct,Es,As,Ts,Os,ks,Ps,Rs,zs,Bs,Ds,Ms=F(()=>{"use strict";ie();_e();be();bc=(e,t,r,a,n,o,s,l,i,c,f,m)=>{let h,w;typeof l=="string"?h=w=(v,E)=>`${l}((${v}),(${E}))`:typeof l=="function"?h=w=l:(h=l.scalar,w=l.vector);let b=q("outputData",f,a.length,4),$=D("aData",i,t.length,4),I=D("bData",c,r.length,4),S;if(n)if(o){let v=M.size(t)===1,E=M.size(r)===1,A=t.length>0&&t[t.length-1]%4===0,k=r.length>0&&r[r.length-1]%4===0;v||E?S=b.setByOffset("global_idx",w(v?`${$.type.value}(${$.getByOffset("0")}.x)`:$.getByOffset("global_idx"),E?`${I.type.value}(${I.getByOffset("0")}.x)`:I.getByOffset("global_idx"))):S=` let outputIndices = ${b.offsetToIndices("global_idx * 4u")}; let offsetA = ${$.broadcastedIndicesToOffset("outputIndices",b)}; let offsetB = ${I.broadcastedIndicesToOffset("outputIndices",b)}; ${b.setByOffset("global_idx",w(s||A?$.getByOffset("offsetA / 4u"):`${$.type.value}(${$.getByOffset("offsetA / 4u")}[offsetA % 4u])`,s||k?I.getByOffset("offsetB / 4u"):`${I.type.value}(${I.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} `}else S=b.setByOffset("global_idx",w($.getByOffset("global_idx"),I.getByOffset("global_idx")));else{if(!o)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let v=(E,A,k="")=>{let U=`aData[indexA${A}][componentA${A}]`,N=`bData[indexB${A}][componentB${A}]`;return` let outputIndices${A} = ${b.offsetToIndices(`global_idx * 4u + ${A}u`)}; let offsetA${A} = ${$.broadcastedIndicesToOffset(`outputIndices${A}`,b)}; let offsetB${A} = ${I.broadcastedIndicesToOffset(`outputIndices${A}`,b)}; let indexA${A} = offsetA${A} / 4u; let indexB${A} = offsetB${A} / 4u; let componentA${A} = offsetA${A} % 4u; let componentB${A} = offsetB${A} % 4u; ${E}[${A}] = ${k}(${h(U,N)}); `};f===9?S=` var data = vec4(0); ${v("data",0,"u32")} ${v("data",1,"u32")} ${v("data",2,"u32")} ${v("data",3,"u32")} outputData[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:S=` ${v("outputData[global_idx]",0)} ${v("outputData[global_idx]",1)} ${v("outputData[global_idx]",2)} ${v("outputData[global_idx]",3)} `}return` ${e.registerUniform("vec_size","u32").declareVariables($,I,b)} ${m??""} ${e.mainStart()} ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} ${S} }`},wc=(e,t,r,a,n,o,s=r.dataType)=>{let l=!M.areEqual(r.dims,a.dims),i=r.dims,c=M.size(r.dims),f=!1,m=!1,h=[l];if(l){let w=vt.calcShape(r.dims,a.dims,!1);if(!w)throw new Error("Can't perform binary op on the given tensors");i=w,c=M.size(i);let b=M.size(r.dims)===1,$=M.size(a.dims)===1,I=r.dims.length>0&&r.dims[r.dims.length-1]%4===0,S=a.dims.length>0&&a.dims[a.dims.length-1]%4===0;h.push(b),h.push($),h.push(I),h.push(S);let v=1;for(let E=1;Ew.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:w=>bc(w,r.dims,a.dims,i,f,l,m,n,r.dataType,a.dataType,s,o),getRunData:()=>({outputs:[{dims:i,dataType:s}],dispatchGroup:{x:Math.ceil(c/64/4)},programUniforms:[{type:12,data:Math.ceil(M.size(i)/4)},...K(r.dims,a.dims,i)]})}},Ct=(e,t,r,a,n,o)=>{e.compute(wc(t,n??"",e.inputs[0],e.inputs[1],r,a,o))},Es=e=>{Ct(e,"Add",(t,r)=>`${t}+${r}`)},As=e=>{Ct(e,"Div",(t,r)=>`${t}/${r}`)},Ts=e=>{Ct(e,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4(${t}==${r})`},void 0,void 0,9)},Os=e=>{Ct(e,"Mul",(t,r)=>`${t}*${r}`)},ks=e=>{let t=D("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;Ct(e,"Pow",{scalar:(a,n)=>`pow_custom(${a},${n})`,vector:(a,n)=>`pow_vector_custom(${a},${n})`},` fn pow_custom(a : ${t}, b : ${t}) -> ${t} { if (b == ${t}(0.0)) { return ${t}(1.0); } else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) { return ${t}(pow(f32(a), f32(b))); // NaN } return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t==="i32"?"round":""}(pow(f32(abs(a)), f32(b)))); } fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>) -> vec4<${t}> { // TODO: implement vectorized pow return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w)); } `)},Ps=e=>{Ct(e,"Sub",(t,r)=>`${t}-${r}`)},Rs=e=>{Ct(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4(${t}>${r})`},void 0,void 0,9)},zs=e=>{Ct(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4(${t}<${r})`},void 0,void 0,9)},Bs=e=>{Ct(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4(${t}>=${r})`},void 0,void 0,9)},Ds=e=>{Ct(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4(${t}<=${r})`},void 0,void 0,9)}});var $c,_c,xc,Sc,Us,Ns,Ws=F(()=>{"use strict";ie();_e();Ye();be();$c=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let r=0,a=e[r],n=a.dataType,o=a.dims.length;e.forEach((s,l)=>{if(l!==r){if(s.dataType!==n)throw new Error("input tensors should be one type");if(s.dims.length!==o)throw new Error("input tensors should have the same shape");s.dims.forEach((i,c)=>{if(c!==t&&i!==a.dims[c])throw new Error("non concat dimensions must match")})}})},_c=(e,t)=>` fn calculateInputIndex(index: u32) -> u32 { let sizeInConcatAxis = array(${t}); for (var i: u32 = 0u; i < ${e}; i += 1u ) { if (index < sizeInConcatAxis[i]) { return i; } } return ${e}u; }`,xc=(e,t)=>{let r=e.length,a=[];for(let n=0;n{let n=M.size(r),o=new Array(e.length),s=new Array(e.length),l=0,i=[],c=[],f=[{type:12,data:n}];for(let $=0;$`uniforms.sizeInConcatAxis${$}`).join(","),b=$=>` ${(()=>{$.registerUniform("outputSize","u32");for(let I=0;I(${w}); ${h} -= sizeInConcatAxis[inputIndex - 1u]; } ${xc(s,m)} }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:i},getRunData:()=>({outputs:[{dims:r,dataType:a}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:f}),getShaderSource:b}},Us=(e,t)=>{let r=e.inputs,a=r[0].dims,n=M.normalizeAxis(t.axis,a.length);$c(r,n);let o=a.slice();o[n]=r.reduce((l,i)=>l+(i.dims.length>n?i.dims[n]:0),0);let s=r.filter(l=>M.size(l.dims)>0);e.compute(Sc(s,n,o,r[0].dataType),{inputs:s})},Ns=e=>$e({axis:e.axis})});var ht,gt,yt,_n,Mt=F(()=>{"use strict";ie();_e();ht=(e,t,r="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},gt=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},yt=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},_n=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[r,a]=e?.activation_params||[.2,.5];return{activation:t,alpha:r,beta:a}}else if(t==="Clip"){let[r,a]=e?.activation_params||[pn,fn];return{activation:t,clipMax:a,clipMin:r}}else if(t==="LeakyRelu"){let[r]=e?.activation_params||[.01];return{activation:t,alpha:r}}return{activation:t}}});var Qe,xn,Sn=F(()=>{"use strict";Qe=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},xn=e=>` ${e?"value = value + getBiasByOutputCoords(coords);":""} `});var Cn,wa=F(()=>{"use strict";Cn=e=>` fn getIndexFromCoords4D(coords : vec4, shape : vec4) -> i32 { return dot(coords, vec4( shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1)); } fn getOutputIndexFromCoords(coords : vec4) -> i32 { return dot(coords, vec4( i32(${e}.x), i32(${e}.y), i32(${e}.z), 1)); } `});var Cc,Ic,Tr,Vs,Ec,Or,Ac,In,kr=F(()=>{"use strict";ie();_e();be();Mt();Sn();Cc=(e,t)=>e?` mm_Asub[inputRow][inputCol] = mm_readA(batch, kStart + inputRow, globalRowStart / innerElementSize + inputCol${t?", batchIndices":""}); `:` mm_Asub[inputRow][inputCol] = mm_readA(batch, globalRow + innerRow, kStart / innerElementSize + inputCol${t?", batchIndices":""}); `,Ic=(e,t)=>e?` let ACached0 = mm_Asub[k * innerElementSize][localRow]; let ACached1 = mm_Asub[k * innerElementSize + 1][localRow]; let ACached2 = mm_Asub[k * innerElementSize + 2][localRow]; ${t===3?"":"let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"} for (var i = 0; i < rowPerThread; i = i + 1) { acc[i] = BCached0 * ACached0[i] + acc[i]; acc[i] = BCached1 * ACached1[i] + acc[i]; acc[i] = BCached2 * ACached2[i] + acc[i]; ${t===3?"":"acc[i] = BCached3 * ACached3[i] + acc[i];"} }`:` for (var i = 0; i < rowPerThread; i = i + 1) { let ACached = mm_Asub[tileRow + i][k]; acc[i] = BCached0 * ACached.x + acc[i]; acc[i] = BCached1 * ACached.y + acc[i]; acc[i] = BCached2 * ACached.z + acc[i]; ${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"} }`,Tr=(e,t,r="f32",a,n=!1,o=32,s=!1,l=32)=>{let i=t[1]*e[1],c=t[0]*e[0],f=n?i:o,m=n?o:i,h=f/t[0],w=o/t[1];if(!((n&&h===4&&e[1]===4||!n&&(h===3||h===4))&&f%t[0]===0&&o%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${n} is true, innerElementSize ${h} and workPerThread[1] ${e[1]} must be 4. Otherwise, innerElementSize ${h} must be 3 or 4. tileAWidth ${f} must be divisible by workgroupSize[0]${t[0]}. tileInner ${o} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return` var mm_Asub: array, ${f/h}>, ${m}>; var mm_Bsub: array, ${c/e[0]}>, ${o}>; const rowPerThread = ${e[1]}; const colPerThread = ${e[0]}; const innerElementSize = ${h}; const tileInner = ${o}; @compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) fn main(@builtin(local_invocation_id) localId : vec3, @builtin(global_invocation_id) globalId : vec3, @builtin(workgroup_id) workgroupId : vec3) { let localRow = i32(localId.y); let tileRow = localRow * rowPerThread; let tileCol = i32(localId.x); let globalRow =i32(globalId.y) * rowPerThread; let globalCol = i32(globalId.x); let batch = ${s?"0":"i32(globalId.z)"}; ${a?`let batchIndices = ${a.offsetToIndices("u32(batch)")};`:""} let globalRowStart = i32(workgroupId.y) * ${i}; let num_tiles = ${s?`${Math.ceil(l/o)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; var kStart = ${s?`i32(globalId.z) * ${l}`:"0"}; var acc: array, rowPerThread>; // Loop over shared dimension. let tileRowB = localRow * ${w}; for (var t = 0; t < num_tiles; t = t + 1) { // Load one tile of A into local memory. for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { let inputRow = tileRow + innerRow; let inputCol = tileCol; ${Cc(n,a)} } // Load one tile of B into local memory. for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) { let inputRow = tileRowB + innerRow; let inputCol = tileCol; mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${a?", batchIndices":""}); } kStart = kStart + tileInner; workgroupBarrier(); // Compute acc values for a single thread. for (var k = 0; k < tileInner / innerElementSize; k = k + 1) { let BCached0 = mm_Bsub[k * innerElementSize][tileCol]; let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol]; let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol]; ${h===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"} ${Ic(n,h)} } workgroupBarrier(); } for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]); } }`},Vs=(e,t)=>e?` mm_Asub[inputRow][inputCol] = mm_readA(batch, kStart + inputRow, globalRowStart + inputCol${t?", batchIndices":""}); `:` mm_Asub[inputRow][inputCol] = mm_readA(batch, globalRowStart + inputRow, kStart + inputCol${t?", batchIndices":""}); `,Ec=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",Or=(e,t,r="f32",a,n=!1,o=32,s=!1,l=32,i=!1)=>{let c=e[1]*t[1],f=e[0]*t[0],m=n?c:o,h=n?o:c;if(!(h%t[1]===0&&m%t[0]===0&&o%t[1]===0))throw new Error(`tileAHight ${h} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${m} must be divisible by workgroupSize[0]${t[0]}, tileInner ${o} must be divisible by workgroupSize[1]${t[1]}`);let w=h/t[1],b=m/t[0],$=o/t[1],I=i?` let localRow = i32(localId.y); let localCol = i32(localId.x); let globalRowStart = i32(workgroupId.y) * ${c}; let globalColStart = i32(workgroupId.x) * ${f}; // Loop over shared dimension. for (var t = 0; t < num_tiles; t = t + 1) { // Load one tile of A into local memory. for (var inputRow = localRow; inputRow < ${h}; inputRow = inputRow + ${t[1]}) { for (var inputCol = localCol; inputCol < ${m}; inputCol = inputCol + ${t[0]}) { ${Vs(n,a)} } } // Load one tile of B into local memory. for (var inputRow = localRow; inputRow < ${o}; inputRow = inputRow + ${t[1]}) { for (var inputCol = localCol; inputCol < ${f}; inputCol = inputCol + ${t[0]}) { mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalColStart + inputCol${a?", batchIndices":""}); } } kStart = kStart + tileInner; workgroupBarrier(); // Compute acc values for a single thread. var BCached : array<${r}, colPerThread>; for (var k = 0; k < tileInner; k = k + 1) { for (var inner = 0; inner < colPerThread; inner = inner + 1) { BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}]; } for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { let ACached = ${n?`mm_Asub[k][localRow + innerRow * ${t[1]}];`:`mm_Asub[localRow + innerRow * ${t[1]}][k];`} for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol]; } } } workgroupBarrier(); } for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { let gRow = globalRowStart + localRow + innerRow * ${t[1]}; for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { let gCol = globalColStart + localCol + innerCol * ${t[0]}; mm_write(batch, gRow, gCol, acc[innerRow][innerCol]); } } `:` let tileRow = i32(localId.y) * rowPerThread; let tileCol = i32(localId.x) * colPerThread; let globalRow = i32(globalId.y) * rowPerThread; let globalCol = i32(globalId.x) * colPerThread; let globalRowStart = i32(workgroupId.y) * ${c}; let tileRowA = i32(localId.y) * ${w}; let tileColA = i32(localId.x) * ${b}; let tileRowB = i32(localId.y) * ${$}; // Loop over shared dimension. for (var t = 0; t < num_tiles; t = t + 1) { // Load one tile of A into local memory. for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) { for (var innerCol = 0; innerCol < ${b}; innerCol = innerCol + 1) { let inputRow = tileRowA + innerRow; let inputCol = tileColA + innerCol; ${Vs(n,a)} } } // Load one tile of B into local memory. for (var innerRow = 0; innerRow < ${$}; innerRow = innerRow + 1) { for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { let inputRow = tileRowB + innerRow; let inputCol = tileCol + innerCol; mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol + innerCol${a?", batchIndices":""}); } } kStart = kStart + tileInner; workgroupBarrier(); // Compute acc values for a single thread. var BCached : array<${r}, colPerThread>; for (var k = 0; k < tileInner; k = k + 1) { for (var inner = 0; inner < colPerThread; inner = inner + 1) { BCached[inner] = mm_Bsub[k][tileCol + inner]; } for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { ${Ec(n)} for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol]; } } } workgroupBarrier(); } for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { mm_write(batch, globalRow + innerRow, globalCol + innerCol, acc[innerRow][innerCol]); } } `;return` var mm_Asub : array, ${h}>; var mm_Bsub : array, ${o}>; const rowPerThread = ${e[1]}; const colPerThread = ${e[0]}; const tileInner = ${o}; @compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) fn main(@builtin(local_invocation_id) localId : vec3, @builtin(global_invocation_id) globalId : vec3, @builtin(workgroup_id) workgroupId : vec3) { let batch = ${s?"0":"i32(globalId.z)"}; ${a?`let batchIndices = ${a.offsetToIndices("u32(batch)")};`:""} let num_tiles = ${s?`${Math.ceil(l/o)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; var kStart = ${s?`i32(globalId.z) * ${l}`:"0"}; var acc : array, rowPerThread>; // Without this initialization strange values show up in acc. for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { acc[innerRow][innerCol] = 0.0; } } ${I} } `},Ac=(e,t,r,a,n,o=!1)=>{let[s,l,i]=n,[c,f,m,h]=a,w=ar(s,i),b=ar(l,i),$=ke(a[0].type.tensor),I=()=>{let E=f.rank,A=c.rank,k=`var aIndices: ${f.type.indices};`;for(let U=E-2-1,N=A-1;U>=0;U--,N--)k+=` aIndices[${U}] = ${A>1?`batchIndices[${N}]`:"batchIndices"};`;return w.forEach(U=>{k+=` aIndices[${U}] = 0;`}),k+=` aIndices[${E-2}] = u32(row); aIndices[${E-1}] = u32(colIn);`,k},S=()=>{let E=m.rank,A=c.rank,k=`var bIndices: ${m.type.indices};`;for(let U=E-2-1,N=A-1;U>=0;U--,N--)k+=` bIndices[${U}] = ${A>1?`batchIndices[${N}]`:"batchIndices"};`;return b.forEach(U=>{k+=` bIndices[${U}] = 0;`}),k+=` bIndices[${E-2}] = u32(row); bIndices[${E-1}] = u32(colIn);`,k};return` fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${c.type.indices}) -> ${Qe(e,$)} { var value = ${Qe(e,$)}(0.0); let col = colIn * ${e}; if(row < uniforms.dim_a_outer && col < uniforms.dim_inner) { ${I()} value = ${f.getByIndices("aIndices")}; } return value; } fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${c.type.indices}) -> ${Qe(e,$)} { var value = ${Qe(e,$)}(0.0); let col = colIn * ${e}; if(row < uniforms.dim_inner && col < uniforms.dim_b_outer) { ${S()} value = ${m.getByIndices("bIndices")}; } return value; } fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${Qe(e,$)}) { let col = colIn * ${e}; if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { var value = valueIn; let coords = vec3(batch, row, colIn); ${t?`value = value + ${o?"bias[colIn]":`${Qe(e,$)}(bias[row])`};`:""} ${r} ${h.setByIndices("vec3(coords)","value")} } } `},In=(e,t,r,a,n=!1)=>{let o=e[0].dims,s=e[1].dims,l=o.slice(0,-2),i=s.slice(0,-2),c=a?a.slice(0,-2):r.slice(0,-2),f=M.size(c),m=o[o.length-2],h=o[o.length-1],w=s[s.length-1],b=h%4===0&&w%4===0,$=m<=8?[4,1,1]:[4,4,1],I=[8,8,1],S=[Math.ceil(w/I[0]/$[0]),Math.ceil(m/I[1]/$[1]),Math.ceil(f/I[2]/$[2])],v=b?4:1,E=[...l,m,h/v],A=E.length,k=[...i,h,w/v],U=k.length,N=[f,m,w/v],H=[{type:6,data:m},{type:6,data:w},{type:6,data:h}];gt(t,H),H.push(...K(c,E,k));let z=["rank","rank"],L=e.length>2;L&&(H.push(...K(e[2].dims)),z.push("rank")),H.push(...K(N));let pe=Ie=>{let we=c.length,ne=hn("batchDims",e[0].dataType,we,1),Ue=ke(e[0].dataType),X=D("a",e[0].dataType,A,v),xe=D("b",e[1].dataType,U,v),fe=q("result",e[0].dataType,N.length,v),ue=[X,xe];if(L){let G=n?v:1;ue.push(D("bias",e[2].dataType,e[2].dims.length,G))}let se=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];yt(t,se);let he=ke(fe.type.tensor),Ee=ht(t,fe.type.value,he),Le=Ac(v,L,Ee,[ne,X,xe,fe],[l,i,c],n);return` ${Ie.registerUniforms(se).registerInternalVariables(ne).declareVariables(...ue,fe)} ${Le} ${b?Tr($,I,Ue,ne):Or($,I,Ue,ne)} `};return{name:"MatMul",shaderCache:{hint:`${$};${t.activation};${b};${n}`,inputDependencies:z},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:H}),getShaderSource:pe}}});var Tc,Gs,Hs=F(()=>{"use strict";ie();Dt();be();Mt();Sn();wa();kr();Tc=(e,t,r,a,n=!1,o,s=4,l=4,i=4,c="f32")=>{let f=L=>{switch(L){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${c}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${L} is not supported.`)}},m=L=>{switch(L){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${L} is not supported.`)}},h=e?` let coord = vec4(batch, xRow, xCol, xCh); `:` let coord = vec4(batch, xCh, xRow, xCol); `,w=e?` let coords = vec4( batch, row / outWidth, row % outWidth, col); `:` let coords = vec4( batch, row, col / outWidth, col % outWidth); `,b=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",$=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",I=e?"row":"col",S=e?"col":"row",v=` let inChannels = i32(uniforms.w_shape[2]); let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; let outRow = ${I} / outWidth; let outCol = ${I} % outWidth; let WRow = ${S} / (i32(uniforms.w_shape[1]) * inChannels); let WCol = ${S} / inChannels % i32(uniforms.w_shape[1]); let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0]; let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1]; let xCh = ${S} % inChannels; var resData = ${Qe(s,c)}(0.0); // The bounds checking is always needed since we use it to pad zero for // the 'same' padding type. if (xRow >= 0 && xRow < ${b} && xCol >= 0 && xCol < ${$}) { ${h} let xIndex = getIndexFromCoords4D(coord, vec4(uniforms.x_shape)); ${f(s)} } return resData;`,E=e?t&&a?` let col = colIn * ${s}; ${v}`:` let col = colIn * ${s}; if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { ${v} } return ${Qe(s,c)}(0.0);`:a&&r?` let col = colIn * ${s}; ${v}`:` let col = colIn * ${s}; if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { ${v} } return ${Qe(s,c)}(0.0);`,A=`${m(l)}`,k=Qe(i,c),U=e?Qe(s,c):Qe(l,c),N=e?Qe(l,c):Qe(s,c),H=ht(o,k,c);return` fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${U} { ${e?E:A} } fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${N} { ${e?A:E} } fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${k}) { let col = colIn * ${i}; if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { var value = valueIn; let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; ${w} ${xn(n)} ${H} setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value); } }`},Gs=(e,t,r,a,n,o,s,l)=>{let i=t.format==="NHWC",c=i?e[0].dims[3]:e[0].dims[1],f=r[0],m=i?r[2]:r[3],h=i?r[1]:r[2],w=i?r[3]:r[1],b=i&&(c%4===0||c%3===0)&&w%4===0,$=i?w:m*h,I=i?m*h:w,S=[8,8,1],v=a<=8?[4,1,1]:[4,4,1],E=[Math.ceil($/S[0]/v[0]),Math.ceil(I/S[1]/v[1]),Math.ceil(f/S[2]/v[2])];De("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${E}`);let A=b?i&&c%4!==0?3:4:1,k=S[1]*v[1],U=S[0]*v[0],N=Math.max(S[0]*A,S[1]),H=a%k===0,z=n%U===0,L=o%N===0,pe=b?[A,4,4]:[1,1,1],Ie=[{type:6,data:a},{type:6,data:n},{type:6,data:o},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];gt(t,Ie),Ie.push(...K(e[0].dims,e[1].dims));let we=["rank","rank"];s&&(Ie.push(...K(e[2].dims)),we.push("rank")),Ie.push(...K(r));let ne=Ue=>{let X=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];yt(t,X);let xe=b?4:1,fe=ke(e[0].dataType),ue=` fn setOutputAtIndex(flatIndex : i32, value : ${b?`vec4<${fe}>`:fe}) { result[flatIndex] = ${b?`vec4<${fe}>`:fe}(value); } fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${b?`vec4<${fe}>`:fe}) { let flatIndex = getOutputIndexFromCoords(vec4(d0, d1, d2, d3)); setOutputAtIndex(flatIndex ${b?"/ 4":""}, value); }`,se=D("x",e[0].dataType,e[0].dims.length,A===3?1:A),he=D("w",e[1].dataType,e[1].dims.length,xe),Ee=[se,he],Le=q("result",e[0].dataType,r.length,xe);if(s){let G=D("bias",e[2].dataType,e[2].dims.length,xe);Ee.push(G),ue+=` fn getBiasByOutputCoords(coords : vec4) -> ${b?`vec4<${fe}>`:fe} { return bias[coords.${i?"w":"y"}${b?"/ 4":""}]; }`}return` ${Cn("uniforms.result_strides")} //struct Uniforms { xShape : vec4, wShape : vec4, outShape : vec4, // outShapeStrides: vec3, filterDims : vec2, pad : vec2, stride : vec2, // dilation : vec2, dimAOuter : i32, dimBOuter : i32, dimInner : i32 }; ${Ue.registerUniforms(X).declareVariables(...Ee,Le)} ${ue} ${Tc(i,H,z,L,s,t,pe[0],pe[1],pe[2],fe)} ${b?Tr(v,S,fe,void 0,!i,N):Or(v,S,fe,void 0,!i,N,!1,void 0,l)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${A};${b};${H};${z};${L};${k};${U};${N}`,inputDependencies:we},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:E[0],y:E[1],z:E[2]},programUniforms:Ie}),getShaderSource:ne}}});var va,Ls,Fs=F(()=>{"use strict";ie();_e();be();$a();Mt();va=(e,t,r)=>{let a=e.length>2,n=a?"value += b[output_channel];":"",o=e[0].dims,s=e[1].dims,l=s[0]/t.group,i=t.format==="NHWC",c=En(o,s,t.dilations,t.pads,t.strides,i),f=M.size(c),m=[{type:12,data:f},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:l}];gt(t,m),m.push(...K(o,s,c));let h=["rank","rank"];a&&(m.push(...K(e[2].dims)),h.push("rank")),m.push(...K(c));let w=b=>{let $=q("output",e[0].dataType,c.length),I=ke($.type.tensor),S=ht(t,$.type.value,I),v=D("x",e[0].dataType,o.length),E=D("w",e[1].dataType,s.length),A=[v,E];a&&A.push(D("b",e[2].dataType,e[2].dims));let k=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];return yt(t,k),` ${b.registerUniforms(k).declareVariables(...A,$)} ${b.mainStart()} ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let outputIndices = ${$.offsetToIndices("global_idx")}; let batch: u32 = outputIndices[0]; let output_channel: u32 = outputIndices[${i?3:1}]; let xRCCorner: vec2 = vec2(outputIndices[${i?1:2}], outputIndices[${i?2:3}]) * uniforms.strides - uniforms.pads; let group_id: u32 = output_channel / uniforms.output_channels_per_group; var value: ${$.type.value} = ${$.type.value}(0); for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) { let input_channel = group_id * uniforms.w_shape[1] + wInChannel; for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) { let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; if (xHeight < 0u || xHeight >= uniforms.x_shape[${i?1:2}]) { continue; } for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) { let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; if (xWidth < 0u || xWidth >= uniforms.x_shape[${i?2:3}]) { continue; } let xVal = ${i?v.get("batch","xHeight","xWidth","input_channel"):v.get("batch","input_channel","xHeight","xWidth")}; let wVal = ${E.get("output_channel","wInChannel","wHeight","wWidth")}; value += xVal*wVal; } } } ${n} ${S} ${$.setByOffset("global_idx","value")} }`};return{name:"GroupedConv",shaderCache:{hint:t.cacheKey,inputDependencies:h},getRunData:()=>({outputs:[{dims:r?r(c):c,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:m}),getShaderSource:w}},Ls=(e,t,r)=>{let a=e.length>2,n=Me(r[3]),o=Me(r[2]),s=M.size(r)/n/o,l=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/n],i=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/n],c=[r[0],r[1],r[2],r[3]/n],f=[{type:12,data:s},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];gt(t,f),f.push(...K(l,i,c));let m=(o-1)*t.strides[1]+i[1],h=w=>{let b=q("output",e[0].dataType,c.length,n),$=ke(b.type.tensor),I=ht(t,b.type.value,$),S=D("x",e[0].dataType,l.length,n),v=D("w",e[1].dataType,i.length,n),E=[S,v];a&&E.push(D("b",e[2].dataType,e[2].dims,n));let A=a?"value += b[output_channel];":"",k=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return yt(t,k),` ${w.registerUniforms(k).declareVariables(...E,b)} ${w.mainStart()} ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let width0 = uniforms.output_shape[3]; let output_channel = global_idx % width0; var index1 = global_idx / width0; let width1 = uniforms.output_shape[2] / ${o}u; let col = (index1 % width1) * ${o}u; index1 = index1 / width1; let row = index1 % uniforms.output_shape[1]; let batch = index1 / uniforms.output_shape[1]; let x_corner = vec2(i32(row), i32(col)) * uniforms.strides - uniforms.pads; var x_vals: array<${S.type.value}, ${m}>; var values: array<${b.type.value}, ${o}>; let input_channel = output_channel; // Use constant instead of uniform can give better performance for w's height/width. for (var w_height: u32 = 0u; w_height < ${i[0]}; w_height++) { let x_height = x_corner.x + i32(w_height); if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) { for (var i = 0; i < ${m}; i++) { let x_width = x_corner.y + i; if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) { x_vals[i] = ${S.get("batch","u32(x_height)","u32(x_width)","input_channel")}; } else { x_vals[i] = ${S.type.value}(0); } } for (var w_width: u32 = 0u; w_width < ${i[1]}; w_width++) { let w_val = ${v.get("w_height","w_width","0","output_channel")}; for (var i = 0u; i < ${o}u; i++) { values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]); } } } } for (var i = 0u; i < ${o}u; i++) { var value = values[i]; ${A} ${I} ${b.set("batch","row","col + i","output_channel","value")}; } }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${n};${o};${m};${i[0]};${i[1]}`,inputDependencies:a?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:f}),getShaderSource:h}}});var _a,Oc,qs,xa=F(()=>{"use strict";ie();_e();kr();be();Mt();_a=(e,t,r,a,n=!1)=>{let o=e[0].dims,s=e[1].dims,l=o[o.length-2],i=s[s.length-1],c=o[o.length-1],f=Me(i),m=Me(c),h=Me(l),w=M.size(r)/f/h,b=e.length>2,$=a?a.slice(0,-2):r.slice(0,-2),S=[M.size($),l,i],v=[{type:12,data:w},{type:12,data:l},{type:12,data:i},{type:12,data:c}];gt(t,v),v.push(...K($,o,s)),b&&v.push(...K(e[2].dims)),v.push(...K(S));let E=A=>{let k=hn("batch_dims",e[0].dataType,$.length),U=D("a",e[0].dataType,o.length,m),N=D("b",e[1].dataType,s.length,f),H=q("output",e[0].dataType,S.length,f),z=ke(H.type.tensor),L=ht(t,H.type.value,z),pe=[U,N],Ie="";if(b){let se=n?f:1;pe.push(D("bias",e[2].dataType,e[2].dims.length,se)),Ie=`${n?`value += bias[col / ${se}];`:`value += ${H.type.value}(bias[row + i]);`}`}let we=o.slice(0,-2),ne=s.slice(0,-2),Ue=ar(we,$),X=ar(ne,$),xe=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];yt(t,xe);let fe=(se,he)=>{let Ee=se.rank,Le=se.name;if(Ee===2)return`var ${Le}_indices = ${se.type.indices}(0u, 0u);`;let G=k.rank,J=`var ${Le}_indices: ${se.type.indices};`;for(let Se=Ee-2-1,Je=G-1;Se>=0;Se--,Je--)J+=` ${Le}_indices[${Se}] = ${G>1?`batch_indices[${Je}]`:"batch_indices"};`;return he.forEach(Se=>{J+=` ${Le}_indices[${Se}] = 0;`}),J+=`${Le}_indices[${Ee-2}] = 0u; ${Le}_indices[${Ee-1}] = 0u;`,J},ue=()=>{let se=`var a_data: ${U.type.value};`;for(let he=0;he; for (var k: u32 = 0u; k < uniforms.K; k = k + ${m}) { ${ue()} } for (var i = 0u; i < ${h}u; i++) { var value = values[i]; ${Ie} ${L} let cur_indices = ${H.type.indices}(batch, row + i, col); let offset = ${H.indicesToOffset("cur_indices")}; ${H.setByOffset(`offset / ${f}`,"value")}; } } `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${f};${m};${h};${n}`,inputDependencies:b?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:v}),getShaderSource:E}},Oc=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},qs=e=>{Oc(e.inputs);let t=vt.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let r=t[t.length-1],a=e.inputs[0].dims[e.inputs[0].dims.length-1];r<8&&a<8?e.compute(_a(e.inputs,{activation:""},t)):e.compute(In(e.inputs,{activation:""},t))}});var En,Sa,kc,js,Ca,Pc,Rc,Ia,$a=F(()=>{"use strict";_e();Hs();kr();Fs();Mt();xa();ir();En=(e,t,r,a,n,o)=>{let s=e[0],l=e.slice(o?1:2,o?3:4),i=l.length,c=t[0],m=t.slice(2).map((b,$)=>b+(b-1)*(r[$]-1)),w=l.map((b,$)=>b+a[$]+a[$+i]).map((b,$)=>Math.floor((b-m[$]+n[$])/n[$]));return w.splice(0,0,s),w.splice(o?3:1,0,c),w},Sa=[2,3,1,0],kc=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support conv 1D and 2D");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],a=e[1].dims[1]*t.group;if(r!==a)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let n=e[0].dims.length-2;if(t.dilations.length!==n)throw new Error(`dilations should be ${n}D`);if(t.strides.length!==n)throw new Error(`strides should be ${n}D`);if(t.pads.length!==n*2)throw new Error(`pads should be ${n*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},js=(e,t)=>{let r=e.kernelShape.slice();for(let o=2;o{let t=_n(e),r=e.format,a=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],n=e.dilations,o=e.group,s=e.kernel_shape,l=e.pads,i=e.strides,c=e.w_is_const();return{autoPad:a,format:r,dilations:n,group:o,kernelShape:s,pads:l,strides:i,wIsConst:c,...t,cacheKey:`${e.format};${t.activation};`}},Pc=(e,t,r)=>{let a=js(r,t),n=r.format==="NHWC";if(r.group!==1){if(!e.adapterInfo.isArchitecture("ampere")&&n&&t[1].dims[0]===r.group&&t[1].dims[1]===1&&r.dilations[0]===1&&r.dilations[1]===1){let N=En(t[0].dims,t[1].dims,r.dilations,a.pads,r.strides,n),H=e.kernelCustomData.wT??e.compute(dt(t[1],Sa),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=H);let z=[t[0],H];t.length===3&&z.push(t[2]),e.compute(Ls(z,a,N),{inputs:z})}else e.compute(va(t,a));return}let o=t.length===3,s=t[0].dims[n?1:2],l=t[0].dims[n?2:3],i=t[0].dims[n?3:1],c=t[1].dims[2],f=t[1].dims[3],m=En(t[0].dims,t[1].dims,r.dilations,a.pads,r.strides,n),h=m[n?1:2],w=m[n?2:3],b=m[n?3:1],$=n&&c===s&&f===l&&r.pads[0]===0&&r.pads[1]===0;if($||c===1&&f===1&&r.dilations[0]===1&&r.dilations[1]===1&&r.strides[0]===1&&r.strides[1]===1&&r.pads[0]===0&&r.pads[1]===0){let U=m[0],N,H,z,L=[];if(n){let we=e.kernelCustomData.wT??e.compute(dt(t[1],Sa),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];if(r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=we),$){let ne=s*l*i;N=t[0].reshape([1,U,ne]),H=we.reshape([1,ne,b]),z=[1,U,b]}else N=t[0].reshape([U,s*l,i]),H=we.reshape([1,i,b]),z=[U,h*w,b];L.push(N),L.push(H)}else N=t[0].reshape([U,i,s*l]),H=t[1].reshape([1,b,i]),z=[U,b,h*w],L.push(H),L.push(N);o&&L.push(t[2]);let pe=z[2],Ie=L[0].dims[L[0].dims.length-1];pe<8&&Ie<8?e.compute(_a(L,a,m,z,n),{inputs:L}):e.compute(In(L,a,m,z,n),{inputs:L});return}let I=!0,S=e.kernelCustomData.wT??e.compute(dt(t[1],Sa),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=S);let v=[t[0],S];o&&v.push(t[2]);let E=n?h*w:b,A=n?b:h*w,k=c*f*i;e.compute(Gs(v,a,m,E,A,k,o,I),{inputs:v})},Rc=(e,t)=>{let r=t.format==="NHWC",a=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&a.push(e.inputs[2]);let n=[0,t.pads[0],0,t.pads[1]],o=[1].concat(t.strides),s=[1].concat(t.dilations),l=[1].concat(t.kernelShape),i=js({...t,pads:n,strides:o,dilations:s,kernelShape:l},a);e.compute(va(a,i,c=>r?[c[0],c[2],c[3]]:[]))},Ia=(e,t)=>{kc(e.inputs,t),e.inputs[0].dims.length===3?Rc(e,t):Pc(e,e.inputs,t)}});var zc,Ks,Ys=F(()=>{"use strict";ie();Dt();be();Mt();Sn();wa();kr();zc=(e,t=!1,r,a,n=4)=>{let o=S=>{switch(S){case 1:return"return w[getIndexFromCoords4D(coord, vec4(uniforms.w_shape))];";case 4:return` let coord1 = vec4(coordX, coordY, col + 1, rowInner); let coord2 = vec4(coordX, coordY, col + 2, rowInner); let coord3 = vec4(coordX, coordY, col + 3, rowInner); let v0 = w[getIndexFromCoords4D(coord, vec4(uniforms.w_shape))]; let v1 = w[getIndexFromCoords4D(coord1, vec4(uniforms.w_shape))]; let v2 = w[getIndexFromCoords4D(coord2, vec4(uniforms.w_shape))]; let v3 = w[getIndexFromCoords4D(coord3, vec4(uniforms.w_shape))]; return ${a}(v0, v1, v2, v3); `;default:throw new Error(`innerElementSize ${S} is not supported.`)}},s=e?` let coord = vec4(batch, iXR, iXC, xCh); `:` let coord = vec4(batch, xCh, iXR, iXC); `,l=e?` let coords = vec4( batch, row / outWidth, row % outWidth, col); `:` let coords = vec4( batch, row, col / outWidth, col % outWidth); `,i=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",c=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",f=e?"row":"col",m=e?"col":"row",h=` let inChannels = ${e?"i32(uniforms.x_shape[3])":"i32(uniforms.x_shape[1])"}; let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; let outRow = ${f} / outWidth; let outCol = ${f} % outWidth; let WRow = ${m} / (uniforms.filter_dims[1] * inChannels); let WCol = ${m} / inChannels % uniforms.filter_dims[1]; let xR = f32(outRow - uniforms.pads[0] + uniforms.dilations[0] * WRow) / f32(uniforms.strides[0]); let xC = f32(outCol - uniforms.pads[1] + uniforms.dilations[1] * WCol) / f32(uniforms.strides[1]); if (xR < 0.0 || xR >= f32(${i}) || fract(xR) > 0.0) { return ${a}(0.0); } if (xC < 0.0 || xC >= f32(${c}) || fract(xC) > 0.0) { return ${a}(0.0); } let iXR = i32(xR); let iXC = i32(xC); let xCh = ${m} % inChannels; ${s} return x[getIndexFromCoords4D(coord, vec4(uniforms.x_shape))/${n}];`,w=e?` let col = colIn * ${n}; if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { ${h} } return ${a}(0.0);`:` let col = colIn * ${n}; if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { ${h} } return ${a}(0.0);`,b=` let col = colIn * ${n}; let inChannels = ${e?"i32(uniforms.x_shape[3])":"i32(uniforms.x_shape[1])"}; let coordX = uniforms.filter_dims[0] - 1 - row / (uniforms.filter_dims[1] * inChannels); let coordY = uniforms.filter_dims[1] - 1 - (row / inChannels) % uniforms.filter_dims[1]; if (${e?"row < uniforms.dim_inner && col < uniforms.dim_b_outer":"row < uniforms.dim_inner && col < uniforms.dim_a_outer"} && coordX >= 0 && coordY >= 0) { let rowInner = row % inChannels; let coord = vec4(coordX, coordY, col, rowInner); ${o(n)} } return ${a}(0.0); `,$=ht(r,a);return` fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${a} { ${e?w:b} } fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${a} { ${e?b:w} } fn mm_write(batch: i32, row : i32, colIn : i32, valueInput : ${a}) { let col = colIn * ${n}; if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { var value = valueInput; let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; ${l} ${xn(t)} ${$} result[getIndexFromCoords4D(coords, vec4(uniforms.result_shape))/${n}] = value; } }`},Ks=(e,t,r,a,n,o,s,l)=>{let i=t.format==="NHWC",c=i?e[0].dims[3]:e[0].dims[1],f=r[0],m=i?r[2]:r[3],h=i?r[1]:r[2],w=i?r[3]:r[1],b=i?c%4===0&&w%4===0:m%4===0&&w%4===0,$=i?w:m*h,I=i?m*h:w,S=b?[8,8,1]:[$<=4||I<=4?4:16,$>4&&I<=4?4:16,1],v=b?[4,4,1]:[$<=4?1:4,$>4&&I<=4?1:4,1],E=[Math.ceil($/S[0]/v[0]),Math.ceil(I/S[1]/v[1]),Math.ceil(f/S[2]/v[2])];De("verbose",()=>`[conv_backprop_mm_webgpu] dispatch = ${E}`);let A=b?4:1,k=Math.max(S[0]*A,S[1]),U=b?4:1,N=[t.kernelShape[i?1:2],t.kernelShape[i?2:3]],H=[N[0]+(t.dilations[0]<=1?0:(N[0]-1)*(t.dilations[0]-1)),N[1]+(t.dilations[1]<=1?0:(N[1]-1)*(t.dilations[1]-1))],z=[H[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),H[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],L=[{type:6,data:a},{type:6,data:n},{type:6,data:o},{type:6,data:t.strides},{type:6,data:t.dilations},{type:6,data:N},{type:6,data:z}];gt(t,L),L.push(...K(e[0].dims,e[1].dims));let pe=["rank","rank"];s&&(L.push(...K(e[2].dims)),pe.push("rank")),L.push(...K(r));let Ie=we=>{let ne=D("x",e[0].dataType,e[0].dims.length,U),Ue=D("w",e[1].dataType,e[1].dims.length,1),X=q("result",e[0].dataType,r.length,U),xe=[ne,Ue],fe="";if(s){let he=D("bias",e[2].dataType,e[2].dims.length,U);xe.push(he),fe+=` fn getBiasByOutputCoords(coords : vec4) -> ${he.type.value} { return bias[coords.${i?"w":"y"}${b?"/ 4":""}]; }`}let ue=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"strides",type:"i32",length:2},{name:"dilations",type:"i32",length:2},{name:"filter_dims",type:"i32",length:N.length},{name:"pads",type:"i32",length:z.length}];yt(t,ue);let se=ke(e[0].dataType,1);if(se!=="f16"&&se!=="f32")throw new Error(`elemType ${se} is not supported.`);return` ${Cn("uniforms.result_strides")} ${we.registerUniforms(ue).declareVariables(...xe,X)}; ${fe} ${zc(i,s,t,ne.type.value,A)} ${b?Tr(v,S,se,void 0,!i,k):Or(v,S,se,void 0,!i,k,!1,void 0,l)}`};return{name:"Conv2DTransposeMatMul",shaderCache:{hint:`${t.cacheKey};${v};${S};${b}`,inputDependencies:pe},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:E[0],y:E[1],z:E[2]},programUniforms:L}),getShaderSource:Ie}}});var Bc,Ea,Zs=F(()=>{"use strict";ie();Dt();_e();be();Bc=(e,t,r,a,n,o=!1,s,l,i=!1)=>{let c=i?1:2,f=i?2:3,m=i?3:1,h=o?2:1,w=` fn setOutputAtIndex(flatIndex : u32, value : ${o?`vec4<${s}>`:s}) { result[flatIndex] = ${o?`vec4<${s}>`:s}(value); }`;a&&(w+=` fn getBiasByOutputCoords(coords : vec4) -> ${o?`vec4<${s}>`:s} { return bias[coords.${i?"w":"y"}${o?"/ 4":""}]; }`);let b=o?4:1,$=D("W",t[1].dataType,t[1].dims.length,b),I=D("Dy",t[0].dataType,t[0].dims.length,b),S=[I,$];a&&S.push(D("bias",t[2].dataType,[r[m]].length,b));let v=q("result",t[0].dataType,r.length,b),E=`{ let batch: u32 = ${n?"global_id.z":"workgroup_id.z"} / uniforms.result_shape[1]; let r = ${n?"global_id.z":"workgroup_id.z"} % uniforms.result_shape[1]; let c = ${n?"global_id.y":"workgroup_id.y"} * ${h}; let d1: u32 = ${n?"global_id.x":"workgroup_id.x"} * 4; let dyCorner = vec2(i32(r), i32(c)) - vec2(uniforms.pads); // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). // ? = to be determined. : = across all values in that axis. var dotProd: array, ${h}>; for (var i = 0; i < ${h}; i++) { dotProd[i] = vec4<${s}>(0.0); } for (var wR: u32 = 0; wR < uniforms.filter_dims[0]; wR = wR + 1) { var dyR = (${s}(dyCorner.x) + ${s}(wR)) / ${s}(uniforms.strides.x); let wRPerm = uniforms.filter_dims[0] - 1 - wR; if (dyR < 0.0 || dyR >= ${s}(uniforms.Dy_shape[1]) || fract(dyR) > 0.0 || wRPerm < 0) { continue; } let idyR: u32 = u32(dyR); for (var wC: u32 = 0; wC < uniforms.filter_dims[1]; wC = wC + 1) { let dyC = (${s}(dyCorner.y) + ${s}(wC)) / ${s}(uniforms.strides.y); let dyC2 = (${s}(dyCorner.y) + 1.0 + ${s}(wC)) / ${s}(uniforms.strides.y); let wCPerm = uniforms.filter_dims[1] - 1 - wC; if (wCPerm < 0) { continue; } var bDyCVal = true; var bDyCVal2 = true; if (dyC < 0.0 || dyC >= ${s}(uniforms.Dy_shape[2]) || fract(dyC) > 0.0) { bDyCVal = false; } if (dyC2 < 0.0 || dyC2 >= ${s}(uniforms.Dy_shape[2]) || fract(dyC2) > 0.0) { bDyCVal2 = false; } let idyC: u32 = u32(dyC); let idyC2: u32 = u32(dyC2); if (bDyCVal && bDyCVal2) { let d2Length = uniforms.Dy_shape[3]; for (var d2 :u32 = 0; d2 < d2Length; d2 = d2 + 4) { let wValue0 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1","d2")}; let wValue1 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")}; let wValue2 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")}; let wValue3 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")}; var xValue = ${I.get("batch","idyR","idyC","d2")}; let tmpval = vec4<${s}>(dot(xValue, wValue0), dot(xValue, wValue1), dot(xValue, wValue2), dot(xValue, wValue3)); dotProd[0] = dotProd[0] + tmpval; xValue = ${I.get("batch","idyR","idyC2","d2")}; dotProd[1] = dotProd[1] + vec4<${s}>(dot(xValue, wValue0), dot(xValue, wValue1), dot(xValue, wValue2), dot(xValue, wValue3)); } } else if (bDyCVal) { let d2Length = uniforms.Dy_shape[${m}]; for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) { let wValue0 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1","d2")}; let wValue1 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")}; let wValue2 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")}; let wValue3 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")}; var xValue = ${I.get("batch","idyR","idyC","d2")}; let tmpval = vec4<${s}>(dot(xValue, wValue0), dot(xValue, wValue1), dot(xValue, wValue2), dot(xValue, wValue3)); dotProd[0] = dotProd[0] + tmpval; } } else if (bDyCVal2) { let d2Length = uniforms.Dy_shape[3]; for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) { let wValue0 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1","d2")}; let wValue1 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")}; let wValue2 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")}; let wValue3 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")}; var xValue = ${I.get("batch","idyR","idyC2","d2")}; let tmpval = vec4<${s}>(dot(xValue, wValue0), dot(xValue, wValue1), dot(xValue, wValue2), dot(xValue, wValue3)); dotProd[1] = dotProd[1] + tmpval; } } } } for (var i: u32 = 0; i < ${h}; i = i + 1) { let value = dotProd[i] + ${a?"bias[c+i]":`vec4<${s}>(0.0)`}; ${v.set("batch","r","c + i","d1","value")}; } }`,A=` let outputIndices = ${v.offsetToIndices("global_idx")}; let batch = ${v.indicesGet("outputIndices",0)}; let d1 = ${v.indicesGet("outputIndices",m)}; let r = ${v.indicesGet("outputIndices",c)}; let c = ${v.indicesGet("outputIndices",f)}; let dyCorner = vec2(i32(r), i32(c)) - uniforms.pads; let dyRCorner = dyCorner.x; let dyCCorner = dyCorner.y; let groupId = d1 / uniforms.output_channels_per_group; let wOutChannel = d1 - groupId * uniforms.output_channels_per_group; // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). // ? = to be determined. : = across all values in that axis. var dotProd = ${s}(0.0); for (var wR: u32 = 0; wR < uniforms.effective_filter_dims.x; wR = wR + 1) { if (wR % uniforms.dilations.x != 0) { continue; } let dyR = (${s}(dyRCorner) + ${s}(wR)) / ${s}(uniforms.strides[0]); let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x; if (dyR < 0.0 || dyR >= ${s}(uniforms.Dy_shape[${c}]) || fract(dyR) > 0.0 || wRPerm < 0) { continue; } let idyR: u32 = u32(dyR); for (var wC: u32 = 0; wC < uniforms.effective_filter_dims.y; wC = wC + 1) { if (wC % uniforms.dilations.y != 0) { continue; } let dyC = (${s}(dyCCorner) + ${s}(wC)) / ${s}(uniforms.strides.y); let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y; if (dyC < 0.0 || dyC >= ${s}(uniforms.Dy_shape[${f}]) || fract(dyC) > 0.0 || wCPerm < 0) { continue; } let idyC: u32 = u32(dyC); var inputChannel = groupId * uniforms.input_channels_per_group; for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group; d2 = d2 + 1) { let xValue = ${i?I.get("batch","idyR","idyC","inputChannel"):I.get("batch","inputChannel","idyR","idyC")}; let wValue = ${$.get("inputChannel","wOutChannel","u32(wRPerm)","u32(wCPerm)")}; dotProd = dotProd + xValue * wValue; inputChannel = inputChannel + 1; } } } let value = dotProd + ${a?"bias[d1]":`${s}(0.0)`}; ${v.setByOffset("global_idx","value")}; `;return` ${e.registerUniforms(l).declareVariables(...S,v)} ${w} ${e.mainStart()} ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; ${o?E:A}}`},Ea=(e,t,r)=>{let a=e.length>2,n=t.outputShape,o=M.size(n),s=[Math.ceil(o/64),1,1];De("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${s}`);let l=t.format==="NHWC",i=["rank","rank"],c=[t.strides[0],t.strides[1]],f=[t.kernelShape[l?1:2],t.kernelShape[l?2:3]],m=[t.dilations[0],t.dilations[1]],h=[f[0]+(t.dilations[0]<=1?0:(t.kernelShape[l?1:2]-1)*(t.dilations[0]-1)),f[1]+(t.dilations[1]<=1?0:(t.kernelShape[l?2:3]-1)*(t.dilations[1]-1))],w=[h[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),h[1]-1-Math.floor(t.pads[1]+t.pads[3])/2],b=!1,$=t.group,I=e[1].dims,S=I[0]/$,v=I[1],E=[{type:6,data:o},{type:12,data:c},{type:12,data:f},{type:12,data:m},{type:12,data:h},{type:6,data:w},{type:12,data:S},{type:12,data:v},...K(e[0].dims,e[1].dims)];a&&(E.push(...K(e[2].dims)),i.push("rank")),E.push(...K(n));let A=s[1]===1&&s[2]===1,k=U=>{let N=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:c.length},{name:"filter_dims",type:"u32",length:f.length},{name:"dilations",type:"u32",length:f.length},{name:"effective_filter_dims",type:"u32",length:h.length},{name:"pads",type:"i32",length:w.length},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],H=ke(e[0].dataType);return`${Bc(U,e,n,a,A,b,H,N,l)}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};`,inputDependencies:i},getRunData:()=>({dispatchGroup:{x:s[0],y:s[1],z:s[2]},outputs:[{dims:r?r(n):n,dataType:e[0].dataType}],programUniforms:E}),getShaderSource:k}}});var Dc,Mc,Uc,Xs,Qs,Nc,Wc,Vc,Gc,Js,eu=F(()=>{"use strict";Ys();Zs();Mt();ir();Dc=(e,t,r,a,n,o)=>(e-1)*t+r+(a-1)*n+1-o,Mc=(e,t,r,a,n)=>{let o=Math.floor(e/2);t==="SAME_UPPER"?(r[a]=o,r[n]=e-o):t==="SAME_LOWER"&&(r[a]=e-o,r[n]=o)},Uc=(e,t,r,a,n,o,s,l,i,c)=>{let f=e.length-2,m=c.length===0;if(i.length===0)for(let b=0;b{let r=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((m,h)=>m*h,1)===0){r.length=0;for(let m=2;mm+h,0)===0){let m=t[0].dims.length-2;i=new Array(m).fill(1)}let c=e.strides.slice();if(c.reduce((m,h)=>m+h,0)===0){let m=t[0].dims.length-2;c=new Array(m).fill(1)}Uc(l,r,i,e.autoPad,e.group,n,c,a,s,o);let f=Object.assign({},e);return Object.assign(f,{kernelShape:r,pads:n,outputPadding:s,outputShape:o,dilations:i,strides:c}),f},Qs=e=>{let t=_n(e),r=e.format,a=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],n=e.dilations,o=e.group,s=e.kernelShape,l=e.pads,i=e.strides,c=e.wIsConst(),f=e.outputPadding,m=e.outputShape;return{autoPad:a,format:r,dilations:n,group:o,kernelShape:s,outputPadding:f,outputShape:m,pads:l,strides:i,wIsConst:c,...t,cacheKey:`${e.format};${t.activation};`}},Nc=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],a=e[1].dims[0];if(r!==a)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let n=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==n))throw new Error("invalid bias");let o=e[0].dims.length-2;if(t.dilations.reduce((f,m)=>f+m,0)>0&&t.dilations.length!==o)throw new Error(`dilations should be ${o}D`);if(t.strides.reduce((f,m)=>f+m,0)>0&&t.strides.length!==o)throw new Error(`strides should be ${o}D`);if(t.pads.reduce((f,m)=>f+m,0)>0&&t.pads.length!==o*2)throw new Error(`pads should be ${o*2}D`);if(t.outputPadding.length!==o&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${o}D`);if(t.kernelShape.reduce((f,m)=>f+m,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},Wc=[2,3,1,0],Vc=(e,t,r)=>{let a=Xs(r,t),n=r.format==="NHWC",o=a.outputShape,s=o[n?3:1],l=t[0].dims[n?3:1];if(a.group!==1||s===1&&l===1){e.compute(Ea(t,a));return}let i=o[n?1:2],c=o[n?2:3],f=t[1].dims[2],m=t[1].dims[3],h=n?i*c:s,w=n?s:i*c,b=f*m*l,$=!0,I=e.kernelCustomData.wT??e.compute(dt(t[1],Wc),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=I);let S=[t[0],I],v=t.length===3;v&&(!n&&t[2].dims.length===1?S.push(t[2].reshape([t[2].dims[0],1,1])):S.push(t[2])),e.compute(Ks(S,a,o,h,w,b,v,$),{inputs:S})},Gc=(e,t)=>{let r=t.format==="NHWC",a=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];a.length===3&&a.push(e.inputs[2]);let n=t.kernelShape;(n.length===0||n[0]===0)&&(n=[e.inputs[1].dims[2]]);let o=t.dilations;(o.length===0||o[0]===0)&&(o=[1]);let s=t.strides;(s.length===0||s[0]===0)&&(s=[1]);let l=t.pads;l.length===0&&(l=[0,0]),l=[0,l[0],0,l[1]],s=[1].concat(s),o=[1].concat(o),n=[1].concat(n);let i=Xs({...t,pads:l,strides:s,dilations:o,kernelShape:n},a);e.compute(Ea(a,i,c=>r?[c[0],c[2],c[3]]:[c[0],c[1],c[3]]))},Js=(e,t)=>{Nc(e.inputs,t),e.inputs[0].dims.length===3?Gc(e,t):Vc(e,e.inputs,t)}});var Hc,tu,ru,nu=F(()=>{"use strict";ie();_e();Ye();be();Hc=(e,t,r,a)=>{let n=M.size(t),o=t.length,s=D("input",e,o),l=q("output",e,o),i=r.dataType===6?r.getInt32Array()[0]:Number(r.getBigInt64Array()[0]),c=M.normalizeAxis(i,o),f=m=>{let h=` i32(${s.indicesGet("inputIndices","uniforms.axis")}) `,w=re("uniforms.input_shape","uniforms.axis",o),b=a.reverse?h+(a.exclusive?" + 1":""):"0",$=a.reverse?w:h+(a.exclusive?"":" + 1");return` ${m.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(s,l)} ${m.mainStart()} ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} var inputIndices = ${l.offsetToIndices("global_idx")}; var sum = ${l.type.value}(0); let first : i32 = ${b}; let last : i32 = ${$}; for (var i : i32 = first; i < last; i++) { ${s.indicesSet("inputIndices","uniforms.axis","u32(i)")}; sum = sum + ${s.getByIndices("inputIndices")}; } ${l.setByOffset("global_idx","sum")}; }`};return{name:"CumSum",shaderCache:{hint:a.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:[{type:12,data:n},{type:6,data:c},...K(t,t)]}),getShaderSource:f}},tu=(e,t)=>{let r=e.inputs[0].dims,a=e.inputs[0].dataType,n=e.inputs[1];e.compute(Hc(a,r,n,t),{inputs:[0]})},ru=e=>{let t=e.exclusive===1,r=e.reverse===1;return $e({exclusive:t,reverse:r})}});var Aa,An,au,Lc,Fc,Ta,Oa,iu,qc,ou,su,uu=F(()=>{"use strict";ie();_e();Ye();be();Aa="[a-zA-Z]|\\.\\.\\.",An="("+Aa+")+",au="^"+An+"$",Lc="("+An+",)*"+An,Fc="^"+Lc+"$",Ta=class{constructor(t=-1){this.symbolToIndices=new Map,this.inputIndex=t}addSymbol(t,r){let a=this.symbolToIndices.get(t);a===void 0?a=[r]:a.push(r),this.symbolToIndices.set(t,a)}},Oa=class{constructor(t,r){this.equation=r;this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[a,n]=r.includes("->")?r.split("->",2):[r,""];if(!a.match(RegExp(Fc)))throw new Error("Invalid LHS term");if(a.split(",").forEach((l,i)=>{let c=t[i].dims.slice();if(!l.match(RegExp(au)))throw new Error("Invalid LHS term");let f=this.processTerm(l,!0,c,i);this.lhs.push(f)}),n==="")n+=[...this.symbolToInfo.entries()].filter(([l,i])=>i.count===1||l==="...").map(([l])=>l).join("");else if(!n.match(RegExp(An)))throw new Error("Invalid RHS");n.match(RegExp(Aa,"g"))?.forEach(l=>{if(l==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let i=this.symbolToInfo.get(l);if(i===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(i.dimValue)}}),this.rhs=this.processTerm(n,!1,this.outputDims)}addSymbol(t,r,a){let n=this.symbolToInfo.get(t);if(n!==void 0){if(n.dimValue!==r&&n.count!==1)throw new Error("Dimension mismatch");n.count++,n.inputIndices.push(a)}else n={count:1,dimValue:r,inputIndices:[a]};this.symbolToInfo.set(t,n)}processTerm(t,r,a,n=-1){let o=a.length,s=!1,l=[],i=0;if(!t.match(RegExp(au))&&!r&&t!=="")throw new Error("Invalid LHS term");let c=t.match(RegExp(Aa,"g")),f=new Ta(n);return c?.forEach((m,h)=>{if(m==="..."){if(s)throw new Error("Only one ellipsis is allowed per input term");s=!0;let w=o-c.length+1;if(w<0)throw new Error("Ellipsis out of bounds");if(l=a.slice(i,i+w),this.hasEllipsis){if(this.ellipsisDims.length!==l.length||this.ellipsisDims.toString()!==l.toString())throw new Error("Ellipsis dimensions mismatch")}else if(r)this.hasEllipsis=!0,this.ellipsisDims=l;else throw new Error("Ellipsis must be specified in the LHS");for(let b=0;be+"_max",qc=(e,t,r,a)=>{let o=e.map(f=>f.length).map((f,m)=>D(`input${m}`,t,f)),s=M.size(a),l=q("output",t,a.length),i=[...r.symbolToInfo.keys()].filter(f=>!r.rhs.symbolToIndices.has(f)),c=f=>{let m=[],h="var prod = 1.0;",w="var sum = 0.0;",b="sum += prod;",$=[],I=[],S=[],v=[],E=r.symbolToInfo.size===r.rhs.symbolToIndices.size;r.symbolToInfo.forEach((k,U)=>{if(r.rhs.symbolToIndices.has(U)){let N=r.rhs.symbolToIndices.get(U)?.[0];N!==void 0&&r.lhs.forEach((H,z)=>{if(k.inputIndices.includes(z)){let L=H.symbolToIndices.get(U);if(L===void 0)throw new Error("Invalid symbol error");L.forEach(pe=>{m.push(`${o[z].indicesSet(`input${z}Indices`,pe,l.indicesGet("outputIndices",N))}`)})}})}else r.lhs.forEach((N,H)=>{if(k.inputIndices.includes(H)){let z=N.symbolToIndices.get(U);if(z===void 0)throw new Error("Invalid symbol error");z.forEach(L=>{$.push(`${o[H].indicesSet(`input${H}Indices`,L,`${U}`)}`)}),v.push(`prod *= ${o[H].getByIndices(`input${H}Indices`)};`)}}),I.push(`for(var ${U}: u32 = 0; ${U} < uniforms.${iu(U)}; ${U}++) {`),S.push("}")});let A=E?[...m,`let sum = ${o.map((k,U)=>k.getByIndices(`input${U}Indices`)).join(" * ")};`]:[...m,w,...I,...$,h,...v,b,...S];return` ${f.registerUniforms(i.map(k=>({name:`${iu(k)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...o,l)} ${f.mainStart()} ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} var outputIndices = ${l.offsetToIndices("global_idx")}; ${o.map((k,U)=>`var input${U}Indices: ${o[U].type.indices};`).join(` `)} ${A.join(` `)}; ${l.setByOffset("global_idx","sum")}; }`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let f=i.filter(h=>r.symbolToInfo.has(h)).map(h=>({type:12,data:r.symbolToInfo.get(h)?.dimValue||0}));f.push({type:12,data:s});let m=e.map((h,w)=>[...K(h)]).reduce((h,w)=>h.concat(w),f);return m.push(...K(a)),{outputs:[{dims:a,dataType:t}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:m}},getShaderSource:c}},ou=(e,t)=>{let r=new Oa(e.inputs,t.equation),a=r.outputDims,n=e.inputs.map((o,s)=>o.dims);e.compute(qc(n,e.inputs[0].dataType,r,a))},su=e=>{let t=e.equation.replace(/\s+/g,"");return $e({equation:t})}});var jc,lu,Kc,Yc,du,cu=F(()=>{"use strict";ie();_e();be();jc=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),a=r.length{let r=e.length-t.length,a=[];for(let n=0;ne.length>t.length?lu(e,t):lu(t,e),Yc=e=>{let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),a=Kc(t,r),n=e[0].dataType,o=n===9?4:1,s=Math.ceil(M.size(a)/o),l=c=>{let f=D("input",n,t.length,o),m=q("output",n,a.length,o),h;if(n===9){let w=(b,$,I="")=>` let outputIndices${$} = ${m.offsetToIndices(`outputOffset + ${$}u`)}; let offset${$} = ${f.broadcastedIndicesToOffset(`outputIndices${$}`,m)}; let index${$} = offset${$} / 4u; let component${$} = offset${$} % 4u; ${b}[${$}] = ${I}(${f.getByOffset(`index${$}`)}[component${$}]); `;h=` let outputOffset = global_idx * ${o}; var data = vec4(0); ${w("data",0,"u32")} ${w("data",1,"u32")} ${w("data",2,"u32")} ${w("data",3,"u32")} ${m.setByOffset("global_idx","data")} }`}else h=` let outputIndices = ${m.offsetToIndices("global_idx")}; let inputOffset = ${f.broadcastedIndicesToOffset("outputIndices",m)}; ${m.setByOffset("global_idx",f.getByOffset("inputOffset"))} }`;return` ${c.registerUniform("vec_size","u32").declareVariables(f,m)} ${c.mainStart()} ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} ${h}`},i=[{type:12,data:s},...K(t,a)];return{name:"Expand",shaderCache:{hint:`${a.length}`,inputDependencies:["rank"]},getShaderSource:l,getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:i})}},du=e=>{jc(e.inputs),e.compute(Yc(e.inputs),{inputs:[0]})}});var Zc,pu,fu=F(()=>{"use strict";ie();_e();be();$n();Zc=e=>{let t=e[0].dataType,r=M.size(e[0].dims),a=M.size(e[1].dims),n=a%4===0,o=s=>{let l=D("x",t,[1],4),i=D("bias",t,[1],4),c=q("y",t,[1],4),f=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],m=w=>` let bias${w}_offset: u32 = (global_idx * 4 + ${w}) % uniforms.bias_size; let bias${w} = ${i.getByOffset(`bias${w}_offset / 4`)}[bias${w}_offset % 4];`,h=n?` let bias = ${i.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${m(0)}${m(1)}${m(2)}${m(3)} let bias = ${l.type.value}(bias0, bias1, bias2, bias3);`;return`${s.registerUniforms(f).declareVariables(l,i,c)} ${ya(at(t))} ${s.mainStart(mn)} ${s.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")} let x = ${l.getByOffset("global_idx")}; ${h} let x_in = x + bias; ${c.setByOffset("global_idx",ba("x_in"))} }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${n}`,inputDependencies:["type","type"]},getShaderSource:o,getRunData:s=>({outputs:[{dims:s[0].dims,dataType:s[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:a}],dispatchGroup:{x:Math.ceil(r/mn/4)}})}},pu=e=>{e.inputs.length<2||M.size(e.inputs[1].dims)===0?$s(e):e.compute(Zc(e.inputs))}});var Xc,Qc,mu,hu,gu=F(()=>{"use strict";ie();_e();Ye();be();Xc=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},Qc=(e,t)=>{let r=e[0].dims,a=e[1].dims,n=r.length,o=M.normalizeAxis(t.axis,n),s=r.slice(0);s.splice(o,1,...a);let l=r[o],i=e[0].dataType===9?4:1,c=Math.ceil(M.size(s)/i),f=[{type:12,data:c},{type:6,data:l},{type:12,data:o},...K(e[0].dims,e[1].dims,s)],m=h=>{let w=D("data",e[0].dataType,e[0].dims.length,i),b=D("inputIndices",e[1].dataType,e[1].dims.length),$=q("output",e[0].dataType,s.length,i),I=v=>{let E=a.length,A=`var indicesIndices${v} = ${b.type.indices}(0);`;for(let k=0;k1?`indicesIndices${v}[${k}]`:`indicesIndices${v}`} = ${s.length>1?`outputIndices${v}[uniforms.axis + ${k}]`:`outputIndices${v}`};`;A+=` var idx${v} = ${b.getByIndices(`indicesIndices${v}`)}; if (idx${v} < 0) { idx${v} = idx${v} + uniforms.axisDimLimit; } var dataIndices${v} : ${w.type.indices}; `;for(let k=0,U=0;k1?`dataIndices${v}[${k}]`:`dataIndices${v}`} = u32(idx${v});`,U+=E):(A+=`${n>1?`dataIndices${v}[${k}]`:`dataIndices${v}`} = ${s.length>1?`outputIndices${v}[${U}]`:`outputIndices${v}`};`,U++);return A},S;if(e[0].dataType===9){let v=(E,A,k="")=>` let outputIndices${A} = ${$.offsetToIndices(`outputOffset + ${A}u`)}; ${I(A)}; let offset${A} = ${w.indicesToOffset(`dataIndices${A}`)}; let index${A} = offset${A} / 4u; let component${A} = offset${A} % 4u; ${E}[${A}] = ${k}(${w.getByOffset(`index${A}`)}[component${A}]); `;S=` let outputOffset = global_idx * ${i}; var value = vec4(0); ${v("value",0,"u32")} ${v("value",1,"u32")} ${v("value",2,"u32")} ${v("value",3,"u32")} ${$.setByOffset("global_idx","value")} `}else S=` let outputIndices = ${$.offsetToIndices("global_idx")}; ${I("")}; let value = ${w.getByIndices("dataIndices")}; ${$.setByOffset("global_idx","value")}; `;return` ${h.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(w,b,$)} ${h.mainStart()} ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} ${S} }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:s,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:f}),getShaderSource:m}},mu=e=>$e({axis:e.axis}),hu=(e,t)=>{let r=e.inputs;Xc(r),e.compute(Qc(e.inputs,t))}});var Jc,ep,yu,bu,wu=F(()=>{"use strict";ie();_e();Ye();be();Jc=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and indices input tensors be of same rank.`)},ep=(e,t)=>{let r=e[0].dims,a=e[0].dataType,n=r.length,o=e[1].dims,s=e[1].dataType,l=M.normalizeAxis(t.axis,n),i=r[l],c=o.slice(0),f=M.size(c),m=D("input",a,n),h=D("indicesInput",s,o.length),w=q("output",a,c.length),b=[{type:12,data:f},{type:6,data:i},{type:12,data:l}];return b.push(...K(r,o,c)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:b}),getShaderSource:S=>` ${S.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(m,h,w)} ${S.mainStart()} ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} let outputIndices = ${w.offsetToIndices("global_idx")}; var idx = ${h.getByOffset("global_idx")}; if (idx < 0) { idx = idx + uniforms.axisDimLimit; } var inputIndices = ${m.type.indices}(outputIndices); ${m.indicesSet("inputIndices","uniforms.axis","u32(idx)")}; let value = ${m.getByIndices("inputIndices")}; ${w.setByOffset("global_idx","value")}; }`}},yu=e=>$e({axis:e.axis}),bu=(e,t)=>{let r=e.inputs;Jc(r),e.compute(ep(e.inputs,t))}});var tp,rp,vu,$u,_u=F(()=>{"use strict";ie();_e();be();tp=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},rp=(e,t)=>{let r=e[0].dims.slice(),a=e[1].dims.slice(),[n,o,s]=cn.getShapeOfGemmResult(r,t.transA,a,t.transB,e.length===3?e[2].dims:void 0),l=[n,o];if(!l)throw new Error("Can't use gemm on the given tensors");let i=M.size(l),c=[{type:12,data:i},{type:12,data:n},{type:12,data:o},{type:12,data:s},{type:1,data:t.alpha},{type:1,data:t.beta}],f=["type","type"];e.length===3&&(c.push(...K(e[2].dims)),f.push("rank")),c.push(...K(l));let m=h=>{let w="";t.transA&&t.transB?w="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?w="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?w="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(w="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let b=t.alpha===1?"":"value *= uniforms.alpha;",$=D("a",e[0].dataType,e[0].dims),I=D("b",e[1].dataType,e[1].dims),S=$.type.value,v=null,E=[$,I];e.length===3&&(v=D("c",e[2].dataType,e[2].dims.length),E.push(v));let A=q("output",e[0].dataType,l.length);E.push(A);let k=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return` ${h.registerUniforms(k).declareVariables(...E)} ${h.mainStart()} ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let m = global_idx / uniforms.N; let n = global_idx % uniforms.N; var value = ${S}(0); for (var k: u32 = 0u; k < uniforms.K; k++) { ${w} } ${b} ${(()=>v!=null?`let cOffset = ${v.broadcastedIndicesToOffset("vec2(m, n)",A)}; value += ${S}(uniforms.beta) * ${v.getByOffset("cOffset")};`:"")()} output[global_idx] = value; }`};return{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:f},getRunData:()=>({outputs:[{dims:l,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:c}),getShaderSource:m}},vu=e=>{let t=e.transA,r=e.transB,a=e.alpha,n=e.beta;return{transA:t,transB:r,alpha:a,beta:n,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},$u=(e,t)=>{tp(e.inputs),e.compute(rp(e.inputs,t))}});var np,ap,ip,xu,Su=F(()=>{"use strict";ie();_e();be();np=(e,t)=>{let r=e[0].dims,a=r,n=2,o=M.sizeToDimension(r,n),s=M.sizeFromDimension(r,n),l=Me(s),i=s/l,c=[r[0],r[1],i],f=["rank","type","type"],m=[{type:12,data:s},{type:12,data:i}];m.push(...K(c,c));let h=w=>{let b=D("x",e[0].dataType,c.length,l),$=D("scale",e[1].dataType,e[1].dims),I=D("bias",e[2].dataType,e[2].dims),S=q("output",e[0].dataType,c.length,l),v=[b,$,I,S],E=b.type.value,A=l===1?"f32":`vec${l}`,k=64,U=[{name:"normSize",type:"u32"},{name:"normPackedSize",type:"u32"}];return` var meanShared : f32; var squaredNormShared : f32; var workgroupShared : array<${A}, ${k}>; const workgroupSize = ${k}u; ${w.registerUniforms(U).declareVariables(...v)} ${w.mainStart(k)} let norm = global_idx / workgroupSize; let batch = norm / uniforms.x_shape[1]; let channel = norm % uniforms.x_shape[1]; let localIndex = local_id.x; // initialize workgroup memory var initial = ${A}(0); for (var h = localIndex; h < uniforms.normPackedSize; h += workgroupSize) { initial = initial + ${A}(${b.get("batch","channel","h")}); } workgroupShared[localIndex] = initial; workgroupBarrier(); // Calculate the mean of current channel data. for (var currSize = workgroupSize >> 1; currSize > 0; currSize = currSize >> 1) { if (localIndex < currSize) { workgroupShared[localIndex] = workgroupShared[localIndex] + workgroupShared[localIndex + currSize]; } workgroupBarrier(); } if (localIndex == 0) { meanShared = ${it("workgroupShared[0]",l)} / f32(uniforms.normSize); } workgroupBarrier(); // reinitialize workgroup memory. initial = ${A}(0); for (var h = localIndex; h < uniforms.normPackedSize; h += workgroupSize) { let deviation = ${A}(${b.get("batch","channel","h")}) - ${A}(meanShared); initial = initial + deviation * deviation; } workgroupShared[localIndex] = initial; workgroupBarrier(); // Calculate the sum of square of deviation of current channel data. for (var currSize = workgroupSize >> 1; currSize > 0; currSize = currSize >> 1) { if (localIndex < currSize) { workgroupShared[localIndex] = workgroupShared[localIndex] + workgroupShared[localIndex + currSize]; } workgroupBarrier(); } if (localIndex == 0) { squaredNormShared = ${it("workgroupShared[0]",l)}; } workgroupBarrier(); let invStdDev = inverseSqrt(squaredNormShared / f32(uniforms.normSize) + f32(${t.epsilon})); let channelScale = invStdDev * f32(${$.getByOffset("channel")}); let channelShift = f32(${I.getByOffset("channel")}) - meanShared * channelScale; for (var h = localIndex; h < uniforms.normPackedSize; h += workgroupSize) { let value = ${b.get("batch","channel","h")} * ${E}(${A}(channelScale)) + ${E}(${A}(channelShift)); ${S.set("batch","channel","h","value")}; } }`};return{name:"InstanceNormalization",shaderCache:{hint:`${t.epsilon};${l}`,inputDependencies:f},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:o},programUniforms:m}),getShaderSource:h}},ap=(e,t,r,a,n,o,s,l)=>{let i=Me(s),c=64,f=i===1?"vec2f":`mat2x${i}f`,m=i===1?"f32":`vec${i}f`,h=(U,N)=>`${f}(${U}, ${N})`,w=n*s/i,b=Math.ceil(o/c),$=["type"],I=[{type:12,data:b},{type:12,data:o},{type:12,data:Math.floor(s/i)},{type:12,data:Math.floor(o*s/i)}],S=U=>{let N=D("input",t.dataType,t.dims,i);return` ${U.declareVariables(N)} @group(0) @binding(1) var output : array<${f}>; struct Uniforms {wg_size:u32, H:u32, C:u32, image_size:u32}; @group(0) @binding(2) var uniforms: Uniforms; ${U.mainStart(c)} let currentImageNumber = global_idx / ${c} / uniforms.C; let currentChannelNumber = (global_idx / ${c}) % uniforms.C; let wgId = global_idx % ${c}; let wgOffset = wgId * uniforms.wg_size; if (wgOffset >= uniforms.H) { return; } let wgMax = min(wgOffset + uniforms.wg_size, uniforms.H); let offset = currentImageNumber * uniforms.image_size + currentChannelNumber; var sum = ${Xe("f32",i)}; var squaredSum = ${Xe("f32",i)}; for (var i: u32 = wgOffset; i < wgMax; i++) { let value = ${m}(input[offset + i * uniforms.C]); sum += value; squaredSum += value * value; } output[global_idx] = ${h("sum","squaredSum")}; }`},v=e.compute({name:"InstanceNormComputeMean",shaderCache:{hint:`${i}`,inputDependencies:$},getRunData:()=>({outputs:[{dims:[n,s,c,2],dataType:1}],dispatchGroup:{x:n*s/i},programUniforms:I}),getShaderSource:S},{inputs:[t],outputs:[-1]})[0],E=[{type:12,data:w},{type:12,data:o},{type:12,data:Math.floor(s/i)},{type:12,data:Math.floor(c*s/i)}],A=["type","type","type"],k=U=>{let N=D("scale",r.dataType,r.dims,i),H=D("bias",a.dataType,a.dims,i);return` @group(0) @binding(0) var input : array<${f}>; @group(0) @binding(1) var scale : array<${N.type.storage}>; @group(0) @binding(2) var bias : array<${H.type.storage}>; @group(0) @binding(3) var output : array<${f}>; struct Uniforms {units_of_work : u32, H: u32, C : u32, image_size : u32}; @group(0) @binding(4) var uniforms: Uniforms; ${U.mainStart()} ${U.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.units_of_work")} let currentImageNumber = global_idx / uniforms.C; let currentChannelNumber = global_idx % uniforms.C; let offset = currentImageNumber * uniforms.image_size; var sum = ${Xe("f32",i)}; var squaredSum = ${Xe("f32",i)}; for (var i: u32 = 0; i < min(${c}, uniforms.H); i++) { let value = input[offset + i + currentChannelNumber * ${c}]; sum += value[0]; squaredSum += value[1]; } sum = sum / f32(uniforms.H); squaredSum = squaredSum / f32(uniforms.H); let invStdDev = inverseSqrt(squaredSum - sum * sum + f32(${l})); let channelScale = invStdDev * ${m}(scale[currentChannelNumber]); let channelShift = ${m}(bias[currentChannelNumber]) - sum * channelScale; output[global_idx] = ${h("channelScale","channelShift")}; }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${i};${l}`,inputDependencies:A},getRunData:()=>({outputs:[{dims:[n,s,2],dataType:1}],dispatchGroup:{x:Math.ceil(w/64)},programUniforms:E}),getShaderSource:k},{inputs:[v,r,a],outputs:[-1]})[0]},ip=(e,t,r)=>{let a=t[0].dims,n=a,o=a[0],s=a[a.length-1],l=M.sizeFromDimension(a,1)/s,i=Me(s),c=M.size(n)/i,f=[{type:12,data:l},{type:12,data:Math.floor(s/i)}],m=["type","type"],h=ap(e,t[0],t[1],t[2],o,l,s,r.epsilon),w=b=>{let $=ke(t[0].dataType),I=i===1?"vec2f":`mat2x${i}f`,S=i===1?$:`vec${i}<${$}>`,v=D("input",t[0].dataType,t[0].dims,i),E=q("output",t[0].dataType,n,i);return` @group(0) @binding(0) var input : array<${v.type.storage}>; @group(0) @binding(1) var scaleInput : array<${I}>; @group(0) @binding(2) var output : array<${E.type.storage}>; struct Uniforms {H: u32, C : u32}; @group(0) @binding(3) var uniforms: Uniforms; ${b.mainStart()} let currentImageNumber = global_idx / (uniforms.C * uniforms.H); let currentChannelNumber = global_idx % uniforms.C; let scaleOffset = currentImageNumber * uniforms.C + currentChannelNumber; let scale = scaleInput[scaleOffset]; output[global_idx] = fma(input[global_idx], ${S}(scale[0]), ${S}(scale[1])); }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${i}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:n,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:f}),getShaderSource:w},{inputs:[t[0],h]})},xu=(e,t)=>{t.format==="NHWC"?ip(e,e.inputs,t):e.compute(np(e.inputs,t))}});var op,sp,Cu,Iu=F(()=>{"use strict";ie();_e();be();op=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},sp=(e,t,r)=>{let a=e[0].dims,n=e[1],o=e[2],s=a,l=M.normalizeAxis(t.axis,a.length),i=M.sizeToDimension(a,l),c=M.sizeFromDimension(a,l),f=M.size(n.dims),m=o?M.size(o.dims):0;if(f!==c||o&&m!==c)throw new Error(`Size of X.shape()[axis:] == ${c}. Size of scale and bias (if provided) must match this. Got scale size of ${f} and bias size of ${m}`);let h=[];for(let A=0;A1,S=r>2,v=A=>{let k=ke(e[0].dataType),U=[D("x",e[0].dataType,e[0].dims,w),D("scale",n.dataType,n.dims,w)];o&&U.push(D("bias",o.dataType,o.dims,w)),U.push(q("output",e[0].dataType,s,w)),I&&U.push(q("mean_data_output",1,h)),S&&U.push(q("inv_std_output",1,h));let N=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return` ${A.registerUniforms(N).declareVariables(...U)} ${A.mainStart()} ${A.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} let offset = global_idx * uniforms.norm_size_vectorized; var mean_vector = ${Xe("f32",w)}; var mean_square_vector = ${Xe("f32",w)}; for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) { let value = ${mt(k,w,"x[h + offset]")}; mean_vector += value; mean_square_vector += value * value; } let mean = ${it("mean_vector",w)} / uniforms.norm_size; let inv_std_dev = inverseSqrt(${it("mean_square_vector",w)} / uniforms.norm_size - mean * mean + uniforms.epsilon); for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) { let f32input = ${mt(k,w,"x[j + offset]")}; let f32scale = ${mt(k,w,"scale[j]")}; output[j + offset] = ${U[0].type.value}((f32input - mean) * inv_std_dev * f32scale ${o?`+ ${mt(k,w,"bias[j]")}`:""} ); } ${I?"mean_data_output[global_idx] = mean":""}; ${S?"inv_std_output[global_idx] = inv_std_dev":""}; }`},E=[{dims:s,dataType:e[0].dataType}];return I&&E.push({dims:h,dataType:1}),S&&E.push({dims:h,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${w};${r}`,inputDependencies:b},getRunData:()=>({outputs:E,dispatchGroup:{x:Math.ceil(i/64)},programUniforms:$}),getShaderSource:v}},Cu=(e,t)=>{op(e.inputs),e.compute(sp(e.inputs,t,e.outputCount))}});var up,lp,Eu,Au,Tu=F(()=>{"use strict";ie();_e();Ye();be();up=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let r=e[0],a=r.dims.length;if(r.dims[a-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let n=Math.floor((t.k+t.blockSize-1)/t.blockSize),o=t.blockSize/8*t.bits,s=e[1];if(!M.areEqual(s.dims,[t.n,n,o]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let i=e[2].dims;if(M.size(i)!==t.n*n)throw new Error("scales input size error.");if(e.length===4){let f=e[3].dims,m=t.bits>4?t.n*n:t.n*Math.floor((n+1)/2);if(M.size(f)!==m)throw new Error("zeroPoints input size error.")}},lp=(e,t)=>{let r=e[0].dims,a=r.length,n=r.slice(0,a-1).concat(t.n),o=r[a-2],l=t.blockSize/8*t.bits/4,i=Me(o),c=Me(t.n),f=Me(t.k),m=Me(l),h=M.size(n)/c/i,w=[{type:12,data:h},{type:12,data:t.k},{type:12,data:t.n},{type:12,data:t.accuracyLevel},{type:12,data:t.bits},{type:12,data:t.blockSize}],b=r.slice();b.splice(-1,1,t.k/f);let $=M.convertShape(e[1].dims).slice();$.splice(-1,1,l/m),w.push(...K(b)),w.push(...K($)),w.push(...K(e[2].dims)),e.length===4&&w.push(...K(M.convertShape(e[3].dims)));let I=n.slice();I.splice(-1,1,t.n/c),w.push(...K(I));let S=v=>{let E=D("a",e[0].dataType,b.length,f),A=D("b",12,$.length,m),k=D("scales",e[2].dataType,e[2].dims.length),U=[E,A,k],N=e.length===4?D("zero_points",12,e[3].dims.length):void 0;N&&U.push(N);let H=q("output",e[0].dataType,n.length,c),z=[{name:"output_size",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"accuracy_level",type:"u32"},{name:"bits",type:"u32"},{name:"block_size",type:"u32"}],L=Math.floor((t.k+t.blockSize-1)/t.blockSize),pe=ke(e[0].dataType),Ie=(()=>{switch(f){case 1:return`array<${pe}, 8>`;case 2:return`mat4x2<${pe}>`;case 4:return`mat2x4<${pe}>`;default:throw new Error(`${f}-component is not supported.`)}})(),we=` fn dequantize(quantized: ${Ie}, zero_point: ${pe}, scale: ${pe}) -> ${Ie} { ${(()=>f===1?`var dequantized = ${Ie}(${Array.from({length:8},(X,xe)=>`(quantized[${xe}] - zero_point) * scale`).join(", ")}); return dequantized;`:`var zero_points: ${Ie} = ${Ie}(${Array(8).fill("zero_point").join(",")}); return (quantized - zero_points) * scale;`)()} }`,ne=` fn ortUnpack8x4snorm(value: u32) -> ${Ie} { var quantized: ${Ie}; var offset: u32 = 0; let count: u32 = 4; for (var i: u32 = 0; i < 8u; i++) { var result = ${pe}(extractBits(value, offset, count)); ${(()=>{switch(f){case 1:return"quantized[i] = result;";case 2:return"quantized[i / 2][i % 2] = result;";case 4:return"quantized[i / 4][i % 4] = result;";default:throw new Error(`${f}-component is not supported.`)}})()} offset += count; } return quantized; }`,Ue=N?` zero_point_offset += 4; if (zero_point_offset == 32) { zero_point_offset = 0; zero_point_index++; zero_point_word = ${N.getByOffset("zero_point_index")}; }`:"";return` ${we}; ${ne}; ${v.registerUniforms(z).declareVariables(...U,H)} ${v.mainStart()} ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} var output_values: array<${H.type.value}, ${i}>; var output_indices = ${H.offsetToIndices("global_idx")}; var n = ${H.indicesGet("output_indices",a-1)}; var m = ${H.indicesGet("output_indices",a-2)}; var a_indices: ${E.type.indices} = output_indices; // Two zero points are packed into one byte because uniforms.bits <= 4. // zero_point_offset is either 0 or 4. It is bit offset within one byte. // TODO support zero_point_offset for bits > 4 ${N?` var zero_point_index: u32 = n * ${c} * ((${L} + 1) / 2) / 4; var zero_point_word: u32 = ${N.getByOffset("zero_point_index")}; var zero_point_offset: u32 = 0;`:""} var scale_index = n * ${L*c}; var b_indices: ${A.type.indices}; for (var c: u32 = 0; c < ${c}; c++) { ${A.indicesSet("b_indices","0",`n * ${c} + c`)}; var block_offset: u32 = 0; for (var block: u32 = 0; block < ${L}; block++) { // The scale and zero points are computed per block. let scale = ${k.getByOffset("scale_index")}; // The default zero point is 8 for unsigned 4-bit quantization. let zero_point = ${pe}(${N?"extractBits(zero_point_word, zero_point_offset, 4)":8}); ${A.indicesSet("b_indices","1","block")}; var word_offset: u32 = block_offset; for (var word: u32 = 0; word < ${l}; word += ${m}) { ${A.indicesSet("b_indices","2","word")}; let b_data = ${A.getByIndices("b_indices")}; for (var i: u32 = 0; i < ${m}; i++) { let b_value = ${m===1?"b_data":"b_data[word + i]"}; let b_quantized_values: ${Ie} = ortUnpack8x4snorm(b_value); let b_dequantized_values = dequantize(b_quantized_values, zero_point, scale); // Number of B elements per 32-bit word is 32/bits = 32/4 = 8 var offset: u32 = word_offset; for (var j: u32 = 0; j < 8/${f}; j++) { ${E.indicesSet("a_indices",a-1,`offset/${f}`)}; for (var k: u32 = 0; k < ${i}u; k++) { ${E.indicesSet("a_indices",a-2,`m * ${i} + k`)}; let a_data = ${E.getByIndices("a_indices")}; output_values[k]${c>1?"[c]":""} += ${f===1?"a_data * b_dequantized_values[j]":"dot(a_data, b_dequantized_values[j])"}; } offset += ${f}; } word_offset += 8; } } scale_index++; ${Ue} block_offset += uniforms.block_size; } // Drop the trailing 4 bits if the zero_poit_offset is not a byte boundary to align with the next byte. ${N?`if (zero_point_offset % 8 > 0) { ${Ue} }`:""} } for (var k: u32 = 0u; k < ${i}u; k++) { ${H.indicesSet("output_indices",a-2,`${i+" * m + k"}`)}; ${H.setByIndices("output_indices","output_values[k]")} } }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.cacheKey};${e.length}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:w}),getShaderSource:S}},Eu=(e,t)=>{up(e.inputs,t),e.compute(lp(e.inputs,t))},Au=e=>$e(e)});var dp,ku,Ou,cp,ka,Pu,Ru=F(()=>{"use strict";ie();_e();Ye();ln();ga();be();ir();dp=(e,t)=>{let r=e[0],a=e[1],n=e[2],o=e[3],s=e[4],l=e[5],i=e[6],c=e[7];if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let f=!1,m=r.dims[0],h=r.dims[1],w=r.dims.length===3?f?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],b=h,$=0,I=0,S=Math.floor(w/t.numHeads);if(i&&c){if(i.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(c.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');$=i.dims[2],I=i.dims[2]}else if(i||c)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let v;if(a){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(a.dims.length<3||a.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==a.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(a.dims.length===3){if(a.dims[2]!==r.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');v=2,b=a.dims[1]}else if(a.dims.length===5){if(a.dims[2]!==t.numHeads||a.dims[3]!==2||a.dims[4]!==S)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(n)throw new Error('Expect "value" be none when "key" has packed kv format.');v=5,b=a.dims[1]}else{if(a.dims[1]!==t.numHeads||a.dims[3]!==S)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');v=0,b=a.dims[2]}}else{if(r.dims.length!==3&&r.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(r.dims.length===5&&(r.dims[2]!==t.numHeads||r.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');v=3}if(o){if(o.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(n&&r.dims.length===5&&r.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let E=0;if(s){E=8;let H=s.dims;throw H.length===1?H[0]===m?E=1:H[0]===3*m+2&&(E=3):H.length===2&&H[0]===m&&H[1]===b&&(E=5),E===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, kv_sequence_length)'):new Error("Mask not supported")}let A=!1,k=w;if(n){if(n.dims.length!==3&&n.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(n.dims.length===3){if(b!==n.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');k=n.dims[2]}else{if(b!==n.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');k=n.dims[1]*n.dims[3],A=!0}}let U=$+b,N=!1;if(s)throw new Error("Key padding mask is not supported");if(l)throw new Error("extraAddQk is not supported");if(i)throw new Error("pastKey is not supported");if(c)throw new Error("pastValue is not supported");return{batchSize:m,sequenceLength:h,pastSequenceLength:$,kvSequenceLength:b,totalSequenceLength:U,maxSequenceLength:I,inputHiddenSize:0,hiddenSize:w,vHiddenSize:k,headSize:S,vHeadSize:Math.floor(k/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:E,scale:t.scale,broadcastResPosBias:N,passPastInKv:A,qkvFormat:v}},ku=e=>$e({...e}),Ou=$e({perm:[0,2,1,3]}),cp=(e,t,r,a,n,o,s)=>{let l=[a,n,o],i=M.size(l),c=[{type:12,data:i},{type:12,data:s},{type:12,data:o}],f=m=>{let h=q("qkv_with_bias",t.dataType,l),w=D("qkv",t.dataType,l),b=D("bias",r.dataType,l),$=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return` ${m.registerUniforms($).declareVariables(w,b,h)} ${m.mainStart()} ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset; qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx]; }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:c}),getShaderSource:f},{inputs:[t,r],outputs:[-1]})[0]},ka=(e,t,r,a,n,o,s,l)=>{let i=o;if(s){if(a===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return i=cp(e,o,s,t,a,r*n,l),i=i.reshape([t,a,r,n]),e.compute(dt(i,Ou.perm),{inputs:[i],outputs:[-1]})[0]}else return o.dims.length===3&&(i=o.reshape([t,a,r,n])),e.compute(dt(i,Ou.perm),{inputs:[i],outputs:[-1]})[0]},Pu=(e,t)=>{let r=dp(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let a=e.inputs[1]&&e.inputs[2]&&e.inputs[1].dims.length===4&&e.inputs[2].dims.length===4,n=ka(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,e.inputs[0],e.inputs[3],0);if(a)return bn(e,n,e.inputs[1],e.inputs[2],e.inputs[4],void 0,void 0,void 0,e.inputs[5],r,t);let o=ka(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.headSize,e.inputs[1],e.inputs[3],r.hiddenSize),s=ka(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.vHeadSize,e.inputs[2],e.inputs[3],2*r.hiddenSize);bn(e,n,o,s,e.inputs[4],void 0,e.inputs[6],e.inputs[7],e.inputs[5],r,t)}});var pp,fp,mp,hp,gp,yp,bp,wp,zu,Bu=F(()=>{"use strict";ie();_e();be();pp=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},fp=(e,t,r)=>{let a="";for(let n=t-1;n>=0;--n)a+=` k = i32(${e.indicesGet("indices",n)}) - ${re("uniforms.pads",n,r)}; if (k < 0) { break; } if (k >= i32(${re("uniforms.x_shape",n,t)})) { break; } offset += k * i32(${re("uniforms.x_strides",n,t)}); `;return` value = ${e.type.value}(uniforms.constant_value); for (var i = 0; i < 1; i++) { var offset = 0; var k = 0; ${a} value = x[offset]; } `},mp=(e,t,r)=>{let a="";for(let n=t-1;n>=0;--n)a+=` k = i32(${e.indicesGet("indices",n)}) - ${re("uniforms.pads",n,r)}; if (k < 0) { k = -k; } { let _2n_1 = 2 * (i32(${re("uniforms.x_shape",n,t)}) - 1); k = k % _2n_1; if(k >= i32(${re("uniforms.x_shape",n,t)})) { k = _2n_1 - k; } } offset += k * i32(${re("uniforms.x_strides",n,t)}); `;return` var offset = 0; var k = 0; ${a} value = x[offset]; `},hp=(e,t,r)=>{let a="";for(let n=t-1;n>=0;--n)a+=` k = i32(${e.indicesGet("indices",n)}) - ${re("uniforms.pads",n,r)}; if (k < 0) { k = 0; } if (k >= i32(${re("uniforms.x_shape",n,t)})) { k = i32(${re("uniforms.x_shape",n,t)}) - 1; } offset += k * i32(${re("uniforms.x_strides",n,t)}); `;return` var offset = 0; var k = 0; ${a} value = x[offset]; `},gp=(e,t,r)=>{let a="";for(let n=t-1;n>=0;--n)a+=` k = i32(${e.indicesGet("indices",n)}) - ${re("uniforms.pads",n,r)}; if (k < 0) { k += i32(${re("uniforms.x_shape",n,t)}]); } if (k >= i32(${re("uniforms.x_shape",n,t)})) { k -= i32(${re("uniforms.x_shape",n,t)}); } offset += k * i32(${re("uniforms.x_strides",n,t)}); `;return` var offset = 0; var k = 0; ${a} value = x[offset]; `},yp=(e,t,r)=>{switch(r.mode){case 0:return fp(e,t,r.pads.length);case 1:return mp(e,t,r.pads.length);case 2:return hp(e,t,r.pads.length);case 3:return gp(e,t,r.pads.length);default:throw new Error("Invalid mode")}},bp=(e,t)=>{let r=M.padShape(e[0].dims.slice(),t.pads),a=e[0].dims,n=M.size(r),o=[{type:12,data:n},{type:12,data:t.pads}];t.mode===0&&o.push({type:e[0].dataType,data:t.value}),o.push(...K(e[0].dims,r));let s=["rank"],l=i=>{let c=q("output",e[0].dataType,r.length),f=D("x",e[0].dataType,a.length),m=f.type.value,h=yp(c,a.length,t),w=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&w.push({name:"constant_value",type:m}),` ${i.registerUniforms(w).declareVariables(f,c)} ${i.mainStart()} ${i.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let indices = ${c.offsetToIndices("global_idx")}; var value = ${m}(0); ${h} output[global_idx] = value; }`};return{name:"Pad",shaderCache:{hint:`${t.mode}`,inputDependencies:s},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(M.size(r)/64)},programUniforms:o}),getShaderSource:l}},wp=(e,t)=>{if(e.length>1){let r=e[1].getBigInt64Array(),a=e.length>=3&&e[2].data?e[2].getFloat32Array()[0]:0,n=e[0].dims.length,o=new Int32Array(2*n).fill(0);if(e.length>=4){let l=e[3].getBigInt64Array();for(let i=0;io[Number(i)]=Number(l));let s=[];return o.forEach(l=>s.push(l)),{mode:t.mode,value:a,pads:s}}else return t},zu=(e,t)=>{pp(e.inputs);let r=wp(e.inputs,t);e.compute(bp(e.inputs,r),{inputs:[0]})}});var Tn,Du,Mu,Uu,Nu,vp,$p,Wu,Vu,Gu,Hu,Lu,Fu,qu,ju,Ku,Yu,Zu,Xu,Qu=F(()=>{"use strict";lt();ie();_e();be();Tn=e=>{if(Ae.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},Du=(e,t,r)=>{let a=t.format==="NHWC",n=e.dims.slice();a&&n.splice(1,0,n.pop());let o=Object.hasOwnProperty.call(t,"dilations"),s=t.kernelShape.slice(),l=t.strides.slice(),i=o?t.dilations.slice():[],c=t.pads.slice();qt.adjustPoolAttributes(r,n,s,l,i,c);let f=qt.computePoolOutputShape(r,n,l,i,s,c,t.autoPad),m=Object.assign({},t);o?Object.assign(m,{kernelShape:s,strides:l,pads:c,dilations:i,cacheKey:t.cacheKey}):Object.assign(m,{kernelShape:s,strides:l,pads:c,cacheKey:t.cacheKey});let h=f.slice();return h.push(h.splice(1,1)[0]),[m,a?h:f]},Mu=(e,t)=>{let r=t.format==="NHWC",a=M.size(e),n=M.size(t.kernelShape),o=[{type:12,data:a},{type:12,data:n}],s=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let l=t.kernelShape[t.kernelShape.length-1],i=t.strides[t.strides.length-1],c=t.pads[t.pads.length/2-1],f=t.pads[t.pads.length-1],m=!!(c+f);o.push({type:12,data:l},{type:12,data:i},{type:12,data:c},{type:12,data:f}),s.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let h=!1;if(t.kernelShape.length===2){let w=t.kernelShape[t.kernelShape.length-2],b=t.strides[t.strides.length-2],$=t.pads[t.pads.length/2-2],I=t.pads[t.pads.length-2];h=!!($+I),o.push({type:12,data:w},{type:12,data:b},{type:12,data:$},{type:12,data:I}),s.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[o,s,!0,m,h]}else{if(r)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let l=M.computeStrides(t.kernelShape);o.push({type:12,data:l},{type:12,data:t.pads},{type:12,data:t.strides}),s.push({name:"kernelStrides",type:"u32",length:l.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let i=t.pads.reduce((c,f)=>c+f);return[o,s,!!i,!1,!1]}},Uu=(e,t,r,a,n,o,s,l,i,c,f,m)=>{let h=n.format==="NHWC",w=t.type.value,b=q("output",t.type.tensor,a);if(n.kernelShape.length<=2){let $="",I="",S="",v=r-(h?2:1);if(f?$=` for (var i: u32 = 0u; i < uniforms.kw; i++) { xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; if (xIndices[${v}] < 0 || xIndices[${v}] >= uniforms.x_shape[${v}]) { pad++; continue; } let x_val = x[${t.indicesToOffset("xIndices")}]; ${o} }`:$=` for (var i: u32 = 0u; i < uniforms.kw; i++) { xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; let x_val = x[${t.indicesToOffset("xIndices")}]; ${o} }`,n.kernelShape.length===2){let A=r-(h?3:2);m?I=` for (var j: u32 = 0u; j < uniforms.kh; j++) { xIndices[${A}] = indices[${A}] * uniforms.sh - uniforms.phStart + j; if (xIndices[${A}] < 0 || xIndices[${A}] >= uniforms.x_shape[${A}]) { pad += i32(uniforms.kw); continue; } `:I=` for (var j: u32 = 0u; j < uniforms.kh; j++) { xIndices[${A}] = indices[${A}] * uniforms.sh - uniforms.phStart + j; `,S=` } `}return` ${e.registerUniforms(i).declareVariables(t,b)} ${e.mainStart()} ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} let indices = ${b.offsetToIndices("global_idx")}; var xIndices = ${b.offsetToIndices("global_idx")}; var value = ${w}(${l}); var pad = 0; ${I} ${$} ${S} ${s} output[global_idx] = value; }`}else{if(h)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let $=n.kernelShape.length,I=n.pads.length,S="";return c?S=` if (xIndices[j] >= uniforms.x_shape[j]) { pad++; isPad = true; break; } } if (!isPad) { let x_val = x[${t.indicesToOffset("xIndices")}]; ${o} }`:S=` } let x_val = x[${t.indicesToOffset("xIndices")}]; ${o} `,` ${e.registerUniforms(i).declareVariables(t,b)} ${e.mainStart()} ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} let indices = ${b.offsetToIndices("global_idx")}; var xIndices = ${b.offsetToIndices("global_idx")}; var offsets: array; var value = ${w}(${l}); var pad = 0; var isPad = false; for (var i: u32 = 0u; i < uniforms.kernelSize; i++) { var offset = i; for (var j = 0u; j < ${$-1}u; j++) { offsets[j] = offset / ${re("uniforms.kernelStrides","j",$)}; offset -= offsets[j] * ${re("uniforms.kernelStrides","j",$)}; } offsets[${$-1}] = offset; isPad = false; for (var j = ${r-$}u; j < ${r}u; j++) { xIndices[j] = indices[j] * ${re("uniforms.strides",`j - ${r-$}u`,$)} + offsets[j - ${r-$}u] - ${re("uniforms.pads","j - 2u",I)}; ${S} } ${s} output[global_idx] = value; }`}},Nu=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,vp=e=>`${Nu(e)};${e.countIncludePad}`,$p=e=>`${Nu(e)};${e.storageOrder};${e.dilations}`,Wu=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),Vu=(e,t,r,a)=>{let[n,o]=Du(t,a,r),s=D("x",t.dataType,t.dims.length),l=s.type.value,i="value += x_val;",c="";n.countIncludePad?c+=`value /= ${l}(uniforms.kernelSize);`:c+=`value /= ${l}(i32(uniforms.kernelSize) - pad);`;let[f,m,h,w,b]=Mu(o,n);f.push(...K(t.dims,o));let $=["rank"];return{name:e,shaderCache:{hint:`${a.cacheKey};${h};${w};${b}`,inputDependencies:$},getRunData:()=>({outputs:[{dims:o,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(M.size(o)/64)},programUniforms:f}),getShaderSource:I=>Uu(I,s,t.dims.length,o.length,n,i,c,0,m,h,w,b)}},Gu=e=>{let t=e.count_include_pad!==0,r=Wu(e);if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let a={countIncludePad:t,...r,cacheKey:""};return{...a,cacheKey:vp(a)}},Hu=(e,t)=>{Tn(e.inputs),e.compute(Vu("AveragePool",e.inputs[0],!1,t))},Lu={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},Fu=e=>{let t=e.format;return{format:t,...Lu,cacheKey:t}},qu=(e,t)=>{Tn(e.inputs),e.compute(Vu("GlobalAveragePool",e.inputs[0],!0,t))},ju=(e,t,r,a)=>{let[n,o]=Du(t,a,r),s=` value = max(x_val, value); `,l="",i=D("x",t.dataType,t.dims.length),c=["rank"],[f,m,h,w,b]=Mu(o,n);return f.push(...K(t.dims,o)),{name:e,shaderCache:{hint:`${a.cacheKey};${h};${w};${b}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:o,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(M.size(o)/64)},programUniforms:f}),getShaderSource:$=>Uu($,i,t.dims.length,o.length,n,s,l,t.dataType===10?-65504:-1e5,m,h,w,b)}},Ku=(e,t)=>{Tn(e.inputs),e.compute(ju("MaxPool",e.inputs[0],!1,t))},Yu=e=>{let t=e.storage_order,r=e.dilations,a=Wu(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(a.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let n={storageOrder:t,dilations:r,...a,cacheKey:""};return{...n,cacheKey:$p(n)}},Zu=e=>{let t=e.format;return{format:t,...Lu,cacheKey:t}},Xu=(e,t)=>{Tn(e.inputs),e.compute(ju("GlobalMaxPool",e.inputs[0],!0,t))}});var xp,Sp,Ju,el=F(()=>{"use strict";lt();ie();be();xp=(e,t,r)=>{let a=e===t,n=et&&r>0;if(a||n||o)throw new Error("Range these inputs' contents are invalid.")},Sp=(e,t,r,a)=>{let n=Math.abs(Math.ceil((t-e)/r)),o=[n],s=n,l=[{type:12,data:s},{type:a,data:e},{type:a,data:r},...K(o)],i=c=>{let f=q("output",a,o.length),m=f.type.value,h=[{name:"outputSize",type:"u32"},{name:"start",type:m},{name:"delta",type:m}];return` ${c.registerUniforms(h).declareVariables(f)} ${c.mainStart()} ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} output[global_idx] = uniforms.start + ${m}(global_idx) * uniforms.delta; }`};return{name:"Range",shaderCache:{hint:`${a}`},getShaderSource:i,getRunData:()=>({outputs:[{dims:o,dataType:a}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:l})}},Ju=e=>{let t=0,r=0,a=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],a=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],a=e.inputs[2].getFloat32Array()[0]),Ae.webgpu.validateInputContent&&xp(t,r,a),e.compute(Sp(t,r,a,e.inputs[0].dataType),{inputs:[]})}});var Cp,Ip,Ep,Ap,Tp,Op,kp,Pp,Rp,zp,Bp,tl,Dp,Mp,Up,Np,Wp,rl,nl,al=F(()=>{"use strict";ie();_e();Ye();be();Cp=(e,t)=>{if(e.every(r=>r>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Ip=(e,t,r)=>{t.every(n=>n>=0&&n{throw new Error("Resize requires axes input values to be positive and less than rank")}));let a=new Array(r).fill(1);return t.forEach((n,o)=>a[n]=e[o]),a},Ep=(e,t,r,a,n,o)=>{let[s,l,i]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],c=e[0].dims.length;if(s>0&&e.length>s&&e[s].dims.length>0)e[s].getFloat32Array().forEach(f=>o.push(f));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(l>0&&e.length>l&&e[l].dims.length>0){if(e[l].getFloat32Array().forEach(f=>a.push(f)),a.length!==0&&a.length!==c&&r>=18&&a.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");Cp(a,t),t.axes.length>0&&Ip(a,t.axes,c).forEach((f,m)=>a[m]=f)}if(i>0&&e.length>i&&(e[i].getBigInt64Array().forEach(f=>n.push(Number(f))),n.length!==c||r>=18&&n.length===t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(a.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(n.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof a<"u"&&typeof n<"u"&&a.length>0&&n.length>c)throw new Error("Resize requires only of scales or sizes to be specified")},Ap=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32, lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return`return ${t}(xResized) / ${t}(xScale);`;case"pytorch_half_pixel":return`if (lengthResized > 1) { return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5; } else { return 0.0; }`;case"tf_half_pixel_for_nn":return`return (${t}(xResized) + 0.5) / ${t}(xScale);`;case"align_corners":return`if (lengthResized == 1) { return 0.0; } else { // The whole part and the fractional part are calculated separately due to inaccuracy of floating // point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an // offset-by-one error later in floor(). let whole = ${t}(xResized * (lengthOriginal - 1) / (lengthResized - 1)); let fract = ${t}(xResized * (lengthOriginal - 1) % (lengthResized - 1)) / ${t}(lengthResized - 1); return whole + fract; }`;case"tf_crop_and_resize":return`if (lengthResized > 1) { return ${t}(roiStart) * ${t}(lengthOriginal - 1) + (${t}(xResized) * ${t}(roiEnd - roiStart) * ${t}(lengthOriginal - 1)) / ${t}(lengthResized - 1); } else { return 0.5 * ${t}(roiStart + roiEnd) * ${t}(lengthOriginal - 1); }`;case"half_pixel_symmetric":return`const outputWidth = ${t}xScale * ${t}(lengthResized); const adjustment = ${t}(lengthResized) / outputWidth; const center = ${t}(lengthOriginal) / 2; const offset = center * (1 - adjustment); return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",Tp=(e,t,r)=>`fn getNearestPixelFromOriginal(xOriginal: ${r}, isDownSample: bool) -> ${r} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";case"simple":default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",Op=(e,t,r)=>{let a=new Array(r).fill(0).concat(new Array(r).fill(1)),n=e.length===0?a:e.slice();return t.length>0?(t.forEach((o,s)=>{a[o]=n[s],a[s+r]=n[t.length+s]}),a):n},kp=(e,t,r,a)=>{let n=[];if(r.length>0)if(a.length>0){if(e.forEach(o=>n.push(o)),Math.max(...a)>e.length)throw new Error("axes is out of bound");a.forEach((o,s)=>n[o]=r[s])}else r.forEach(o=>n.push(o));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");n=e.map((o,s)=>Math.round(o*t[s]))}return n},Pp=(e,t,r)=>{let a=(()=>{switch(r.keepAspectRatioPolicy){case"not_larger":return r.axes.length>0?Math.min(...r.axes.map(o=>t[o]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return r.axes.length>0?Math.max(...r.axes.map(o=>t[o]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${r.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let n=e.slice();return r.axes.length>0?(r.axes.forEach(o=>t[o]=a),r.axes.forEach(o=>n[o]=Math.round(e[o]*t[o]))):(t.fill(a,0,t.length),n.forEach((o,s)=>n[s]=Math.round(o*t[s]))),n},Rp=(e,t,r,a,n)=>` fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${r.length}> { var original_indices: array<${e.type.value}, ${r.length}>; for (var i:u32 = 0; i < ${r.length}; i++) { var output_index = ${e.indicesGet("output_indices","i")}; var scale = ${re("uniforms.scales","i",a)}; var roi_low = ${re("uniforms.roi","i",n)}; var roi_hi = ${re("uniforms.roi",`i + ${t.length}`,n)}; if (scale == 1.0) { original_indices[i] = ${e.type.value}(output_index); } else { var input_shape_i = ${re("uniforms.input_shape","i",t.length)}; var output_shape_i = ${re("uniforms.output_shape","i",r.length)}; original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, input_shape_i, roi_low, roi_hi); } } return original_indices; }`,zp=(e,t,r,a,n,o,s)=>` fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { var input_indices: ${e.type.indices}; for (var i:u32 = 0; i < ${a.length}; i++) { var output_index = ${t.indicesGet("output_indices","i")}; var input_index: u32; var scale = ${re("uniforms.scales","i",n)}; if (scale == 1.0) { input_index = output_index; } else { var roi_low = ${re("uniforms.roi","i",o)}; var roi_hi = ${re("uniforms.roi",`i + ${r.length}`,o)}; var input_shape_i = ${re("uniforms.input_shape","i",r.length)}; var output_shape_i = ${re("uniforms.output_shape","i",a.length)}; var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, input_shape_i, roi_low, roi_hi); if (!${s} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) { if (original_idx < 0) { input_index = 0; } else if (original_idx > ${t.type.value}(input_shape_i - 1)) { input_index = input_shape_i - 1; } else { input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1)); } } else { input_index = u32(original_idx); } } ${e.indicesSet("input_indices","i"," input_index")} } return input_indices; }`,Bp=(e,t)=>` fn checkInputIndices(input_indices: ${e.type.indices}) -> bool { for (var i:u32 = 0; i < ${t.length}; i++) { var input_index = ${e.indicesGet("input_indices","i")}; if (input_index < 0 || input_index >= ${re("uniforms.input_shape","i",t.length)}) { return false; } } return true; }`,tl=(e,t,r,a)=>e.rank>a?` ${e.indicesSet("input_indices",t,"channel")}; ${e.indicesSet("input_indices",r,"batch")}; `:"",Dp=(e,t,r,a,n)=>{let[s,l,i,c]=r.length===2?[-1,0,1,-1]:[0,2,3,1],f=e.type.value;return` fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${f} { var input_indices: ${e.type.indices}; ${e.indicesSet("input_indices",l,`max(0, min(row, ${r[l]} - 1))`)}; ${e.indicesSet("input_indices",i,`max(0, min(col, ${r[i]} - 1))`)}; ${tl(e,c,s,2)} return ${e.getByIndices("input_indices")}; } fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${f} { var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); var row:${f} = originalIndices[${l}]; var col:${f} = originalIndices[${i}]; ${a?`if (row < 0 || row > (${r[l]} - 1) || col < 0 || col > (${r[i]} - 1)) { return ${n}; }`:""}; row = max(0, min(row, ${r[l]} - 1)); col = max(0, min(col, ${r[i]} - 1)); var row1: u32 = u32(row); var col1: u32 = u32(col); var row2: u32 = u32(row + 1); var col2: u32 = u32(col + 1); var channel: u32 = ${r.length>2?`u32(originalIndices[${c}])`:"0"}; var batch: u32 = ${r.length>2?`u32(originalIndices[${s}])`:"0"}; var x11: ${f} = getInputValue(batch, channel, row1, col1); var x12: ${f} = getInputValue(batch, channel, row1, col2); var x21: ${f} = getInputValue(batch, channel, row2, col1); var x22: ${f} = getInputValue(batch, channel, row2, col2); var dx1: ${f} = abs(row - ${f}(row1)); var dx2: ${f} = abs(${f}(row2) - row); var dy1: ${f} = abs(col - ${f}(col1)); var dy2: ${f} = abs(${f}(col2) - col); if (row1 == row2) { dx1 = 0.5; dx2 = 0.5; } if (col1 == col2) { dy1 = 0.5; dy2 = 0.5; } return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1); }`},Mp=(e,t,r,a,n,o,s,l,i,c)=>{let f=r.length===2,m=!0,[h,w]=f?[0,1]:m?[2,3]:[1,2],b=e.type.value,$=I=>{let S=I===h?"row":"col";return` fn ${S}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${b} { var output_index = ${t.indicesGet("output_indices",I)}; var originalIdx: ${b} = getOriginalCoordinateFromResizedCoordinate(output_index, ${n[I]}, ${a[I]}, ${r[I]}, ${o[I]}, ${o[I]} + ${r.length}); var fractOriginalIdx: ${b} = originalIdx - floor(originalIdx); var coefs = getCubicInterpolationCoefs(fractOriginalIdx); if (${l} && (originalIdx < 0 || originalIdx > (${r[I]} - 1))) { return ${i}; } var data: array<${b}, 4> = array<${b}, 4>(0.0, 0.0, 0.0, 0.0); for (var i: i32 = -1; i < 3; i++) { var ${S}: ${b} = originalIdx + ${b}(i); if (${S} < 0 || ${S} >= ${r[I]}) { ${(()=>c?`coefs[i + 1] = 0.0; continue;`:l?`return ${i};`:`${S} = max(0, min(${S}, ${r[I]} - 1));`)()}; } var input_indices_copy: ${e.type.indices} = input_indices; ${e.indicesSet("input_indices_copy",I,`u32(${S})`)}; data[i + 1] = ${I===h?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"}; } return cubicInterpolation1D(data, coefs); }`};return` ${$(h)}; ${$(w)}; fn getCubicInterpolationCoefs(s: ${b}) -> array<${b}, 4> { var absS = abs(s); var coeffs: array<${b}, 4> = array<${b}, 4>(0.0, 0.0, 0.0, 0.0); var oneMinusAbsS: ${b} = 1.0 - absS; var twoMinusAbsS: ${b} = 2.0 - absS; var onePlusAbsS: ${b} = 1.0 + absS; coeffs[0] = ((${s} * onePlusAbsS - 5 * ${s}) * onePlusAbsS + 8 * ${s}) * onePlusAbsS - 4 * ${s}; coeffs[1] = ((${s} + 2) * absS - (${s} + 3)) * absS * absS + 1; coeffs[2] = ((${s} + 2) * oneMinusAbsS - (${s} + 3)) * oneMinusAbsS * oneMinusAbsS + 1; coeffs[3] = ((${s} * twoMinusAbsS - 5 * ${s}) * twoMinusAbsS + 8 * ${s}) * twoMinusAbsS - 4 * ${s}; return coeffs; } fn cubicInterpolation1D(x: array<${b}, 4>, coefs: array<${b}, 4>) -> ${b} { var coefsSum: ${b} = coefs[0] + coefs[1] + coefs[2] + coefs[3]; return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum; } fn bicubicInterpolation(output_indices: ${t.type.indices}) -> ${b} { var input_indices: ${e.type.indices} = output_indices; return colCubicInterpolation(input_indices, output_indices); } `},Up=(e,t,r,a,n)=>{let[s,l,i,c,f]=r.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],m=e.type.value;return` fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${m} { var input_indices: ${e.type.indices}; ${e.indicesSet("input_indices",l,`max(0, min(depth, ${r[l]} - 1))`)}; ${e.indicesSet("input_indices",i,`max(0, min(height, ${r[i]} - 1))`)}; ${e.indicesSet("input_indices",c,`max(0, min(width, ${r[c]} - 1))`)}; ${tl(e,f,s,3)} return ${e.getByIndices("input_indices")}; } fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${m} { var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); var depth:${m} = originalIndices[${l}]; var height:${m} = originalIndices[${i}]; var width:${m} = originalIndices[${c}]; ${a?`if (depth < 0 || depth > (${r[l]} - 1) || height < 0 || height > (${r[i]} - 1) || width < 0 || (width > ${r[c]} - 1)) { return ${n}; }`:""}; depth = max(0, min(depth, ${r[l]} - 1)); height = max(0, min(height, ${r[i]} - 1)); width = max(0, min(width, ${r[c]} - 1)); var depth1: u32 = u32(depth); var height1: u32 = u32(height); var width1: u32 = u32(width); var depth2: u32 = u32(depth + 1); var height2: u32 = u32(height + 1); var width2: u32 = u32(width + 1); var channel: u32 = ${r.length>3?`u32(originalIndices[${f}])`:"0"}; var batch: u32 = ${r.length>3?`u32(originalIndices[${s}])`:"0"}; var x111: ${m} = getInputValue(batch, channel, depth1, height1, width1); var x112: ${m} = getInputValue(batch, channel, depth1, height1, width2); var x121: ${m} = getInputValue(batch, channel, depth1, height2, width1); var x122: ${m} = getInputValue(batch, channel, depth1, height2, width2); var x211: ${m} = getInputValue(batch, channel, depth2, height1, width1); var x212: ${m} = getInputValue(batch, channel, depth2, height1, width2); var x221: ${m} = getInputValue(batch, channel, depth2, height2, width1); var x222: ${m} = getInputValue(batch, channel, depth2, height2, width2); var dx1: ${m} = abs(depth - ${m}(depth1)); var dx2: ${m} = abs(${m}(depth2) - depth); var dy1: ${m} = abs(height - ${m}(height1)); var dy2: ${m} = abs(${m}(height2) - height); var dz1: ${m} = abs(width - ${m}(width1)); var dz2: ${m} = abs(${m}(width2) - width); if (depth1 == depth2) { dx1 = 0.5; dx2 = 0.5; } if (height1 == height2) { dy1 = 0.5; dy2 = 0.5; } if (width1 == width2) { dz1 = 0.5; dz2 = 0.5; } return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 + x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1); }`},Np=(e,t,r,a,n,o)=>{let s=e.dims,l=Op(o,t.axes,s.length),i=kp(s,a,n,t.axes),c=a.slice();a.length===0&&(c=s.map((v,E)=>v===0?1:i[E]/v),t.keepAspectRatioPolicy!=="stretch"&&(i=Pp(s,c,t)));let f=q("output",e.dataType,i.length),m=D("input",e.dataType,s.length),h=M.size(i),w=s.length===i.length&&s.every((v,E)=>v===i[E]),b=t.coordinateTransformMode==="tf_crop_and_resize",$=t.extrapolationValue,I=m.type.value,S=v=>` ${w?"":` ${Ap(t.coordinateTransformMode,I)}; ${(()=>{switch(t.mode){case"nearest":return` ${Bp(m,s)}; ${Tp(t.nearestMode,r,I)}; ${zp(m,f,s,i,c.length,l.length,b)}; `;case"linear":return` ${Rp(f,s,i,c.length,l.length)}; ${(()=>{if(s.length===2||s.length===4)return`${Dp(m,f,s,b,$)}`;if(s.length===3||s.length===5)return`${Up(m,f,s,b,$)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()}; `;case"cubic":return` ${(()=>{if(s.length===2||s.length===4)return`${Mp(m,f,s,i,c,l,t.cubicCoeffA,b,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()}; `;default:throw Error("Invalid resize mode")}})()}; `} ${v.registerUniform("output_size","u32").registerUniform("scales","f32",c.length).registerUniform("roi","f32",l.length).declareVariables(m,f)} ${v.mainStart()} ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} ${w?"output[global_idx] = input[global_idx];":` let output_indices = ${f.offsetToIndices("global_idx")}; var input_indices: ${m.type.indices}; ${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices); if (checkInputIndices(input_indices)) { output[global_idx] = ${m.getByIndices("input_indices")}; } else { output[global_idx] = ${t.extrapolationValue}; }`;case"linear":return`output[global_idx] = ${s.length===2||s.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()}; `} }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${c.length>0?c:""}|${n.length>0?n:""}|${l.length>0?l:""}|${w}|${s}`,inputDependencies:["rank"]},getShaderSource:S,getRunData:()=>({outputs:[{dims:i,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:[{type:12,data:h},{type:1,data:c},{type:1,data:l},...K(s,i)]})}},Wp=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},rl=(e,t)=>{let r=[],a=[],n=[],o=Wp(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Ep(e.inputs,t,o,r,a,n),e.compute(Np(e.inputs[0],t,o,r,a,n),{inputs:[0]})},nl=e=>{let t=e.antialias,r=e.axes,a=e.coordinateTransformMode,n=e.cubicCoeffA,o=e.excludeOutside!==0,s=e.extrapolationValue,l=e.keepAspectRatioPolicy,i=e.mode,c=e.nearestMode===""?"simple":e.nearestMode;return $e({antialias:t,axes:r,coordinateTransformMode:a,cubicCoeffA:n,excludeOutside:o,extrapolationValue:s,keepAspectRatioPolicy:l,mode:i,nearestMode:c})}});var Vp,Gp,il,ol=F(()=>{"use strict";ie();_e();be();Vp=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],r=e[1],a=e[2];if(t.dataType!==r.dataType||t.dataType!==a.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(r.dims.length!==3&&r.dims.length!==2)throw new Error("Skip must be 2D or 3D");let n=t.dims[t.dims.length-1],o=t.dims[t.dims.length-2];if(r.dims[r.dims.length-1]!==n)throw new Error("Skip must have the same hidden size as input");if(r.dims[r.dims.length-2]!==o)throw new Error("Skip must have the same sequence length as input");if(a.dims.length!==1)throw new Error("Gamma must be 1D");if(a.dims[a.dims.length-1]!==n)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let s=e[3];if(s.dims.length!==1)throw new Error("Beta must be 1D");if(s.dims[s.dims.length-1]!==n)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let s=e[4];if(s.dims.length!==1)throw new Error("Bias must be 1D");if(s.dims[s.dims.length-1]!==n)throw new Error("Bias must have the same hidden size as input")}},Gp=(e,t,r,a)=>{let n=e[0].dims,o=M.size(n),s=n,l=o,i=n.slice(-1)[0],c=a?n.slice(0,-1).concat(1):[],f=e.length>3,m=e.length>4,h=a&&r>1,w=a&&r>2,b=r>3,$=Me(i),I=[{type:12,data:l},{type:12,data:$},{type:12,data:i},{type:1,data:t.epsilon}],S=E=>{let A=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],k=[D("x",e[0].dataType,e[0].dims,$),D("skip",e[1].dataType,e[1].dims,$),D("gamma",e[2].dataType,e[2].dims,$)];f&&k.push(D("beta",e[3].dataType,e[3].dims,$)),m&&k.push(D("bias",e[4].dataType,e[4].dims,$)),k.push(q("output",e[0].dataType,s,$)),h&&k.push(q("mean_output",1,c)),w&&k.push(q("inv_std_output",1,c)),b&&k.push(q("input_skip_bias_sum",e[0].dataType,s,$));let U=ke(e[0].dataType);return` ${E.registerUniforms(A).declareVariables(...k)} ${E.mainStart()} ${E.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size / uniforms.hidden_size")} let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components; let offset = global_idx * hidden_size_vectorized; var sum = ${Xe("f32",$)}; var squareSum = ${Xe("f32",$)}; for (var i: u32 = 0; i < hidden_size_vectorized; i++) { let skip_value = skip[offset + i]; let bias_value = ${m?"bias[i]":"0.0"}; let input_value = x[offset + i]; let value = input_value + skip_value + bias_value; ${b?"input_skip_bias_sum[offset + i] = value;":""} output[offset + i] = value; let f32_value = ${mt(U,$,"value")}; sum += f32_value; squareSum += f32_value * f32_value; } let mean = ${it("sum",$)} / f32(uniforms.hidden_size); let inv_std_dev = inverseSqrt(${it("squareSum",$)} / f32(uniforms.hidden_size) - mean * mean + uniforms.epsilon); ${h?"mean_output[global_idx] = mean;":""} ${w?"inv_std_output[global_idx] = inv_std_dev;":""} for (var i: u32 = 0; i < hidden_size_vectorized; i++) { output[offset + i] = (output[offset + i] - ${U}(mean)) * ${U}(inv_std_dev) * gamma[i] + ${f?"beta[i]":"0.0"}; } }`},v=[{dims:s,dataType:e[0].dataType}];return r>1&&v.push({dims:c,dataType:1}),r>2&&v.push({dims:c,dataType:1}),r>3&&v.push({dims:n,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${$};${h};${w};${b}`,inputDependencies:e.map((E,A)=>"type")},getShaderSource:S,getRunData:()=>({outputs:v,dispatchGroup:{x:Math.ceil(l/i/64)},programUniforms:I})}},il=(e,t)=>{Vp(e.inputs);let a=[0];e.outputCount>1&&a.push(-3),e.outputCount>2&&a.push(-3),e.outputCount>3&&a.push(3),e.compute(Gp(e.inputs,t,e.outputCount,!1),{outputs:a})}});var Hp,On,Lp,sl,Fp,qp,ul,ll,dl=F(()=>{"use strict";ie();_e();Ye();be();Hp=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((r,a)=>{if(e[a+1].dataType!==6&&e[a+1].dataType!==7)throw new Error(`Input ${a} must be an array of int32 or int64`)})},On=(e,t)=>{let r=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(a=>r.push(Number(a)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(a=>r.push(Number(a)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return r},Lp=(e,t)=>{if(e.length>1){let r=On(e,1),a=On(e,2),n=On(e,3);return n.length===0&&(n=[...Array(e[0].dims.length).keys()]),$e({starts:r,ends:a,axes:n})}else return t},sl=(e,t,r,a,n)=>{let o=e;return e<0&&(o+=r[a[t]]),n[t]<0?Math.max(0,Math.min(o,r[a[t]]-1)):Math.max(0,Math.min(o,r[a[t]]))},Fp=(e,t,r)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { var input_indices: ${e.type.indices}; var carry = 0u; for (var i = ${r.length}; i >= 0; i--) { let input_shape_i = ${re("uniforms.input_shape","i",r.length)}; let steps_i = ${re("uniforms.steps","i",r.length)}; let signs_i = ${re("uniforms.signs","i",r.length)}; let starts_i = ${re("uniforms.starts","i",r.length)}; var output_index = ${t.indicesGet("output_indices","i")}; var input_index = output_index * steps_i + starts_i + carry; carry = input_index / input_shape_i; input_index = input_index % input_shape_i; if (signs_i < 0) { input_index = input_shape_i - input_index - 1u + starts_i; } ${e.indicesSet("input_indices","i","input_index")}; } return input_indices; }`,qp=(e,t)=>{let r=e[0].dims,a=M.size(r),n=t.axes.length>0?M.normalizeAxes(t.axes,r.length):[...Array(r.length).keys()],o=On(e,4);o.forEach(S=>S!==0||(()=>{throw new Error("step cannot be 0")})),o.length===0&&(o=Array(n.length).fill(1));let s=t.starts.map((S,v)=>sl(S,v,r,n,o)),l=t.ends.map((S,v)=>sl(S,v,r,n,o));if(n.length!==s.length||n.length!==l.length)throw new Error("start, ends and axes should have the same number of elements");if(n.length!==r.length)for(let S=0;SMath.sign(S));o.forEach((S,v,E)=>{if(S<0){let A=(l[v]-s[v])/S,k=s[v],U=k+A*o[v];s[v]=U,l[v]=k,E[v]=-S}});let c=r.slice(0);n.forEach((S,v)=>{c[S]=Math.ceil((l[S]-s[S])/o[S])});let f={dims:c,dataType:e[0].dataType},m=q("output",e[0].dataType,c.length),h=D("input",e[0].dataType,e[0].dims.length),w=M.size(c),b=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:s.length},{name:"signs",type:"i32",length:i.length},{name:"steps",type:"u32",length:o.length}],$=[{type:12,data:w},{type:12,data:s},{type:6,data:i},{type:12,data:o},...K(e[0].dims,c)],I=S=>` ${S.registerUniforms(b).declareVariables(h,m)} ${Fp(h,m,r)} ${S.mainStart()} ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} let output_indices = ${m.offsetToIndices("global_idx")}; let input_indices = calculateInputIndices(output_indices); ${m.setByOffset("global_idx",h.getByIndices("input_indices"))} }`;return{name:"Slice",shaderCache:{hint:`${i.length}_${s.length}_${o.length}`,inputDependencies:["rank"]},getShaderSource:I,getRunData:()=>({outputs:[f],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:$})}},ul=(e,t)=>{Hp(e.inputs,t);let r=Lp(e.inputs,t);e.compute(qp(e.inputs,r),{inputs:[0]})},ll=e=>{let t=e.starts,r=e.ends,a=e.axes;return $e({starts:t,ends:r,axes:a})}});var jp,Kp,cl,pl,fl=F(()=>{"use strict";ie();_e();Ye();be();jp=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},Kp=(e,t)=>{let r=e.dims,a=M.size(r),n=64,o=t.axis;if(o<0&&(o=r.length+o),oS===4?`max(max(${I}.x, ${I}.y), max(${I}.z, ${I}.w))`:S===2?`max(${I}.x, ${I}.y)`:S===3?`max(max(${I}.x, ${I}.y), ${I}.z)`:I,m=D("x",e.dataType,e.dims,i),h=q("result",e.dataType,e.dims,i),w=m.type.value,b=ke(e.dataType)==="f32"?`var threadMax = ${w}(-3.402823e+38f);`:`var threadMax = ${w}(-65504.0h);`,$=I=>` var rowMaxShared : ${w}; var rowSumShared : ${w}; var threadShared : array<${w}, ${n}>; fn getValue(row: i32, col: i32, row_stride: i32) -> ${w} { let index = row * row_stride + col; return x[index]; } fn setValue(row: i32, col: i32, row_stride: i32, value: ${w}) { let index = row * row_stride + col; result[index] = value; } ${I.registerUniform("packedCols","i32").declareVariables(m,h)} ${I.mainStart()} let gindex = i32(global_idx); let lindex = i32(local_idx); const wg = ${n}; let row = gindex / wg; let cols = uniforms.packedCols; let row_stride : i32 = uniforms.packedCols; // find the rows max ${b} for (var col = lindex; col < cols; col += wg) { let value = getValue(row, col, row_stride); threadMax = max(threadMax, value); } if (lindex < cols) { threadShared[lindex] = threadMax; } workgroupBarrier(); var reduceSize = min(cols, wg); for (var currSize = reduceSize >> 1; currSize > 0; currSize = reduceSize >> 1) { reduceSize = currSize + (reduceSize & 1); if (lindex < currSize) { threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]); } workgroupBarrier(); } if (lindex == 0) { rowMaxShared = ${w}(${f("threadShared[0]",i)}); } workgroupBarrier(); // find the rows sum var threadSum = ${w}(0.0); for (var col = lindex; col < cols; col += wg) { let subExp = exp(getValue(row, col, row_stride) - rowMaxShared); threadSum += subExp; } threadShared[lindex] = threadSum; workgroupBarrier(); for (var currSize = wg >> 1; currSize > 0; currSize = currSize >> 1) { if (lindex < currSize) { threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize]; } workgroupBarrier(); } if (lindex == 0) { rowSumShared = ${w}(${it("threadShared[0]",i)}); } workgroupBarrier(); // calculate final value for each element in the row for (var col = lindex; col < cols; col += wg) { let value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared; setValue(row, col, row_stride, value); } }`;return{name:"Softmax",shaderCache:{hint:`${i}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.dataType}],dispatchGroup:{x:l},programUniforms:[{type:12,data:c}]}),getShaderSource:$}},cl=(e,t)=>{jp(e.inputs),e.compute(Kp(e.inputs[0],t))},pl=e=>$e({axis:e.axis})});var Yp,Zp,Xp,Qp,Jp,ml,hl,gl=F(()=>{"use strict";ie();_e();Ye();be();Yp=e=>{if(!e||e.length<1)throw new Error("too few inputs")},Zp=(e,t)=>{let r=[],a=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(n=>r.push(Number(n))),a=r.length),$e({numOutputs:a,axis:t.axis,splitSizes:r})},Xp=e=>` fn calculateOutputIndex(index: u32) -> u32 { for (var i: u32 = 0u; i < ${e}u; i += 1u ) { if (index < ${re("uniforms.size_in_split_axis","i",e)}) { return i; } } return ${e}u; }`,Qp=e=>{let t=e.length,r=[];for(let a=0;a{let r=e[0].dims,a=M.size(r),n=e[0].dataType,o=M.normalizeAxis(t.axis,r.length),s=new Array(t.numOutputs),l=D("input",n,r.length),i=new Array(t.numOutputs),c=[],f=[],m=0,h=[{type:12,data:a}];for(let b=0;b` ${b.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",i.length).declareVariables(l,...s)} ${Xp(i.length)} ${Qp(s)} ${b.mainStart()} ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")} var indices = ${l.offsetToIndices("global_idx")}; var index = ${l.indicesGet("indices",o)}; let output_number = calculateOutputIndex(index); if (output_number != 0) { index -= ${re("uniforms.size_in_split_axis","output_number - 1u",i.length)}; ${l.indicesSet("indices",o,"index")}; } writeBufferData(output_number, indices, global_idx); }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:c,dispatchGroup:{x:Math.ceil(a/64)},programUniforms:h})}},ml=(e,t)=>{Yp(e.inputs);let r=e.inputs.length===1?t:Zp(e.inputs,t);e.compute(Jp(e.inputs,r),{inputs:[0]})},hl=e=>{let t=e.axis,r=e.splitSizes,a=e.numOutputs<0?r.length:e.numOutputs;if(a!==r.length)throw new Error("numOutputs and splitSizes lengh must be equal");return $e({axis:t,numOutputs:a,splitSizes:r})}});var yl,ef,tf,rf,bl,wl=F(()=>{"use strict";ie();_e();be();yl=e=>Array.from(e.getBigInt64Array(),Number),ef=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(yl(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},tf=(e,t)=>{let r=[];for(let a=0;a{let t=e[0].dims,r=yl(e[1]),a=tf(t,r),n=M.size(a),o=e[0].dataType,s=D("input",o,t.length),l=q("output",o,a.length),i=c=>` const inputShape = ${s.indices(...t)}; ${c.registerUniform("output_size","u32").declareVariables(s,l)} ${c.mainStart()} ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} let output_indices = ${l.offsetToIndices("global_idx")}; var input_indices: ${s.type.indices}; for (var i = 0; i < ${t.length}; i++) { let input_dim_i = ${s.indicesGet("uniforms.input_shape","i")}; let input_dim_value = ${l.indicesGet("output_indices","i")} % input_dim_i; ${s.indicesSet("input_indices","i","input_dim_value")} } ${l.setByOffset("global_idx",s.getByIndices("input_indices"))} }`;return{name:"Tile",shaderCache:{hint:`${r}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:[{type:12,data:n},...K(e[0].dims,a)]}),getShaderSource:i}},bl=e=>{ef(e.inputs),e.compute(rf(e.inputs),{inputs:[0]})}});var nf,af,vl,$l=F(()=>{"use strict";ie();_e();be();nf=(e,t,r,a,n)=>{let o=q("output_data",n,r.length,4),s=D("a_data",t[1].dataType,t[1].dims.length,4),l=D("b_data",t[2].dataType,t[2].dims.length,4),i=D("c_data",t[0].dataType,t[0].dims.length,4),c,f=(m,h,w)=>`select(${h}, ${m}, ${w})`;if(!a)c=o.setByOffset("global_idx",f(s.getByOffset("global_idx"),l.getByOffset("global_idx"),i.getByOffset("global_idx")));else{let m=(h,w,b="")=>{let $=`a_data[index_a${w}][component_a${w}]`,I=`b_data[index_b${w}][component_b${w}]`,S=`bool(c_data[index_c${w}] & (0xffu << (component_c${w} * 8)))`;return` let output_indices${w} = ${o.offsetToIndices(`global_idx * 4u + ${w}u`)}; let offset_a${w} = ${s.broadcastedIndicesToOffset(`output_indices${w}`,o)}; let offset_b${w} = ${l.broadcastedIndicesToOffset(`output_indices${w}`,o)}; let offset_c${w} = ${i.broadcastedIndicesToOffset(`output_indices${w}`,o)}; let index_a${w} = offset_a${w} / 4u; let index_b${w} = offset_b${w} / 4u; let index_c${w} = offset_c${w} / 4u; let component_a${w} = offset_a${w} % 4u; let component_b${w} = offset_b${w} % 4u; let component_c${w} = offset_c${w} % 4u; ${h}[${w}] = ${b}(${f($,I,S)}); `};n===9?c=` var data = vec4(0); ${m("data",0,"u32")} ${m("data",1,"u32")} ${m("data",2,"u32")} ${m("data",3,"u32")} output_data[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:c=` ${m("output_data[global_idx]",0)} ${m("output_data[global_idx]",1)} ${m("output_data[global_idx]",2)} ${m("output_data[global_idx]",3)} `}return` ${e.registerUniform("vec_size","u32").declareVariables(i,s,l,o)} ${e.mainStart()} ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} ${c} }`},af=e=>{let t=e[1].dims,r=e[2].dims,a=e[0].dims,n=e[1].dataType,o=!(M.areEqual(t,r)&&M.areEqual(r,a)),s=t,l=M.size(t);if(o){let c=vt.calcShape(vt.calcShape(t,r,!1),a,!1);if(!c)throw new Error("Can't perform where op on the given tensors");s=c,l=M.size(s)}let i=Math.ceil(l/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:c=>nf(c,e,s,o,n),getRunData:()=>({outputs:[{dims:s,dataType:n}],dispatchGroup:{x:Math.ceil(l/64/4)},programUniforms:[{type:12,data:i},...K(a,t,r,s)]})}},vl=e=>{e.compute(af(e.inputs))}});var _l,xl=F(()=>{"use strict";Mo();ga();Wo();Go();Is();Ms();Ws();$a();eu();nu();uu();cu();fu();gu();wu();_u();Su();Iu();xa();Tu();Ru();Bu();Qu();el();gn();al();ol();dl();fl();gl();wl();ir();$n();$l();_l=new Map([["Abs",[Ho]],["Acos",[Lo]],["Acosh",[Fo]],["Add",[Es]],["ArgMax",[Do,ha]],["ArgMin",[Bo,ha]],["Asin",[qo]],["Asinh",[jo]],["Atan",[Ko]],["Atanh",[Yo]],["Attention",[Uo]],["AveragePool",[Hu,Gu]],["BatchNormalization",[No]],["BiasAdd",[Vo]],["BiasSplitGelu",[Cs]],["Cast",[Xo,Zo]],["Ceil",[Jo]],["Clip",[Qo]],["Concat",[Us,Ns]],["Conv",[Ia,Ca]],["ConvTranspose",[Js,Qs]],["Cos",[es]],["Cosh",[ts]],["CumSum",[tu,ru]],["Div",[As]],["Einsum",[ou,su]],["Elu",[rs,wn]],["Equal",[Ts]],["Erf",[ns]],["Exp",[as]],["Expand",[du]],["FastGelu",[pu]],["Floor",[is]],["FusedConv",[Ia,Ca]],["Gather",[hu,mu]],["GatherElements",[bu,yu]],["Gelu",[os]],["Gemm",[$u,vu]],["GlobalAveragePool",[qu,Fu]],["GlobalMaxPool",[Xu,Zu]],["Greater",[Rs]],["GreaterOrEqual",[Bs]],["HardSigmoid",[ms,fs]],["InstanceNormalization",[xu]],["LayerNormalization",[Cu]],["LeakyRelu",[ss,wn]],["Less",[zs]],["LessOrEqual",[Ds]],["Log",[xs]],["MatMul",[qs]],["MatMulNBits",[Eu,Au]],["MaxPool",[Ku,Yu]],["Mul",[Os]],["MultiHeadAttention",[Pu,ku]],["Neg",[ls]],["Not",[us]],["Pad",[zu]],["Pow",[ks]],["Range",[Ju]],["Reciprocal",[ds]],["ReduceMin",[To]],["ReduceMean",[So]],["ReduceMax",[Ao]],["ReduceSum",[ko]],["ReduceProd",[Oo]],["ReduceL1",[Co]],["ReduceL2",[Io]],["ReduceLogSum",[Ro]],["ReduceLogSumExp",[Eo]],["ReduceSumSquare",[Po]],["Relu",[cs]],["Resize",[rl,nl]],["Sigmoid",[ps]],["Sin",[hs]],["Sinh",[gs]],["Slice",[ul,ll]],["SkipLayerNormalization",[il]],["Split",[ml,hl]],["Sqrt",[ys]],["Softmax",[cl,pl]],["Sub",[Ps]],["Tan",[bs]],["Tanh",[vs]],["ThresholdedRelu",[_s,wn]],["Tile",[bl]],["Transpose",[co,po]],["Where",[vl]]])});var kn,Sl=F(()=>{"use strict";lt();Dt();be();kn=class{constructor(t){this.backend=t;this.repo=new Map,this.attributesBound=!1}getArtifact(t){return this.repo.get(t)}setArtifact(t,r){this.repo.set(t,r)}run(t,r,a,n,o){st(t.programInfo.name);let s=this.backend.device,l=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let i=[];for(let f of r)i.push({binding:i.length,resource:{buffer:f.buffer}});for(let f of a)i.push({binding:i.length,resource:{buffer:f.buffer}});o&&i.push({binding:i.length,resource:o});let c=s.createBindGroup({layout:t.computePipeline.getBindGroupLayout(0),entries:i,label:t.programInfo.name});if(this.backend.sessionStatus==="capturing"){let f={kernelId:this.backend.currentKernelId,computePipeline:t.computePipeline,bindGroup:c,dispatchGroup:n};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(f)}l.setPipeline(t.computePipeline),l.setBindGroup(0,c),l.dispatchWorkgroups(...n),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),nt(t.programInfo.name)}dispose(){}build(t,r){st(t.name);let a=this.backend.device,n=[];a.features.has("shader-f16")&&n.push("enable f16;");let o=uo(r),s=t.getShaderSource(o),l=`${n.join(` `)} ${o.additionalImplementations} ${s}`,i=a.createShaderModule({code:l,label:t.name});De("verbose",()=>`[WebGPU] ${t.name} shader code: ${l}`);let c=a.createComputePipeline({compute:{module:i,entryPoint:"main"},layout:"auto",label:t.name});return nt(t.name),{programInfo:t,computePipeline:c}}normalizeDispatchGroupSize(t){let r=typeof t=="number"?t:t.x,a=typeof t=="number"?1:t.y||1,n=typeof t=="number"?1:t.z||1,o=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(r<=o&&a<=o&&n<=o)return[r,a,n];let s=r*a*n,l=Math.ceil(Math.sqrt(s));if(l>o){if(l=Math.ceil(Math.cbrt(s)),l>o)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[l,l,l]}else return[l,l,1]}}});var of,sf,Pa,Pn,Cl=F(()=>{"use strict";lt();ie();Dt();no();so();xl();Sl();of=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let r=[];for(let a=0;a{let a=e.name;return e.shaderCache?.hint&&(a+="["+e.shaderCache.hint+"]"),a+=":"+r+`:${of(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,a},Pa=class{constructor(t){t&&(this.architecture=t.architecture,this.vendor=t.vendor)}isArchitecture(t){return this.architecture===t}isVendor(t){return this.vendor===t}},Pn=class{constructor(){this.currentSessionId=null;this.currentKernelId=null;this.commandEncoder=null;this.computePassEncoder=null;this.maxDispatchNumber=16;this.pendingDispatchNumber=0;this.pendingKernels=[];this.pendingQueries=new Map;this.sessionStatus="default";this.capturedCommandList=new Map;this.capturedPendingKernels=new Map;this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let t=this.kernelCustomData.get(this.currentKernelId);return t||(t={},this.kernelCustomData.set(this.currentKernelId,t)),t}async initialize(t,r){this.env=t;let a=[],n={requiredLimits:{maxComputeWorkgroupStorageSize:r.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:r.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:r.limits.maxStorageBufferBindingSize,maxBufferSize:r.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:r.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:r.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:r.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:r.limits.maxComputeWorkgroupSizeZ},requiredFeatures:a};r.features.has("chromium-experimental-timestamp-query-inside-passes")?a.push("chromium-experimental-timestamp-query-inside-passes"):r.features.has("timestamp-query")&&a.push("timestamp-query"),r.features.has("shader-f16")&&a.push("shader-f16"),this.device=await r.requestDevice(n),this.adapterInfo=new Pa(await r.requestAdapterInfo()),this.gpuDataManager=oo(this),this.programManager=new kn(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,to(t.logLevel,!!t.debug),this.device.onuncapturederror=o=>{o.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${o.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:r,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let t=this.getCommandEncoder(),r={};this.queryType==="at-passes"&&(r.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=t.beginComputePass(r)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;st(),this.endComputePass();let t;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),t=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(t,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,t,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&t.mapAsync(GPUMapMode.READ).then(()=>{let r=new BigUint64Array(t.getMappedRange()),a=this.pendingQueries.get(t);for(let n=0;n"u"&&(this.queryTimeBase=w);let $=Number(w-this.queryTimeBase),I=Number(b-this.queryTimeBase);if(!Number.isSafeInteger($)||!Number.isSafeInteger(I))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:m.map(S=>({dims:S.dims,dataType:Bt(S.dataType)})),outputsMetadata:h.map(S=>({dims:S.dims,dataType:Bt(S.dataType)})),kernelId:s,kernelType:i,kernelName:c,programName:f,startTime:$,endTime:I});else{let S="";m.forEach((E,A)=>{S+=`input[${A}]: [${E.dims}] | ${Bt(E.dataType)}, `});let v="";h.forEach((E,A)=>{v+=`output[${A}]: [${E.dims}] | ${Bt(E.dataType)}, `}),console.log(`[profiling] kernel "${s}|${i}|${c}|${f}" ${S}${v}execution time: ${I-$} ns`)}tn("GPU",`${f}::${w}::${b}`)}t.unmap(),this.pendingQueries.delete(t)}),nt()}run(t,r,a,n,o){st(t.name);let s=[];for(let v=0;vE):a;if(f.length!==l.length)throw new Error(`Output size ${f.length} must be equal to ${l.length}.`);let m=[],h=[];for(let v=0;v=l.length)throw new Error(`Invalid output index: ${f[v]}`);if(f[v]===-3)continue;let E=f[v]===-1,A=f[v]===-2,k=E||A?o(l[v].dataType,l[v].dims):n(f[v],l[v].dataType,l[v].dims);if(m.push(k),k.data===0)continue;let U=this.gpuDataManager.get(k.data);if(!U)throw new Error(`no GPU data for output: ${k.data}`);if(E&&this.temporaryData.push(U),A){let N=this.kernelPersistentData.get(this.currentKernelId);N||(N=[],this.kernelPersistentData.set(this.currentKernelId,N)),N.push(U)}h.push(U)}if(s.length!==r.length||h.length!==m.length){if(h.length===0)return nt(t.name),m;throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let w;if(c){let v=0,E=[];c.forEach(N=>{let H=typeof N.data=="number"?[N.data]:N.data;if(H.length===0)return;let z=N.type===10?2:4,L,pe;N.type===10?(pe=H.length>4?16:H.length>2?8:H.length*z,L=H.length>4?16:z*H.length):(pe=H.length<=2?H.length*z:16,L=16),v=Math.ceil(v/pe)*pe,E.push(v);let Ie=N.type===10?8:4;v+=H.length>4?Math.ceil(H.length/Ie)*L:H.length*z});let A=16;v=Math.ceil(v/A)*A;let k=new ArrayBuffer(v);c.forEach((N,H)=>{let z=E[H],L=typeof N.data=="number"?[N.data]:N.data;if(N.type===6)new Int32Array(k,z,L.length).set(L);else if(N.type===12)new Uint32Array(k,z,L.length).set(L);else if(N.type===10)new Uint16Array(k,z,L.length).set(L);else if(N.type===1)new Float32Array(k,z,L.length).set(L);else throw new Error(`Unsupported uniform type: ${Bt(N.type)}`)});let U=this.gpuDataManager.create(v,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(U.buffer,0,k,0,v),this.gpuDataManager.release(U.id),w={offset:0,size:v,buffer:U.buffer}}let b=this.programManager.normalizeDispatchGroupSize(i),$=b[1]===1&&b[2]===1,I=sf(t,r,$),S=this.programManager.getArtifact(I);if(S||(S=this.programManager.build(t,b),this.programManager.setArtifact(I,S),De("info",()=>`[artifact] key: ${I}, programName: ${t.name}`)),De("info",()=>`[ProgramManager] run "${t.name}" (key=${I}) with ${b[0]}x${b[1]}x${b[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let v={kernelId:this.currentKernelId,programName:S.programInfo.name,inputTensorViews:r,outputTensorViews:m};this.pendingKernels.push(v),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(v)}return this.programManager.run(S,s,h,b,w),nt(t.name),m}upload(t,r){this.gpuDataManager.upload(t,r)}memcpy(t,r){this.gpuDataManager.memcpy(t,r)}async download(t,r){await this.gpuDataManager.download(t,r)}alloc(t){return this.gpuDataManager.create(t).id}free(t){return this.gpuDataManager.release(t)}createKernel(t,r,a,n){let o=_l.get(t);if(!o)throw new Error(`kernel not implemented: ${t}`);let s={kernelType:t,kernelName:n,kernelEntry:o[0],attributes:[o[1],a]};this.kernels.set(r,s)}releaseKernel(t){let r=this.kernelPersistentData.get(t);if(r){for(let a of r)this.gpuDataManager.release(a.id);this.kernelPersistentData.delete(t)}this.kernelCustomData.delete(t),this.kernels.delete(t)}computeKernel(t,r,a){let n=this.kernels.get(t);if(!n)throw new Error(`kernel not created: ${t}`);let o=n.kernelType,s=n.kernelName,l=n.kernelEntry,i=n.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${o}] ${s}" is not allowed to be called recursively`);this.currentKernelId=t,i[0]&&(i[1]=i[0](i[1]),i[0]=void 0),De("info",()=>`[WebGPU] Start to run kernel "[${o}] ${s}"...`);let c=this.env.debug;this.temporaryData=[];try{return c&&this.device.pushErrorScope("validation"),l(r,i[1]),0}catch(f){return a.push(Promise.resolve(`[WebGPU] Kernel "[${o}] ${s}" failed. ${f}`)),1}finally{c&&a.push(this.device.popErrorScope().then(f=>f?`GPU validation error for kernel "[${o}] ${s}": ${f.message}`:null));for(let f of this.temporaryData)this.gpuDataManager.release(f.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(t,r,a,n){let o=this.sessionExternalDataMapping.get(t);o||(o=new Map,this.sessionExternalDataMapping.set(t,o));let s=o.get(r),l=this.gpuDataManager.registerExternalBuffer(a,n,s?.[1]);return o.set(r,[l,a]),l}unregisterBuffers(t){let r=this.sessionExternalDataMapping.get(t);r&&(r.forEach(a=>this.gpuDataManager.unregisterExternalBuffer(a[1])),this.sessionExternalDataMapping.delete(t))}getBuffer(t){let r=this.gpuDataManager.get(t);if(!r)throw new Error(`no GPU data for buffer: ${t}`);return r.buffer}createDownloader(t,r,a){return async()=>{let n=await ua(this,t,r);return ro(n.buffer,a)}}writeTimestamp(t){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,t)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){De("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){De("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){De("info","replay"),this.sessionStatus="replaying";let t=this.capturedCommandList.get(this.currentSessionId),r=this.capturedPendingKernels.get(this.currentSessionId),a=t.length;this.pendingKernels=[];for(let n=0;n=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onReleaseSession(t){this.unregisterBuffers(t),this.capturedCommandList.has(t)&&this.capturedCommandList.delete(t),this.capturedPendingKernels.has(t)&&this.capturedPendingKernels.delete(t),this.gpuDataManager.onReleaseSession(t)}onRunStart(t){this.currentSessionId=t,this.setQueryType()}}});var Il={};rr(Il,{init:()=>uf});var Pr,Ra,uf,El=F(()=>{"use strict";ie();Cl();Dt();_e();Pr=class e{constructor(t,r,a,n){this.module=t;this.dataType=r;this.data=a;this.dims=n}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=M.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=M.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=M.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(M.size(t)!==M.size(this.dims))throw new Error("Invalid new shape");return new e(this.module,this.dataType,this.data,t)}},Ra=class{constructor(t,r,a){this.module=t;this.backend=r;this.customDataOffset=0;this.customDataSize=0;this.adapterInfo=r.adapterInfo;let n=t.HEAPU32,o=a>>>2;this.opKernelContext=n[o++];let s=n[o++];this.outputCount=n[o++],this.customDataOffset=n[o++],this.customDataSize=n[o++];let l=[];for(let i=0;itypeof l=="number"?this.inputs[l]:l)??this.inputs,n=r?.outputs??[],o=(l,i,c)=>new Pr(this.module,i,this.output(l,c),c),s=(l,i)=>{let c=Ir(l);if(!c)throw new Error(`Unsupported data type: ${l}`);let f=c*M.size(i),m=f>0?this.backend.gpuDataManager.create(f).id:0;return new Pr(this.module,l,m,i)};return this.backend.run(t,a,n,o,s)}output(t,r){let a=this.module.stackSave();try{let n=this.module.stackAlloc((1+r.length)*4),o=n>>2;this.module.HEAPU32[o++]=r.length;for(let s=0;s{let n=t.jsepInit;if(!n)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let o=new Pn;await o.initialize(r,a),n("webgpu",[o,s=>o.alloc(s),s=>o.free(s),(s,l,i,c=!1)=>{if(c)De("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${s}, dst=${l}, size=${i}`),o.memcpy(s,l);else{De("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${s}, gpuDataId=${l}, size=${i}`);let f=t.HEAPU8.subarray(s>>>0,(s>>>0)+i);o.upload(l,f)}},async(s,l,i)=>{De("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${s}, dataOffset=${l}, size=${i}`),await o.download(s,()=>t.HEAPU8.subarray(l>>>0,(l>>>0)+i))},(s,l,i)=>o.createKernel(s,l,i,t.UTF8ToString(t._JsepGetNodeName(l))),s=>o.releaseKernel(s),(s,l,i,c)=>{De("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${i}, kernel=${s}, contextDataOffset=${l}`);let f=new Ra(t,o,l);return o.computeKernel(s,f,c)},()=>o.captureBegin(),()=>o.captureEnd(),()=>o.replay()])}else n("webnn")}});var lf,Tl,Ol,jt,df,za,kl,Pl,Al,Rl,zl,Bl,Dl=F(()=>{"use strict";Zi();Qi();ie();nr();on();oa();lf=(e,t)=>{Ge()._OrtInit(e,t)!==0&&Be("Can't initialize onnxruntime.")},Tl=async e=>{lf(e.wasm.numThreads,Er(e.logLevel))},Ol=async(e,t)=>{{let r=(El(),Gt(Il)).init;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");let a=e.webgpu.adapter;if(a){if(typeof a.limits!="object"||typeof a.features!="object"||typeof a.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let n=e.webgpu.powerPreference;if(n!==void 0&&n!=="low-power"&&n!=="high-performance")throw new Error(`Invalid powerPreference setting: "${n}"`);let o=e.webgpu.forceFallbackAdapter;if(o!==void 0&&typeof o!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${o}"`);if(a=await navigator.gpu.requestAdapter({powerPreference:n,forceFallbackAdapter:o}),!a)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}if(!e.wasm.simd)throw new Error("Not supported for WebGPU=ON and SIMD=OFF. Please set `env.wasm.simd` to true when using `webgpu` EP");await r("webgpu",Ge(),e,a)}if(t==="webnn"){if(typeof navigator>"u"||!navigator.ml)throw new Error("WebNN is not supported in current environment");await r("webnn",Ge(),e)}}},jt=new Map,df=e=>{let t=Ge(),r=t.stackSave();try{let a=t.stackAlloc(8);return t._OrtGetInputOutputCount(e,a,a+4)!==0&&Be("Can't get session input/output count."),[t.HEAP32[a/4],t.HEAP32[a/4+1]]}finally{t.stackRestore(r)}},za=e=>{let t=Ge(),r=t._malloc(e.byteLength);if(r===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,r),[r,e.byteLength]},kl=async(e,t)=>{let r,a,n=Ge();Array.isArray(e)?[r,a]=e:e.buffer===n.HEAPU8.buffer?[r,a]=[e.byteOffset,e.byteLength]:[r,a]=za(e);let o=0,s=0,l=0,i=[],c=[],f=[];try{if([s,i]=Xi(t),t?.externalData&&n.mountExternalData){let v=[];for(let E of t.externalData){let A=typeof E=="string"?E:E.path;v.push(Ar(typeof E=="string"?E:E.data).then(k=>{n.mountExternalData(A,k)}))}await Promise.all(v)}o=await n._OrtCreateSession(r,a,s),o===0&&Be("Can't create a session.");let[m,h]=df(o),w=!!t?.enableGraphCapture,b=[],$=[],I=[];for(let v=0;vv==="gpu-buffer")&&(l=n._OrtCreateBinding(o),l===0&&Be("Can't create IO binding."),S={handle:l,outputPreferredLocations:I,outputPreferredLocationsEncoded:I.map(v=>ia(v))}),jt.set(o,[o,c,f,S,w,!1]),[o,b,$]}catch(m){throw c.forEach(h=>n._OrtFree(h)),f.forEach(h=>n._OrtFree(h)),l!==0&&n._OrtReleaseBinding(l),o!==0&&n._OrtReleaseSession(o),m}finally{n._free(r),s!==0&&n._OrtReleaseSessionOptions(s),i.forEach(m=>n._free(m)),n.unmountExternalData?.()}},Pl=e=>{let t=Ge(),r=jt.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[a,n,o,s,l]=r;s&&(l&&t._OrtClearBoundOutputs(s.handle),t._OrtReleaseBinding(s.handle)),t.jsepOnReleaseSession?.(e),n.forEach(i=>t._OrtFree(i)),o.forEach(i=>t._OrtFree(i)),t._OrtReleaseSession(a),jt.delete(e)},Al=(e,t,r,a,n,o=!1)=>{if(!e){t.push(0);return}let s=Ge(),l=e[0],i=e[1],c=e[3],f,m;if(l==="string"&&c==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");if(o&&c!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${n} when enableGraphCapture is true.`);if(c==="gpu-buffer"){let b=e[2].gpuBuffer,$=Ir(aa(l));m=i.reduce((S,v)=>S*v,1)*$;let I=s.jsepRegisterBuffer;if(!I)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');f=I(a,n,b,m)}else{let b=e[2];if(Array.isArray(b)){m=4*b.length,f=s._malloc(m),r.push(f);let $=f/4;for(let I=0;Is.HEAP32[b++]=I);let $=s._OrtCreateTensor(aa(l),f,m,w,i.length,ia(c));$===0&&Be(`Can't create tensor for input/output. session=${a}, index=${n}.`),t.push($)}finally{s.stackRestore(h)}},Rl=async(e,t,r,a,n,o)=>{let s=Ge(),l=jt.get(e);if(!l)throw new Error(`cannot run inference. invalid session id: ${e}`);let i=l[0],c=l[1],f=l[2],m=l[3],h=l[4],w=l[5],b=t.length,$=a.length,I=0,S=[],v=[],E=[],A=[],k=s.stackSave(),U=s.stackAlloc(b*4),N=s.stackAlloc(b*4),H=s.stackAlloc($*4),z=s.stackAlloc($*4);try{[I,S]=Yi(o);for(let X=0;XVe*qe,1);he=Bt(J);let It=m?.outputPreferredLocations[a[X]];if(he==="string"){if(It==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");let Ve=[],qe=Ee/4;for(let tt=0;tt0){let Ve=s.jsepGetBuffer;if(!Ve)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let qe=Ve(Ee),tt=Ir(J);if(tt===void 0||!un(he))throw new Error(`Unsupported data type: ${he}`);se=!0,Ue.push([he,et,{gpuBuffer:qe,download:s.jsepCreateDownloader(qe,Fe*tt,he),dispose:()=>{s._OrtReleaseTensor(xe)}},"gpu-buffer"])}else{let Ve=sn(he),qe=new Ve(Fe);new Uint8Array(qe.buffer,qe.byteOffset,qe.byteLength).set(s.HEAPU8.subarray(Ee,Ee+qe.byteLength)),Ue.push([he,et,qe,"cpu"])}}finally{s.stackRestore(fe),he==="string"&&Ee&&s._free(Ee),se||s._OrtReleaseTensor(xe)}}return m&&!h&&(s._OrtClearBoundOutputs(m.handle),jt.set(e,[i,c,f,m,h,!1])),Ue}finally{s.stackRestore(k),v.forEach(L=>s._OrtReleaseTensor(L)),E.forEach(L=>s._OrtReleaseTensor(L)),A.forEach(L=>s._free(L)),I!==0&&s._OrtReleaseRunOptions(I),S.forEach(L=>s._free(L))}},zl=e=>{let t=Ge(),r=jt.get(e);if(!r)throw new Error("invalid session id");let a=r[0],n=t._OrtEndProfiling(a);n===0&&Be("Can't get an profile file name."),t._OrtFree(n)},Bl=e=>{let t=[];for(let r of e){let a=r[2];!Array.isArray(a)&&"buffer"in a&&t.push(a.buffer)}return t}});var Ml=tr((Iv,pf)=>{pf.exports='/*!\n * ONNX Runtime Web v1.17.3\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n"use strict";(()=>{var vn=Object.defineProperty;var dd=Object.getOwnPropertyDescriptor;var ld=Object.getOwnPropertyNames;var cd=Object.prototype.hasOwnProperty;var q=(e,t)=>()=>(e&&(t=e(e=0)),t);var pr=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),Mr=(e,t)=>{for(var r in t)vn(e,r,{get:t[r],enumerable:!0})},pd=(e,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of ld(t))!cd.call(e,n)&&n!==r&&vn(e,n,{get:()=>t[n],enumerable:!(o=dd(t,n))||o.enumerable});return e};var Ft=e=>pd(vn({},"__esModule",{value:!0}),e);var $n={};Mr($n,{createReadStream:()=>Po,readFile:()=>md,readFileSync:()=>fd});var md,fd,Po,_n=q(()=>{md=void 0,fd=void 0,Po=void 0});var xn={};Mr(xn,{join:()=>hd});var hd,Sn=q(()=>{hd=void 0});var Ro=pr((ko,Cn)=>{"use strict";var Oo=(()=>{var e=typeof document<"u"&&document.currentScript?document.currentScript.src:void 0;return typeof __filename<"u"&&(e=e||__filename),function(t={}){var r=t,o,n;r.ready=new Promise((d,h)=>{o=d,n=h}),r.mountExternalData=(d,h)=>{(r.Fa||(r.Fa=new Map)).set(d,h)},r.unmountExternalData=()=>{delete r.Fa};let s=()=>{let d=(w,S,P)=>(...N)=>{let K=nt,X=S?.();N=w(...N);let ae=S?.();return X!==ae&&(w=ae,P(X),S=P=null),nt!=K?Cr():N},h=w=>async(...S)=>{try{if(r.Ea)throw Error("Session already started");let P=r.Ea={Za:S[0],errors:[]},N=await w(...S);if(r.Ea!==P)throw Error("Session mismatch");r.La?.flush();let K=P.errors;if(0ae),0r._OrtCreateSession,w=>r._OrtCreateSession=w),r._OrtRun=h(d(r._OrtRun,()=>r._OrtRun,w=>r._OrtRun=w)),r._OrtRunWithBinding=h(d(r._OrtRunWithBinding,()=>r._OrtRunWithBinding,w=>r._OrtRunWithBinding=w)),r._OrtBindInput=d(r._OrtBindInput,()=>r._OrtBindInput,w=>r._OrtBindInput=w),s=void 0};r.jsepInit=(d,h)=>{if(s?.(),d==="webgpu"){[r.La,r.Ra,r.Va,r.Ma,r.Ua,r.sa,r.Wa,r.Ya,r.Sa,r.Ta,r.Xa]=h;let w=r.La;r.jsepRegisterBuffer=(S,P,N,K)=>w.registerBuffer(S,P,N,K),r.jsepGetBuffer=S=>w.getBuffer(S),r.jsepCreateDownloader=(S,P,N)=>w.createDownloader(S,P,N),r.jsepOnReleaseSession=S=>{w.onReleaseSession(S)},r.jsepOnRunStart=S=>w.onRunStart(S)}};var u=Object.assign({},r),l="./this.program",a=(d,h)=>{throw h},p=typeof window=="object",m=typeof importScripts=="function",f=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string",b="",_,y,$;if(f){var I=(_n(),Ft($n)),C=(Sn(),Ft(xn));b=m?C.dirname(b)+"/":__dirname+"/",_=(d,h)=>(d=Ae(d)?new URL(d):C.normalize(d),I.readFileSync(d,h?void 0:"utf8")),$=d=>(d=_(d,!0),d.buffer||(d=new Uint8Array(d)),d),y=(d,h,w,S=!0)=>{d=Ae(d)?new URL(d):C.normalize(d),I.readFile(d,S?void 0:"utf8",(P,N)=>{P?w(P):h(S?N.buffer:N)})},!r.thisProgram&&1{throw process.exitCode=d,h},r.inspect=()=>"[Emscripten Module object]"}else(p||m)&&(m?b=self.location.href:typeof document<"u"&&document.currentScript&&(b=document.currentScript.src),e&&(b=e),b.indexOf("blob:")!==0?b=b.substr(0,b.replace(/[?#].*/,"").lastIndexOf("/")+1):b="",_=d=>{var h=new XMLHttpRequest;return h.open("GET",d,!1),h.send(null),h.responseText},m&&($=d=>{var h=new XMLHttpRequest;return h.open("GET",d,!1),h.responseType="arraybuffer",h.send(null),new Uint8Array(h.response)}),y=(d,h,w)=>{var S=new XMLHttpRequest;S.open("GET",d,!0),S.responseType="arraybuffer",S.onload=()=>{S.status==200||S.status==0&&S.response?h(S.response):w()},S.onerror=w,S.send(null)});var v=console.log.bind(console),A=console.error.bind(console);Object.assign(r,u),u=null,typeof WebAssembly!="object"&&se("no native wasm support detected");var T,D=!1,U,V,H,R,L,pe,Ie;function we(){var d=T.buffer;r.HEAP8=V=new Int8Array(d),r.HEAP16=new Int16Array(d),r.HEAPU8=H=new Uint8Array(d),r.HEAPU16=new Uint16Array(d),r.HEAP32=R=new Int32Array(d),r.HEAPU32=L=new Uint32Array(d),r.HEAPF32=pe=new Float32Array(d),r.HEAPF64=Ie=new Float64Array(d)}var ne=[],ze=[],Q=[],xe=0,me=null,ue=null;function se(d){throw d="Aborted("+d+")",A(d),D=!0,U=1,d=new WebAssembly.RuntimeError(d+". Build with -sASSERTIONS for more info."),n(d),d}var he=d=>d.startsWith("data:application/octet-stream;base64,"),Ae=d=>d.startsWith("file://"),He;if(He="ort-wasm-simd.wasm",!he(He)){var G=He;He=r.locateFile?r.locateFile(G,b):b+G}function J(d){if($)return $(d);throw"both async and sync fetching of the wasm failed"}function Se(d){if(p||m){if(typeof fetch=="function"&&!Ae(d))return fetch(d,{credentials:"same-origin"}).then(h=>{if(!h.ok)throw"failed to load wasm binary file at \'"+d+"\'";return h.arrayBuffer()}).catch(()=>J(d));if(y)return new Promise((h,w)=>{y(d,S=>h(new Uint8Array(S)),w)})}return Promise.resolve().then(()=>J(d))}function Qe(d,h,w){return Se(d).then(S=>WebAssembly.instantiate(S,h)).then(S=>S).then(w,S=>{A(`failed to asynchronously prepare wasm: ${S}`),se(S)})}function Xe(d,h){var w=He;return typeof WebAssembly.instantiateStreaming!="function"||he(w)||Ae(w)||f||typeof fetch!="function"?Qe(w,d,h):fetch(w,{credentials:"same-origin"}).then(S=>WebAssembly.instantiateStreaming(S,d).then(h,function(P){return A(`wasm streaming compile failed: ${P}`),A("falling back to ArrayBuffer instantiation"),Qe(w,d,h)}))}var Le,wt={931760:(d,h,w,S)=>{if(typeof r>"u"||!r.Fa)return 1;if(d=qe(d>>>0),d.startsWith("./")&&(d=d.substring(2)),d=r.Fa.get(d),!d)return 2;if(h>>>=0,w>>>=0,h+w>d.byteLength)return 3;try{return H.set(d.subarray(h,h+w),S>>>0>>>0),0}catch{return 4}},932261:()=>{r.Sa()},932292:()=>{r.Ta()},932321:()=>{r.Xa()},932346:d=>r.Ra(d),932379:d=>r.Va(d),932411:(d,h,w)=>{r.Ma(d,h,w,!0)},932450:(d,h,w)=>{r.Ma(d,h,w)},932483:d=>{r.sa("Abs",d,void 0)},932534:d=>{r.sa("Neg",d,void 0)},932585:d=>{r.sa("Floor",d,void 0)},932638:d=>{r.sa("Ceil",d,void 0)},932690:d=>{r.sa("Reciprocal",d,void 0)},932748:d=>{r.sa("Sqrt",d,void 0)},932800:d=>{r.sa("Exp",d,void 0)},932851:d=>{r.sa("Erf",d,void 0)},932902:d=>{r.sa("Sigmoid",d,void 0)},932957:(d,h,w)=>{r.sa("HardSigmoid",d,{alpha:h,beta:w})},933036:d=>{r.sa("Log",d,void 0)},933087:d=>{r.sa("Sin",d,void 0)},933138:d=>{r.sa("Cos",d,void 0)},933189:d=>{r.sa("Tan",d,void 0)},933240:d=>{r.sa("Asin",d,void 0)},933292:d=>{r.sa("Acos",d,void 0)},933344:d=>{r.sa("Atan",d,void 0)},933396:d=>{r.sa("Sinh",d,void 0)},933448:d=>{r.sa("Cosh",d,void 0)},933500:d=>{r.sa("Asinh",d,void 0)},933553:d=>{r.sa("Acosh",d,void 0)},933606:d=>{r.sa("Atanh",d,void 0)},933659:d=>{r.sa("Tanh",d,void 0)},933711:d=>{r.sa("Not",d,void 0)},933762:(d,h,w)=>{r.sa("Clip",d,{min:h,max:w})},933831:d=>{r.sa("Clip",d,void 0)},933883:(d,h)=>{r.sa("Elu",d,{alpha:h})},933941:d=>{r.sa("Relu",d,void 0)},933993:(d,h)=>{r.sa("LeakyRelu",d,{alpha:h})},934057:(d,h)=>{r.sa("ThresholdedRelu",d,{alpha:h})},934127:(d,h)=>{r.sa("Cast",d,{to:h})},934185:d=>{r.sa("Add",d,void 0)},934236:d=>{r.sa("Sub",d,void 0)},934287:d=>{r.sa("Mul",d,void 0)},934338:d=>{r.sa("Div",d,void 0)},934389:d=>{r.sa("Pow",d,void 0)},934440:d=>{r.sa("Equal",d,void 0)},934493:d=>{r.sa("Greater",d,void 0)},934548:d=>{r.sa("GreaterOrEqual",d,void 0)},934610:d=>{r.sa("Less",d,void 0)},934662:d=>{r.sa("LessOrEqual",d,void 0)},934721:(d,h,w,S,P)=>{r.sa("ReduceMean",d,{keepDims:!!h,noopWithEmptyAxes:!!w,axes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},934880:(d,h,w,S,P)=>{r.sa("ReduceMax",d,{keepDims:!!h,noopWithEmptyAxes:!!w,axes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},935038:(d,h,w,S,P)=>{r.sa("ReduceMin",d,{keepDims:!!h,noopWithEmptyAxes:!!w,axes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},935196:(d,h,w,S,P)=>{r.sa("ReduceProd",d,{keepDims:!!h,noopWithEmptyAxes:!!w,axes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},935355:(d,h,w,S,P)=>{r.sa("ReduceSum",d,{keepDims:!!h,noopWithEmptyAxes:!!w,axes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},935513:(d,h,w,S,P)=>{r.sa("ReduceL1",d,{keepDims:!!h,noopWithEmptyAxes:!!w,axes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},935670:(d,h,w,S,P)=>{r.sa("ReduceL2",d,{keepDims:!!h,noopWithEmptyAxes:!!w,axes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},935827:(d,h,w,S,P)=>{r.sa("ReduceLogSum",d,{keepDims:!!h,noopWithEmptyAxes:!!w,axes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},935988:(d,h,w,S,P)=>{r.sa("ReduceSumSquare",d,{keepDims:!!h,noopWithEmptyAxes:!!w,axes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},936152:(d,h,w,S,P)=>{r.sa("ReduceLogSumExp",d,{keepDims:!!h,noopWithEmptyAxes:!!w,axes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},936316:d=>{r.sa("Where",d,void 0)},936369:(d,h,w)=>{r.sa("Transpose",d,{perm:h?Array.from(R.subarray(h>>>0,w>>>0)):[]})},936477:(d,h,w,S,P,N,K,X,ae,de,fe,Oe,Re,O,oe)=>{r.sa("ConvTranspose",d,{format:ae?"NHWC":"NCHW",autoPad:h,dilations:[w],group:S,kernel_shape:[P],pads:[N,K],strides:[X],wIsConst:()=>!!V[de>>>0],outputPadding:fe?Array.from(R.subarray(fe>>>0,Oe>>>0)):[],outputShape:Re?Array.from(R.subarray(Re>>>0,O>>>0)):[],activation:qe(oe)})},936879:(d,h,w,S,P,N,K,X,ae,de,fe,Oe,Re,O)=>{r.sa("ConvTranspose",d,{format:X?"NHWC":"NCHW",autoPad:h,dilations:Array.from(R.subarray(w>>>0,(w>>>0)+2>>>0)),group:S,kernelShape:Array.from(R.subarray(P>>>0,(P>>>0)+2>>>0)),pads:Array.from(R.subarray(N>>>0,(N>>>0)+4>>>0)),strides:Array.from(R.subarray(K>>>0,(K>>>0)+2>>>0)),wIsConst:()=>!!V[ae>>>0],outputPadding:de?Array.from(R.subarray(de>>>0,fe>>>0)):[],outputShape:Oe?Array.from(R.subarray(Oe>>>0,Re>>>0)):[],activation:qe(O)})},937444:(d,h,w,S,P,N,K,X,ae,de,fe,Oe,Re,O,oe)=>{r.sa("ConvTranspose",d,{format:ae?"NHWC":"NCHW",autoPad:h,dilations:[w],group:S,kernel_shape:[P],pads:[N,K],strides:[X],wIsConst:()=>!!V[de>>>0],outputPadding:fe?Array.from(R.subarray(fe>>>0,Oe>>>0)):[],outputShape:Re?Array.from(R.subarray(Re>>>0,O>>>0)):[],activation:qe(oe)})},937846:(d,h,w,S,P,N,K,X,ae,de,fe,Oe,Re,O)=>{r.sa("ConvTranspose",d,{format:X?"NHWC":"NCHW",autoPad:h,dilations:Array.from(R.subarray(w>>>0,(w>>>0)+2>>>0)),group:S,kernelShape:Array.from(R.subarray(P>>>0,(P>>>0)+2>>>0)),pads:Array.from(R.subarray(N>>>0,(N>>>0)+4>>>0)),strides:Array.from(R.subarray(K>>>0,(K>>>0)+2>>>0)),wIsConst:()=>!!V[ae>>>0],outputPadding:de?Array.from(R.subarray(de>>>0,fe>>>0)):[],outputShape:Oe?Array.from(R.subarray(Oe>>>0,Re>>>0)):[],activation:qe(O)})},938411:(d,h)=>{r.sa("GlobalAveragePool",d,{format:h?"NHWC":"NCHW"})},938502:(d,h,w,S,P,N,K,X,ae,de,fe,Oe,Re,O,oe,Ce)=>{r.sa("AveragePool",d,{format:Ce?"NHWC":"NCHW",auto_pad:h,ceil_mode:w,count_include_pad:S,storage_order:P,dilations:[N,K],kernel_shape:[X,ae],pads:[de,fe,Oe,Re],strides:[O,oe]})},938786:(d,h)=>{r.sa("GlobalAveragePool",d,{format:h?"NHWC":"NCHW"})},938877:(d,h,w,S,P,N,K,X,ae,de,fe,Oe,Re,O,oe,Ce)=>{r.sa("AveragePool",d,{format:Ce?"NHWC":"NCHW",auto_pad:h,ceil_mode:w,count_include_pad:S,storage_order:P,dilations:[N,K],kernel_shape:[X,ae],pads:[de,fe,Oe,Re],strides:[O,oe]})},939161:(d,h)=>{r.sa("GlobalMaxPool",d,{format:h?"NHWC":"NCHW"})},939248:(d,h,w,S,P,N,K,X,ae,de,fe,Oe,Re,O,oe,Ce)=>{r.sa("MaxPool",d,{format:Ce?"NHWC":"NCHW",auto_pad:h,ceil_mode:w,count_include_pad:S,storage_order:P,dilations:[N,K],kernel_shape:[X,ae],pads:[de,fe,Oe,Re],strides:[O,oe]})},939528:(d,h)=>{r.sa("GlobalMaxPool",d,{format:h?"NHWC":"NCHW"})},939615:(d,h,w,S,P,N,K,X,ae,de,fe,Oe,Re,O,oe,Ce)=>{r.sa("MaxPool",d,{format:Ce?"NHWC":"NCHW",auto_pad:h,ceil_mode:w,count_include_pad:S,storage_order:P,dilations:[N,K],kernel_shape:[X,ae],pads:[de,fe,Oe,Re],strides:[O,oe]})},939895:(d,h,w,S,P)=>{r.sa("Gemm",d,{alpha:h,beta:w,transA:S,transB:P})},939999:d=>{r.sa("MatMul",d,void 0)},940053:(d,h,w,S)=>{r.sa("ArgMax",d,{keepDims:!!h,selectLastIndex:!!w,axis:S})},940161:(d,h,w,S)=>{r.sa("ArgMin",d,{keepDims:!!h,selectLastIndex:!!w,axis:S})},940269:(d,h)=>{r.sa("Softmax",d,{axis:h})},940332:(d,h)=>{r.sa("Concat",d,{axis:h})},940392:(d,h,w,S,P)=>{r.sa("Split",d,{axis:h,numOutputs:w,splitSizes:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},940532:d=>{r.sa("Expand",d,void 0)},940586:(d,h)=>{r.sa("Gather",d,{axis:Number(h)})},940657:(d,h)=>{r.sa("GatherElements",d,{axis:Number(h)})},940736:(d,h,w,S,P,N,K,X,ae,de,fe)=>{r.sa("Resize",d,{antialias:h,axes:w?Array.from(R.subarray(w>>>0,S>>>0)):[],coordinateTransformMode:qe(P),cubicCoeffA:N,excludeOutside:K,extrapolationValue:X,keepAspectRatioPolicy:qe(ae),mode:qe(de),nearestMode:qe(fe)})},941082:(d,h,w,S,P,N,K)=>{r.sa("Slice",d,{starts:h?Array.from(R.subarray(h>>>0,w>>>0)):[],ends:S?Array.from(R.subarray(S>>>0,P>>>0)):[],axes:N?Array.from(R.subarray(N>>>0,K>>>0)):[]})},941298:d=>{r.sa("Tile",d,void 0)},941350:(d,h,w)=>{r.sa("LayerNormalization",d,{axis:Number(h),epsilon:Number(w)})},941457:(d,h,w)=>{r.sa("InstanceNormalization",d,{epsilon:h,format:w?"NHWC":"NCHW"})},941571:(d,h,w)=>{r.sa("InstanceNormalization",d,{epsilon:h,format:w?"NHWC":"NCHW"})},941685:d=>{r.sa("Range",d,void 0)},941738:(d,h)=>{r.sa("Einsum",d,{equation:qe(h)})},941819:(d,h,w,S,P)=>{r.sa("Pad",d,{mode:h,value:w,pads:S?Array.from(R.subarray(S>>>0,P>>>0)):[]})},941946:(d,h,w,S,P,N)=>{r.sa("BatchNormalization",d,{epsilon:h,momentum:w,spatial:!!P,trainingMode:!!S,format:N?"NHWC":"NCHW"})},942115:(d,h,w,S,P,N)=>{r.sa("BatchNormalization",d,{epsilon:h,momentum:w,spatial:!!P,trainingMode:!!S,format:N?"NHWC":"NCHW"})},942284:(d,h,w)=>{r.sa("CumSum",d,{exclusive:Number(h),reverse:Number(w)})},942381:(d,h,w,S,P,N,K,X,ae)=>{r.sa("Attention",d,{numHeads:h,isUnidirectional:w,maskFilterValue:S,scale:P,doRotary:N,qkvHiddenSizes:K?Array.from(R.subarray(Number(X)>>>0,Number(X)+K>>>0)):[],pastPresentShareBuffer:!!ae})},942653:d=>{r.sa("BiasAdd",d,void 0)},942708:d=>{r.sa("BiasSplitGelu",d,void 0)},942769:d=>{r.sa("FastGelu",d,void 0)},942825:(d,h,w,S,P,N,K,X,ae,de,fe,Oe,Re)=>{r.sa("Conv",d,{format:ae?"NHWC":"NCHW",auto_pad:h,dilations:[w],group:S,kernel_shape:[P],pads:N?Array.from(R.subarray(N>>>0,K>>>0)):[],strides:[X],w_is_const:()=>!!V[de>>>0],activation:qe(fe),activation_params:Oe?Array.from(pe.subarray(Oe>>>0,Re>>>0)):[]})},943195:(d,h,w,S,P,N,K,X,ae,de,fe,Oe,Re,O,oe,Ce)=>{r.sa("Conv",d,{format:Oe?"NHWC":"NCHW",auto_pad:h,dilations:[w,S],group:P,kernel_shape:[N,K],pads:X?Array.from(R.subarray(X>>>0,ae>>>0)):[],strides:[de,fe],w_is_const:()=>!!V[Re>>>0],activation:qe(O),activation_params:oe?Array.from(pe.subarray(oe>>>0,Ce>>>0)):[]})},943586:d=>{r.sa("Gelu",d,void 0)},943638:(d,h,w,S,P,N)=>{r.sa("MatMulNBits",d,{k:h,n:w,accuracyLevel:S,bits:P,blockSize:N})},943765:(d,h,w,S,P,N)=>{r.sa("MultiHeadAttention",d,{numHeads:h,isUnidirectional:w,maskFilterValue:S,scale:P,doRotary:N})},943924:(d,h)=>{r.sa("SkipLayerNormalization",d,{epsilon:h})},944005:d=>{r.Wa(d)},944039:(d,h)=>r.Ya(d,h,r.Ea.Za,r.Ea.errors)};function Ne(d){this.name="ExitStatus",this.message=`Program terminated with exit(${d})`,this.status=d}function Fe(d){this.Ja=d-24,this.Pa=function(h){L[this.Ja+4>>>2>>>0]=h},this.Oa=function(h){L[this.Ja+8>>>2>>>0]=h},this.eb=function(h,w){this.Na(),this.Pa(h),this.Oa(w)},this.Na=function(){L[this.Ja+16>>>2>>>0]=0}}var Je=0,kt=0,vt=typeof TextDecoder<"u"?new TextDecoder("utf8"):void 0,xt=(d,h,w)=>{h>>>=0;var S=h+w;for(w=h;d[w]&&!(w>=S);)++w;if(16P?S+=String.fromCharCode(P):(P-=65536,S+=String.fromCharCode(55296|P>>10,56320|P&1023))}}else S+=String.fromCharCode(P)}return S},qe=(d,h)=>(d>>>=0)?xt(H,d,h):"",Zt=d=>{for(var h=0,w=0;w=S?h++:2047>=S?h+=2:55296<=S&&57343>=S?(h+=4,++w):h+=3}return h},Vt=(d,h,w,S)=>{if(w>>>=0,!(0=K){var X=d.charCodeAt(++N);K=65536+((K&1023)<<10)|X&1023}if(127>=K){if(w>=S)break;h[w++>>>0]=K}else{if(2047>=K){if(w+1>=S)break;h[w++>>>0]=192|K>>6}else{if(65535>=K){if(w+2>=S)break;h[w++>>>0]=224|K>>12}else{if(w+3>=S)break;h[w++>>>0]=240|K>>18,h[w++>>>0]=128|K>>12&63}h[w++>>>0]=128|K>>6&63}h[w++>>>0]=128|K&63}}return h[w>>>0]=0,w-P},St=d=>d%4===0&&(d%100!==0||d%400===0),$t=[0,31,60,91,121,152,182,213,244,274,305,335],Nt=[0,31,59,90,120,151,181,212,243,273,304,334],Wt=d=>{var h=Zt(d)+1,w=Lt(h);return w&&Vt(d,H,w,h),w},Rt=[],Qt=(d,h)=>{Rt.length=0;for(var w;w=H[d++>>>0];){var S=w!=105;S&=w!=112,h+=S&&h%8?4:0,Rt.push(w==112?L[h>>>2>>>0]:w==105?R[h>>>2>>>0]:Ie[h>>>3>>>0]),h+=S?8:4}return Rt},at={},Xt=()=>{if(!Gt){var d={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:l||"./this.program"},h;for(h in at)at[h]===void 0?delete d[h]:d[h]=at[h];var w=[];for(h in d)w.push(`${h}=${d[h]}`);Gt=w}return Gt},Gt,Ke=[null,[],[]],vr=[31,29,31,30,31,30,31,31,30,31,30,31],ke=[31,28,31,30,31,30,31,31,30,31,30,31];function $r(d){var h=Array(Zt(d)+1);return Vt(d,h,0,h.length),h}function Ht(d,h,w,S){function P(O,oe,Ce){for(O=typeof O=="number"?O.toString():O||"";O.lengthPr?-1:0ct-O.getDate())oe-=ct-O.getDate()+1,O.setDate(1),11>Ce?O.setMonth(Ce+1):(O.setMonth(0),O.setFullYear(O.getFullYear()+1));else{O.setDate(O.getDate()+oe);break}}return Ce=new Date(O.getFullYear()+1,0,4),oe=X(new Date(O.getFullYear(),0,4)),Ce=X(Ce),0>=K(oe,O)?0>=K(Ce,O)?O.getFullYear()+1:O.getFullYear():O.getFullYear()-1}d>>>=0,h>>>=0,w>>>=0,S>>>=0;var de=L[S+40>>>2>>>0];S={bb:R[S>>>2>>>0],ab:R[S+4>>>2>>>0],Ga:R[S+8>>>2>>>0],Ka:R[S+12>>>2>>>0],Ha:R[S+16>>>2>>>0],Da:R[S+20>>>2>>>0],xa:R[S+24>>>2>>>0],Ca:R[S+28>>>2>>>0],fb:R[S+32>>>2>>>0],$a:R[S+36>>>2>>>0],cb:de?qe(de):""},w=qe(w),de={"%c":"%a %b %d %H:%M:%S %Y","%D":"%m/%d/%y","%F":"%Y-%m-%d","%h":"%b","%r":"%I:%M:%S %p","%R":"%H:%M","%T":"%H:%M:%S","%x":"%m/%d/%y","%X":"%H:%M:%S","%Ec":"%c","%EC":"%C","%Ex":"%m/%d/%y","%EX":"%H:%M:%S","%Ey":"%y","%EY":"%Y","%Od":"%d","%Oe":"%e","%OH":"%H","%OI":"%I","%Om":"%m","%OM":"%M","%OS":"%S","%Ou":"%u","%OU":"%U","%OV":"%V","%Ow":"%w","%OW":"%W","%Oy":"%y"};for(var fe in de)w=w.replace(new RegExp(fe,"g"),de[fe]);var Oe="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),Re="January February March April May June July August September October November December".split(" ");de={"%a":O=>Oe[O.xa].substring(0,3),"%A":O=>Oe[O.xa],"%b":O=>Re[O.Ha].substring(0,3),"%B":O=>Re[O.Ha],"%C":O=>N((O.Da+1900)/100|0,2),"%d":O=>N(O.Ka,2),"%e":O=>P(O.Ka,2," "),"%g":O=>ae(O).toString().substring(2),"%G":O=>ae(O),"%H":O=>N(O.Ga,2),"%I":O=>(O=O.Ga,O==0?O=12:12{for(var oe=0,Ce=0;Ce<=O.Ha-1;oe+=(St(O.Da+1900)?vr:ke)[Ce++]);return N(O.Ka+oe,3)},"%m":O=>N(O.Ha+1,2),"%M":O=>N(O.ab,2),"%n":()=>`\n`,"%p":O=>0<=O.Ga&&12>O.Ga?"AM":"PM","%S":O=>N(O.bb,2),"%t":()=>" ","%u":O=>O.xa||7,"%U":O=>N(Math.floor((O.Ca+7-O.xa)/7),2),"%V":O=>{var oe=Math.floor((O.Ca+7-(O.xa+6)%7)/7);if(2>=(O.xa+371-O.Ca-2)%7&&oe++,oe)oe==53&&(Ce=(O.xa+371-O.Ca)%7,Ce==4||Ce==3&&St(O.Da)||(oe=1));else{oe=52;var Ce=(O.xa+7-O.Ca-1)%7;(Ce==4||Ce==5&&St(O.Da%400-1))&&oe++}return N(oe,2)},"%w":O=>O.xa,"%W":O=>N(Math.floor((O.Ca+7-(O.xa+6)%7)/7),2),"%y":O=>(O.Da+1900).toString().substring(2),"%Y":O=>O.Da+1900,"%z":O=>{O=O.$a;var oe=0<=O;return O=Math.abs(O)/60,(oe?"+":"-")+("0000"+(O/60*100+O%60)).slice(-4)},"%Z":O=>O.cb,"%%":()=>"%"},w=w.replace(/%%/g,"\\0\\0");for(fe in de)w.includes(fe)&&(w=w.replace(new RegExp(fe,"g"),de[fe](S)));return w=w.replace(/\\0\\0/g,"%"),fe=$r(w),fe.length>h?0:(V.set(fe,d>>>0),fe.length-1)}var Ct=d=>{try{d()}catch(h){se(h)}};function mn(){var d=ee,h={};for(let[w,S]of Object.entries(d))h[w]=typeof S=="function"?function(){_t.push(w);try{return S.apply(null,arguments)}finally{D||(_t.pop(),nt&&it===1&&_t.length===0&&(it=0,Ct(dr),typeof Fibers<"u"&&Fibers.gb()))}}:S;return h}var it=0,nt=null,te=0,_t=[],Jt={},_r={},xr=0,er=null,Sr=[];function Cr(){return new Promise((d,h)=>{er={resolve:d,reject:h}})}function Ir(){var d=Lt(65548),h=d+12;L[d>>>2>>>0]=h,L[d+4>>>2>>>0]=h+65536,h=_t[0];var w=Jt[h];return w===void 0&&(w=xr++,Jt[h]=w,_r[w]=h),R[d+8>>>2>>>0]=w,d}function Ar(d){if(!D){if(it===0){var h=!1,w=!1;d((S=0)=>{if(!D&&(te=S,h=!0,w)){it=2,Ct(()=>lr(nt)),typeof Browser<"u"&&Browser.Ia.Qa&&Browser.Ia.resume(),S=!1;try{var P=(0,ee[_r[R[nt+8>>>2>>>0]]])()}catch(X){P=X,S=!0}var N=!1;if(!nt){var K=er;K&&(er=null,(S?K.reject:K.resolve)(P),N=!0)}if(S&&!N)throw P}}),w=!0,h||(it=1,nt=Ir(),typeof Browser<"u"&&Browser.Ia.Qa&&Browser.Ia.pause(),Ct(()=>ur(nt)))}else it===2?(it=0,Ct(cr),nr(nt),nt=null,Sr.forEach(S=>{if(!D)try{S();try{U=U=S=U,r.onExit?.(S),D=!0,a(S,new Ne(S))}catch(P){P instanceof Ne||P=="unwind"||a(1,P)}}catch(P){P instanceof Ne||P=="unwind"||a(1,P)}})):se(`invalid state: ${it}`);return te}}function tr(d){return Ar(h=>{d().then(h)})}var Tr={n:function(d,h,w){return tr(async()=>{await r.Ua(d,h,w)})},a:function(d,h,w){throw d>>>=0,new Fe(d).eb(h>>>0,w>>>0),Je=d,kt++,Je},g:function(){return 0},J:function(){},A:function(){},C:function(){},L:function(){return 0},H:function(){},D:function(){},G:function(){},l:function(){},B:function(){},y:function(){},I:function(){},z:function(){},m:()=>1,q:function(d,h,w){d=h+2097152>>>0<4194305-!!d?(d>>>0)+4294967296*h:NaN,w>>>=0,d=new Date(1e3*d),R[w>>>2>>>0]=d.getUTCSeconds(),R[w+4>>>2>>>0]=d.getUTCMinutes(),R[w+8>>>2>>>0]=d.getUTCHours(),R[w+12>>>2>>>0]=d.getUTCDate(),R[w+16>>>2>>>0]=d.getUTCMonth(),R[w+20>>>2>>>0]=d.getUTCFullYear()-1900,R[w+24>>>2>>>0]=d.getUTCDay(),R[w+28>>>2>>>0]=(d.getTime()-Date.UTC(d.getUTCFullYear(),0,1,0,0,0,0))/864e5|0},r:function(d,h,w){d=h+2097152>>>0<4194305-!!d?(d>>>0)+4294967296*h:NaN,w>>>=0,d=new Date(1e3*d),R[w>>>2>>>0]=d.getSeconds(),R[w+4>>>2>>>0]=d.getMinutes(),R[w+8>>>2>>>0]=d.getHours(),R[w+12>>>2>>>0]=d.getDate(),R[w+16>>>2>>>0]=d.getMonth(),R[w+20>>>2>>>0]=d.getFullYear()-1900,R[w+24>>>2>>>0]=d.getDay(),R[w+28>>>2>>>0]=(St(d.getFullYear())?$t:Nt)[d.getMonth()]+d.getDate()-1|0,R[w+36>>>2>>>0]=-(60*d.getTimezoneOffset()),h=new Date(d.getFullYear(),6,1).getTimezoneOffset();var S=new Date(d.getFullYear(),0,1).getTimezoneOffset();R[w+32>>>2>>>0]=(h!=S&&d.getTimezoneOffset()==Math.min(S,h))|0},s:function(d){d>>>=0;var h=new Date(R[d+20>>>2>>>0]+1900,R[d+16>>>2>>>0],R[d+12>>>2>>>0],R[d+8>>>2>>>0],R[d+4>>>2>>>0],R[d>>>2>>>0],0),w=R[d+32>>>2>>>0],S=h.getTimezoneOffset(),P=new Date(h.getFullYear(),6,1).getTimezoneOffset(),N=new Date(h.getFullYear(),0,1).getTimezoneOffset(),K=Math.min(N,P);return 0>w?R[d+32>>>2>>>0]=+(P!=N&&K==S):0>>2>>>0]=h.getDay(),R[d+28>>>2>>>0]=(St(h.getFullYear())?$t:Nt)[h.getMonth()]+h.getDate()-1|0,R[d>>>2>>>0]=h.getSeconds(),R[d+4>>>2>>>0]=h.getMinutes(),R[d+8>>>2>>>0]=h.getHours(),R[d+12>>>2>>>0]=h.getDate(),R[d+16>>>2>>>0]=h.getMonth(),R[d+20>>>2>>>0]=h.getYear(),d=h.getTime(),isNaN(d)?(R[rr()>>>2>>>0]=61,d=-1):d/=1e3,or((Le=d,1<=+Math.abs(Le)?0>>0:~~+Math.ceil((Le-+(~~Le>>>0))/4294967296)>>>0:0)),d>>>0},o:function(){return-52},p:function(){},w:function(d,h,w){function S(ae){return(ae=ae.toTimeString().match(/\\(([A-Za-z ]+)\\)$/))?ae[1]:"GMT"}w>>>=0;var P=new Date().getFullYear(),N=new Date(P,0,1),K=new Date(P,6,1);P=N.getTimezoneOffset();var X=K.getTimezoneOffset();L[d>>>0>>>2>>>0]=60*Math.max(P,X),R[h>>>0>>>2>>>0]=+(P!=X),d=S(N),h=S(K),d=Wt(d),h=Wt(h),X>>2>>>0]=d,L[w+4>>>2>>>0]=h):(L[w>>>2>>>0]=h,L[w+4>>>2>>>0]=d)},e:()=>{se("")},b:function(d,h,w){return d>>>=0,h=Qt(h>>>0,w>>>0),wt[d].apply(null,h)},i:function(d,h,w){return d>>>=0,h=Qt(h>>>0,w>>>0),wt[d].apply(null,h)},h:()=>Date.now(),x:function(){return 4294901760},c:()=>performance.now(),K:function(d,h,w){return h>>>=0,H.copyWithin(d>>>0>>>0,h>>>0,h+(w>>>0)>>>0)},u:function(d){d>>>=0;var h=H.length;if(4294901760=w;w*=2){var S=h*(1+.2/w);S=Math.min(S,d+100663296);var P=Math;S=Math.max(d,S);e:{P=(P.min.call(P,4294901760,S+(65536-S%65536)%65536)-T.buffer.byteLength+65535)/65536;try{T.grow(P),we();var N=1;break e}catch{}N=void 0}if(N)return!0}return!1},E:function(d,h){d>>>=0,h>>>=0;var w=0;return Xt().forEach((S,P)=>{var N=h+w;for(P=L[d+4*P>>>2>>>0]=N,N=0;N>>0>>>0]=S.charCodeAt(N);V[P>>>0>>>0]=0,w+=S.length+1}),0},F:function(d,h){d>>>=0,h>>>=0;var w=Xt();L[d>>>2>>>0]=w.length;var S=0;return w.forEach(P=>S+=P.length+1),L[h>>>2>>>0]=S,0},f:()=>52,k:function(){return 52},t:function(){return 70},j:function(d,h,w,S){h>>>=0,w>>>=0,S>>>=0;for(var P=0,N=0;N>>2>>>0],X=L[h+4>>>2>>>0];h+=8;for(var ae=0;ae>>0],fe=Ke[d];de===0||de===10?((d===1?v:A)(xt(fe,0)),fe.length=0):fe.push(de)}P+=X}return L[S>>>2>>>0]=P,0},v:Ht,d:function(d,h,w,S){return Ht(d>>>0,h>>>0,w>>>0,S>>>0)}},ee=function(){function d(w){return ee=w.exports,ee=mn(),ee=Er(),T=ee.M,we(),ze.unshift(ee.N),xe--,xe==0&&(me!==null&&(clearInterval(me),me=null),ue&&(w=ue,ue=null,w())),ee}var h={a:Tr};if(xe++,r.instantiateWasm)try{return r.instantiateWasm(h,d)}catch(w){A(`Module.instantiateWasm callback failed with error: ${w}`),n(w)}return Xe(h,function(w){d(w.instance)}).catch(n),{}}();r._OrtInit=(d,h)=>(r._OrtInit=ee.O)(d,h),r._OrtGetLastError=(d,h)=>(r._OrtGetLastError=ee.P)(d,h),r._OrtCreateSessionOptions=(d,h,w,S,P,N,K,X,ae,de)=>(r._OrtCreateSessionOptions=ee.Q)(d,h,w,S,P,N,K,X,ae,de),r._OrtAppendExecutionProvider=(d,h)=>(r._OrtAppendExecutionProvider=ee.R)(d,h),r._OrtAddFreeDimensionOverride=(d,h,w)=>(r._OrtAddFreeDimensionOverride=ee.S)(d,h,w),r._OrtAddSessionConfigEntry=(d,h,w)=>(r._OrtAddSessionConfigEntry=ee.T)(d,h,w),r._OrtReleaseSessionOptions=d=>(r._OrtReleaseSessionOptions=ee.U)(d),r._OrtCreateSession=(d,h,w)=>(r._OrtCreateSession=ee.V)(d,h,w),r._OrtReleaseSession=d=>(r._OrtReleaseSession=ee.W)(d),r._OrtGetInputOutputCount=(d,h,w)=>(r._OrtGetInputOutputCount=ee.X)(d,h,w),r._OrtGetInputName=(d,h)=>(r._OrtGetInputName=ee.Y)(d,h),r._OrtGetOutputName=(d,h)=>(r._OrtGetOutputName=ee.Z)(d,h),r._OrtFree=d=>(r._OrtFree=ee._)(d),r._OrtCreateTensor=(d,h,w,S,P,N)=>(r._OrtCreateTensor=ee.$)(d,h,w,S,P,N),r._OrtGetTensorData=(d,h,w,S,P)=>(r._OrtGetTensorData=ee.aa)(d,h,w,S,P),r._OrtReleaseTensor=d=>(r._OrtReleaseTensor=ee.ba)(d),r._OrtCreateRunOptions=(d,h,w,S)=>(r._OrtCreateRunOptions=ee.ca)(d,h,w,S),r._OrtAddRunConfigEntry=(d,h,w)=>(r._OrtAddRunConfigEntry=ee.da)(d,h,w),r._OrtReleaseRunOptions=d=>(r._OrtReleaseRunOptions=ee.ea)(d),r._OrtCreateBinding=d=>(r._OrtCreateBinding=ee.fa)(d),r._OrtBindInput=(d,h,w)=>(r._OrtBindInput=ee.ga)(d,h,w),r._OrtBindOutput=(d,h,w,S)=>(r._OrtBindOutput=ee.ha)(d,h,w,S),r._OrtClearBoundOutputs=d=>(r._OrtClearBoundOutputs=ee.ia)(d),r._OrtReleaseBinding=d=>(r._OrtReleaseBinding=ee.ja)(d),r._OrtRunWithBinding=(d,h,w,S,P)=>(r._OrtRunWithBinding=ee.ka)(d,h,w,S,P),r._OrtRun=(d,h,w,S,P,N,K,X)=>(r._OrtRun=ee.la)(d,h,w,S,P,N,K,X),r._OrtEndProfiling=d=>(r._OrtEndProfiling=ee.ma)(d),r._JsepOutput=(d,h,w)=>(r._JsepOutput=ee.na)(d,h,w),r._JsepGetNodeName=d=>(r._JsepGetNodeName=ee.oa)(d);var rr=()=>(rr=ee.pa)(),Lt=r._malloc=d=>(Lt=r._malloc=ee.qa)(d),nr=r._free=d=>(nr=r._free=ee.ra)(d),or=d=>(or=ee.ta)(d),ar=()=>(ar=ee.ua)(),ir=d=>(ir=ee.va)(d),sr=d=>(sr=ee.wa)(d),ur=d=>(ur=ee.ya)(d),dr=()=>(dr=ee.za)(),lr=d=>(lr=ee.Aa)(d),cr=()=>(cr=ee.Ba)();r.___start_em_js=944151,r.___stop_em_js=944312;function Er(){var d=ee;d=Object.assign({},d);var h=S=>()=>S()>>>0,w=S=>P=>S(P)>>>0;return d.pa=h(d.pa),d.qa=w(d.qa),d.ua=h(d.ua),d.wa=w(d.wa),d}r.stackAlloc=sr,r.stackSave=ar,r.stackRestore=ir,r.UTF8ToString=qe,r.stringToUTF8=(d,h,w)=>Vt(d,H,h,w),r.lengthBytesUTF8=Zt;var It;ue=function d(){It||Bt(),It||(ue=d)};function Bt(){if(!(0Oo)});var Bo=pr(()=>{});var Do=pr(()=>{});var Mo={};Mr(Mo,{cpus:()=>gd});var gd,zo=q(()=>{gd=void 0});var No=pr((Vo,In)=>{"use strict";var Uo=(()=>{var e=typeof document<"u"&&document.currentScript?document.currentScript.src:void 0;return typeof __filename<"u"&&(e=e||__filename),function(t={}){function r(){return ne.buffer!=me.buffer&&G(),me}function o(){return ne.buffer!=me.buffer&&G(),ue}function n(){return ne.buffer!=me.buffer&&G(),se}function s(){return ne.buffer!=me.buffer&&G(),he}function u(){return ne.buffer!=me.buffer&&G(),Ae}function l(){return ne.buffer!=me.buffer&&G(),He}var a=t,p,m;a.ready=new Promise((i,c)=>{p=i,m=c}),a.mountExternalData=(i,c)=>{(a.cb||(a.cb=new Map)).set(i,c)},a.unmountExternalData=()=>{delete a.cb};let f=()=>{let i=(g,x,E)=>(...k)=>{let W=pt,Z=x?.();k=g(...k);let ge=x?.();return Z!==ge&&(g=ge,E(Z),x=E=null),pt!=W?td():k},c=g=>async(...x)=>{try{if(a.bb)throw Error("Session already started");let E=a.bb={Gb:x[0],errors:[]},k=await g(...x);if(a.bb!==E)throw Error("Session mismatch");a.kb?.flush();let W=E.errors;if(0ge),0a._OrtCreateSession,g=>a._OrtCreateSession=g),a._OrtRun=c(i(a._OrtRun,()=>a._OrtRun,g=>a._OrtRun=g)),a._OrtRunWithBinding=c(i(a._OrtRunWithBinding,()=>a._OrtRunWithBinding,g=>a._OrtRunWithBinding=g)),a._OrtBindInput=i(a._OrtBindInput,()=>a._OrtBindInput,g=>a._OrtBindInput=g),f=void 0};a.jsepInit=(i,c)=>{if(f?.(),i==="webgpu"){[a.kb,a.xb,a.Bb,a.lb,a.Ab,a.Ea,a.Cb,a.Eb,a.yb,a.zb,a.Db]=c;let g=a.kb;a.jsepRegisterBuffer=(x,E,k,W)=>g.registerBuffer(x,E,k,W),a.jsepGetBuffer=x=>g.getBuffer(x),a.jsepCreateDownloader=(x,E,k)=>g.createDownloader(x,E,k),a.jsepOnReleaseSession=x=>{g.onReleaseSession(x)},a.jsepOnRunStart=x=>g.onRunStart(x)}};var b=Object.assign({},a),_="./this.program",y=(i,c)=>{throw c},$=typeof window=="object",I=typeof importScripts=="function",C=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string",v=a.ENVIRONMENT_IS_PTHREAD||!1,A="";function T(i){return a.locateFile?a.locateFile(i,A):A+i}var D,U,V;if(C){var H=(_n(),Ft($n)),R=(Sn(),Ft(xn));A=I?R.dirname(A)+"/":__dirname+"/",D=(c,g)=>(c=vt(c)?new URL(c):R.normalize(c),H.readFileSync(c,g?void 0:"utf8")),V=c=>(c=D(c,!0),c.buffer||(c=new Uint8Array(c)),c),U=(c,g,x,E=!0)=>{c=vt(c)?new URL(c):R.normalize(c),H.readFile(c,E?void 0:"utf8",(k,W)=>{k?x(k):g(E?W.buffer:W)})},!a.thisProgram&&1{throw process.exitCode=c,g},a.inspect=()=>"[Emscripten Module object]";let i;try{i=Bo()}catch(c){throw console.error(\'The "worker_threads" module is not supported in this node.js build - perhaps a newer version is needed?\'),c}global.Worker=i.Worker}else($||I)&&(I?A=self.location.href:typeof document<"u"&&document.currentScript&&(A=document.currentScript.src),typeof e<"u"&&e&&(A=e),A.indexOf("blob:")!==0?A=A.substr(0,A.replace(/[?#].*/,"").lastIndexOf("/")+1):A="",C||(D=i=>{var c=new XMLHttpRequest;return c.open("GET",i,!1),c.send(null),c.responseText},I&&(V=i=>{var c=new XMLHttpRequest;return c.open("GET",i,!1),c.responseType="arraybuffer",c.send(null),new Uint8Array(c.response)}),U=(i,c,g)=>{var x=new XMLHttpRequest;x.open("GET",i,!0),x.responseType="arraybuffer",x.onload=()=>{x.status==200||x.status==0&&x.response?c(x.response):g()},x.onerror=g,x.send(null)}));C&&typeof performance>"u"&&(global.performance=Do().performance);var L=console.log.bind(console),pe=console.error.bind(console);C&&(L=(...i)=>H.writeSync(1,i.join(" ")+`\n`),pe=(...i)=>H.writeSync(2,i.join(" ")+`\n`));var Ie=L,we=pe;Object.assign(a,b),b=null,typeof WebAssembly!="object"&&Je("no native wasm support detected");var ne,ze,Q=!1,xe,me,ue,se,he,Ae,He;function G(){var i=ne.buffer;a.HEAP8=me=new Int8Array(i),a.HEAP16=new Int16Array(i),a.HEAPU8=ue=new Uint8Array(i),a.HEAPU16=new Uint16Array(i),a.HEAP32=se=new Int32Array(i),a.HEAPU32=he=new Uint32Array(i),a.HEAPF32=Ae=new Float32Array(i),a.HEAPF64=He=new Float64Array(i)}var J=16777216;if(v)ne=a.wasmMemory;else if(a.wasmMemory)ne=a.wasmMemory;else if(ne=new WebAssembly.Memory({initial:J/65536,maximum:65536,shared:!0}),!(ne.buffer instanceof SharedArrayBuffer))throw we("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"),C&&we("(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and/or recent version)"),Error("bad memory");G(),J=ne.buffer.byteLength;var Se=[],Qe=[],Xe=[],Le=0,wt=null,Ne=null;function Fe(){if(Le--,Le==0&&(wt!==null&&(clearInterval(wt),wt=null),Ne)){var i=Ne;Ne=null,i()}}function Je(i){throw i="Aborted("+i+")",we(i),Q=!0,xe=1,i=new WebAssembly.RuntimeError(i+". Build with -sASSERTIONS for more info."),m(i),i}var kt=i=>i.startsWith("data:application/octet-stream;base64,"),vt=i=>i.startsWith("file://"),xt;xt="ort-wasm-simd-threaded.wasm",kt(xt)||(xt=T(xt));function qe(i){if(V)return V(i);throw"both async and sync fetching of the wasm failed"}function Zt(i){if($||I){if(typeof fetch=="function"&&!vt(i))return fetch(i,{credentials:"same-origin"}).then(c=>{if(!c.ok)throw"failed to load wasm binary file at \'"+i+"\'";return c.arrayBuffer()}).catch(()=>qe(i));if(U)return new Promise((c,g)=>{U(i,x=>c(new Uint8Array(x)),g)})}return Promise.resolve().then(()=>qe(i))}function Vt(i,c,g){return Zt(i).then(x=>WebAssembly.instantiate(x,c)).then(x=>x).then(g,x=>{we(`failed to asynchronously prepare wasm: ${x}`),Je(x)})}function St(i,c){var g=xt;return typeof WebAssembly.instantiateStreaming!="function"||kt(g)||vt(g)||C||typeof fetch!="function"?Vt(g,i,c):fetch(g,{credentials:"same-origin"}).then(x=>WebAssembly.instantiateStreaming(x,i).then(c,function(E){return we(`wasm streaming compile failed: ${E}`),we("falling back to ArrayBuffer instantiation"),Vt(g,i,c)}))}var $t,Nt={933148:(i,c,g,x)=>{if(typeof a>"u"||!a.cb)return 1;if(i=Ke(i>>>0),i.startsWith("./")&&(i=i.substring(2)),i=a.cb.get(i),!i)return 2;if(c>>>=0,g>>>=0,x>>>=0,c+g>i.byteLength)return 3;try{return o().set(i.subarray(c,c+g),x>>>0),0}catch{return 4}},933649:()=>{a.yb()},933680:()=>{a.zb()},933709:()=>{a.Db()},933734:i=>a.xb(i),933767:i=>a.Bb(i),933799:(i,c,g)=>{a.lb(i,c,g,!0)},933838:(i,c,g)=>{a.lb(i,c,g)},933871:i=>{a.Ea("Abs",i,void 0)},933922:i=>{a.Ea("Neg",i,void 0)},933973:i=>{a.Ea("Floor",i,void 0)},934026:i=>{a.Ea("Ceil",i,void 0)},934078:i=>{a.Ea("Reciprocal",i,void 0)},934136:i=>{a.Ea("Sqrt",i,void 0)},934188:i=>{a.Ea("Exp",i,void 0)},934239:i=>{a.Ea("Erf",i,void 0)},934290:i=>{a.Ea("Sigmoid",i,void 0)},934345:(i,c,g)=>{a.Ea("HardSigmoid",i,{alpha:c,beta:g})},934424:i=>{a.Ea("Log",i,void 0)},934475:i=>{a.Ea("Sin",i,void 0)},934526:i=>{a.Ea("Cos",i,void 0)},934577:i=>{a.Ea("Tan",i,void 0)},934628:i=>{a.Ea("Asin",i,void 0)},934680:i=>{a.Ea("Acos",i,void 0)},934732:i=>{a.Ea("Atan",i,void 0)},934784:i=>{a.Ea("Sinh",i,void 0)},934836:i=>{a.Ea("Cosh",i,void 0)},934888:i=>{a.Ea("Asinh",i,void 0)},934941:i=>{a.Ea("Acosh",i,void 0)},934994:i=>{a.Ea("Atanh",i,void 0)},935047:i=>{a.Ea("Tanh",i,void 0)},935099:i=>{a.Ea("Not",i,void 0)},935150:(i,c,g)=>{a.Ea("Clip",i,{min:c,max:g})},935219:i=>{a.Ea("Clip",i,void 0)},935271:(i,c)=>{a.Ea("Elu",i,{alpha:c})},935329:i=>{a.Ea("Relu",i,void 0)},935381:(i,c)=>{a.Ea("LeakyRelu",i,{alpha:c})},935445:(i,c)=>{a.Ea("ThresholdedRelu",i,{alpha:c})},935515:(i,c)=>{a.Ea("Cast",i,{to:c})},935573:i=>{a.Ea("Add",i,void 0)},935624:i=>{a.Ea("Sub",i,void 0)},935675:i=>{a.Ea("Mul",i,void 0)},935726:i=>{a.Ea("Div",i,void 0)},935777:i=>{a.Ea("Pow",i,void 0)},935828:i=>{a.Ea("Equal",i,void 0)},935881:i=>{a.Ea("Greater",i,void 0)},935936:i=>{a.Ea("GreaterOrEqual",i,void 0)},935998:i=>{a.Ea("Less",i,void 0)},936050:i=>{a.Ea("LessOrEqual",i,void 0)},936109:(i,c,g,x,E)=>{a.Ea("ReduceMean",i,{keepDims:!!c,noopWithEmptyAxes:!!g,axes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},936268:(i,c,g,x,E)=>{a.Ea("ReduceMax",i,{keepDims:!!c,noopWithEmptyAxes:!!g,axes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},936426:(i,c,g,x,E)=>{a.Ea("ReduceMin",i,{keepDims:!!c,noopWithEmptyAxes:!!g,axes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},936584:(i,c,g,x,E)=>{a.Ea("ReduceProd",i,{keepDims:!!c,noopWithEmptyAxes:!!g,axes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},936743:(i,c,g,x,E)=>{a.Ea("ReduceSum",i,{keepDims:!!c,noopWithEmptyAxes:!!g,axes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},936901:(i,c,g,x,E)=>{a.Ea("ReduceL1",i,{keepDims:!!c,noopWithEmptyAxes:!!g,axes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},937058:(i,c,g,x,E)=>{a.Ea("ReduceL2",i,{keepDims:!!c,noopWithEmptyAxes:!!g,axes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},937215:(i,c,g,x,E)=>{a.Ea("ReduceLogSum",i,{keepDims:!!c,noopWithEmptyAxes:!!g,axes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},937376:(i,c,g,x,E)=>{a.Ea("ReduceSumSquare",i,{keepDims:!!c,noopWithEmptyAxes:!!g,axes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},937540:(i,c,g,x,E)=>{a.Ea("ReduceLogSumExp",i,{keepDims:!!c,noopWithEmptyAxes:!!g,axes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},937704:i=>{a.Ea("Where",i,void 0)},937757:(i,c,g)=>{a.Ea("Transpose",i,{perm:c?Array.from(n().subarray(c>>>0,g>>>0)):[]})},937865:(i,c,g,x,E,k,W,Z,ge,le,ve,Ue,Ve,B,ce)=>{a.Ea("ConvTranspose",i,{format:ge?"NHWC":"NCHW",autoPad:c,dilations:[g],group:x,kernel_shape:[E],pads:[k,W],strides:[Z],wIsConst:()=>!!r()[le>>>0],outputPadding:ve?Array.from(n().subarray(ve>>>0,Ue>>>0)):[],outputShape:Ve?Array.from(n().subarray(Ve>>>0,B>>>0)):[],activation:Ke(ce)})},938267:(i,c,g,x,E,k,W,Z,ge,le,ve,Ue,Ve,B)=>{a.Ea("ConvTranspose",i,{format:Z?"NHWC":"NCHW",autoPad:c,dilations:Array.from(n().subarray(g>>>0,(g>>>0)+2>>>0)),group:x,kernelShape:Array.from(n().subarray(E>>>0,(E>>>0)+2>>>0)),pads:Array.from(n().subarray(k>>>0,(k>>>0)+4>>>0)),strides:Array.from(n().subarray(W>>>0,(W>>>0)+2>>>0)),wIsConst:()=>!!r()[ge>>>0],outputPadding:le?Array.from(n().subarray(le>>>0,ve>>>0)):[],outputShape:Ue?Array.from(n().subarray(Ue>>>0,Ve>>>0)):[],activation:Ke(B)})},938832:(i,c,g,x,E,k,W,Z,ge,le,ve,Ue,Ve,B,ce)=>{a.Ea("ConvTranspose",i,{format:ge?"NHWC":"NCHW",autoPad:c,dilations:[g],group:x,kernel_shape:[E],pads:[k,W],strides:[Z],wIsConst:()=>!!r()[le>>>0],outputPadding:ve?Array.from(n().subarray(ve>>>0,Ue>>>0)):[],outputShape:Ve?Array.from(n().subarray(Ve>>>0,B>>>0)):[],activation:Ke(ce)})},939234:(i,c,g,x,E,k,W,Z,ge,le,ve,Ue,Ve,B)=>{a.Ea("ConvTranspose",i,{format:Z?"NHWC":"NCHW",autoPad:c,dilations:Array.from(n().subarray(g>>>0,(g>>>0)+2>>>0)),group:x,kernelShape:Array.from(n().subarray(E>>>0,(E>>>0)+2>>>0)),pads:Array.from(n().subarray(k>>>0,(k>>>0)+4>>>0)),strides:Array.from(n().subarray(W>>>0,(W>>>0)+2>>>0)),wIsConst:()=>!!r()[ge>>>0],outputPadding:le?Array.from(n().subarray(le>>>0,ve>>>0)):[],outputShape:Ue?Array.from(n().subarray(Ue>>>0,Ve>>>0)):[],activation:Ke(B)})},939799:(i,c)=>{a.Ea("GlobalAveragePool",i,{format:c?"NHWC":"NCHW"})},939890:(i,c,g,x,E,k,W,Z,ge,le,ve,Ue,Ve,B,ce,Te)=>{a.Ea("AveragePool",i,{format:Te?"NHWC":"NCHW",auto_pad:c,ceil_mode:g,count_include_pad:x,storage_order:E,dilations:[k,W],kernel_shape:[Z,ge],pads:[le,ve,Ue,Ve],strides:[B,ce]})},940174:(i,c)=>{a.Ea("GlobalAveragePool",i,{format:c?"NHWC":"NCHW"})},940265:(i,c,g,x,E,k,W,Z,ge,le,ve,Ue,Ve,B,ce,Te)=>{a.Ea("AveragePool",i,{format:Te?"NHWC":"NCHW",auto_pad:c,ceil_mode:g,count_include_pad:x,storage_order:E,dilations:[k,W],kernel_shape:[Z,ge],pads:[le,ve,Ue,Ve],strides:[B,ce]})},940549:(i,c)=>{a.Ea("GlobalMaxPool",i,{format:c?"NHWC":"NCHW"})},940636:(i,c,g,x,E,k,W,Z,ge,le,ve,Ue,Ve,B,ce,Te)=>{a.Ea("MaxPool",i,{format:Te?"NHWC":"NCHW",auto_pad:c,ceil_mode:g,count_include_pad:x,storage_order:E,dilations:[k,W],kernel_shape:[Z,ge],pads:[le,ve,Ue,Ve],strides:[B,ce]})},940916:(i,c)=>{a.Ea("GlobalMaxPool",i,{format:c?"NHWC":"NCHW"})},941003:(i,c,g,x,E,k,W,Z,ge,le,ve,Ue,Ve,B,ce,Te)=>{a.Ea("MaxPool",i,{format:Te?"NHWC":"NCHW",auto_pad:c,ceil_mode:g,count_include_pad:x,storage_order:E,dilations:[k,W],kernel_shape:[Z,ge],pads:[le,ve,Ue,Ve],strides:[B,ce]})},941283:(i,c,g,x,E)=>{a.Ea("Gemm",i,{alpha:c,beta:g,transA:x,transB:E})},941387:i=>{a.Ea("MatMul",i,void 0)},941441:(i,c,g,x)=>{a.Ea("ArgMax",i,{keepDims:!!c,selectLastIndex:!!g,axis:x})},941549:(i,c,g,x)=>{a.Ea("ArgMin",i,{keepDims:!!c,selectLastIndex:!!g,axis:x})},941657:(i,c)=>{a.Ea("Softmax",i,{axis:c})},941720:(i,c)=>{a.Ea("Concat",i,{axis:c})},941780:(i,c,g,x,E)=>{a.Ea("Split",i,{axis:c,numOutputs:g,splitSizes:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},941920:i=>{a.Ea("Expand",i,void 0)},941974:(i,c)=>{a.Ea("Gather",i,{axis:Number(c)})},942045:(i,c)=>{a.Ea("GatherElements",i,{axis:Number(c)})},942124:(i,c,g,x,E,k,W,Z,ge,le,ve)=>{a.Ea("Resize",i,{antialias:c,axes:g?Array.from(n().subarray(g>>>0,x>>>0)):[],coordinateTransformMode:Ke(E),cubicCoeffA:k,excludeOutside:W,extrapolationValue:Z,keepAspectRatioPolicy:Ke(ge),mode:Ke(le),nearestMode:Ke(ve)})},942470:(i,c,g,x,E,k,W)=>{a.Ea("Slice",i,{starts:c?Array.from(n().subarray(c>>>0,g>>>0)):[],ends:x?Array.from(n().subarray(x>>>0,E>>>0)):[],axes:k?Array.from(n().subarray(k>>>0,W>>>0)):[]})},942686:i=>{a.Ea("Tile",i,void 0)},942738:(i,c,g)=>{a.Ea("LayerNormalization",i,{axis:Number(c),epsilon:Number(g)})},942845:(i,c,g)=>{a.Ea("InstanceNormalization",i,{epsilon:c,format:g?"NHWC":"NCHW"})},942959:(i,c,g)=>{a.Ea("InstanceNormalization",i,{epsilon:c,format:g?"NHWC":"NCHW"})},943073:i=>{a.Ea("Range",i,void 0)},943126:(i,c)=>{a.Ea("Einsum",i,{equation:Ke(c)})},943207:(i,c,g,x,E)=>{a.Ea("Pad",i,{mode:c,value:g,pads:x?Array.from(n().subarray(x>>>0,E>>>0)):[]})},943334:(i,c,g,x,E,k)=>{a.Ea("BatchNormalization",i,{epsilon:c,momentum:g,spatial:!!E,trainingMode:!!x,format:k?"NHWC":"NCHW"})},943503:(i,c,g,x,E,k)=>{a.Ea("BatchNormalization",i,{epsilon:c,momentum:g,spatial:!!E,trainingMode:!!x,format:k?"NHWC":"NCHW"})},943672:(i,c,g)=>{a.Ea("CumSum",i,{exclusive:Number(c),reverse:Number(g)})},943769:(i,c,g,x,E,k,W,Z,ge)=>{a.Ea("Attention",i,{numHeads:c,isUnidirectional:g,maskFilterValue:x,scale:E,doRotary:k,qkvHiddenSizes:W?Array.from(n().subarray(Number(Z)>>>0,Number(Z)+W>>>0)):[],pastPresentShareBuffer:!!ge})},944041:i=>{a.Ea("BiasAdd",i,void 0)},944096:i=>{a.Ea("BiasSplitGelu",i,void 0)},944157:i=>{a.Ea("FastGelu",i,void 0)},944213:(i,c,g,x,E,k,W,Z,ge,le,ve,Ue,Ve)=>{a.Ea("Conv",i,{format:ge?"NHWC":"NCHW",auto_pad:c,dilations:[g],group:x,kernel_shape:[E],pads:k?Array.from(n().subarray(k>>>0,W>>>0)):[],strides:[Z],w_is_const:()=>!!r()[le>>>0],activation:Ke(ve),activation_params:Ue?Array.from(u().subarray(Ue>>>0,Ve>>>0)):[]})},944583:(i,c,g,x,E,k,W,Z,ge,le,ve,Ue,Ve,B,ce,Te)=>{a.Ea("Conv",i,{format:Ue?"NHWC":"NCHW",auto_pad:c,dilations:[g,x],group:E,kernel_shape:[k,W],pads:Z?Array.from(n().subarray(Z>>>0,ge>>>0)):[],strides:[le,ve],w_is_const:()=>!!r()[Ve>>>0],activation:Ke(B),activation_params:ce?Array.from(u().subarray(ce>>>0,Te>>>0)):[]})},944974:i=>{a.Ea("Gelu",i,void 0)},945026:(i,c,g,x,E,k)=>{a.Ea("MatMulNBits",i,{k:c,n:g,accuracyLevel:x,bits:E,blockSize:k})},945153:(i,c,g,x,E,k)=>{a.Ea("MultiHeadAttention",i,{numHeads:c,isUnidirectional:g,maskFilterValue:x,scale:E,doRotary:k})},945312:(i,c)=>{a.Ea("SkipLayerNormalization",i,{epsilon:c})},945393:i=>{a.Cb(i)},945427:(i,c)=>a.Eb(i,c,a.bb.Gb,a.bb.errors)};function Wt(i){this.name="ExitStatus",this.message=`Program terminated with exit(${i})`,this.status=i}var Rt=i=>{i.terminate(),i.onmessage=()=>{}},Qt=i=>{te.Ya.length==0&&(it(),te.mb(te.Ya[0]));var c=te.Ya.pop();if(!c)return 6;te.Za.push(c),te.Qa[i.Xa]=c,c.Xa=i.Xa;var g={cmd:"run",start_routine:i.Hb,arg:i.ub,pthread_ptr:i.Xa};return C&&c.unref(),c.postMessage(g,i.Nb),0},at=0,Xt=typeof TextDecoder<"u"?new TextDecoder("utf8"):void 0,Gt=(i,c,g)=>{c>>>=0;var x=c+g;for(g=c;i[g]&&!(g>=x);)++g;if(16E?x+=String.fromCharCode(E):(E-=65536,x+=String.fromCharCode(55296|E>>10,56320|E&1023))}}else x+=String.fromCharCode(E)}return x},Ke=(i,c)=>(i>>>=0)?Gt(o(),i,c):"",vr=i=>{var c=bn();return i=i(),Br(c),i};function ke(i,c){var g=arguments.length-2,x=arguments;return vr(()=>{for(var E=wn(8*g),k=E>>>3,W=0;W>>0]=Z}return wo(i,g,E,c)})}function $r(i){if(v)return ke(0,1,i);xe=i,0{if(xe=i,v)throw Jt(i),"unwind";$r(i)},Ct=i=>{i instanceof Wt||i=="unwind"||y(1,i)};function mn(){for(var i=a.numThreads;i--;)it();Se.unshift(()=>{Le++,nt(()=>Fe())})}function it(){var i=T("ort-wasm-simd-threaded.worker.js");i=new Worker(i),te.Ya.push(i)}function nt(i){v?i():Promise.all(te.Ya.map(te.mb)).then(i)}var te={Ya:[],Za:[],qb:[],Qa:{},hb(){v?(te.receiveObjectTransfer=te.Fb,te.threadInitTLS=te.pb,te.setExitStatus=te.ob):mn()},ob:i=>xe=i,Qb:["$terminateWorker"],Ib:()=>{for(var i of te.Za)Rt(i);for(i of te.Ya)Rt(i);te.Ya=[],te.Za=[],te.Qa=[]},nb:i=>{var c=i.Xa;delete te.Qa[c],te.Ya.push(i),te.Za.splice(te.Za.indexOf(i),1),i.Xa=0,gn(c)},Fb(){},pb(){te.qb.forEach(i=>i())},mb:i=>new Promise(c=>{i.onmessage=k=>{k=k.data;var W=k.cmd;if(k.targetThread&&k.targetThread!=Rr()){var Z=te.Qa[k.targetThread];Z?Z.postMessage(k,k.transferList):we(`Internal error! Worker sent a message "${W}" to target pthread ${k.targetThread}, but that thread no longer exists!`)}else W==="checkMailbox"?Bt():W==="spawnThread"?Qt(k):W==="cleanupThread"?te.nb(te.Qa[k.thread]):W==="killThread"?(k=k.thread,W=te.Qa[k],delete te.Qa[k],Rt(W),gn(k),te.Za.splice(te.Za.indexOf(W),1),W.Xa=0):W==="cancelThread"?te.Qa[k.thread].postMessage({cmd:"cancel"}):W==="loaded"?(i.loaded=!0,C&&!i.Xa&&i.unref(),c(i)):W==="alert"?alert(`Thread ${k.threadId}: ${k.text}`):k.target==="setimmediate"?i.postMessage(k):W==="callHandler"?a[k.handler](...k.args):W&&we(`worker sent an unknown command ${W}`)},i.onerror=k=>{throw we(`worker sent an error! ${k.filename}:${k.lineno}: ${k.message}`),k},C&&(i.on("message",k=>i.onmessage({data:k})),i.on("error",k=>i.onerror(k)));var g=[],x=["onExit"],E;for(E of x)a.hasOwnProperty(E)&&g.push(E);i.postMessage({cmd:"load",handlers:g,urlOrBlob:a.mainScriptUrlOrBlob||e,wasmMemory:ne,wasmModule:ze})})};a.PThread=te;var _t=i=>{for(;0{var i=Rr(),c=s()[i+52>>>2>>>0];i=s()[i+56>>>2>>>0],_o(c,c-i),Br(c)};function Jt(i){if(v)return ke(1,0,i);Ht(i)}a.invokeEntryPoint=(i,c)=>{i=xo.apply(null,[i,c]),0>>2>>>0]=c},this.sb=function(c){s()[this.gb+8>>>2>>>0]=c},this.hb=function(c,g){this.rb(),this.tb(c),this.sb(g)},this.rb=function(){s()[this.gb+16>>>2>>>0]=0}}var xr=0,er=0;function Sr(i,c,g,x){return v?ke(2,1,i,c,g,x):Cr(i,c,g,x)}function Cr(i,c,g,x){if(i>>>=0,c>>>=0,g>>>=0,x>>>=0,typeof SharedArrayBuffer>"u")return we("Current environment does not support SharedArrayBuffer, pthreads are not available!"),6;var E=[];return v&&E.length===0?Sr(i,c,g,x):(i={Hb:g,Xa:i,ub:x,Nb:E},v?(i.Pb="spawnThread",postMessage(i,E),0):Qt(i))}function Ir(i,c,g){return v?ke(3,1,i,c,g):0}function Ar(i,c){if(v)return ke(4,1,i,c)}var tr=i=>{for(var c=0,g=0;g=x?c++:2047>=x?c+=2:55296<=x&&57343>=x?(c+=4,++g):c+=3}return c},Tr=(i,c,g,x)=>{if(g>>>=0,!(0=W){var Z=i.charCodeAt(++k);W=65536+((W&1023)<<10)|Z&1023}if(127>=W){if(g>=x)break;c[g++>>>0]=W}else{if(2047>=W){if(g+1>=x)break;c[g++>>>0]=192|W>>6}else{if(65535>=W){if(g+2>=x)break;c[g++>>>0]=224|W>>12}else{if(g+3>=x)break;c[g++>>>0]=240|W>>18,c[g++>>>0]=128|W>>12&63}c[g++>>>0]=128|W>>6&63}c[g++>>>0]=128|W&63}}return c[g>>>0]=0,g-E},ee=(i,c,g)=>Tr(i,o(),c,g);function rr(i,c){if(v)return ke(5,1,i,c)}function Lt(i,c,g){if(v)return ke(6,1,i,c,g)}function nr(i,c,g){return v?ke(7,1,i,c,g):0}function or(i,c){if(v)return ke(8,1,i,c)}function ar(i,c,g){if(v)return ke(9,1,i,c,g)}function ir(i,c,g,x){if(v)return ke(10,1,i,c,g,x)}function sr(i,c,g,x){if(v)return ke(11,1,i,c,g,x)}function ur(i,c,g,x){if(v)return ke(12,1,i,c,g,x)}function dr(i){if(v)return ke(13,1,i)}function lr(i,c){if(v)return ke(14,1,i,c)}function cr(i,c,g){if(v)return ke(15,1,i,c,g)}var Er=()=>{if(!(0>>=0,typeof Atomics.Ob=="function"&&(Atomics.Ob(n(),i>>>2,i).value.then(Bt),i+=128,Atomics.store(n(),i>>>2,1))}a.__emscripten_thread_mailbox_await=It;var Bt=()=>{var i=Rr();if(i&&(It(i),!Q))try{vo(),Er()}catch(c){Ct(c)}};a.checkMailbox=Bt;var d=[],h=i=>i%4===0&&(i%100!==0||i%400===0),w=[0,31,60,91,121,152,182,213,244,274,305,335],S=[0,31,59,90,120,151,181,212,243,273,304,334];function P(i,c,g,x,E,k,W,Z){return v?ke(16,1,i,c,g,x,E,k,W,Z):-52}function N(i,c,g,x,E,k,W){if(v)return ke(17,1,i,c,g,x,E,k,W)}var K=i=>{var c=tr(i)+1,g=hn(c);return g&&ee(i,g,c),g},X=[],ae=(i,c)=>{X.length=0;for(var g;g=o()[i++>>>0];){var x=g!=105;x&=g!=112,c+=x&&c%8?4:0,X.push(g==112?s()[c>>>2>>>0]:g==105?n()[c>>>2>>>0]:l()[c>>>3>>>0]),c+=x?8:4}return X},de={},fe=()=>{if(!Oe){var i={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:_||"./this.program"},c;for(c in de)de[c]===void 0?delete i[c]:i[c]=de[c];var g=[];for(c in i)g.push(`${c}=${i[c]}`);Oe=g}return Oe},Oe;function Re(i,c){if(v)return ke(18,1,i,c);i>>>=0,c>>>=0;var g=0;return fe().forEach((x,E)=>{var k=c+g;for(E=s()[i+4*E>>>2>>>0]=k,k=0;k>>0>>>0]=x.charCodeAt(k);r()[E>>>0>>>0]=0,g+=x.length+1}),0}function O(i,c){if(v)return ke(19,1,i,c);i>>>=0,c>>>=0;var g=fe();s()[i>>>2>>>0]=g.length;var x=0;return g.forEach(E=>x+=E.length+1),s()[c>>>2>>>0]=x,0}function oe(i){return v?ke(20,1,i):52}function Ce(i,c,g,x){return v?ke(21,1,i,c,g,x):52}function ct(i,c,g,x,E){return v?ke(22,1,i,c,g,x,E):70}var Pr=[null,[],[]];function uo(i,c,g,x){if(v)return ke(23,1,i,c,g,x);c>>>=0,g>>>=0,x>>>=0;for(var E=0,k=0;k>>2>>>0],Z=s()[c+4>>>2>>>0];c+=8;for(var ge=0;ge>>0],ve=Pr[i];le===0||le===10?((i===1?Ie:we)(Gt(ve,0)),ve.length=0):ve.push(le)}E+=Z}return s()[x>>>2>>>0]=E,0}var lo=[31,29,31,30,31,30,31,31,30,31,30,31],co=[31,28,31,30,31,30,31,31,30,31,30,31];function Zu(i){var c=Array(tr(i)+1);return Tr(i,c,0,c.length),c}var Qu=(i,c)=>{r().set(i,c>>>0)};function po(i,c,g,x){function E(B,ce,Te){for(B=typeof B=="number"?B.toString():B||"";B.lengthEo?-1:0Dt-B.getDate())ce-=Dt-B.getDate()+1,B.setDate(1),11>Te?B.setMonth(Te+1):(B.setMonth(0),B.setFullYear(B.getFullYear()+1));else{B.setDate(B.getDate()+ce);break}}return Te=new Date(B.getFullYear()+1,0,4),ce=Z(new Date(B.getFullYear(),0,4)),Te=Z(Te),0>=W(ce,B)?0>=W(Te,B)?B.getFullYear()+1:B.getFullYear():B.getFullYear()-1}i>>>=0,c>>>=0,g>>>=0,x>>>=0;var le=s()[x+40>>>2>>>0];x={Lb:n()[x>>>2>>>0],Kb:n()[x+4>>>2>>>0],eb:n()[x+8>>>2>>>0],jb:n()[x+12>>>2>>>0],fb:n()[x+16>>>2>>>0],ab:n()[x+20>>>2>>>0],Wa:n()[x+24>>>2>>>0],$a:n()[x+28>>>2>>>0],Rb:n()[x+32>>>2>>>0],Jb:n()[x+36>>>2>>>0],Mb:le?Ke(le):""},g=Ke(g),le={"%c":"%a %b %d %H:%M:%S %Y","%D":"%m/%d/%y","%F":"%Y-%m-%d","%h":"%b","%r":"%I:%M:%S %p","%R":"%H:%M","%T":"%H:%M:%S","%x":"%m/%d/%y","%X":"%H:%M:%S","%Ec":"%c","%EC":"%C","%Ex":"%m/%d/%y","%EX":"%H:%M:%S","%Ey":"%y","%EY":"%Y","%Od":"%d","%Oe":"%e","%OH":"%H","%OI":"%I","%Om":"%m","%OM":"%M","%OS":"%S","%Ou":"%u","%OU":"%U","%OV":"%V","%Ow":"%w","%OW":"%W","%Oy":"%y"};for(var ve in le)g=g.replace(new RegExp(ve,"g"),le[ve]);var Ue="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),Ve="January February March April May June July August September October November December".split(" ");le={"%a":B=>Ue[B.Wa].substring(0,3),"%A":B=>Ue[B.Wa],"%b":B=>Ve[B.fb].substring(0,3),"%B":B=>Ve[B.fb],"%C":B=>k((B.ab+1900)/100|0,2),"%d":B=>k(B.jb,2),"%e":B=>E(B.jb,2," "),"%g":B=>ge(B).toString().substring(2),"%G":B=>ge(B),"%H":B=>k(B.eb,2),"%I":B=>(B=B.eb,B==0?B=12:12{for(var ce=0,Te=0;Te<=B.fb-1;ce+=(h(B.ab+1900)?lo:co)[Te++]);return k(B.jb+ce,3)},"%m":B=>k(B.fb+1,2),"%M":B=>k(B.Kb,2),"%n":()=>`\n`,"%p":B=>0<=B.eb&&12>B.eb?"AM":"PM","%S":B=>k(B.Lb,2),"%t":()=>" ","%u":B=>B.Wa||7,"%U":B=>k(Math.floor((B.$a+7-B.Wa)/7),2),"%V":B=>{var ce=Math.floor((B.$a+7-(B.Wa+6)%7)/7);if(2>=(B.Wa+371-B.$a-2)%7&&ce++,ce)ce==53&&(Te=(B.Wa+371-B.$a)%7,Te==4||Te==3&&h(B.ab)||(ce=1));else{ce=52;var Te=(B.Wa+7-B.$a-1)%7;(Te==4||Te==5&&h(B.ab%400-1))&&ce++}return k(ce,2)},"%w":B=>B.Wa,"%W":B=>k(Math.floor((B.$a+7-(B.Wa+6)%7)/7),2),"%y":B=>(B.ab+1900).toString().substring(2),"%Y":B=>B.ab+1900,"%z":B=>{B=B.Jb;var ce=0<=B;return B=Math.abs(B)/60,(ce?"+":"-")+("0000"+(B/60*100+B%60)).slice(-4)},"%Z":B=>B.Mb,"%%":()=>"%"},g=g.replace(/%%/g,"\\0\\0");for(ve in le)g.includes(ve)&&(g=g.replace(new RegExp(ve,"g"),le[ve](x)));return g=g.replace(/\\0\\0/g,"%"),ve=Zu(g),ve.length>c?0:(Qu(ve,i),ve.length-1)}var Or=i=>{try{i()}catch(c){Je(c)}};function Xu(){var i=Y,c={};for(let[g,x]of Object.entries(i))c[g]=typeof x=="function"?function(){kr.push(g);try{return x.apply(null,arguments)}finally{Q||(kr.pop(),pt&&At===1&&kr.length===0&&(At=0,at+=1,Or(Co),typeof Fibers<"u"&&Fibers.Sb()))}}:x;return c}var At=0,pt=null,mo=0,kr=[],fo={},ho={},Ju=0,fn=null,ed=[];function td(){return new Promise((i,c)=>{fn={resolve:i,reject:c}})}function rd(){var i=hn(65548),c=i+12;s()[i>>>2>>>0]=c,s()[i+4>>>2>>>0]=c+65536,c=kr[0];var g=fo[c];return g===void 0&&(g=Ju++,fo[c]=g,ho[g]=c),c=g,n()[i+8>>>2>>>0]=c,i}function nd(){var i=n()[pt+8>>>2>>>0];return i=Y[ho[i]],--at,i()}function od(i){if(!Q){if(At===0){var c=!1,g=!1;i((x=0)=>{if(!Q&&(mo=x,c=!0,g)){At=2,Or(()=>Io(pt)),typeof Browser<"u"&&Browser.ib.wb&&Browser.ib.resume(),x=!1;try{var E=nd()}catch(Z){E=Z,x=!0}var k=!1;if(!pt){var W=fn;W&&(fn=null,(x?W.reject:W.resolve)(E),k=!0)}if(x&&!k)throw E}}),g=!0,c||(At=1,pt=rd(),typeof Browser<"u"&&Browser.ib.wb&&Browser.ib.pause(),Or(()=>So(pt)))}else At===2?(At=0,Or(Ao),yo(pt),pt=null,ed.forEach(x=>{if(!Q)try{x(),Er()}catch(E){Ct(E)}})):Je(`invalid state: ${At}`);return mo}}function ad(i){return od(c=>{i().then(c)})}te.hb();var id=[$r,Jt,Sr,Ir,Ar,rr,Lt,nr,or,ar,ir,sr,ur,dr,lr,cr,P,N,Re,O,oe,Ce,ct,uo],sd={r:function(i,c,g){return ad(async()=>{await a.Ab(i,c,g)})},b:function(i,c,g){throw i>>>=0,new _r(i).hb(c>>>0,g>>>0),xr=i,er++,xr},N:function(i){bo(i>>>0,!I,1,!$,131072,!1),te.pb()},l:function(i){i>>>=0,v?postMessage({cmd:"cleanupThread",thread:i}):te.nb(te.Qa[i])},J:Cr,i:Ir,T:Ar,F:rr,H:Lt,U:nr,R:or,L:ar,Q:ir,p:sr,G:ur,D:dr,S:lr,E:cr,q:()=>1,B:function(i,c){i>>>=0,i==c>>>0?setTimeout(()=>Bt()):v?postMessage({targetThread:i,cmd:"checkMailbox"}):(i=te.Qa[i])&&i.postMessage({cmd:"checkMailbox"})},K:function(i,c,g,x){c>>>=0,d.length=g,x=x>>>0>>>3;for(var E=0;E>>0];return i=0>i?Nt[-i-1]:id[i],te.vb=c,c=i.apply(null,d),te.vb=0,c},M:It,W:function(i){C&&te.Qa[i>>>0].ref()},u:function(i,c,g){i=c+2097152>>>0<4194305-!!i?(i>>>0)+4294967296*c:NaN,g>>>=0,i=new Date(1e3*i),n()[g>>>2>>>0]=i.getUTCSeconds(),n()[g+4>>>2>>>0]=i.getUTCMinutes(),n()[g+8>>>2>>>0]=i.getUTCHours(),n()[g+12>>>2>>>0]=i.getUTCDate(),n()[g+16>>>2>>>0]=i.getUTCMonth(),n()[g+20>>>2>>>0]=i.getUTCFullYear()-1900,n()[g+24>>>2>>>0]=i.getUTCDay(),i=(i.getTime()-Date.UTC(i.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,n()[g+28>>>2>>>0]=i},v:function(i,c,g){i=c+2097152>>>0<4194305-!!i?(i>>>0)+4294967296*c:NaN,g>>>=0,i=new Date(1e3*i),n()[g>>>2>>>0]=i.getSeconds(),n()[g+4>>>2>>>0]=i.getMinutes(),n()[g+8>>>2>>>0]=i.getHours(),n()[g+12>>>2>>>0]=i.getDate(),n()[g+16>>>2>>>0]=i.getMonth(),n()[g+20>>>2>>>0]=i.getFullYear()-1900,n()[g+24>>>2>>>0]=i.getDay(),c=(h(i.getFullYear())?w:S)[i.getMonth()]+i.getDate()-1|0,n()[g+28>>>2>>>0]=c,n()[g+36>>>2>>>0]=-(60*i.getTimezoneOffset()),c=new Date(i.getFullYear(),6,1).getTimezoneOffset();var x=new Date(i.getFullYear(),0,1).getTimezoneOffset();i=(c!=x&&i.getTimezoneOffset()==Math.min(x,c))|0,n()[g+32>>>2>>>0]=i},w:function(i){i>>>=0;var c=new Date(n()[i+20>>>2>>>0]+1900,n()[i+16>>>2>>>0],n()[i+12>>>2>>>0],n()[i+8>>>2>>>0],n()[i+4>>>2>>>0],n()[i>>>2>>>0],0),g=n()[i+32>>>2>>>0],x=c.getTimezoneOffset(),E=new Date(c.getFullYear(),6,1).getTimezoneOffset(),k=new Date(c.getFullYear(),0,1).getTimezoneOffset(),W=Math.min(k,E);return 0>g?n()[i+32>>>2>>>0]=+(E!=k&&W==x):0>>2>>>0]=c.getDay(),g=(h(c.getFullYear())?w:S)[c.getMonth()]+c.getDate()-1|0,n()[i+28>>>2>>>0]=g,n()[i>>>2>>>0]=c.getSeconds(),n()[i+4>>>2>>>0]=c.getMinutes(),n()[i+8>>>2>>>0]=c.getHours(),n()[i+12>>>2>>>0]=c.getDate(),n()[i+16>>>2>>>0]=c.getMonth(),n()[i+20>>>2>>>0]=c.getYear(),i=c.getTime(),isNaN(i)?(n()[go()>>>2>>>0]=61,i=-1):i/=1e3,$o(($t=i,1<=+Math.abs($t)?0<$t?+Math.floor($t/4294967296)>>>0:~~+Math.ceil(($t-+(~~$t>>>0))/4294967296)>>>0:0)),i>>>0},s:P,t:N,A:function(i,c,g){function x(le){return(le=le.toTimeString().match(/\\(([A-Za-z ]+)\\)$/))?le[1]:"GMT"}i>>>=0,c>>>=0,g>>>=0;var E=new Date().getFullYear(),k=new Date(E,0,1),W=new Date(E,6,1);E=k.getTimezoneOffset();var Z=W.getTimezoneOffset(),ge=Math.max(E,Z);s()[i>>>2>>>0]=60*ge,n()[c>>>2>>>0]=+(E!=Z),i=x(k),c=x(W),i=K(i),c=K(c),Z>>2>>>0]=i,s()[g+4>>>2>>>0]=c):(s()[g>>>2>>>0]=c,s()[g+4>>>2>>>0]=i)},d:()=>{Je("")},c:function(i,c,g){return i>>>=0,c=ae(c>>>0,g>>>0),Nt[i].apply(null,c)},k:function(i,c,g){return i>>>=0,c=ae(c>>>0,g>>>0),Nt[i].apply(null,c)},m:()=>{},j:()=>Date.now(),V:()=>{throw at+=1,"unwind"},C:function(){return 4294901760},f:()=>performance.timeOrigin+performance.now(),g:()=>C?(zo(),Ft(Mo)).cpus().length:navigator.hardwareConcurrency,y:function(i){i>>>=0;var c=o().length;if(i<=c||4294901760=g;g*=2){var x=c*(1+.2/g);x=Math.min(x,i+100663296);var E=Math;x=Math.max(i,x);e:{E=(E.min.call(E,4294901760,x+(65536-x%65536)%65536)-ne.buffer.byteLength+65535)/65536;try{ne.grow(E),G();var k=1;break e}catch{}k=void 0}if(k)return!0}return!1},O:Re,P:O,I:Ht,h:oe,o:Ce,x:ct,n:uo,a:ne||a.wasmMemory,z:po,e:function(i,c,g,x){return po(i>>>0,c>>>0,g>>>0,x>>>0)}},Y=function(){function i(g,x){return Y=g.exports,Y=Xu(),Y=ud(),te.qb.push(Y.Da),Qe.unshift(Y.X),ze=x,Fe(),Y}var c={a:sd};if(Le++,a.instantiateWasm)try{return a.instantiateWasm(c,i)}catch(g){we(`Module.instantiateWasm callback failed with error: ${g}`),m(g)}return St(c,function(g){i(g.instance,g.module)}).catch(m),{}}();a._OrtInit=(i,c)=>(a._OrtInit=Y.Y)(i,c),a._OrtGetLastError=(i,c)=>(a._OrtGetLastError=Y.Z)(i,c),a._OrtCreateSessionOptions=(i,c,g,x,E,k,W,Z,ge,le)=>(a._OrtCreateSessionOptions=Y._)(i,c,g,x,E,k,W,Z,ge,le),a._OrtAppendExecutionProvider=(i,c)=>(a._OrtAppendExecutionProvider=Y.$)(i,c),a._OrtAddFreeDimensionOverride=(i,c,g)=>(a._OrtAddFreeDimensionOverride=Y.aa)(i,c,g),a._OrtAddSessionConfigEntry=(i,c,g)=>(a._OrtAddSessionConfigEntry=Y.ba)(i,c,g),a._OrtReleaseSessionOptions=i=>(a._OrtReleaseSessionOptions=Y.ca)(i),a._OrtCreateSession=(i,c,g)=>(a._OrtCreateSession=Y.da)(i,c,g),a._OrtReleaseSession=i=>(a._OrtReleaseSession=Y.ea)(i),a._OrtGetInputOutputCount=(i,c,g)=>(a._OrtGetInputOutputCount=Y.fa)(i,c,g),a._OrtGetInputName=(i,c)=>(a._OrtGetInputName=Y.ga)(i,c),a._OrtGetOutputName=(i,c)=>(a._OrtGetOutputName=Y.ha)(i,c),a._OrtFree=i=>(a._OrtFree=Y.ia)(i),a._OrtCreateTensor=(i,c,g,x,E,k)=>(a._OrtCreateTensor=Y.ja)(i,c,g,x,E,k),a._OrtGetTensorData=(i,c,g,x,E)=>(a._OrtGetTensorData=Y.ka)(i,c,g,x,E),a._OrtReleaseTensor=i=>(a._OrtReleaseTensor=Y.la)(i),a._OrtCreateRunOptions=(i,c,g,x)=>(a._OrtCreateRunOptions=Y.ma)(i,c,g,x),a._OrtAddRunConfigEntry=(i,c,g)=>(a._OrtAddRunConfigEntry=Y.na)(i,c,g),a._OrtReleaseRunOptions=i=>(a._OrtReleaseRunOptions=Y.oa)(i),a._OrtCreateBinding=i=>(a._OrtCreateBinding=Y.pa)(i),a._OrtBindInput=(i,c,g)=>(a._OrtBindInput=Y.qa)(i,c,g),a._OrtBindOutput=(i,c,g,x)=>(a._OrtBindOutput=Y.ra)(i,c,g,x),a._OrtClearBoundOutputs=i=>(a._OrtClearBoundOutputs=Y.sa)(i),a._OrtReleaseBinding=i=>(a._OrtReleaseBinding=Y.ta)(i),a._OrtRunWithBinding=(i,c,g,x,E)=>(a._OrtRunWithBinding=Y.ua)(i,c,g,x,E),a._OrtRun=(i,c,g,x,E,k,W,Z)=>(a._OrtRun=Y.va)(i,c,g,x,E,k,W,Z),a._OrtEndProfiling=i=>(a._OrtEndProfiling=Y.wa)(i),a._JsepOutput=(i,c,g)=>(a._JsepOutput=Y.xa)(i,c,g),a._JsepGetNodeName=i=>(a._JsepGetNodeName=Y.ya)(i);var go=()=>(go=Y.za)(),Rr=a._pthread_self=()=>(Rr=a._pthread_self=Y.Aa)(),hn=a._malloc=i=>(hn=a._malloc=Y.Ba)(i),yo=a._free=i=>(yo=a._free=Y.Ca)(i);a.__emscripten_tls_init=()=>(a.__emscripten_tls_init=Y.Da)();var bo=a.__emscripten_thread_init=(i,c,g,x,E,k)=>(bo=a.__emscripten_thread_init=Y.Fa)(i,c,g,x,E,k);a.__emscripten_thread_crashed=()=>(a.__emscripten_thread_crashed=Y.Ga)();var wo=(i,c,g,x)=>(wo=Y.Ha)(i,c,g,x),gn=i=>(gn=Y.Ia)(i),yn=a.__emscripten_thread_exit=i=>(yn=a.__emscripten_thread_exit=Y.Ja)(i),vo=()=>(vo=Y.Ka)(),$o=i=>($o=Y.La)(i),_o=(i,c)=>(_o=Y.Ma)(i,c),bn=()=>(bn=Y.Na)(),Br=i=>(Br=Y.Oa)(i),wn=i=>(wn=Y.Pa)(i),xo=a.dynCall_ii=(i,c)=>(xo=a.dynCall_ii=Y.Ra)(i,c),So=i=>(So=Y.Sa)(i),Co=()=>(Co=Y.Ta)(),Io=i=>(Io=Y.Ua)(i),Ao=()=>(Ao=Y.Va)();a.___start_em_js=945539,a.___stop_em_js=945700;function ud(){var i=Y;i=Object.assign({},i);var c=x=>()=>x()>>>0,g=x=>E=>x(E)>>>0;return i.za=c(i.za),i.Aa=c(i.Aa),i.Ba=g(i.Ba),i.emscripten_main_runtime_thread_id=c(i.emscripten_main_runtime_thread_id),i.Na=c(i.Na),i.Pa=g(i.Pa),i}a.wasmMemory=ne,a.stackAlloc=wn,a.stackSave=bn,a.stackRestore=Br,a.keepRuntimeAlive=()=>0Uo)});var Wo=pr((mp,yd)=>{yd.exports=\'"use strict";var Module={},ENVIRONMENT_IS_NODE=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string";if(ENVIRONMENT_IS_NODE){var nodeWorkerThreads=require("worker_threads"),parentPort=nodeWorkerThreads.parentPort;parentPort.on("message",e=>onmessage({data:e}));var fs=require("fs"),vm=require("vm");Object.assign(global,{self:global,require,Module,location:{href:__filename},Worker:nodeWorkerThreads.Worker,importScripts:e=>vm.runInThisContext(fs.readFileSync(e,"utf8"),{filename:e}),postMessage:e=>parentPort.postMessage(e),performance:global.performance||{now:Date.now}})}var initializedJS=!1;function threadPrintErr(){var e=Array.prototype.slice.call(arguments).join(" ");if(ENVIRONMENT_IS_NODE){fs.writeSync(2,e+`\\n`);return}console.error(e)}function threadAlert(){var e=Array.prototype.slice.call(arguments).join(" ");postMessage({cmd:"alert",text:e,threadId:Module._pthread_self()})}var err=threadPrintErr;self.alert=threadAlert,Module.instantiateWasm=(e,t)=>{var a=Module.wasmModule;Module.wasmModule=null;var r=new WebAssembly.Instance(a,e);return t(r)},self.onunhandledrejection=e=>{throw e.reason||e};function handleMessage(e){try{if(e.data.cmd==="load"){let a=[];self.onmessage=r=>a.push(r),self.startWorker=r=>{Module=r,postMessage({cmd:"loaded"});for(let s of a)handleMessage(s);self.onmessage=handleMessage},Module.wasmModule=e.data.wasmModule;for(const r of e.data.handlers)Module[r]=(...s)=>{postMessage({cmd:"callHandler",handler:r,args:s})};if(Module.wasmMemory=e.data.wasmMemory,Module.buffer=Module.wasmMemory.buffer,Module.ENVIRONMENT_IS_PTHREAD=!0,typeof e.data.urlOrBlob=="string")importScripts(e.data.urlOrBlob);else{var t=URL.createObjectURL(e.data.urlOrBlob);importScripts(t),URL.revokeObjectURL(t)}ortWasmThreaded(Module)}else if(e.data.cmd==="run"){Module.__emscripten_thread_init(e.data.pthread_ptr,0,0,1),Module.__emscripten_thread_mailbox_await(e.data.pthread_ptr),Module.establishStackSpace(),Module.PThread.receiveObjectTransfer(e.data),Module.PThread.threadInitTLS(),initializedJS||(initializedJS=!0);try{Module.invokeEntryPoint(e.data.start_routine,e.data.arg)}catch(a){if(a!="unwind")throw a}}else e.data.cmd==="cancel"?Module._pthread_self()&&Module.__emscripten_thread_exit(-1):e.data.target==="setimmediate"||(e.data.cmd==="checkMailbox"?initializedJS&&Module.checkMailbox():e.data.cmd&&(err(`worker.js received unknown command ${e.data.cmd}`),err(e.data)))}catch(a){throw Module.__emscripten_thread_crashed?.(),a}}self.onmessage=handleMessage;\\n\'});var En,Tt,fr,Ur,hr,Ko,Pn,ie=q(()=>{"use strict";En=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;default:throw new Error(`unsupported data type: ${e}`)}},Tt=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";default:throw new Error(`unsupported data type: ${e}`)}},fr=e=>[void 0,4,1,1,2,2,4,8,void 0,1,2,8,4,8,void 0,void 0,void 0][e],Ur=e=>{switch(e){case"float16":return typeof Float16Array<"u"&&Float16Array.from?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},hr=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},Ko=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool",Pn=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;default:throw new Error(`unsupported data location: ${e}`)}}});var Vr=q(()=>{"use strict"});var Yo=q(()=>{"use strict";Vr()});var Zo,Qo=q(()=>{"use strict";Zo="1.17.3"});var Xo,rt,On=q(()=>{"use strict";Qo();Xo="warning",rt={wasm:{},webgl:{},webgpu:{},versions:{common:Zo},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);Xo=e}},get logLevel(){return Xo}};Object.defineProperty(rt,"logLevel",{enumerable:!0})});var qt,Jo=q(()=>{"use strict";On();qt=rt});var ea=q(()=>{"use strict"});var ta=q(()=>{"use strict";Nr()});var na=q(()=>{"use strict"});var oa=q(()=>{"use strict";Nr()});var Nr=q(()=>{"use strict";ea();ta();na();oa()});var Wr=q(()=>{"use strict";Nr()});var kn,aa,Mt,Et,Rn=q(()=>{"use strict";On();kn=(e,t)=>{(typeof rt.trace>"u"?!rt.wasm.trace:!rt.trace)||console.timeStamp(`${e}::ORT::${t}`)},aa=(e,t)=>{let r=new Error().stack?.split(/\\r\\n|\\r|\\n/g)||[],o=!1;for(let n=0;n{(typeof rt.trace>"u"?!rt.wasm.trace:!rt.trace)||aa("BEGIN",e)},Et=e=>{(typeof rt.trace>"u"?!rt.wasm.trace:!rt.trace)||aa("END",e)}});var ia=q(()=>{"use strict";Vr();Wr();Rn()});var sa=q(()=>{"use strict";ia()});var ua=q(()=>{"use strict"});var da=q(()=>{"use strict"});var la=q(()=>{"use strict"});var ca=q(()=>{"use strict"});var pa=q(()=>{"use strict";Vr();Wr()});var ma=q(()=>{"use strict";pa()});var Kt=q(()=>{"use strict";Yo();Jo();sa();Wr();ua();da();Rn();la();ca();ma()});var Td,Ed,fa,ha,ga,Pd,De,Pt=q(()=>{"use strict";ie();Td=["V","I","W","E","F"],Ed=(e,t)=>{console.log(`[${Td[e]},${new Date().toISOString()}]${t}`)},ga=(e,t)=>{fa=e,ha=t},Pd=(e,t)=>{let r=hr(e),o=hr(fa);r>=o&&Ed(r,typeof t=="function"?t():t)},De=(...e)=>{ha&&Pd(...e)}});var ya,ba=q(()=>{"use strict";ie();ya=(e,t)=>new(Ur(t))(e)});var Gr=q(()=>{"use strict"});var Hr,Od,wa,Dn,Bn,$a,_a=q(()=>{"use strict";Pt();Gr();Hr=e=>Math.ceil(e/16)*16,Od=1,wa=()=>Od++,Dn=async(e,t,r,o)=>{let n=Hr(r),s=e.device.createBuffer({size:n,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let u=e.getCommandEncoder();e.endComputePass(),u.copyBufferToBuffer(t,0,s,0,n),e.flush(),await s.mapAsync(GPUMapMode.READ);let l=s.getMappedRange();if(o){let a=o();return a.set(new Uint8Array(l,0,r)),a}else return new Uint8Array(l.slice(0,r))}finally{s.destroy()}},Bn=class{constructor(t){this.backend=t;this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersForUploadingPending=[],this.buffersPending=[],this.externalBuffers=new Map,this.capturedPendingBuffers=new Map}upload(t,r){let o=r.buffer,n=r.byteOffset,s=r.byteLength,u=Hr(s),l=this.storageCache.get(t);if(!l)throw new Error("gpu data for uploading does not exist");if(l.originalSize!==s)throw new Error(`inconsistent data size. gpu data size=${l.originalSize}, data size=${s}`);let a=this.backend.device.createBuffer({mappedAtCreation:!0,size:u,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),p=a.getMappedRange();new Uint8Array(p).set(new Uint8Array(o,n,s)),a.unmap();let m=this.backend.getCommandEncoder();this.backend.endComputePass(),m.copyBufferToBuffer(a,0,l.gpuData.buffer,0,u),De("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${t})`),this.buffersForUploadingPending.push(a)}memcpy(t,r){let o=this.storageCache.get(t);if(!o)throw new Error("source gpu data for memcpy does not exist");let n=this.storageCache.get(r);if(!n)throw new Error("destination gpu data for memcpy does not exist");if(o.originalSize!==n.originalSize)throw new Error("inconsistent source and destination gpu data size");let s=Hr(o.originalSize),u=this.backend.getCommandEncoder();this.backend.endComputePass(),u.copyBufferToBuffer(o.gpuData.buffer,0,n.gpuData.buffer,0,s)}registerExternalBuffer(t,r,o){let n;if(o){if(n=this.externalBuffers.get(o),n===void 0)throw new Error("previous buffer is not registered");if(t===o)return De("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${n}, buffer is the same, skip.`),n;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet.\n Please use the previous external buffer!`);this.externalBuffers.delete(o)}else n=wa();return this.storageCache.set(n,{gpuData:{id:n,type:0,buffer:t},originalSize:r}),this.externalBuffers.set(t,n),De("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${n}, registered.`),n}unregisterExternalBuffer(t){let r=this.externalBuffers.get(t);r!==void 0&&(this.storageCache.delete(r),this.externalBuffers.delete(t),De("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${r}`))}create(t,r=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let o=Hr(t),n,s=(r&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,u=(r&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(s||u){let a=s?this.freeBuffers:this.freeUniformBuffers,p=a.get(o);p||(p=[],a.set(o,p)),p.length>0?n=p.pop():n=this.backend.device.createBuffer({size:o,usage:r})}else n=this.backend.device.createBuffer({size:o,usage:r});let l={id:wa(),type:0,buffer:n};return this.storageCache.set(l.id,{gpuData:l,originalSize:t}),De("verbose",()=>`[WebGPU] GpuDataManager.create(size=${t}) => id=${l.id}`),l}get(t){return this.storageCache.get(t)?.gpuData}release(t){let r=this.storageCache.get(t);if(!r)throw new Error("releasing data does not exist");return De("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(r.gpuData.buffer),r.originalSize}async download(t,r){let o=this.storageCache.get(t);if(!o)throw new Error("data does not exist");await Dn(this.backend,o.gpuData.buffer,o.originalSize,r)}refreshPendingBuffers(){for(let t of this.buffersForUploadingPending)t.destroy();if(this.buffersForUploadingPending=[],this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let t of this.buffersPending)(t.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE?this.freeBuffers.get(t.size).push(t):(t.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM?this.freeUniformBuffers.get(t.size).push(t):t.destroy();this.buffersPending=[]}else{let t=this.capturedPendingBuffers.get(this.backend.currentSessionId);t||(t=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,t));for(let r of this.buffersPending)t.push(r);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.freeUniformBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onReleaseSession(t){let r=this.capturedPendingBuffers.get(t);r&&(r.forEach(o=>{o.destroy()}),this.capturedPendingBuffers.delete(t))}},$a=(...e)=>new Bn(...e)});var Mn,$e,je=q(()=>{"use strict";Mn=class{constructor(t){Object.assign(this,t)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(t=>`${this[t]}`).join(";")),this.key}},$e=e=>new Mn(e)});var zn,mt,z,zt,Lr,Fr,qr,_e=q(()=>{"use strict";zn=class{static calcMatMulShape(t,r){return t[1]!==r[0]?void 0:[t[0],r[1]]}},mt=class{static calcShape(t,r,o=!1){let n=t.length,s=r.length;if(n===0)return r;if(s===0)return t;let u=Math.max(t.length,r.length),l=new Array(u);if(o){if(n<2||s<2)return;let a=zn.calcMatMulShape([t[n-2],t[n-1]],[r[s-2],r[s-1]]);if(a===void 0)return;[l[u-2],l[u-1]]=a}for(let a=o?3:1;a<=u;a++){let p=n-a<0?1:t[n-a],m=s-a<0?1:r[s-a];if(p!==m&&p>1&&m>1)return;let f=Math.max(p,m);if(p&&m)l[u-a]=Math.max(p,m);else{if(f>1)return;l[u-a]=0}}return l}static isValidBroadcast(t,r){let o=t.length,n=r.length;if(o>n)return!1;for(let s=1;s<=o;s++)if(t[o-s]!==1&&t[o-s]!==r[n-s])return!1;return!0}},z=class e{static size(t){return e.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let o=t.length;if(o===0)return[];let n=new Array(o),s=o-1;for(;s>=0;){if(t[s]%r===0){n[s]=t[s]/r;break}if(r%t[s]!==0)throw new Error("cannot convert shape");n[s]=1,r/=t[s],s--}for(s--;s>=0;s--)n[s]=t[s];return n}static sizeFromDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,r,t.length)}static sizeToDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,o){let n=1;for(let s=r;s=0;--n)o[n]=o[n+1]*t[n+1];return o}static normalizeAxis(t,r){if(t<-r&&t>=r)throw new Error("unsupported axis for this operation.");return t<0?t+r:t}static normalizeAxes(t,r){return t.map(o=>this.normalizeAxis(o,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(o=>t[o]):t.slice().reverse()}static padShape(t,r){let o=t.length;return t.map((n,s)=>n+r[s]+r[s+o])}static areEqual(t,r){return t.length!==r.length?!1:t.every((o,n)=>o===r[n])}},zt=class e{static adjustPoolAttributes(t,r,o,n,s,u){if(!t&&o.length!==r.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let l=0;l=o.length?o.push(r[l+2]):o[l]=r[l+2];for(let l=0;l=o[l]||u[l+o.length]>=o[l])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,o,n,s,u,l){if(l){if(s.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(r.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let a=0;a{"use strict";ie();_e();Kr=64,Vn=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(e){case 10:return t>1?`vec${t}`:"f16";case 1:return t>1?`vec${t}`:"f32";case 6:return t>1?`vec${t}`:"i32";case 12:return t>1?`vec${t}`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4"];default:throw new Error(`Unknown data type: ${e}`)}},Pe=(e,t=1)=>{let r=Vn(e,t);return typeof r=="string"?r:r[0]},et=(e,t=1)=>{let r=Vn(e,t);return typeof r=="string"?r:r[1]},j=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:z.computeStrides(r)})}),t},Me=e=>e%4===0?4:e%2===0?2:1,Ye=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,st=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}f(${r})`,tt=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,re=(e,t,r,o)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?o==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:o==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,Nn=(e,t,r,o,n)=>{let s=typeof r=="number",u=s?r:r.length,l=[...new Array(u).keys()],a=u<2?"u32":u<=4?`vec${u}`:`array`,p=Vn(t,n),m=typeof p=="string"?p:p[1],f=typeof p=="string"?p:p[0],b={indices:a,value:m,storage:f,tensor:t},_=G=>typeof G=="string"?G:`${G}u`,y={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},$=s?"uniforms.":"",I=`${$}${e}_shape`,C=`${$}${e}_strides`,v="";for(let G=0;G ${b.indices} {\n var indices: ${b.indices};\n var current = offset;\n ${v}\n return indices;\n }`,T=G=>(y.offsetToIndices=!0,u<2?G:`o2i_${e}(${G})`),D=[];if(u>=2)for(let G=u-1;G>=0;G--)D.push(`${re(C,G,u)} * (indices[${G}])`);let U=u<2?"":`\n fn i2o_${e}(indices: ${b.indices}) -> u32 {\n return ${D.join("+")};\n }`,V=G=>(y.indicesToOffset=!0,u<2?G:`i2o_${e}(${G})`),H=(...G)=>u===0?"0u":`${b.indices}(${G.map(_).join(",")})`,R=(G,J)=>u<2?`${G}`:`${re(G,J,u)}`,L=(G,J,Se)=>u<2?`${G}=${Se};`:`${re(G,J,u)}=${Se};`,pe={},Ie=(G,J)=>{y.broadcastedIndicesToOffset=!0;let Se=`${J.name}broadcastedIndicesTo${e}Offset`;if(Se in pe)return`${Se}(${G})`;let Qe=[];for(let Xe=u-1;Xe>=0;Xe--){let Le=J.indicesGet("outputIndices",Xe+J.rank-u);Qe.push(`${R(C,Xe)} * (${Le} % ${R(I,Xe)})`)}return pe[Se]=`fn ${Se}(outputIndices: ${J.type.indices}) -> u32 {\n return ${Qe.length>0?Qe.join("+"):"0u"};\n }`,`${Se}(${G})`},we=(G,J)=>(()=>{if(b.storage===b.value)return`${e}[${G}]=${J};`;if(b.storage==="vec2"&&b.value==="i32")return`${e}[${G}]=vec2(u32(${J}), select(0u, 0xFFFFFFFFu, ${J} < 0));`;if(b.storage==="vec2"&&b.value==="u32")return`${e}[${G}]=vec2(u32(${J}), 0u);`;if(b.storage==="u32"&&b.value==="vec4")return`${e}[${G}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${J}));`;throw new Error(`not supported combination of storage type ${b.storage} and value type ${b.value} yet`)})(),ne=G=>(()=>{if(b.storage===b.value)return`${e}[${G}]`;if(b.storage==="vec2"&&b.value==="i32")return`i32(${e}[${G}].x)`;if(b.storage==="vec2"&&b.value==="u32")return`u32(${e}[${G}].x)`;if(b.storage==="u32"&&b.value==="vec4")return`vec4(bool(${e}[${G}] & 0xFFu), bool(${e}[${G}] & 0xFF00u), bool(${e}[${G}] & 0xFF0000u), bool(${e}[${G}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${b.storage} and value type ${b.value} yet`)})(),ze=u<2?"":`\n fn get_${e}ByIndices(indices: ${b.indices}) -> ${m} {\n return ${ne(`i2o_${e}(indices)`)};\n }`,Q=u<2?"":(()=>{let G=l.map(Se=>`d${Se}: u32`).join(", "),J=l.map(Se=>`d${Se}`).join(", ");return`\n fn get_${e}(${G}) -> ${m} {\n return get_${e}ByIndices(${H(J)});\n }`})(),xe=(...G)=>{if(G.length!==u)throw new Error(`indices length must be ${u}`);let J=G.map(_).join(",");return u===0?ne("0u"):u===1?ne(J[0]):(y.get=!0,y.getByIndices=!0,y.indicesToOffset=!0,`get_${e}(${J})`)},me=G=>u<2?ne(G):(y.getByIndices=!0,y.indicesToOffset=!0,`get_${e}ByIndices(${G})`),ue=u<2?"":`\n fn set_${e}ByIndices(indices: ${b.indices}, value: ${m}) {\n ${we(`i2o_${e}(indices)`,"value")}\n }`,se=u<2?"":(()=>{let G=l.map(Se=>`d${Se}: u32`).join(", "),J=l.map(Se=>`d${Se}`).join(", ");return`\n fn set_${e}(${G}, value: ${m}) {\n set_${e}ByIndices(${H(J)}, value);\n }`})();return{impl:()=>{let G=[],J=!1;return y.offsetToIndices&&(G.push(A),J=!0),y.indicesToOffset&&(G.push(U),J=!0),y.broadcastedIndicesToOffset&&(Object.values(pe).forEach(Se=>G.push(Se)),J=!0),y.set&&(G.push(se),J=!0),y.setByIndices&&(G.push(ue),J=!0),y.get&&(G.push(Q),J=!0),y.getByIndices&&(G.push(ze),J=!0),!s&&J&&G.unshift(`const ${I} = ${b.indices}(${r.join(",")});`,`const ${C} = ${b.indices}(${z.computeStrides(r).join(",")});`),G.join(`\n`)},type:b,offsetToIndices:T,indicesToOffset:V,broadcastedIndicesToOffset:Ie,indices:H,indicesGet:R,indicesSet:L,set:(...G)=>{if(G.length!==u+1)throw new Error(`indices length must be ${u}`);let J=G[u];if(typeof J!="string")throw new Error("value must be string");let Se=G.slice(0,u).map(_).join(",");return u===0?we("0u",J):u===1?we(Se[0],J):(y.set=!0,y.setByIndices=!0,y.indicesToOffset=!0,`set_${e}(${Se}, ${J})`)},setByOffset:we,setByIndices:(G,J)=>u<2?we(G,J):(y.setByIndices=!0,y.indicesToOffset=!0,`set_${e}ByIndices(${G}, ${J});`),get:xe,getByOffset:ne,getByIndices:me,usage:o,name:e,strides:C,shape:I,rank:u}},M=(e,t,r,o=1)=>Nn(e,t,r,"input",o),F=(e,t,r,o=1)=>Nn(e,t,r,"output",o),jr=(e,t,r,o=1)=>Nn(e,t,r,"internal",o),Un=class{constructor(t){this.normalizedDispatchGroup=t;this.internalVariables=[];this.variables=[];this.uniforms=[];this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(t){return`if (global_idx >= ${typeof t=="number"?`${t}u`:t}) { return; }`}mainStart(t=Kr){let r=typeof t=="number"?t:t[0],o=typeof t=="number"?1:t[1],n=typeof t=="number"?1:t[2],s=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,u=s?`@builtin(global_invocation_id) global_id : vec3,\n @builtin(workgroup_id) workgroup_id : vec3,\n @builtin(local_invocation_id) local_id : vec3`:`@builtin(local_invocation_id) local_id : vec3,\n @builtin(local_invocation_index) local_idx : u32,\n @builtin(workgroup_id) workgroup_id : vec3,\n @builtin(num_workgroups) num_workgroups : vec3`,l=s?"let global_idx = global_id.x; let local_idx = local_id.x;":`let global_idx = (workgroup_id.z * num_workgroups[0] * num_workgroups[1] +\n workgroup_id.y * num_workgroups[0] + workgroup_id.x) * ${r*o*n}u + local_idx;`;return`@compute @workgroup_size(${r}, ${o}, ${n})\n fn main(${u}) {\n ${l}\n `}appendVariableUniforms(t){t.rank!==0&&(t.shape.startsWith("uniforms.")&&this.uniforms.push({name:t.shape.replace("uniforms.",""),type:"u32",length:t.rank}),t.strides.startsWith("uniforms.")&&this.uniforms.push({name:t.strides.replace("uniforms.",""),type:"u32",length:t.rank}))}declareVariable(t,r){if(t.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(t),this.appendVariableUniforms(t);let o=t.usage==="input"?"read":"read_write",n=t.type.storage;return`@group(0) @binding(${r}) var ${t.name}: array<${n}>;`}declareVariables(...t){return t.map(r=>this.declareVariable(r,this.variableIndex++)).join(`\n`)}registerInternalVariable(t){if(t.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(t),this.appendVariableUniforms(t)}registerInternalVariables(...t){return t.forEach(r=>this.registerInternalVariable(r)),this}registerUniform(t,r,o=1){return this.uniforms.push({name:t,type:r,length:o}),this}registerUniforms(t){return this.uniforms=this.uniforms.concat(t),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let t=[];for(let{name:r,type:o,length:n}of this.uniforms)if(n&&n>4)o==="f16"?t.push(`@align(16) ${r}:array, ${Math.ceil(n/8)}>`):t.push(`${r}:array, ${Math.ceil(n/4)}>`);else{let s=n==null||n===1?o:`vec${n}<${o}>`;t.push(`${r}:${s}`)}return`\n struct Uniforms { ${t.join(", ")} };\n @group(0) @binding(${this.variableIndex}) var uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(t=>t.impl()).join(`\n`)+this.internalVariables.map(t=>t.impl()).join(`\n`)}},xa=e=>new Un(e),jt=(e,t)=>{let r=e.length,o=[];for(let n=0;n1&&u===1&&o.unshift(s)}return o}});var kd,Sa,Rd,Bd,ot,Ca,Ia,Yt=q(()=>{"use strict";ie();_e();je();be();kd=e=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.")},Sa=(e,t)=>t&&t.length!==e?[...new Array(e).keys()].reverse():t,Rd=(e,t)=>z.sortBasedOnPerm(e,Sa(e.length,t)),Bd=(e,t,r,o)=>{let n=[];n.push(`fn perm(i: ${o.type.indices}) -> ${r.type.indices} {\n var a: ${r.type.indices};`);for(let s=0;s{let r=e.dataType,o=e.dims.length,n=Sa(o,t),s=Rd(e.dims,n),u=F("output",r,s.length),l=M("a",r,o),a=p=>`\n ${p.registerUniform("output_size","u32").declareVariables(l,u)}\n\n ${Bd(n,o,l,u)}\n\n ${p.mainStart()}\n ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}\n\n let indices = ${u.offsetToIndices("global_idx")};\n let aIndices = perm(indices);\n\n ${u.setByOffset("global_idx",l.getByIndices("aIndices"))}\n }`;return{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:p=>{let m=z.size(s);return{outputs:[{dims:s,dataType:p[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...j(p[0].dims,s)]}},getShaderSource:a}},Ca=(e,t)=>{kd(e.inputs),e.compute(ot(e.inputs[0],t.perm))},Ia=e=>$e({perm:e.perm})});var Dd,Md,zd,Ud,Vd,Nd,Wd,Gd,Hd,Ld,ft,Aa,Ta,Ea,Pa,Oa,ka,Ra,Ba,Da,Ma,za=q(()=>{"use strict";ie();_e();be();Yr();Yt();Dd={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},Md={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},zd={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Ud={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},Vd=(e,t)=>{let r=[];for(let o=t-e;o{let r=[],o=e.length;for(let s=0;se[s]);return[r,n]},Wd=(e,t)=>{let r=e.length+t.length,o=[],n=0;for(let s=0;s{for(let r=0;r{let r=[];if(!Gd(e,t)){for(let o=0;or.push(o))}return r},Ld=(e,t,r,o,n,s,u)=>{let l=r[0].dims,a=z.size(s),p=z.size(u),m=M("_A",r[0].dataType,l),f=F("output",n,s),b=32,_=`\n var aBestValues : array;\n `;return{name:e,shaderCache:t,getShaderSource:$=>`\n ${$.registerUniform("reduceSize","u32").declareVariables(m,f)}\n ${_}\n fn DIV_CEIL(a : u32, b : u32) -> u32 {\n return ((a - 1u) / b + 1u);\n }\n ${$.mainStart(b)}\n\n let outputIndex = global_idx / ${b};\n let offset = outputIndex * uniforms.reduceSize;\n\n var bestValue = f32(${zd[o]});\n let Length = uniforms.reduceSize;\n for (var k = local_idx; k < Length; k = k + ${b}) {\n let candidate = f32(${m.getByOffset("offset + k")});\n bestValue = ${Dd[o]};\n }\n aBestValues[local_idx] = bestValue;\n workgroupBarrier();\n\n var reduceSize = min(Length, ${b}u);\n for (var currentSize = reduceSize / 2u; reduceSize > 1u;\n currentSize = reduceSize / 2u) {\n let interval = DIV_CEIL(reduceSize, 2u);\n if (local_idx < currentSize) {\n let candidate = aBestValues[local_idx + interval];\n bestValue = ${Md[o]};\n aBestValues[local_idx] = bestValue;\n }\n reduceSize = interval;\n workgroupBarrier();\n }\n\n if (local_idx == 0u) {\n ${f.setByOffset("outputIndex",`${o==="mean"?`${f.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${f.type.storage}(${Ud[o]})`}`)};\n }\n }`,getRunData:()=>({outputs:[{dims:s,dataType:n}],dispatchGroup:{x:a},programUniforms:[{type:12,data:p}]})}},ft=(e,t,r,o)=>{let n=e.inputs.length===1?r:Wn(e.inputs,r),s=n.axes;s.length===0&&!n.noopWithEmptyAxes&&(s=e.inputs[0].dims.map((_,y)=>y));let u=z.normalizeAxes(s,e.inputs[0].dims.length),l=u,a=e.inputs[0],p=Hd(l,e.inputs[0].dims.length);p.length>0&&(a=e.compute(ot(e.inputs[0],p),{inputs:[0],outputs:[-1]})[0],l=Vd(l.length,a.dims.length));let[m,f]=Nd(a.dims,l),b=m;n.keepDims&&(b=Wd(m,u)),e.compute(Ld(t,{hint:n.cacheKey,inputDependencies:["type"]},[a],o,e.inputs[0].dataType,b,f),{inputs:[a]})},Aa=(e,t)=>{ft(e,"ReduceMeanShared",t,"mean")},Ta=(e,t)=>{ft(e,"ReduceL1Shared",t,"l1")},Ea=(e,t)=>{ft(e,"ReduceL2Shared",t,"l2")},Pa=(e,t)=>{ft(e,"ReduceLogSumExpShared",t,"logSumExp")},Oa=(e,t)=>{ft(e,"ReduceMaxShared",t,"max")},ka=(e,t)=>{ft(e,"ReduceMinShared",t,"min")},Ra=(e,t)=>{ft(e,"ReduceProdShared",t,"prod")},Ba=(e,t)=>{ft(e,"ReduceSumShared",t,"sum")},Da=(e,t)=>{ft(e,"ReduceSumSquareShared",t,"sumSquare")},Ma=(e,t)=>{ft(e,"ReduceLogSumShared",t,"logSum")}});var ht,Fd,Zr,Wn,gt,qd,Kd,jd,Yd,Zd,Qd,Xd,Jd,el,tl,yt,Ua,Va,Na,Wa,Ga,Ha,La,Fa,qa,Ka,Yr=q(()=>{"use strict";ie();_e();je();be();za();ht=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},Fd=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Zr=(e,t,r,o,n,s,u=!1,l=!1)=>{let a=[],p=r[0].dims,m=p.length,f=z.normalizeAxes(n,m),b=!l&&f.length===0;p.forEach((I,C)=>{b||f.indexOf(C)>=0?u&&a.push(1):a.push(I)});let _=a.length,y=z.size(a);return{name:e,shaderCache:t,getShaderSource:I=>{let C=[],v=M("_A",r[0].dataType,m),A=F("output",s,_),T=o(v,A,f),D=T[2];for(let U=0,V=0;U=0?(u&&V++,D=`for(var j${U}: u32 = 0; j${U} < ${p[U]}; j${U}++) {\n ${T[2].includes("last_index")?`let last_index = j${U};`:""}\n ${v.indicesSet("input_indices",U,`j${U}`)}\n ${D}\n }`):(C.push(`${v.indicesSet("input_indices",U,A.indicesGet("output_indices",V))};`),V++);return`\n\n ${I.registerUniform("output_size","u32").declareVariables(v,A)}\n\n ${I.mainStart()}\n ${I.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}\n var input_indices: ${v.type.indices};\n let output_indices = ${A.offsetToIndices("global_idx")};\n\n ${C.join(`\n`)}\n ${T[0]} // init ops for reduce max/min\n ${T[1]}\n ${D}\n ${T[3]}\n ${T.length===4?A.setByOffset("global_idx","value"):T.slice(4).join(`\n`)}\n }`},getRunData:()=>({outputs:[{dims:a,dataType:s}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:[{type:12,data:y},...j(p,a)]})}},Wn=(e,t)=>{let r=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(o=>r.push(Number(o))),$e({axes:r,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},gt=(e,t,r,o)=>{let n=e.inputs,s=n.length===1?r:Wn(n,r);e.compute(Zr(t,{hint:s.cacheKey,inputDependencies:["rank"]},[n[0]],s.noopWithEmptyAxes&&s.axes.length===0?Fd:o,s.axes,n[0].dataType,s.keepDims,s.noopWithEmptyAxes),{inputs:[0]})},qd=(e,t)=>{ht(e.inputs),gt(e,"ReduceLogSum",t,(o,n)=>[`var value = ${n.type.storage}(0);`,"",`value += ${o.getByIndices("input_indices")};`,"value = log(value);"])},Kd=(e,t)=>{ht(e.inputs),gt(e,"ReduceL1",t,(o,n)=>[`var value = ${n.type.storage}(0);`,"",`value += abs(${o.getByIndices("input_indices")});`,""])},jd=(e,t)=>{ht(e.inputs),gt(e,"ReduceL2",t,(o,n)=>[`var t = ${n.type.value}(0); var value = ${n.type.value}(0);`,"",`t = ${o.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},Yd=(e,t)=>{ht(e.inputs),gt(e,"ReduceLogSumExp",t,(o,n)=>[`var value = ${n.type.storage}(0);`,"",`value += exp(${o.getByIndices("input_indices")});`,"value = log(value);"])},Zd=(e,t)=>{ht(e.inputs),gt(e,"ReduceMax",t,(o,n,s)=>{let u=[];for(let l=0;l=0||s.length===0)&&u.push(o.indicesSet("input_indices",l,0));return[`${u.join(`\n`)}`,`var value = ${o.getByIndices("input_indices")};`,`value = max(value, ${o.getByIndices("input_indices")});`,""]})},Qd=(e,t)=>{ht(e.inputs),gt(e,"ReduceMean",t,(o,n,s)=>{let u=1;for(let l=0;l=0||s.length===0)&&(u*=e.inputs[0].dims[l]);return["var sum = f32(0);","",`sum += f32(${o.getByIndices("input_indices")});`,`let value = ${n.type.value}(sum / ${u});`]})},Xd=(e,t)=>{ht(e.inputs),gt(e,"ReduceMin",t,(o,n,s)=>{let u=[];for(let l=0;l=0||s.length===0)&&u.push(`input_indices[${l}] = 0;`);return[`${u.join(`\n`)}`,`var value = ${o.getByIndices("input_indices")};`,`value = min(value, ${o.getByIndices("input_indices")});`,""]})},Jd=(e,t)=>{ht(e.inputs),gt(e,"ReduceProd",t,(o,n)=>[`var value = ${n.type.storage}(1);`,"",`value *= ${o.getByIndices("input_indices")};`,""])},el=(e,t)=>{ht(e.inputs),gt(e,"ReduceSum",t,(o,n)=>[`var value = ${n.type.storage}(0);`,"",`value += ${o.getByIndices("input_indices")};`,""])},tl=(e,t)=>{ht(e.inputs),gt(e,"ReduceSumSquare",t,(o,n)=>[`var t = ${n.type.value}(0); var value = ${n.type.value}(0);`,"",`t = ${o.getByIndices("input_indices")}; value += t * t;`,""])},yt=(e,t,r)=>{if(t.length===0)return r;let o=1,n=1;for(let s=0;s1024},Ua=(e,t)=>{yt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Qd(e,t):Aa(e,t)},Va=(e,t)=>{yt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Kd(e,t):Ta(e,t)},Na=(e,t)=>{yt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?jd(e,t):Ea(e,t)},Wa=(e,t)=>{yt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Yd(e,t):Pa(e,t)},Ga=(e,t)=>{yt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Zd(e,t):Oa(e,t)},Ha=(e,t)=>{yt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Xd(e,t):ka(e,t)},La=(e,t)=>{yt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Jd(e,t):Ra(e,t)},Fa=(e,t)=>{yt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?el(e,t):Ba(e,t)},qa=(e,t)=>{yt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?tl(e,t):Da(e,t)},Ka=(e,t)=>{yt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?qd(e,t):Ma(e,t)}});var ja,Ya,Za,Gn,Qa=q(()=>{"use strict";ie();je();Yr();ja=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},Ya=(e,t)=>{ja(e.inputs);let r=(o,n,s)=>{let u=[];for(let l=0;l=0||s.length===0)&&u.push(`input_indices[${l}] = 0;`);return[`${u.join(`\n`)}`,`var value = ${o.getByIndices("input_indices")};\nvar best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) {\n value = ${o.getByIndices("input_indices")};\n best_index = i32(last_index);\n }`,"",n.setByOffset("global_idx","best_index")]};e.compute(Zr("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},Za=(e,t)=>{ja(e.inputs);let r=(o,n,s)=>{let u=[];for(let l=0;l=0||s.length===0)&&u.push(`input_indices[${l}] = 0;`);return[`${u.join(`\n`)}`,`var value = ${o.getByIndices("input_indices")};\nvar best_index : i32 = 0;`,`if (${o.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) {\n value = ${o.getByIndices("input_indices")};\n best_index = i32(last_index);\n }`,"",n.setByOffset("global_idx","best_index")]};e.compute(Zr("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},Gn=e=>$e(e)});var rl,nl,ol,al,Qr,il,Xa,Hn=q(()=>{"use strict";ie();Gr();be();rl=(e,t)=>{let r=e[0],o=e[1],n=e[2],s=e[3],u=e[4],l=e[5];if(u&&l)throw new Error("Attention cannot have both past and relative_position_bias");if(r.dims.length!==3)throw new Error(\'Input "input" must have 3 dimensions\');let a=r.dims[0],p=r.dims[1],m=r.dims[2];if(n.dims.length!==1)throw new Error(\'Input "bias" is expected to have 1 dimensions\');if(o.dims.length!==2)throw new Error(\'Input "weights" is expected to have 2 dimensions\');if(o.dims[0]!==m)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(n.dims[0]!==o.dims[1])throw new Error(\'Input "bias" dimension 0 should have same length as dimension 1 of input "weights"\');let f=n.dims[0]/3,b=f,_=b;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let A of t.qkvHiddenSizes)if(A%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");f=t.qkvHiddenSizes[0],b=t.qkvHiddenSizes[1],_=t.qkvHiddenSizes[2]}let y=p;if(f!==b)throw new Error("qkv_hidden_sizes first element should be same as the second");if(n.dims[0]!==f+b+_)throw new Error(\'Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes\');let $=0;if(u){if(b!==_)throw new Error(\'Input "past" expect k_hidden_size == v_hidden_size\');if(u.dims.length!==5)throw new Error(\'Input "past" must have 5 dimensions\');if(u.dims[0]!==2)throw new Error(\'Input "past" first dimension must be 2\');if(u.dims[1]!==a)throw new Error(\'Input "past" second dimension must be batch_size\');if(u.dims[2]!==t.numHeads)throw new Error(\'Input "past" third dimension must be num_heads\');if(u.dims[4]!==b/t.numHeads)throw new Error(\'Input "past" fifth dimension must be k_hidden_size / num_heads\');t.pastPresentShareBuffer||($=u.dims[3])}let I=y+$,C=-1,v=0;if(s)throw new Error("Mask not supported");if(u)throw new Error("past is not supported");if(l)throw new Error("relativePositionBias is not supported");return{batchSize:a,sequenceLength:p,pastSequenceLength:$,kvSequenceLength:y,totalSequenceLength:I,maxSequenceLength:C,inputHiddenSize:m,hiddenSize:f,vHiddenSize:_,headSize:Math.floor(f/t.numHeads),vHeadSize:Math.floor(_/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:v,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},nl=(e,t,r,o)=>{let n=Me(o),s=64,u=o/n;u{let b=F("x",t.dataType,t.dims,n),_="thread_max_vector";n===2?_="max(thread_max_vector.x, thread_max_vector.y)":n===4&&(_="max(max(thread_max_vector.x, thread_max_vector.y), max(thread_max_vector.z, thread_max_vector.w))");let y=et(t.dataType),$=[{name:"d_inv",type:y},{name:"d_comp",type:"u32"},{name:"elements_per_wg",type:"u32"}];return`\n var wgMax: array;\n var wgSum: array;\n ${f.registerUniforms($).declareVariables(b)}\n ${f.mainStart([s,1,1])}\n let localOffset = local_idx * uniforms.elements_per_wg;\n let offset: u32 = workgroup_id.x * uniforms.d_comp + localOffset;\n\n var thread_max_vector = ${Ye("f32",n,"-3.402823e+38f")};\n for (var i: u32 = 0; i < uniforms.elements_per_wg && i + localOffset < uniforms.d_comp; i++) {\n thread_max_vector = max(${st(y,n,"x[offset + i]")}, thread_max_vector);\n }\n wgMax[local_idx] = ${_};\n workgroupBarrier();\n\n var maxValue = -3.402823e+38f;\n for (var i = 0u; i < ${s}; i++) {\n maxValue = max(wgMax[i], maxValue);\n }\n\n var sumVector = ${Ye("f32",n,"0")};\n for (var i: u32 = 0; i < uniforms.elements_per_wg && i + localOffset < uniforms.d_comp; i++) {\n sumVector += exp(${st(y,n,"x[offset + i]")} - maxValue);\n }\n wgSum[local_idx] = ${tt("sumVector",n)};\n workgroupBarrier();\n\n var sum: f32 = 0;\n for (var i = 0u; i < ${s}; i++) {\n sum += wgSum[i];\n }\n\n if (sum == 0) {\n for (var i: u32 = 0; i < uniforms.elements_per_wg && i + localOffset < uniforms.d_comp; i++) {\n x[offset + i] = ${Ye(y,n,"uniforms.d_inv")};\n }\n } else {\n for (var i: u32 = 0; i < uniforms.elements_per_wg && i + localOffset < uniforms.d_comp; i++) {\n let f32input = ${st(y,n,"x[offset + i]")};\n x[offset + i] = ${b.type.value}(exp(f32input - maxValue) / sum);\n }\n }\n }`};e.compute({name:"AttentionProbsSoftmax",shaderCache:{hint:`${s};${p};${n}`},getShaderSource:m,getRunData:()=>({outputs:[],dispatchGroup:{x:r},programUniforms:a})},{inputs:[t],outputs:[]})},ol=(e,t,r,o,n,s)=>{let u=[n.batchSize,n.numHeads,n.sequenceLength,n.kvSequenceLength+n.pastSequenceLength],l=s.scale===0?1/Math.sqrt(n.headSize):s.scale,a=Me(n.headSize),p=n.headSize/a,m=12,f={x:Math.ceil(n.totalSequenceLength/m),y:Math.ceil(n.sequenceLength/m),z:n.batchSize*n.numHeads},b=[{type:12,data:n.sequenceLength},{type:12,data:p},{type:12,data:n.totalSequenceLength},{type:12,data:n.kvSequenceLength},{type:t.dataType,data:l}],_=[t,r],y=I=>{let C=M("q",t.dataType,t.dims,a),v=M("key",r.dataType,r.dims,a),A=F("output",t.dataType,u),T=Pe(t.dataType),D=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"alpha",type:T}];return`\n const beta: ${T} = 1.0;\n const TILE_SIZE = ${m}u;\n\n var tileQ: array<${C.type.storage}, ${m*m}>;\n var tileK: array<${C.type.storage}, ${m*m}>;\n ${I.registerUniforms(D).declareVariables(C,v,A)}\n ${I.mainStart([m,m,1])}\n // x holds the N and y holds the M\n let headIdx = workgroup_id.z;\n let m = workgroup_id.y * TILE_SIZE;\n let n = workgroup_id.x * TILE_SIZE;\n let lm = m + local_id.y;\n let ln = n + local_id.x;\n\n let qOffset = uniforms.M * uniforms.K * headIdx + m * uniforms.K;\n let kOffset = uniforms.kv_sequence_length * uniforms.K * headIdx + n * uniforms.K;\n\n var value = ${Ye(T,a)};\n for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {\n if (m + local_id.y < uniforms.M && w + local_id.x < uniforms.K) {\n tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x];\n }\n if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) {\n tileK[TILE_SIZE * local_id.y + local_id.x] = key[kOffset + local_id.y * uniforms.K + w + local_id.x];\n }\n workgroupBarrier();\n\n for (var k: u32 = 0u; k({outputs:[{dims:u,dataType:t.dataType,gpuDataType:0}],dispatchGroup:f,programUniforms:b}),getShaderSource:y},{inputs:_,outputs:[-1]})[0];return nl(e,$,n.batchSize*n.numHeads*n.sequenceLength,n.totalSequenceLength),$},al=(e,t,r,o)=>{let n=[o.batchSize,o.sequenceLength,o.vHiddenSize],s=12,u={x:Math.ceil(o.vHeadSize/s),y:Math.ceil(o.sequenceLength/s),z:o.batchSize*o.numHeads},l=[{type:12,data:o.sequenceLength},{type:12,data:o.totalSequenceLength},{type:12,data:o.vHeadSize},{type:12,data:o.numHeads},{type:12,data:o.vHiddenSize}],a=p=>{let m=M("probs",t.dataType,t.dims),f=M("v",r.dataType,r.dims),b=F("output",t.dataType,n),_=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"v_hidden_size",type:"u32"}];return`\n const TILE_SIZE = ${s}u;\n var tileQ: array<${m.type.value}, ${s*s}>;\n var tileK: array<${m.type.value}, ${s*s}>;\n ${p.registerUniforms(_).declareVariables(m,f,b)}\n ${p.mainStart([s,s,1])}\n let headIdx = workgroup_id.z;\n let m = workgroup_id.y * TILE_SIZE + local_id.y;\n let n = workgroup_id.x * TILE_SIZE + local_id.x;\n\n let offsetA = headIdx * (uniforms.M * uniforms.K) + m * uniforms.K;\n let offsetB = headIdx * (uniforms.N * uniforms.K) + n;\n\n var value = ${m.type.storage}(0);\n for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {\n if (m < uniforms.M && w + local_id.x < uniforms.K) {\n tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x];\n }\n if (n < uniforms.N && w + local_id.y < uniforms.K) {\n tileK[TILE_SIZE * local_id.y + local_id.x] = v[offsetB + (w + local_id.y) * uniforms.N];\n }\n workgroupBarrier();\n for (var k: u32 = 0u; k({outputs:[{dims:n,dataType:t.dataType,gpuDataType:0}],dispatchGroup:u,programUniforms:l}),getShaderSource:a},{inputs:[t,r],outputs:[0]})[0]},Qr=(e,t,r,o,n,s,u,l,a,p,m)=>{let f=ol(e,t,r,a,p,m);al(e,f,o,p)},il=(e,t)=>{let r=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],o=t.sequenceLength,n=t.inputHiddenSize,s=t.headSize,u=12,l={x:Math.ceil(t.headSize/u),y:Math.ceil(t.sequenceLength/u),z:t.batchSize*t.numHeads},a=[e.inputs[0],e.inputs[1],e.inputs[2]],p=[{type:12,data:o},{type:12,data:n},{type:12,data:s},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],m=f=>{let b=F("output_q",a[0].dataType,r),_=F("output_k",a[0].dataType,r),y=F("output_v",a[0].dataType,r),$=M("input",a[0].dataType,a[0].dims),I=M("weight",a[1].dataType,a[1].dims),C=M("bias",a[2].dataType,a[2].dims),v=$.type.storage,A=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return`\n const TILE_SIZE = ${u}u;\n var tileInput: array<${v}, ${u*u}>;\n var tileWeightQ: array<${v}, ${u*u}>;\n var tileWeightK: array<${v}, ${u*u}>;\n var tileWeightV: array<${v}, ${u*u}>;\n ${f.registerUniforms(A).declareVariables($,I,C,b,_,y)}\n ${f.mainStart([u,u,1])}\n let batchIndex = workgroup_id.z / uniforms.num_heads;\n let headNumber = workgroup_id.z % uniforms.num_heads;\n let m = workgroup_id.y * TILE_SIZE + local_id.y;\n let n = workgroup_id.x * TILE_SIZE + local_id.x;\n\n let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K;\n let biasOffsetQ = headNumber * uniforms.head_size;\n let biasOffsetK = uniforms.hidden_size + biasOffsetQ;\n let biasOffsetV = uniforms.hidden_size + biasOffsetK;\n\n var valueQ = ${v}(0);\n var valueK = ${v}(0);\n var valueV = ${v}(0);\n for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {\n if (m < uniforms.M && w + local_id.x < uniforms.K) {\n tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x];\n }\n if (n < uniforms.N && w + local_id.y < uniforms.K) {\n let offset = n + (w + local_id.y) * uniforms.ldb;\n tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset];\n tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset];\n tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset];\n }\n workgroupBarrier();\n for (var k: u32 = 0u; k({outputs:[{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:l,programUniforms:p}),getShaderSource:m},{inputs:a,outputs:[-1,-1,-1]})},Xa=(e,t)=>{let r=rl(e.inputs,t),[o,n,s]=il(e,r);return Qr(e,o,n,s,e.inputs[4],void 0,void 0,void 0,e.inputs[5],r,t)}});var sl,ul,dl,Ja,ei=q(()=>{"use strict";Kt();ie();_e();je();be();sl=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let r=(o,n,s)=>{let u=n.length;if(u!==o.length)throw new Error(`${s}: num dimensions != ${u}`);n.forEach((l,a)=>{if(l!==o[a])throw new Error(`${s}: dim[${a}] do not match`)})};if(e[0].dims.length>1){let o=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);r(e[1].dims,o,"Invalid input scale"),r(e[2].dims,o,"Invalid input B"),r(e[3].dims,o,"Invalid input mean"),r(e[4].dims,o,"Invalid input var")}else r(e[1].dims,[1],"Invalid input scale"),r(e[2].dims,[1],"Invalid input B"),r(e[3].dims,[1],"Invalid input mean"),r(e[4].dims,[1],"Invalid input var")},ul=(e,t)=>{let{epsilon:r,spatial:o,format:n}=t,s=e[0].dims,u=o?Me(s[s.length-1]):1,l=n==="NHWC"&&s.length>1?u:1,a=z.size(s)/u,p=o,m=p?s.length:s,f=M("x",e[0].dataType,e[0].dims,u),b=M("scale",e[1].dataType,e[1].dims,l),_=M("bias",e[2].dataType,e[2].dims,l),y=M("inputMean",e[3].dataType,e[3].dims,l),$=M("inputVar",e[4].dataType,e[4].dims,l),I=F("y",e[0].dataType,m,u),C=()=>{let A="";if(o)A=`let cOffset = ${s.length===1?"0u":n==="NHWC"?`outputIndices[${s.length-1}] / ${u}`:"outputIndices[1]"};`;else if(n==="NCHW")A=`\n ${I.indicesSet("outputIndices","0","0")}\n let cOffset = ${I.indicesToOffset("outputIndices")};`;else{A=`var cIndices = ${b.type.indices}(0);\n cIndices[0] = outputIndices[${s.length-1}];`;for(let T=1;T`\n const epsilon = ${r};\n ${A.registerUniform("outputSize","u32").declareVariables(f,b,_,y,$,I)}\n ${A.mainStart()}\n ${A.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}\n var outputIndices = ${I.offsetToIndices(`global_idx * ${u}`)};\n ${C()}\n let scale = ${b.getByOffset("cOffset")};\n let bias = ${_.getByOffset("cOffset")};\n let inputMean = ${y.getByOffset("cOffset")};\n let inputVar = ${$.getByOffset("cOffset")};\n let x = ${f.getByOffset("global_idx")};\n let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias;\n ${I.setByOffset("global_idx","value")}\n }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${o}_${u}`,inputDependencies:p?["rank","type","type","type","type"]:void 0},getShaderSource:v,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:p?[{type:12,data:a},...j(s)]:[{type:12,data:a}]})}},dl=e=>$e(e),Ja=(e,t)=>{let{inputs:r,outputCount:o}=e,n=dl({...t,outputCount:o});if(qt.webgpu.validateInputContent&&sl(r,n),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(ul(r,n))}});var ll,cl,ti,ri=q(()=>{"use strict";_e();be();ll=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},cl=e=>{let t=e[0].dims,r=e[0].dims[2],o=z.size(t)/4,n=e[0].dataType,s=M("input",n,t,4),u=M("bias",n,[r],4),l=M("residual",n,t,4),a=F("output",n,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(o/64)}}),getShaderSource:m=>`\n const channels = ${r}u / 4;\n ${m.declareVariables(s,u,l,a)}\n\n ${m.mainStart()}\n ${m.guardAgainstOutOfBoundsWorkgroupSizes(o)}\n let value = ${s.getByOffset("global_idx")}\n + ${u.getByOffset("global_idx % channels")} + ${l.getByOffset("global_idx")};\n ${a.setByOffset("global_idx","value")}\n }`}},ti=e=>{ll(e.inputs),e.compute(cl(e.inputs))}});var pl,Ee,ni,oi,ai,ii,si,ui,di,li,ci,ml,pi,mi,fi,hi,Xr,gi,Jr,yi,bi,wi,vi,$i,_i,xi,Si,Ci,Ii,Ai,Ti,Ei,Pi,Oi,ki,Ri,Bi,Ln,Fn,Di,Mi,zi,en=q(()=>{"use strict";ie();_e();je();be();pl=(e,t,r,o,n,s)=>{let u=Math.ceil(t/4),l="";typeof n=="string"?l=`${n}(a)`:l=n("a");let a=M("inputData",r,[u],4),p=F("outputData",o,[u],4);return`\n ${e.registerUniform("vec_size","u32").declareVariables(a,p)}\n\n ${s??""}\n\n ${e.mainStart()}\n ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}\n\n let a = ${a.getByOffset("global_idx")};\n ${p.setByOffset("global_idx",l)}\n }`},Ee=(e,t,r,o,n,s=e.dataType)=>({name:t,shaderCache:{hint:n,inputDependencies:["type"]},getShaderSource:u=>pl(u,z.size(e.dims),e.dataType,s,r,o),getRunData:u=>({outputs:[{dims:e.dims,dataType:s}],dispatchGroup:{x:Math.ceil(z.size(u[0].dims)/64/4)},programUniforms:[{type:12,data:Math.ceil(z.size(e.dims)/4)}]})}),ni=e=>{e.compute(Ee(e.inputs[0],"Abs","abs"))},oi=e=>{e.compute(Ee(e.inputs[0],"Acos","acos"))},ai=e=>{e.compute(Ee(e.inputs[0],"Acosh","acosh"))},ii=e=>{e.compute(Ee(e.inputs[0],"Asin","asin"))},si=e=>{e.compute(Ee(e.inputs[0],"Asinh","asinh"))},ui=e=>{e.compute(Ee(e.inputs[0],"Atan","atan"))},di=e=>{e.compute(Ee(e.inputs[0],"Atanh","atanh"))},li=e=>$e(e),ci=(e,t)=>{let r;switch(t.to){case 10:r="vec4";break;case 1:r="vec4";break;case 12:r="vec4";break;case 6:r="vec4";break;case 9:r="vec4";break;default:throw new RangeError(`not supported type (specified in attribute \'to\' from \'Cast\' operator): ${t.to}`)}e.compute(Ee(e.inputs[0],"Cast",r,void 0,t.cacheKey,t.to))},ml=e=>{let t=e.length>=2&&e[1].data!==0?e[1].getFloat32Array()[0]:Fr,r=e.length>=3&&e[2].data!==0?e[2].getFloat32Array()[0]:qr;return $e({min:t,max:r})},pi=(e,t)=>{let r=e.inputs.length===1?t:ml(e.inputs),o=et(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Clip",n=>`clamp(${n}, clip_min_, clip_max_)`,`\n const clip_min_: vec4<${o}> = vec4(${o}(${r.min}));\n const clip_max_: vec4<${o}> = vec4(${o}(${r.max}));\n`,r.cacheKey),{inputs:[0]})},mi=e=>{e.compute(Ee(e.inputs[0],"Ceil","ceil"))},fi=e=>{e.compute(Ee(e.inputs[0],"Cos","cos"))},hi=e=>{e.compute(Ee(e.inputs[0],"Cosh","cosh"))},Xr=e=>$e(e),gi=(e,t)=>{let r=et(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Elu",o=>`elu_vf32(${o})`,`\n const elu_alpha_ = ${r}(${t.alpha});\n\n fn elu_f32(a: ${r}) -> ${r} {\n return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0);\n }\n\n fn elu_vf32(v: vec4<${r}>) -> vec4<${r}> {\n return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w));\n }`,t.cacheKey))},Jr=(e="f32")=>`\nconst r0: ${e} = 0.3275911;\nconst r1: ${e} = 0.254829592;\nconst r2: ${e} = -0.284496736;\nconst r3: ${e} = 1.421413741;\nconst r4: ${e} = -1.453152027;\nconst r5: ${e} = 1.061405429;\n\nfn erf_vf32(v: vec4<${e}>) -> vec4<${e}> {\n let absv = abs(v);\n let x = 1.0 / (1.0 + r0 * absv);\n return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv));\n}`,yi=e=>{let t=et(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Erf",r=>`erf_vf32(${r})`,Jr(t)))},bi=e=>{e.compute(Ee(e.inputs[0],"Exp","exp"))},wi=e=>{e.compute(Ee(e.inputs[0],"Floor","floor"))},vi=e=>{let t=et(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Gelu",r=>`0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`,Jr(t)))},$i=(e,t)=>{let r=et(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"LeakyRelu",o=>`select(leaky_relu_alpha_ * ${o}, ${o}, ${o} >= vec4<${r}>(0.0))`,`const leaky_relu_alpha_ = ${r}(${t.alpha});`,t.cacheKey))},_i=e=>{e.compute(Ee(e.inputs[0],"Not",t=>`!${t}`))},xi=e=>{e.compute(Ee(e.inputs[0],"Neg",t=>`-${t}`))},Si=e=>{e.compute(Ee(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},Ci=e=>{let t=et(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"Relu",r=>`select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`))},Ii=e=>{e.compute(Ee(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},Ai=e=>$e(e),Ti=(e,t)=>{let r=et(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"HardSigmoid",o=>`max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${o} + vec4<${r}>(${t.beta})))`,void 0,t.cacheKey))},Ei=e=>{e.compute(Ee(e.inputs[0],"Sin","sin"))},Pi=e=>{e.compute(Ee(e.inputs[0],"Sinh","sinh"))},Oi=e=>{e.compute(Ee(e.inputs[0],"Sqrt","sqrt"))},ki=e=>{e.compute(Ee(e.inputs[0],"Tan","tan"))},Ri=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,Bi=e=>{e.compute(Ee(e.inputs[0],"Tanh",Ri))},Ln=(e="f32")=>`\nconst fast_gelu_a: ${e} = 0.5;\nconst fast_gelu_b: ${e} = 0.7978845608028654;\nconst fast_gelu_c: ${e} = 0.035677408136300125;\n\nfn tanh_v(v: vec4<${e}>) -> vec4<${e}> {\n return ${Ri("v")};\n}\n`,Fn=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,Di=e=>{let t=et(e.inputs[0].dataType);e.compute(Ee(e.inputs[0],"FastGelu",Fn,Ln(t),void 0,e.inputs[0].dataType))},Mi=(e,t)=>{let r=et(e.inputs[0].dataType);return e.compute(Ee(e.inputs[0],"ThresholdedRelu",o=>`select(vec4<${r}>(0.0), ${o}, ${o} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`,t.cacheKey)),0},zi=e=>{e.compute(Ee(e.inputs[0],"Log","log"))}});var fl,hl,Vi,Ni=q(()=>{"use strict";_e();be();en();fl=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},hl=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let r=M("input",e[0].dataType,e[0].dims,4),o=M("bias",e[0].dataType,[e[0].dims[2]],4),n=F("output",e[0].dataType,t,4),s=z.size(t)/4,u=Pe(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)}}),getShaderSource:a=>`\n const M_SQRT2 = sqrt(2.0);\n const halfChannels = ${e[0].dims[2]/4/2}u;\n\n ${a.declareVariables(r,o,n)}\n\n ${Jr(u)}\n\n ${a.mainStart()}\n ${a.guardAgainstOutOfBoundsWorkgroupSizes(s)}\n let biasIdx = global_idx % halfChannels;\n let batchIndex = global_idx / halfChannels;\n let inputOffset = biasIdx + batchIndex * halfChannels * 2;\n let valueLeft = input[inputOffset] + bias[biasIdx];\n let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels];\n let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1);\n\n ${n.setByOffset("global_idx","valueLeft * geluRight")}\n }`}},Vi=e=>{fl(e.inputs),e.compute(hl(e.inputs))}});var gl,yl,bt,Wi,Gi,Hi,Li,Fi,qi,Ki,ji,Yi,Zi,Qi=q(()=>{"use strict";ie();_e();be();gl=(e,t,r,o,n,s,u,l,a,p,m,f)=>{let b,_;typeof l=="string"?b=_=(v,A)=>`${l}((${v}),(${A}))`:typeof l=="function"?b=_=l:(b=l.scalar,_=l.vector);let y=F("outputData",m,o.length,4),$=M("aData",a,t.length,4),I=M("bData",p,r.length,4),C;if(n)if(s){let v=z.size(t)===1,A=z.size(r)===1,T=t.length>0&&t[t.length-1]%4===0,D=r.length>0&&r[r.length-1]%4===0;v||A?C=y.setByOffset("global_idx",_(v?`${$.type.value}(${$.getByOffset("0")}.x)`:$.getByOffset("global_idx"),A?`${I.type.value}(${I.getByOffset("0")}.x)`:I.getByOffset("global_idx"))):C=`\n let outputIndices = ${y.offsetToIndices("global_idx * 4u")};\n let offsetA = ${$.broadcastedIndicesToOffset("outputIndices",y)};\n let offsetB = ${I.broadcastedIndicesToOffset("outputIndices",y)};\n ${y.setByOffset("global_idx",_(u||T?$.getByOffset("offsetA / 4u"):`${$.type.value}(${$.getByOffset("offsetA / 4u")}[offsetA % 4u])`,u||D?I.getByOffset("offsetB / 4u"):`${I.type.value}(${I.getByOffset("offsetB / 4u")}[offsetB % 4u])`))}\n `}else C=y.setByOffset("global_idx",_($.getByOffset("global_idx"),I.getByOffset("global_idx")));else{if(!s)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let v=(A,T,D="")=>{let U=`aData[indexA${T}][componentA${T}]`,V=`bData[indexB${T}][componentB${T}]`;return`\n let outputIndices${T} = ${y.offsetToIndices(`global_idx * 4u + ${T}u`)};\n let offsetA${T} = ${$.broadcastedIndicesToOffset(`outputIndices${T}`,y)};\n let offsetB${T} = ${I.broadcastedIndicesToOffset(`outputIndices${T}`,y)};\n let indexA${T} = offsetA${T} / 4u;\n let indexB${T} = offsetB${T} / 4u;\n let componentA${T} = offsetA${T} % 4u;\n let componentB${T} = offsetB${T} % 4u;\n ${A}[${T}] = ${D}(${b(U,V)});\n `};m===9?C=`\n var data = vec4(0);\n ${v("data",0,"u32")}\n ${v("data",1,"u32")}\n ${v("data",2,"u32")}\n ${v("data",3,"u32")}\n outputData[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:C=`\n ${v("outputData[global_idx]",0)}\n ${v("outputData[global_idx]",1)}\n ${v("outputData[global_idx]",2)}\n ${v("outputData[global_idx]",3)}\n `}return`\n ${e.registerUniform("vec_size","u32").declareVariables($,I,y)}\n\n ${f??""}\n\n ${e.mainStart()}\n ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}\n ${C}\n }`},yl=(e,t,r,o,n,s,u=r.dataType)=>{let l=!z.areEqual(r.dims,o.dims),a=r.dims,p=z.size(r.dims),m=!1,f=!1,b=[l];if(l){let _=mt.calcShape(r.dims,o.dims,!1);if(!_)throw new Error("Can\'t perform binary op on the given tensors");a=_,p=z.size(a);let y=z.size(r.dims)===1,$=z.size(o.dims)===1,I=r.dims.length>0&&r.dims[r.dims.length-1]%4===0,C=o.dims.length>0&&o.dims[o.dims.length-1]%4===0;b.push(y),b.push($),b.push(I),b.push(C);let v=1;for(let A=1;A_.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:_=>gl(_,r.dims,o.dims,a,m,l,f,n,r.dataType,o.dataType,u,s),getRunData:()=>({outputs:[{dims:a,dataType:u}],dispatchGroup:{x:Math.ceil(p/64/4)},programUniforms:[{type:12,data:Math.ceil(z.size(a)/4)},...j(r.dims,o.dims,a)]})}},bt=(e,t,r,o,n,s)=>{e.compute(yl(t,n??"",e.inputs[0],e.inputs[1],r,o,s))},Wi=e=>{bt(e,"Add",(t,r)=>`${t}+${r}`)},Gi=e=>{bt(e,"Div",(t,r)=>`${t}/${r}`)},Hi=e=>{bt(e,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4(${t}==${r})`},void 0,void 0,9)},Li=e=>{bt(e,"Mul",(t,r)=>`${t}*${r}`)},Fi=e=>{let t=M("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;bt(e,"Pow",{scalar:(o,n)=>`pow_custom(${o},${n})`,vector:(o,n)=>`pow_vector_custom(${o},${n})`},`\n fn pow_custom(a : ${t}, b : ${t}) -> ${t} {\n if (b == ${t}(0.0)) {\n return ${t}(1.0);\n } else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) {\n return ${t}(pow(f32(a), f32(b))); // NaN\n }\n return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t==="i32"?"round":""}(pow(f32(abs(a)), f32(b))));\n }\n fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>) -> vec4<${t}> {\n // TODO: implement vectorized pow\n return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w));\n }\n `)},qi=e=>{bt(e,"Sub",(t,r)=>`${t}-${r}`)},Ki=e=>{bt(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4(${t}>${r})`},void 0,void 0,9)},ji=e=>{bt(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4(${t}<${r})`},void 0,void 0,9)},Yi=e=>{bt(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4(${t}>=${r})`},void 0,void 0,9)},Zi=e=>{bt(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4(${t}<=${r})`},void 0,void 0,9)}});var wl,vl,$l,_l,Xi,Ji,es=q(()=>{"use strict";ie();_e();je();be();wl=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let r=0,o=e[r],n=o.dataType,s=o.dims.length;e.forEach((u,l)=>{if(l!==r){if(u.dataType!==n)throw new Error("input tensors should be one type");if(u.dims.length!==s)throw new Error("input tensors should have the same shape");u.dims.forEach((a,p)=>{if(p!==t&&a!==o.dims[p])throw new Error("non concat dimensions must match")})}})},vl=(e,t)=>`\n fn calculateInputIndex(index: u32) -> u32 {\n let sizeInConcatAxis = array(${t});\n for (var i: u32 = 0u; i < ${e}; i += 1u ) {\n if (index < sizeInConcatAxis[i]) {\n return i;\n }\n }\n return ${e}u;\n }`,$l=(e,t)=>{let r=e.length,o=[];for(let n=0;n{let n=z.size(r),s=new Array(e.length),u=new Array(e.length),l=0,a=[],p=[],m=[{type:12,data:n}];for(let $=0;$`uniforms.sizeInConcatAxis${$}`).join(","),y=$=>`\n\n ${(()=>{$.registerUniform("outputSize","u32");for(let I=0;I(${_});\n ${b} -= sizeInConcatAxis[inputIndex - 1u];\n }\n\n ${$l(u,f)}\n }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:r,dataType:o}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:m}),getShaderSource:y}},Xi=(e,t)=>{let r=e.inputs,o=r[0].dims,n=z.normalizeAxis(t.axis,o.length);wl(r,n);let s=o.slice();s[n]=r.reduce((l,a)=>l+(a.dims.length>n?a.dims[n]:0),0);let u=r.filter(l=>z.size(l.dims)>0);e.compute(_l(u,n,s,r[0].dataType),{inputs:u})},Ji=e=>$e({axis:e.axis})});var ut,dt,lt,tn,Ot=q(()=>{"use strict";ie();_e();ut=(e,t,r="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},dt=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},lt=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},tn=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[r,o]=e?.activation_params||[.2,.5];return{activation:t,alpha:r,beta:o}}else if(t==="Clip"){let[r,o]=e?.activation_params||[Fr,qr];return{activation:t,clipMax:o,clipMin:r}}else if(t==="LeakyRelu"){let[r]=e?.activation_params||[.01];return{activation:t,alpha:r}}return{activation:t}}});var Ze,rn,nn=q(()=>{"use strict";Ze=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},rn=e=>`\n ${e?"value = value + getBiasByOutputCoords(coords);":""}\n `});var on,qn=q(()=>{"use strict";on=e=>`\nfn getIndexFromCoords4D(coords : vec4, shape : vec4) -> i32 {\n return dot(coords, vec4(\n shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1));\n}\nfn getOutputIndexFromCoords(coords : vec4) -> i32 {\n return dot(coords, vec4(\n i32(${e}.x), i32(${e}.y), i32(${e}.z), 1));\n}\n`});var xl,Sl,gr,ts,Cl,yr,Il,an,br=q(()=>{"use strict";ie();_e();be();Ot();nn();xl=(e,t)=>e?`\n mm_Asub[inputRow][inputCol] = mm_readA(batch,\n kStart + inputRow,\n globalRowStart / innerElementSize + inputCol${t?", batchIndices":""});\n `:`\n mm_Asub[inputRow][inputCol] = mm_readA(batch,\n globalRow + innerRow,\n kStart / innerElementSize + inputCol${t?", batchIndices":""});\n `,Sl=(e,t)=>e?`\n let ACached0 = mm_Asub[k * innerElementSize][localRow];\n let ACached1 = mm_Asub[k * innerElementSize + 1][localRow];\n let ACached2 = mm_Asub[k * innerElementSize + 2][localRow];\n ${t===3?"":"let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"}\n for (var i = 0; i < rowPerThread; i = i + 1) {\n acc[i] = BCached0 * ACached0[i] + acc[i];\n acc[i] = BCached1 * ACached1[i] + acc[i];\n acc[i] = BCached2 * ACached2[i] + acc[i];\n ${t===3?"":"acc[i] = BCached3 * ACached3[i] + acc[i];"}\n }`:`\n for (var i = 0; i < rowPerThread; i = i + 1) {\n let ACached = mm_Asub[tileRow + i][k];\n acc[i] = BCached0 * ACached.x + acc[i];\n acc[i] = BCached1 * ACached.y + acc[i];\n acc[i] = BCached2 * ACached.z + acc[i];\n ${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"}\n }`,gr=(e,t,r="f32",o,n=!1,s=32,u=!1,l=32)=>{let a=t[1]*e[1],p=t[0]*e[0],m=n?a:s,f=n?s:a,b=m/t[0],_=s/t[1];if(!((n&&b===4&&e[1]===4||!n&&(b===3||b===4))&&m%t[0]===0&&s%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${n} is true, innerElementSize ${b} and workPerThread[1] ${e[1]} must be 4.\n Otherwise, innerElementSize ${b} must be 3 or 4.\n tileAWidth ${m} must be divisible by workgroupSize[0]${t[0]}. tileInner ${s} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return`\nvar mm_Asub: array, ${m/b}>, ${f}>;\nvar mm_Bsub: array, ${p/e[0]}>, ${s}>;\n\nconst rowPerThread = ${e[1]};\nconst colPerThread = ${e[0]};\nconst innerElementSize = ${b};\nconst tileInner = ${s};\n\n@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]})\nfn main(@builtin(local_invocation_id) localId : vec3,\n @builtin(global_invocation_id) globalId : vec3,\n @builtin(workgroup_id) workgroupId : vec3) {\n let localRow = i32(localId.y);\n let tileRow = localRow * rowPerThread;\n let tileCol = i32(localId.x);\n\n let globalRow =i32(globalId.y) * rowPerThread;\n let globalCol = i32(globalId.x);\n let batch = ${u?"0":"i32(globalId.z)"};\n ${o?`let batchIndices = ${o.offsetToIndices("u32(batch)")};`:""}\n let globalRowStart = i32(workgroupId.y) * ${a};\n\n let num_tiles = ${u?`${Math.ceil(l/s)}`:"(uniforms.dim_inner - 1) / tileInner + 1"};\n var kStart = ${u?`i32(globalId.z) * ${l}`:"0"};\n\n var acc: array, rowPerThread>;\n\n // Loop over shared dimension.\n let tileRowB = localRow * ${_};\n for (var t = 0; t < num_tiles; t = t + 1) {\n // Load one tile of A into local memory.\n for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {\n let inputRow = tileRow + innerRow;\n let inputCol = tileCol;\n ${xl(n,o)}\n }\n\n // Load one tile of B into local memory.\n for (var innerRow = 0; innerRow < ${_}; innerRow = innerRow + 1) {\n let inputRow = tileRowB + innerRow;\n let inputCol = tileCol;\n mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${o?", batchIndices":""});\n }\n kStart = kStart + tileInner;\n workgroupBarrier();\n\n // Compute acc values for a single thread.\n for (var k = 0; k < tileInner / innerElementSize; k = k + 1) {\n let BCached0 = mm_Bsub[k * innerElementSize][tileCol];\n let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol];\n let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol];\n ${b===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"}\n\n ${Sl(n,b)}\n }\n\n workgroupBarrier();\n }\n\n for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {\n mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]);\n }\n}`},ts=(e,t)=>e?`\n mm_Asub[inputRow][inputCol] = mm_readA(batch,\n kStart + inputRow,\n globalRowStart + inputCol${t?", batchIndices":""});\n `:`\n mm_Asub[inputRow][inputCol] = mm_readA(batch,\n globalRowStart + inputRow,\n kStart + inputCol${t?", batchIndices":""});\n `,Cl=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",yr=(e,t,r="f32",o,n=!1,s=32,u=!1,l=32,a=!1)=>{let p=e[1]*t[1],m=e[0]*t[0],f=n?p:s,b=n?s:p;if(!(b%t[1]===0&&f%t[0]===0&&s%t[1]===0))throw new Error(`tileAHight ${b} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${f} must be divisible by workgroupSize[0]${t[0]}, tileInner ${s} must be divisible by workgroupSize[1]${t[1]}`);let _=b/t[1],y=f/t[0],$=s/t[1],I=a?`\n let localRow = i32(localId.y);\n let localCol = i32(localId.x);\n let globalRowStart = i32(workgroupId.y) * ${p};\n let globalColStart = i32(workgroupId.x) * ${m};\n\n // Loop over shared dimension.\n for (var t = 0; t < num_tiles; t = t + 1) {\n // Load one tile of A into local memory.\n for (var inputRow = localRow; inputRow < ${b}; inputRow = inputRow + ${t[1]}) {\n for (var inputCol = localCol; inputCol < ${f}; inputCol = inputCol + ${t[0]}) {\n ${ts(n,o)}\n }\n }\n // Load one tile of B into local memory.\n for (var inputRow = localRow; inputRow < ${s}; inputRow = inputRow + ${t[1]}) {\n for (var inputCol = localCol; inputCol < ${m}; inputCol = inputCol + ${t[0]}) {\n mm_Bsub[inputRow][inputCol] = mm_readB(batch,\n kStart + inputRow,\n globalColStart + inputCol${o?", batchIndices":""});\n }\n }\n kStart = kStart + tileInner;\n workgroupBarrier();\n\n // Compute acc values for a single thread.\n var BCached : array<${r}, colPerThread>;\n for (var k = 0; k < tileInner; k = k + 1) {\n for (var inner = 0; inner < colPerThread; inner = inner + 1) {\n BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}];\n }\n for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {\n let ACached = ${n?`mm_Asub[k][localRow + innerRow * ${t[1]}];`:`mm_Asub[localRow + innerRow * ${t[1]}][k];`}\n for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {\n acc[innerRow][innerCol] = acc[innerRow][innerCol] +\n ACached * BCached[innerCol];\n }\n }\n }\n workgroupBarrier();\n }\n for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {\n let gRow = globalRowStart + localRow + innerRow * ${t[1]};\n for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {\n let gCol = globalColStart + localCol + innerCol * ${t[0]};\n mm_write(batch, gRow, gCol, acc[innerRow][innerCol]);\n }\n }\n `:`\nlet tileRow = i32(localId.y) * rowPerThread;\nlet tileCol = i32(localId.x) * colPerThread;\n\nlet globalRow = i32(globalId.y) * rowPerThread;\nlet globalCol = i32(globalId.x) * colPerThread;\nlet globalRowStart = i32(workgroupId.y) * ${p};\n\nlet tileRowA = i32(localId.y) * ${_};\nlet tileColA = i32(localId.x) * ${y};\nlet tileRowB = i32(localId.y) * ${$};\n// Loop over shared dimension.\nfor (var t = 0; t < num_tiles; t = t + 1) {\n // Load one tile of A into local memory.\n for (var innerRow = 0; innerRow < ${_}; innerRow = innerRow + 1) {\n for (var innerCol = 0; innerCol < ${y}; innerCol = innerCol + 1) {\n let inputRow = tileRowA + innerRow;\n let inputCol = tileColA + innerCol;\n ${ts(n,o)}\n }\n }\n\n // Load one tile of B into local memory.\n for (var innerRow = 0; innerRow < ${$}; innerRow = innerRow + 1) {\n for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {\n let inputRow = tileRowB + innerRow;\n let inputCol = tileCol + innerCol;\n mm_Bsub[inputRow][inputCol] = mm_readB(batch,\n kStart + inputRow,\n globalCol + innerCol${o?", batchIndices":""});\n }\n }\n kStart = kStart + tileInner;\n workgroupBarrier();\n\n // Compute acc values for a single thread.\n var BCached : array<${r}, colPerThread>;\n for (var k = 0; k < tileInner; k = k + 1) {\n for (var inner = 0; inner < colPerThread; inner = inner + 1) {\n BCached[inner] = mm_Bsub[k][tileCol + inner];\n }\n\n for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {\n ${Cl(n)}\n for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {\n acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol];\n }\n }\n }\n\n workgroupBarrier();\n}\n\nfor (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {\n for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {\n mm_write(batch, globalRow + innerRow, globalCol + innerCol,\n acc[innerRow][innerCol]);\n }\n}\n`;return`\n var mm_Asub : array, ${b}>;\n var mm_Bsub : array, ${s}>;\n const rowPerThread = ${e[1]};\n const colPerThread = ${e[0]};\n const tileInner = ${s};\n\n@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]})\nfn main(@builtin(local_invocation_id) localId : vec3,\n @builtin(global_invocation_id) globalId : vec3,\n @builtin(workgroup_id) workgroupId : vec3) {\n let batch = ${u?"0":"i32(globalId.z)"};\n ${o?`let batchIndices = ${o.offsetToIndices("u32(batch)")};`:""}\n let num_tiles = ${u?`${Math.ceil(l/s)}`:"(uniforms.dim_inner - 1) / tileInner + 1"};\n var kStart = ${u?`i32(globalId.z) * ${l}`:"0"};\n\n var acc : array, rowPerThread>;\n\n // Without this initialization strange values show up in acc.\n for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {\n for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {\n acc[innerRow][innerCol] = 0.0;\n }\n }\n ${I}\n }\n`},Il=(e,t,r,o,n,s=!1)=>{let[u,l,a]=n,[p,m,f,b]=o,_=jt(u,a),y=jt(l,a),$=Pe(o[0].type.tensor),I=()=>{let A=m.rank,T=p.rank,D=`var aIndices: ${m.type.indices};`;for(let U=A-2-1,V=T-1;U>=0;U--,V--)D+=`\naIndices[${U}] = ${T>1?`batchIndices[${V}]`:"batchIndices"};`;return _.forEach(U=>{D+=`\naIndices[${U}] = 0;`}),D+=`\naIndices[${A-2}] = u32(row);\n aIndices[${A-1}] = u32(colIn);`,D},C=()=>{let A=f.rank,T=p.rank,D=`var bIndices: ${f.type.indices};`;for(let U=A-2-1,V=T-1;U>=0;U--,V--)D+=`\nbIndices[${U}] = ${T>1?`batchIndices[${V}]`:"batchIndices"};`;return y.forEach(U=>{D+=`\nbIndices[${U}] = 0;`}),D+=`\nbIndices[${A-2}] = u32(row);\n bIndices[${A-1}] = u32(colIn);`,D};return`\n fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${p.type.indices}) -> ${Ze(e,$)} {\n var value = ${Ze(e,$)}(0.0);\n let col = colIn * ${e};\n if(row < uniforms.dim_a_outer && col < uniforms.dim_inner)\n {\n ${I()}\n value = ${m.getByIndices("aIndices")};\n }\n return value;\n }\n\n fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${p.type.indices}) -> ${Ze(e,$)} {\n var value = ${Ze(e,$)}(0.0);\n let col = colIn * ${e};\n if(row < uniforms.dim_inner && col < uniforms.dim_b_outer)\n {\n ${C()}\n value = ${f.getByIndices("bIndices")};\n }\n return value;\n }\n\n fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${Ze(e,$)}) {\n let col = colIn * ${e};\n if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) {\n var value = valueIn;\n let coords = vec3(batch, row, colIn);\n ${t?`value = value + ${s?"bias[colIn]":`${Ze(e,$)}(bias[row])`};`:""}\n ${r}\n ${b.setByIndices("vec3(coords)","value")}\n }\n }\n `},an=(e,t,r,o,n=!1)=>{let s=e[0].dims,u=e[1].dims,l=s.slice(0,-2),a=u.slice(0,-2),p=o?o.slice(0,-2):r.slice(0,-2),m=z.size(p),f=s[s.length-2],b=s[s.length-1],_=u[u.length-1],y=b%4===0&&_%4===0,$=f<=8?[4,1,1]:[4,4,1],I=[8,8,1],C=[Math.ceil(_/I[0]/$[0]),Math.ceil(f/I[1]/$[1]),Math.ceil(m/I[2]/$[2])],v=y?4:1,A=[...l,f,b/v],T=A.length,D=[...a,b,_/v],U=D.length,V=[m,f,_/v],H=[{type:6,data:f},{type:6,data:_},{type:6,data:b}];dt(t,H),H.push(...j(p,A,D));let R=["rank","rank"],L=e.length>2;L&&(H.push(...j(e[2].dims)),R.push("rank")),H.push(...j(V));let pe=Ie=>{let we=p.length,ne=jr("batchDims",e[0].dataType,we,1),ze=Pe(e[0].dataType),Q=M("a",e[0].dataType,T,v),xe=M("b",e[1].dataType,U,v),me=F("result",e[0].dataType,V.length,v),ue=[Q,xe];if(L){let G=n?v:1;ue.push(M("bias",e[2].dataType,e[2].dims.length,G))}let se=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];lt(t,se);let he=Pe(me.type.tensor),Ae=ut(t,me.type.value,he),He=Il(v,L,Ae,[ne,Q,xe,me],[l,a,p],n);return`\n ${Ie.registerUniforms(se).registerInternalVariables(ne).declareVariables(...ue,me)}\n ${He}\n ${y?gr($,I,ze,ne):yr($,I,ze,ne)}\n `};return{name:"MatMul",shaderCache:{hint:`${$};${t.activation};${y};${n}`,inputDependencies:R},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:C[0],y:C[1],z:C[2]},programUniforms:H}),getShaderSource:pe}}});var Al,rs,ns=q(()=>{"use strict";ie();Pt();be();Ot();nn();qn();br();Al=(e,t,r,o,n=!1,s,u=4,l=4,a=4,p="f32")=>{let m=L=>{switch(L){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${p}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${L} is not supported.`)}},f=L=>{switch(L){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${L} is not supported.`)}},b=e?`\n let coord = vec4(batch, xRow, xCol, xCh);\n `:`\n let coord = vec4(batch, xCh, xRow, xCol);\n `,_=e?`\n let coords = vec4(\n batch,\n row / outWidth,\n row % outWidth,\n col);\n `:`\n let coords = vec4(\n batch,\n row,\n col / outWidth,\n col % outWidth);\n `,y=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",$=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",I=e?"row":"col",C=e?"col":"row",v=`\n let inChannels = i32(uniforms.w_shape[2]);\n let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};\n let outRow = ${I} / outWidth;\n let outCol = ${I} % outWidth;\n\n let WRow = ${C} / (i32(uniforms.w_shape[1]) * inChannels);\n let WCol = ${C} / inChannels % i32(uniforms.w_shape[1]);\n let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0];\n let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1];\n let xCh = ${C} % inChannels;\n var resData = ${Ze(u,p)}(0.0);\n // The bounds checking is always needed since we use it to pad zero for\n // the \'same\' padding type.\n if (xRow >= 0 && xRow < ${y} && xCol >= 0 && xCol < ${$}) {\n ${b}\n let xIndex = getIndexFromCoords4D(coord, vec4(uniforms.x_shape));\n ${m(u)}\n }\n return resData;`,A=e?t&&o?`\n let col = colIn * ${u};\n ${v}`:`\n let col = colIn * ${u};\n if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) {\n ${v}\n }\n return ${Ze(u,p)}(0.0);`:o&&r?`\n let col = colIn * ${u};\n ${v}`:`\n let col = colIn * ${u};\n if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {\n ${v}\n }\n return ${Ze(u,p)}(0.0);`,T=`${f(l)}`,D=Ze(a,p),U=e?Ze(u,p):Ze(l,p),V=e?Ze(l,p):Ze(u,p),H=ut(s,D,p);return`\n fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${U} {\n ${e?A:T}\n }\n\n fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${V} {\n ${e?T:A}\n }\n\n fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${D}) {\n let col = colIn * ${a};\n if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer)\n {\n var value = valueIn;\n let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};\n ${_}\n ${rn(n)}\n ${H}\n setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value);\n }\n }`},rs=(e,t,r,o,n,s,u,l)=>{let a=t.format==="NHWC",p=a?e[0].dims[3]:e[0].dims[1],m=r[0],f=a?r[2]:r[3],b=a?r[1]:r[2],_=a?r[3]:r[1],y=a&&(p%4===0||p%3===0)&&_%4===0,$=a?_:f*b,I=a?f*b:_,C=[8,8,1],v=o<=8?[4,1,1]:[4,4,1],A=[Math.ceil($/C[0]/v[0]),Math.ceil(I/C[1]/v[1]),Math.ceil(m/C[2]/v[2])];De("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${A}`);let T=y?a&&p%4!==0?3:4:1,D=C[1]*v[1],U=C[0]*v[0],V=Math.max(C[0]*T,C[1]),H=o%D===0,R=n%U===0,L=s%V===0,pe=y?[T,4,4]:[1,1,1],Ie=[{type:6,data:o},{type:6,data:n},{type:6,data:s},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];dt(t,Ie),Ie.push(...j(e[0].dims,e[1].dims));let we=["rank","rank"];u&&(Ie.push(...j(e[2].dims)),we.push("rank")),Ie.push(...j(r));let ne=ze=>{let Q=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];lt(t,Q);let xe=y?4:1,me=Pe(e[0].dataType),ue=`\n fn setOutputAtIndex(flatIndex : i32, value : ${y?`vec4<${me}>`:me}) {\n result[flatIndex] = ${y?`vec4<${me}>`:me}(value);\n }\n fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${y?`vec4<${me}>`:me}) {\n let flatIndex = getOutputIndexFromCoords(vec4(d0, d1, d2, d3));\n setOutputAtIndex(flatIndex ${y?"/ 4":""}, value);\n }`,se=M("x",e[0].dataType,e[0].dims.length,T===3?1:T),he=M("w",e[1].dataType,e[1].dims.length,xe),Ae=[se,he],He=F("result",e[0].dataType,r.length,xe);if(u){let G=M("bias",e[2].dataType,e[2].dims.length,xe);Ae.push(G),ue+=`\n fn getBiasByOutputCoords(coords : vec4) -> ${y?`vec4<${me}>`:me} {\n return bias[coords.${a?"w":"y"}${y?"/ 4":""}];\n }`}return`\n ${on("uniforms.result_strides")}\n //struct Uniforms { xShape : vec4, wShape : vec4, outShape : vec4,\n // outShapeStrides: vec3, filterDims : vec2, pad : vec2, stride : vec2,\n // dilation : vec2, dimAOuter : i32, dimBOuter : i32, dimInner : i32 };\n ${ze.registerUniforms(Q).declareVariables(...Ae,He)}\n ${ue}\n ${Al(a,H,R,L,u,t,pe[0],pe[1],pe[2],me)}\n ${y?gr(v,C,me,void 0,!a,V):yr(v,C,me,void 0,!a,V,!1,void 0,l)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${T};${y};${H};${R};${L};${D};${U};${V}`,inputDependencies:we},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:A[0],y:A[1],z:A[2]},programUniforms:Ie}),getShaderSource:ne}}});var Kn,os,as=q(()=>{"use strict";ie();_e();be();jn();Ot();Kn=(e,t,r)=>{let o=e.length>2,n=o?"value += b[output_channel];":"",s=e[0].dims,u=e[1].dims,l=u[0]/t.group,a=t.format==="NHWC",p=sn(s,u,t.dilations,t.pads,t.strides,a),m=z.size(p),f=[{type:12,data:m},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:l}];dt(t,f),f.push(...j(s,u,p));let b=["rank","rank"];o&&(f.push(...j(e[2].dims)),b.push("rank")),f.push(...j(p));let _=y=>{let $=F("output",e[0].dataType,p.length),I=Pe($.type.tensor),C=ut(t,$.type.value,I),v=M("x",e[0].dataType,s.length),A=M("w",e[1].dataType,u.length),T=[v,A];o&&T.push(M("b",e[2].dataType,e[2].dims));let D=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];return lt(t,D),`\n ${y.registerUniforms(D).declareVariables(...T,$)}\n\n ${y.mainStart()}\n ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}\n\n let outputIndices = ${$.offsetToIndices("global_idx")};\n let batch: u32 = outputIndices[0];\n let output_channel: u32 = outputIndices[${a?3:1}];\n let xRCCorner: vec2 = vec2(outputIndices[${a?1:2}], outputIndices[${a?2:3}]) * uniforms.strides - uniforms.pads;\n let group_id: u32 = output_channel / uniforms.output_channels_per_group;\n\n var value: ${$.type.value} = ${$.type.value}(0);\n for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) {\n let input_channel = group_id * uniforms.w_shape[1] + wInChannel;\n for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) {\n let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0];\n\n if (xHeight < 0u || xHeight >= uniforms.x_shape[${a?1:2}]) {\n continue;\n }\n\n for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) {\n let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1];\n if (xWidth < 0u || xWidth >= uniforms.x_shape[${a?2:3}]) {\n continue;\n }\n\n let xVal = ${a?v.get("batch","xHeight","xWidth","input_channel"):v.get("batch","input_channel","xHeight","xWidth")};\n let wVal = ${A.get("output_channel","wInChannel","wHeight","wWidth")};\n value += xVal*wVal;\n }\n }\n }\n ${n}\n ${C}\n ${$.setByOffset("global_idx","value")}\n }`};return{name:"GroupedConv",shaderCache:{hint:t.cacheKey,inputDependencies:b},getRunData:()=>({outputs:[{dims:r?r(p):p,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:f}),getShaderSource:_}},os=(e,t,r)=>{let o=e.length>2,n=Me(r[3]),s=Me(r[2]),u=z.size(r)/n/s,l=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/n],a=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/n],p=[r[0],r[1],r[2],r[3]/n],m=[{type:12,data:u},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];dt(t,m),m.push(...j(l,a,p));let f=(s-1)*t.strides[1]+a[1],b=_=>{let y=F("output",e[0].dataType,p.length,n),$=Pe(y.type.tensor),I=ut(t,y.type.value,$),C=M("x",e[0].dataType,l.length,n),v=M("w",e[1].dataType,a.length,n),A=[C,v];o&&A.push(M("b",e[2].dataType,e[2].dims,n));let T=o?"value += b[output_channel];":"",D=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return lt(t,D),`\n ${_.registerUniforms(D).declareVariables(...A,y)}\n ${_.mainStart()}\n ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}\n let width0 = uniforms.output_shape[3];\n let output_channel = global_idx % width0;\n var index1 = global_idx / width0;\n let width1 = uniforms.output_shape[2] / ${s}u;\n let col = (index1 % width1) * ${s}u;\n index1 = index1 / width1;\n let row = index1 % uniforms.output_shape[1];\n let batch = index1 / uniforms.output_shape[1];\n\n let x_corner = vec2(i32(row), i32(col)) * uniforms.strides - uniforms.pads;\n\n var x_vals: array<${C.type.value}, ${f}>;\n var values: array<${y.type.value}, ${s}>;\n let input_channel = output_channel;\n // Use constant instead of uniform can give better performance for w\'s height/width.\n for (var w_height: u32 = 0u; w_height < ${a[0]}; w_height++) {\n let x_height = x_corner.x + i32(w_height);\n if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) {\n for (var i = 0; i < ${f}; i++) {\n let x_width = x_corner.y + i;\n if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) {\n x_vals[i] = ${C.get("batch","u32(x_height)","u32(x_width)","input_channel")};\n } else {\n x_vals[i] = ${C.type.value}(0);\n }\n }\n for (var w_width: u32 = 0u; w_width < ${a[1]}; w_width++) {\n let w_val = ${v.get("w_height","w_width","0","output_channel")};\n for (var i = 0u; i < ${s}u; i++) {\n values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]);\n }\n }\n }\n }\n\n for (var i = 0u; i < ${s}u; i++) {\n var value = values[i];\n ${T}\n ${I}\n ${y.set("batch","row","col + i","output_channel","value")};\n }\n }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${n};${s};${f};${a[0]};${a[1]}`,inputDependencies:o?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:m}),getShaderSource:b}}});var Yn,Tl,is,Zn=q(()=>{"use strict";ie();_e();br();be();Ot();Yn=(e,t,r,o,n=!1)=>{let s=e[0].dims,u=e[1].dims,l=s[s.length-2],a=u[u.length-1],p=s[s.length-1],m=Me(a),f=Me(p),b=Me(l),_=z.size(r)/m/b,y=e.length>2,$=o?o.slice(0,-2):r.slice(0,-2),C=[z.size($),l,a],v=[{type:12,data:_},{type:12,data:l},{type:12,data:a},{type:12,data:p}];dt(t,v),v.push(...j($,s,u)),y&&v.push(...j(e[2].dims)),v.push(...j(C));let A=T=>{let D=jr("batch_dims",e[0].dataType,$.length),U=M("a",e[0].dataType,s.length,f),V=M("b",e[1].dataType,u.length,m),H=F("output",e[0].dataType,C.length,m),R=Pe(H.type.tensor),L=ut(t,H.type.value,R),pe=[U,V],Ie="";if(y){let se=n?m:1;pe.push(M("bias",e[2].dataType,e[2].dims.length,se)),Ie=`${n?`value += bias[col / ${se}];`:`value += ${H.type.value}(bias[row + i]);`}`}let we=s.slice(0,-2),ne=u.slice(0,-2),ze=jt(we,$),Q=jt(ne,$),xe=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];lt(t,xe);let me=(se,he)=>{let Ae=se.rank,He=se.name;if(Ae===2)return`var ${He}_indices = ${se.type.indices}(0u, 0u);`;let G=D.rank,J=`var ${He}_indices: ${se.type.indices};`;for(let Se=Ae-2-1,Qe=G-1;Se>=0;Se--,Qe--)J+=`\n${He}_indices[${Se}] = ${G>1?`batch_indices[${Qe}]`:"batch_indices"};`;return he.forEach(Se=>{J+=`\n${He}_indices[${Se}] = 0;`}),J+=`${He}_indices[${Ae-2}] = 0u;\n ${He}_indices[${Ae-1}] = 0u;`,J},ue=()=>{let se=`var a_data: ${U.type.value};`;for(let he=0;he;\n for (var k: u32 = 0u; k < uniforms.K; k = k + ${f}) {\n ${ue()}\n }\n for (var i = 0u; i < ${b}u; i++) {\n var value = values[i];\n ${Ie}\n ${L}\n let cur_indices = ${H.type.indices}(batch, row + i, col);\n let offset = ${H.indicesToOffset("cur_indices")};\n ${H.setByOffset(`offset / ${m}`,"value")};\n }\n }\n `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${m};${f};${b};${n}`,inputDependencies:y?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(_/64)},programUniforms:v}),getShaderSource:A}},Tl=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},is=e=>{Tl(e.inputs);let t=mt.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can\'t use matmul on the given tensors");let r=t[t.length-1],o=e.inputs[0].dims[e.inputs[0].dims.length-1];r<8&&o<8?e.compute(Yn(e.inputs,{activation:""},t)):e.compute(an(e.inputs,{activation:""},t))}});var sn,Qn,El,ss,Xn,Pl,Ol,Jn,jn=q(()=>{"use strict";_e();ns();br();as();Ot();Zn();Yt();sn=(e,t,r,o,n,s)=>{let u=e[0],l=e.slice(s?1:2,s?3:4),a=l.length,p=t[0],f=t.slice(2).map((y,$)=>y+(y-1)*(r[$]-1)),_=l.map((y,$)=>y+o[$]+o[$+a]).map((y,$)=>Math.floor((y-f[$]+n[$])/n[$]));return _.splice(0,0,u),_.splice(s?3:1,0,p),_},Qn=[2,3,1,0],El=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support conv 1D and 2D");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],o=e[1].dims[1]*t.group;if(r!==o)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let n=e[0].dims.length-2;if(t.dilations.length!==n)throw new Error(`dilations should be ${n}D`);if(t.strides.length!==n)throw new Error(`strides should be ${n}D`);if(t.pads.length!==n*2)throw new Error(`pads should be ${n*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},ss=(e,t)=>{let r=e.kernelShape.slice();for(let s=2;s{let t=tn(e),r=e.format,o=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],n=e.dilations,s=e.group,u=e.kernel_shape,l=e.pads,a=e.strides,p=e.w_is_const();return{autoPad:o,format:r,dilations:n,group:s,kernelShape:u,pads:l,strides:a,wIsConst:p,...t,cacheKey:`${e.format};${t.activation};`}},Pl=(e,t,r)=>{let o=ss(r,t),n=r.format==="NHWC";if(r.group!==1){if(!e.adapterInfo.isArchitecture("ampere")&&n&&t[1].dims[0]===r.group&&t[1].dims[1]===1&&r.dilations[0]===1&&r.dilations[1]===1){let V=sn(t[0].dims,t[1].dims,r.dilations,o.pads,r.strides,n),H=e.kernelCustomData.wT??e.compute(ot(t[1],Qn),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=H);let R=[t[0],H];t.length===3&&R.push(t[2]),e.compute(os(R,o,V),{inputs:R})}else e.compute(Kn(t,o));return}let s=t.length===3,u=t[0].dims[n?1:2],l=t[0].dims[n?2:3],a=t[0].dims[n?3:1],p=t[1].dims[2],m=t[1].dims[3],f=sn(t[0].dims,t[1].dims,r.dilations,o.pads,r.strides,n),b=f[n?1:2],_=f[n?2:3],y=f[n?3:1],$=n&&p===u&&m===l&&r.pads[0]===0&&r.pads[1]===0;if($||p===1&&m===1&&r.dilations[0]===1&&r.dilations[1]===1&&r.strides[0]===1&&r.strides[1]===1&&r.pads[0]===0&&r.pads[1]===0){let U=f[0],V,H,R,L=[];if(n){let we=e.kernelCustomData.wT??e.compute(ot(t[1],Qn),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];if(r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=we),$){let ne=u*l*a;V=t[0].reshape([1,U,ne]),H=we.reshape([1,ne,y]),R=[1,U,y]}else V=t[0].reshape([U,u*l,a]),H=we.reshape([1,a,y]),R=[U,b*_,y];L.push(V),L.push(H)}else V=t[0].reshape([U,a,u*l]),H=t[1].reshape([1,y,a]),R=[U,y,b*_],L.push(H),L.push(V);s&&L.push(t[2]);let pe=R[2],Ie=L[0].dims[L[0].dims.length-1];pe<8&&Ie<8?e.compute(Yn(L,o,f,R,n),{inputs:L}):e.compute(an(L,o,f,R,n),{inputs:L});return}let I=!0,C=e.kernelCustomData.wT??e.compute(ot(t[1],Qn),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=C);let v=[t[0],C];s&&v.push(t[2]);let A=n?b*_:y,T=n?y:b*_,D=p*m*a;e.compute(rs(v,o,f,A,T,D,s,I),{inputs:v})},Ol=(e,t)=>{let r=t.format==="NHWC",o=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&o.push(e.inputs[2]);let n=[0,t.pads[0],0,t.pads[1]],s=[1].concat(t.strides),u=[1].concat(t.dilations),l=[1].concat(t.kernelShape),a=ss({...t,pads:n,strides:s,dilations:u,kernelShape:l},o);e.compute(Kn(o,a,p=>r?[p[0],p[2],p[3]]:[]))},Jn=(e,t)=>{El(e.inputs,t),e.inputs[0].dims.length===3?Ol(e,t):Pl(e,e.inputs,t)}});var kl,us,ds=q(()=>{"use strict";ie();Pt();be();Ot();nn();qn();br();kl=(e,t=!1,r,o,n=4)=>{let s=C=>{switch(C){case 1:return"return w[getIndexFromCoords4D(coord, vec4(uniforms.w_shape))];";case 4:return`\n let coord1 = vec4(coordX, coordY, col + 1, rowInner);\n let coord2 = vec4(coordX, coordY, col + 2, rowInner);\n let coord3 = vec4(coordX, coordY, col + 3, rowInner);\n let v0 = w[getIndexFromCoords4D(coord, vec4(uniforms.w_shape))];\n let v1 = w[getIndexFromCoords4D(coord1, vec4(uniforms.w_shape))];\n let v2 = w[getIndexFromCoords4D(coord2, vec4(uniforms.w_shape))];\n let v3 = w[getIndexFromCoords4D(coord3, vec4(uniforms.w_shape))];\n return ${o}(v0, v1, v2, v3);\n `;default:throw new Error(`innerElementSize ${C} is not supported.`)}},u=e?`\n let coord = vec4(batch, iXR, iXC, xCh);\n `:`\n let coord = vec4(batch, xCh, iXR, iXC);\n `,l=e?`\n let coords = vec4(\n batch,\n row / outWidth,\n row % outWidth,\n col);\n `:`\n let coords = vec4(\n batch,\n row,\n col / outWidth,\n col % outWidth);\n `,a=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",p=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",m=e?"row":"col",f=e?"col":"row",b=`\n let inChannels = ${e?"i32(uniforms.x_shape[3])":"i32(uniforms.x_shape[1])"};\n let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};\n let outRow = ${m} / outWidth;\n let outCol = ${m} % outWidth;\n\n let WRow = ${f} / (uniforms.filter_dims[1] * inChannels);\n let WCol = ${f} / inChannels % uniforms.filter_dims[1];\n let xR = f32(outRow - uniforms.pads[0] + uniforms.dilations[0] * WRow) / f32(uniforms.strides[0]);\n let xC = f32(outCol - uniforms.pads[1] + uniforms.dilations[1] * WCol) / f32(uniforms.strides[1]);\n if (xR < 0.0 || xR >= f32(${a}) || fract(xR) > 0.0) {\n return ${o}(0.0);\n }\n if (xC < 0.0 || xC >= f32(${p}) || fract(xC) > 0.0) {\n return ${o}(0.0);\n }\n let iXR = i32(xR);\n let iXC = i32(xC);\n let xCh = ${f} % inChannels;\n ${u}\n return x[getIndexFromCoords4D(coord, vec4(uniforms.x_shape))/${n}];`,_=e?`\n let col = colIn * ${n};\n if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) {\n ${b}\n }\n return ${o}(0.0);`:`\n let col = colIn * ${n};\n if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {\n ${b}\n }\n return ${o}(0.0);`,y=`\n let col = colIn * ${n};\n let inChannels = ${e?"i32(uniforms.x_shape[3])":"i32(uniforms.x_shape[1])"};\n let coordX = uniforms.filter_dims[0] - 1 - row / (uniforms.filter_dims[1] * inChannels);\n let coordY = uniforms.filter_dims[1] - 1 - (row / inChannels) % uniforms.filter_dims[1];\n if (${e?"row < uniforms.dim_inner && col < uniforms.dim_b_outer":"row < uniforms.dim_inner && col < uniforms.dim_a_outer"} && coordX >= 0 && coordY >= 0) {\n let rowInner = row % inChannels;\n let coord = vec4(coordX, coordY, col, rowInner);\n ${s(n)}\n }\n return ${o}(0.0);\n `,$=ut(r,o);return`\n fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${o} {\n ${e?_:y}\n }\n\n fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${o} {\n ${e?y:_}\n }\n\n fn mm_write(batch: i32, row : i32, colIn : i32, valueInput : ${o}) {\n let col = colIn * ${n};\n if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) {\n var value = valueInput;\n let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};\n ${l}\n ${rn(t)}\n ${$}\n result[getIndexFromCoords4D(coords, vec4(uniforms.result_shape))/${n}] = value;\n }\n }`},us=(e,t,r,o,n,s,u,l)=>{let a=t.format==="NHWC",p=a?e[0].dims[3]:e[0].dims[1],m=r[0],f=a?r[2]:r[3],b=a?r[1]:r[2],_=a?r[3]:r[1],y=a?p%4===0&&_%4===0:f%4===0&&_%4===0,$=a?_:f*b,I=a?f*b:_,C=y?[8,8,1]:[$<=4||I<=4?4:16,$>4&&I<=4?4:16,1],v=y?[4,4,1]:[$<=4?1:4,$>4&&I<=4?1:4,1],A=[Math.ceil($/C[0]/v[0]),Math.ceil(I/C[1]/v[1]),Math.ceil(m/C[2]/v[2])];De("verbose",()=>`[conv_backprop_mm_webgpu] dispatch = ${A}`);let T=y?4:1,D=Math.max(C[0]*T,C[1]),U=y?4:1,V=[t.kernelShape[a?1:2],t.kernelShape[a?2:3]],H=[V[0]+(t.dilations[0]<=1?0:(V[0]-1)*(t.dilations[0]-1)),V[1]+(t.dilations[1]<=1?0:(V[1]-1)*(t.dilations[1]-1))],R=[H[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),H[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],L=[{type:6,data:o},{type:6,data:n},{type:6,data:s},{type:6,data:t.strides},{type:6,data:t.dilations},{type:6,data:V},{type:6,data:R}];dt(t,L),L.push(...j(e[0].dims,e[1].dims));let pe=["rank","rank"];u&&(L.push(...j(e[2].dims)),pe.push("rank")),L.push(...j(r));let Ie=we=>{let ne=M("x",e[0].dataType,e[0].dims.length,U),ze=M("w",e[1].dataType,e[1].dims.length,1),Q=F("result",e[0].dataType,r.length,U),xe=[ne,ze],me="";if(u){let he=M("bias",e[2].dataType,e[2].dims.length,U);xe.push(he),me+=`\n fn getBiasByOutputCoords(coords : vec4) -> ${he.type.value} {\n return bias[coords.${a?"w":"y"}${y?"/ 4":""}];\n }`}let ue=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"strides",type:"i32",length:2},{name:"dilations",type:"i32",length:2},{name:"filter_dims",type:"i32",length:V.length},{name:"pads",type:"i32",length:R.length}];lt(t,ue);let se=Pe(e[0].dataType,1);if(se!=="f16"&&se!=="f32")throw new Error(`elemType ${se} is not supported.`);return`\n ${on("uniforms.result_strides")}\n ${we.registerUniforms(ue).declareVariables(...xe,Q)};\n ${me}\n ${kl(a,u,t,ne.type.value,T)}\n ${y?gr(v,C,se,void 0,!a,D):yr(v,C,se,void 0,!a,D,!1,void 0,l)}`};return{name:"Conv2DTransposeMatMul",shaderCache:{hint:`${t.cacheKey};${v};${C};${y}`,inputDependencies:pe},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:A[0],y:A[1],z:A[2]},programUniforms:L}),getShaderSource:Ie}}});var Rl,eo,ls=q(()=>{"use strict";ie();Pt();_e();be();Rl=(e,t,r,o,n,s=!1,u,l,a=!1)=>{let p=a?1:2,m=a?2:3,f=a?3:1,b=s?2:1,_=`\n fn setOutputAtIndex(flatIndex : u32, value : ${s?`vec4<${u}>`:u}) {\n result[flatIndex] = ${s?`vec4<${u}>`:u}(value);\n }`;o&&(_+=`\n fn getBiasByOutputCoords(coords : vec4) -> ${s?`vec4<${u}>`:u} {\n return bias[coords.${a?"w":"y"}${s?"/ 4":""}];\n }`);let y=s?4:1,$=M("W",t[1].dataType,t[1].dims.length,y),I=M("Dy",t[0].dataType,t[0].dims.length,y),C=[I,$];o&&C.push(M("bias",t[2].dataType,[r[f]].length,y));let v=F("result",t[0].dataType,r.length,y),A=`{\n let batch: u32 = ${n?"global_id.z":"workgroup_id.z"} / uniforms.result_shape[1];\n let r = ${n?"global_id.z":"workgroup_id.z"} % uniforms.result_shape[1];\n let c = ${n?"global_id.y":"workgroup_id.y"} * ${b};\n let d1: u32 = ${n?"global_id.x":"workgroup_id.x"} * 4;\n\n let dyCorner = vec2(i32(r), i32(c)) - vec2(uniforms.pads);\n\n // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).\n // ? = to be determined. : = across all values in that axis.\n var dotProd: array, ${b}>;\n for (var i = 0; i < ${b}; i++) {\n dotProd[i] = vec4<${u}>(0.0);\n }\n for (var wR: u32 = 0; wR < uniforms.filter_dims[0]; wR = wR + 1) {\n var dyR = (${u}(dyCorner.x) + ${u}(wR)) / ${u}(uniforms.strides.x);\n let wRPerm = uniforms.filter_dims[0] - 1 - wR;\n if (dyR < 0.0 || dyR >= ${u}(uniforms.Dy_shape[1]) ||\n fract(dyR) > 0.0 || wRPerm < 0) {\n continue;\n }\n let idyR: u32 = u32(dyR);\n\n for (var wC: u32 = 0; wC < uniforms.filter_dims[1]; wC = wC + 1) {\n let dyC = (${u}(dyCorner.y) + ${u}(wC)) / ${u}(uniforms.strides.y);\n let dyC2 = (${u}(dyCorner.y) + 1.0 + ${u}(wC)) / ${u}(uniforms.strides.y);\n let wCPerm = uniforms.filter_dims[1] - 1 - wC;\n if (wCPerm < 0) {\n continue;\n }\n var bDyCVal = true;\n var bDyCVal2 = true;\n if (dyC < 0.0 || dyC >= ${u}(uniforms.Dy_shape[2]) ||\n fract(dyC) > 0.0) {\n bDyCVal = false;\n }\n if (dyC2 < 0.0 || dyC2 >= ${u}(uniforms.Dy_shape[2]) ||\n fract(dyC2) > 0.0) {\n bDyCVal2 = false;\n }\n\n let idyC: u32 = u32(dyC);\n let idyC2: u32 = u32(dyC2);\n if (bDyCVal && bDyCVal2) {\n let d2Length = uniforms.Dy_shape[3];\n for (var d2 :u32 = 0; d2 < d2Length; d2 = d2 + 4) {\n let wValue0 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1","d2")};\n let wValue1 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")};\n let wValue2 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")};\n let wValue3 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")};\n\n var xValue = ${I.get("batch","idyR","idyC","d2")};\n let tmpval = vec4<${u}>(dot(xValue, wValue0),\n dot(xValue, wValue1),\n dot(xValue, wValue2),\n dot(xValue, wValue3));\n dotProd[0] = dotProd[0] + tmpval;\n\n xValue = ${I.get("batch","idyR","idyC2","d2")};\n\n dotProd[1] = dotProd[1] + vec4<${u}>(dot(xValue, wValue0),\n dot(xValue, wValue1),\n dot(xValue, wValue2),\n dot(xValue, wValue3));\n }\n } else if (bDyCVal) {\n let d2Length = uniforms.Dy_shape[${f}];\n for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) {\n let wValue0 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1","d2")};\n let wValue1 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")};\n let wValue2 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")};\n let wValue3 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")};\n\n var xValue = ${I.get("batch","idyR","idyC","d2")};\n let tmpval = vec4<${u}>(dot(xValue, wValue0),\n dot(xValue, wValue1),\n dot(xValue, wValue2),\n dot(xValue, wValue3));\n dotProd[0] = dotProd[0] + tmpval;\n }\n } else if (bDyCVal2) {\n let d2Length = uniforms.Dy_shape[3];\n for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) {\n let wValue0 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1","d2")};\n let wValue1 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")};\n let wValue2 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")};\n let wValue3 = ${$.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")};\n\n var xValue = ${I.get("batch","idyR","idyC2","d2")};\n let tmpval = vec4<${u}>(dot(xValue, wValue0),\n dot(xValue, wValue1),\n dot(xValue, wValue2),\n dot(xValue, wValue3));\n dotProd[1] = dotProd[1] + tmpval;\n }\n }\n }\n }\n\n for (var i: u32 = 0; i < ${b}; i = i + 1) {\n let value = dotProd[i] + ${o?"bias[c+i]":`vec4<${u}>(0.0)`};\n ${v.set("batch","r","c + i","d1","value")};\n }\n }`,T=`\n let outputIndices = ${v.offsetToIndices("global_idx")};\n let batch = ${v.indicesGet("outputIndices",0)};\n let d1 = ${v.indicesGet("outputIndices",f)};\n let r = ${v.indicesGet("outputIndices",p)};\n let c = ${v.indicesGet("outputIndices",m)};\n let dyCorner = vec2(i32(r), i32(c)) - uniforms.pads;\n let dyRCorner = dyCorner.x;\n let dyCCorner = dyCorner.y;\n let groupId = d1 / uniforms.output_channels_per_group;\n let wOutChannel = d1 - groupId * uniforms.output_channels_per_group;\n // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).\n // ? = to be determined. : = across all values in that axis.\n var dotProd = ${u}(0.0);\n for (var wR: u32 = 0; wR < uniforms.effective_filter_dims.x; wR = wR + 1) {\n if (wR % uniforms.dilations.x != 0) {\n continue;\n }\n let dyR = (${u}(dyRCorner) + ${u}(wR)) / ${u}(uniforms.strides[0]);\n let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x;\n if (dyR < 0.0 || dyR >= ${u}(uniforms.Dy_shape[${p}]) || fract(dyR) > 0.0 ||\n wRPerm < 0) {\n continue;\n }\n let idyR: u32 = u32(dyR);\n\n for (var wC: u32 = 0; wC < uniforms.effective_filter_dims.y; wC = wC + 1) {\n if (wC % uniforms.dilations.y != 0) {\n continue;\n }\n let dyC = (${u}(dyCCorner) + ${u}(wC)) / ${u}(uniforms.strides.y);\n let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y;\n if (dyC < 0.0 || dyC >= ${u}(uniforms.Dy_shape[${m}]) ||\n fract(dyC) > 0.0 || wCPerm < 0) {\n continue;\n }\n let idyC: u32 = u32(dyC);\n var inputChannel = groupId * uniforms.input_channels_per_group;\n for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group; d2 = d2 + 1) {\n let xValue = ${a?I.get("batch","idyR","idyC","inputChannel"):I.get("batch","inputChannel","idyR","idyC")};\n let wValue = ${$.get("inputChannel","wOutChannel","u32(wRPerm)","u32(wCPerm)")};\n dotProd = dotProd + xValue * wValue;\n inputChannel = inputChannel + 1;\n }\n }\n }\n let value = dotProd + ${o?"bias[d1]":`${u}(0.0)`};\n ${v.setByOffset("global_idx","value")};\n `;return`\n ${e.registerUniforms(l).declareVariables(...C,v)}\n ${_}\n\n ${e.mainStart()}\n ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")};\n ${s?A:T}}`},eo=(e,t,r)=>{let o=e.length>2,n=t.outputShape,s=z.size(n),u=[Math.ceil(s/64),1,1];De("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${u}`);let l=t.format==="NHWC",a=["rank","rank"],p=[t.strides[0],t.strides[1]],m=[t.kernelShape[l?1:2],t.kernelShape[l?2:3]],f=[t.dilations[0],t.dilations[1]],b=[m[0]+(t.dilations[0]<=1?0:(t.kernelShape[l?1:2]-1)*(t.dilations[0]-1)),m[1]+(t.dilations[1]<=1?0:(t.kernelShape[l?2:3]-1)*(t.dilations[1]-1))],_=[b[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),b[1]-1-Math.floor(t.pads[1]+t.pads[3])/2],y=!1,$=t.group,I=e[1].dims,C=I[0]/$,v=I[1],A=[{type:6,data:s},{type:12,data:p},{type:12,data:m},{type:12,data:f},{type:12,data:b},{type:6,data:_},{type:12,data:C},{type:12,data:v},...j(e[0].dims,e[1].dims)];o&&(A.push(...j(e[2].dims)),a.push("rank")),A.push(...j(n));let T=u[1]===1&&u[2]===1,D=U=>{let V=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:p.length},{name:"filter_dims",type:"u32",length:m.length},{name:"dilations",type:"u32",length:m.length},{name:"effective_filter_dims",type:"u32",length:b.length},{name:"pads",type:"i32",length:_.length},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],H=Pe(e[0].dataType);return`${Rl(U,e,n,o,T,y,H,V,l)}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};`,inputDependencies:a},getRunData:()=>({dispatchGroup:{x:u[0],y:u[1],z:u[2]},outputs:[{dims:r?r(n):n,dataType:e[0].dataType}],programUniforms:A}),getShaderSource:D}}});var Bl,Dl,Ml,cs,ps,zl,Ul,Vl,Nl,ms,fs=q(()=>{"use strict";ds();ls();Ot();Yt();Bl=(e,t,r,o,n,s)=>(e-1)*t+r+(o-1)*n+1-s,Dl=(e,t,r,o,n)=>{let s=Math.floor(e/2);t==="SAME_UPPER"?(r[o]=s,r[n]=e-s):t==="SAME_LOWER"&&(r[o]=e-s,r[n]=s)},Ml=(e,t,r,o,n,s,u,l,a,p)=>{let m=e.length-2,f=p.length===0;if(a.length===0)for(let y=0;y{let r=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((f,b)=>f*b,1)===0){r.length=0;for(let f=2;ff+b,0)===0){let f=t[0].dims.length-2;a=new Array(f).fill(1)}let p=e.strides.slice();if(p.reduce((f,b)=>f+b,0)===0){let f=t[0].dims.length-2;p=new Array(f).fill(1)}Ml(l,r,a,e.autoPad,e.group,n,p,o,u,s);let m=Object.assign({},e);return Object.assign(m,{kernelShape:r,pads:n,outputPadding:u,outputShape:s,dilations:a,strides:p}),m},ps=e=>{let t=tn(e),r=e.format,o=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],n=e.dilations,s=e.group,u=e.kernelShape,l=e.pads,a=e.strides,p=e.wIsConst(),m=e.outputPadding,f=e.outputShape;return{autoPad:o,format:r,dilations:n,group:s,kernelShape:u,outputPadding:m,outputShape:f,pads:l,strides:a,wIsConst:p,...t,cacheKey:`${e.format};${t.activation};`}},zl=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],o=e[1].dims[0];if(r!==o)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let n=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==n))throw new Error("invalid bias");let s=e[0].dims.length-2;if(t.dilations.reduce((m,f)=>m+f,0)>0&&t.dilations.length!==s)throw new Error(`dilations should be ${s}D`);if(t.strides.reduce((m,f)=>m+f,0)>0&&t.strides.length!==s)throw new Error(`strides should be ${s}D`);if(t.pads.reduce((m,f)=>m+f,0)>0&&t.pads.length!==s*2)throw new Error(`pads should be ${s*2}D`);if(t.outputPadding.length!==s&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${s}D`);if(t.kernelShape.reduce((m,f)=>m+f,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},Ul=[2,3,1,0],Vl=(e,t,r)=>{let o=cs(r,t),n=r.format==="NHWC",s=o.outputShape,u=s[n?3:1],l=t[0].dims[n?3:1];if(o.group!==1||u===1&&l===1){e.compute(eo(t,o));return}let a=s[n?1:2],p=s[n?2:3],m=t[1].dims[2],f=t[1].dims[3],b=n?a*p:u,_=n?u:a*p,y=m*f*l,$=!0,I=e.kernelCustomData.wT??e.compute(ot(t[1],Ul),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=I);let C=[t[0],I],v=t.length===3;v&&(!n&&t[2].dims.length===1?C.push(t[2].reshape([t[2].dims[0],1,1])):C.push(t[2])),e.compute(us(C,o,s,b,_,y,v,$),{inputs:C})},Nl=(e,t)=>{let r=t.format==="NHWC",o=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];o.length===3&&o.push(e.inputs[2]);let n=t.kernelShape;(n.length===0||n[0]===0)&&(n=[e.inputs[1].dims[2]]);let s=t.dilations;(s.length===0||s[0]===0)&&(s=[1]);let u=t.strides;(u.length===0||u[0]===0)&&(u=[1]);let l=t.pads;l.length===0&&(l=[0,0]),l=[0,l[0],0,l[1]],u=[1].concat(u),s=[1].concat(s),n=[1].concat(n);let a=cs({...t,pads:l,strides:u,dilations:s,kernelShape:n},o);e.compute(eo(o,a,p=>r?[p[0],p[2],p[3]]:[p[0],p[1],p[3]]))},ms=(e,t)=>{zl(e.inputs,t),e.inputs[0].dims.length===3?Nl(e,t):Vl(e,e.inputs,t)}});var Wl,hs,gs,ys=q(()=>{"use strict";ie();_e();je();be();Wl=(e,t,r,o)=>{let n=z.size(t),s=t.length,u=M("input",e,s),l=F("output",e,s),a=r.dataType===6?r.getInt32Array()[0]:Number(r.getBigInt64Array()[0]),p=z.normalizeAxis(a,s),m=f=>{let b=` i32(${u.indicesGet("inputIndices","uniforms.axis")}) `,_=re("uniforms.input_shape","uniforms.axis",s),y=o.reverse?b+(o.exclusive?" + 1":""):"0",$=o.reverse?_:b+(o.exclusive?"":" + 1");return`\n ${f.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(u,l)}\n ${f.mainStart()}\n ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}\n var inputIndices = ${l.offsetToIndices("global_idx")};\n var sum = ${l.type.value}(0);\n let first : i32 = ${y};\n let last : i32 = ${$};\n for (var i : i32 = first; i < last; i++) {\n ${u.indicesSet("inputIndices","uniforms.axis","u32(i)")};\n sum = sum + ${u.getByIndices("inputIndices")};\n }\n ${l.setByOffset("global_idx","sum")};\n }`};return{name:"CumSum",shaderCache:{hint:o.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:[{type:12,data:n},{type:6,data:p},...j(t,t)]}),getShaderSource:m}},hs=(e,t)=>{let r=e.inputs[0].dims,o=e.inputs[0].dataType,n=e.inputs[1];e.compute(Wl(o,r,n,t),{inputs:[0]})},gs=e=>{let t=e.exclusive===1,r=e.reverse===1;return $e({exclusive:t,reverse:r})}});var to,un,bs,Gl,Hl,ro,no,ws,Ll,vs,$s,_s=q(()=>{"use strict";ie();_e();je();be();to="[a-zA-Z]|\\\\.\\\\.\\\\.",un="("+to+")+",bs="^"+un+"$",Gl="("+un+",)*"+un,Hl="^"+Gl+"$",ro=class{constructor(t=-1){this.symbolToIndices=new Map,this.inputIndex=t}addSymbol(t,r){let o=this.symbolToIndices.get(t);o===void 0?o=[r]:o.push(r),this.symbolToIndices.set(t,o)}},no=class{constructor(t,r){this.equation=r;this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[o,n]=r.includes("->")?r.split("->",2):[r,""];if(!o.match(RegExp(Hl)))throw new Error("Invalid LHS term");if(o.split(",").forEach((l,a)=>{let p=t[a].dims.slice();if(!l.match(RegExp(bs)))throw new Error("Invalid LHS term");let m=this.processTerm(l,!0,p,a);this.lhs.push(m)}),n==="")n+=[...this.symbolToInfo.entries()].filter(([l,a])=>a.count===1||l==="...").map(([l])=>l).join("");else if(!n.match(RegExp(un)))throw new Error("Invalid RHS");n.match(RegExp(to,"g"))?.forEach(l=>{if(l==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let a=this.symbolToInfo.get(l);if(a===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(a.dimValue)}}),this.rhs=this.processTerm(n,!1,this.outputDims)}addSymbol(t,r,o){let n=this.symbolToInfo.get(t);if(n!==void 0){if(n.dimValue!==r&&n.count!==1)throw new Error("Dimension mismatch");n.count++,n.inputIndices.push(o)}else n={count:1,dimValue:r,inputIndices:[o]};this.symbolToInfo.set(t,n)}processTerm(t,r,o,n=-1){let s=o.length,u=!1,l=[],a=0;if(!t.match(RegExp(bs))&&!r&&t!=="")throw new Error("Invalid LHS term");let p=t.match(RegExp(to,"g")),m=new ro(n);return p?.forEach((f,b)=>{if(f==="..."){if(u)throw new Error("Only one ellipsis is allowed per input term");u=!0;let _=s-p.length+1;if(_<0)throw new Error("Ellipsis out of bounds");if(l=o.slice(a,a+_),this.hasEllipsis){if(this.ellipsisDims.length!==l.length||this.ellipsisDims.toString()!==l.toString())throw new Error("Ellipsis dimensions mismatch")}else if(r)this.hasEllipsis=!0,this.ellipsisDims=l;else throw new Error("Ellipsis must be specified in the LHS");for(let y=0;ye+"_max",Ll=(e,t,r,o)=>{let s=e.map(m=>m.length).map((m,f)=>M(`input${f}`,t,m)),u=z.size(o),l=F("output",t,o.length),a=[...r.symbolToInfo.keys()].filter(m=>!r.rhs.symbolToIndices.has(m)),p=m=>{let f=[],b="var prod = 1.0;",_="var sum = 0.0;",y="sum += prod;",$=[],I=[],C=[],v=[],A=r.symbolToInfo.size===r.rhs.symbolToIndices.size;r.symbolToInfo.forEach((D,U)=>{if(r.rhs.symbolToIndices.has(U)){let V=r.rhs.symbolToIndices.get(U)?.[0];V!==void 0&&r.lhs.forEach((H,R)=>{if(D.inputIndices.includes(R)){let L=H.symbolToIndices.get(U);if(L===void 0)throw new Error("Invalid symbol error");L.forEach(pe=>{f.push(`${s[R].indicesSet(`input${R}Indices`,pe,l.indicesGet("outputIndices",V))}`)})}})}else r.lhs.forEach((V,H)=>{if(D.inputIndices.includes(H)){let R=V.symbolToIndices.get(U);if(R===void 0)throw new Error("Invalid symbol error");R.forEach(L=>{$.push(`${s[H].indicesSet(`input${H}Indices`,L,`${U}`)}`)}),v.push(`prod *= ${s[H].getByIndices(`input${H}Indices`)};`)}}),I.push(`for(var ${U}: u32 = 0; ${U} < uniforms.${ws(U)}; ${U}++) {`),C.push("}")});let T=A?[...f,`let sum = ${s.map((D,U)=>D.getByIndices(`input${U}Indices`)).join(" * ")};`]:[...f,_,...I,...$,b,...v,y,...C];return`\n ${m.registerUniforms(a.map(D=>({name:`${ws(D)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...s,l)}\n\n ${m.mainStart()}\n ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}\n var outputIndices = ${l.offsetToIndices("global_idx")};\n ${s.map((D,U)=>`var input${U}Indices: ${s[U].type.indices};`).join(`\n`)}\n ${T.join(`\n`)};\n ${l.setByOffset("global_idx","sum")};\n }`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let m=a.filter(b=>r.symbolToInfo.has(b)).map(b=>({type:12,data:r.symbolToInfo.get(b)?.dimValue||0}));m.push({type:12,data:u});let f=e.map((b,_)=>[...j(b)]).reduce((b,_)=>b.concat(_),m);return f.push(...j(o)),{outputs:[{dims:o,dataType:t}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:f}},getShaderSource:p}},vs=(e,t)=>{let r=new no(e.inputs,t.equation),o=r.outputDims,n=e.inputs.map((s,u)=>s.dims);e.compute(Ll(n,e.inputs[0].dataType,r,o))},$s=e=>{let t=e.equation.replace(/\\s+/g,"");return $e({equation:t})}});var Fl,xs,ql,Kl,Ss,Cs=q(()=>{"use strict";ie();_e();be();Fl=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),o=r.length{let r=e.length-t.length,o=[];for(let n=0;ne.length>t.length?xs(e,t):xs(t,e),Kl=e=>{let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),o=ql(t,r),n=e[0].dataType,s=n===9?4:1,u=Math.ceil(z.size(o)/s),l=p=>{let m=M("input",n,t.length,s),f=F("output",n,o.length,s),b;if(n===9){let _=(y,$,I="")=>`\n let outputIndices${$} = ${f.offsetToIndices(`outputOffset + ${$}u`)};\n let offset${$} = ${m.broadcastedIndicesToOffset(`outputIndices${$}`,f)};\n let index${$} = offset${$} / 4u;\n let component${$} = offset${$} % 4u;\n ${y}[${$}] = ${I}(${m.getByOffset(`index${$}`)}[component${$}]);\n `;b=`\n let outputOffset = global_idx * ${s};\n var data = vec4(0);\n ${_("data",0,"u32")}\n ${_("data",1,"u32")}\n ${_("data",2,"u32")}\n ${_("data",3,"u32")}\n ${f.setByOffset("global_idx","data")}\n }`}else b=`\n let outputIndices = ${f.offsetToIndices("global_idx")};\n let inputOffset = ${m.broadcastedIndicesToOffset("outputIndices",f)};\n ${f.setByOffset("global_idx",m.getByOffset("inputOffset"))}\n }`;return`\n ${p.registerUniform("vec_size","u32").declareVariables(m,f)}\n ${p.mainStart()}\n ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}\n ${b}`},a=[{type:12,data:u},...j(t,o)];return{name:"Expand",shaderCache:{hint:`${o.length}`,inputDependencies:["rank"]},getShaderSource:l,getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:a})}},Ss=e=>{Fl(e.inputs),e.compute(Kl(e.inputs),{inputs:[0]})}});var jl,Is,As=q(()=>{"use strict";ie();_e();be();en();jl=e=>{let t=e[0].dataType,r=z.size(e[0].dims),o=z.size(e[1].dims),n=o%4===0,s=u=>{let l=M("x",t,[1],4),a=M("bias",t,[1],4),p=F("y",t,[1],4),m=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],f=_=>`\n let bias${_}_offset: u32 = (global_idx * 4 + ${_}) % uniforms.bias_size;\n let bias${_} = ${a.getByOffset(`bias${_}_offset / 4`)}[bias${_}_offset % 4];`,b=n?`\n let bias = ${a.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${f(0)}${f(1)}${f(2)}${f(3)}\n let bias = ${l.type.value}(bias0, bias1, bias2, bias3);`;return`${u.registerUniforms(m).declareVariables(l,a,p)}\n\n ${Ln(et(t))}\n\n ${u.mainStart(Kr)}\n ${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")}\n\n let x = ${l.getByOffset("global_idx")};\n ${b}\n let x_in = x + bias;\n ${p.setByOffset("global_idx",Fn("x_in"))}\n }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${n}`,inputDependencies:["type","type"]},getShaderSource:s,getRunData:u=>({outputs:[{dims:u[0].dims,dataType:u[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:o}],dispatchGroup:{x:Math.ceil(r/Kr/4)}})}},Is=e=>{e.inputs.length<2||z.size(e.inputs[1].dims)===0?Di(e):e.compute(jl(e.inputs))}});var Yl,Zl,Ts,Es,Ps=q(()=>{"use strict";ie();_e();je();be();Yl=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},Zl=(e,t)=>{let r=e[0].dims,o=e[1].dims,n=r.length,s=z.normalizeAxis(t.axis,n),u=r.slice(0);u.splice(s,1,...o);let l=r[s],a=e[0].dataType===9?4:1,p=Math.ceil(z.size(u)/a),m=[{type:12,data:p},{type:6,data:l},{type:12,data:s},...j(e[0].dims,e[1].dims,u)],f=b=>{let _=M("data",e[0].dataType,e[0].dims.length,a),y=M("inputIndices",e[1].dataType,e[1].dims.length),$=F("output",e[0].dataType,u.length,a),I=v=>{let A=o.length,T=`var indicesIndices${v} = ${y.type.indices}(0);`;for(let D=0;D1?`indicesIndices${v}[${D}]`:`indicesIndices${v}`} = ${u.length>1?`outputIndices${v}[uniforms.axis + ${D}]`:`outputIndices${v}`};`;T+=`\n var idx${v} = ${y.getByIndices(`indicesIndices${v}`)};\n if (idx${v} < 0) {\n idx${v} = idx${v} + uniforms.axisDimLimit;\n }\n var dataIndices${v} : ${_.type.indices};\n `;for(let D=0,U=0;D1?`dataIndices${v}[${D}]`:`dataIndices${v}`} = u32(idx${v});`,U+=A):(T+=`${n>1?`dataIndices${v}[${D}]`:`dataIndices${v}`} = ${u.length>1?`outputIndices${v}[${U}]`:`outputIndices${v}`};`,U++);return T},C;if(e[0].dataType===9){let v=(A,T,D="")=>`\n let outputIndices${T} = ${$.offsetToIndices(`outputOffset + ${T}u`)};\n ${I(T)};\n let offset${T} = ${_.indicesToOffset(`dataIndices${T}`)};\n let index${T} = offset${T} / 4u;\n let component${T} = offset${T} % 4u;\n ${A}[${T}] = ${D}(${_.getByOffset(`index${T}`)}[component${T}]);\n `;C=`\n let outputOffset = global_idx * ${a};\n var value = vec4(0);\n ${v("value",0,"u32")}\n ${v("value",1,"u32")}\n ${v("value",2,"u32")}\n ${v("value",3,"u32")}\n ${$.setByOffset("global_idx","value")}\n `}else C=`\n let outputIndices = ${$.offsetToIndices("global_idx")};\n ${I("")};\n let value = ${_.getByIndices("dataIndices")};\n ${$.setByOffset("global_idx","value")};\n `;return`\n ${b.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(_,y,$)}\n ${b.mainStart()}\n ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}\n ${C}\n }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:m}),getShaderSource:f}},Ts=e=>$e({axis:e.axis}),Es=(e,t)=>{let r=e.inputs;Yl(r),e.compute(Zl(e.inputs,t))}});var Ql,Xl,Os,ks,Rs=q(()=>{"use strict";ie();_e();je();be();Ql=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and\n indices input tensors be of same rank.`)},Xl=(e,t)=>{let r=e[0].dims,o=e[0].dataType,n=r.length,s=e[1].dims,u=e[1].dataType,l=z.normalizeAxis(t.axis,n),a=r[l],p=s.slice(0),m=z.size(p),f=M("input",o,n),b=M("indicesInput",u,s.length),_=F("output",o,p.length),y=[{type:12,data:m},{type:6,data:a},{type:12,data:l}];return y.push(...j(r,s,p)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:p,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:y}),getShaderSource:C=>`\n ${C.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(f,b,_)}\n ${C.mainStart()}\n ${C.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}\n\n let outputIndices = ${_.offsetToIndices("global_idx")};\n\n var idx = ${b.getByOffset("global_idx")};\n if (idx < 0) {\n idx = idx + uniforms.axisDimLimit;\n }\n var inputIndices = ${f.type.indices}(outputIndices);\n ${f.indicesSet("inputIndices","uniforms.axis","u32(idx)")};\n let value = ${f.getByIndices("inputIndices")};\n\n ${_.setByOffset("global_idx","value")};\n }`}},Os=e=>$e({axis:e.axis}),ks=(e,t)=>{let r=e.inputs;Ql(r),e.compute(Xl(e.inputs,t))}});var Jl,ec,Bs,Ds,Ms=q(()=>{"use strict";ie();_e();be();Jl=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},ec=(e,t)=>{let r=e[0].dims.slice(),o=e[1].dims.slice(),[n,s,u]=Lr.getShapeOfGemmResult(r,t.transA,o,t.transB,e.length===3?e[2].dims:void 0),l=[n,s];if(!l)throw new Error("Can\'t use gemm on the given tensors");let a=z.size(l),p=[{type:12,data:a},{type:12,data:n},{type:12,data:s},{type:12,data:u},{type:1,data:t.alpha},{type:1,data:t.beta}],m=["type","type"];e.length===3&&(p.push(...j(e[2].dims)),m.push("rank")),p.push(...j(l));let f=b=>{let _="";t.transA&&t.transB?_="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?_="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?_="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(_="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let y=t.alpha===1?"":"value *= uniforms.alpha;",$=M("a",e[0].dataType,e[0].dims),I=M("b",e[1].dataType,e[1].dims),C=$.type.value,v=null,A=[$,I];e.length===3&&(v=M("c",e[2].dataType,e[2].dims.length),A.push(v));let T=F("output",e[0].dataType,l.length);A.push(T);let D=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return`\n ${b.registerUniforms(D).declareVariables(...A)}\n\n ${b.mainStart()}\n ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}\n\n let m = global_idx / uniforms.N;\n let n = global_idx % uniforms.N;\n\n var value = ${C}(0);\n for (var k: u32 = 0u; k < uniforms.K; k++) {\n ${_}\n }\n\n ${y}\n ${(()=>v!=null?`let cOffset = ${v.broadcastedIndicesToOffset("vec2(m, n)",T)}; value += ${C}(uniforms.beta) * ${v.getByOffset("cOffset")};`:"")()}\n output[global_idx] = value;\n }`};return{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:l,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:p}),getShaderSource:f}},Bs=e=>{let t=e.transA,r=e.transB,o=e.alpha,n=e.beta;return{transA:t,transB:r,alpha:o,beta:n,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},Ds=(e,t)=>{Jl(e.inputs),e.compute(ec(e.inputs,t))}});var tc,rc,nc,zs,Us=q(()=>{"use strict";ie();_e();be();tc=(e,t)=>{let r=e[0].dims,o=r,n=2,s=z.sizeToDimension(r,n),u=z.sizeFromDimension(r,n),l=Me(u),a=u/l,p=[r[0],r[1],a],m=["rank","type","type"],f=[{type:12,data:u},{type:12,data:a}];f.push(...j(p,p));let b=_=>{let y=M("x",e[0].dataType,p.length,l),$=M("scale",e[1].dataType,e[1].dims),I=M("bias",e[2].dataType,e[2].dims),C=F("output",e[0].dataType,p.length,l),v=[y,$,I,C],A=y.type.value,T=l===1?"f32":`vec${l}`,D=64,U=[{name:"normSize",type:"u32"},{name:"normPackedSize",type:"u32"}];return`\n var meanShared : f32;\n var squaredNormShared : f32;\n var workgroupShared : array<${T}, ${D}>;\n const workgroupSize = ${D}u;\n ${_.registerUniforms(U).declareVariables(...v)}\n ${_.mainStart(D)}\n let norm = global_idx / workgroupSize;\n let batch = norm / uniforms.x_shape[1];\n let channel = norm % uniforms.x_shape[1];\n let localIndex = local_id.x;\n\n // initialize workgroup memory\n var initial = ${T}(0);\n for (var h = localIndex; h < uniforms.normPackedSize; h += workgroupSize) {\n initial = initial + ${T}(${y.get("batch","channel","h")});\n }\n workgroupShared[localIndex] = initial;\n workgroupBarrier();\n\n // Calculate the mean of current channel data.\n for (var currSize = workgroupSize >> 1; currSize > 0; currSize = currSize >> 1) {\n if (localIndex < currSize) {\n workgroupShared[localIndex] = workgroupShared[localIndex] + workgroupShared[localIndex + currSize];\n }\n workgroupBarrier();\n }\n if (localIndex == 0) {\n meanShared = ${tt("workgroupShared[0]",l)} / f32(uniforms.normSize);\n }\n workgroupBarrier();\n\n // reinitialize workgroup memory.\n initial = ${T}(0);\n for (var h = localIndex; h < uniforms.normPackedSize; h += workgroupSize) {\n let deviation = ${T}(${y.get("batch","channel","h")}) - ${T}(meanShared);\n initial = initial + deviation * deviation;\n }\n workgroupShared[localIndex] = initial;\n workgroupBarrier();\n\n // Calculate the sum of square of deviation of current channel data.\n for (var currSize = workgroupSize >> 1; currSize > 0; currSize = currSize >> 1) {\n if (localIndex < currSize) {\n workgroupShared[localIndex] = workgroupShared[localIndex] + workgroupShared[localIndex + currSize];\n }\n workgroupBarrier();\n }\n if (localIndex == 0) {\n squaredNormShared = ${tt("workgroupShared[0]",l)};\n }\n workgroupBarrier();\n\n let invStdDev = inverseSqrt(squaredNormShared / f32(uniforms.normSize) + f32(${t.epsilon}));\n let channelScale = invStdDev * f32(${$.getByOffset("channel")});\n let channelShift = f32(${I.getByOffset("channel")}) - meanShared * channelScale;\n for (var h = localIndex; h < uniforms.normPackedSize; h += workgroupSize) {\n let value = ${y.get("batch","channel","h")} * ${A}(${T}(channelScale)) + ${A}(${T}(channelShift));\n ${C.set("batch","channel","h","value")};\n }\n }`};return{name:"InstanceNormalization",shaderCache:{hint:`${t.epsilon};${l}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:s},programUniforms:f}),getShaderSource:b}},rc=(e,t,r,o,n,s,u,l)=>{let a=Me(u),p=64,m=a===1?"vec2f":`mat2x${a}f`,f=a===1?"f32":`vec${a}f`,b=(U,V)=>`${m}(${U}, ${V})`,_=n*u/a,y=Math.ceil(s/p),$=["type"],I=[{type:12,data:y},{type:12,data:s},{type:12,data:Math.floor(u/a)},{type:12,data:Math.floor(s*u/a)}],C=U=>{let V=M("input",t.dataType,t.dims,a);return`\n ${U.declareVariables(V)}\n @group(0) @binding(1) var output : array<${m}>;\n struct Uniforms {wg_size:u32, H:u32, C:u32, image_size:u32};\n @group(0) @binding(2) var uniforms: Uniforms;\n\n ${U.mainStart(p)}\n let currentImageNumber = global_idx / ${p} / uniforms.C;\n let currentChannelNumber = (global_idx / ${p}) % uniforms.C;\n let wgId = global_idx % ${p};\n let wgOffset = wgId * uniforms.wg_size;\n if (wgOffset >= uniforms.H) {\n return;\n }\n let wgMax = min(wgOffset + uniforms.wg_size, uniforms.H);\n\n let offset = currentImageNumber * uniforms.image_size + currentChannelNumber;\n var sum = ${Ye("f32",a)};\n var squaredSum = ${Ye("f32",a)};\n for (var i: u32 = wgOffset; i < wgMax; i++) {\n let value = ${f}(input[offset + i * uniforms.C]);\n sum += value;\n squaredSum += value * value;\n }\n output[global_idx] = ${b("sum","squaredSum")};\n }`},v=e.compute({name:"InstanceNormComputeMean",shaderCache:{hint:`${a}`,inputDependencies:$},getRunData:()=>({outputs:[{dims:[n,u,p,2],dataType:1}],dispatchGroup:{x:n*u/a},programUniforms:I}),getShaderSource:C},{inputs:[t],outputs:[-1]})[0],A=[{type:12,data:_},{type:12,data:s},{type:12,data:Math.floor(u/a)},{type:12,data:Math.floor(p*u/a)}],T=["type","type","type"],D=U=>{let V=M("scale",r.dataType,r.dims,a),H=M("bias",o.dataType,o.dims,a);return`\n @group(0) @binding(0) var input : array<${m}>;\n @group(0) @binding(1) var scale : array<${V.type.storage}>;\n @group(0) @binding(2) var bias : array<${H.type.storage}>;\n @group(0) @binding(3) var output : array<${m}>;\n struct Uniforms {units_of_work : u32, H: u32, C : u32, image_size : u32};\n @group(0) @binding(4) var uniforms: Uniforms;\n\n ${U.mainStart()}\n ${U.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.units_of_work")}\n let currentImageNumber = global_idx / uniforms.C;\n let currentChannelNumber = global_idx % uniforms.C;\n\n let offset = currentImageNumber * uniforms.image_size;\n var sum = ${Ye("f32",a)};\n var squaredSum = ${Ye("f32",a)};\n for (var i: u32 = 0; i < min(${p}, uniforms.H); i++) {\n let value = input[offset + i + currentChannelNumber * ${p}];\n sum += value[0];\n squaredSum += value[1];\n }\n sum = sum / f32(uniforms.H);\n squaredSum = squaredSum / f32(uniforms.H);\n let invStdDev = inverseSqrt(squaredSum - sum * sum + f32(${l}));\n let channelScale = invStdDev * ${f}(scale[currentChannelNumber]);\n let channelShift = ${f}(bias[currentChannelNumber]) - sum * channelScale;\n\n output[global_idx] = ${b("channelScale","channelShift")};\n }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${a};${l}`,inputDependencies:T},getRunData:()=>({outputs:[{dims:[n,u,2],dataType:1}],dispatchGroup:{x:Math.ceil(_/64)},programUniforms:A}),getShaderSource:D},{inputs:[v,r,o],outputs:[-1]})[0]},nc=(e,t,r)=>{let o=t[0].dims,n=o,s=o[0],u=o[o.length-1],l=z.sizeFromDimension(o,1)/u,a=Me(u),p=z.size(n)/a,m=[{type:12,data:l},{type:12,data:Math.floor(u/a)}],f=["type","type"],b=rc(e,t[0],t[1],t[2],s,l,u,r.epsilon),_=y=>{let $=Pe(t[0].dataType),I=a===1?"vec2f":`mat2x${a}f`,C=a===1?$:`vec${a}<${$}>`,v=M("input",t[0].dataType,t[0].dims,a),A=F("output",t[0].dataType,n,a);return`\n @group(0) @binding(0) var input : array<${v.type.storage}>;\n @group(0) @binding(1) var scaleInput : array<${I}>;\n @group(0) @binding(2) var output : array<${A.type.storage}>;\n struct Uniforms {H: u32, C : u32};\n @group(0) @binding(3) var uniforms: Uniforms;\n\n ${y.mainStart()}\n let currentImageNumber = global_idx / (uniforms.C * uniforms.H);\n let currentChannelNumber = global_idx % uniforms.C;\n\n let scaleOffset = currentImageNumber * uniforms.C + currentChannelNumber;\n let scale = scaleInput[scaleOffset];\n output[global_idx] = fma(input[global_idx], ${C}(scale[0]), ${C}(scale[1]));\n }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${a}`,inputDependencies:f},getRunData:()=>({outputs:[{dims:n,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:m}),getShaderSource:_},{inputs:[t[0],b]})},zs=(e,t)=>{t.format==="NHWC"?nc(e,e.inputs,t):e.compute(tc(e.inputs,t))}});var oc,ac,Vs,Ns=q(()=>{"use strict";ie();_e();be();oc=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},ac=(e,t,r)=>{let o=e[0].dims,n=e[1],s=e[2],u=o,l=z.normalizeAxis(t.axis,o.length),a=z.sizeToDimension(o,l),p=z.sizeFromDimension(o,l),m=z.size(n.dims),f=s?z.size(s.dims):0;if(m!==p||s&&f!==p)throw new Error(`Size of X.shape()[axis:] == ${p}.\n Size of scale and bias (if provided) must match this.\n Got scale size of ${m} and bias size of ${f}`);let b=[];for(let T=0;T1,C=r>2,v=T=>{let D=Pe(e[0].dataType),U=[M("x",e[0].dataType,e[0].dims,_),M("scale",n.dataType,n.dims,_)];s&&U.push(M("bias",s.dataType,s.dims,_)),U.push(F("output",e[0].dataType,u,_)),I&&U.push(F("mean_data_output",1,b)),C&&U.push(F("inv_std_output",1,b));let V=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return`\n ${T.registerUniforms(V).declareVariables(...U)}\n ${T.mainStart()}\n ${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")}\n let offset = global_idx * uniforms.norm_size_vectorized;\n var mean_vector = ${Ye("f32",_)};\n var mean_square_vector = ${Ye("f32",_)};\n\n for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) {\n let value = ${st(D,_,"x[h + offset]")};\n mean_vector += value;\n mean_square_vector += value * value;\n }\n let mean = ${tt("mean_vector",_)} / uniforms.norm_size;\n let inv_std_dev = inverseSqrt(${tt("mean_square_vector",_)} / uniforms.norm_size - mean * mean + uniforms.epsilon);\n\n for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) {\n let f32input = ${st(D,_,"x[j + offset]")};\n let f32scale = ${st(D,_,"scale[j]")};\n output[j + offset] = ${U[0].type.value}((f32input - mean) * inv_std_dev * f32scale\n ${s?`+ ${st(D,_,"bias[j]")}`:""}\n );\n }\n\n ${I?"mean_data_output[global_idx] = mean":""};\n ${C?"inv_std_output[global_idx] = inv_std_dev":""};\n }`},A=[{dims:u,dataType:e[0].dataType}];return I&&A.push({dims:b,dataType:1}),C&&A.push({dims:b,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${_};${r}`,inputDependencies:y},getRunData:()=>({outputs:A,dispatchGroup:{x:Math.ceil(a/64)},programUniforms:$}),getShaderSource:v}},Vs=(e,t)=>{oc(e.inputs),e.compute(ac(e.inputs,t,e.outputCount))}});var ic,sc,Ws,Gs,Hs=q(()=>{"use strict";ie();_e();je();be();ic=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let r=e[0],o=r.dims.length;if(r.dims[o-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let n=Math.floor((t.k+t.blockSize-1)/t.blockSize),s=t.blockSize/8*t.bits,u=e[1];if(!z.areEqual(u.dims,[t.n,n,s]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let a=e[2].dims;if(z.size(a)!==t.n*n)throw new Error("scales input size error.");if(e.length===4){let m=e[3].dims,f=t.bits>4?t.n*n:t.n*Math.floor((n+1)/2);if(z.size(m)!==f)throw new Error("zeroPoints input size error.")}},sc=(e,t)=>{let r=e[0].dims,o=r.length,n=r.slice(0,o-1).concat(t.n),s=r[o-2],l=t.blockSize/8*t.bits/4,a=Me(s),p=Me(t.n),m=Me(t.k),f=Me(l),b=z.size(n)/p/a,_=[{type:12,data:b},{type:12,data:t.k},{type:12,data:t.n},{type:12,data:t.accuracyLevel},{type:12,data:t.bits},{type:12,data:t.blockSize}],y=r.slice();y.splice(-1,1,t.k/m);let $=z.convertShape(e[1].dims).slice();$.splice(-1,1,l/f),_.push(...j(y)),_.push(...j($)),_.push(...j(e[2].dims)),e.length===4&&_.push(...j(z.convertShape(e[3].dims)));let I=n.slice();I.splice(-1,1,t.n/p),_.push(...j(I));let C=v=>{let A=M("a",e[0].dataType,y.length,m),T=M("b",12,$.length,f),D=M("scales",e[2].dataType,e[2].dims.length),U=[A,T,D],V=e.length===4?M("zero_points",12,e[3].dims.length):void 0;V&&U.push(V);let H=F("output",e[0].dataType,n.length,p),R=[{name:"output_size",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"accuracy_level",type:"u32"},{name:"bits",type:"u32"},{name:"block_size",type:"u32"}],L=Math.floor((t.k+t.blockSize-1)/t.blockSize),pe=Pe(e[0].dataType),Ie=(()=>{switch(m){case 1:return`array<${pe}, 8>`;case 2:return`mat4x2<${pe}>`;case 4:return`mat2x4<${pe}>`;default:throw new Error(`${m}-component is not supported.`)}})(),we=`\n fn dequantize(quantized: ${Ie}, zero_point: ${pe}, scale: ${pe}) -> ${Ie} {\n ${(()=>m===1?`var dequantized = ${Ie}(${Array.from({length:8},(Q,xe)=>`(quantized[${xe}] - zero_point) * scale`).join(", ")});\n return dequantized;`:`var zero_points: ${Ie} = ${Ie}(${Array(8).fill("zero_point").join(",")});\n return (quantized - zero_points) * scale;`)()}\n }`,ne=`\n fn ortUnpack8x4snorm(value: u32) -> ${Ie} {\n var quantized: ${Ie};\n var offset: u32 = 0;\n let count: u32 = 4;\n for (var i: u32 = 0; i < 8u; i++) {\n var result = ${pe}(extractBits(value, offset, count));\n ${(()=>{switch(m){case 1:return"quantized[i] = result;";case 2:return"quantized[i / 2][i % 2] = result;";case 4:return"quantized[i / 4][i % 4] = result;";default:throw new Error(`${m}-component is not supported.`)}})()}\n offset += count;\n }\n return quantized;\n }`,ze=V?`\n zero_point_offset += 4;\n if (zero_point_offset == 32) {\n zero_point_offset = 0;\n zero_point_index++;\n zero_point_word = ${V.getByOffset("zero_point_index")};\n }`:"";return`\n ${we};\n ${ne};\n ${v.registerUniforms(R).declareVariables(...U,H)}\n ${v.mainStart()}\n ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}\n var output_values: array<${H.type.value}, ${a}>;\n var output_indices = ${H.offsetToIndices("global_idx")};\n var n = ${H.indicesGet("output_indices",o-1)};\n var m = ${H.indicesGet("output_indices",o-2)};\n var a_indices: ${A.type.indices} = output_indices;\n // Two zero points are packed into one byte because uniforms.bits <= 4.\n // zero_point_offset is either 0 or 4. It is bit offset within one byte.\n // TODO support zero_point_offset for bits > 4\n ${V?`\n var zero_point_index: u32 = n * ${p} * ((${L} + 1) / 2) / 4;\n var zero_point_word: u32 = ${V.getByOffset("zero_point_index")};\n var zero_point_offset: u32 = 0;`:""}\n var scale_index = n * ${L*p};\n var b_indices: ${T.type.indices};\n for (var c: u32 = 0; c < ${p}; c++) {\n ${T.indicesSet("b_indices","0",`n * ${p} + c`)};\n var block_offset: u32 = 0;\n for (var block: u32 = 0; block < ${L}; block++) {\n // The scale and zero points are computed per block.\n let scale = ${D.getByOffset("scale_index")};\n // The default zero point is 8 for unsigned 4-bit quantization.\n let zero_point = ${pe}(${V?"extractBits(zero_point_word, zero_point_offset, 4)":8});\n ${T.indicesSet("b_indices","1","block")};\n var word_offset: u32 = block_offset;\n for (var word: u32 = 0; word < ${l}; word += ${f}) {\n ${T.indicesSet("b_indices","2","word")};\n let b_data = ${T.getByIndices("b_indices")};\n for (var i: u32 = 0; i < ${f}; i++) {\n let b_value = ${f===1?"b_data":"b_data[word + i]"};\n let b_quantized_values: ${Ie} = ortUnpack8x4snorm(b_value);\n let b_dequantized_values = dequantize(b_quantized_values, zero_point, scale);\n // Number of B elements per 32-bit word is 32/bits = 32/4 = 8\n var offset: u32 = word_offset;\n for (var j: u32 = 0; j < 8/${m}; j++) {\n ${A.indicesSet("a_indices",o-1,`offset/${m}`)};\n for (var k: u32 = 0; k < ${a}u; k++) {\n ${A.indicesSet("a_indices",o-2,`m * ${a} + k`)};\n let a_data = ${A.getByIndices("a_indices")};\n output_values[k]${p>1?"[c]":""} += ${m===1?"a_data * b_dequantized_values[j]":"dot(a_data, b_dequantized_values[j])"};\n }\n offset += ${m};\n }\n word_offset += 8;\n }\n }\n scale_index++;\n ${ze}\n block_offset += uniforms.block_size;\n }\n // Drop the trailing 4 bits if the zero_poit_offset is not a byte boundary to align with the next byte.\n ${V?`if (zero_point_offset % 8 > 0) {\n ${ze}\n }`:""}\n }\n for (var k: u32 = 0u; k < ${a}u; k++) {\n ${H.indicesSet("output_indices",o-2,`${a+" * m + k"}`)};\n ${H.setByIndices("output_indices","output_values[k]")}\n }\n }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.cacheKey};${e.length}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(b/64)},programUniforms:_}),getShaderSource:C}},Ws=(e,t)=>{ic(e.inputs,t),e.compute(sc(e.inputs,t))},Gs=e=>$e(e)});var uc,Fs,Ls,dc,oo,qs,Ks=q(()=>{"use strict";ie();_e();je();Gr();Hn();be();Yt();uc=(e,t)=>{let r=e[0],o=e[1],n=e[2],s=e[3],u=e[4],l=e[5],a=e[6],p=e[7];if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let m=!1,f=r.dims[0],b=r.dims[1],_=r.dims.length===3?m?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],y=b,$=0,I=0,C=Math.floor(_/t.numHeads);if(a&&p){if(a.dims.length!==4)throw new Error(\'Input "past_key" is expected to have 4 dimensions\');if(p.dims.length!==4)throw new Error(\'Input "past_value" is expected to have 4 dimensions\');$=a.dims[2],I=a.dims[2]}else if(a||p)throw new Error(\'Input "past_key" and "past_value" shall be both present or both absent\');let v;if(o){if(r.dims.length!==3)throw new Error(\'Input "query" is expected to have 3 dimensions when key is given\');if(o.dims.length<3||o.dims.length>5)throw new Error(\'Input "key" is expected to have 3, 4, or 5 dimensions\');if(r.dims[0]!==o.dims[0])throw new Error(\'Input "query" and "key" shall have same dim 0 (batch size)\');if(o.dims.length===3){if(o.dims[2]!==r.dims[2])throw new Error(\'Input "query" and "key" shall have same dim 2 (hidden_size)\');v=2,y=o.dims[1]}else if(o.dims.length===5){if(o.dims[2]!==t.numHeads||o.dims[3]!==2||o.dims[4]!==C)throw new Error(\'Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv\');if(n)throw new Error(\'Expect "value" be none when "key" has packed kv format.\');v=5,y=o.dims[1]}else{if(o.dims[1]!==t.numHeads||o.dims[3]!==C)throw new Error(\'Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key\');v=0,y=o.dims[2]}}else{if(r.dims.length!==3&&r.dims.length!==5)throw new Error(\'Input "query" is expected to have 3 or 5 dimensions when key is empty\');if(r.dims.length===5&&(r.dims[2]!==t.numHeads||r.dims[3]!==3))throw new Error(\'Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv\');v=3}if(s){if(s.dims.length!==1)throw new Error(\'Input "bias" is expected to have 1 dimension\');if(n&&r.dims.length===5&&r.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let A=0;if(u){A=8;let H=u.dims;throw H.length===1?H[0]===f?A=1:H[0]===3*f+2&&(A=3):H.length===2&&H[0]===f&&H[1]===y&&(A=5),A===8?new Error(\'Input "key_padding_mask" shape shall be (batch_size) or (batch_size, kv_sequence_length)\'):new Error("Mask not supported")}let T=!1,D=_;if(n){if(n.dims.length!==3&&n.dims.length!==4)throw new Error(\'Input "value" is expected to have 3 or 4 dimensions\');if(r.dims[0]!==n.dims[0])throw new Error(\'Input "query" and "value" shall have same dim 0 (batch_size)\');if(n.dims.length===3){if(y!==n.dims[1])throw new Error(\'Input "key" and "value" shall have the same dim 1 (kv_sequence_length)\');D=n.dims[2]}else{if(y!==n.dims[2])throw new Error(\'Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)\');D=n.dims[1]*n.dims[3],T=!0}}let U=$+y,V=!1;if(u)throw new Error("Key padding mask is not supported");if(l)throw new Error("extraAddQk is not supported");if(a)throw new Error("pastKey is not supported");if(p)throw new Error("pastValue is not supported");return{batchSize:f,sequenceLength:b,pastSequenceLength:$,kvSequenceLength:y,totalSequenceLength:U,maxSequenceLength:I,inputHiddenSize:0,hiddenSize:_,vHiddenSize:D,headSize:C,vHeadSize:Math.floor(D/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:A,scale:t.scale,broadcastResPosBias:V,passPastInKv:T,qkvFormat:v}},Fs=e=>$e({...e}),Ls=$e({perm:[0,2,1,3]}),dc=(e,t,r,o,n,s,u)=>{let l=[o,n,s],a=z.size(l),p=[{type:12,data:a},{type:12,data:u},{type:12,data:s}],m=f=>{let b=F("qkv_with_bias",t.dataType,l),_=M("qkv",t.dataType,l),y=M("bias",r.dataType,l),$=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return`\n ${f.registerUniforms($).declareVariables(_,y,b)}\n ${f.mainStart()}\n ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}\n let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset;\n\n qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx];\n }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:l,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:p}),getShaderSource:m},{inputs:[t,r],outputs:[-1]})[0]},oo=(e,t,r,o,n,s,u,l)=>{let a=s;if(u){if(o===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return a=dc(e,s,u,t,o,r*n,l),a=a.reshape([t,o,r,n]),e.compute(ot(a,Ls.perm),{inputs:[a],outputs:[-1]})[0]}else return s.dims.length===3&&(a=s.reshape([t,o,r,n])),e.compute(ot(a,Ls.perm),{inputs:[a],outputs:[-1]})[0]},qs=(e,t)=>{let r=uc(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let o=e.inputs[1]&&e.inputs[2]&&e.inputs[1].dims.length===4&&e.inputs[2].dims.length===4,n=oo(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,e.inputs[0],e.inputs[3],0);if(o)return Qr(e,n,e.inputs[1],e.inputs[2],e.inputs[4],void 0,void 0,void 0,e.inputs[5],r,t);let s=oo(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.headSize,e.inputs[1],e.inputs[3],r.hiddenSize),u=oo(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.vHeadSize,e.inputs[2],e.inputs[3],2*r.hiddenSize);Qr(e,n,s,u,e.inputs[4],void 0,e.inputs[6],e.inputs[7],e.inputs[5],r,t)}});var lc,cc,pc,mc,fc,hc,gc,yc,js,Ys=q(()=>{"use strict";ie();_e();be();lc=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},cc=(e,t,r)=>{let o="";for(let n=t-1;n>=0;--n)o+=`\n k = i32(${e.indicesGet("indices",n)}) - ${re("uniforms.pads",n,r)};\n if (k < 0) {\n break;\n }\n if (k >= i32(${re("uniforms.x_shape",n,t)})) {\n break;\n }\n offset += k * i32(${re("uniforms.x_strides",n,t)});\n `;return`\n value = ${e.type.value}(uniforms.constant_value);\n for (var i = 0; i < 1; i++) {\n var offset = 0;\n var k = 0;\n ${o}\n value = x[offset];\n }\n `},pc=(e,t,r)=>{let o="";for(let n=t-1;n>=0;--n)o+=`\n k = i32(${e.indicesGet("indices",n)}) - ${re("uniforms.pads",n,r)};\n if (k < 0) {\n k = -k;\n }\n {\n let _2n_1 = 2 * (i32(${re("uniforms.x_shape",n,t)}) - 1);\n k = k % _2n_1;\n if(k >= i32(${re("uniforms.x_shape",n,t)})) {\n k = _2n_1 - k;\n }\n }\n offset += k * i32(${re("uniforms.x_strides",n,t)});\n `;return`\n var offset = 0;\n var k = 0;\n ${o}\n value = x[offset];\n `},mc=(e,t,r)=>{let o="";for(let n=t-1;n>=0;--n)o+=`\n k = i32(${e.indicesGet("indices",n)}) - ${re("uniforms.pads",n,r)};\n if (k < 0) {\n k = 0;\n }\n if (k >= i32(${re("uniforms.x_shape",n,t)})) {\n k = i32(${re("uniforms.x_shape",n,t)}) - 1;\n }\n offset += k * i32(${re("uniforms.x_strides",n,t)});\n `;return`\n var offset = 0;\n var k = 0;\n ${o}\n value = x[offset];\n `},fc=(e,t,r)=>{let o="";for(let n=t-1;n>=0;--n)o+=`\n k = i32(${e.indicesGet("indices",n)}) - ${re("uniforms.pads",n,r)};\n if (k < 0) {\n k += i32(${re("uniforms.x_shape",n,t)}]);\n }\n if (k >= i32(${re("uniforms.x_shape",n,t)})) {\n k -= i32(${re("uniforms.x_shape",n,t)});\n }\n offset += k * i32(${re("uniforms.x_strides",n,t)});\n `;return`\n var offset = 0;\n var k = 0;\n ${o}\n value = x[offset];\n `},hc=(e,t,r)=>{switch(r.mode){case 0:return cc(e,t,r.pads.length);case 1:return pc(e,t,r.pads.length);case 2:return mc(e,t,r.pads.length);case 3:return fc(e,t,r.pads.length);default:throw new Error("Invalid mode")}},gc=(e,t)=>{let r=z.padShape(e[0].dims.slice(),t.pads),o=e[0].dims,n=z.size(r),s=[{type:12,data:n},{type:12,data:t.pads}];t.mode===0&&s.push({type:e[0].dataType,data:t.value}),s.push(...j(e[0].dims,r));let u=["rank"],l=a=>{let p=F("output",e[0].dataType,r.length),m=M("x",e[0].dataType,o.length),f=m.type.value,b=hc(p,o.length,t),_=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&_.push({name:"constant_value",type:f}),`\n ${a.registerUniforms(_).declareVariables(m,p)}\n ${a.mainStart()}\n ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}\n\n let indices = ${p.offsetToIndices("global_idx")};\n\n var value = ${f}(0);\n ${b}\n output[global_idx] = value;\n }`};return{name:"Pad",shaderCache:{hint:`${t.mode}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(z.size(r)/64)},programUniforms:s}),getShaderSource:l}},yc=(e,t)=>{if(e.length>1){let r=e[1].getBigInt64Array(),o=e.length>=3&&e[2].data?e[2].getFloat32Array()[0]:0,n=e[0].dims.length,s=new Int32Array(2*n).fill(0);if(e.length>=4){let l=e[3].getBigInt64Array();for(let a=0;as[Number(a)]=Number(l));let u=[];return s.forEach(l=>u.push(l)),{mode:t.mode,value:o,pads:u}}else return t},js=(e,t)=>{lc(e.inputs);let r=yc(e.inputs,t);e.compute(gc(e.inputs,r),{inputs:[0]})}});var dn,Zs,Qs,Xs,Js,bc,wc,eu,tu,ru,nu,ou,au,iu,su,uu,du,lu,cu,pu=q(()=>{"use strict";Kt();ie();_e();be();dn=e=>{if(qt.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},Zs=(e,t,r)=>{let o=t.format==="NHWC",n=e.dims.slice();o&&n.splice(1,0,n.pop());let s=Object.hasOwnProperty.call(t,"dilations"),u=t.kernelShape.slice(),l=t.strides.slice(),a=s?t.dilations.slice():[],p=t.pads.slice();zt.adjustPoolAttributes(r,n,u,l,a,p);let m=zt.computePoolOutputShape(r,n,l,a,u,p,t.autoPad),f=Object.assign({},t);s?Object.assign(f,{kernelShape:u,strides:l,pads:p,dilations:a,cacheKey:t.cacheKey}):Object.assign(f,{kernelShape:u,strides:l,pads:p,cacheKey:t.cacheKey});let b=m.slice();return b.push(b.splice(1,1)[0]),[f,o?b:m]},Qs=(e,t)=>{let r=t.format==="NHWC",o=z.size(e),n=z.size(t.kernelShape),s=[{type:12,data:o},{type:12,data:n}],u=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let l=t.kernelShape[t.kernelShape.length-1],a=t.strides[t.strides.length-1],p=t.pads[t.pads.length/2-1],m=t.pads[t.pads.length-1],f=!!(p+m);s.push({type:12,data:l},{type:12,data:a},{type:12,data:p},{type:12,data:m}),u.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let b=!1;if(t.kernelShape.length===2){let _=t.kernelShape[t.kernelShape.length-2],y=t.strides[t.strides.length-2],$=t.pads[t.pads.length/2-2],I=t.pads[t.pads.length-2];b=!!($+I),s.push({type:12,data:_},{type:12,data:y},{type:12,data:$},{type:12,data:I}),u.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[s,u,!0,f,b]}else{if(r)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let l=z.computeStrides(t.kernelShape);s.push({type:12,data:l},{type:12,data:t.pads},{type:12,data:t.strides}),u.push({name:"kernelStrides",type:"u32",length:l.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let a=t.pads.reduce((p,m)=>p+m);return[s,u,!!a,!1,!1]}},Xs=(e,t,r,o,n,s,u,l,a,p,m,f)=>{let b=n.format==="NHWC",_=t.type.value,y=F("output",t.type.tensor,o);if(n.kernelShape.length<=2){let $="",I="",C="",v=r-(b?2:1);if(m?$=`\n for (var i: u32 = 0u; i < uniforms.kw; i++) {\n xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i;\n if (xIndices[${v}] < 0 || xIndices[${v}]\n >= uniforms.x_shape[${v}]) {\n pad++;\n continue;\n }\n let x_val = x[${t.indicesToOffset("xIndices")}];\n ${s}\n }`:$=`\n for (var i: u32 = 0u; i < uniforms.kw; i++) {\n xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i;\n let x_val = x[${t.indicesToOffset("xIndices")}];\n ${s}\n }`,n.kernelShape.length===2){let T=r-(b?3:2);f?I=`\n for (var j: u32 = 0u; j < uniforms.kh; j++) {\n xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j;\n if (xIndices[${T}] < 0 || xIndices[${T}] >= uniforms.x_shape[${T}]) {\n pad += i32(uniforms.kw);\n continue;\n }\n `:I=`\n for (var j: u32 = 0u; j < uniforms.kh; j++) {\n xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j;\n `,C=`\n }\n `}return`\n ${e.registerUniforms(a).declareVariables(t,y)}\n\n ${e.mainStart()}\n ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}\n\n let indices = ${y.offsetToIndices("global_idx")};\n var xIndices = ${y.offsetToIndices("global_idx")};\n\n var value = ${_}(${l});\n var pad = 0;\n ${I}\n ${$}\n ${C}\n ${u}\n\n output[global_idx] = value;\n }`}else{if(b)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let $=n.kernelShape.length,I=n.pads.length,C="";return p?C=`\n if (xIndices[j] >= uniforms.x_shape[j]) {\n pad++;\n isPad = true;\n break;\n }\n }\n if (!isPad) {\n let x_val = x[${t.indicesToOffset("xIndices")}];\n ${s}\n }`:C=`\n }\n let x_val = x[${t.indicesToOffset("xIndices")}];\n ${s}\n `,`\n ${e.registerUniforms(a).declareVariables(t,y)}\n\n ${e.mainStart()}\n ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}\n let indices = ${y.offsetToIndices("global_idx")};\n var xIndices = ${y.offsetToIndices("global_idx")};\n\n var offsets: array;\n\n var value = ${_}(${l});\n var pad = 0;\n var isPad = false;\n\n for (var i: u32 = 0u; i < uniforms.kernelSize; i++) {\n var offset = i;\n for (var j = 0u; j < ${$-1}u; j++) {\n offsets[j] = offset / ${re("uniforms.kernelStrides","j",$)};\n offset -= offsets[j] * ${re("uniforms.kernelStrides","j",$)};\n }\n offsets[${$-1}] = offset;\n\n isPad = false;\n for (var j = ${r-$}u; j < ${r}u; j++) {\n xIndices[j] = indices[j] * ${re("uniforms.strides",`j - ${r-$}u`,$)}\n + offsets[j - ${r-$}u] - ${re("uniforms.pads","j - 2u",I)};\n ${C}\n }\n ${u}\n\n output[global_idx] = value;\n }`}},Js=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,bc=e=>`${Js(e)};${e.countIncludePad}`,wc=e=>`${Js(e)};${e.storageOrder};${e.dilations}`,eu=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),tu=(e,t,r,o)=>{let[n,s]=Zs(t,o,r),u=M("x",t.dataType,t.dims.length),l=u.type.value,a="value += x_val;",p="";n.countIncludePad?p+=`value /= ${l}(uniforms.kernelSize);`:p+=`value /= ${l}(i32(uniforms.kernelSize) - pad);`;let[m,f,b,_,y]=Qs(s,n);m.push(...j(t.dims,s));let $=["rank"];return{name:e,shaderCache:{hint:`${o.cacheKey};${b};${_};${y}`,inputDependencies:$},getRunData:()=>({outputs:[{dims:s,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(z.size(s)/64)},programUniforms:m}),getShaderSource:I=>Xs(I,u,t.dims.length,s.length,n,a,p,0,f,b,_,y)}},ru=e=>{let t=e.count_include_pad!==0,r=eu(e);if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let o={countIncludePad:t,...r,cacheKey:""};return{...o,cacheKey:bc(o)}},nu=(e,t)=>{dn(e.inputs),e.compute(tu("AveragePool",e.inputs[0],!1,t))},ou={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},au=e=>{let t=e.format;return{format:t,...ou,cacheKey:t}},iu=(e,t)=>{dn(e.inputs),e.compute(tu("GlobalAveragePool",e.inputs[0],!0,t))},su=(e,t,r,o)=>{let[n,s]=Zs(t,o,r),u=`\n value = max(x_val, value);\n `,l="",a=M("x",t.dataType,t.dims.length),p=["rank"],[m,f,b,_,y]=Qs(s,n);return m.push(...j(t.dims,s)),{name:e,shaderCache:{hint:`${o.cacheKey};${b};${_};${y}`,inputDependencies:p},getRunData:()=>({outputs:[{dims:s,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(z.size(s)/64)},programUniforms:m}),getShaderSource:$=>Xs($,a,t.dims.length,s.length,n,u,l,t.dataType===10?-65504:-1e5,f,b,_,y)}},uu=(e,t)=>{dn(e.inputs),e.compute(su("MaxPool",e.inputs[0],!1,t))},du=e=>{let t=e.storage_order,r=e.dilations,o=eu(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(o.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let n={storageOrder:t,dilations:r,...o,cacheKey:""};return{...n,cacheKey:wc(n)}},lu=e=>{let t=e.format;return{format:t,...ou,cacheKey:t}},cu=(e,t)=>{dn(e.inputs),e.compute(su("GlobalMaxPool",e.inputs[0],!0,t))}});var $c,_c,mu,fu=q(()=>{"use strict";Kt();ie();be();$c=(e,t,r)=>{let o=e===t,n=et&&r>0;if(o||n||s)throw new Error("Range these inputs\' contents are invalid.")},_c=(e,t,r,o)=>{let n=Math.abs(Math.ceil((t-e)/r)),s=[n],u=n,l=[{type:12,data:u},{type:o,data:e},{type:o,data:r},...j(s)],a=p=>{let m=F("output",o,s.length),f=m.type.value,b=[{name:"outputSize",type:"u32"},{name:"start",type:f},{name:"delta",type:f}];return`\n ${p.registerUniforms(b).declareVariables(m)}\n ${p.mainStart()}\n ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}\n output[global_idx] = uniforms.start + ${f}(global_idx) * uniforms.delta;\n }`};return{name:"Range",shaderCache:{hint:`${o}`},getShaderSource:a,getRunData:()=>({outputs:[{dims:s,dataType:o}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:l})}},mu=e=>{let t=0,r=0,o=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],o=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],o=e.inputs[2].getFloat32Array()[0]),qt.webgpu.validateInputContent&&$c(t,r,o),e.compute(_c(t,r,o,e.inputs[0].dataType),{inputs:[]})}});var xc,Sc,Cc,Ic,Ac,Tc,Ec,Pc,Oc,kc,Rc,hu,Bc,Dc,Mc,zc,Uc,gu,yu,bu=q(()=>{"use strict";ie();_e();je();be();xc=(e,t)=>{if(e.every(r=>r>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and\n one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Sc=(e,t,r)=>{t.every(n=>n>=0&&n{throw new Error("Resize requires axes input values to be positive and less than rank")}));let o=new Array(r).fill(1);return t.forEach((n,s)=>o[n]=e[s]),o},Cc=(e,t,r,o,n,s)=>{let[u,l,a]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],p=e[0].dims.length;if(u>0&&e.length>u&&e[u].dims.length>0)e[u].getFloat32Array().forEach(m=>s.push(m));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(l>0&&e.length>l&&e[l].dims.length>0){if(e[l].getFloat32Array().forEach(m=>o.push(m)),o.length!==0&&o.length!==p&&r>=18&&o.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");xc(o,t),t.axes.length>0&&Sc(o,t.axes,p).forEach((m,f)=>o[f]=m)}if(a>0&&e.length>a&&(e[a].getBigInt64Array().forEach(m=>n.push(Number(m))),n.length!==p||r>=18&&n.length===t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(o.length!==t.axes.length)throw new Error(\'Resize requires "scales" input size to be of axes rank when axes attributes is specified\');if(n.length!==t.axes.length)throw new Error(\'Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified\')}if(typeof o<"u"&&typeof n<"u"&&o.length>0&&n.length>p)throw new Error("Resize requires only of scales or sizes to be specified")},Ic=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32,\n lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return`return ${t}(xResized) / ${t}(xScale);`;case"pytorch_half_pixel":return`if (lengthResized > 1) {\n return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5;\n } else {\n return 0.0;\n }`;case"tf_half_pixel_for_nn":return`return (${t}(xResized) + 0.5) / ${t}(xScale);`;case"align_corners":return`if (lengthResized == 1) {\n return 0.0;\n } else {\n // The whole part and the fractional part are calculated separately due to inaccuracy of floating\n // point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an\n // offset-by-one error later in floor().\n let whole = ${t}(xResized * (lengthOriginal - 1) / (lengthResized - 1));\n let fract =\n ${t}(xResized * (lengthOriginal - 1) % (lengthResized - 1)) / ${t}(lengthResized - 1);\n return whole + fract;\n }`;case"tf_crop_and_resize":return`if (lengthResized > 1) {\n return ${t}(roiStart) * ${t}(lengthOriginal - 1) +\n (${t}(xResized) * ${t}(roiEnd - roiStart) * ${t}(lengthOriginal - 1)) /\n ${t}(lengthResized - 1);\n } else {\n return 0.5 * ${t}(roiStart + roiEnd) * ${t}(lengthOriginal - 1);\n }`;case"half_pixel_symmetric":return`const outputWidth = ${t}xScale * ${t}(lengthResized);\n const adjustment = ${t}(lengthResized) / outputWidth;\n const center = ${t}(lengthOriginal) / 2;\n const offset = center * (1 - adjustment);\n return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",Ac=(e,t,r)=>`fn getNearestPixelFromOriginal(xOriginal: ${r}, isDownSample: bool) -> ${r} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";case"simple":default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",Tc=(e,t,r)=>{let o=new Array(r).fill(0).concat(new Array(r).fill(1)),n=e.length===0?o:e.slice();return t.length>0?(t.forEach((s,u)=>{o[s]=n[u],o[u+r]=n[t.length+u]}),o):n},Ec=(e,t,r,o)=>{let n=[];if(r.length>0)if(o.length>0){if(e.forEach(s=>n.push(s)),Math.max(...o)>e.length)throw new Error("axes is out of bound");o.forEach((s,u)=>n[s]=r[u])}else r.forEach(s=>n.push(s));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");n=e.map((s,u)=>Math.round(s*t[u]))}return n},Pc=(e,t,r)=>{let o=(()=>{switch(r.keepAspectRatioPolicy){case"not_larger":return r.axes.length>0?Math.min(...r.axes.map(s=>t[s]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return r.axes.length>0?Math.max(...r.axes.map(s=>t[s]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${r.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let n=e.slice();return r.axes.length>0?(r.axes.forEach(s=>t[s]=o),r.axes.forEach(s=>n[s]=Math.round(e[s]*t[s]))):(t.fill(o,0,t.length),n.forEach((s,u)=>n[u]=Math.round(s*t[u]))),n},Oc=(e,t,r,o,n)=>`\n fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${r.length}> {\n var original_indices: array<${e.type.value}, ${r.length}>;\n for (var i:u32 = 0; i < ${r.length}; i++) {\n var output_index = ${e.indicesGet("output_indices","i")};\n var scale = ${re("uniforms.scales","i",o)};\n var roi_low = ${re("uniforms.roi","i",n)};\n var roi_hi = ${re("uniforms.roi",`i + ${t.length}`,n)};\n if (scale == 1.0) {\n original_indices[i] = ${e.type.value}(output_index);\n } else {\n var input_shape_i = ${re("uniforms.input_shape","i",t.length)};\n var output_shape_i = ${re("uniforms.output_shape","i",r.length)};\n original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i,\n input_shape_i, roi_low, roi_hi);\n }\n }\n return original_indices;\n }`,kc=(e,t,r,o,n,s,u)=>`\n fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} {\n var input_indices: ${e.type.indices};\n for (var i:u32 = 0; i < ${o.length}; i++) {\n var output_index = ${t.indicesGet("output_indices","i")};\n var input_index: u32;\n var scale = ${re("uniforms.scales","i",n)};\n if (scale == 1.0) {\n input_index = output_index;\n } else {\n var roi_low = ${re("uniforms.roi","i",s)};\n var roi_hi = ${re("uniforms.roi",`i + ${r.length}`,s)};\n var input_shape_i = ${re("uniforms.input_shape","i",r.length)};\n var output_shape_i = ${re("uniforms.output_shape","i",o.length)};\n var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i,\n input_shape_i, roi_low, roi_hi);\n if (!${u} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) {\n if (original_idx < 0) {\n input_index = 0;\n } else if (original_idx > ${t.type.value}(input_shape_i - 1)) {\n input_index = input_shape_i - 1;\n } else {\n input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1));\n }\n } else {\n input_index = u32(original_idx);\n }\n }\n ${e.indicesSet("input_indices","i"," input_index")}\n }\n return input_indices;\n }`,Rc=(e,t)=>`\n fn checkInputIndices(input_indices: ${e.type.indices}) -> bool {\n for (var i:u32 = 0; i < ${t.length}; i++) {\n var input_index = ${e.indicesGet("input_indices","i")};\n if (input_index < 0 || input_index >= ${re("uniforms.input_shape","i",t.length)}) {\n return false;\n }\n }\n return true;\n }`,hu=(e,t,r,o)=>e.rank>o?`\n ${e.indicesSet("input_indices",t,"channel")};\n ${e.indicesSet("input_indices",r,"batch")};\n`:"",Bc=(e,t,r,o,n)=>{let[u,l,a,p]=r.length===2?[-1,0,1,-1]:[0,2,3,1],m=e.type.value;return`\n fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${m} {\n var input_indices: ${e.type.indices};\n ${e.indicesSet("input_indices",l,`max(0, min(row, ${r[l]} - 1))`)};\n ${e.indicesSet("input_indices",a,`max(0, min(col, ${r[a]} - 1))`)};\n ${hu(e,p,u,2)}\n return ${e.getByIndices("input_indices")};\n }\n\n fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${m} {\n var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices);\n var row:${m} = originalIndices[${l}];\n var col:${m} = originalIndices[${a}];\n ${o?`if (row < 0 || row > (${r[l]} - 1) || col < 0 || col > (${r[a]} - 1)) {\n return ${n};\n }`:""};\n row = max(0, min(row, ${r[l]} - 1));\n col = max(0, min(col, ${r[a]} - 1));\n var row1: u32 = u32(row);\n var col1: u32 = u32(col);\n var row2: u32 = u32(row + 1);\n var col2: u32 = u32(col + 1);\n var channel: u32 = ${r.length>2?`u32(originalIndices[${p}])`:"0"};\n var batch: u32 = ${r.length>2?`u32(originalIndices[${u}])`:"0"};\n var x11: ${m} = getInputValue(batch, channel, row1, col1);\n var x12: ${m} = getInputValue(batch, channel, row1, col2);\n var x21: ${m} = getInputValue(batch, channel, row2, col1);\n var x22: ${m} = getInputValue(batch, channel, row2, col2);\n var dx1: ${m} = abs(row - ${m}(row1));\n var dx2: ${m} = abs(${m}(row2) - row);\n var dy1: ${m} = abs(col - ${m}(col1));\n var dy2: ${m} = abs(${m}(col2) - col);\n if (row1 == row2) {\n dx1 = 0.5;\n dx2 = 0.5;\n }\n if (col1 == col2) {\n dy1 = 0.5;\n dy2 = 0.5;\n }\n return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1);\n }`},Dc=(e,t,r,o,n,s,u,l,a,p)=>{let m=r.length===2,f=!0,[b,_]=m?[0,1]:f?[2,3]:[1,2],y=e.type.value,$=I=>{let C=I===b?"row":"col";return`\n fn ${C}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${y} {\n var output_index = ${t.indicesGet("output_indices",I)};\n var originalIdx: ${y} = getOriginalCoordinateFromResizedCoordinate(output_index, ${n[I]},\n ${o[I]}, ${r[I]}, ${s[I]}, ${s[I]} + ${r.length});\n var fractOriginalIdx: ${y} = originalIdx - floor(originalIdx);\n var coefs = getCubicInterpolationCoefs(fractOriginalIdx);\n\n if (${l} && (originalIdx < 0 || originalIdx > (${r[I]} - 1))) {\n return ${a};\n }\n var data: array<${y}, 4> = array<${y}, 4>(0.0, 0.0, 0.0, 0.0);\n for (var i: i32 = -1; i < 3; i++) {\n var ${C}: ${y} = originalIdx + ${y}(i);\n if (${C} < 0 || ${C} >= ${r[I]}) {\n ${(()=>p?`coefs[i + 1] = 0.0;\n continue;`:l?`return ${a};`:`${C} = max(0, min(${C}, ${r[I]} - 1));`)()};\n }\n var input_indices_copy: ${e.type.indices} = input_indices;\n ${e.indicesSet("input_indices_copy",I,`u32(${C})`)};\n data[i + 1] = ${I===b?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"};\n }\n return cubicInterpolation1D(data, coefs);\n }`};return`\n ${$(b)};\n ${$(_)};\n fn getCubicInterpolationCoefs(s: ${y}) -> array<${y}, 4> {\n var absS = abs(s);\n var coeffs: array<${y}, 4> = array<${y}, 4>(0.0, 0.0, 0.0, 0.0);\n var oneMinusAbsS: ${y} = 1.0 - absS;\n var twoMinusAbsS: ${y} = 2.0 - absS;\n var onePlusAbsS: ${y} = 1.0 + absS;\n coeffs[0] = ((${u} * onePlusAbsS - 5 * ${u}) * onePlusAbsS + 8 * ${u}) * onePlusAbsS - 4 * ${u};\n coeffs[1] = ((${u} + 2) * absS - (${u} + 3)) * absS * absS + 1;\n coeffs[2] = ((${u} + 2) * oneMinusAbsS - (${u} + 3)) * oneMinusAbsS * oneMinusAbsS + 1;\n coeffs[3] = ((${u} * twoMinusAbsS - 5 * ${u}) * twoMinusAbsS + 8 * ${u}) * twoMinusAbsS - 4 * ${u};\n return coeffs;\n }\n\n fn cubicInterpolation1D(x: array<${y}, 4>, coefs: array<${y}, 4>) -> ${y} {\n var coefsSum: ${y} = coefs[0] + coefs[1] + coefs[2] + coefs[3];\n return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum;\n }\n\n fn bicubicInterpolation(output_indices: ${t.type.indices}) -> ${y} {\n var input_indices: ${e.type.indices} = output_indices;\n return colCubicInterpolation(input_indices, output_indices);\n }\n `},Mc=(e,t,r,o,n)=>{let[u,l,a,p,m]=r.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],f=e.type.value;return`\n fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${f} {\n var input_indices: ${e.type.indices};\n ${e.indicesSet("input_indices",l,`max(0, min(depth, ${r[l]} - 1))`)};\n ${e.indicesSet("input_indices",a,`max(0, min(height, ${r[a]} - 1))`)};\n ${e.indicesSet("input_indices",p,`max(0, min(width, ${r[p]} - 1))`)};\n ${hu(e,m,u,3)}\n return ${e.getByIndices("input_indices")};\n }\n\n fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${f} {\n var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices);\n var depth:${f} = originalIndices[${l}];\n var height:${f} = originalIndices[${a}];\n var width:${f} = originalIndices[${p}];\n ${o?`if (depth < 0 || depth > (${r[l]} - 1) || height < 0 || height > (${r[a]} - 1) || width < 0 || (width > ${r[p]} - 1)) {\n return ${n};\n }`:""};\n\n depth = max(0, min(depth, ${r[l]} - 1));\n height = max(0, min(height, ${r[a]} - 1));\n width = max(0, min(width, ${r[p]} - 1));\n var depth1: u32 = u32(depth);\n var height1: u32 = u32(height);\n var width1: u32 = u32(width);\n var depth2: u32 = u32(depth + 1);\n var height2: u32 = u32(height + 1);\n var width2: u32 = u32(width + 1);\n var channel: u32 = ${r.length>3?`u32(originalIndices[${m}])`:"0"};\n var batch: u32 = ${r.length>3?`u32(originalIndices[${u}])`:"0"};\n\n var x111: ${f} = getInputValue(batch, channel, depth1, height1, width1);\n var x112: ${f} = getInputValue(batch, channel, depth1, height1, width2);\n var x121: ${f} = getInputValue(batch, channel, depth1, height2, width1);\n var x122: ${f} = getInputValue(batch, channel, depth1, height2, width2);\n var x211: ${f} = getInputValue(batch, channel, depth2, height1, width1);\n var x212: ${f} = getInputValue(batch, channel, depth2, height1, width2);\n var x221: ${f} = getInputValue(batch, channel, depth2, height2, width1);\n var x222: ${f} = getInputValue(batch, channel, depth2, height2, width2);\n var dx1: ${f} = abs(depth - ${f}(depth1));\n var dx2: ${f} = abs(${f}(depth2) - depth);\n var dy1: ${f} = abs(height - ${f}(height1));\n var dy2: ${f} = abs(${f}(height2) - height);\n var dz1: ${f} = abs(width - ${f}(width1));\n var dz2: ${f} = abs(${f}(width2) - width);\n if (depth1 == depth2) {\n dx1 = 0.5;\n dx2 = 0.5;\n }\n if (height1 == height2) {\n dy1 = 0.5;\n dy2 = 0.5;\n }\n if (width1 == width2) {\n dz1 = 0.5;\n dz2 = 0.5;\n }\n return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 +\n x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1);\n }`},zc=(e,t,r,o,n,s)=>{let u=e.dims,l=Tc(s,t.axes,u.length),a=Ec(u,o,n,t.axes),p=o.slice();o.length===0&&(p=u.map((v,A)=>v===0?1:a[A]/v),t.keepAspectRatioPolicy!=="stretch"&&(a=Pc(u,p,t)));let m=F("output",e.dataType,a.length),f=M("input",e.dataType,u.length),b=z.size(a),_=u.length===a.length&&u.every((v,A)=>v===a[A]),y=t.coordinateTransformMode==="tf_crop_and_resize",$=t.extrapolationValue,I=f.type.value,C=v=>`\n ${_?"":`\n ${Ic(t.coordinateTransformMode,I)};\n ${(()=>{switch(t.mode){case"nearest":return`\n ${Rc(f,u)};\n ${Ac(t.nearestMode,r,I)};\n ${kc(f,m,u,a,p.length,l.length,y)};\n `;case"linear":return`\n ${Oc(m,u,a,p.length,l.length)};\n ${(()=>{if(u.length===2||u.length===4)return`${Bc(f,m,u,y,$)}`;if(u.length===3||u.length===5)return`${Mc(f,m,u,y,$)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()};\n `;case"cubic":return`\n ${(()=>{if(u.length===2||u.length===4)return`${Dc(f,m,u,a,p,l,t.cubicCoeffA,y,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()};\n `;default:throw Error("Invalid resize mode")}})()};\n `}\n ${v.registerUniform("output_size","u32").registerUniform("scales","f32",p.length).registerUniform("roi","f32",l.length).declareVariables(f,m)}\n ${v.mainStart()}\n ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}\n ${_?"output[global_idx] = input[global_idx];":`\n let output_indices = ${m.offsetToIndices("global_idx")};\n var input_indices: ${f.type.indices};\n ${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices);\n if (checkInputIndices(input_indices)) {\n output[global_idx] = ${f.getByIndices("input_indices")};\n } else {\n output[global_idx] = ${t.extrapolationValue};\n }`;case"linear":return`output[global_idx] = ${u.length===2||u.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()};\n`}\n }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${p.length>0?p:""}|${n.length>0?n:""}|${l.length>0?l:""}|${_}|${u}`,inputDependencies:["rank"]},getShaderSource:C,getRunData:()=>({outputs:[{dims:a,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(b/64)},programUniforms:[{type:12,data:b},{type:1,data:p},{type:1,data:l},...j(u,a)]})}},Uc=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},gu=(e,t)=>{let r=[],o=[],n=[],s=Uc(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Cc(e.inputs,t,s,r,o,n),e.compute(zc(e.inputs[0],t,s,r,o,n),{inputs:[0]})},yu=e=>{let t=e.antialias,r=e.axes,o=e.coordinateTransformMode,n=e.cubicCoeffA,s=e.excludeOutside!==0,u=e.extrapolationValue,l=e.keepAspectRatioPolicy,a=e.mode,p=e.nearestMode===""?"simple":e.nearestMode;return $e({antialias:t,axes:r,coordinateTransformMode:o,cubicCoeffA:n,excludeOutside:s,extrapolationValue:u,keepAspectRatioPolicy:l,mode:a,nearestMode:p})}});var Vc,Nc,wu,vu=q(()=>{"use strict";ie();_e();be();Vc=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],r=e[1],o=e[2];if(t.dataType!==r.dataType||t.dataType!==o.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(r.dims.length!==3&&r.dims.length!==2)throw new Error("Skip must be 2D or 3D");let n=t.dims[t.dims.length-1],s=t.dims[t.dims.length-2];if(r.dims[r.dims.length-1]!==n)throw new Error("Skip must have the same hidden size as input");if(r.dims[r.dims.length-2]!==s)throw new Error("Skip must have the same sequence length as input");if(o.dims.length!==1)throw new Error("Gamma must be 1D");if(o.dims[o.dims.length-1]!==n)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let u=e[3];if(u.dims.length!==1)throw new Error("Beta must be 1D");if(u.dims[u.dims.length-1]!==n)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let u=e[4];if(u.dims.length!==1)throw new Error("Bias must be 1D");if(u.dims[u.dims.length-1]!==n)throw new Error("Bias must have the same hidden size as input")}},Nc=(e,t,r,o)=>{let n=e[0].dims,s=z.size(n),u=n,l=s,a=n.slice(-1)[0],p=o?n.slice(0,-1).concat(1):[],m=e.length>3,f=e.length>4,b=o&&r>1,_=o&&r>2,y=r>3,$=Me(a),I=[{type:12,data:l},{type:12,data:$},{type:12,data:a},{type:1,data:t.epsilon}],C=A=>{let T=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],D=[M("x",e[0].dataType,e[0].dims,$),M("skip",e[1].dataType,e[1].dims,$),M("gamma",e[2].dataType,e[2].dims,$)];m&&D.push(M("beta",e[3].dataType,e[3].dims,$)),f&&D.push(M("bias",e[4].dataType,e[4].dims,$)),D.push(F("output",e[0].dataType,u,$)),b&&D.push(F("mean_output",1,p)),_&&D.push(F("inv_std_output",1,p)),y&&D.push(F("input_skip_bias_sum",e[0].dataType,u,$));let U=Pe(e[0].dataType);return`\n\n ${A.registerUniforms(T).declareVariables(...D)}\n\n ${A.mainStart()}\n ${A.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size / uniforms.hidden_size")}\n let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components;\n let offset = global_idx * hidden_size_vectorized;\n var sum = ${Ye("f32",$)};\n var squareSum = ${Ye("f32",$)};\n for (var i: u32 = 0; i < hidden_size_vectorized; i++) {\n let skip_value = skip[offset + i];\n let bias_value = ${f?"bias[i]":"0.0"};\n let input_value = x[offset + i];\n let value = input_value + skip_value + bias_value;\n ${y?"input_skip_bias_sum[offset + i] = value;":""}\n output[offset + i] = value;\n let f32_value = ${st(U,$,"value")};\n sum += f32_value;\n squareSum += f32_value * f32_value;\n }\n let mean = ${tt("sum",$)} / f32(uniforms.hidden_size);\n let inv_std_dev = inverseSqrt(${tt("squareSum",$)} / f32(uniforms.hidden_size) - mean * mean + uniforms.epsilon);\n ${b?"mean_output[global_idx] = mean;":""}\n ${_?"inv_std_output[global_idx] = inv_std_dev;":""}\n for (var i: u32 = 0; i < hidden_size_vectorized; i++) {\n output[offset + i] = (output[offset + i] - ${U}(mean)) * ${U}(inv_std_dev) * gamma[i] + ${m?"beta[i]":"0.0"};\n }\n }`},v=[{dims:u,dataType:e[0].dataType}];return r>1&&v.push({dims:p,dataType:1}),r>2&&v.push({dims:p,dataType:1}),r>3&&v.push({dims:n,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${$};${b};${_};${y}`,inputDependencies:e.map((A,T)=>"type")},getShaderSource:C,getRunData:()=>({outputs:v,dispatchGroup:{x:Math.ceil(l/a/64)},programUniforms:I})}},wu=(e,t)=>{Vc(e.inputs);let o=[0];e.outputCount>1&&o.push(-3),e.outputCount>2&&o.push(-3),e.outputCount>3&&o.push(3),e.compute(Nc(e.inputs,t,e.outputCount,!1),{outputs:o})}});var Wc,ln,Gc,$u,Hc,Lc,_u,xu,Su=q(()=>{"use strict";ie();_e();je();be();Wc=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((r,o)=>{if(e[o+1].dataType!==6&&e[o+1].dataType!==7)throw new Error(`Input ${o} must be an array of int32 or int64`)})},ln=(e,t)=>{let r=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(o=>r.push(Number(o)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(o=>r.push(Number(o)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return r},Gc=(e,t)=>{if(e.length>1){let r=ln(e,1),o=ln(e,2),n=ln(e,3);return n.length===0&&(n=[...Array(e[0].dims.length).keys()]),$e({starts:r,ends:o,axes:n})}else return t},$u=(e,t,r,o,n)=>{let s=e;return e<0&&(s+=r[o[t]]),n[t]<0?Math.max(0,Math.min(s,r[o[t]]-1)):Math.max(0,Math.min(s,r[o[t]]))},Hc=(e,t,r)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} {\n var input_indices: ${e.type.indices};\n var carry = 0u;\n for (var i = ${r.length}; i >= 0; i--) {\n let input_shape_i = ${re("uniforms.input_shape","i",r.length)};\n let steps_i = ${re("uniforms.steps","i",r.length)};\n let signs_i = ${re("uniforms.signs","i",r.length)};\n let starts_i = ${re("uniforms.starts","i",r.length)};\n var output_index = ${t.indicesGet("output_indices","i")};\n var input_index = output_index * steps_i + starts_i + carry;\n carry = input_index / input_shape_i;\n input_index = input_index % input_shape_i;\n if (signs_i < 0) {\n input_index = input_shape_i - input_index - 1u + starts_i;\n }\n ${e.indicesSet("input_indices","i","input_index")};\n }\n return input_indices;\n }`,Lc=(e,t)=>{let r=e[0].dims,o=z.size(r),n=t.axes.length>0?z.normalizeAxes(t.axes,r.length):[...Array(r.length).keys()],s=ln(e,4);s.forEach(C=>C!==0||(()=>{throw new Error("step cannot be 0")})),s.length===0&&(s=Array(n.length).fill(1));let u=t.starts.map((C,v)=>$u(C,v,r,n,s)),l=t.ends.map((C,v)=>$u(C,v,r,n,s));if(n.length!==u.length||n.length!==l.length)throw new Error("start, ends and axes should have the same number of elements");if(n.length!==r.length)for(let C=0;CMath.sign(C));s.forEach((C,v,A)=>{if(C<0){let T=(l[v]-u[v])/C,D=u[v],U=D+T*s[v];u[v]=U,l[v]=D,A[v]=-C}});let p=r.slice(0);n.forEach((C,v)=>{p[C]=Math.ceil((l[C]-u[C])/s[C])});let m={dims:p,dataType:e[0].dataType},f=F("output",e[0].dataType,p.length),b=M("input",e[0].dataType,e[0].dims.length),_=z.size(p),y=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:u.length},{name:"signs",type:"i32",length:a.length},{name:"steps",type:"u32",length:s.length}],$=[{type:12,data:_},{type:12,data:u},{type:6,data:a},{type:12,data:s},...j(e[0].dims,p)],I=C=>`\n ${C.registerUniforms(y).declareVariables(b,f)}\n ${Hc(b,f,r)}\n ${C.mainStart()}\n ${C.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}\n let output_indices = ${f.offsetToIndices("global_idx")};\n let input_indices = calculateInputIndices(output_indices);\n ${f.setByOffset("global_idx",b.getByIndices("input_indices"))}\n }`;return{name:"Slice",shaderCache:{hint:`${a.length}_${u.length}_${s.length}`,inputDependencies:["rank"]},getShaderSource:I,getRunData:()=>({outputs:[m],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:$})}},_u=(e,t)=>{Wc(e.inputs,t);let r=Gc(e.inputs,t);e.compute(Lc(e.inputs,r),{inputs:[0]})},xu=e=>{let t=e.starts,r=e.ends,o=e.axes;return $e({starts:t,ends:r,axes:o})}});var Fc,qc,Cu,Iu,Au=q(()=>{"use strict";ie();_e();je();be();Fc=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},qc=(e,t)=>{let r=e.dims,o=z.size(r),n=64,s=t.axis;if(s<0&&(s=r.length+s),sC===4?`max(max(${I}.x, ${I}.y), max(${I}.z, ${I}.w))`:C===2?`max(${I}.x, ${I}.y)`:C===3?`max(max(${I}.x, ${I}.y), ${I}.z)`:I,f=M("x",e.dataType,e.dims,a),b=F("result",e.dataType,e.dims,a),_=f.type.value,y=Pe(e.dataType)==="f32"?`var threadMax = ${_}(-3.402823e+38f);`:`var threadMax = ${_}(-65504.0h);`,$=I=>`\n var rowMaxShared : ${_};\n var rowSumShared : ${_};\n var threadShared : array<${_}, ${n}>;\n\n fn getValue(row: i32, col: i32, row_stride: i32) -> ${_} {\n let index = row * row_stride + col;\n return x[index];\n }\n\n fn setValue(row: i32, col: i32, row_stride: i32, value: ${_}) {\n let index = row * row_stride + col;\n result[index] = value;\n }\n ${I.registerUniform("packedCols","i32").declareVariables(f,b)}\n ${I.mainStart()}\n let gindex = i32(global_idx);\n let lindex = i32(local_idx);\n const wg = ${n};\n let row = gindex / wg;\n let cols = uniforms.packedCols;\n let row_stride : i32 = uniforms.packedCols;\n\n // find the rows max\n ${y}\n for (var col = lindex; col < cols; col += wg) {\n let value = getValue(row, col, row_stride);\n threadMax = max(threadMax, value);\n }\n if (lindex < cols) {\n threadShared[lindex] = threadMax;\n }\n workgroupBarrier();\n\n var reduceSize = min(cols, wg);\n for (var currSize = reduceSize >> 1; currSize > 0; currSize = reduceSize >> 1) {\n reduceSize = currSize + (reduceSize & 1);\n if (lindex < currSize) {\n threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]);\n }\n workgroupBarrier();\n }\n if (lindex == 0) {\n rowMaxShared = ${_}(${m("threadShared[0]",a)});\n }\n workgroupBarrier();\n\n // find the rows sum\n var threadSum = ${_}(0.0);\n for (var col = lindex; col < cols; col += wg) {\n let subExp = exp(getValue(row, col, row_stride) - rowMaxShared);\n threadSum += subExp;\n }\n threadShared[lindex] = threadSum;\n workgroupBarrier();\n\n for (var currSize = wg >> 1; currSize > 0; currSize = currSize >> 1) {\n if (lindex < currSize) {\n threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize];\n }\n workgroupBarrier();\n }\n if (lindex == 0) {\n rowSumShared = ${_}(${tt("threadShared[0]",a)});\n }\n workgroupBarrier();\n\n // calculate final value for each element in the row\n for (var col = lindex; col < cols; col += wg) {\n let value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared;\n setValue(row, col, row_stride, value);\n }\n }`;return{name:"Softmax",shaderCache:{hint:`${a}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.dataType}],dispatchGroup:{x:l},programUniforms:[{type:12,data:p}]}),getShaderSource:$}},Cu=(e,t)=>{Fc(e.inputs),e.compute(qc(e.inputs[0],t))},Iu=e=>$e({axis:e.axis})});var Kc,jc,Yc,Zc,Qc,Tu,Eu,Pu=q(()=>{"use strict";ie();_e();je();be();Kc=e=>{if(!e||e.length<1)throw new Error("too few inputs")},jc=(e,t)=>{let r=[],o=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(n=>r.push(Number(n))),o=r.length),$e({numOutputs:o,axis:t.axis,splitSizes:r})},Yc=e=>`\nfn calculateOutputIndex(index: u32) -> u32 {\n for (var i: u32 = 0u; i < ${e}u; i += 1u ) {\n if (index < ${re("uniforms.size_in_split_axis","i",e)}) {\n return i;\n }\n }\n return ${e}u;\n}`,Zc=e=>{let t=e.length,r=[];for(let o=0;o{let r=e[0].dims,o=z.size(r),n=e[0].dataType,s=z.normalizeAxis(t.axis,r.length),u=new Array(t.numOutputs),l=M("input",n,r.length),a=new Array(t.numOutputs),p=[],m=[],f=0,b=[{type:12,data:o}];for(let y=0;y`\n ${y.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",a.length).declareVariables(l,...u)}\n ${Yc(a.length)}\n ${Zc(u)}\n\n ${y.mainStart()}\n ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")}\n\n var indices = ${l.offsetToIndices("global_idx")};\n var index = ${l.indicesGet("indices",s)};\n let output_number = calculateOutputIndex(index);\n if (output_number != 0) {\n index -= ${re("uniforms.size_in_split_axis","output_number - 1u",a.length)};\n ${l.indicesSet("indices",s,"index")};\n }\n writeBufferData(output_number, indices, global_idx);\n }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:_,getRunData:()=>({outputs:p,dispatchGroup:{x:Math.ceil(o/64)},programUniforms:b})}},Tu=(e,t)=>{Kc(e.inputs);let r=e.inputs.length===1?t:jc(e.inputs,t);e.compute(Qc(e.inputs,r),{inputs:[0]})},Eu=e=>{let t=e.axis,r=e.splitSizes,o=e.numOutputs<0?r.length:e.numOutputs;if(o!==r.length)throw new Error("numOutputs and splitSizes lengh must be equal");return $e({axis:t,numOutputs:o,splitSizes:r})}});var Ou,Xc,Jc,ep,ku,Ru=q(()=>{"use strict";ie();_e();be();Ou=e=>Array.from(e.getBigInt64Array(),Number),Xc=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(Ou(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},Jc=(e,t)=>{let r=[];for(let o=0;o{let t=e[0].dims,r=Ou(e[1]),o=Jc(t,r),n=z.size(o),s=e[0].dataType,u=M("input",s,t.length),l=F("output",s,o.length),a=p=>`\n const inputShape = ${u.indices(...t)};\n ${p.registerUniform("output_size","u32").declareVariables(u,l)}\n ${p.mainStart()}\n ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}\n let output_indices = ${l.offsetToIndices("global_idx")};\n var input_indices: ${u.type.indices};\n for (var i = 0; i < ${t.length}; i++) {\n let input_dim_i = ${u.indicesGet("uniforms.input_shape","i")};\n let input_dim_value = ${l.indicesGet("output_indices","i")} % input_dim_i;\n\n ${u.indicesSet("input_indices","i","input_dim_value")}\n }\n ${l.setByOffset("global_idx",u.getByIndices("input_indices"))}\n }`;return{name:"Tile",shaderCache:{hint:`${r}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:[{type:12,data:n},...j(e[0].dims,o)]}),getShaderSource:a}},ku=e=>{Xc(e.inputs),e.compute(ep(e.inputs),{inputs:[0]})}});var tp,rp,Bu,Du=q(()=>{"use strict";ie();_e();be();tp=(e,t,r,o,n)=>{let s=F("output_data",n,r.length,4),u=M("a_data",t[1].dataType,t[1].dims.length,4),l=M("b_data",t[2].dataType,t[2].dims.length,4),a=M("c_data",t[0].dataType,t[0].dims.length,4),p,m=(f,b,_)=>`select(${b}, ${f}, ${_})`;if(!o)p=s.setByOffset("global_idx",m(u.getByOffset("global_idx"),l.getByOffset("global_idx"),a.getByOffset("global_idx")));else{let f=(b,_,y="")=>{let $=`a_data[index_a${_}][component_a${_}]`,I=`b_data[index_b${_}][component_b${_}]`,C=`bool(c_data[index_c${_}] & (0xffu << (component_c${_} * 8)))`;return`\n let output_indices${_} = ${s.offsetToIndices(`global_idx * 4u + ${_}u`)};\n let offset_a${_} = ${u.broadcastedIndicesToOffset(`output_indices${_}`,s)};\n let offset_b${_} = ${l.broadcastedIndicesToOffset(`output_indices${_}`,s)};\n let offset_c${_} = ${a.broadcastedIndicesToOffset(`output_indices${_}`,s)};\n let index_a${_} = offset_a${_} / 4u;\n let index_b${_} = offset_b${_} / 4u;\n let index_c${_} = offset_c${_} / 4u;\n let component_a${_} = offset_a${_} % 4u;\n let component_b${_} = offset_b${_} % 4u;\n let component_c${_} = offset_c${_} % 4u;\n ${b}[${_}] = ${y}(${m($,I,C)});\n `};n===9?p=`\n var data = vec4(0);\n ${f("data",0,"u32")}\n ${f("data",1,"u32")}\n ${f("data",2,"u32")}\n ${f("data",3,"u32")}\n output_data[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:p=`\n ${f("output_data[global_idx]",0)}\n ${f("output_data[global_idx]",1)}\n ${f("output_data[global_idx]",2)}\n ${f("output_data[global_idx]",3)}\n `}return`\n ${e.registerUniform("vec_size","u32").declareVariables(a,u,l,s)}\n ${e.mainStart()}\n ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}\n ${p}\n }`},rp=e=>{let t=e[1].dims,r=e[2].dims,o=e[0].dims,n=e[1].dataType,s=!(z.areEqual(t,r)&&z.areEqual(r,o)),u=t,l=z.size(t);if(s){let p=mt.calcShape(mt.calcShape(t,r,!1),o,!1);if(!p)throw new Error("Can\'t perform where op on the given tensors");u=p,l=z.size(u)}let a=Math.ceil(l/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:p=>tp(p,e,u,s,n),getRunData:()=>({outputs:[{dims:u,dataType:n}],dispatchGroup:{x:Math.ceil(l/64/4)},programUniforms:[{type:12,data:a},...j(o,t,r,u)]})}},Bu=e=>{e.compute(rp(e.inputs))}});var Mu,zu=q(()=>{"use strict";Qa();Hn();ei();ri();Ni();Qi();es();jn();fs();ys();_s();Cs();As();Ps();Rs();Ms();Us();Ns();Zn();Hs();Ks();Ys();pu();fu();Yr();bu();vu();Su();Au();Pu();Ru();Yt();en();Du();Mu=new Map([["Abs",[ni]],["Acos",[oi]],["Acosh",[ai]],["Add",[Wi]],["ArgMax",[Za,Gn]],["ArgMin",[Ya,Gn]],["Asin",[ii]],["Asinh",[si]],["Atan",[ui]],["Atanh",[di]],["Attention",[Xa]],["AveragePool",[nu,ru]],["BatchNormalization",[Ja]],["BiasAdd",[ti]],["BiasSplitGelu",[Vi]],["Cast",[ci,li]],["Ceil",[mi]],["Clip",[pi]],["Concat",[Xi,Ji]],["Conv",[Jn,Xn]],["ConvTranspose",[ms,ps]],["Cos",[fi]],["Cosh",[hi]],["CumSum",[hs,gs]],["Div",[Gi]],["Einsum",[vs,$s]],["Elu",[gi,Xr]],["Equal",[Hi]],["Erf",[yi]],["Exp",[bi]],["Expand",[Ss]],["FastGelu",[Is]],["Floor",[wi]],["FusedConv",[Jn,Xn]],["Gather",[Es,Ts]],["GatherElements",[ks,Os]],["Gelu",[vi]],["Gemm",[Ds,Bs]],["GlobalAveragePool",[iu,au]],["GlobalMaxPool",[cu,lu]],["Greater",[Ki]],["GreaterOrEqual",[Yi]],["HardSigmoid",[Ti,Ai]],["InstanceNormalization",[zs]],["LayerNormalization",[Vs]],["LeakyRelu",[$i,Xr]],["Less",[ji]],["LessOrEqual",[Zi]],["Log",[zi]],["MatMul",[is]],["MatMulNBits",[Ws,Gs]],["MaxPool",[uu,du]],["Mul",[Li]],["MultiHeadAttention",[qs,Fs]],["Neg",[xi]],["Not",[_i]],["Pad",[js]],["Pow",[Fi]],["Range",[mu]],["Reciprocal",[Si]],["ReduceMin",[Ha]],["ReduceMean",[Ua]],["ReduceMax",[Ga]],["ReduceSum",[Fa]],["ReduceProd",[La]],["ReduceL1",[Va]],["ReduceL2",[Na]],["ReduceLogSum",[Ka]],["ReduceLogSumExp",[Wa]],["ReduceSumSquare",[qa]],["Relu",[Ci]],["Resize",[gu,yu]],["Sigmoid",[Ii]],["Sin",[Ei]],["Sinh",[Pi]],["Slice",[_u,xu]],["SkipLayerNormalization",[wu]],["Split",[Tu,Eu]],["Sqrt",[Oi]],["Softmax",[Cu,Iu]],["Sub",[qi]],["Tan",[ki]],["Tanh",[Bi]],["ThresholdedRelu",[Mi,Xr]],["Tile",[ku]],["Transpose",[Ca,Ia]],["Where",[Bu]]])});var cn,Uu=q(()=>{"use strict";Kt();Pt();be();cn=class{constructor(t){this.backend=t;this.repo=new Map,this.attributesBound=!1}getArtifact(t){return this.repo.get(t)}setArtifact(t,r){this.repo.set(t,r)}run(t,r,o,n,s){Mt(t.programInfo.name);let u=this.backend.device,l=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let a=[];for(let m of r)a.push({binding:a.length,resource:{buffer:m.buffer}});for(let m of o)a.push({binding:a.length,resource:{buffer:m.buffer}});s&&a.push({binding:a.length,resource:s});let p=u.createBindGroup({layout:t.computePipeline.getBindGroupLayout(0),entries:a,label:t.programInfo.name});if(this.backend.sessionStatus==="capturing"){let m={kernelId:this.backend.currentKernelId,computePipeline:t.computePipeline,bindGroup:p,dispatchGroup:n};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(m)}l.setPipeline(t.computePipeline),l.setBindGroup(0,p),l.dispatchWorkgroups(...n),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),Et(t.programInfo.name)}dispose(){}build(t,r){Mt(t.name);let o=this.backend.device,n=[];o.features.has("shader-f16")&&n.push("enable f16;");let s=xa(r),u=t.getShaderSource(s),l=`${n.join(`\n`)}\n${s.additionalImplementations}\n${u}`,a=o.createShaderModule({code:l,label:t.name});De("verbose",()=>`[WebGPU] ${t.name} shader code: ${l}`);let p=o.createComputePipeline({compute:{module:a,entryPoint:"main"},layout:"auto",label:t.name});return Et(t.name),{programInfo:t,computePipeline:p}}normalizeDispatchGroupSize(t){let r=typeof t=="number"?t:t.x,o=typeof t=="number"?1:t.y||1,n=typeof t=="number"?1:t.z||1,s=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(r<=s&&o<=s&&n<=s)return[r,o,n];let u=r*o*n,l=Math.ceil(Math.sqrt(u));if(l>s){if(l=Math.ceil(Math.cbrt(u)),l>s)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[l,l,l]}else return[l,l,1]}}});var np,op,ao,pn,Vu=q(()=>{"use strict";Kt();ie();Pt();ba();_a();zu();Uu();np=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let r=[];for(let o=0;o{let o=e.name;return e.shaderCache?.hint&&(o+="["+e.shaderCache.hint+"]"),o+=":"+r+`:${np(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,o},ao=class{constructor(t){t&&(this.architecture=t.architecture,this.vendor=t.vendor)}isArchitecture(t){return this.architecture===t}isVendor(t){return this.vendor===t}},pn=class{constructor(){this.currentSessionId=null;this.currentKernelId=null;this.commandEncoder=null;this.computePassEncoder=null;this.maxDispatchNumber=16;this.pendingDispatchNumber=0;this.pendingKernels=[];this.pendingQueries=new Map;this.sessionStatus="default";this.capturedCommandList=new Map;this.capturedPendingKernels=new Map;this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let t=this.kernelCustomData.get(this.currentKernelId);return t||(t={},this.kernelCustomData.set(this.currentKernelId,t)),t}async initialize(t,r){this.env=t;let o=[],n={requiredLimits:{maxComputeWorkgroupStorageSize:r.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:r.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:r.limits.maxStorageBufferBindingSize,maxBufferSize:r.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:r.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:r.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:r.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:r.limits.maxComputeWorkgroupSizeZ},requiredFeatures:o};r.features.has("chromium-experimental-timestamp-query-inside-passes")?o.push("chromium-experimental-timestamp-query-inside-passes"):r.features.has("timestamp-query")&&o.push("timestamp-query"),r.features.has("shader-f16")&&o.push("shader-f16"),this.device=await r.requestDevice(n),this.adapterInfo=new ao(await r.requestAdapterInfo()),this.gpuDataManager=$a(this),this.programManager=new cn(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,ga(t.logLevel,!!t.debug),this.device.onuncapturederror=s=>{s.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${s.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:r,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let t=this.getCommandEncoder(),r={};this.queryType==="at-passes"&&(r.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=t.beginComputePass(r)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Mt(),this.endComputePass();let t;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),t=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(t,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,t,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&t.mapAsync(GPUMapMode.READ).then(()=>{let r=new BigUint64Array(t.getMappedRange()),o=this.pendingQueries.get(t);for(let n=0;n"u"&&(this.queryTimeBase=_);let $=Number(_-this.queryTimeBase),I=Number(y-this.queryTimeBase);if(!Number.isSafeInteger($)||!Number.isSafeInteger(I))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:f.map(C=>({dims:C.dims,dataType:Tt(C.dataType)})),outputsMetadata:b.map(C=>({dims:C.dims,dataType:Tt(C.dataType)})),kernelId:u,kernelType:a,kernelName:p,programName:m,startTime:$,endTime:I});else{let C="";f.forEach((A,T)=>{C+=`input[${T}]: [${A.dims}] | ${Tt(A.dataType)}, `});let v="";b.forEach((A,T)=>{v+=`output[${T}]: [${A.dims}] | ${Tt(A.dataType)}, `}),console.log(`[profiling] kernel "${u}|${a}|${p}|${m}" ${C}${v}execution time: ${I-$} ns`)}kn("GPU",`${m}::${_}::${y}`)}t.unmap(),this.pendingQueries.delete(t)}),Et()}run(t,r,o,n,s){Mt(t.name);let u=[];for(let v=0;vA):o;if(m.length!==l.length)throw new Error(`Output size ${m.length} must be equal to ${l.length}.`);let f=[],b=[];for(let v=0;v=l.length)throw new Error(`Invalid output index: ${m[v]}`);if(m[v]===-3)continue;let A=m[v]===-1,T=m[v]===-2,D=A||T?s(l[v].dataType,l[v].dims):n(m[v],l[v].dataType,l[v].dims);if(f.push(D),D.data===0)continue;let U=this.gpuDataManager.get(D.data);if(!U)throw new Error(`no GPU data for output: ${D.data}`);if(A&&this.temporaryData.push(U),T){let V=this.kernelPersistentData.get(this.currentKernelId);V||(V=[],this.kernelPersistentData.set(this.currentKernelId,V)),V.push(U)}b.push(U)}if(u.length!==r.length||b.length!==f.length){if(b.length===0)return Et(t.name),f;throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let _;if(p){let v=0,A=[];p.forEach(V=>{let H=typeof V.data=="number"?[V.data]:V.data;if(H.length===0)return;let R=V.type===10?2:4,L,pe;V.type===10?(pe=H.length>4?16:H.length>2?8:H.length*R,L=H.length>4?16:R*H.length):(pe=H.length<=2?H.length*R:16,L=16),v=Math.ceil(v/pe)*pe,A.push(v);let Ie=V.type===10?8:4;v+=H.length>4?Math.ceil(H.length/Ie)*L:H.length*R});let T=16;v=Math.ceil(v/T)*T;let D=new ArrayBuffer(v);p.forEach((V,H)=>{let R=A[H],L=typeof V.data=="number"?[V.data]:V.data;if(V.type===6)new Int32Array(D,R,L.length).set(L);else if(V.type===12)new Uint32Array(D,R,L.length).set(L);else if(V.type===10)new Uint16Array(D,R,L.length).set(L);else if(V.type===1)new Float32Array(D,R,L.length).set(L);else throw new Error(`Unsupported uniform type: ${Tt(V.type)}`)});let U=this.gpuDataManager.create(v,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(U.buffer,0,D,0,v),this.gpuDataManager.release(U.id),_={offset:0,size:v,buffer:U.buffer}}let y=this.programManager.normalizeDispatchGroupSize(a),$=y[1]===1&&y[2]===1,I=op(t,r,$),C=this.programManager.getArtifact(I);if(C||(C=this.programManager.build(t,y),this.programManager.setArtifact(I,C),De("info",()=>`[artifact] key: ${I}, programName: ${t.name}`)),De("info",()=>`[ProgramManager] run "${t.name}" (key=${I}) with ${y[0]}x${y[1]}x${y[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let v={kernelId:this.currentKernelId,programName:C.programInfo.name,inputTensorViews:r,outputTensorViews:f};this.pendingKernels.push(v),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(v)}return this.programManager.run(C,u,b,y,_),Et(t.name),f}upload(t,r){this.gpuDataManager.upload(t,r)}memcpy(t,r){this.gpuDataManager.memcpy(t,r)}async download(t,r){await this.gpuDataManager.download(t,r)}alloc(t){return this.gpuDataManager.create(t).id}free(t){return this.gpuDataManager.release(t)}createKernel(t,r,o,n){let s=Mu.get(t);if(!s)throw new Error(`kernel not implemented: ${t}`);let u={kernelType:t,kernelName:n,kernelEntry:s[0],attributes:[s[1],o]};this.kernels.set(r,u)}releaseKernel(t){let r=this.kernelPersistentData.get(t);if(r){for(let o of r)this.gpuDataManager.release(o.id);this.kernelPersistentData.delete(t)}this.kernelCustomData.delete(t),this.kernels.delete(t)}computeKernel(t,r,o){let n=this.kernels.get(t);if(!n)throw new Error(`kernel not created: ${t}`);let s=n.kernelType,u=n.kernelName,l=n.kernelEntry,a=n.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${s}] ${u}" is not allowed to be called recursively`);this.currentKernelId=t,a[0]&&(a[1]=a[0](a[1]),a[0]=void 0),De("info",()=>`[WebGPU] Start to run kernel "[${s}] ${u}"...`);let p=this.env.debug;this.temporaryData=[];try{return p&&this.device.pushErrorScope("validation"),l(r,a[1]),0}catch(m){return o.push(Promise.resolve(`[WebGPU] Kernel "[${s}] ${u}" failed. ${m}`)),1}finally{p&&o.push(this.device.popErrorScope().then(m=>m?`GPU validation error for kernel "[${s}] ${u}": ${m.message}`:null));for(let m of this.temporaryData)this.gpuDataManager.release(m.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(t,r,o,n){let s=this.sessionExternalDataMapping.get(t);s||(s=new Map,this.sessionExternalDataMapping.set(t,s));let u=s.get(r),l=this.gpuDataManager.registerExternalBuffer(o,n,u?.[1]);return s.set(r,[l,o]),l}unregisterBuffers(t){let r=this.sessionExternalDataMapping.get(t);r&&(r.forEach(o=>this.gpuDataManager.unregisterExternalBuffer(o[1])),this.sessionExternalDataMapping.delete(t))}getBuffer(t){let r=this.gpuDataManager.get(t);if(!r)throw new Error(`no GPU data for buffer: ${t}`);return r.buffer}createDownloader(t,r,o){return async()=>{let n=await Dn(this,t,r);return ya(n.buffer,o)}}writeTimestamp(t){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,t)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){De("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){De("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){De("info","replay"),this.sessionStatus="replaying";let t=this.capturedCommandList.get(this.currentSessionId),r=this.capturedPendingKernels.get(this.currentSessionId),o=t.length;this.pendingKernels=[];for(let n=0;n=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onReleaseSession(t){this.unregisterBuffers(t),this.capturedCommandList.has(t)&&this.capturedCommandList.delete(t),this.capturedPendingKernels.has(t)&&this.capturedPendingKernels.delete(t),this.gpuDataManager.onReleaseSession(t)}onRunStart(t){this.currentSessionId=t,this.setQueryType()}}});var Nu={};Mr(Nu,{init:()=>ap});var wr,io,ap,Wu=q(()=>{"use strict";ie();Vu();Pt();_e();wr=class e{constructor(t,r,o,n){this.module=t;this.dataType=r;this.data=o;this.dims=n}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=z.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(z.size(t)!==z.size(this.dims))throw new Error("Invalid new shape");return new e(this.module,this.dataType,this.data,t)}},io=class{constructor(t,r,o){this.module=t;this.backend=r;this.customDataOffset=0;this.customDataSize=0;this.adapterInfo=r.adapterInfo;let n=t.HEAPU32,s=o>>>2;this.opKernelContext=n[s++];let u=n[s++];this.outputCount=n[s++],this.customDataOffset=n[s++],this.customDataSize=n[s++];let l=[];for(let a=0;atypeof l=="number"?this.inputs[l]:l)??this.inputs,n=r?.outputs??[],s=(l,a,p)=>new wr(this.module,a,this.output(l,p),p),u=(l,a)=>{let p=fr(l);if(!p)throw new Error(`Unsupported data type: ${l}`);let m=p*z.size(a),f=m>0?this.backend.gpuDataManager.create(m).id:0;return new wr(this.module,l,f,a)};return this.backend.run(t,o,n,s,u)}output(t,r){let o=this.module.stackSave();try{let n=this.module.stackAlloc((1+r.length)*4),s=n>>2;this.module.HEAPU32[s++]=r.length;for(let u=0;u{let n=t.jsepInit;if(!n)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let s=new pn;await s.initialize(r,o),n("webgpu",[s,u=>s.alloc(u),u=>s.free(u),(u,l,a,p=!1)=>{if(p)De("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${u}, dst=${l}, size=${a}`),s.memcpy(u,l);else{De("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${u}, gpuDataId=${l}, size=${a}`);let m=t.HEAPU8.subarray(u>>>0,(u>>>0)+a);s.upload(l,m)}},async(u,l,a)=>{De("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${u}, dataOffset=${l}, size=${a}`),await s.download(u,()=>t.HEAPU8.subarray(l>>>0,(l>>>0)+a))},(u,l,a)=>s.createKernel(u,l,a,t.UTF8ToString(t._JsepGetNodeName(l))),u=>s.releaseKernel(u),(u,l,a,p)=>{De("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${a}, kernel=${u}, contextDataOffset=${l}`);let m=new io(t,s,l);return s.computeKernel(u,m,p)},()=>s.captureBegin(),()=>s.captureEnd(),()=>s.replay()])}else n("webnn")}});var Ho;Ho=Ro();var bd=No(),An,Tn=!1,zr=!1,Go=!1,wd=e=>{if(e===1)return!1;if(typeof SharedArrayBuffer>"u")return typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+e+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),!1;typeof process<"u"&&process.versions&&process.versions.node&&console.warn("env.wasm.numThreads is set to "+e+", however, currently onnxruntime-web does not support multi-threads in Node.js. Please consider using onnxruntime-node for performance critical scenarios.");try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},vd=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},$d=(e,t)=>e?t?"ort-wasm-simd-threaded.wasm":"ort-wasm-simd.wasm":t?"ort-wasm-threaded.wasm":"ort-wasm.wasm",Lo=async e=>{if(Tn)return Promise.resolve();if(zr)throw new Error("multiple calls to \'initializeWebAssembly()\' detected.");if(Go)throw new Error("previous call to \'initializeWebAssembly()\' failed.");zr=!0;let t=e.initTimeout,r=e.numThreads,o=e.simd,n=wd(r),s=o&&vd(),u=e.wasmPaths,l=typeof u=="string"?u:void 0,a=$d(s,n),p=typeof u=="object"?u[a]:void 0,m=!1,f=[];if(t>0&&f.push(new Promise(b=>{setTimeout(()=>{m=!0,b()},t)})),f.push(new Promise((b,_)=>{let y=n?bd:Ho,$={locateFile:(I,C)=>{if(n&&I.endsWith(".worker.js")&&typeof Blob<"u")return URL.createObjectURL(new Blob([Wo()],{type:"text/javascript"}));if(I.endsWith(".wasm")){if(p)return p;let v=l??C;return a==="ort-wasm-simd.wasm"?v+"ort-wasm-simd.jsep.wasm":a==="ort-wasm-simd-threaded.wasm"?v+"ort-wasm-simd-threaded.jsep.wasm":v+a}return C+I}};if(n)if($.numThreads=r,typeof Blob>"u")$.mainScriptUrlOrBlob=(void 0)(__dirname,"ort-wasm-threaded.js");else{let I=`var ortWasmThreaded=${y.toString()};`;$.mainScriptUrlOrBlob=new Blob([I],{type:"text/javascript"})}y($).then(I=>{zr=!1,Tn=!0,An=I,b()},I=>{zr=!1,Go=!0,_(I)})})),await Promise.race(f),m)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},We=()=>{if(Tn&&An)return An;throw new Error("WebAssembly is not initialized yet.")};var Ge=(e,t)=>{let r=We(),o=r.lengthBytesUTF8(e)+1,n=r._malloc(o);return r.stringToUTF8(e,n,o),t.push(n),n},mr=(e,t,r,o)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([n,s])=>{let u=t?t+n:n;if(typeof s=="object")mr(s,u+".",r,o);else if(typeof s=="string"||typeof s=="number")o(u,s.toString());else if(typeof s=="boolean")o(u,s?"1":"0");else throw new Error(`Can\'t handle extra config type: ${typeof s}`)})},Be=e=>{let t=We(),r=t.stackSave();try{let o=t.stackAlloc(8);t._OrtGetLastError(o,o+4);let n=t.HEAP32[o/4],s=t.HEAPU32[o/4+1],u=s?t.UTF8ToString(s):"";throw new Error(`${e} ERROR_CODE: ${n}, ERROR_MESSAGE: ${u}`)}finally{t.stackRestore(r)}};var Fo=e=>{let t=We(),r=0,o=[],n=e||{};try{if(e?.logSeverityLevel===void 0)n.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log serverity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)n.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(n.terminate=!1);let s=0;return e?.tag!==void 0&&(s=Ge(e.tag,o)),r=t._OrtCreateRunOptions(n.logSeverityLevel,n.logVerbosityLevel,!!n.terminate,s),r===0&&Be("Can\'t create run options."),e?.extra!==void 0&&mr(e.extra,"",new WeakSet,(u,l)=>{let a=Ge(u,o),p=Ge(l,o);t._OrtAddRunConfigEntry(r,a,p)!==0&&Be(`Can\'t set a run config entry: ${u} - ${l}.`)}),[r,o]}catch(s){throw r!==0&&t._OrtReleaseRunOptions(r),o.forEach(u=>t._free(u)),s}};var _d=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},xd=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},Sd=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(r=>(typeof r=="string"?r:r.name)==="webgpu")&&(e.enableMemPattern=!1)},Cd=(e,t,r)=>{for(let o of t){let n=typeof o=="string"?o:o.name;switch(n){case"webnn":if(n="WEBNN",typeof o!="string"){let u=o;if(u?.deviceType){let l=Ge("deviceType",r),a=Ge(u.deviceType,r);We()._OrtAddSessionConfigEntry(e,l,a)!==0&&Be(`Can\'t set a session config entry: \'deviceType\' - ${u.deviceType}.`)}if(u?.numThreads){let l=u.numThreads;(typeof l!="number"||!Number.isInteger(l)||l<0)&&(l=0);let a=Ge("numThreads",r),p=Ge(l.toString(),r);We()._OrtAddSessionConfigEntry(e,a,p)!==0&&Be(`Can\'t set a session config entry: \'numThreads\' - ${u.numThreads}.`)}if(u?.powerPreference){let l=Ge("powerPreference",r),a=Ge(u.powerPreference,r);We()._OrtAddSessionConfigEntry(e,l,a)!==0&&Be(`Can\'t set a session config entry: \'powerPreference\' - ${u.powerPreference}.`)}}break;case"webgpu":if(n="JS",typeof o!="string"){let u=o;if(u?.preferredLayout){if(u.preferredLayout!=="NCHW"&&u.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either \'NCHW\' or \'NHWC\': ${u.preferredLayout}`);let l=Ge("preferredLayout",r),a=Ge(u.preferredLayout,r);We()._OrtAddSessionConfigEntry(e,l,a)!==0&&Be(`Can\'t set a session config entry: \'preferredLayout\' - ${u.preferredLayout}.`)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${n}`)}let s=Ge(n,r);We()._OrtAppendExecutionProvider(e,s)!==0&&Be(`Can\'t append execution provider: ${n}.`)}},qo=e=>{let t=We(),r=0,o=[],n=e||{};Sd(n);try{let s=_d(n.graphOptimizationLevel??"all"),u=xd(n.executionMode??"sequential"),l=typeof n.logId=="string"?Ge(n.logId,o):0,a=n.logSeverityLevel??2;if(!Number.isInteger(a)||a<0||a>4)throw new Error(`log serverity level is not valid: ${a}`);let p=n.logVerbosityLevel??0;if(!Number.isInteger(p)||p<0||p>4)throw new Error(`log verbosity level is not valid: ${p}`);let m=typeof n.optimizedModelFilePath=="string"?Ge(n.optimizedModelFilePath,o):0;if(r=t._OrtCreateSessionOptions(s,!!n.enableCpuMemArena,!!n.enableMemPattern,u,!!n.enableProfiling,0,l,a,p,m),r===0&&Be("Can\'t create session options."),n.executionProviders&&Cd(r,n.executionProviders,o),n.enableGraphCapture!==void 0){if(typeof n.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${n.enableGraphCapture}`);let f=Ge("enableGraphCapture",o),b=Ge(n.enableGraphCapture.toString(),o);t._OrtAddSessionConfigEntry(r,f,b)!==0&&Be(`Can\'t set a session config entry: \'enableGraphCapture\' - ${n.enableGraphCapture}.`)}if(n.freeDimensionOverrides)for(let[f,b]of Object.entries(n.freeDimensionOverrides)){if(typeof f!="string")throw new Error(`free dimension override name must be a string: ${f}`);if(typeof b!="number"||!Number.isInteger(b)||b<0)throw new Error(`free dimension override value must be a non-negative integer: ${b}`);let _=Ge(f,o);t._OrtAddFreeDimensionOverride(r,_,b)!==0&&Be(`Can\'t set a free dimension override: ${f} - ${b}.`)}return n.extra!==void 0&&mr(n.extra,"",new WeakSet,(f,b)=>{let _=Ge(f,o),y=Ge(b,o);t._OrtAddSessionConfigEntry(r,_,y)!==0&&Be(`Can\'t set a session config entry: ${f} - ${b}.`)}),[r,o]}catch(s){throw r!==0&&t._OrtReleaseSessionOptions(r),o.forEach(u=>t._free(u)),s}};ie();var jo=async e=>{if(typeof e=="string")if(typeof process<"u"&&process.versions&&process.versions.node)try{return new Uint8Array(await(void 0)(e))}catch(t){if(t.code==="ERR_FS_FILE_TOO_LARGE"){let r=(void 0)(e),o=[];for await(let n of r)o.push(n);return new Uint8Array(Buffer.concat(o))}throw t}else{let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),o=r?parseInt(r,10):0;if(o<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let n=t.body.getReader(),s;try{s=new ArrayBuffer(o)}catch(l){if(l instanceof RangeError){let a=Math.ceil(o/65536);s=new WebAssembly.Memory({initial:a,maximum:a}).buffer}else throw l}let u=0;for(;;){let{done:l,value:a}=await n.read();if(l)break;let p=a.byteLength;new Uint8Array(s,u,p).set(a),u+=p}return new Uint8Array(s,0,o)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)};var ip=(e,t)=>{We()._OrtInit(e,t)!==0&&Be("Can\'t initialize onnxruntime.")},Hu=async e=>{ip(e.wasm.numThreads,hr(e.logLevel))},Lu=async(e,t)=>{{let r=(Wu(),Ft(Nu)).init;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");let o=e.webgpu.adapter;if(o){if(typeof o.limits!="object"||typeof o.features!="object"||typeof o.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let n=e.webgpu.powerPreference;if(n!==void 0&&n!=="low-power"&&n!=="high-performance")throw new Error(`Invalid powerPreference setting: "${n}"`);let s=e.webgpu.forceFallbackAdapter;if(s!==void 0&&typeof s!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${s}"`);if(o=await navigator.gpu.requestAdapter({powerPreference:n,forceFallbackAdapter:s}),!o)throw new Error(\'Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.\')}if(!e.wasm.simd)throw new Error("Not supported for WebGPU=ON and SIMD=OFF. Please set `env.wasm.simd` to true when using `webgpu` EP");await r("webgpu",We(),e,o)}if(t==="webnn"){if(typeof navigator>"u"||!navigator.ml)throw new Error("WebNN is not supported in current environment");await r("webnn",We(),e)}}},Ut=new Map,sp=e=>{let t=We(),r=t.stackSave();try{let o=t.stackAlloc(8);return t._OrtGetInputOutputCount(e,o,o+4)!==0&&Be("Can\'t get session input/output count."),[t.HEAP32[o/4],t.HEAP32[o/4+1]]}finally{t.stackRestore(r)}},so=e=>{let t=We(),r=t._malloc(e.byteLength);if(r===0)throw new Error(`Can\'t create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,r),[r,e.byteLength]},Fu=async(e,t)=>{let r,o,n=We();Array.isArray(e)?[r,o]=e:e.buffer===n.HEAPU8.buffer?[r,o]=[e.byteOffset,e.byteLength]:[r,o]=so(e);let s=0,u=0,l=0,a=[],p=[],m=[];try{if([u,a]=qo(t),t?.externalData&&n.mountExternalData){let v=[];for(let A of t.externalData){let T=typeof A=="string"?A:A.path;v.push(jo(typeof A=="string"?A:A.data).then(D=>{n.mountExternalData(T,D)}))}await Promise.all(v)}s=await n._OrtCreateSession(r,o,u),s===0&&Be("Can\'t create a session.");let[f,b]=sp(s),_=!!t?.enableGraphCapture,y=[],$=[],I=[];for(let v=0;vv==="gpu-buffer")&&(l=n._OrtCreateBinding(s),l===0&&Be("Can\'t create IO binding."),C={handle:l,outputPreferredLocations:I,outputPreferredLocationsEncoded:I.map(v=>Pn(v))}),Ut.set(s,[s,p,m,C,_,!1]),[s,y,$]}catch(f){throw p.forEach(b=>n._OrtFree(b)),m.forEach(b=>n._OrtFree(b)),l!==0&&n._OrtReleaseBinding(l),s!==0&&n._OrtReleaseSession(s),f}finally{n._free(r),u!==0&&n._OrtReleaseSessionOptions(u),a.forEach(f=>n._free(f)),n.unmountExternalData?.()}},qu=e=>{let t=We(),r=Ut.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[o,n,s,u,l]=r;u&&(l&&t._OrtClearBoundOutputs(u.handle),t._OrtReleaseBinding(u.handle)),t.jsepOnReleaseSession?.(e),n.forEach(a=>t._OrtFree(a)),s.forEach(a=>t._OrtFree(a)),t._OrtReleaseSession(o),Ut.delete(e)},Gu=(e,t,r,o,n,s=!1)=>{if(!e){t.push(0);return}let u=We(),l=e[0],a=e[1],p=e[3],m,f;if(l==="string"&&p==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");if(s&&p!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${n} when enableGraphCapture is true.`);if(p==="gpu-buffer"){let y=e[2].gpuBuffer,$=fr(En(l));f=a.reduce((C,v)=>C*v,1)*$;let I=u.jsepRegisterBuffer;if(!I)throw new Error(\'Tensor location "gpu-buffer" is not supported without using WebGPU.\');m=I(o,n,y,f)}else{let y=e[2];if(Array.isArray(y)){f=4*y.length,m=u._malloc(f),r.push(m);let $=m/4;for(let I=0;Iu.HEAP32[y++]=I);let $=u._OrtCreateTensor(En(l),m,f,_,a.length,Pn(p));$===0&&Be(`Can\'t create tensor for input/output. session=${o}, index=${n}.`),t.push($)}finally{u.stackRestore(b)}},Ku=async(e,t,r,o,n,s)=>{let u=We(),l=Ut.get(e);if(!l)throw new Error(`cannot run inference. invalid session id: ${e}`);let a=l[0],p=l[1],m=l[2],f=l[3],b=l[4],_=l[5],y=t.length,$=o.length,I=0,C=[],v=[],A=[],T=[],D=u.stackSave(),U=u.stackAlloc(y*4),V=u.stackAlloc(y*4),H=u.stackAlloc($*4),R=u.stackAlloc($*4);try{[I,C]=Fo(s);for(let Q=0;QNe*Fe,1);he=Tt(J);let wt=f?.outputPreferredLocations[o[Q]];if(he==="string"){if(wt==="gpu-buffer")throw new Error("String tensor is not supported on GPU.");let Ne=[],Fe=Ae/4;for(let Je=0;Je0){let Ne=u.jsepGetBuffer;if(!Ne)throw new Error(\'preferredLocation "gpu-buffer" is not supported without using WebGPU.\');let Fe=Ne(Ae),Je=fr(J);if(Je===void 0||!Ko(he))throw new Error(`Unsupported data type: ${he}`);se=!0,ze.push([he,Xe,{gpuBuffer:Fe,download:u.jsepCreateDownloader(Fe,Le*Je,he),dispose:()=>{u._OrtReleaseTensor(xe)}},"gpu-buffer"])}else{let Ne=Ur(he),Fe=new Ne(Le);new Uint8Array(Fe.buffer,Fe.byteOffset,Fe.byteLength).set(u.HEAPU8.subarray(Ae,Ae+Fe.byteLength)),ze.push([he,Xe,Fe,"cpu"])}}finally{u.stackRestore(me),he==="string"&&Ae&&u._free(Ae),se||u._OrtReleaseTensor(xe)}}return f&&!b&&(u._OrtClearBoundOutputs(f.handle),Ut.set(e,[a,p,m,f,b,!1])),ze}finally{u.stackRestore(D),v.forEach(L=>u._OrtReleaseTensor(L)),A.forEach(L=>u._OrtReleaseTensor(L)),T.forEach(L=>u._free(L)),I!==0&&u._OrtReleaseRunOptions(I),C.forEach(L=>u._free(L))}},ju=e=>{let t=We(),r=Ut.get(e);if(!r)throw new Error("invalid session id");let o=r[0],n=t._OrtEndProfiling(o);n===0&&Be("Can\'t get an profile file name."),t._OrtFree(n)},Yu=e=>{let t=[];for(let r of e){let o=r[2];!Array.isArray(o)&&"buffer"in o&&t.push(o.buffer)}return t};self.onmessage=e=>{let{type:t,in:r}=e.data;try{switch(t){case"init-wasm":Lo(r.wasm).then(()=>{Hu(r).then(()=>{postMessage({type:t})},o=>{postMessage({type:t,err:o})})},o=>{postMessage({type:t,err:o})});break;case"init-ep":{let{epName:o,env:n}=r;Lu(n,o).then(()=>{postMessage({type:t})},s=>{postMessage({type:t,err:s})});break}case"copy-from":{let{buffer:o}=r,n=so(o);postMessage({type:t,out:n});break}case"create":{let{model:o,options:n}=r;Fu(o,n).then(s=>{postMessage({type:t,out:s})},s=>{postMessage({type:t,err:s})});break}case"release":qu(r),postMessage({type:t});break;case"run":{let{sessionId:o,inputIndices:n,inputs:s,outputIndices:u,options:l}=r;Ku(o,n,s,u,new Array(u.length).fill(null),l).then(a=>{a.some(p=>p[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:a},Yu([...s,...a]))},a=>{postMessage({type:t,err:a})});break}case"end-profiling":ju(r),postMessage({type:t});break;default:}}catch(o){postMessage({type:t,err:o})}};})();\n/**\n * @license\n * Copyright 2021 Google LLC. All Rights Reserved.\n * Licensed under the Apache License, Version 2.0 (the "License");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an "AS IS" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n * =============================================================================\n */\n/**\n * @license\n * Copyright 2020 Google LLC. All Rights Reserved.\n * Licensed under the Apache License, Version 2.0 (the "License");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an "AS IS" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n * =============================================================================\n */\n/**\n * @license\n * Copyright 2019 Google LLC. All Rights Reserved.\n * Licensed under the Apache License, Version 2.0 (the "License");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an "AS IS" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n * =============================================================================\n */\n'});var Kt,ct,Rr,zn,Bn,Ba,Da,or,sr,ff,Rn,Ul,Nl,Wl,Vl,Gl,Hl,Ll,Ma=F(()=>{"use strict";lt();Dl();nr();Kt=()=>!!Ae.wasm.proxy&&typeof document<"u",Rr=!1,zn=!1,Bn=!1,Da=new Map,or=(e,t)=>{let r=Da.get(e);r?r.push(t):Da.set(e,[t])},sr=()=>{if(Rr||!zn||Bn||!ct)throw new Error("worker not ready")},ff=e=>{switch(e.data.type){case"init-wasm":Rr=!1,e.data.err?(Bn=!0,Ba[1](e.data.err)):(zn=!0,Ba[0]());break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=Da.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},Rn=typeof document<"u"?document?.currentScript?.src:void 0,Ul=async()=>{if(!zn){if(Rr)throw new Error("multiple calls to 'initWasm()' detected.");if(Bn)throw new Error("previous call to 'initWasm()' failed.");if(Rr=!0,Kt())return Ae.wasm.wasmPaths===void 0&&Rn&&Rn.indexOf("blob:")!==0&&(Ae.wasm.wasmPaths=Rn.substr(0,+Rn.lastIndexOf("/")+1)),new Promise((e,t)=>{ct?.terminate();let r=URL.createObjectURL(new Blob([Ml()],{type:"text/javascript"}));ct=new Worker(r,{name:"ort-wasm-proxy-worker"}),ct.onerror=n=>t(n),ct.onmessage=ff,URL.revokeObjectURL(r),Ba=[e,t];let a={type:"init-wasm",in:Ae};ct.postMessage(a)});try{await Ki(Ae.wasm),await Tl(Ae),zn=!0}catch(e){throw Bn=!0,e}finally{Rr=!1}}},Nl=async e=>{if(Kt())return sr(),new Promise((t,r)=>{or("init-ep",[t,r]);let a={type:"init-ep",in:{epName:e,env:Ae}};ct.postMessage(a)});await Ol(Ae,e)},Wl=async e=>Kt()?(sr(),new Promise((t,r)=>{or("copy-from",[t,r]);let a={type:"copy-from",in:{buffer:e}};ct.postMessage(a,[e.buffer])})):za(e),Vl=async(e,t)=>{if(Kt()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return sr(),new Promise((r,a)=>{or("create",[r,a]);let n={type:"create",in:{model:e,options:{...t}}},o=[];e instanceof Uint8Array&&o.push(e.buffer),ct.postMessage(n,o)})}else return kl(e,t)},Gl=async e=>{if(Kt())return sr(),new Promise((t,r)=>{or("release",[t,r]);let a={type:"release",in:e};ct.postMessage(a)});Pl(e)},Hl=async(e,t,r,a,n,o)=>{if(Kt()){if(r.some(s=>s[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(n.some(s=>s))throw new Error("pre-allocated output tensor is not supported for proxy.");return sr(),new Promise((s,l)=>{or("run",[s,l]);let i=r,c={type:"run",in:{sessionId:e,inputIndices:t,inputs:i,outputIndices:a,options:o}};ct.postMessage(c,Bl(i))})}else return Rl(e,t,r,a,n,o)},Ll=async e=>{if(Kt())return sr(),new Promise((t,r)=>{or("end-profiling",[t,r]);let a={type:"end-profiling",in:e};ct.postMessage(a)});zl(e)}});var Fl,mf,Dn,ql=F(()=>{"use strict";lt();Ma();ie();oa();Fl=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},mf=e=>{switch(e[3]){case"cpu":return new Ze(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!un(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:r,download:a,dispose:n}=e[2];return Ze.fromGpuBuffer(r,{dataType:t,dims:e[1],download:a,dispose:n})}default:throw new Error(`invalid data location: ${e[3]}`)}},Dn=class{async fetchModelAndCopyToWasmMemory(t){return Wl(await Ar(t))}async loadModel(t,r){st();let a;typeof t=="string"?typeof process<"u"&&process.versions&&process.versions.node?a=await Ar(t):a=await this.fetchModelAndCopyToWasmMemory(t):a=t,[this.sessionId,this.inputNames,this.outputNames]=await Vl(a,r),nt()}async dispose(){return Gl(this.sessionId)}async run(t,r,a){st();let n=[],o=[];Object.entries(t).forEach(h=>{let w=h[0],b=h[1],$=this.inputNames.indexOf(w);if($===-1)throw new Error(`invalid input '${w}'`);n.push(b),o.push($)});let s=[],l=[];Object.entries(r).forEach(h=>{let w=h[0],b=h[1],$=this.outputNames.indexOf(w);if($===-1)throw new Error(`invalid output '${w}'`);s.push(b),l.push($)});let i=n.map((h,w)=>Fl(h,()=>`input "${this.inputNames[o[w]]}"`)),c=s.map((h,w)=>h?Fl(h,()=>`output "${this.outputNames[l[w]]}"`):null),f=await Hl(this.sessionId,o,i,l,c,a),m={};for(let h=0;h{"use strict";lt();Ma();ql();hf=()=>{if((typeof Ae.wasm.initTimeout!="number"||Ae.wasm.initTimeout<0)&&(Ae.wasm.initTimeout=0),typeof Ae.wasm.simd!="boolean"&&(Ae.wasm.simd=!0),typeof Ae.wasm.proxy!="boolean"&&(Ae.wasm.proxy=!1),typeof Ae.wasm.trace!="boolean"&&(Ae.wasm.trace=!1),typeof Ae.wasm.numThreads!="number"||!Number.isInteger(Ae.wasm.numThreads)||Ae.wasm.numThreads<=0){(typeof self<"u"&&!self.crossOriginIsolated||typeof process<"u"&&process.versions&&process.versions.node)&&(Ae.wasm.numThreads=1);let e=typeof navigator>"u"?(void 0)().length:navigator.hardwareConcurrency;Ae.wasm.numThreads=Math.min(4,Math.ceil((e||1)/2))}},Mn=class{async init(t){hf(),await Ul(),await Nl(t)}async createInferenceSessionHandler(t,r){let a=new Dn;return await a.loadModel(t,r),Promise.resolve(a)}}});var Kl={};rr(Kl,{wasmBackend:()=>gf});var gf,Yl=F(()=>{"use strict";jl();gf=new Mn});lt();lt();lt();var Ri="1.17.3";var Kv=Yn;{let e=(Yl(),Gt(Kl)).wasmBackend;Lt("webgpu",e,5),Lt("webnn",e,5),Lt("cpu",e,10),Lt("wasm",e,10)}Object.defineProperty(Ae.versions,"web",{value:Ri,enumerable:!0}); /** * @license * Copyright 2021 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ /** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ //# sourceMappingURL=ort.webgpu.min.js.map /***/ }), /***/ "./src/backends/onnx.js": /*!******************************!*\ !*** ./src/backends/onnx.js ***! \******************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache; __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "Tensor": () => (/* reexport safe */ onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__.Tensor), /* harmony export */ "createInferenceSession": () => (/* binding */ createInferenceSession), /* harmony export */ "deviceToExecutionProviders": () => (/* binding */ deviceToExecutionProviders), /* harmony export */ "isONNXProxy": () => (/* binding */ isONNXProxy), /* harmony export */ "isONNXTensor": () => (/* binding */ isONNXTensor) /* harmony export */ }); /* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); /* harmony import */ var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! onnxruntime-node */ "?2ce3"); /* harmony import */ var onnxruntime_web_webgpu__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! onnxruntime-web/webgpu */ "./node_modules/onnxruntime-web/dist/esm/ort.webgpu.min.js"); /* harmony import */ var onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! onnxruntime-common */ "./node_modules/onnxruntime-common/dist/esm/index.js"); /** * @file Handler file for choosing the correct version of ONNX Runtime, based on the environment. * Ideally, we could import the `onnxruntime-web` and `onnxruntime-node` packages only when needed, * but dynamic imports don't seem to work with the current webpack version and/or configuration. * This is possibly due to the experimental nature of top-level await statements. * So, we just import both packages, and use the appropriate one based on the environment: * - When running in node, we use `onnxruntime-node`. * - When running in the browser, we use `onnxruntime-web` (`onnxruntime-node` is not bundled). * * This module is not directly exported, but can be accessed through the environment variables: * ```javascript * import { env } from '@xenova/transformers'; * console.log(env.backends.onnx); * ``` * * @module backends/onnx */ // NOTE: Import order matters here. We need to import `onnxruntime-node` before `onnxruntime-web`. // In either case, we select the default export if it exists, otherwise we use the named export. /** @type {import('../utils/devices.js').DeviceType[]} */ const supportedExecutionProviders = []; /** @type {import('../utils/devices.js').DeviceType[]} */ let defaultExecutionProviders; let ONNX; if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_NODE_ENV) { ONNX = onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ ?? /*#__PURE__*/ (onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache || (onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache = __webpack_require__.t(onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__, 2))); supportedExecutionProviders.push('cpu'); defaultExecutionProviders = ['cpu']; } else { ONNX = onnxruntime_web_webgpu__WEBPACK_IMPORTED_MODULE_2__; if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBGPU_AVAILABLE) { supportedExecutionProviders.push('webgpu'); } supportedExecutionProviders.push('wasm'); defaultExecutionProviders = ['wasm']; } // @ts-ignore const InferenceSession = ONNX.InferenceSession; /** * Map a device to the execution providers to use for the given device. * @param {import("../utils/devices.js").DeviceType} [device=null] (Optional) The device to run the inference on. * @returns {import("../utils/devices.js").DeviceType[]} The execution providers to use for the given device. */ function deviceToExecutionProviders(device) { // TODO: Use mapping from device to execution providers for overloaded devices (e.g., 'gpu' or 'cpu'). let executionProviders = defaultExecutionProviders; if (device) { // User has specified a device if (!supportedExecutionProviders.includes(device)) { throw new Error(`Unsupported device: "${device}". Should be one of: ${supportedExecutionProviders.join(', ')}.`) } executionProviders = [device]; } return executionProviders; } /** * Create an ONNX inference session. * @param {Uint8Array} buffer The ONNX model buffer. * @param {Object} session_options ONNX inference session options. * @returns {Promise} The ONNX inference session. */ async function createInferenceSession(buffer, session_options) { return await InferenceSession.create(buffer, session_options); } /** * Check if an object is an ONNX tensor. * @param {any} x The object to check * @returns {boolean} Whether the object is an ONNX tensor. */ function isONNXTensor(x) { return x instanceof ONNX.Tensor; } // @ts-ignore const ONNX_ENV = ONNX?.env; if (ONNX_ENV?.wasm) { // Initialize wasm backend with suitable default settings. // Set path to wasm files. This is needed when running in a web worker. // https://onnxruntime.ai/docs/api/js/interfaces/Env.WebAssemblyFlags.html#wasmPaths // We use remote wasm files by default to make it easier for newer users. // In practice, users should probably self-host the necessary .wasm files. // TODO: update this before release ONNX_ENV.wasm.wasmPaths = 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.17.3/dist/'; // Proxy the WASM backend to prevent the UI from freezing // NOTE: This is only needed when running in a non-worker browser environment. ONNX_ENV.wasm.proxy = !_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBWORKER_ENV; // https://developer.mozilla.org/en-US/docs/Web/API/crossOriginIsolated if (typeof crossOriginIsolated === 'undefined' || !crossOriginIsolated) { ONNX_ENV.wasm.numThreads = 1; } // Running in a browser-environment // TODO: Check if 1.17.1 fixes this issue. // SIMD for WebAssembly does not operate correctly in some recent versions of iOS (16.4.x). // As a temporary fix, we disable it for now. // For more information, see: https://github.com/microsoft/onnxruntime/issues/15644 const isIOS = typeof navigator !== 'undefined' && /iP(hone|od|ad).+16_4.+AppleWebKit/.test(navigator.userAgent); if (isIOS) { ONNX_ENV.wasm.simd = false; } } /** * Check if ONNX's WASM backend is being proxied. * @returns {boolean} Whether ONNX's WASM backend is being proxied. */ function isONNXProxy() { // TODO: Update this when allowing non-WASM backends. return ONNX_ENV?.wasm?.proxy; } // Expose ONNX environment variables to `env.backends.onnx` _env_js__WEBPACK_IMPORTED_MODULE_0__.env.backends.onnx = ONNX_ENV; /***/ }), /***/ "./src/configs.js": /*!************************!*\ !*** ./src/configs.js ***! \************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "AutoConfig": () => (/* binding */ AutoConfig), /* harmony export */ "PretrainedConfig": () => (/* binding */ PretrainedConfig) /* harmony export */ }); /* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); /** * @file Helper module for using model configs. For more information, see the corresponding * [Python documentation](https://huggingface.co/docs/transformers/main/en/model_doc/auto#transformers.AutoConfig). * * **Example:** Load an `AutoConfig`. * * ```javascript * import { AutoConfig } from '@xenova/transformers'; * let config = await AutoConfig.from_pretrained('bert-base-uncased'); * console.log(config); * // PretrainedConfig { * // "model_type": "bert", * // "is_encoder_decoder": false, * // "architectures": [ * // "BertForMaskedLM" * // ], * // "vocab_size": 30522 * // "num_attention_heads": 12, * // "num_hidden_layers": 12, * // "hidden_size": 768, * // "max_position_embeddings": 512, * // ... * // } * ``` * * @module configs */ /** * @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions */ /** * Loads a config from the specified path. * @param {string} pretrained_model_name_or_path The path to the config directory. * @param {PretrainedOptions} options Additional options for loading the config. * @returns {Promise} A promise that resolves with information about the loaded config. */ async function loadConfig(pretrained_model_name_or_path, options) { let info = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_0__.getModelJSON)(pretrained_model_name_or_path, 'config.json', true, options); return info; } /** * Base class for all configuration classes. For more information, see the corresponding * [Python documentation](https://huggingface.co/docs/transformers/main/en/main_classes/configuration#transformers.PretrainedConfig). */ class PretrainedConfig { // NOTE: Typo in original /** * Create a new PreTrainedTokenizer instance. * @param {Object} configJSON The JSON of the config. */ constructor(configJSON) { this.model_type = null; this.is_encoder_decoder = false; Object.assign(this, configJSON); } /** * Loads a pre-trained config from the given `pretrained_model_name_or_path`. * * @param {string} pretrained_model_name_or_path The path to the pre-trained config. * @param {PretrainedOptions} options Additional options for loading the config. * @throws {Error} Throws an error if the config.json is not found in the `pretrained_model_name_or_path`. * * @returns {Promise} A new instance of the `PretrainedConfig` class. */ static async from_pretrained(pretrained_model_name_or_path, { progress_callback = null, config = null, cache_dir = null, local_files_only = false, revision = 'main', } = {}) { let data = config ?? await loadConfig(pretrained_model_name_or_path, { progress_callback, config, cache_dir, local_files_only, revision, }) return new this(data); } } /** * Helper class which is used to instantiate pretrained configs with the `from_pretrained` function. * * @example * const config = await AutoConfig.from_pretrained('Xenova/bert-base-uncased'); */ class AutoConfig { /** @type {typeof PretrainedConfig.from_pretrained} */ static async from_pretrained(...args) { return PretrainedConfig.from_pretrained(...args); } } /***/ }), /***/ "./src/env.js": /*!********************!*\ !*** ./src/env.js ***! \********************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "apis": () => (/* binding */ apis), /* harmony export */ "env": () => (/* binding */ env) /* harmony export */ }); /* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! fs */ "?569f"); /* harmony import */ var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! path */ "?3f59"); /* harmony import */ var url__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! url */ "?154a"); /** * @file Module used to configure Transformers.js. * * **Example:** Disable remote models. * ```javascript * import { env } from '@xenova/transformers'; * env.allowRemoteModels = false; * ``` * * **Example:** Set local model path. * ```javascript * import { env } from '@xenova/transformers'; * env.localModelPath = '/path/to/local/models/'; * ``` * * **Example:** Set cache directory. * ```javascript * import { env } from '@xenova/transformers'; * env.cacheDir = '/path/to/cache/directory/'; * ``` * * @module env */ const VERSION = '3.0.0-alpha.0'; // Check if various APIs are available (depends on environment) const IS_BROWSER_ENV = typeof self !== 'undefined'; const IS_WEBWORKER_ENV = IS_BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope'; const IS_WEB_CACHE_AVAILABLE = IS_BROWSER_ENV && 'caches' in self; const IS_WEBGPU_AVAILABLE = typeof navigator !== 'undefined' && 'gpu' in navigator; const IS_NODE_ENV = typeof process !== 'undefined' && process?.release?.name === 'node'; const IS_FS_AVAILABLE = !isEmpty(fs__WEBPACK_IMPORTED_MODULE_0__); const IS_PATH_AVAILABLE = !isEmpty(path__WEBPACK_IMPORTED_MODULE_1__); /** * A read-only object containing information about the APIs available in the current environment. */ const apis = Object.freeze({ /** Whether we are running in a browser environment */ IS_BROWSER_ENV, /** Whether we are running in a web worker environment */ IS_WEBWORKER_ENV, /** Whether the Cache API is available */ IS_WEB_CACHE_AVAILABLE, /** Whether the WebGPU API is available */ IS_WEBGPU_AVAILABLE, /** Whether we are running in a Node.js environment */ IS_NODE_ENV, /** Whether the filesystem API is available */ IS_FS_AVAILABLE, /** Whether the path API is available */ IS_PATH_AVAILABLE, }); const RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE; const __dirname = RUNNING_LOCALLY ? path__WEBPACK_IMPORTED_MODULE_1__.dirname(path__WEBPACK_IMPORTED_MODULE_1__.dirname(url__WEBPACK_IMPORTED_MODULE_2__.fileURLToPath("file:///Users/ajr/Documents/GitHub/transformers.js/src/env.js"))) : './'; // Only used for environments with access to file system const DEFAULT_CACHE_DIR = RUNNING_LOCALLY ? path__WEBPACK_IMPORTED_MODULE_1__.join(__dirname, '/.cache/') : null; // Set local model path, based on available APIs const DEFAULT_LOCAL_MODEL_PATH = '/models/'; const localModelPath = RUNNING_LOCALLY ? path__WEBPACK_IMPORTED_MODULE_1__.join(__dirname, DEFAULT_LOCAL_MODEL_PATH) : DEFAULT_LOCAL_MODEL_PATH; /** * Global variable given visible to users to control execution. This provides users a simple way to configure Transformers.js. * @typedef {Object} TransformersEnvironment * @property {string} version This version of Transformers.js. * @property {Object} backends Expose environment variables of different backends, * allowing users to set these variables if they want to. * @property {boolean} allowRemoteModels Whether to allow loading of remote files, defaults to `true`. * If set to `false`, it will have the same effect as setting `local_files_only=true` when loading pipelines, models, tokenizers, processors, etc. * @property {string} remoteHost Host URL to load models from. Defaults to the Hugging Face Hub. * @property {string} remotePathTemplate Path template to fill in and append to `remoteHost` when loading models. * @property {boolean} allowLocalModels Whether to allow loading of local files, defaults to `false` if running in-browser, and `true` otherwise. * If set to `false`, it will skip the local file check and try to load the model from the remote host. * @property {string} localModelPath Path to load local models from. Defaults to `/models/`. * @property {boolean} useFS Whether to use the file system to load files. By default, it is `true` if available. * @property {boolean} useBrowserCache Whether to use Cache API to cache models. By default, it is `true` if available. * @property {boolean} useFSCache Whether to use the file system to cache files. By default, it is `true` if available. * @property {string} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`. * @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`. * @property {Object} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which * implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache */ /** @type {TransformersEnvironment} */ const env = { version: VERSION, /////////////////// Backends settings /////////////////// // NOTE: These will be populated later by the backends themselves. backends: { // onnxruntime-web/onnxruntime-node onnx: {}, // TensorFlow.js tfjs: {}, }, /////////////////// Model settings /////////////////// allowRemoteModels: true, remoteHost: 'https://huggingface.co/', remotePathTemplate: '{model}/resolve/{revision}/', allowLocalModels: !IS_BROWSER_ENV, localModelPath: localModelPath, useFS: IS_FS_AVAILABLE, /////////////////// Cache settings /////////////////// useBrowserCache: IS_WEB_CACHE_AVAILABLE, useFSCache: IS_FS_AVAILABLE, cacheDir: DEFAULT_CACHE_DIR, useCustomCache: false, customCache: null, ////////////////////////////////////////////////////// } /** * @param {Object} obj * @private */ function isEmpty(obj) { return Object.keys(obj).length === 0; } /***/ }), /***/ "./src/generation/configuration_utils.js": /*!***********************************************!*\ !*** ./src/generation/configuration_utils.js ***! \***********************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "GenerationConfig": () => (/* binding */ GenerationConfig) /* harmony export */ }); /* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/core.js */ "./src/utils/core.js"); /** * @module generation/configuration_utils */ /** * Class that holds a configuration for a generation task. */ class GenerationConfig { // Parameters that control the length of the output /** * The maximum length the generated tokens can have. * Corresponds to the length of the input prompt + `max_new_tokens`. * Its effect is overridden by `max_new_tokens`, if also set. * @type {number} * @default 20 */ max_length = 20; /** * The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt. * @type {number} * @default null */ max_new_tokens = null; /** * The minimum length of the sequence to be generated. * Corresponds to the length of the input prompt + `min_new_tokens`. * Its effect is overridden by `min_new_tokens`, if also set. * @type {number} * @default 0 */ min_length = 0; /** * The minimum numbers of tokens to generate, ignoring the number of tokens in the prompt. * @type {number} * @default null */ min_new_tokens = null; /** * Controls the stopping condition for beam-based methods, like beam-search. It accepts the following values: * - `true`, where the generation stops as soon as there are `num_beams` complete candidates; * - `false`, where an heuristic is applied and the generation stops when is it very unlikely to find better candidates; * - `"never"`, where the beam search procedure only stops when there cannot be better candidates (canonical beam search algorithm). * @type {boolean|"never"} * @default false */ early_stopping = false; /** * The maximum amount of time you allow the computation to run for in seconds. * Generation will still finish the current pass after allocated time has been passed. * @type {number} * @default null */ max_time = null; // Parameters that control the generation strategy used /** * Whether or not to use sampling; use greedy decoding otherwise. * @type {boolean} * @default false */ do_sample = false; /** * Number of beams for beam search. 1 means no beam search. * @type {number} * @default 1 */ num_beams = 1; /** * Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams. * See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more details. * @type {number} * @default 1 */ num_beam_groups = 1; /** * The values balance the model confidence and the degeneration penalty in contrastive search decoding. * @type {number} * @default null */ penalty_alpha = null; /** * Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding. * @type {boolean} * @default true */ use_cache = true; // Parameters for manipulation of the model output logits /** * The value used to modulate the next token probabilities. * @type {number} * @default 1.0 */ temperature = 1.0; /** * The number of highest probability vocabulary tokens to keep for top-k-filtering. * @type {number} * @default 50 */ top_k = 50; /** * If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or higher are kept for generation. * @type {number} * @default 1.0 */ top_p = 1.0; /** * Local typicality measures how similar the conditional probability of predicting a target token next is to the expected conditional probability of predicting a random token next, given the partial text already generated. * If set to float < 1, the smallest set of the most locally typical tokens with probabilities that add up to `typical_p` or higher are kept for generation. * See [this paper](https://arxiv.org/pdf/2202.00666.pdf) for more details. * @type {number} * @default 1.0 */ typical_p = 1.0; /** * If set to float strictly between 0 and 1, only tokens with a conditional probability greater than `epsilon_cutoff` will be sampled. * In the paper, suggested values range from 3e-4 to 9e-4, depending on the size of the model. * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. * @type {number} * @default 0.0 */ epsilon_cutoff = 0.0; /** * Eta sampling is a hybrid of locally typical sampling and epsilon sampling. * If set to float strictly between 0 and 1, a token is only considered if it is greater than either `eta_cutoff` or `sqrt(eta_cutoff) * exp(-entropy(softmax(next_token_logits)))`. * The latter term is intuitively the expected next token probability, scaled by `sqrt(eta_cutoff)`. In the paper, suggested values range from 3e-4 to 2e-3, depending on the size of the model. * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. * @type {number} * @default 0.0 */ eta_cutoff = 0.0; /** * This value is subtracted from a beam's score if it generates a token same as any beam from other group at a particular time. * Note that `diversity_penalty` is only effective if `group beam search` is enabled. * @type {number} * @default 0.0 */ diversity_penalty = 0.0; /** * The parameter for repetition penalty. 1.0 means no penalty. * See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details. * @type {number} * @default 1.0 */ repetition_penalty = 1.0; /** * The paramater for encoder_repetition_penalty. * An exponential penalty on sequences that are not in the original input. * 1.0 means no penalty. * @type {number} * @default 1.0 */ encoder_repetition_penalty = 1.0; /** * Exponential penalty to the length that is used with beam-based generation. * It is applied as an exponent to the sequence length, which in turn is used to divide the score of the sequence. * Since the score is the log likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while `length_penalty` < 0.0 encourages shorter sequences. * @type {number} * @default 1.0 */ length_penalty = 1.0; /** * If set to int > 0, all ngrams of that size can only occur once. * @type {number} * @default 0 */ no_repeat_ngram_size = 0; /** * List of token ids that are not allowed to be generated. * In order to get the token ids of the words that should not appear in the generated text, use * `tokenizer(bad_words, { add_prefix_space: true, add_special_tokens: false }).input_ids`. * @type {number[][]} * @default null */ bad_words_ids = null; /** * List of token ids that must be generated. * If given a `number[][]`, this is treated as a simple list of words that must be included, the opposite to `bad_words_ids`. * If given `number[][][]`, this triggers a [disjunctive constraint](https://github.com/huggingface/transformers/issues/14081), where one can allow different forms of each word. * @type {number[][]|number[][][]} * @default null */ force_words_ids = null; /** * Whether to renormalize the logits after applying all the logits processors or warpers (including the custom ones). * It's highly recommended to set this flag to `true` as the search algorithms suppose the score logits are normalized but some logit processors or warpers break the normalization. * @type {boolean} * @default false */ renormalize_logits = false; /** * Custom constraints that can be added to the generation to ensure that the output will contain the use of certain tokens as defined by `Constraint` objects, in the most sensible way possible. * @type {Object[]} * @default null */ constraints = null; /** * The id of the token to force as the first generated token after the `decoder_start_token_id`. * Useful for multilingual models like mBART where the first generated token needs to be the target language token. * @type {number} * @default null */ forced_bos_token_id = null; /** * The id of the token to force as the last generated token when `max_length` is reached. * Optionally, use a list to set multiple *end-of-sequence* tokens. * @type {number|number[]} * @default null */ forced_eos_token_id = null; /** * Whether to remove possible *nan* and *inf* outputs of the model to prevent the generation method to crash. Note that using `remove_invalid_values` can slow down generation. * @type {boolean} */ remove_invalid_values = false; /** * This Tuple adds an exponentially increasing length penalty, after a certain amount of tokens have been generated. * The tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty starts and `decay_factor` represents the factor of exponential decay. * @type {[number, number]} * @default null */ exponential_decay_length_penalty = null; /** * A list of tokens that will be suppressed at generation. * The `SuppressTokens` logit processor will set their log probs to `-inf` so that they are not sampled. * @type {number[]} * @default null */ suppress_tokens = null; /** * A list of tokens that will be suppressed at the beginning of the generation. * The `SuppressBeginTokens` logit processor will set their log probs to `-inf` so that they are not sampled. * @type {number[]} * @default null */ begin_suppress_tokens = null; /** * A list of pairs of integers which indicates a mapping from generation indices to token indices that will be forced before sampling. * For example, `[[1, 123]]` means the second generated token will always be a token of index 123. * @type {[number, number][]} * @default null */ forced_decoder_ids = null; /** * The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. * Higher guidance scale encourages the model to generate samples that are more closely linked to the input * prompt, usually at the expense of poorer quality. * @type {number} * @default null */ guidance_scale = null; // Parameters that define the output variables of `generate` /** * The number of independently computed returned sequences for each element in the batch. * @type {number} * @default 1 */ num_return_sequences = 1; /** * Whether or not to return the attentions tensors of all attention layers. * See `attentions` under returned tensors for more details. * @type {boolean} * @default false */ output_attentions = false; /** * Whether or not to return the hidden states of all layers. * See `hidden_states` under returned tensors for more details. * @type {boolean} * @default false */ output_hidden_states = false; /** * Whether or not to return the prediction scores. * See `scores` under returned tensors for more details. * @type {boolean} * @default false */ output_scores = false; /** * Whether or not to return a `ModelOutput` instead of a plain tuple. * @type {boolean} * @default false */ return_dict_in_generate = false; // Special tokens that can be used at generation time /** * The id of the *padding* token. * @type {number} * @default null */ pad_token_id = null; /** * The id of the *beginning-of-sequence* token. * @type {number} * @default null */ bos_token_id = null; /** * The id of the *end-of-sequence* token. * Optionally, use a list to set multiple *end-of-sequence* tokens. * @type {number|number[]} * @default null */ eos_token_id = null; // Generation parameters exclusive to encoder-decoder models /** * If set to int > 0, all ngrams of that size that occur in the `encoder_input_ids` cannot occur in the `decoder_input_ids`. * @type {number} * @default 0 */ encoder_no_repeat_ngram_size = 0; /** * If an encoder-decoder model starts decoding with a different token than *bos*, the id of that token. * @type {number} * @default null */ decoder_start_token_id = null; // Wild card /** * Additional generation kwargs will be forwarded to the `generate` function of the model. * Kwargs that are not present in `generate`'s signature will be used in the model forward pass. * @type {Object} * @default {} */ generation_kwargs = {}; /** * * @param {GenerationConfig} config */ constructor(config) { Object.assign(this, (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, Object.getOwnPropertyNames(this))); } } /***/ }), /***/ "./src/generation/logits_process.js": /*!******************************************!*\ !*** ./src/generation/logits_process.js ***! \******************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "ClassifierFreeGuidanceLogitsProcessor": () => (/* binding */ ClassifierFreeGuidanceLogitsProcessor), /* harmony export */ "ForcedBOSTokenLogitsProcessor": () => (/* binding */ ForcedBOSTokenLogitsProcessor), /* harmony export */ "ForcedEOSTokenLogitsProcessor": () => (/* binding */ ForcedEOSTokenLogitsProcessor), /* harmony export */ "LogitsProcessor": () => (/* binding */ LogitsProcessor), /* harmony export */ "LogitsProcessorList": () => (/* binding */ LogitsProcessorList), /* harmony export */ "LogitsWarper": () => (/* binding */ LogitsWarper), /* harmony export */ "MinLengthLogitsProcessor": () => (/* binding */ MinLengthLogitsProcessor), /* harmony export */ "MinNewTokensLengthLogitsProcessor": () => (/* binding */ MinNewTokensLengthLogitsProcessor), /* harmony export */ "NoBadWordsLogitsProcessor": () => (/* binding */ NoBadWordsLogitsProcessor), /* harmony export */ "NoRepeatNGramLogitsProcessor": () => (/* binding */ NoRepeatNGramLogitsProcessor), /* harmony export */ "RepetitionPenaltyLogitsProcessor": () => (/* binding */ RepetitionPenaltyLogitsProcessor), /* harmony export */ "SuppressTokensAtBeginLogitsProcessor": () => (/* binding */ SuppressTokensAtBeginLogitsProcessor), /* harmony export */ "TemperatureLogitsWarper": () => (/* binding */ TemperatureLogitsWarper), /* harmony export */ "TopKLogitsWarper": () => (/* binding */ TopKLogitsWarper), /* harmony export */ "TopPLogitsWarper": () => (/* binding */ TopPLogitsWarper), /* harmony export */ "WhisperTimeStampLogitsProcessor": () => (/* binding */ WhisperTimeStampLogitsProcessor) /* harmony export */ }); /* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); /* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); /* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/maths.js */ "./src/utils/maths.js"); /** * @module generation/logits_process */ /** * Abstract base class for all logit processors that can be applied during generation. */ class LogitsProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * Apply the processor to the input logits. * * @abstract * @param {number[][]} input_ids The input ids. * @param {Tensor} logits The logits to process. * @throws {Error} Throws an error if `_call` is not implemented in the subclass. */ _call(input_ids, logits) { throw Error("`_call` should be implemented in a subclass") } } /** * Abstract base class for all logit warpers that can be applied during generation with multinomial sampling. */ class LogitsWarper extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * Apply the processor to the input logits. * * @abstract * @param {number[][]} input_ids The input ids. * @param {Tensor} logits The logits to process. * @throws {Error} Throws an error if `_call` is not implemented in the subclass. */ _call(input_ids, logits) { throw Error("`_call` should be implemented in a subclass") } } /** * A class representing a list of logits processors. A logits processor is a function that modifies the logits * output of a language model. This class provides methods for adding new processors and applying all processors to a * batch of logits. */ class LogitsProcessorList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * Constructs a new instance of `LogitsProcessorList`. */ constructor() { super(); this.processors = []; } /** * Adds a new logits processor to the list. * * @param {LogitsProcessor} item The logits processor function to add. */ push(item) { this.processors.push(item); } /** * Adds multiple logits processors to the list. * * @param {LogitsProcessor[]} items The logits processor functions to add. */ extend(items) { this.processors.push(...items); } /** * Applies all logits processors in the list to a batch of logits, modifying them in-place. * * @param {number[][]} input_ids The input IDs for the language model. * @param {Tensor} logits */ _call(input_ids, logits) { let toReturn = logits; // NOTE: Most processors modify logits inplace for (const processor of this.processors) { toReturn = processor(input_ids, toReturn); } return toReturn; } [Symbol.iterator]() { return this.processors.values(); } } // DEPRECATED: https://github.com/huggingface/transformers/pull/29485 // /** // * A logits processor that forces a specific token to be generated by the decoder. // */ // export class ForceTokensLogitsProcessor extends LogitsProcessor { // /** // * Constructs a new instance of `ForceTokensLogitsProcessor`. // * // * @param {[number, number][]} forced_decoder_ids The ids of tokens that should be forced. // */ // constructor(forced_decoder_ids) { // super(); // // TODO: convert to `new Map(forced_decoder_ids)` // this.force_token_map = Object.fromEntries(forced_decoder_ids ?? []); // } // /** // * Apply the processor to the input logits. // * // * @param {number[][]} input_ids The input ids. // * @param {Tensor} logits The logits to process. // * @returns {Tensor} The processed logits. // */ // _call(input_ids, logits) { // console.log('this.force_token_map', this.force_token_map) // console.log('call ForceTokensLogitsProcessor', input_ids, logits) // console.log('input_ids.length', input_ids.length) // let map = this.force_token_map[input_ids.length]; // if (map) { // There exists a mapping // logits.data.fill(-Infinity) // logits.data[map] = 0; // } // console.log('map', map) // // throw Error("Not implemented") // return logits; // } // } /** * A LogitsProcessor that forces a BOS token at the beginning of the generated sequence. */ class ForcedBOSTokenLogitsProcessor extends LogitsProcessor { /** * Create a ForcedBOSTokenLogitsProcessor. * @param {number} bos_token_id The ID of the beginning-of-sequence token to be forced. */ constructor(bos_token_id) { super(); this.bos_token_id = bos_token_id; } /** * Apply the BOS token forcing to the logits. * @param {number[][]} input_ids The input IDs. * @param {Tensor} logits The logits. * @returns {Object} The logits with BOS token forcing. */ _call(input_ids, logits) { for (let i = 0; i < input_ids.length; ++i) { if (input_ids[i].length === 1) { const batch_logits = logits[i]; batch_logits.data.fill(-Infinity); batch_logits.data[this.bos_token_id] = 0; } } return logits; } } /** * A logits processor that forces end-of-sequence token probability to 1. */ class ForcedEOSTokenLogitsProcessor extends LogitsProcessor { /** * Create a ForcedEOSTokenLogitsProcessor. * @param {number} max_length Max length of the sequence. * @param {number|number[]} forced_eos_token_id The ID of the end-of-sequence token to be forced. */ constructor(max_length, forced_eos_token_id) { super(); this.max_length = max_length; this.forced_eos_token_id = forced_eos_token_id; } /** * Apply the processor to input_ids and logits. * * @param {number[][]} input_ids The input ids. * @param {Tensor} logits The logits tensor. */ _call(input_ids, logits) { // console.log('call ForcedEOSTokenLogitsProcessor') // TODO } } /** * A LogitsProcessor that suppresses a list of tokens as soon as the `generate` function starts * generating using `begin_index` tokens. This should ensure that the tokens defined by * `begin_suppress_tokens` at not sampled at the begining of the generation. */ class SuppressTokensAtBeginLogitsProcessor extends LogitsProcessor { /** * Create a SuppressTokensAtBeginLogitsProcessor. * @param {number[]} begin_suppress_tokens The IDs of the tokens to suppress. * @param {number} begin_index The number of tokens to generate before suppressing tokens. */ constructor(begin_suppress_tokens, begin_index) { super(); this.begin_suppress_tokens = begin_suppress_tokens; this.begin_index = begin_index; } /** * Apply the BOS token forcing to the logits. * @param {number[][]} input_ids The input IDs. * @param {Tensor} logits The logits. * @returns {Object} The logits with BOS token forcing. */ _call(input_ids, logits) { for (let i = 0; i < input_ids.length; ++i) { if (input_ids[i].length === this.begin_index) { const batch_logits = logits[i]; for (const token_id of this.begin_suppress_tokens) { batch_logits.data[token_id] = -Infinity; } } } return logits; } } /** * A LogitsProcessor that handles adding timestamps to generated text. */ class WhisperTimeStampLogitsProcessor extends LogitsProcessor { /** * Constructs a new WhisperTimeStampLogitsProcessor. * @param {Object} generate_config The config object passed to the `generate()` method of a transformer model. * @param {number} generate_config.eos_token_id The ID of the end-of-sequence token. * @param {number} generate_config.no_timestamps_token_id The ID of the token used to indicate that a token should not have a timestamp. * @param {[number, number][]} [generate_config.forced_decoder_ids] An array of two-element arrays representing decoder IDs that are forced to appear in the output. The second element of each array indicates whether the token is a timestamp. * @param {number} [generate_config.max_initial_timestamp_index] The maximum index at which an initial timestamp can appear. */ constructor(generate_config) { super(); this.eos_token_id = generate_config.eos_token_id; this.no_timestamps_token_id = generate_config.no_timestamps_token_id; this.timestamp_begin = this.no_timestamps_token_id + 1; this.begin_index = (generate_config.forced_decoder_ids || []).length + 2; if (generate_config.forced_decoder_ids.slice(-1)[0][1] === this.no_timestamps_token_id) { this.begin_index -= 1; } this.max_initial_timestamp_index = generate_config.max_initial_timestamp_index; } /** * Modify the logits to handle timestamp tokens. * @param {number[][]} input_ids The input sequence of tokens. * @param {Tensor} logits The logits output by the model. * @returns {Tensor} The modified logits. */ _call(input_ids, logits) { for (let i = 0; i < input_ids.length; ++i) { const batch_logits = logits[i]; const logitsData = /** @type {Float32Array} */(batch_logits.data); // suppress <|notimestamps|> which is handled by without_timestamps logitsData[this.no_timestamps_token_id] = -Infinity; if (input_ids[i].length === this.begin_index - 1) { logitsData.fill(-Infinity); logitsData[this.timestamp_begin] = 0; continue; } // timestamps have to appear in pairs, except directly before eos_token; mask logits accordingly const seq = input_ids[i].slice(this.begin_index); const last_was_timestamp = seq.length >= 1 && seq[seq.length - 1] >= this.timestamp_begin; const penultimate_was_timestamp = seq.length < 2 || seq[seq.length - 2] >= this.timestamp_begin; if (last_was_timestamp) { if (penultimate_was_timestamp) { // has to be non-timestamp logitsData.subarray(this.timestamp_begin).fill(-Infinity); } else { // cannot be normal text tokens logitsData.subarray(0, this.eos_token_id).fill(-Infinity); } } // apply the `max_initial_timestamp` option if (input_ids[i].length === this.begin_index && this.max_initial_timestamp_index !== null) { const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index; logitsData.subarray(last_allowed + 1).fill(-Infinity); } // if sum of probability over timestamps is above any other token, sample timestamp const logprobs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.log_softmax)(logitsData); const timestamp_logprob = Math.log(logprobs.subarray(this.timestamp_begin).map(Math.exp).reduce((a, b) => a + b)); const max_text_token_logprob = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logprobs.subarray(0, this.timestamp_begin))[0]; if (timestamp_logprob > max_text_token_logprob) { logitsData.subarray(0, this.timestamp_begin).fill(-Infinity); } } return logits; } } /** * A logits processor that disallows ngrams of a certain size to be repeated. */ class NoRepeatNGramLogitsProcessor extends LogitsProcessor { /** * Create a NoRepeatNGramLogitsProcessor. * @param {number} no_repeat_ngram_size The no-repeat-ngram size. All ngrams of this size can only occur once. */ constructor(no_repeat_ngram_size) { super(); this.no_repeat_ngram_size = no_repeat_ngram_size; } /** * Generate n-grams from a sequence of token ids. * @param {number[]} prevInputIds List of previous input ids * @returns {Map} Map of generated n-grams */ getNgrams(prevInputIds) { const curLen = prevInputIds.length; /**@type {number[][]} */ const ngrams = []; for (let j = 0; j < curLen + 1 - this.no_repeat_ngram_size; ++j) { const ngram = []; for (let k = 0; k < this.no_repeat_ngram_size; ++k) { ngram.push(prevInputIds[j + k]); } ngrams.push(ngram); } /** @type {Map} */ const generatedNgram = new Map(); for (const ngram of ngrams) { const prevNgram = ngram.slice(0, ngram.length - 1); const prevNgramKey = JSON.stringify(prevNgram); const prevNgramValue = generatedNgram.get(prevNgramKey) ?? []; prevNgramValue.push(ngram[ngram.length - 1]); generatedNgram.set(prevNgramKey, prevNgramValue); } return generatedNgram; } /** * Generate n-grams from a sequence of token ids. * @param {Map} bannedNgrams Map of banned n-grams * @param {number[]} prevInputIds List of previous input ids * @returns {number[]} Map of generated n-grams */ getGeneratedNgrams(bannedNgrams, prevInputIds) { const ngramIdx = prevInputIds.slice(prevInputIds.length + 1 - this.no_repeat_ngram_size, prevInputIds.length); const banned = bannedNgrams.get(JSON.stringify(ngramIdx)) ?? []; return banned; } /** * Calculate banned n-gram tokens * @param {number[]} prevInputIds List of previous input ids * @returns {number[]} Map of generated n-grams */ calcBannedNgramTokens(prevInputIds) { const bannedTokens = []; if (prevInputIds.length + 1 < this.no_repeat_ngram_size) { // return no banned tokens if we haven't generated no_repeat_ngram_size tokens yet return bannedTokens; } else { const generatedNgrams = this.getNgrams(prevInputIds); const bannedTokens = this.getGeneratedNgrams(generatedNgrams, prevInputIds); return bannedTokens; } } /** * Apply the no-repeat-ngram processor to the logits. * @param {number[][]} input_ids The input IDs. * @param {Tensor} logits The logits. * @returns {Object} The logits with no-repeat-ngram processing. */ _call(input_ids, logits) { for (let i = 0; i < input_ids.length; ++i) { const batch_logits = logits[i]; const bannedTokens = this.calcBannedNgramTokens(input_ids[i]); for (const token of bannedTokens) { batch_logits.data[token] = -Infinity; } } return logits; } } /** * A logits processor that penalises repeated output tokens. */ class RepetitionPenaltyLogitsProcessor extends LogitsProcessor { /** * Create a RepetitionPenaltyLogitsProcessor. * @param {number} penalty The penalty to apply for repeated tokens. */ constructor(penalty) { super(); this.penalty = penalty; } /** * Apply the repetition penalty to the logits. * @param {number[][]} input_ids The input IDs. * @param {Tensor} logits The logits. * @returns {Object} The logits with repetition penalty processing. */ _call(input_ids, logits) { // Modify the logits corresponding to each element in `input_ids`. // As a consequence, the logits corresponding to tokens that appear // many times in the output will be penalised more. for (let i = 0; i < input_ids.length; ++i) { const batch_logits = logits[i]; for (const input_id of input_ids[i]) { if (batch_logits.data[input_id] < 0) { batch_logits.data[input_id] *= this.penalty; } else { batch_logits.data[input_id] /= this.penalty; } } } return logits } } /** * A logits processor that enforces a minimum number of tokens. */ class MinLengthLogitsProcessor extends LogitsProcessor { /** * Create a MinLengthLogitsProcessor. * @param {number} min_length The minimum length below which the score of `eos_token_id` is set to negative infinity. * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. */ constructor(min_length, eos_token_id) { super(); this.min_length = min_length; this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; } /** * Apply logit processor. * @param {number[][]} input_ids The input IDs. * @param {Tensor} logits The logits. * @returns {Object} The processed logits. */ _call(input_ids, logits) { for (let i = 0; i < input_ids.length; ++i) { if (input_ids[i].length < this.min_length) { const batch_logits = logits[i]; for (const eos_token of this.eos_token_id) { batch_logits.data[eos_token] = -Infinity; } } } return logits } } /** * A logits processor that enforces a minimum number of new tokens. */ class MinNewTokensLengthLogitsProcessor extends LogitsProcessor { /** * Create a MinNewTokensLengthLogitsProcessor. * @param {number} prompt_length_to_skip The input tokens length. * @param {number} min_new_tokens The minimum *new* tokens length below which the score of `eos_token_id` is set to negative infinity. * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. */ constructor(prompt_length_to_skip, min_new_tokens, eos_token_id) { super(); this.prompt_length_to_skip = prompt_length_to_skip; this.min_new_tokens = min_new_tokens; this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; } /** * Apply logit processor. * @param {number[][]} input_ids The input IDs. * @param {Tensor} logits The logits. * @returns {Object} The processed logits. */ _call(input_ids, logits) { for (let i = 0; i < input_ids.length; ++i) { const new_tokens_length = input_ids[i].length - this.prompt_length_to_skip; if (new_tokens_length < this.min_new_tokens) { const batch_logits = logits[i]; for (const eos_token of this.eos_token_id) { batch_logits[eos_token] = -Infinity; } } } return logits } } class NoBadWordsLogitsProcessor extends LogitsProcessor { /** * Create a `NoBadWordsLogitsProcessor`. * @param {number[][]} bad_words_ids List of list of token ids that are not allowed to be generated. * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. */ constructor(bad_words_ids, eos_token_id) { super(); this.bad_words_ids = bad_words_ids; this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; } /** * Apply logit processor. * @param {number[][]} input_ids The input IDs. * @param {Tensor} logits The logits. * @returns {Object} The processed logits. */ _call(input_ids, logits) { for (let i = 0; i < input_ids.length; ++i) { const batch_logits = logits[i]; for (const bad_word_ids of this.bad_words_ids) { // Whether to modify the logits of the last token in the bad word id sequence let mark = true; // For each bad word in the list, if the current sequence of input ids ends with this sequence (excluding the last), // then we set the logits of the last bad word id to -Infinity. for (let i = 1; i <= bad_word_ids.length - 1 && bad_word_ids.length < input_ids[i].length; ++i) { if (bad_word_ids.at(-i - 1) !== input_ids[i].at(-i)) { // We have found a mismatch mark = false; break; } } if (mark) { batch_logits[bad_word_ids.at(-1)] = -Infinity; } } } return logits } } /** * [`LogitsProcessor`] for classifier free guidance (CFG). The scores are split over the batch dimension, * where the first half correspond to the conditional logits (predicted from the input prompt) and the second half * correspond to the unconditional logits (predicted from an empty or 'null' prompt). The processor computes a * weighted average across the conditional and unconditional logits, parameterised by the `guidance_scale`. * * See [the paper](https://arxiv.org/abs/2306.05284) for more information. */ class ClassifierFreeGuidanceLogitsProcessor extends LogitsProcessor { /** * Create a `ClassifierFreeGuidanceLogitsProcessor`. * @param {number} guidance_scale The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. * Higher guidance scale encourages the model to generate samples that are more closely linked to the input * prompt, usually at the expense of poorer quality. */ constructor(guidance_scale) { super(); if (guidance_scale <= 1) { throw new Error( `Require guidance scale >1 to use the classifier free guidance processor, got guidance scale ${guidance_scale}.` ) } this.guidance_scale = guidance_scale; } /** * Apply logit processor. * @param {number[][]} input_ids The input IDs. * @param {Tensor} logits The logits. * @returns {Object} The processed logits. */ _call(input_ids, logits) { if (logits.dims[0] !== 2 * input_ids.length) { throw new Error( `Logits should have twice the batch size of the input ids, the first half of batches corresponding to ` + `the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got ` + `batch size ${logits.dims[0]} for the logits and ${input_ids.length} for the input ids.` ) } const unguided_bsz = input_ids.length; const cond_logits = logits.slice([0, unguided_bsz], null); const uncond_logits = logits.slice([unguided_bsz, logits.dims[0]], null); // Merge into uncond_logits (to save memory). This is equivalent to the following: // scores = uncond_logits + (cond_logits - uncond_logits) * guidance_scale for (let i = 0; i < uncond_logits.data.length; ++i) { uncond_logits.data[i] += (cond_logits.data[i] - uncond_logits.data[i]) * this.guidance_scale; } return uncond_logits; } } /** * [`LogitsWarper`] for temperature (exponential scaling output probability distribution), which effectively means * that it can control the randomness of the predicted tokens. Often used together with [`TopPLogitsWarper`] and [`TopKLogitsWarper`]. */ class TemperatureLogitsWarper extends LogitsWarper { /** * Create a `TemperatureLogitsWarper`. * @param {number} temperature Strictly positive float value used to modulate the logits distribution. * A value smaller than `1` decreases randomness (and vice versa), with `0` being equivalent to shifting * all probability mass to the most likely token. */ constructor(temperature) { super(); if (typeof temperature !== 'number' || temperature <= 0) { let errorMessage = `\`temperature\` (=${temperature}) must be a strictly positive float, otherwise your next token scores will be invalid.`; if (temperature === 0) { errorMessage += " If you're looking for greedy decoding strategies, set `do_sample=false`." } } this.temperature = temperature; } /** * Apply logit warper. * @param {number[][]} input_ids The input IDs. * @param {Tensor} logits The logits. * @returns {Object} The processed logits. */ _call(input_ids, logits) { const logitsData = /** @type {Float32Array} */(logits.data); for (let i = 0; i < logitsData.length; ++i) { logitsData[i] /= this.temperature; } return logits; } } /** * [`LogitsWarper`] that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off. * Often used together with [`TemperatureLogitsWarper`] and [`TopKLogitsWarper`]. */ class TopPLogitsWarper extends LogitsWarper { /** * Create a `TopPLogitsWarper`. * @param {number} top_p If set to < 1, only the smallest set of most probable tokens with * probabilities that add up to `top_p` or higher are kept for generation. * @param {Object} options Additional options for the top-p sampling. * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. */ constructor(top_p, { filter_value = -Infinity, min_tokens_to_keep = 1, } = {}) { super(); if (top_p < 0 || top_p > 1.0) { throw new Error(`\`top_p\` must be a float > 0 and < 1, but is ${top_p}`) } if (!Number.isInteger(min_tokens_to_keep) || min_tokens_to_keep < 1) { throw new Error(`\`min_tokens_to_keep\` must be a positive integer, but is ${min_tokens_to_keep}`) } this.top_p = top_p this.filter_value = filter_value this.min_tokens_to_keep = min_tokens_to_keep } } /** * [`LogitsWarper`] that performs top-k, i.e. restricting to the k highest probability elements. * Often used together with [`TemperatureLogitsWarper`] and [`TopPLogitsWarper`]. */ class TopKLogitsWarper extends LogitsWarper { /** * Create a `TopKLogitsWarper`. * @param {number} top_k If set to > 0, only the top `top_k` tokens are kept for generation. * @param {Object} options Additional options for the top-k sampling. * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. */ constructor(top_k, { filter_value = -Infinity, min_tokens_to_keep = 1, } = {}) { super(); if (!Number.isInteger(top_k) || top_k < 0) { throw new Error(`\`top_k\` must be a positive integer, but is ${top_k}`) } this.top_k = Math.max(top_k, min_tokens_to_keep) this.filter_value = filter_value } } /***/ }), /***/ "./src/generation/logits_sampler.js": /*!******************************************!*\ !*** ./src/generation/logits_sampler.js ***! \******************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "LogitsSampler": () => (/* binding */ LogitsSampler) /* harmony export */ }); /* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); /* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); /* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/maths.js */ "./src/utils/maths.js"); /* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); /** * @module generation/logits_sampler */ /** * Sampler is a base class for all sampling methods used for text generation. */ class LogitsSampler extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * Creates a new Sampler object with the specified generation config. * @param {GenerationConfig} generation_config The generation config. */ constructor(generation_config) { super(); this.generation_config = generation_config; } /** * Executes the sampler, using the specified logits. * @param {Tensor} logits * @param {number} index * @returns {[number, number][]} */ _call(logits, index = -1) { // Sample from logits, of dims [batch, sequence_length, vocab_size]. // If index is specified, sample from [batch, index, vocab_size]. return this.sample(logits, index); } /** * Abstract method for sampling the logits. * @param {Tensor} logits * @param {number} index * @throws {Error} * @returns {[number, number][]} */ sample(logits, index) { throw Error("sample should be implemented in subclasses.") } /** * Returns the specified logits as an array, with temperature applied. * @param {Tensor} logits * @param {number} index * @returns {Float32Array} */ getLogits(logits, index) { let vocabSize = logits.dims.at(-1); let logs = /** @type {Float32Array} */(logits.data); if (index === -1) { logs = logs.slice(-vocabSize); } else { let startIndex = index * vocabSize; logs = logs.slice(startIndex, startIndex + vocabSize); } return logs; } /** * Selects an item randomly based on the specified probabilities. * @param {Array} probabilities An array of probabilities to use for selection. * @returns {number} The index of the selected item. */ randomSelect(probabilities) { // Return index of chosen item let sumProbabilities = probabilities.reduce((acc, curr) => acc + curr, 0); let r = Math.random() * sumProbabilities; for (let i = 0; i < probabilities.length; ++i) { r -= probabilities[i]; if (r <= 0) { return i; } } return 0; // return first (most probable) as a fallback } /** * Returns a Sampler object based on the specified options. * @param {GenerationConfig} generation_config An object containing options for the sampler. * @returns {LogitsSampler} A Sampler object. */ static getSampler(generation_config) { // - *greedy decoding*: `num_beams=1` and `do_sample=False` // - *contrastive search*: `penalty_alpha>0` and `top_k>1` // - *multinomial sampling*: `num_beams=1` and `do_sample=True` // - *beam-search decoding*: `num_beams>1` and `do_sample=False` // - *beam-search multinomial sampling*: `num_beams>1` and `do_sample=True` // - *diverse beam-search decoding*: `num_beams>1` and `num_beam_groups>1` // - *constrained beam-search decoding*: `constraints!=None` or `force_words_ids!=None` // NOTE: beam search is implemented directly into the generation function if (generation_config.do_sample) { return new MultinomialSampler(generation_config); } else if (generation_config.num_beams > 1) { return new BeamSearchSampler(generation_config); } else { if (generation_config.num_return_sequences > 1) { throw Error(`num_return_sequences has to be 1 when doing greedy search, but is ${generation_config.num_return_sequences}.`) } return new GreedySampler(generation_config); } } } /** * Class representing a Greedy Sampler. */ class GreedySampler extends LogitsSampler { /** * Sample the maximum probability of a given logits tensor. * @param {Tensor} logits * @param {number} [index=-1] * @returns {[number, number][]} An array with a single tuple, containing the index of the maximum value and a meaningless score (since this is a greedy search). */ sample(logits, index = -1) { // NOTE: no need to do log_softmax here since we only take the maximum let logs = this.getLogits(logits, index); let argmax = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logs)[1]; // Note: score is meaningless in this context, since we are performing // greedy search (p = 1 => log(p) = 0) return [ [argmax, 0] ]; } } /** * Class representing a MultinomialSampler. */ class MultinomialSampler extends LogitsSampler { /** * Sample from the logits. * @param {Tensor} logits * @param {number} index * @returns {[number, number][]} */ sample(logits, index = -1) { let k = logits.dims.at(-1); // defaults to vocab size if (this.generation_config.top_k > 0) { k = Math.min(this.generation_config.top_k, k); } // Get logits of nth token const logs = this.getLogits(logits, index); // Get top k tokens const topLogits = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.getTopItems)(logs, k); // Compute softmax over logits const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(topLogits.map(x => x[1])); return Array.from({ length: this.generation_config.num_beams }, () => { const sampledIndex = this.randomSelect(probabilities); return [ topLogits[sampledIndex][0], // token id Math.log(probabilities[sampledIndex]), // score ]; }); } } /** * Class representing a BeamSearchSampler. */ class BeamSearchSampler extends LogitsSampler { /** * Sample from the logits. * @param {Tensor} logits * @param {number} index * @returns {[number, number][]} */ sample(logits, index = -1) { let k = logits.dims.at(-1); // defaults to vocab size if (this.generation_config.top_k > 0) { k = Math.min(this.generation_config.top_k, k); } // Get logits of nth token const logs = this.getLogits(logits, index); // Get top k tokens const topLogits = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.getTopItems)(logs, k); // Compute softmax over logits const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(topLogits.map(x => x[1])); return Array.from({ length: this.generation_config.num_beams }, (_, i) => { return [ topLogits[i][0], // token id Math.log(probabilities[i]), // score ]; }); } } /***/ }), /***/ "./src/generation/stopping_criteria.js": /*!*********************************************!*\ !*** ./src/generation/stopping_criteria.js ***! \*********************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "EosTokenCriteria": () => (/* binding */ EosTokenCriteria), /* harmony export */ "MaxLengthCriteria": () => (/* binding */ MaxLengthCriteria), /* harmony export */ "StoppingCriteria": () => (/* binding */ StoppingCriteria), /* harmony export */ "StoppingCriteriaList": () => (/* binding */ StoppingCriteriaList) /* harmony export */ }); /* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); /** * @module generation/stopping_criteria */ // NOTE: // Stopping Criteria returns a list of `batch_size` booleans, indicating whether each sequence in the batch should be stopped. /** * Abstract base class for all stopping criteria that can be applied during generation. */ class StoppingCriteria extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * * @param {number[][]} input_ids (`number[][]` of shape `(batch_size, sequence_length)`): * Indices of input sequence tokens in the vocabulary. * @param {number[][]} scores scores (`number[][]` of shape `(batch_size, config.vocab_size)`): * Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax * or scores for each vocabulary token after SoftMax. * @returns {boolean[]} A list of booleans indicating whether each sequence should be stopped. */ _call(input_ids, scores) { throw Error("StoppingCriteria needs to be subclassed"); } } /** */ class StoppingCriteriaList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * Constructs a new instance of `StoppingCriteriaList`. */ constructor() { super(); this.criteria = []; } /** * Adds a new stopping criterion to the list. * * @param {StoppingCriteria} item The stopping criterion to add. */ push(item) { this.criteria.push(item); } /** * Adds multiple stopping criteria to the list. * * @param {StoppingCriteriaList|StoppingCriteria[]} items The stopping criteria to add. */ extend(items) { if (items instanceof StoppingCriteriaList) { items = items.criteria; } this.criteria.push(...items); } _call(input_ids, scores) { const is_done = new Array(input_ids.length).fill(false); for (const criterion of this.criteria) { const criterion_done = criterion(input_ids, scores); for (let i = 0; i < is_done.length; ++i) { is_done[i] ||= criterion_done[i]; } } return is_done; } [Symbol.iterator]() { return this.criteria.values(); } } /** * This class can be used to stop generation whenever the full generated number of tokens exceeds `max_length`. * Keep in mind for decoder-only type of transformers, this will include the initial prompted tokens. */ class MaxLengthCriteria extends StoppingCriteria { /** * * @param {number} max_length The maximum length that the output sequence can have in number of tokens. * @param {number} [max_position_embeddings=null] The maximum model length, as defined by the model's `config.max_position_embeddings` attribute. */ constructor(max_length, max_position_embeddings = null) { super(); this.max_length = max_length; this.max_position_embeddings = max_position_embeddings; } _call(input_ids) { return input_ids.map(ids => ids.length >= this.max_length); } } // TODO: add MaxTimeCriteria /** * This class can be used to stop generation whenever the "end-of-sequence" token is generated. * By default, it uses the `model.generation_config.eos_token_id`. */ class EosTokenCriteria extends StoppingCriteria { /** * * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. * Optionally, use a list to set multiple *end-of-sequence* tokens. */ constructor(eos_token_id) { super(); if (!Array.isArray(eos_token_id)) { eos_token_id = [eos_token_id]; } this.eos_token_id = eos_token_id; } /** * * @param {number[][]} input_ids * @param {number[][]} scores * @returns {boolean[]} */ _call(input_ids, scores) { return input_ids.map(ids => this.eos_token_id.includes(ids.at(-1))); } } /***/ }), /***/ "./src/generation/streamers.js": /*!*************************************!*\ !*** ./src/generation/streamers.js ***! \*************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "BaseStreamer": () => (/* binding */ BaseStreamer) /* harmony export */ }); /** * @module generation/streamers */ class BaseStreamer { /** * Function that is called by `.generate()` to push new tokens * @param {bigint[][]} value */ put(value) { throw Error('Not implemented'); } /** * Function that is called by `.generate()` to signal the end of generation */ end() { throw Error('Not implemented'); } } /***/ }), /***/ "./src/models.js": /*!***********************!*\ !*** ./src/models.js ***! \***********************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "ASTForAudioClassification": () => (/* binding */ ASTForAudioClassification), /* harmony export */ "ASTModel": () => (/* binding */ ASTModel), /* harmony export */ "ASTPreTrainedModel": () => (/* binding */ ASTPreTrainedModel), /* harmony export */ "AlbertForMaskedLM": () => (/* binding */ AlbertForMaskedLM), /* harmony export */ "AlbertForQuestionAnswering": () => (/* binding */ AlbertForQuestionAnswering), /* harmony export */ "AlbertForSequenceClassification": () => (/* binding */ AlbertForSequenceClassification), /* harmony export */ "AlbertModel": () => (/* binding */ AlbertModel), /* harmony export */ "AlbertPreTrainedModel": () => (/* binding */ AlbertPreTrainedModel), /* harmony export */ "AutoModel": () => (/* binding */ AutoModel), /* harmony export */ "AutoModelForAudioClassification": () => (/* binding */ AutoModelForAudioClassification), /* harmony export */ "AutoModelForAudioFrameClassification": () => (/* binding */ AutoModelForAudioFrameClassification), /* harmony export */ "AutoModelForCTC": () => (/* binding */ AutoModelForCTC), /* harmony export */ "AutoModelForCausalLM": () => (/* binding */ AutoModelForCausalLM), /* harmony export */ "AutoModelForDepthEstimation": () => (/* binding */ AutoModelForDepthEstimation), /* harmony export */ "AutoModelForDocumentQuestionAnswering": () => (/* binding */ AutoModelForDocumentQuestionAnswering), /* harmony export */ "AutoModelForImageClassification": () => (/* binding */ AutoModelForImageClassification), /* harmony export */ "AutoModelForImageFeatureExtraction": () => (/* binding */ AutoModelForImageFeatureExtraction), /* harmony export */ "AutoModelForImageMatting": () => (/* binding */ AutoModelForImageMatting), /* harmony export */ "AutoModelForImageSegmentation": () => (/* binding */ AutoModelForImageSegmentation), /* harmony export */ "AutoModelForImageToImage": () => (/* binding */ AutoModelForImageToImage), /* harmony export */ "AutoModelForMaskGeneration": () => (/* binding */ AutoModelForMaskGeneration), /* harmony export */ "AutoModelForMaskedLM": () => (/* binding */ AutoModelForMaskedLM), /* harmony export */ "AutoModelForObjectDetection": () => (/* binding */ AutoModelForObjectDetection), /* harmony export */ "AutoModelForQuestionAnswering": () => (/* binding */ AutoModelForQuestionAnswering), /* harmony export */ "AutoModelForSemanticSegmentation": () => (/* binding */ AutoModelForSemanticSegmentation), /* harmony export */ "AutoModelForSeq2SeqLM": () => (/* binding */ AutoModelForSeq2SeqLM), /* harmony export */ "AutoModelForSequenceClassification": () => (/* binding */ AutoModelForSequenceClassification), /* harmony export */ "AutoModelForSpeechSeq2Seq": () => (/* binding */ AutoModelForSpeechSeq2Seq), /* harmony export */ "AutoModelForTextToSpectrogram": () => (/* binding */ AutoModelForTextToSpectrogram), /* harmony export */ "AutoModelForTextToWaveform": () => (/* binding */ AutoModelForTextToWaveform), /* harmony export */ "AutoModelForTokenClassification": () => (/* binding */ AutoModelForTokenClassification), /* harmony export */ "AutoModelForVision2Seq": () => (/* binding */ AutoModelForVision2Seq), /* harmony export */ "AutoModelForXVector": () => (/* binding */ AutoModelForXVector), /* harmony export */ "AutoModelForZeroShotObjectDetection": () => (/* binding */ AutoModelForZeroShotObjectDetection), /* harmony export */ "BartForConditionalGeneration": () => (/* binding */ BartForConditionalGeneration), /* harmony export */ "BartForSequenceClassification": () => (/* binding */ BartForSequenceClassification), /* harmony export */ "BartModel": () => (/* binding */ BartModel), /* harmony export */ "BartPretrainedModel": () => (/* binding */ BartPretrainedModel), /* harmony export */ "BaseModelOutput": () => (/* binding */ BaseModelOutput), /* harmony export */ "BeitForImageClassification": () => (/* binding */ BeitForImageClassification), /* harmony export */ "BeitModel": () => (/* binding */ BeitModel), /* harmony export */ "BeitPreTrainedModel": () => (/* binding */ BeitPreTrainedModel), /* harmony export */ "BertForMaskedLM": () => (/* binding */ BertForMaskedLM), /* harmony export */ "BertForQuestionAnswering": () => (/* binding */ BertForQuestionAnswering), /* harmony export */ "BertForSequenceClassification": () => (/* binding */ BertForSequenceClassification), /* harmony export */ "BertForTokenClassification": () => (/* binding */ BertForTokenClassification), /* harmony export */ "BertModel": () => (/* binding */ BertModel), /* harmony export */ "BertPreTrainedModel": () => (/* binding */ BertPreTrainedModel), /* harmony export */ "BlenderbotForConditionalGeneration": () => (/* binding */ BlenderbotForConditionalGeneration), /* harmony export */ "BlenderbotModel": () => (/* binding */ BlenderbotModel), /* harmony export */ "BlenderbotPreTrainedModel": () => (/* binding */ BlenderbotPreTrainedModel), /* harmony export */ "BlenderbotSmallForConditionalGeneration": () => (/* binding */ BlenderbotSmallForConditionalGeneration), /* harmony export */ "BlenderbotSmallModel": () => (/* binding */ BlenderbotSmallModel), /* harmony export */ "BlenderbotSmallPreTrainedModel": () => (/* binding */ BlenderbotSmallPreTrainedModel), /* harmony export */ "BloomForCausalLM": () => (/* binding */ BloomForCausalLM), /* harmony export */ "BloomModel": () => (/* binding */ BloomModel), /* harmony export */ "BloomPreTrainedModel": () => (/* binding */ BloomPreTrainedModel), /* harmony export */ "CLIPModel": () => (/* binding */ CLIPModel), /* harmony export */ "CLIPPreTrainedModel": () => (/* binding */ CLIPPreTrainedModel), /* harmony export */ "CLIPSegForImageSegmentation": () => (/* binding */ CLIPSegForImageSegmentation), /* harmony export */ "CLIPSegModel": () => (/* binding */ CLIPSegModel), /* harmony export */ "CLIPSegPreTrainedModel": () => (/* binding */ CLIPSegPreTrainedModel), /* harmony export */ "CLIPTextModelWithProjection": () => (/* binding */ CLIPTextModelWithProjection), /* harmony export */ "CLIPVisionModelWithProjection": () => (/* binding */ CLIPVisionModelWithProjection), /* harmony export */ "CamembertForMaskedLM": () => (/* binding */ CamembertForMaskedLM), /* harmony export */ "CamembertForQuestionAnswering": () => (/* binding */ CamembertForQuestionAnswering), /* harmony export */ "CamembertForSequenceClassification": () => (/* binding */ CamembertForSequenceClassification), /* harmony export */ "CamembertForTokenClassification": () => (/* binding */ CamembertForTokenClassification), /* harmony export */ "CamembertModel": () => (/* binding */ CamembertModel), /* harmony export */ "CamembertPreTrainedModel": () => (/* binding */ CamembertPreTrainedModel), /* harmony export */ "CausalLMOutput": () => (/* binding */ CausalLMOutput), /* harmony export */ "CausalLMOutputWithPast": () => (/* binding */ CausalLMOutputWithPast), /* harmony export */ "ChineseCLIPModel": () => (/* binding */ ChineseCLIPModel), /* harmony export */ "ChineseCLIPPreTrainedModel": () => (/* binding */ ChineseCLIPPreTrainedModel), /* harmony export */ "ClapAudioModelWithProjection": () => (/* binding */ ClapAudioModelWithProjection), /* harmony export */ "ClapModel": () => (/* binding */ ClapModel), /* harmony export */ "ClapPreTrainedModel": () => (/* binding */ ClapPreTrainedModel), /* harmony export */ "ClapTextModelWithProjection": () => (/* binding */ ClapTextModelWithProjection), /* harmony export */ "CodeGenForCausalLM": () => (/* binding */ CodeGenForCausalLM), /* harmony export */ "CodeGenModel": () => (/* binding */ CodeGenModel), /* harmony export */ "CodeGenPreTrainedModel": () => (/* binding */ CodeGenPreTrainedModel), /* harmony export */ "ConvBertForMaskedLM": () => (/* binding */ ConvBertForMaskedLM), /* harmony export */ "ConvBertForQuestionAnswering": () => (/* binding */ ConvBertForQuestionAnswering), /* harmony export */ "ConvBertForSequenceClassification": () => (/* binding */ ConvBertForSequenceClassification), /* harmony export */ "ConvBertForTokenClassification": () => (/* binding */ ConvBertForTokenClassification), /* harmony export */ "ConvBertModel": () => (/* binding */ ConvBertModel), /* harmony export */ "ConvBertPreTrainedModel": () => (/* binding */ ConvBertPreTrainedModel), /* harmony export */ "ConvNextForImageClassification": () => (/* binding */ ConvNextForImageClassification), /* harmony export */ "ConvNextModel": () => (/* binding */ ConvNextModel), /* harmony export */ "ConvNextPreTrainedModel": () => (/* binding */ ConvNextPreTrainedModel), /* harmony export */ "ConvNextV2ForImageClassification": () => (/* binding */ ConvNextV2ForImageClassification), /* harmony export */ "ConvNextV2Model": () => (/* binding */ ConvNextV2Model), /* harmony export */ "ConvNextV2PreTrainedModel": () => (/* binding */ ConvNextV2PreTrainedModel), /* harmony export */ "DPTForDepthEstimation": () => (/* binding */ DPTForDepthEstimation), /* harmony export */ "DPTModel": () => (/* binding */ DPTModel), /* harmony export */ "DPTPreTrainedModel": () => (/* binding */ DPTPreTrainedModel), /* harmony export */ "DebertaForMaskedLM": () => (/* binding */ DebertaForMaskedLM), /* harmony export */ "DebertaForQuestionAnswering": () => (/* binding */ DebertaForQuestionAnswering), /* harmony export */ "DebertaForSequenceClassification": () => (/* binding */ DebertaForSequenceClassification), /* harmony export */ "DebertaForTokenClassification": () => (/* binding */ DebertaForTokenClassification), /* harmony export */ "DebertaModel": () => (/* binding */ DebertaModel), /* harmony export */ "DebertaPreTrainedModel": () => (/* binding */ DebertaPreTrainedModel), /* harmony export */ "DebertaV2ForMaskedLM": () => (/* binding */ DebertaV2ForMaskedLM), /* harmony export */ "DebertaV2ForQuestionAnswering": () => (/* binding */ DebertaV2ForQuestionAnswering), /* harmony export */ "DebertaV2ForSequenceClassification": () => (/* binding */ DebertaV2ForSequenceClassification), /* harmony export */ "DebertaV2ForTokenClassification": () => (/* binding */ DebertaV2ForTokenClassification), /* harmony export */ "DebertaV2Model": () => (/* binding */ DebertaV2Model), /* harmony export */ "DebertaV2PreTrainedModel": () => (/* binding */ DebertaV2PreTrainedModel), /* harmony export */ "DeiTForImageClassification": () => (/* binding */ DeiTForImageClassification), /* harmony export */ "DeiTModel": () => (/* binding */ DeiTModel), /* harmony export */ "DeiTPreTrainedModel": () => (/* binding */ DeiTPreTrainedModel), /* harmony export */ "DepthAnythingForDepthEstimation": () => (/* binding */ DepthAnythingForDepthEstimation), /* harmony export */ "DepthAnythingPreTrainedModel": () => (/* binding */ DepthAnythingPreTrainedModel), /* harmony export */ "DetrForObjectDetection": () => (/* binding */ DetrForObjectDetection), /* harmony export */ "DetrForSegmentation": () => (/* binding */ DetrForSegmentation), /* harmony export */ "DetrModel": () => (/* binding */ DetrModel), /* harmony export */ "DetrObjectDetectionOutput": () => (/* binding */ DetrObjectDetectionOutput), /* harmony export */ "DetrPreTrainedModel": () => (/* binding */ DetrPreTrainedModel), /* harmony export */ "DetrSegmentationOutput": () => (/* binding */ DetrSegmentationOutput), /* harmony export */ "Dinov2ForImageClassification": () => (/* binding */ Dinov2ForImageClassification), /* harmony export */ "Dinov2Model": () => (/* binding */ Dinov2Model), /* harmony export */ "Dinov2PreTrainedModel": () => (/* binding */ Dinov2PreTrainedModel), /* harmony export */ "DistilBertForMaskedLM": () => (/* binding */ DistilBertForMaskedLM), /* harmony export */ "DistilBertForQuestionAnswering": () => (/* binding */ DistilBertForQuestionAnswering), /* harmony export */ "DistilBertForSequenceClassification": () => (/* binding */ DistilBertForSequenceClassification), /* harmony export */ "DistilBertForTokenClassification": () => (/* binding */ DistilBertForTokenClassification), /* harmony export */ "DistilBertModel": () => (/* binding */ DistilBertModel), /* harmony export */ "DistilBertPreTrainedModel": () => (/* binding */ DistilBertPreTrainedModel), /* harmony export */ "DonutSwinModel": () => (/* binding */ DonutSwinModel), /* harmony export */ "DonutSwinPreTrainedModel": () => (/* binding */ DonutSwinPreTrainedModel), /* harmony export */ "EfficientNetForImageClassification": () => (/* binding */ EfficientNetForImageClassification), /* harmony export */ "EfficientNetModel": () => (/* binding */ EfficientNetModel), /* harmony export */ "EfficientNetPreTrainedModel": () => (/* binding */ EfficientNetPreTrainedModel), /* harmony export */ "ElectraForMaskedLM": () => (/* binding */ ElectraForMaskedLM), /* harmony export */ "ElectraForQuestionAnswering": () => (/* binding */ ElectraForQuestionAnswering), /* harmony export */ "ElectraForSequenceClassification": () => (/* binding */ ElectraForSequenceClassification), /* harmony export */ "ElectraForTokenClassification": () => (/* binding */ ElectraForTokenClassification), /* harmony export */ "ElectraModel": () => (/* binding */ ElectraModel), /* harmony export */ "ElectraPreTrainedModel": () => (/* binding */ ElectraPreTrainedModel), /* harmony export */ "EsmForMaskedLM": () => (/* binding */ EsmForMaskedLM), /* harmony export */ "EsmForSequenceClassification": () => (/* binding */ EsmForSequenceClassification), /* harmony export */ "EsmForTokenClassification": () => (/* binding */ EsmForTokenClassification), /* harmony export */ "EsmModel": () => (/* binding */ EsmModel), /* harmony export */ "EsmPreTrainedModel": () => (/* binding */ EsmPreTrainedModel), /* harmony export */ "FalconForCausalLM": () => (/* binding */ FalconForCausalLM), /* harmony export */ "FalconModel": () => (/* binding */ FalconModel), /* harmony export */ "FalconPreTrainedModel": () => (/* binding */ FalconPreTrainedModel), /* harmony export */ "GLPNForDepthEstimation": () => (/* binding */ GLPNForDepthEstimation), /* harmony export */ "GLPNModel": () => (/* binding */ GLPNModel), /* harmony export */ "GLPNPreTrainedModel": () => (/* binding */ GLPNPreTrainedModel), /* harmony export */ "GPT2LMHeadModel": () => (/* binding */ GPT2LMHeadModel), /* harmony export */ "GPT2Model": () => (/* binding */ GPT2Model), /* harmony export */ "GPT2PreTrainedModel": () => (/* binding */ GPT2PreTrainedModel), /* harmony export */ "GPTBigCodeForCausalLM": () => (/* binding */ GPTBigCodeForCausalLM), /* harmony export */ "GPTBigCodeModel": () => (/* binding */ GPTBigCodeModel), /* harmony export */ "GPTBigCodePreTrainedModel": () => (/* binding */ GPTBigCodePreTrainedModel), /* harmony export */ "GPTJForCausalLM": () => (/* binding */ GPTJForCausalLM), /* harmony export */ "GPTJModel": () => (/* binding */ GPTJModel), /* harmony export */ "GPTJPreTrainedModel": () => (/* binding */ GPTJPreTrainedModel), /* harmony export */ "GPTNeoForCausalLM": () => (/* binding */ GPTNeoForCausalLM), /* harmony export */ "GPTNeoModel": () => (/* binding */ GPTNeoModel), /* harmony export */ "GPTNeoPreTrainedModel": () => (/* binding */ GPTNeoPreTrainedModel), /* harmony export */ "GPTNeoXForCausalLM": () => (/* binding */ GPTNeoXForCausalLM), /* harmony export */ "GPTNeoXModel": () => (/* binding */ GPTNeoXModel), /* harmony export */ "GPTNeoXPreTrainedModel": () => (/* binding */ GPTNeoXPreTrainedModel), /* harmony export */ "HubertForCTC": () => (/* binding */ HubertForCTC), /* harmony export */ "HubertForSequenceClassification": () => (/* binding */ HubertForSequenceClassification), /* harmony export */ "HubertModel": () => (/* binding */ HubertModel), /* harmony export */ "HubertPreTrainedModel": () => (/* binding */ HubertPreTrainedModel), /* harmony export */ "ImageMattingOutput": () => (/* binding */ ImageMattingOutput), /* harmony export */ "LlamaForCausalLM": () => (/* binding */ LlamaForCausalLM), /* harmony export */ "LlamaModel": () => (/* binding */ LlamaModel), /* harmony export */ "LlamaPreTrainedModel": () => (/* binding */ LlamaPreTrainedModel), /* harmony export */ "LlavaForConditionalGeneration": () => (/* binding */ LlavaForConditionalGeneration), /* harmony export */ "LlavaPreTrainedModel": () => (/* binding */ LlavaPreTrainedModel), /* harmony export */ "LongT5ForConditionalGeneration": () => (/* binding */ LongT5ForConditionalGeneration), /* harmony export */ "LongT5Model": () => (/* binding */ LongT5Model), /* harmony export */ "LongT5PreTrainedModel": () => (/* binding */ LongT5PreTrainedModel), /* harmony export */ "M2M100ForConditionalGeneration": () => (/* binding */ M2M100ForConditionalGeneration), /* harmony export */ "M2M100Model": () => (/* binding */ M2M100Model), /* harmony export */ "M2M100PreTrainedModel": () => (/* binding */ M2M100PreTrainedModel), /* harmony export */ "MBartForCausalLM": () => (/* binding */ MBartForCausalLM), /* harmony export */ "MBartForConditionalGeneration": () => (/* binding */ MBartForConditionalGeneration), /* harmony export */ "MBartForSequenceClassification": () => (/* binding */ MBartForSequenceClassification), /* harmony export */ "MBartModel": () => (/* binding */ MBartModel), /* harmony export */ "MBartPreTrainedModel": () => (/* binding */ MBartPreTrainedModel), /* harmony export */ "MPNetForMaskedLM": () => (/* binding */ MPNetForMaskedLM), /* harmony export */ "MPNetForQuestionAnswering": () => (/* binding */ MPNetForQuestionAnswering), /* harmony export */ "MPNetForSequenceClassification": () => (/* binding */ MPNetForSequenceClassification), /* harmony export */ "MPNetForTokenClassification": () => (/* binding */ MPNetForTokenClassification), /* harmony export */ "MPNetModel": () => (/* binding */ MPNetModel), /* harmony export */ "MPNetPreTrainedModel": () => (/* binding */ MPNetPreTrainedModel), /* harmony export */ "MT5ForConditionalGeneration": () => (/* binding */ MT5ForConditionalGeneration), /* harmony export */ "MT5Model": () => (/* binding */ MT5Model), /* harmony export */ "MT5PreTrainedModel": () => (/* binding */ MT5PreTrainedModel), /* harmony export */ "MarianMTModel": () => (/* binding */ MarianMTModel), /* harmony export */ "MarianModel": () => (/* binding */ MarianModel), /* harmony export */ "MarianPreTrainedModel": () => (/* binding */ MarianPreTrainedModel), /* harmony export */ "MaskedLMOutput": () => (/* binding */ MaskedLMOutput), /* harmony export */ "MistralForCausalLM": () => (/* binding */ MistralForCausalLM), /* harmony export */ "MistralModel": () => (/* binding */ MistralModel), /* harmony export */ "MistralPreTrainedModel": () => (/* binding */ MistralPreTrainedModel), /* harmony export */ "MobileBertForMaskedLM": () => (/* binding */ MobileBertForMaskedLM), /* harmony export */ "MobileBertForQuestionAnswering": () => (/* binding */ MobileBertForQuestionAnswering), /* harmony export */ "MobileBertForSequenceClassification": () => (/* binding */ MobileBertForSequenceClassification), /* harmony export */ "MobileBertModel": () => (/* binding */ MobileBertModel), /* harmony export */ "MobileBertPreTrainedModel": () => (/* binding */ MobileBertPreTrainedModel), /* harmony export */ "MobileViTForImageClassification": () => (/* binding */ MobileViTForImageClassification), /* harmony export */ "MobileViTModel": () => (/* binding */ MobileViTModel), /* harmony export */ "MobileViTPreTrainedModel": () => (/* binding */ MobileViTPreTrainedModel), /* harmony export */ "ModelOutput": () => (/* binding */ ModelOutput), /* harmony export */ "MptForCausalLM": () => (/* binding */ MptForCausalLM), /* harmony export */ "MptModel": () => (/* binding */ MptModel), /* harmony export */ "MptPreTrainedModel": () => (/* binding */ MptPreTrainedModel), /* harmony export */ "MusicgenForCausalLM": () => (/* binding */ MusicgenForCausalLM), /* harmony export */ "MusicgenForConditionalGeneration": () => (/* binding */ MusicgenForConditionalGeneration), /* harmony export */ "MusicgenModel": () => (/* binding */ MusicgenModel), /* harmony export */ "MusicgenPreTrainedModel": () => (/* binding */ MusicgenPreTrainedModel), /* harmony export */ "NomicBertModel": () => (/* binding */ NomicBertModel), /* harmony export */ "NomicBertPreTrainedModel": () => (/* binding */ NomicBertPreTrainedModel), /* harmony export */ "OPTForCausalLM": () => (/* binding */ OPTForCausalLM), /* harmony export */ "OPTModel": () => (/* binding */ OPTModel), /* harmony export */ "OPTPreTrainedModel": () => (/* binding */ OPTPreTrainedModel), /* harmony export */ "OwlViTForObjectDetection": () => (/* binding */ OwlViTForObjectDetection), /* harmony export */ "OwlViTModel": () => (/* binding */ OwlViTModel), /* harmony export */ "OwlViTPreTrainedModel": () => (/* binding */ OwlViTPreTrainedModel), /* harmony export */ "Owlv2ForObjectDetection": () => (/* binding */ Owlv2ForObjectDetection), /* harmony export */ "Owlv2Model": () => (/* binding */ Owlv2Model), /* harmony export */ "Owlv2PreTrainedModel": () => (/* binding */ Owlv2PreTrainedModel), /* harmony export */ "PhiForCausalLM": () => (/* binding */ PhiForCausalLM), /* harmony export */ "PhiModel": () => (/* binding */ PhiModel), /* harmony export */ "PhiPreTrainedModel": () => (/* binding */ PhiPreTrainedModel), /* harmony export */ "PreTrainedModel": () => (/* binding */ PreTrainedModel), /* harmony export */ "PretrainedMixin": () => (/* binding */ PretrainedMixin), /* harmony export */ "QuestionAnsweringModelOutput": () => (/* binding */ QuestionAnsweringModelOutput), /* harmony export */ "Qwen2ForCausalLM": () => (/* binding */ Qwen2ForCausalLM), /* harmony export */ "Qwen2Model": () => (/* binding */ Qwen2Model), /* harmony export */ "Qwen2PreTrainedModel": () => (/* binding */ Qwen2PreTrainedModel), /* harmony export */ "ResNetForImageClassification": () => (/* binding */ ResNetForImageClassification), /* harmony export */ "ResNetModel": () => (/* binding */ ResNetModel), /* harmony export */ "ResNetPreTrainedModel": () => (/* binding */ ResNetPreTrainedModel), /* harmony export */ "RoFormerForMaskedLM": () => (/* binding */ RoFormerForMaskedLM), /* harmony export */ "RoFormerForQuestionAnswering": () => (/* binding */ RoFormerForQuestionAnswering), /* harmony export */ "RoFormerForSequenceClassification": () => (/* binding */ RoFormerForSequenceClassification), /* harmony export */ "RoFormerForTokenClassification": () => (/* binding */ RoFormerForTokenClassification), /* harmony export */ "RoFormerModel": () => (/* binding */ RoFormerModel), /* harmony export */ "RoFormerPreTrainedModel": () => (/* binding */ RoFormerPreTrainedModel), /* harmony export */ "RobertaForMaskedLM": () => (/* binding */ RobertaForMaskedLM), /* harmony export */ "RobertaForQuestionAnswering": () => (/* binding */ RobertaForQuestionAnswering), /* harmony export */ "RobertaForSequenceClassification": () => (/* binding */ RobertaForSequenceClassification), /* harmony export */ "RobertaForTokenClassification": () => (/* binding */ RobertaForTokenClassification), /* harmony export */ "RobertaModel": () => (/* binding */ RobertaModel), /* harmony export */ "RobertaPreTrainedModel": () => (/* binding */ RobertaPreTrainedModel), /* harmony export */ "SamImageSegmentationOutput": () => (/* binding */ SamImageSegmentationOutput), /* harmony export */ "SamModel": () => (/* binding */ SamModel), /* harmony export */ "SamPreTrainedModel": () => (/* binding */ SamPreTrainedModel), /* harmony export */ "SegformerForImageClassification": () => (/* binding */ SegformerForImageClassification), /* harmony export */ "SegformerForSemanticSegmentation": () => (/* binding */ SegformerForSemanticSegmentation), /* harmony export */ "SegformerModel": () => (/* binding */ SegformerModel), /* harmony export */ "SegformerPreTrainedModel": () => (/* binding */ SegformerPreTrainedModel), /* harmony export */ "Seq2SeqLMOutput": () => (/* binding */ Seq2SeqLMOutput), /* harmony export */ "SequenceClassifierOutput": () => (/* binding */ SequenceClassifierOutput), /* harmony export */ "SiglipModel": () => (/* binding */ SiglipModel), /* harmony export */ "SiglipPreTrainedModel": () => (/* binding */ SiglipPreTrainedModel), /* harmony export */ "SiglipTextModel": () => (/* binding */ SiglipTextModel), /* harmony export */ "SiglipVisionModel": () => (/* binding */ SiglipVisionModel), /* harmony export */ "SpeechT5ForSpeechToText": () => (/* binding */ SpeechT5ForSpeechToText), /* harmony export */ "SpeechT5ForTextToSpeech": () => (/* binding */ SpeechT5ForTextToSpeech), /* harmony export */ "SpeechT5HifiGan": () => (/* binding */ SpeechT5HifiGan), /* harmony export */ "SpeechT5Model": () => (/* binding */ SpeechT5Model), /* harmony export */ "SpeechT5PreTrainedModel": () => (/* binding */ SpeechT5PreTrainedModel), /* harmony export */ "SqueezeBertForMaskedLM": () => (/* binding */ SqueezeBertForMaskedLM), /* harmony export */ "SqueezeBertForQuestionAnswering": () => (/* binding */ SqueezeBertForQuestionAnswering), /* harmony export */ "SqueezeBertForSequenceClassification": () => (/* binding */ SqueezeBertForSequenceClassification), /* harmony export */ "SqueezeBertModel": () => (/* binding */ SqueezeBertModel), /* harmony export */ "SqueezeBertPreTrainedModel": () => (/* binding */ SqueezeBertPreTrainedModel), /* harmony export */ "StableLmForCausalLM": () => (/* binding */ StableLmForCausalLM), /* harmony export */ "StableLmModel": () => (/* binding */ StableLmModel), /* harmony export */ "StableLmPreTrainedModel": () => (/* binding */ StableLmPreTrainedModel), /* harmony export */ "Starcoder2ForCausalLM": () => (/* binding */ Starcoder2ForCausalLM), /* harmony export */ "Starcoder2Model": () => (/* binding */ Starcoder2Model), /* harmony export */ "Starcoder2PreTrainedModel": () => (/* binding */ Starcoder2PreTrainedModel), /* harmony export */ "Swin2SRForImageSuperResolution": () => (/* binding */ Swin2SRForImageSuperResolution), /* harmony export */ "Swin2SRModel": () => (/* binding */ Swin2SRModel), /* harmony export */ "Swin2SRPreTrainedModel": () => (/* binding */ Swin2SRPreTrainedModel), /* harmony export */ "SwinForImageClassification": () => (/* binding */ SwinForImageClassification), /* harmony export */ "SwinModel": () => (/* binding */ SwinModel), /* harmony export */ "SwinPreTrainedModel": () => (/* binding */ SwinPreTrainedModel), /* harmony export */ "T5ForConditionalGeneration": () => (/* binding */ T5ForConditionalGeneration), /* harmony export */ "T5Model": () => (/* binding */ T5Model), /* harmony export */ "T5PreTrainedModel": () => (/* binding */ T5PreTrainedModel), /* harmony export */ "TableTransformerForObjectDetection": () => (/* binding */ TableTransformerForObjectDetection), /* harmony export */ "TableTransformerModel": () => (/* binding */ TableTransformerModel), /* harmony export */ "TableTransformerObjectDetectionOutput": () => (/* binding */ TableTransformerObjectDetectionOutput), /* harmony export */ "TableTransformerPreTrainedModel": () => (/* binding */ TableTransformerPreTrainedModel), /* harmony export */ "TokenClassifierOutput": () => (/* binding */ TokenClassifierOutput), /* harmony export */ "TrOCRForCausalLM": () => (/* binding */ TrOCRForCausalLM), /* harmony export */ "TrOCRPreTrainedModel": () => (/* binding */ TrOCRPreTrainedModel), /* harmony export */ "UniSpeechForCTC": () => (/* binding */ UniSpeechForCTC), /* harmony export */ "UniSpeechForSequenceClassification": () => (/* binding */ UniSpeechForSequenceClassification), /* harmony export */ "UniSpeechModel": () => (/* binding */ UniSpeechModel), /* harmony export */ "UniSpeechPreTrainedModel": () => (/* binding */ UniSpeechPreTrainedModel), /* harmony export */ "UniSpeechSatForAudioFrameClassification": () => (/* binding */ UniSpeechSatForAudioFrameClassification), /* harmony export */ "UniSpeechSatForCTC": () => (/* binding */ UniSpeechSatForCTC), /* harmony export */ "UniSpeechSatForSequenceClassification": () => (/* binding */ UniSpeechSatForSequenceClassification), /* harmony export */ "UniSpeechSatModel": () => (/* binding */ UniSpeechSatModel), /* harmony export */ "UniSpeechSatPreTrainedModel": () => (/* binding */ UniSpeechSatPreTrainedModel), /* harmony export */ "ViTForImageClassification": () => (/* binding */ ViTForImageClassification), /* harmony export */ "ViTModel": () => (/* binding */ ViTModel), /* harmony export */ "ViTPreTrainedModel": () => (/* binding */ ViTPreTrainedModel), /* harmony export */ "VisionEncoderDecoderModel": () => (/* binding */ VisionEncoderDecoderModel), /* harmony export */ "VitMatteForImageMatting": () => (/* binding */ VitMatteForImageMatting), /* harmony export */ "VitMattePreTrainedModel": () => (/* binding */ VitMattePreTrainedModel), /* harmony export */ "VitsModel": () => (/* binding */ VitsModel), /* harmony export */ "VitsModelOutput": () => (/* binding */ VitsModelOutput), /* harmony export */ "VitsPreTrainedModel": () => (/* binding */ VitsPreTrainedModel), /* harmony export */ "Wav2Vec2BertForCTC": () => (/* binding */ Wav2Vec2BertForCTC), /* harmony export */ "Wav2Vec2BertForSequenceClassification": () => (/* binding */ Wav2Vec2BertForSequenceClassification), /* harmony export */ "Wav2Vec2BertModel": () => (/* binding */ Wav2Vec2BertModel), /* harmony export */ "Wav2Vec2BertPreTrainedModel": () => (/* binding */ Wav2Vec2BertPreTrainedModel), /* harmony export */ "Wav2Vec2ForAudioFrameClassification": () => (/* binding */ Wav2Vec2ForAudioFrameClassification), /* harmony export */ "Wav2Vec2ForCTC": () => (/* binding */ Wav2Vec2ForCTC), /* harmony export */ "Wav2Vec2ForSequenceClassification": () => (/* binding */ Wav2Vec2ForSequenceClassification), /* harmony export */ "Wav2Vec2Model": () => (/* binding */ Wav2Vec2Model), /* harmony export */ "Wav2Vec2PreTrainedModel": () => (/* binding */ Wav2Vec2PreTrainedModel), /* harmony export */ "WavLMForAudioFrameClassification": () => (/* binding */ WavLMForAudioFrameClassification), /* harmony export */ "WavLMForCTC": () => (/* binding */ WavLMForCTC), /* harmony export */ "WavLMForSequenceClassification": () => (/* binding */ WavLMForSequenceClassification), /* harmony export */ "WavLMForXVector": () => (/* binding */ WavLMForXVector), /* harmony export */ "WavLMModel": () => (/* binding */ WavLMModel), /* harmony export */ "WavLMPreTrainedModel": () => (/* binding */ WavLMPreTrainedModel), /* harmony export */ "WhisperForConditionalGeneration": () => (/* binding */ WhisperForConditionalGeneration), /* harmony export */ "WhisperModel": () => (/* binding */ WhisperModel), /* harmony export */ "WhisperPreTrainedModel": () => (/* binding */ WhisperPreTrainedModel), /* harmony export */ "XLMForQuestionAnswering": () => (/* binding */ XLMForQuestionAnswering), /* harmony export */ "XLMForSequenceClassification": () => (/* binding */ XLMForSequenceClassification), /* harmony export */ "XLMForTokenClassification": () => (/* binding */ XLMForTokenClassification), /* harmony export */ "XLMModel": () => (/* binding */ XLMModel), /* harmony export */ "XLMPreTrainedModel": () => (/* binding */ XLMPreTrainedModel), /* harmony export */ "XLMRobertaForMaskedLM": () => (/* binding */ XLMRobertaForMaskedLM), /* harmony export */ "XLMRobertaForQuestionAnswering": () => (/* binding */ XLMRobertaForQuestionAnswering), /* harmony export */ "XLMRobertaForSequenceClassification": () => (/* binding */ XLMRobertaForSequenceClassification), /* harmony export */ "XLMRobertaForTokenClassification": () => (/* binding */ XLMRobertaForTokenClassification), /* harmony export */ "XLMRobertaModel": () => (/* binding */ XLMRobertaModel), /* harmony export */ "XLMRobertaPreTrainedModel": () => (/* binding */ XLMRobertaPreTrainedModel), /* harmony export */ "XLMWithLMHeadModel": () => (/* binding */ XLMWithLMHeadModel), /* harmony export */ "XVectorOutput": () => (/* binding */ XVectorOutput), /* harmony export */ "YolosForObjectDetection": () => (/* binding */ YolosForObjectDetection), /* harmony export */ "YolosModel": () => (/* binding */ YolosModel), /* harmony export */ "YolosObjectDetectionOutput": () => (/* binding */ YolosObjectDetectionOutput), /* harmony export */ "YolosPreTrainedModel": () => (/* binding */ YolosPreTrainedModel) /* harmony export */ }); /* harmony import */ var _configs_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./configs.js */ "./src/configs.js"); /* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./backends/onnx.js */ "./src/backends/onnx.js"); /* harmony import */ var _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/dtypes.js */ "./src/utils/dtypes.js"); /* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); /* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); /* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); /* harmony import */ var _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./generation/logits_process.js */ "./src/generation/logits_process.js"); /* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); /* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); /* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); /* harmony import */ var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./generation/stopping_criteria.js */ "./src/generation/stopping_criteria.js"); /* harmony import */ var _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./generation/logits_sampler.js */ "./src/generation/logits_sampler.js"); /** * @file Definitions of all models available in Transformers.js. * * **Example:** Load and run an `AutoModel`. * * ```javascript * import { AutoModel, AutoTokenizer } from '@xenova/transformers'; * * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); * let model = await AutoModel.from_pretrained('Xenova/bert-base-uncased'); * * let inputs = await tokenizer('I love transformers!'); * let { logits } = await model(inputs); * // Tensor { * // data: Float32Array(183132) [-7.117443084716797, -7.107812881469727, -7.092104911804199, ...] * // dims: (3) [1, 6, 30522], * // type: "float32", * // size: 183132, * // } * ``` * * We also provide other `AutoModel`s (listed below), which you can use in the same way as the Python library. For example: * * **Example:** Load and run an `AutoModelForSeq2SeqLM`. * ```javascript * import { AutoModelForSeq2SeqLM, AutoTokenizer } from '@xenova/transformers'; * * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/t5-small'); * let model = await AutoModelForSeq2SeqLM.from_pretrained('Xenova/t5-small'); * * let { input_ids } = await tokenizer('translate English to German: I love transformers!'); * let outputs = await model.generate(input_ids); * let decoded = tokenizer.decode(outputs[0], { skip_special_tokens: true }); * // 'Ich liebe Transformatoren!' * ``` * * @module models */ ////////////////////////////////////////////////// // Model types: used internally const MODEL_TYPES = { EncoderOnly: 0, EncoderDecoder: 1, Seq2Seq: 2, Vision2Seq: 3, DecoderOnly: 4, MaskGeneration: 5, ImageTextToText: 6, Musicgen: 7, } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Helper functions // NOTE: These will be populated fully later const MODEL_TYPE_MAPPING = new Map(); const MODEL_NAME_TO_CLASS_MAPPING = new Map(); const MODEL_CLASS_TO_NAME_MAPPING = new Map(); /** * Constructs an InferenceSession using a model file located at the specified path. * @param {string} pretrained_model_name_or_path The path to the directory containing the model file. * @param {string} fileName The name of the model file. * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. * @returns {Promise<{buffer: Uint8Array, session_options: Object}>} A Promise that resolves to the data needed to create an InferenceSession object. * @private */ async function getSession(pretrained_model_name_or_path, fileName, options) { let device = options.device; if (device && typeof device !== 'string') { if (device.hasOwnProperty(fileName)) { device = device[fileName]; } else { console.warn(`Device not specified for ${fileName}. Using the default device.`); device = null; } } // If the device is not specified, we use the default (supported) execution providers. const executionProviders = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.deviceToExecutionProviders)( /** @type {import("./utils/devices.js").DeviceType|null} */(device) ); // If options.dtype is specified, we use it to choose the suffix for the model file. // Otherwise, we use the default dtype for the device. let dtype = options.dtype; if (typeof dtype !== 'string') { if (dtype && dtype.hasOwnProperty(fileName)) { dtype = dtype[fileName]; } else { dtype = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DEVICE_DTYPE_MAPPING[executionProviders[0]]; console.warn(`Dtype not specified for ${fileName}. Using the default dtype: ${dtype}.`); } } if (!_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING.hasOwnProperty(dtype)) { throw new Error(`Invalid dtype: ${dtype}. Should be one of: ${Object.keys(_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES).join(', ')}`); } else if (dtype === _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp16 && !(await (0,_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.isFp16Supported)())) { throw new Error(`The device does not support fp16.`); } // Construct the model file name const suffix = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING[dtype]; const modelFileName = `${options.subfolder ?? ''}/${fileName}${suffix}.onnx`; const buffer = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, modelFileName, true, options); const session_options = { ...options.session_options } ?? {}; // Overwrite `executionProviders` if not specified session_options.executionProviders ??= executionProviders; // handle onnx external data files if (session_options.externalData !== undefined) { for (let i = 0; i < session_options.externalData.length; ++i) { const ext = session_options.externalData[i]; // if the external data is a string, fetch the file and replace the string with its content if (typeof ext.data === "string") { const ext_buffer = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, ext.data, true, options); ext.data = ext_buffer; } } } // TODO: Add support for preferredOutputLocation // if (options.device == "webgpu") { // for (let i = 0; i < config.layers; ++i) { // options.session_options.preferredOutputLocation[`present.${i}.key`] = 'gpu-buffer'; // options.session_options.preferredOutputLocation[`present.${i}.value`] = 'gpu-buffer'; // } // } return { buffer, session_options }; } /** * Helper function to sequentially create multiple InferenceSession objects. * NOTE: It is important to create the sessions sequentially, otherwise ORT will throw an error indicating * that multiple calls to `initWasm` were made. * * @param {string} pretrained_model_name_or_path The path to the directory containing the model file. * @param {Record} names The names of the model files to load. * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. * @returns {Promise>} A Promise that resolves to a dictionary of InferenceSession objects. * @private */ async function constructSessions(pretrained_model_name_or_path, names, options) { const keys = Object.keys(names); const sessionData = await Promise.all( keys.map(async (name) => getSession(pretrained_model_name_or_path, names[name], options)) ); const sessions = {}; for (let i = 0; i < keys.length; ++i) { const { buffer, session_options } = sessionData[i]; const session = await (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.createInferenceSession)(buffer, session_options); sessions[keys[i]] = session; } return sessions; } /** * Validate model inputs * @param {Object} session The InferenceSession object that will be run. * @param {Object} inputs The inputs to check. * @returns {Record} The checked inputs. * @throws {Error} If any inputs are missing. * @private */ function validateInputs(session, inputs) { /** * NOTE: Create either a shallow or deep copy based on `onnx.wasm.proxy` * @type {Record} */ const checkedInputs = Object.create(null); const missingInputs = []; for (const inputName of session.inputNames) { const tensor = inputs[inputName]; // Rare case where one of the model's input names corresponds to a built-in // object name (e.g., toString), which would cause a simple (!tensor) check to fail, // because it's not undefined but a function. if (!(tensor instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor)) { missingInputs.push(inputName); continue; } // NOTE: When `env.wasm.proxy is true` the tensor is moved across the Worker // boundary, transferring ownership to the worker and invalidating the tensor. // So, in this case, we simply sacrifice a clone for it. checkedInputs[inputName] = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)() ? tensor.clone() : tensor; } if (missingInputs.length > 0) { throw new Error( `An error occurred during model execution: "Missing the following inputs: ${missingInputs.join(', ')}.`); } const numInputsProvided = Object.keys(inputs).length; const numInputsNeeded = session.inputNames.length; if (numInputsProvided > numInputsNeeded) { // No missing inputs, but too many inputs were provided. // Warn the user and ignore the extra inputs. let ignored = Object.keys(inputs).filter(inputName => !session.inputNames.includes(inputName)); console.warn(`WARNING: Too many inputs were provided (${numInputsProvided} > ${numInputsNeeded}). The following inputs will be ignored: "${ignored.join(', ')}".`); } return checkedInputs; } /** * Executes an InferenceSession using the specified inputs. * NOTE: `inputs` must contain at least the input names of the model. * - If additional inputs are passed, they will be ignored. * - If inputs are missing, an error will be thrown. * * @param {Object} session The InferenceSession object to run. * @param {Object} inputs An object that maps input names to input tensors. * @returns {Promise} A Promise that resolves to an object that maps output names to output tensors. * @private */ async function sessionRun(session, inputs) { const checkedInputs = validateInputs(session, inputs); try { // 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. console.error(`An error occurred during model execution: "${e}".`); console.error('Inputs given to model:', checkedInputs); throw e; } } /** * Replaces ONNX Tensor objects with custom Tensor objects to support additional functions. * @param {Object} obj The object to replace tensor objects in. * @returns {Object} The object with tensor objects replaced by custom Tensor objects. * @private */ function replaceTensors(obj) { for (let prop in obj) { if ((0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(obj[prop])) { obj[prop] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(obj[prop]); } else if (typeof obj[prop] === 'object') { replaceTensors(obj[prop]); } } return obj; } /** * Converts an array or Tensor of integers to an int64 Tensor. * @param {Array|Tensor} items The input integers to be converted. * @returns {Tensor} The int64 Tensor with the converted values. * @throws {Error} If the input array is empty or the input is a batched Tensor and not all sequences have the same length. * @private */ function toI64Tensor(items) { if (items instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor) { return items; } // items is an array if (items.length === 0) { throw Error("items must be non-empty"); } if (Array.isArray(items[0])) { // batched if (items.some(x => x.length !== items[0].length)) { throw Error("Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' and/or 'truncation=True' to have batched tensors with the same length.") } return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor('int64', BigInt64Array.from(items.flat().map(x => BigInt(x))), [items.length, items[0].length] ); } else { //flat return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor('int64', BigInt64Array.from(items.map(x => BigInt(x))), [1, items.length] ); } } /** * Prepares an attention mask for a sequence of tokens based on configuration options. * @param {Object} self The calling object instance. * @param {Tensor} tokens The input tokens. * @returns {Tensor} The attention mask tensor. * @private */ function prepareAttentionMask(self, tokens) { // Prepare attention mask let pad_token_id = self.config.pad_token_id ?? null; let eos_token_id = self.config.eos_token_id ?? null; if ((0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.isIntegralNumber)(eos_token_id)) { eos_token_id = [eos_token_id]; } let is_pad_token_in_inputs = tokens.indexOf(pad_token_id) !== -1; let is_pad_token_not_equal_to_eos_token_id = (eos_token_id === null) || !eos_token_id.includes(pad_token_id) if (is_pad_token_in_inputs && is_pad_token_not_equal_to_eos_token_id) { let data = BigInt64Array.from( // Note: != so that int matches bigint // @ts-ignore tokens.data.map(x => x != pad_token_id) ) return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor('int64', data, tokens.dims) } else { return (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones_like)(tokens); } } /** * Creates a boolean tensor with a single value. * @param {boolean} value The value of the tensor. * @returns {Tensor} The boolean tensor. * @private */ function boolTensor(value) { return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor('bool', [value], [1]); } // JS doesn't support mixins, so we define some reused functions here, and allow "this" to be passed in /** * Perform forward pass on the seq2seq model (both encoder and decoder). * @param {Object} self The seq2seq model object. * @param {Object} model_inputs The input object for the model containing encoder and decoder inputs. * @returns {Promise} Promise that resolves with the output of the seq2seq model. * @private */ async function seq2seqForward(self, model_inputs) { let { encoder_outputs, past_key_values } = model_inputs; // Encode if needed if (!encoder_outputs) { const encoder_inputs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, self.sessions['model'].inputNames); // Encoder outputs are not given, so we must compute them. encoder_outputs = (await encoderForward(self, encoder_inputs)).last_hidden_state; } const { input_ids, decoder_input_ids, ...other_decoder_inputs } = model_inputs; other_decoder_inputs.input_ids = decoder_input_ids; other_decoder_inputs.encoder_hidden_states = encoder_outputs; if (self.sessions['decoder_model_merged'].inputNames.includes('encoder_attention_mask')) { other_decoder_inputs.encoder_attention_mask = model_inputs.attention_mask } const decoderResults = await decoderForward(self, other_decoder_inputs, true); // Get cross attention and/or decoder attentions if they are present // const attns = self.getAttentions(decoderResults); return decoderResults; } /** * Forward pass of an encoder model. * @param {Object} self The encoder model. * @param {Object} model_inputs The input data to be used for the forward pass. * @returns {Promise} Promise that resolves with an object containing the model's outputs. * @private */ async function encoderForward(self, model_inputs) { const session = self.sessions['model']; const encoderFeeds = Object.create(null); for (const key of session.inputNames) { encoderFeeds[key] = model_inputs[key]; } if (session.inputNames.includes('token_type_ids') && !encoderFeeds.token_type_ids) { // Assign default `token_type_ids` (all zeroes) to the `encoderFeeds` if the model expects it, // but they weren't created by the tokenizer. encoderFeeds.token_type_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( 'int64', new BigInt64Array(encoderFeeds.input_ids.data.length), encoderFeeds.input_ids.dims ) } return await sessionRun(session, encoderFeeds); } /** * Forward pass of a decoder model. * @param {Object} self The decoder model. * @param {Object} model_inputs The input data to be used for the forward pass. * @returns {Promise} Promise that resolves with an object containing the logits and past key values. * @private */ async function decoderForward(self, model_inputs, is_encoder_decoder = false) { const session = self.sessions[ is_encoder_decoder ? 'decoder_model_merged' : 'model' ] const { past_key_values, ...new_model_inputs } = model_inputs; if (session.inputNames.includes('use_cache_branch')) { new_model_inputs.use_cache_branch = boolTensor(!!past_key_values); } // Unpack the `past_key_values` object into model inputs self.addPastKeyValues(new_model_inputs, past_key_values); const fixed = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(new_model_inputs, session.inputNames); return await sessionRun(session, fixed); } function decoder_prepare_inputs_for_generation(self, input_ids, model_inputs, generation_config) { const session = self.sessions['model']; if (session.inputNames.includes('position_ids') && model_inputs.attention_mask && !model_inputs.position_ids) { // If the model supports providing position_ids, we create position_ids on the fly for batch generation, // by computing the cumulative sum of the attention mask along the sequence length dimension. // // Equivalent to: // position_ids = attention_mask.long().cumsum(-1) - 1 // position_ids.masked_fill_(attention_mask == 0, 1) // if past_key_values: // position_ids = position_ids[:, -input_ids.shape[1] :] const [bz, seq_len] = model_inputs.attention_mask.dims; const data = new BigInt64Array(model_inputs.attention_mask.data.length); for (let i = 0; i < bz; ++i) { const start = i * seq_len; let sum = BigInt(0); for (let j = 0; j < seq_len; ++j) { const index = start + j; if (model_inputs.attention_mask.data[index] === 0n) { data[index] = BigInt(1); } else { // === 1n data[index] = sum; sum += model_inputs.attention_mask.data[index]; } } } model_inputs.position_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor('int64', data, model_inputs.attention_mask.dims); if (model_inputs.past_key_values) { model_inputs.position_ids = model_inputs.position_ids.slice(null, -1).unsqueeze_(-1); } } return model_inputs; } function encoder_decoder_prepare_inputs_for_generation(self, input_ids, model_inputs, generation_config) { // console.log('model_inputs', model_inputs) const { ...new_model_inputs } = model_inputs; const past_key_values = model_inputs.past_key_values; // self.addPastKeyValues(new_model_inputs, past_key_values); if (past_key_values) { // keep only final IDs: input_ids = input_ids.map(x => [x.at(-1)]); } else { // input_ids; } new_model_inputs['decoder_input_ids'] = toI64Tensor(input_ids); return new_model_inputs; } ////////////////////////////////////////////////// ////////////////////////////////////////////////// /** * A base class for pre-trained models that provides the model configuration and an ONNX session. */ class PreTrainedModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { main_input_name = 'input_ids'; forward_params = ['input_ids', 'attention_mask']; /** * Creates a new instance of the `PreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. */ constructor(config, sessions) { super(); this.config = config; this.sessions = sessions; const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); const modelType = MODEL_TYPE_MAPPING.get(modelName); this.can_generate = false; this._forward = null; this._prepare_inputs_for_generation = null; if (modelType === MODEL_TYPES.DecoderOnly) { this.can_generate = true; this._forward = decoderForward; this._prepare_inputs_for_generation = decoder_prepare_inputs_for_generation; } else if (modelType === MODEL_TYPES.Seq2Seq || modelType === MODEL_TYPES.Vision2Seq || modelType === MODEL_TYPES.Musicgen) { this.can_generate = true; this._forward = seq2seqForward; this._prepare_inputs_for_generation = encoder_decoder_prepare_inputs_for_generation; } else if (modelType === MODEL_TYPES.EncoderDecoder) { // console.warn('TODO: Implement EncoderDecoderForward') this._forward = seq2seqForward; } else if (modelType === MODEL_TYPES.ImageTextToText) { this.can_generate = true; console.warn('TODO: Implement visionDecoderForward'); // this._forward = visionDecoderForward; } else { // should be MODEL_TYPES.EncoderOnly this._forward = encoderForward; } } /** * Disposes of all the ONNX sessions that were created during inference. * @returns {Promise} An array of promises, one for each ONNX session that is being disposed. * @todo Use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry */ async dispose() { const promises = []; for (let key of Object.keys(this)) { let item = this[key]; // TODO improve check for ONNX session if (item?.handler?.dispose !== undefined) { promises.push(item.handler.dispose()) } } return await Promise.all(promises); } /** * Instantiate one of the model classes of the library from a pretrained model. * * The model class to instantiate is selected based on the `model_type` property of the config object * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) * * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: * - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co. * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a * user or organization name, like `dbmdz/bert-base-german-cased`. * - A path to a *directory* containing model weights, e.g., `./my_model_directory/`. * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. * * @returns {Promise} A new instance of the `PreTrainedModel` class. */ static async from_pretrained(pretrained_model_name_or_path, { progress_callback = null, config = null, cache_dir = null, local_files_only = false, revision = 'main', model_file_name = null, subfolder = 'onnx', device = null, dtype = null, session_options = {}, } = {}) { let options = { progress_callback, config, cache_dir, local_files_only, revision, model_file_name, subfolder, device, dtype, session_options, } const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this); const modelType = MODEL_TYPE_MAPPING.get(modelName); let info; if (modelType === MODEL_TYPES.DecoderOnly) { info = await Promise.all([ _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options), constructSessions(pretrained_model_name_or_path, { model: options.model_file_name ?? 'model', }, options), (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelJSON)(pretrained_model_name_or_path, 'generation_config.json', false, options), ]); } else if (modelType === MODEL_TYPES.Seq2Seq || modelType === MODEL_TYPES.Vision2Seq) { info = await Promise.all([ _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options), constructSessions(pretrained_model_name_or_path, { model: 'encoder_model', decoder_model_merged: 'decoder_model_merged', }, options), (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelJSON)(pretrained_model_name_or_path, 'generation_config.json', false, options), ]); } else if (modelType === MODEL_TYPES.MaskGeneration) { info = await Promise.all([ _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options), constructSessions(pretrained_model_name_or_path, { model: 'vision_encoder', prompt_encoder_mask_decoder: 'prompt_encoder_mask_decoder', }, options), ]); } else if (modelType === MODEL_TYPES.EncoderDecoder) { info = await Promise.all([ _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options), constructSessions(pretrained_model_name_or_path, { model: 'encoder_model', decoder_model_merged: 'decoder_model_merged', }, options), ]); } else if (modelType === MODEL_TYPES.ImageTextToText) { info = await Promise.all([ _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options), constructSessions(pretrained_model_name_or_path, { embed_tokens: 'embed_tokens', vision_encoder: 'vision_encoder', decoder_model_merged: 'decoder_model_merged', }, options), (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelJSON)(pretrained_model_name_or_path, 'generation_config.json', false, options), ]); } else if (modelType === MODEL_TYPES.Musicgen) { info = await Promise.all([ _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options), constructSessions(pretrained_model_name_or_path, { model: 'text_encoder', decoder_model_merged: 'decoder_model_merged', encodec_decode: 'encodec_decode', }, options), (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelJSON)(pretrained_model_name_or_path, 'generation_config.json', false, options), ]); } else { // should be MODEL_TYPES.EncoderOnly if (modelType !== MODEL_TYPES.EncoderOnly) { console.warn(`Model type for '${modelName ?? config?.model_type}' not found, assuming encoder-only architecture. Please report this at https://github.com/xenova/transformers.js/issues/new/choose.`) } info = await Promise.all([ _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options), constructSessions(pretrained_model_name_or_path, { model: options.model_file_name ?? 'model', }, options), ]); } // @ts-ignore return new this(...info); } /** * Runs the model with the provided inputs * @param {Object} model_inputs Object containing input tensors * @returns {Promise} Object containing output tensors */ async _call(model_inputs) { return await this.forward(model_inputs); } /** * Forward method for a pretrained model. If not overridden by a subclass, the correct forward method * will be chosen based on the model type. * @param {Object} model_inputs The input data to the model in the format specified in the ONNX model. * @returns {Promise} The output data from the model in the format specified in the ONNX model. * @throws {Error} This method must be implemented in subclasses. */ async forward(model_inputs) { return await this._forward(this, model_inputs); } /** * This function returns a [`LogitsProcessorList`] list object that contains all relevant [`LogitsWarper`] * instances used for multinomial sampling. * @param {GenerationConfig} generation_config The generation config. * @returns {LogitsProcessorList} generation_config */ _get_logits_warper(generation_config) { // instantiate warpers list const warpers = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.LogitsProcessorList(); if (generation_config.temperature !== null && generation_config.temperature !== 1.0) { warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.TemperatureLogitsWarper(generation_config.temperature)); } if (generation_config.top_k !== null && generation_config.top_k !== 0) { // TODO: add min_tokens_to_keep warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.TopKLogitsWarper(generation_config.top_k)); } if (generation_config.top_p !== null && generation_config.top_p < 1.0) { // TODO: add min_tokens_to_keep warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.TopPLogitsWarper(generation_config.top_p)); } return warpers; } /** * @param {GenerationConfig} generation_config * @param {number} input_ids_seq_length The starting sequence length for the input ids. * @returns {LogitsProcessorList} * @private */ _get_logits_processor( generation_config, input_ids_seq_length, // encoder_input_ids, TODO // prefix_allowed_tokens_fn, TODO logits_processor = null ) { const processors = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.LogitsProcessorList(); // if (generation_config.diversity_penalty !== null && generation_config.diversity_penalty > 0.0) { // processors.push(new HammingDiversityLogitsProcessor( // generation_config.diversity_penalty, // generation_config.num_beams, // generation_config.num_beam_groups // )); // } // if (generation_config.encoder_repetition_penalty !== null && generation_config.encoder_repetition_penalty !== 1.0) { // processors.push(new EncoderRepetitionPenaltyLogitsProcessor( // generation_config.encoder_repetition_penalty, // encoder_input_ids // )); // } if (generation_config.repetition_penalty !== null && generation_config.repetition_penalty !== 1.0) { processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.RepetitionPenaltyLogitsProcessor(generation_config.repetition_penalty)); } if (generation_config.no_repeat_ngram_size !== null && generation_config.no_repeat_ngram_size > 0) { processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.NoRepeatNGramLogitsProcessor(generation_config.no_repeat_ngram_size)); } // if (generation_config.encoder_no_repeat_ngram_size !== null && generation_config.encoder_no_repeat_ngram_size > 0) { // if (this.config.is_encoder_decoder) { // processors.push(new EncoderNoRepeatNGramLogitsProcessor( // generation_config.encoder_no_repeat_ngram_size, // encoder_input_ids // )); // } else { // throw new Error("It's impossible to use `encoder_no_repeat_ngram_size` with decoder-only architecture"); // } // } if (generation_config.bad_words_ids !== null) { processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.NoBadWordsLogitsProcessor(generation_config.bad_words_ids, generation_config.eos_token_id)); } if (generation_config.min_length !== null && generation_config.eos_token_id !== null && generation_config.min_length > 0) { processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.MinLengthLogitsProcessor(generation_config.min_length, generation_config.eos_token_id)); } if (generation_config.min_new_tokens !== null && generation_config.eos_token_id !== null && generation_config.min_new_tokens > 0) { processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.MinNewTokensLengthLogitsProcessor( input_ids_seq_length, generation_config.min_new_tokens, generation_config.eos_token_id )); } // if (prefix_allowed_tokens_fn !== null) { // processors.push(new PrefixConstrainedLogitsProcessor( // prefix_allowed_tokens_fn, // generation_config.num_beams / generation_config.num_beam_groups // )); // } if (generation_config.forced_bos_token_id !== null) { processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.ForcedBOSTokenLogitsProcessor(generation_config.forced_bos_token_id)); } if (generation_config.forced_eos_token_id !== null) { processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.ForcedEOSTokenLogitsProcessor( generation_config.max_length, generation_config.forced_eos_token_id )); } // if (generation_config.remove_invalid_values === true) { // processors.push(new InfNanRemoveLogitsProcessor()); // } // if (generation_config.exponential_decay_length_penalty !== null) { // processors.push(new ExponentialDecayLengthPenalty( // generation_config.exponential_decay_length_penalty, // generation_config.eos_token_id, // input_ids_seq_length // )); // } // if (generation_config.suppress_tokens !== null) { // processors.push(new SuppressTokensLogitsProcessor(generation_config.suppress_tokens)); // } if (generation_config.begin_suppress_tokens !== null) { let begin_index = (input_ids_seq_length > 1 || generation_config.forced_bos_token_id === null) ? input_ids_seq_length : input_ids_seq_length + 1; if (generation_config.forced_decoder_ids !== null) { // generation starts after the last token that is forced begin_index += generation_config.forced_decoder_ids[generation_config.forced_decoder_ids.length - 1][0]; } processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, begin_index)); } // DEPRECATED: https://github.com/huggingface/transformers/pull/29485 // if (generation_config.forced_decoder_ids !== null) { // processors.push(new ForceTokensLogitsProcessor(generation_config.forced_decoder_ids)); // } // 8. prepare batched CFG externally if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_6__.ClassifierFreeGuidanceLogitsProcessor(generation_config.guidance_scale)); } if (logits_processor !== null) { processors.extend(logits_processor) } // `LogitNormalization` should always be the last logit processor, when present // if (generation_config.renormalize_logits === true) { // processors.push(new LogitNormalization()); // } return processors; } /** * This function merges multiple generation configs together to form a final generation config to be used by the model for text generation. * It first creates an empty `GenerationConfig` object, then it applies the model's own `generation_config` property to it. Finally, if a `generation_config` object was passed in the arguments, it overwrites the corresponding properties in the final config with those of the passed config object. * @param {GenerationConfig} generation_config A `GenerationConfig` object containing generation parameters. * @param {Object} kwargs Additional generation parameters to be used in place of those in the `generation_config` object. * @returns {GenerationConfig} The final generation config object to be used by the model for text generation. */ _prepare_generation_config(generation_config, kwargs) { // Create empty generation config (contains defaults) // We pass `this.config` so that if `eos_token_id` or `bos_token_id` exist in the model's config, we will use them const gen_config = new _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_7__.GenerationConfig(this.config); // Apply model's generation config, if it exists if ('generation_config' in this) { Object.assign(gen_config, this.generation_config); } // Next, use any generation config specified by the user // when calling `generate` if (generation_config) { Object.assign(gen_config, generation_config); } // Finally, if any kwargs were passed, use them to overwrite if (kwargs) { Object.assign(gen_config, (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(kwargs, Object.getOwnPropertyNames(gen_config))); } return gen_config; } /** * * @param {GenerationConfig} generation_config * @param {StoppingCriteriaList} [stopping_criteria=null] */ _get_stopping_criteria(generation_config, stopping_criteria = null) { const criteria = new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_10__.StoppingCriteriaList(); if (generation_config.max_length !== null) { criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_10__.MaxLengthCriteria( generation_config.max_length, this.config.max_position_embeddings ?? null, )); } // if (generation_config.max_time !== null) { // criteria.push(new MaxTimeCriteria(generation_config.max_time)); // } if (generation_config.eos_token_id !== null) { criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_10__.EosTokenCriteria(generation_config.eos_token_id)); } if (stopping_criteria) { criteria.extend(stopping_criteria); } return criteria; } /** * Confirms that the model class is compatible with generation. * If not, raises an exception that points to the right class to use. */ _validate_model_class() { if (!this.can_generate) { const generate_compatible_mappings = [ MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, // MODEL_FOR_CAUSAL_IMAGE_MODELING_MAPPING, // TODO MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES, ]; const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); const generate_compatible_classes = new Set(); const modelType = this.config.model_type; for (const model_mapping of generate_compatible_mappings) { const supported_models = model_mapping.get(modelType); if (supported_models) { generate_compatible_classes.add(supported_models[0]); } } let errorMessage = `The current model class (${modelName}) is not compatible with \`.generate()\`, as it doesn't have a language model head.` if (generate_compatible_classes.size > 0) { errorMessage += ` Please use the following class instead: ${[...generate_compatible_classes].join(', ')}`; } throw Error(errorMessage); } } prepare_inputs_for_generation(...args) { return this._prepare_inputs_for_generation(this, ...args); } _update_model_kwargs_for_generation({ generated_input_ids, outputs, model_inputs, is_encoder_decoder }) { // update past_key_values model_inputs['past_key_values'] = this.getPastKeyValues(outputs, model_inputs.past_key_values); // update inputs for next run model_inputs['input_ids'] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor('int64', generated_input_ids, [generated_input_ids.length, 1]); if (!is_encoder_decoder) { // update attention mask model_inputs.attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat)( [ model_inputs.attention_mask, (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones)([model_inputs.attention_mask.dims[0], 1]), ], 1 ); } else if ('decoder_attention_mask' in model_inputs) { // TODO: update decoder attention mask if the model requires it } // force recreate position_ids in next iteration model_inputs['position_ids'] = null; return model_inputs; } /** * This function extracts the model-specific `inputs` for generation. * @param {Object} params * @param {Tensor} [params.inputs=null] * @param {number} [params.bos_token_id=null] * @param {Record} [params.model_kwargs] * @returns {{inputs_tensor: Tensor, model_inputs: Record, model_input_name: string}} The model-specific inputs for generation. */ _prepare_model_inputs({ inputs, bos_token_id, model_kwargs }) { const model_inputs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_kwargs, this.forward_params); // console.log('model_inputs', model_inputs) const input_name = this.main_input_name; if (input_name in model_inputs) { if (inputs) { throw new Error( "`inputs`: {inputs}` were passed alongside {input_name} which is not allowed. " + "Make sure to either pass {inputs} or {input_name}=..." ); } } else { model_inputs[input_name] = inputs; } const inputs_tensor = model_inputs[input_name]; return { inputs_tensor, model_inputs, model_input_name: input_name }; } async _prepare_encoder_decoder_kwargs_for_generation({ inputs_tensor, model_inputs, model_input_name, generation_config }) { const encoder_kwargs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, this.sessions['model'].inputNames); let { last_hidden_state } = await encoderForward(this, encoder_kwargs); // for classifier free guidance we need to add a 'null' input to our encoder hidden states if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { last_hidden_state = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat)([ last_hidden_state, (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full_like)(last_hidden_state, 0.0), ], 0); if ('attention_mask' in model_inputs) { model_inputs['attention_mask'] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat)([ model_inputs['attention_mask'], (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros_like)(model_inputs['attention_mask']), ], 0); } } model_inputs['encoder_outputs'] = last_hidden_state; return model_inputs; } /** * Prepares `decoder_input_ids` for generation with encoder-decoder models * @param {*} param0 */ _prepare_decoder_input_ids_for_generation({ batch_size, model_input_name, model_kwargs, decoder_start_token_id, bos_token_id, generation_config }) { decoder_start_token_id = decoder_start_token_id ?? bos_token_id; let decoder_input_ids_start_data; if (this.config.model_type === 'musicgen') { // Custom logic // TODO: move to Musicgen class decoder_input_ids_start_data = new Array(batch_size * this.config.decoder.num_codebooks) .fill(decoder_start_token_id); } else if (Array.isArray(decoder_start_token_id)) { if (decoder_start_token_id.length !== batch_size) { throw new Error( `\`decoder_start_token_id\` expcted to have length ${batch_size} but got ${decoder_start_token_id.length}` ) } // TODO: support list of start tokens? decoder_input_ids_start_data = decoder_start_token_id; } else { decoder_input_ids_start_data = new Array(batch_size).fill(decoder_start_token_id); } const decoder_input_ids_start = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( 'int64', decoder_input_ids_start_data, [decoder_input_ids_start_data.length, 1], ); // TODO add other functionality const decoder_input_ids = decoder_input_ids_start; model_kwargs['decoder_attention_mask'] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones_like)(decoder_input_ids); return { input_ids: decoder_input_ids, model_inputs: model_kwargs }; } /** * Generates sequences of token ids for models with a language modeling head. * @param {import('./generation/parameters.js').GenerationFunctionParameters} options * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. */ async generate({ inputs = null, generation_config = null, logits_processor = null, stopping_criteria = null, streamer = null, // inputs_attention_mask = null, ...kwargs }) { this._validate_model_class(); // Update generation config with defaults and kwargs generation_config = this._prepare_generation_config(generation_config, kwargs); // 3. Define model inputs let { inputs_tensor, model_inputs, model_input_name } = this._prepare_model_inputs({ inputs, model_kwargs: kwargs, }); const is_encoder_decoder = this.config.is_encoder_decoder; // 4. Define other model kwargs if (!is_encoder_decoder) { // decoder-only models should use left-padding for generation } else if (!('encoder_outputs' in model_inputs)) { // if model is encoder decoder encoder_outputs are created // and added to `model_kwargs` model_inputs = await this._prepare_encoder_decoder_kwargs_for_generation( { inputs_tensor, model_inputs, model_input_name, generation_config } ) } // 5. Prepare `input_ids` which will be used for auto-regressive generation // TODO: Update to align with HF transformers' implementation let input_ids; if (is_encoder_decoder) { // Generating from the encoder outputs ({ input_ids, model_inputs } = this._prepare_decoder_input_ids_for_generation({ batch_size: model_inputs[model_input_name].dims.at(0), model_input_name, model_kwargs: model_inputs, decoder_start_token_id: generation_config.decoder_start_token_id, bos_token_id: generation_config.bos_token_id, generation_config, })); } else { input_ids = model_inputs[model_input_name] } // 6. Prepare `max_length` depending on other stopping criteria. let input_ids_length = input_ids.dims.at(-1); if (generation_config.max_new_tokens !== null) { generation_config.max_length = input_ids_length + generation_config.max_new_tokens; } // input_ids_length = model_inputs[model_input_name].dims.at(1); // // inputs instanceof Tensor ? : inputs.length; // // decoder-only // if (input_ids_length === 0) { // throw Error("Must supply a non-empty array of input token ids.") // } // let decoder_input_ids = // generation_config.decoder_input_ids // ?? generation_config.decoder_start_token_id // ?? generation_config.bos_token_id // ?? generation_config.eos_token_id; // Update logits processor // 8. prepare distribution pre_processing samplers const prepared_logits_processor = this._get_logits_processor( generation_config, input_ids_length, logits_processor, ) // 9. prepare stopping criteria const prepared_stopping_criteria = this._get_stopping_criteria( generation_config, stopping_criteria ) // /** @type {number[]} */ // let eos_token_ids = generation_config.eos_token_id; // if (eos_token_ids !== null && !Array.isArray(eos_token_ids)) { // eos_token_ids = [eos_token_ids]; // } const numInputs = model_inputs[model_input_name].dims.at(0); // TODO: // done is a list of booleans to keep track of which inputs are done // const done = new Array(numInputs).fill(false); // For efficiency purposes, we remove completed rows from model_inputs // when the beam is complete, and we keep track of the row index // const rowIndexToBatchIndex = new Map(); const sampler = _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_11__.LogitsSampler.getSampler(generation_config); // TODO make > numInputs const scores = new Array(numInputs).fill(0); /** @type {bigint[][]} */ const all_input_ids = input_ids.tolist(); if (streamer) { streamer.put(all_input_ids); } // const all_generated_input_ids = Array.from({ length: numInputs }, () => []); // NOTE: For now, we don't support spawning new beams // TODO: when we do, we simply copy past key values and accumulate into single large tensor //////////////////////////////////////////////////// // Generic search which handles 4 generation modes: // - GenerationMode.GREEDY_SEARCH // - GenerationMode.SAMPLE // - GenerationMode.BEAM_SEARCH // - GenerationMode.BEAM_SAMPLE //////////////////////////////////////////////////// while (true) { // prepare model inputs model_inputs = this.prepare_inputs_for_generation(all_input_ids, model_inputs, generation_config); const outputs = await this.forward(model_inputs); // Logits are of the form [batch_size, out_seq_length, vocab_size] // In most cases, this will be [batch_size, 1, vocab_size] // So, we select the last token's logits: // (equivalent to `logits = outputs.logits[:, -1, :]`) const logits = outputs.logits.slice(null, -1, null); const next_tokens_scores = prepared_logits_processor(all_input_ids, logits); // only for this batch const generated_input_ids = []; // const new_kv_cache = [];// NOTE: Only used for beam search when concatenating new kv // Loop over each batch for (let batch_idx = 0; batch_idx < next_tokens_scores.dims.at(0); ++batch_idx) { const logs = next_tokens_scores[batch_idx]; const sampledTokens = sampler(logs); for (const [newTokenId, logProb] of sampledTokens) { const bigint = BigInt(newTokenId); // TODO: If branching, use previous beam as a starting point // update generated ids, model inputs, and length for next step scores[batch_idx] += logProb; all_input_ids[batch_idx].push(bigint); generated_input_ids.push(bigint); } } if (streamer) { streamer.put(all_input_ids); } const stop = prepared_stopping_criteria(all_input_ids); if (stop.every(x => x)) { break; } model_inputs = this._update_model_kwargs_for_generation({ generated_input_ids, outputs, model_inputs, is_encoder_decoder, }) } if (streamer) { streamer.end(); } // TODO: ensure all_input_ids is padded correctly... return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor('int64', all_input_ids.flat(), [all_input_ids.length, all_input_ids[0].length]); // TODO: // let numOutputTokens = 1; // const maxOutputTokens = numOutputTokens + (generation_config.max_new_tokens ?? Infinity); // // Only use max length if max_new_tokens is not provided // const useMaxLength = Number.isInteger(generation_config.max_length) && (generation_config.max_new_tokens ?? null) === null; // // console.log('inputs', inputs) // let beams = this.getStartBeams(inputs, generation_config, numOutputTokens, inputs_attention_mask); // while (beams.some(x => !x.done) && numOutputTokens < maxOutputTokens) { // let newest_beams = []; // for (let beam of beams) { // if (beam.done) { // // Add this beam back into the pool // newest_beams.push(beam); // continue // } // if (useMaxLength && beam.output_token_ids.length >= generation_config.max_length) { // // Set this beam to done and add it back into the pool // beam.done = true; // newest_beams.push(beam); // continue // } // // TODO generalize // let output = await this.runBeam(beam); // // add attentions/scores to beam only if user requested // if (generation_config.output_attentions) { // this.addAttentionsToBeam(beam, output); // } // if (generation_config.output_scores) { // // TODO add // } // let logits = output.logits.slice(null, -1, null); // // Apply logits processor // logits_processor(beam.output_token_ids, logits); // let sampledTokens = sampler(logits); // for (let [newTokenId, logProb] of sampledTokens) { // // use previous beam as a starting point // let newBeam = { ...beam }; // // update new beam // // @ts-ignore // this.updateBeam(newBeam, newTokenId); // newBeam.score += logProb; // if (eos_token_ids && eos_token_ids.includes(newTokenId)) { // newBeam.done = true; // } // newest_beams.push(newBeam); // } // } // ++numOutputTokens; // // Next, we get the best beams, per ID // newest_beams = this.groupBeams(newest_beams).map( // group => group // .sort((a, b) => b.score - a.score) // sort by score // .slice(0, generation_config.num_beams) // remove outside beam width // ); // // Flatten beams // beams = newest_beams.flat(); // // Run callback // if (generation_config.callback_function) { // throw new Error("Callback function not yet implemented") // generation_config.callback_function(beams); // } // } // // TODO: Ensure that we can return non-batched outputs // const groupedBeams = this.groupBeams(beams); // const getFlattened = (key) => groupedBeams.map( // batch => { // if (generation_config.num_return_sequences > 1) { // return batch.slice(0, generation_config.num_return_sequences).map(x => x[key]); // } else { // return [batch[0][key]]; // } // } // ).flat(); // Flatten across batches (depth=1) // const sequences = getFlattened('output_token_ids'); // [1, seqLength] // if (generation_config.return_dict_in_generate) { // // NOTE: `decoder_attentions` and `cross_attentions` should be: // // list (one element for each generated token) // // of list (one element for each layer of the decoder) // // of torch.FloatTensor of shape (batch_size, num_heads, generated_length, sequence_length) // // However, since we are only generating one batch at a time, they are of the form: // // list (batches) // // of list (one element for each generated token) // // of list (one element for each layer of the decoder) // // of torch.FloatTensor of shape (1, num_heads, generated_length, sequence_length) // // // // TODO: In future (when true parallelism, we should be able to return the correct shape) // const decoder_attentions = getFlattened('decoder_attentions'); // const cross_attentions = getFlattened('cross_attentions'); // return { // sequences, // decoder_attentions, // cross_attentions, // } // } else { // return sequences; // } } /** * Helper function to add attentions to beam * @param {Object} beam * @param {Object} output * @private */ addAttentionsToBeam(beam, output) { if (this.config.is_encoder_decoder) { if (!output.cross_attentions || output.cross_attentions.length === 0) { throw Error( "`output_attentions` is true, but the model did not produce cross-attentions. " + "This is most likely because the model was not exported with `output_attentions=True`." ) } if (!beam.cross_attentions) { beam.cross_attentions = []; } beam.cross_attentions.push(output.cross_attentions); } if (!output.decoder_attentions || output.decoder_attentions.length === 0) { throw Error( "`output_attentions` is true, but the model did not produce decoder-attentions. " + "This is most likely because the model was not exported with `output_attentions=True`." ) } if (!beam.decoder_attentions) { beam.decoder_attentions = []; } beam.decoder_attentions.push(output.decoder_attentions); } /** * Groups an array of beam objects by their ids. * * @param {Array} beams The array of beam objects to group. * @returns {Array} An array of arrays, where each inner array contains beam objects with the same id. */ groupBeams(beams) { // Group beams by their ids const groups = Object.create(null); for (const obj of beams) { if (groups[obj.id] === undefined) { groups[obj.id] = [obj]; } else { groups[obj.id].push(obj); } } return Object.values(groups); } /** * Returns an object containing past key values from the given decoder results object. * * @param {Object} decoderResults The decoder results object. * @param {Object} pastKeyValues The previous past key values. * @returns {Object} An object containing past key values. */ getPastKeyValues(decoderResults, pastKeyValues) { const pkvs = Object.create(null); for (const name in decoderResults) { if (name.startsWith('present')) { let newName = name.replace('present', 'past_key_values'); if (pastKeyValues && name.includes('encoder')) { // Optimization introduced by optimum to reuse past key values. So, we just replace the constant // outputs with the previous past key values. // https://github.com/huggingface/optimum/blob/0bf2c05fb7e1182b52d21b703cfc95fd9e4ea3dc/optimum/onnxruntime/base.py#L677-L704 pkvs[newName] = pastKeyValues[newName]; } else { pkvs[newName] = decoderResults[name]; } } } return pkvs; } /** * Returns an object containing attentions from the given decoder results object. * * @param {Object} decoderResults The decoder results object. * @returns {Object} An object containing attentions. */ getAttentions(decoderResults) { const attns = Object.create(null); for (const attnName of ['cross_attentions', 'decoder_attentions']) { const result = []; for (const name in decoderResults) { if (name.startsWith(attnName)) { const index = name.split('.').pop() result[index] = decoderResults[name]; } } attns[attnName] = result; } return attns; } /** * Adds past key values to the decoder feeds object. If pastKeyValues is null, creates new tensors for past key values. * * @param {Object} decoderFeeds The decoder feeds object to add past key values to. * @param {Object} pastKeyValues An object containing past key values. */ addPastKeyValues(decoderFeeds, pastKeyValues) { if (pastKeyValues) { Object.assign(decoderFeeds, pastKeyValues) } else { // TODO support batches (i.e., batch_size > 1) const batch_size = 1; const dtype = 'float32'; // this.config.precision || const empty = []; // (dtype === 'float16') ? new Uint16Array() : []; // @ts-ignore if (this.config.is_encoder_decoder && (this.add_encoder_pkv ?? true)) { // @ts-ignore let encoder_dims = [batch_size, this.num_encoder_heads, 0, this.encoder_dim_kv]; // @ts-ignore let decoder_dims = [batch_size, this.num_decoder_heads, 0, this.decoder_dim_kv]; // @ts-ignore for (let i = 0; i < this.num_decoder_layers; ++i) { decoderFeeds[`past_key_values.${i}.encoder.key`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, encoder_dims) decoderFeeds[`past_key_values.${i}.encoder.value`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, encoder_dims) decoderFeeds[`past_key_values.${i}.decoder.key`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, decoder_dims) decoderFeeds[`past_key_values.${i}.decoder.value`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, decoder_dims) } } else if (this.config.model_type === 'falcon') { // NOTE: Custom implementation for Falcon // @ts-ignore let dims = [batch_size * this.num_heads, 0, this.dim_kv] // @ts-ignore for (let i = 0; i < this.num_layers; ++i) { decoderFeeds[`past_key_values.${i}.key`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, dims) decoderFeeds[`past_key_values.${i}.value`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, dims) } } else if (this.config.multi_query) { // e.g., for `gpt_bigcode` // @ts-ignore let dims = [batch_size * this.num_heads, 0, 2 * this.dim_kv] // @ts-ignore for (let i = 0; i < this.num_layers; ++i) { decoderFeeds[`past_key_values.${i}.key_value`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, dims) } } else if (this.config.model_type === 'bloom') { // NOTE: Custom implementation for Bloom // @ts-ignore let keyDims = [batch_size * this.num_heads, this.dim_kv, 0] // [batch_size x num_heads,64,past_sequence_length] // @ts-ignore let valueDims = [batch_size * this.num_heads, 0, this.dim_kv] // [batch_size x num_heads,past_sequence_length,64] // @ts-ignore for (let i = 0; i < this.num_layers; ++i) { decoderFeeds[`past_key_values.${i}.key`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, keyDims) decoderFeeds[`past_key_values.${i}.value`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, valueDims) } } else { // Decoder-only // @ts-ignore let dims = [batch_size, this.num_heads, 0, this.dim_kv] // @ts-ignore for (let i = 0; i < this.num_layers; ++i) { decoderFeeds[`past_key_values.${i}.key`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, dims) decoderFeeds[`past_key_values.${i}.value`] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor(dtype, empty, dims) } } } } } ////////////////////////////////////////////////// // Base model output class class ModelOutput { } /** * Base class for model's outputs, with potential hidden states and attentions. */ class BaseModelOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.last_hidden_state Sequence of hidden-states at the output of the last layer of the model. * @param {Tensor} [output.hidden_states] Hidden-states of the model at the output of each layer plus the optional initial embedding outputs. * @param {Tensor} [output.attentions] Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. */ constructor({ last_hidden_state, hidden_states = null, attentions = null }) { super(); this.last_hidden_state = last_hidden_state; this.hidden_states = hidden_states; this.attentions = attentions; } } ////////////////////////////////////////////////// // Bert models class BertPreTrainedModel extends PreTrainedModel { } class BertModel extends BertPreTrainedModel { } /** * BertForMaskedLM is a class representing a BERT model for masked language modeling. */ class BertForMaskedLM extends BertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for masked language modeling. */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * BertForSequenceClassification is a class representing a BERT model for sequence classification. */ class BertForSequenceClassification extends BertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * BertForTokenClassification is a class representing a BERT model for token classification. */ class BertForTokenClassification extends BertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * BertForQuestionAnswering is a class representing a BERT model for question answering. */ class BertForQuestionAnswering extends BertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for question answering. */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // NomicBert models class NomicBertPreTrainedModel extends PreTrainedModel { } class NomicBertModel extends NomicBertPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // RoFormer models class RoFormerPreTrainedModel extends PreTrainedModel { } /** * The bare RoFormer Model transformer outputting raw hidden-states without any specific head on top. */ class RoFormerModel extends RoFormerPreTrainedModel { } /** * RoFormer Model with a `language modeling` head on top. */ class RoFormerForMaskedLM extends RoFormerPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for masked language modeling. */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * RoFormer Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) */ class RoFormerForSequenceClassification extends RoFormerPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * RoFormer Model with a token classification head on top (a linear layer on top of the hidden-states output) * e.g. for Named-Entity-Recognition (NER) tasks. */ class RoFormerForTokenClassification extends RoFormerPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * RoFormer Model with a span classification head on top for extractive question-answering tasks like SQuAD * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`). */ class RoFormerForQuestionAnswering extends RoFormerPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for question answering. */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } // TODO: Add RoFormerForCausalLM and RoFormerForMultipleChoice ////////////////////////////////////////////////// ////////////////////////////////////////////////// // ConvBert models class ConvBertPreTrainedModel extends PreTrainedModel { } /** * The bare ConvBERT Model transformer outputting raw hidden-states without any specific head on top. */ class ConvBertModel extends ConvBertPreTrainedModel { } /** * ConvBERT Model with a language modeling head on top. */ class ConvBertForMaskedLM extends ConvBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for masked language modeling. */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * ConvBERT Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) */ class ConvBertForSequenceClassification extends ConvBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * ConvBERT Model with a token classification head on top (a linear layer on top of the hidden-states output) * e.g. for Named-Entity-Recognition (NER) tasks. */ class ConvBertForTokenClassification extends ConvBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * ConvBERT Model with a span classification head on top for extractive question-answering tasks like SQuAD * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`) */ class ConvBertForQuestionAnswering extends ConvBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for question answering. */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Electra models class ElectraPreTrainedModel extends PreTrainedModel { } /** * The bare Electra Model transformer outputting raw hidden-states without any specific head on top. * Identical to the BERT model except that it uses an additional linear layer between the embedding * layer and the encoder if the hidden size and embedding size are different. */ class ElectraModel extends ElectraPreTrainedModel { } // TODO add ElectraForPreTraining /** * Electra model with a language modeling head on top. */ class ElectraForMaskedLM extends ElectraPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for masked language modeling. */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * ELECTRA Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) */ class ElectraForSequenceClassification extends ElectraPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * Electra model with a token classification head on top. */ class ElectraForTokenClassification extends ElectraPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * LECTRA Model with a span classification head on top for extractive question-answering tasks like SQuAD * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`). */ class ElectraForQuestionAnswering extends ElectraPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for question answering. */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // CamemBERT models class CamembertPreTrainedModel extends PreTrainedModel { } /** * The bare CamemBERT Model transformer outputting raw hidden-states without any specific head on top. */ class CamembertModel extends CamembertPreTrainedModel { } /** * CamemBERT Model with a `language modeling` head on top. */ class CamembertForMaskedLM extends CamembertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for masked language modeling. */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * CamemBERT Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for GLUE tasks. */ class CamembertForSequenceClassification extends CamembertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * CamemBERT Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. */ class CamembertForTokenClassification extends CamembertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * CamemBERT Model with a span classification head on top for extractive question-answering tasks */ class CamembertForQuestionAnswering extends CamembertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for question answering. */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // DeBERTa models class DebertaPreTrainedModel extends PreTrainedModel { } /** * The bare DeBERTa Model transformer outputting raw hidden-states without any specific head on top. */ class DebertaModel extends DebertaPreTrainedModel { } /** * DeBERTa Model with a `language modeling` head on top. */ class DebertaForMaskedLM extends DebertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for masked language modeling. */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * DeBERTa Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) */ class DebertaForSequenceClassification extends DebertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * DeBERTa Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. */ class DebertaForTokenClassification extends DebertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * DeBERTa Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear * layers on top of the hidden-states output to compute `span start logits` and `span end logits`). */ class DebertaForQuestionAnswering extends DebertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for question answering. */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // DeBERTa-v2 models class DebertaV2PreTrainedModel extends PreTrainedModel { } /** * The bare DeBERTa-V2 Model transformer outputting raw hidden-states without any specific head on top. */ class DebertaV2Model extends DebertaV2PreTrainedModel { } /** * DeBERTa-V2 Model with a `language modeling` head on top. */ class DebertaV2ForMaskedLM extends DebertaV2PreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for masked language modeling. */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * DeBERTa-V2 Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) */ class DebertaV2ForSequenceClassification extends DebertaV2PreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * DeBERTa-V2 Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. */ class DebertaV2ForTokenClassification extends DebertaV2PreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * DeBERTa-V2 Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear * layers on top of the hidden-states output to compute `span start logits` and `span end logits`). */ class DebertaV2ForQuestionAnswering extends DebertaV2PreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for question answering. */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // DistilBert models class DistilBertPreTrainedModel extends PreTrainedModel { } class DistilBertModel extends DistilBertPreTrainedModel { } /** * DistilBertForSequenceClassification is a class representing a DistilBERT model for sequence classification. */ class DistilBertForSequenceClassification extends DistilBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * DistilBertForTokenClassification is a class representing a DistilBERT model for token classification. */ class DistilBertForTokenClassification extends DistilBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * DistilBertForQuestionAnswering is a class representing a DistilBERT model for question answering. */ class DistilBertForQuestionAnswering extends DistilBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for question answering. */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } /** * DistilBertForMaskedLM is a class representing a DistilBERT model for masking task. */ class DistilBertForMaskedLM extends DistilBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // ESM models class EsmPreTrainedModel extends PreTrainedModel { } /** * The bare ESM Model transformer outputting raw hidden-states without any specific head on top. */ class EsmModel extends EsmPreTrainedModel { } /** * ESM Model with a `language modeling` head on top. */ class EsmForMaskedLM extends EsmPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for masked language modeling. */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * ESM Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) */ class EsmForSequenceClassification extends EsmPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * ESM Model with a token classification head on top (a linear layer on top of the hidden-states output) * e.g. for Named-Entity-Recognition (NER) tasks. */ class EsmForTokenClassification extends EsmPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // MobileBert models class MobileBertPreTrainedModel extends PreTrainedModel { } class MobileBertModel extends MobileBertPreTrainedModel { } /** * MobileBertForMaskedLM is a class representing a MobileBERT model for masking task. */ class MobileBertForMaskedLM extends MobileBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * MobileBert Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) */ class MobileBertForSequenceClassification extends MobileBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * MobileBert Model with a span classification head on top for extractive question-answering tasks */ class MobileBertForQuestionAnswering extends MobileBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // MPNet models class MPNetPreTrainedModel extends PreTrainedModel { } /** * The bare MPNet Model transformer outputting raw hidden-states without any specific head on top. */ class MPNetModel extends MPNetPreTrainedModel { } /** * MPNetForMaskedLM is a class representing a MPNet model for masked language modeling. */ class MPNetForMaskedLM extends MPNetPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for masked language modeling. */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * MPNetForSequenceClassification is a class representing a MPNet model for sequence classification. */ class MPNetForSequenceClassification extends MPNetPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * MPNetForTokenClassification is a class representing a MPNet model for token classification. */ class MPNetForTokenClassification extends MPNetPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * MPNetForQuestionAnswering is a class representing a MPNet model for question answering. */ class MPNetForQuestionAnswering extends MPNetPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for question answering. */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // SqueezeBert models class SqueezeBertPreTrainedModel extends PreTrainedModel { } class SqueezeBertModel extends SqueezeBertPreTrainedModel { } class SqueezeBertForMaskedLM extends SqueezeBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } class SqueezeBertForSequenceClassification extends SqueezeBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } class SqueezeBertForQuestionAnswering extends SqueezeBertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Albert models class AlbertPreTrainedModel extends PreTrainedModel { } class AlbertModel extends AlbertPreTrainedModel { } class AlbertForSequenceClassification extends AlbertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } class AlbertForQuestionAnswering extends AlbertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } class AlbertForMaskedLM extends AlbertPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // T5 models class T5PreTrainedModel extends PreTrainedModel { forward_params = ['input_ids', 'attention_mask', 'encoder_outputs', 'decoder_input_ids', 'decoder_attention_mask', 'past_key_values']; /** * Creates a new instance of the `T5PreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.num_decoder_layers; this.num_decoder_heads = this.config.num_heads; this.decoder_dim_kv = this.config.d_kv; this.num_encoder_layers = this.config.num_layers; this.num_encoder_heads = this.config.num_heads; this.encoder_dim_kv = this.config.d_kv; } }; class T5Model extends T5PreTrainedModel { } /** * T5Model is a class representing a T5 model for conditional generation. */ class T5ForConditionalGeneration extends T5PreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // LONGT5 models /** * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. */ class LongT5PreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `LongT5ForConditionalGeneration` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.num_decoder_layers; this.num_decoder_heads = this.config.num_heads; this.decoder_dim_kv = this.config.d_kv; this.num_encoder_layers = this.config.num_layers; this.num_encoder_heads = this.config.num_heads; this.encoder_dim_kv = this.config.d_kv; } }; /** * The bare LONGT5 Model transformer outputting raw hidden-states without any specific head on top. */ class LongT5Model extends LongT5PreTrainedModel { } /** * LONGT5 Model with a `language modeling` head on top. */ class LongT5ForConditionalGeneration extends LongT5PreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // MT5 models class MT5PreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `MT5ForConditionalGeneration` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.num_decoder_layers; this.num_decoder_heads = this.config.num_heads; this.decoder_dim_kv = this.config.d_kv; this.num_encoder_layers = this.config.num_layers; this.num_encoder_heads = this.config.num_heads; this.encoder_dim_kv = this.config.d_kv; } }; class MT5Model extends MT5PreTrainedModel { } /** * A class representing a conditional sequence-to-sequence model based on the MT5 architecture. */ class MT5ForConditionalGeneration extends MT5PreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Bart models class BartPretrainedModel extends PreTrainedModel { /** * Creates a new instance of the `BartForConditionalGeneration` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.decoder_layers; this.num_decoder_heads = this.config.decoder_attention_heads; this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads; this.num_encoder_layers = this.config.encoder_layers; this.num_encoder_heads = this.config.encoder_attention_heads; this.encoder_dim_kv = this.config.d_model / this.num_encoder_heads; } }; /** * The bare BART Model outputting raw hidden-states without any specific head on top. */ class BartModel extends BartPretrainedModel { } /** * The BART Model with a language modeling head. Can be used for summarization. */ class BartForConditionalGeneration extends BartPretrainedModel { } /** * Bart model with a sequence classification/head on top (a linear layer on top of the pooled output) */ class BartForSequenceClassification extends BartPretrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // MBart models class MBartPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `MBartForConditionalGeneration` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.decoder_layers; this.num_decoder_heads = this.config.decoder_attention_heads; this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads; this.num_encoder_layers = this.config.encoder_layers; this.num_encoder_heads = this.config.encoder_attention_heads; this.encoder_dim_kv = this.config.d_model / this.num_encoder_heads; } }; /** * The bare MBART Model outputting raw hidden-states without any specific head on top. */ class MBartModel extends MBartPreTrainedModel { } /** * The MBART Model with a language modeling head. Can be used for summarization, after fine-tuning the pretrained models. */ class MBartForConditionalGeneration extends MBartPreTrainedModel { } /** * MBart model with a sequence classification/head on top (a linear layer on top of the pooled output). */ class MBartForSequenceClassification extends MBartPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } class MBartForCausalLM extends MBartPreTrainedModel { /** * Creates a new instance of the `MBartForCausalLM` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions, generation_config); this.num_decoder_layers = this.config.decoder_layers; this.num_decoder_heads = this.config.decoder_attention_heads; this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads; this.num_encoder_layers = this.config.encoder_layers; this.num_encoder_heads = this.config.encoder_attention_heads; this.encoder_dim_kv = this.config.d_model / this.num_encoder_heads; } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Blenderbot models class BlenderbotPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `BlenderbotForConditionalGeneration` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.decoder_layers; this.num_decoder_heads = this.config.decoder_attention_heads; this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads; this.num_encoder_layers = this.config.encoder_layers; this.num_encoder_heads = this.config.encoder_attention_heads; this.encoder_dim_kv = this.config.d_model / this.num_encoder_heads; } }; /** * The bare Blenderbot Model outputting raw hidden-states without any specific head on top. */ class BlenderbotModel extends BlenderbotPreTrainedModel { } /** * The Blenderbot Model with a language modeling head. Can be used for summarization. */ class BlenderbotForConditionalGeneration extends BlenderbotPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Blenderbot models class BlenderbotSmallPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `BlenderbotForConditionalGeneration` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.decoder_layers; this.num_decoder_heads = this.config.decoder_attention_heads; this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads; this.num_encoder_layers = this.config.encoder_layers; this.num_encoder_heads = this.config.encoder_attention_heads; this.encoder_dim_kv = this.config.d_model / this.num_encoder_heads; } }; /** * The bare BlenderbotSmall Model outputting raw hidden-states without any specific head on top. */ class BlenderbotSmallModel extends BlenderbotSmallPreTrainedModel { } /** * The BlenderbotSmall Model with a language modeling head. Can be used for summarization. */ class BlenderbotSmallForConditionalGeneration extends BlenderbotSmallPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Roberta models class RobertaPreTrainedModel extends PreTrainedModel { } class RobertaModel extends RobertaPreTrainedModel { } /** * RobertaForMaskedLM class for performing masked language modeling on Roberta models. */ class RobertaForMaskedLM extends RobertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * RobertaForSequenceClassification class for performing sequence classification on Roberta models. */ class RobertaForSequenceClassification extends RobertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * RobertaForTokenClassification class for performing token classification on Roberta models. */ class RobertaForTokenClassification extends RobertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * RobertaForQuestionAnswering class for performing question answering on Roberta models. */ class RobertaForQuestionAnswering extends RobertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // XLM models /** * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. */ class XLMPreTrainedModel extends PreTrainedModel { } /** * The bare XLM Model transformer outputting raw hidden-states without any specific head on top. */ class XLMModel extends XLMPreTrainedModel { } /** * The XLM Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). */ class XLMWithLMHeadModel extends XLMPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * XLM Model with a sequence classification/regression head on top (a linear layer on top of the pooled output) */ class XLMForSequenceClassification extends XLMPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * XLM Model with a token classification head on top (a linear layer on top of the hidden-states output) */ class XLMForTokenClassification extends XLMPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * XLM Model with a span classification head on top for extractive question-answering tasks */ class XLMForQuestionAnswering extends XLMPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // XLMRoberta models class XLMRobertaPreTrainedModel extends PreTrainedModel { } class XLMRobertaModel extends XLMRobertaPreTrainedModel { } /** * XLMRobertaForMaskedLM class for performing masked language modeling on XLMRoberta models. */ class XLMRobertaForMaskedLM extends XLMRobertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new MaskedLMOutput(await super._call(model_inputs)); } } /** * XLMRobertaForSequenceClassification class for performing sequence classification on XLMRoberta models. */ class XLMRobertaForSequenceClassification extends XLMRobertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * XLMRobertaForTokenClassification class for performing token classification on XLMRoberta models. */ class XLMRobertaForTokenClassification extends XLMRobertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for token classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } /** * XLMRobertaForQuestionAnswering class for performing question answering on XLMRoberta models. */ class XLMRobertaForQuestionAnswering extends XLMRobertaPreTrainedModel { /** * Calls the model on new inputs. * * @param {Object} model_inputs The inputs to the model. * @returns {Promise} returned object */ async _call(model_inputs) { return new QuestionAnsweringModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Audio Spectrogram Transformer (AST) models class ASTPreTrainedModel extends PreTrainedModel { }; /** * The bare AST Model transformer outputting raw hidden-states without any specific head on top. */ class ASTModel extends ASTPreTrainedModel { } /** * Audio Spectrogram Transformer model with an audio classification head on top * (a linear layer on top of the pooled output) e.g. for datasets like AudioSet, Speech Commands v2. */ class ASTForAudioClassification extends ASTPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Whisper models class WhisperPreTrainedModel extends PreTrainedModel { requires_attention_mask = false; main_input_name = 'input_features'; forward_params = ['input_features', 'attention_mask', 'decoder_input_ids', 'decoder_attention_mask', 'past_key_values']; /** * Creates a new instance of the `WhisperForConditionalGeneration` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.decoder_layers; this.num_decoder_heads = this.config.decoder_attention_heads; this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads; this.num_encoder_layers = this.config.encoder_layers; this.num_encoder_heads = this.config.encoder_attention_heads; this.encoder_dim_kv = this.config.d_model / this.num_encoder_heads; } }; /** * WhisperModel class for training Whisper models without a language model head. */ class WhisperModel extends WhisperPreTrainedModel { } class WhisperGenerationConfig extends _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_7__.GenerationConfig { /** * Whether to return the timestamps with the text. This enables the `WhisperTimestampsLogitsProcessor`. * @type {boolean} */ return_timestamps = null; /** * Whether to return token-level timestamps * with the text. This can be used with or without the `return_timestamps` option. To get word-level * timestamps, use the tokenizer to group the tokens into words. * @type {boolean} */ return_token_timestamps = null; /** * The number of audio frames available in this chunk. This is only used generating word-level timestamps. * @type {number} */ num_frames = null; /** * Alignment heads to predict word-level timestamps. This is a list of [layer, head] pairs that * select the cross-attention heads that are highly correlated to word-level timing. * @type {[number, number][]} */ alignment_heads = null; /** * Task to use for generation, either "translate" or "transcribe". * @type {string} */ task = null; /** * Language token to use for generation, can be either in the form of `<|en|>`, `en` or `english`. * You can find all the possible language tokens in the `model.generation_config.lang_to_id` dictionary. * @type {string} */ language = null; } /** * WhisperForConditionalGeneration class for generating conditional outputs from Whisper models. */ class WhisperForConditionalGeneration extends WhisperPreTrainedModel { /** * * @param {WhisperGenerationConfig} generation_config */ _retrieve_init_tokens(generation_config) { const init_tokens = [generation_config.decoder_start_token_id] throw new Error("Not implemented yet") } /** * @typedef {Object} WhisperGenerationSpecificParams * @property {WhisperGenerationConfig} generation_config */ /** * Transcribes or translates log-mel input features to a sequence of auto-regressively generated token ids. * @param {import('./generation/parameters.js').GenerationFunctionParameters & {generation_config: WhisperGenerationConfig} & WhisperGenerationConfig} options * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. */ async generate({ inputs = null, generation_config = null, logits_processor = null, stopping_criteria = null, // Whisper-specific options language = null, task = null, ...kwargs }) { throw new Error("WhisperForConditionalGeneration.generate is not yet in Transformers.js v3.") // console.log('inputs', inputs); // console.log('kwargs', kwargs); // async generate({ // inputs, // }, // generation_config = null, // logits_processor = null, // // { // // return_timestamps = null, // // return_token_timestamps = null, // // language = null, // // task = null, // // } = {}, // ) { // Create generation config object // TODO: this doesn't create a WhisperGenerationConfig, it makes a GenerationConfig generation_config = this._prepare_generation_config(generation_config); // Whisper has additional options for returning timestamps generation_config.return_timestamps ??= false; // TODO add language and task if (generation_config.return_timestamps) { throw new Error("Not implemented yet") // logits_processor = [new WhisperTimeStampLogitsProcessor(generation_config)] } if (generation_config.return_token_timestamps) { generation_config.output_attentions = true; generation_config.return_dict_in_generate = true; if (generation_config.task === 'translate') { console.warn("Token-level timestamps may not be reliable for task 'translate'.") } if (!generation_config.alignment_heads) { throw new Error( "Model generation config has no `alignment_heads`, token-level timestamps not available. " + "See https://gist.github.com/hollance/42e32852f24243b748ae6bc1f985b13a on how to add this property to the generation config." ) } } const init_tokens = this._retrieve_init_tokens(generation_config) // https://github.com/huggingface/transformers/pull/28687/files const outputs = await super.generate({ inputs, generation_config, logits_processor, ...kwargs }); if (generation_config.return_token_timestamps && generation_config.alignment_heads) { outputs["token_timestamps"] = this._extract_token_timestamps( outputs, generation_config.alignment_heads, generation_config.num_frames, ) } return outputs } /** * Calculates token-level timestamps using the encoder-decoder cross-attentions and * dynamic time-warping (DTW) to map each output token to a position in the input audio. * @param {Object} generate_outputs Outputs generated by the model * @param {Tensor[][][]} generate_outputs.cross_attentions The cross attentions output by the model * @param {Tensor[][][]} generate_outputs.decoder_attentions The decoder attentions output by the model * @param {number[][]} generate_outputs.sequences The sequences output by the model * @param {number[][]} alignment_heads Alignment heads of the model * @param {number} [num_frames=null] Number of frames in the input audio. * @param {number} [time_precision=0.02] Precision of the timestamps in seconds * @returns {Tensor} tensor containing the timestamps in seconds for each predicted token */ _extract_token_timestamps(generate_outputs, alignment_heads, num_frames = null, time_precision = 0.02) { if (!generate_outputs.cross_attentions) { throw new Error( "Model outputs must contain cross attentions to extract timestamps. " + "This is most likely because the model was not exported with `output_attentions=True`." ) } let median_filter_width = this.config.median_filter_width; if (median_filter_width === undefined) { console.warn("Model config has no `median_filter_width`, using default value of 7.") median_filter_width = 7; } const batchedMatrices = generate_outputs.cross_attentions.map(batch => { // Create a list with `decoder_layers` elements, each a tensor of shape // (batch size, attention_heads, output length, input length). let cross_attentions = Array.from({ length: this.config.decoder_layers }, (_, i) => (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat)(batch.map(x => x[i]), 2) ); let weights = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.stack)(alignment_heads.map(([l, h]) => { return num_frames ? cross_attentions[l].slice(null, h, null, [0, num_frames]) : cross_attentions[l].slice(null, h); })); weights = weights.transpose(1, 0, 2, 3) let [std, calculatedMean] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.std_mean)(weights, -2, 0, true); // Normalize and smoothen the weights. let smoothedWeights = weights.clone(); // [1, 8, seqLength, 1500] for (let a = 0; a < smoothedWeights.dims[0]; ++a) { let aTensor = smoothedWeights[a]; // [8, seqLength, 1500] for (let b = 0; b < aTensor.dims[0]; ++b) { let bTensor = aTensor[b]; // [seqLength, 1500] const stdTensor = std[a][b][0]; // [1500] const meanTensor = calculatedMean[a][b][0]; // [1500] for (let c = 0; c < bTensor.dims[0]; ++c) { let cTensor = bTensor[c]; // [1500] for (let d = 0; d < cTensor.data.length; ++d) { cTensor.data[d] = (cTensor.data[d] - meanTensor.data[d]) / stdTensor.data[d] } // Apply median filter. cTensor.data.set((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.medianFilter)(cTensor.data, median_filter_width)) } } } // Average the different cross-attention heads. const matrix = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean)(smoothedWeights, 1); return matrix; }); const timestampsShape = [generate_outputs.sequences.length, generate_outputs.sequences[0].length]; const timestamps = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( 'float32', new Float32Array(timestampsShape[0] * timestampsShape[1]), timestampsShape ); // Perform dynamic time warping on each element of the batch. for (let batch_idx = 0; batch_idx < timestampsShape[0]; ++batch_idx) { // NOTE: Since we run only one batch at a time, we can squeeze to get the same dimensions // as the python implementation const matrix = batchedMatrices[batch_idx].neg().squeeze_(0); let [text_indices, time_indices] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.dynamicTimeWarping)(matrix); let diffs = Array.from({ length: text_indices.length - 1 }, (v, i) => text_indices[i + 1] - text_indices[i]); let jumps = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.mergeArrays)([1], diffs).map(x => !!x); // convert to boolean let jump_times = []; for (let i = 0; i < jumps.length; ++i) { if (jumps[i]) { jump_times.push(time_indices[i] * time_precision); // NOTE: No point in rounding here, since we set to Float32Array later } } timestamps[batch_idx].data.set(jump_times, 1) } return timestamps; } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// /** * Vision Encoder-Decoder model based on OpenAI's GPT architecture for image captioning and other vision tasks */ class VisionEncoderDecoderModel extends PreTrainedModel { main_input_name = 'pixel_values'; /** * Creates a new instance of the `VisionEncoderDecoderModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // Extract configs const encoderConfig = this.config.encoder; const decoderConfig = this.config.decoder; // Validate encoder const encoderModelType = encoderConfig.model_type; const encoderModel = MODEL_MAPPING_NAMES_ENCODER_ONLY.get(encoderModelType) ?? MODEL_MAPPING_NAMES_ENCODER_DECODER.get(encoderModelType); if (!encoderModel) { console.warn(`Model type for encoder '${encoderModelType}' not found, assuming encoder-only architecture. Please report this at https://github.com/xenova/transformers.js/issues/new/choose.`); } // Validate decoder const decoderModel = MODEL_FOR_CAUSAL_LM_MAPPING_NAMES.get(decoderConfig.model_type); if (!decoderModel) { throw new Error(`Unable to construct \`VisionEncoderDecoder\` due to unsupported decoder: "${this.config.decoder.model_type}"`); } // @ts-ignore const decoderModelClass = decoderModel[1]; // @ts-ignore const decoder = new decoderModelClass(decoderConfig, { /* No sessions */ }, generation_config); this.add_encoder_pkv = 'num_decoder_layers' in decoder; if (this.add_encoder_pkv) { // Decoder is part of an encoder-decoder model this.num_decoder_layers = decoder.num_decoder_layers; this.num_decoder_heads = decoder.num_decoder_heads; this.decoder_dim_kv = decoder.decoder_dim_kv; this.num_encoder_layers = decoder.num_encoder_layers; this.num_encoder_heads = decoder.num_encoder_heads; this.encoder_dim_kv = decoder.encoder_dim_kv; } else { // Decoder is a decoder-only model this.num_layers = decoder.num_layers; this.num_heads = decoder.num_heads; this.dim_kv = decoder.dim_kv; } } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // LLaVa Models class LlavaPreTrainedModel extends PreTrainedModel { forward_params = [ 'input_ids', 'past_key_values', 'pixel_values', 'attention_mask', ]; constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; const decoderConfig = this.config.text_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id this.config.pad_token_id = decoderConfig.eos_token_id; this.num_heads = decoderConfig.num_attention_heads; this.num_layers = decoderConfig.num_hidden_layers; this.dim_kv = decoderConfig.hidden_size / this.num_heads; } } /** * The LLAVA model which consists of a vision backbone and a language model. */ class LlavaForConditionalGeneration extends LlavaPreTrainedModel { async encode_image({ pixel_values }) { // image_inputs === { pixel_values } return (await sessionRun(this.sessions['vision_encoder'], { pixel_values })).image_features; } async encode_text({ input_ids }) { // text_inputs === { input_ids, attention_mask } return (await sessionRun(this.sessions['embed_tokens'], { input_ids })).inputs_embeds; } _merge_input_ids_with_image_features({ inputs_embeds, image_features, input_ids, attention_mask, }) { const image_token_index = this.config.image_token_index; const idsList = input_ids.tolist(); // NOTE: we use .findIndex instead of .indexOf to perform weak comparison (==) between BigInt and Number const indexOfImage = idsList.map(x => x.findIndex(x => x == image_token_index)); const noImages = indexOfImage.every(x => x === -1); const allImages = indexOfImage.every(x => x !== -1); if (!noImages && !allImages) { // Check for padding reasons throw new Error('Every input should contain either 0 or 1 image token.'); } if (noImages) { return { inputs_embeds, attention_mask, position_ids: null, }; } let stacked = []; let stacked_attention_mask = []; for (let i = 0; i < indexOfImage.length; ++i) { const index = indexOfImage[i]; const e = inputs_embeds[i]; const im = image_features[i]; const am = attention_mask[i]; stacked.push( (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat)([ e.slice([0, index]), im, e.slice([index + 1, e.dims[0]]), ], 0) ); stacked_attention_mask.push( (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat)([ am.slice([0, index]), (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones)([im.dims[0]]), am.slice([index + 1, am.dims[0]]) ], 0) ) } return { inputs_embeds: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.stack)(stacked, 0), attention_mask: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.stack)(stacked_attention_mask, 0), position_ids: null, }; } prepare_inputs_for_generation(input_ids, model_inputs, generation_config) { return model_inputs; } /** * * @param {Object} params * @param {Tensor} [params.input_ids=null] * @param {Tensor} [params.attention_mask=null] * @param {Tensor} [params.pixel_values=null] * @param {Tensor} [params.position_ids=null] * @param {Tensor} [params.inputs_embeds=null] * @param {Tensor} [params.past_key_values=null] * @param {Object} [params.generation_config=null] * @param {Object} [params.logits_processor=null] * @returns {Promise} The model's output tensor */ async forward({ // These are produced by the processors: input_ids = null, attention_mask = null, pixel_values = null, // Used during generation: position_ids = null, inputs_embeds = null, past_key_values = null, // Generic generation parameters generation_config = null, logits_processor = null, // TODO: needed? ...kwargs }) { if (!inputs_embeds) { // 1. Extract the input embeddings inputs_embeds = await this.encode_text({ input_ids }); // 2. Possibly, merge text and images if (pixel_values && input_ids.dims[1] !== 1) { const image_features = await this.encode_image({ pixel_values }); ({ inputs_embeds, inputs_embeds, attention_mask, position_ids } = this._merge_input_ids_with_image_features({ image_features, inputs_embeds, input_ids, attention_mask, })); } else if (past_key_values && pixel_values && input_ids.dims[1] === 1) { // In case input_ids.shape[1] == 1 & pixel_values==None & past_key_values != None, we are in the case of // generation with cache const target_length = input_ids.dims[1]; // always 1 const past_length = Object.values(past_key_values)[0].dims.at(-2); attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat)([ (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones)([input_ids.dims[0], past_length]), attention_mask.slice(null, [attention_mask.dims[1] - target_length, attention_mask.dims[1]]), ], 1); } } const outputs = await decoderForward(this, { inputs_embeds, past_key_values, attention_mask, generation_config, logits_processor, }, true); return outputs; } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // CLIP models class CLIPPreTrainedModel extends PreTrainedModel { } /** * CLIP Text and Vision Model with a projection layers on top * * **Example:** Perform zero-shot image classification with a `CLIPModel`. * * ```javascript * import { AutoTokenizer, AutoProcessor, CLIPModel, RawImage } from '@xenova/transformers'; * * // Load tokenizer, processor, and model * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16'); * let processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); * let model = await CLIPModel.from_pretrained('Xenova/clip-vit-base-patch16'); * * // Run tokenization * let texts = ['a photo of a car', 'a photo of a football match'] * let text_inputs = tokenizer(texts, { padding: true, truncation: true }); * * // Read image and run processor * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); * let image_inputs = await processor(image); * * // Run model with both text and pixel inputs * let output = await model({ ...text_inputs, ...image_inputs }); * // { * // logits_per_image: Tensor { * // dims: [ 1, 2 ], * // data: Float32Array(2) [ 18.579734802246094, 24.31830596923828 ], * // }, * // logits_per_text: Tensor { * // dims: [ 2, 1 ], * // data: Float32Array(2) [ 18.579734802246094, 24.31830596923828 ], * // }, * // text_embeds: Tensor { * // dims: [ 2, 512 ], * // data: Float32Array(1024) [ ... ], * // }, * // image_embeds: Tensor { * // dims: [ 1, 512 ], * // data: Float32Array(512) [ ... ], * // } * // } * ``` */ class CLIPModel extends CLIPPreTrainedModel { } /** * CLIP Text Model with a projection layer on top (a linear layer on top of the pooled output) * * **Example:** Compute text embeddings with `CLIPTextModelWithProjection`. * * ```javascript * import { AutoTokenizer, CLIPTextModelWithProjection } from '@xenova/transformers'; * * // Load tokenizer and text model * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16'); * const text_model = await CLIPTextModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16'); * * // Run tokenization * let texts = ['a photo of a car', 'a photo of a football match']; * let text_inputs = tokenizer(texts, { padding: true, truncation: true }); * * // Compute embeddings * const { text_embeds } = await text_model(text_inputs); * // Tensor { * // dims: [ 2, 512 ], * // type: 'float32', * // data: Float32Array(1024) [ ... ], * // size: 1024 * // } * ``` */ class CLIPTextModelWithProjection extends CLIPPreTrainedModel { /** @type {PreTrainedModel.from_pretrained} */ static async from_pretrained(pretrained_model_name_or_path, options = {}) { // Update default model file name if not provided options.model_file_name ??= 'text_model'; return super.from_pretrained(pretrained_model_name_or_path, options); } } /** * CLIP Vision Model with a projection layer on top (a linear layer on top of the pooled output) * * **Example:** Compute vision embeddings with `CLIPVisionModelWithProjection`. * * ```javascript * import { AutoProcessor, CLIPVisionModelWithProjection, RawImage} from '@xenova/transformers'; * * // Load processor and vision model * const processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); * const vision_model = await CLIPVisionModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16'); * * // Read image and run processor * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); * let image_inputs = await processor(image); * * // Compute embeddings * const { image_embeds } = await vision_model(image_inputs); * // Tensor { * // dims: [ 1, 512 ], * // type: 'float32', * // data: Float32Array(512) [ ... ], * // size: 512 * // } * ``` */ class CLIPVisionModelWithProjection extends CLIPPreTrainedModel { /** @type {PreTrainedModel.from_pretrained} */ static async from_pretrained(pretrained_model_name_or_path, options = {}) { // Update default model file name if not provided options.model_file_name ??= 'vision_model'; return super.from_pretrained(pretrained_model_name_or_path, options); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // SigLIP models class SiglipPreTrainedModel extends PreTrainedModel { } /** * SigLIP Text and Vision Model with a projection layers on top * * **Example:** Perform zero-shot image classification with a `SiglipModel`. * * ```javascript * import { AutoTokenizer, AutoProcessor, SiglipModel, RawImage } from '@xenova/transformers'; * * // Load tokenizer, processor, and model * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/siglip-base-patch16-224'); * const processor = await AutoProcessor.from_pretrained('Xenova/siglip-base-patch16-224'); * const model = await SiglipModel.from_pretrained('Xenova/siglip-base-patch16-224'); * * // Run tokenization * const texts = ['a photo of 2 cats', 'a photo of 2 dogs']; * const text_inputs = tokenizer(texts, { padding: 'max_length', truncation: true }); * * // Read image and run processor * const image = await RawImage.read('http://images.cocodataset.org/val2017/000000039769.jpg'); * const image_inputs = await processor(image); * * // Run model with both text and pixel inputs * const output = await model({ ...text_inputs, ...image_inputs }); * // { * // logits_per_image: Tensor { * // dims: [ 1, 2 ], * // data: Float32Array(2) [ -1.6019744873046875, -10.720091819763184 ], * // }, * // logits_per_text: Tensor { * // dims: [ 2, 1 ], * // data: Float32Array(2) [ -1.6019744873046875, -10.720091819763184 ], * // }, * // text_embeds: Tensor { * // dims: [ 2, 768 ], * // data: Float32Array(1536) [ ... ], * // }, * // image_embeds: Tensor { * // dims: [ 1, 768 ], * // data: Float32Array(768) [ ... ], * // } * // } * ``` */ class SiglipModel extends SiglipPreTrainedModel { } /** * The text model from SigLIP without any head or projection on top. * * **Example:** Compute text embeddings with `SiglipTextModel`. * * ```javascript * import { AutoTokenizer, SiglipTextModel } from '@xenova/transformers'; * * // Load tokenizer and text model * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/siglip-base-patch16-224'); * const text_model = await SiglipTextModel.from_pretrained('Xenova/siglip-base-patch16-224'); * * // Run tokenization * const texts = ['a photo of 2 cats', 'a photo of 2 dogs']; * const text_inputs = tokenizer(texts, { padding: 'max_length', truncation: true }); * * // Compute embeddings * const { pooler_output } = await text_model(text_inputs); * // Tensor { * // dims: [ 2, 768 ], * // type: 'float32', * // data: Float32Array(1536) [ ... ], * // size: 1536 * // } * ``` */ class SiglipTextModel extends SiglipPreTrainedModel { /** @type {PreTrainedModel.from_pretrained} */ static async from_pretrained(pretrained_model_name_or_path, options = {}) { // Update default model file name if not provided options.model_file_name ??= 'text_model'; return super.from_pretrained(pretrained_model_name_or_path, options); } } /** * The vision model from SigLIP without any head or projection on top. * * **Example:** Compute vision embeddings with `SiglipVisionModel`. * * ```javascript * import { AutoProcessor, SiglipVisionModel, RawImage} from '@xenova/transformers'; * * // Load processor and vision model * const processor = await AutoProcessor.from_pretrained('Xenova/siglip-base-patch16-224'); * const vision_model = await SiglipVisionModel.from_pretrained('Xenova/siglip-base-patch16-224'); * * // Read image and run processor * const image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); * const image_inputs = await processor(image); * * // Compute embeddings * const { pooler_output } = await vision_model(image_inputs); * // Tensor { * // dims: [ 1, 768 ], * // type: 'float32', * // data: Float32Array(768) [ ... ], * // size: 768 * // } * ``` */ class SiglipVisionModel extends CLIPPreTrainedModel { /** @type {PreTrainedModel.from_pretrained} */ static async from_pretrained(pretrained_model_name_or_path, options = {}) { // Update default model file name if not provided options.model_file_name ??= 'vision_model'; return super.from_pretrained(pretrained_model_name_or_path, options); } } ////////////////////////////////////////////////// // ChineseCLIP models class ChineseCLIPPreTrainedModel extends PreTrainedModel { } class ChineseCLIPModel extends ChineseCLIPPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // CLIPSeg models class CLIPSegPreTrainedModel extends PreTrainedModel { } class CLIPSegModel extends CLIPSegPreTrainedModel { } /** * CLIPSeg model with a Transformer-based decoder on top for zero-shot and one-shot image segmentation. * * **Example:** Perform zero-shot image segmentation with a `CLIPSegForImageSegmentation` model. * * ```javascript * import { AutoTokenizer, AutoProcessor, CLIPSegForImageSegmentation, RawImage } from '@xenova/transformers'; * * // Load tokenizer, processor, and model * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clipseg-rd64-refined'); * const processor = await AutoProcessor.from_pretrained('Xenova/clipseg-rd64-refined'); * const model = await CLIPSegForImageSegmentation.from_pretrained('Xenova/clipseg-rd64-refined'); * * // Run tokenization * const texts = ['a glass', 'something to fill', 'wood', 'a jar']; * const text_inputs = tokenizer(texts, { padding: true, truncation: true }); * * // Read image and run processor * const image = await RawImage.read('https://github.com/timojl/clipseg/blob/master/example_image.jpg?raw=true'); * const image_inputs = await processor(image); * * // Run model with both text and pixel inputs * const { logits } = await model({ ...text_inputs, ...image_inputs }); * // logits: Tensor { * // dims: [4, 352, 352], * // type: 'float32', * // data: Float32Array(495616) [ ... ], * // size: 495616 * // } * ``` * * You can visualize the predictions as follows: * ```javascript * const preds = logits * .unsqueeze_(1) * .sigmoid_() * .mul_(255) * .round_() * .to('uint8'); * * for (let i = 0; i < preds.dims[0]; ++i) { * const img = RawImage.fromTensor(preds[i]); * img.save(`prediction_${i}.png`); * } * ``` */ class CLIPSegForImageSegmentation extends CLIPSegPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // GPT2 models class GPT2PreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `GPT2PreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.n_head this.num_layers = this.config.n_layer this.dim_kv = this.config.n_embd / this.num_heads; } } class GPT2Model extends GPT2PreTrainedModel { } /** * GPT-2 language model head on top of the GPT-2 base model. This model is suitable for text generation tasks. */ class GPT2LMHeadModel extends GPT2PreTrainedModel { } // export class GPT2ForSequenceClassification extends GPT2PreTrainedModel { // TODO // } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // GPTNeo models class GPTNeoPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `GPTNeoPreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.num_heads; this.num_layers = this.config.num_layers; this.dim_kv = this.config.hidden_size / this.num_heads; } } class GPTNeoModel extends GPTNeoPreTrainedModel { } class GPTNeoForCausalLM extends GPTNeoPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // GPTNeoX models class GPTNeoXPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `GPTNeoXPreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.num_attention_heads; this.num_layers = this.config.num_hidden_layers; this.dim_kv = this.config.hidden_size / this.num_heads; } } class GPTNeoXModel extends GPTNeoXPreTrainedModel { } class GPTNeoXForCausalLM extends GPTNeoXPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // GPT-J models class GPTJPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `GPTJPreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.n_head this.num_layers = this.config.n_layer this.dim_kv = this.config.n_embd / this.num_heads; } } class GPTJModel extends GPTJPreTrainedModel { } class GPTJForCausalLM extends GPTJPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // GPTBigCode models class GPTBigCodePreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `GPTBigCodePreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.n_head this.num_layers = this.config.n_layer this.dim_kv = this.config.n_embd / this.num_heads; } } class GPTBigCodeModel extends GPTBigCodePreTrainedModel { } class GPTBigCodeForCausalLM extends GPTBigCodePreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // CodeGen models class CodeGenPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `CodeGenPreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.n_head this.num_layers = this.config.n_layer this.dim_kv = this.config.n_embd / this.num_heads; } } /** * CodeGenModel is a class representing a code generation model without a language model head. */ class CodeGenModel extends CodeGenPreTrainedModel { } /** * CodeGenForCausalLM is a class that represents a code generation model based on the GPT-2 architecture. It extends the `CodeGenPreTrainedModel` class. */ class CodeGenForCausalLM extends CodeGenPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // LLama models /** * The bare LLama Model outputting raw hidden-states without any specific head on top. */ class LlamaPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `LlamaPreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.num_key_value_heads ?? this.config.num_attention_heads this.num_layers = this.config.num_hidden_layers this.dim_kv = this.config.hidden_size / this.config.num_attention_heads } } /** * The bare LLaMA Model outputting raw hidden-states without any specific head on top. */ class LlamaModel extends LlamaPreTrainedModel { } class LlamaForCausalLM extends LlamaPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Qwen2 models /** * The bare Qwen2 Model outputting raw hidden-states without any specific head on top. */ class Qwen2PreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `Qwen2PreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.num_key_value_heads ?? this.config.num_attention_heads this.num_layers = this.config.num_hidden_layers this.dim_kv = this.config.hidden_size / this.config.num_attention_heads } } /** * The bare Qwen2 Model outputting raw hidden-states without any specific head on top. */ class Qwen2Model extends Qwen2PreTrainedModel { } class Qwen2ForCausalLM extends Qwen2PreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Phi models class PhiPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `PhiPreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id; this.num_heads = this.config.num_attention_heads; this.num_layers = this.config.num_hidden_layers; this.dim_kv = this.config.hidden_size / this.num_heads; } } /** * The bare Phi Model outputting raw hidden-states without any specific head on top. */ class PhiModel extends PhiPreTrainedModel { } class PhiForCausalLM extends PhiPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Bloom models /** * The Bloom Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). */ class BloomPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `BloomPreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.n_head this.num_layers = this.config.n_layer this.dim_kv = this.config.hidden_size / this.num_heads; } } /** * The bare Bloom Model transformer outputting raw hidden-states without any specific head on top. */ class BloomModel extends BloomPreTrainedModel { } /** * The Bloom Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). */ class BloomForCausalLM extends BloomPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // MPT models class MptPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `MptPreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.n_heads this.num_layers = this.config.n_layers this.dim_kv = this.config.d_model / this.num_heads; } } /** * The bare Mpt Model transformer outputting raw hidden-states without any specific head on top. */ class MptModel extends MptPreTrainedModel { } /** * The MPT Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). */ class MptForCausalLM extends MptPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // OPT models class OPTPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `OPTPreTrainedModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.num_attention_heads; this.num_layers = this.config.num_hidden_layers; this.dim_kv = this.config.hidden_size / this.num_heads; } } /** * The bare OPT Model outputting raw hidden-states without any specific head on top. */ class OPTModel extends OPTPreTrainedModel { } /** * The OPT Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). */ class OPTForCausalLM extends OPTPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class ViTPreTrainedModel extends PreTrainedModel { } class ViTModel extends ViTPreTrainedModel { } class ViTForImageClassification extends ViTPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class VitMattePreTrainedModel extends PreTrainedModel { } /** * ViTMatte framework leveraging any vision backbone e.g. for ADE20k, CityScapes. * * **Example:** Perform image matting with a `VitMatteForImageMatting` model. * ```javascript * import { AutoProcessor, VitMatteForImageMatting, RawImage } from '@xenova/transformers'; * * // Load processor and model * const processor = await AutoProcessor.from_pretrained('Xenova/vitmatte-small-distinctions-646'); * const model = await VitMatteForImageMatting.from_pretrained('Xenova/vitmatte-small-distinctions-646'); * * // Load image and trimap * const image = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_image.png'); * const trimap = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_trimap.png'); * * // Prepare image + trimap for the model * const inputs = await processor(image, trimap); * * // Predict alpha matte * const { alphas } = await model(inputs); * // Tensor { * // dims: [ 1, 1, 640, 960 ], * // type: 'float32', * // size: 614400, * // data: Float32Array(614400) [ 0.9894027709960938, 0.9970508813858032, ... ] * // } * ``` * * You can visualize the alpha matte as follows: * ```javascript * import { Tensor, cat } from '@xenova/transformers'; * * // Visualize predicted alpha matte * const imageTensor = image.toTensor(); * * // Convert float (0-1) alpha matte to uint8 (0-255) * const alphaChannel = alphas * .squeeze(0) * .mul_(255) * .clamp_(0, 255) * .round_() * .to('uint8'); * * // Concatenate original image with predicted alpha * const imageData = cat([imageTensor, alphaChannel], 0); * * // Save output image * const outputImage = RawImage.fromTensor(imageData); * outputImage.save('output.png'); * ``` */ class VitMatteForImageMatting extends VitMattePreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new ImageMattingOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class MobileViTPreTrainedModel extends PreTrainedModel { } class MobileViTModel extends MobileViTPreTrainedModel { } class MobileViTForImageClassification extends MobileViTPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } // TODO: MobileViTForSemanticSegmentation ////////////////////////////////////////////////// ////////////////////////////////////////////////// class OwlViTPreTrainedModel extends PreTrainedModel { } class OwlViTModel extends OwlViTPreTrainedModel { } class OwlViTForObjectDetection extends OwlViTPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class Owlv2PreTrainedModel extends PreTrainedModel { } class Owlv2Model extends Owlv2PreTrainedModel { } class Owlv2ForObjectDetection extends Owlv2PreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Beit Models class BeitPreTrainedModel extends PreTrainedModel { } class BeitModel extends BeitPreTrainedModel { } class BeitForImageClassification extends BeitPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class DetrPreTrainedModel extends PreTrainedModel { } class DetrModel extends DetrPreTrainedModel { } class DetrForObjectDetection extends DetrPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new DetrObjectDetectionOutput(await super._call(model_inputs)); } } class DetrForSegmentation extends DetrPreTrainedModel { /** * Runs the model with the provided inputs * @param {Object} model_inputs Model inputs * @returns {Promise} Object containing segmentation outputs */ async _call(model_inputs) { return new DetrSegmentationOutput(await super._call(model_inputs)); } } class DetrObjectDetectionOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.logits Classification logits (including no-object) for all queries. * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). */ constructor({ logits, pred_boxes }) { super(); this.logits = logits; this.pred_boxes = pred_boxes; } } class DetrSegmentationOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.logits The output logits of the model. * @param {Tensor} output.pred_boxes Predicted boxes. * @param {Tensor} output.pred_masks Predicted masks. */ constructor({ logits, pred_boxes, pred_masks }) { super(); this.logits = logits; this.pred_boxes = pred_boxes; this.pred_masks = pred_masks; } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class TableTransformerPreTrainedModel extends PreTrainedModel { } /** * The bare Table Transformer Model (consisting of a backbone and encoder-decoder Transformer) * outputting raw hidden-states without any specific head on top. */ class TableTransformerModel extends TableTransformerPreTrainedModel { } /** * Table Transformer Model (consisting of a backbone and encoder-decoder Transformer) * with object detection heads on top, for tasks such as COCO detection. */ class TableTransformerForObjectDetection extends TableTransformerPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new TableTransformerObjectDetectionOutput(await super._call(model_inputs)); } } class TableTransformerObjectDetectionOutput extends DetrObjectDetectionOutput { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class DeiTPreTrainedModel extends PreTrainedModel { } class DeiTModel extends DeiTPreTrainedModel { } class DeiTForImageClassification extends DeiTPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// /** * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. */ class ResNetPreTrainedModel extends PreTrainedModel { } /** * The bare ResNet model outputting raw features without any specific head on top. */ class ResNetModel extends ResNetPreTrainedModel { } /** * ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. */ class ResNetForImageClassification extends ResNetPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class SwinPreTrainedModel extends PreTrainedModel { } class SwinModel extends SwinPreTrainedModel { } class SwinForImageClassification extends SwinPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class Swin2SRPreTrainedModel extends PreTrainedModel { } /** * The bare Swin2SR Model transformer outputting raw hidden-states without any specific head on top. */ class Swin2SRModel extends Swin2SRPreTrainedModel { } /** * Swin2SR Model transformer with an upsampler head on top for image super resolution and restoration. * * **Example:** Super-resolution w/ `Xenova/swin2SR-classical-sr-x2-64`. * * ```javascript * import { AutoProcessor, Swin2SRForImageSuperResolution, RawImage } from '@xenova/transformers'; * * // Load processor and model * const model_id = 'Xenova/swin2SR-classical-sr-x2-64'; * const processor = await AutoProcessor.from_pretrained(model_id); * const model = await Swin2SRForImageSuperResolution.from_pretrained(model_id); * * // Prepare model inputs * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/butterfly.jpg'; * const image = await RawImage.fromURL(url); * const inputs = await processor(image); * * // Run model * const outputs = await model(inputs); * * // Convert Tensor to RawImage * const output = outputs.reconstruction.squeeze().clamp_(0, 1).mul_(255).round_().to('uint8'); * const outputImage = RawImage.fromTensor(output); * // RawImage { * // data: Uint8Array(786432) [ 41, 31, 24, ... ], * // width: 512, * // height: 512, * // channels: 3 * // } * ``` */ class Swin2SRForImageSuperResolution extends Swin2SRPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class DPTPreTrainedModel extends PreTrainedModel { } /** * The bare DPT Model transformer outputting raw hidden-states without any specific head on top. */ class DPTModel extends DPTPreTrainedModel { } /** * DPT Model with a depth estimation head on top (consisting of 3 convolutional layers) e.g. for KITTI, NYUv2. * * **Example:** Depth estimation w/ `Xenova/dpt-hybrid-midas`. * ```javascript * import { DPTForDepthEstimation, AutoProcessor, RawImage, interpolate, max } from '@xenova/transformers'; * * // Load model and processor * const model_id = 'Xenova/dpt-hybrid-midas'; * const model = await DPTForDepthEstimation.from_pretrained(model_id); * const processor = await AutoProcessor.from_pretrained(model_id); * * // Load image from URL * const url = 'http://images.cocodataset.org/val2017/000000039769.jpg'; * const image = await RawImage.fromURL(url); * * // Prepare image for the model * const inputs = await processor(image); * * // Run model * const { predicted_depth } = await model(inputs); * * // Interpolate to original size * const prediction = interpolate(predicted_depth, image.size.reverse(), 'bilinear', false); * * // Visualize the prediction * const formatted = prediction.mul_(255 / max(prediction.data)[0]).to('uint8'); * const depth = RawImage.fromTensor(formatted); * // RawImage { * // data: Uint8Array(307200) [ 85, 85, 84, ... ], * // width: 640, * // height: 480, * // channels: 1 * // } * ``` */ class DPTForDepthEstimation extends DPTPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class DepthAnythingPreTrainedModel extends PreTrainedModel { } /** * Depth Anything Model with a depth estimation head on top (consisting of 3 convolutional layers) e.g. for KITTI, NYUv2. */ class DepthAnythingForDepthEstimation extends DepthAnythingPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class GLPNPreTrainedModel extends PreTrainedModel { } /** * The bare GLPN encoder (Mix-Transformer) outputting raw hidden-states without any specific head on top. */ class GLPNModel extends GLPNPreTrainedModel { } /** * GLPN Model transformer with a lightweight depth estimation head on top e.g. for KITTI, NYUv2. * * **Example:** Depth estimation w/ `Xenova/glpn-kitti`. * ```javascript * import { GLPNForDepthEstimation, AutoProcessor, RawImage, interpolate, max } from '@xenova/transformers'; * * // Load model and processor * const model_id = 'Xenova/glpn-kitti'; * const model = await GLPNForDepthEstimation.from_pretrained(model_id); * const processor = await AutoProcessor.from_pretrained(model_id); * * // Load image from URL * const url = 'http://images.cocodataset.org/val2017/000000039769.jpg'; * const image = await RawImage.fromURL(url); * * // Prepare image for the model * const inputs = await processor(image); * * // Run model * const { predicted_depth } = await model(inputs); * * // Interpolate to original size * const prediction = interpolate(predicted_depth, image.size.reverse(), 'bilinear', false); * * // Visualize the prediction * const formatted = prediction.mul_(255 / max(prediction.data)[0]).to('uint8'); * const depth = RawImage.fromTensor(formatted); * // RawImage { * // data: Uint8Array(307200) [ 207, 169, 154, ... ], * // width: 640, * // height: 480, * // channels: 1 * // } * ``` */ class GLPNForDepthEstimation extends GLPNPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class DonutSwinPreTrainedModel extends PreTrainedModel { } /** * The bare Donut Swin Model transformer outputting raw hidden-states without any specific head on top. * * **Example:** Step-by-step Document Parsing. * * ```javascript * import { AutoProcessor, AutoTokenizer, AutoModelForVision2Seq, RawImage } from '@xenova/transformers'; * * // Choose model to use * const model_id = 'Xenova/donut-base-finetuned-cord-v2'; * * // Prepare image inputs * const processor = await AutoProcessor.from_pretrained(model_id); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/receipt.png'; * const image = await RawImage.read(url); * const image_inputs = await processor(image); * * // Prepare decoder inputs * const tokenizer = await AutoTokenizer.from_pretrained(model_id); * const task_prompt = ''; * const decoder_input_ids = tokenizer(task_prompt, { * add_special_tokens: false, * }).input_ids; * * // Create the model * const model = await AutoModelForVision2Seq.from_pretrained(model_id); * * // Run inference * const output = await model.generate(image_inputs.pixel_values, { * decoder_input_ids, * max_length: model.config.decoder.max_position_embeddings, * }); * * // Decode output * const decoded = tokenizer.batch_decode(output)[0]; * // CINNAMON SUGAR 17,000 1 x 17,000 17,000 17,000 20,000 3,000 * ``` * * **Example:** Step-by-step Document Visual Question Answering (DocVQA) * * ```javascript * import { AutoProcessor, AutoTokenizer, AutoModelForVision2Seq, RawImage } from '@xenova/transformers'; * * // Choose model to use * const model_id = 'Xenova/donut-base-finetuned-docvqa'; * * // Prepare image inputs * const processor = await AutoProcessor.from_pretrained(model_id); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/invoice.png'; * const image = await RawImage.read(url); * const image_inputs = await processor(image); * * // Prepare decoder inputs * const tokenizer = await AutoTokenizer.from_pretrained(model_id); * const question = 'What is the invoice number?'; * const task_prompt = `${question}`; * const decoder_input_ids = tokenizer(task_prompt, { * add_special_tokens: false, * }).input_ids; * * // Create the model * const model = await AutoModelForVision2Seq.from_pretrained(model_id); * * // Run inference * const output = await model.generate(image_inputs.pixel_values, { * decoder_input_ids, * max_length: model.config.decoder.max_position_embeddings, * }); * * // Decode output * const decoded = tokenizer.batch_decode(output)[0]; * // What is the invoice number? us-001 * ``` */ class DonutSwinModel extends DonutSwinPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class ConvNextPreTrainedModel extends PreTrainedModel { } /** * The bare ConvNext model outputting raw features without any specific head on top. */ class ConvNextModel extends ConvNextPreTrainedModel { } /** * ConvNext Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. */ class ConvNextForImageClassification extends ConvNextPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class ConvNextV2PreTrainedModel extends PreTrainedModel { } /** * The bare ConvNextV2 model outputting raw features without any specific head on top. */ class ConvNextV2Model extends ConvNextV2PreTrainedModel { } /** * ConvNextV2 Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. */ class ConvNextV2ForImageClassification extends ConvNextV2PreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class Dinov2PreTrainedModel extends PreTrainedModel { } /** * The bare DINOv2 Model transformer outputting raw hidden-states without any specific head on top. */ class Dinov2Model extends Dinov2PreTrainedModel { } /** * Dinov2 Model transformer with an image classification head on top (a linear layer on top of the final hidden state of the [CLS] token) e.g. for ImageNet. */ class Dinov2ForImageClassification extends Dinov2PreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class YolosPreTrainedModel extends PreTrainedModel { } class YolosModel extends YolosPreTrainedModel { } class YolosForObjectDetection extends YolosPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new YolosObjectDetectionOutput(await super._call(model_inputs)); } } class YolosObjectDetectionOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.logits Classification logits (including no-object) for all queries. * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). */ constructor({ logits, pred_boxes }) { super(); this.logits = logits; this.pred_boxes = pred_boxes; } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class SamPreTrainedModel extends PreTrainedModel { } /** * Segment Anything Model (SAM) for generating segmentation masks, given an input image * and optional 2D location and bounding boxes. * * **Example:** Perform mask generation w/ `Xenova/sam-vit-base`. * ```javascript * import { SamModel, AutoProcessor, RawImage } from '@xenova/transformers'; * * const model = await SamModel.from_pretrained('Xenova/sam-vit-base'); * const processor = await AutoProcessor.from_pretrained('Xenova/sam-vit-base'); * * const img_url = 'https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png'; * const raw_image = await RawImage.read(img_url); * const input_points = [[[450, 600]]] // 2D localization of a window * * const inputs = await processor(raw_image, input_points); * const outputs = await model(inputs); * * const masks = await processor.post_process_masks(outputs.pred_masks, inputs.original_sizes, inputs.reshaped_input_sizes); * // [ * // Tensor { * // dims: [ 1, 3, 1764, 2646 ], * // type: 'bool', * // data: Uint8Array(14002632) [ ... ], * // size: 14002632 * // } * // ] * const scores = outputs.iou_scores; * // Tensor { * // dims: [ 1, 1, 3 ], * // type: 'float32', * // data: Float32Array(3) [ * // 0.8892380595207214, * // 0.9311248064041138, * // 0.983696699142456 * // ], * // size: 3 * // } * ``` */ class SamModel extends SamPreTrainedModel { /** * Compute image embeddings and positional image embeddings, given the pixel values of an image. * @param {Object} model_inputs Object containing the model inputs. * @param {Tensor} model_inputs.pixel_values Pixel values obtained using a `SamProcessor`. * @returns {Promise<{ image_embeddings: Tensor, image_positional_embeddings: Tensor }>} The image embeddings and positional image embeddings. */ async get_image_embeddings({ pixel_values }) { // in: // - pixel_values: tensor.float32[batch_size,3,1024,1024] // // out: // - image_embeddings: tensor.float32[batch_size,256,64,64] // - image_positional_embeddings: tensor.float32[batch_size,256,64,64] return await encoderForward(this, { pixel_values }) } /** * @typedef {Object} SamModelInputs Object containing the model inputs. * @property {Tensor} pixel_values Pixel values as a Tensor with shape `(batch_size, num_channels, height, width)`. * These can be obtained using a `SamProcessor`. * @property {Tensor} input_points Input 2D spatial points with shape `(batch_size, num_points, 2)`. * This is used by the prompt encoder to encode the prompt. * @property {Tensor} [input_labels] Input labels for the points, as a Tensor of shape `(batch_size, point_batch_size, num_points)`. * This is used by the prompt encoder to encode the prompt. There are 4 types of labels: * - `1`: the point is a point that contains the object of interest * - `0`: the point is a point that does not contain the object of interest * - `-1`: the point corresponds to the background * - `-10`: the point is a padding point, thus should be ignored by the prompt encoder * @property {Tensor} [input_boxes] Input bounding boxes with shape `(batch_size, num_boxes, 4)`. * @property {Tensor} [image_embeddings] Image embeddings used by the mask decoder. * @property {Tensor} [image_positional_embeddings] Image positional embeddings used by the mask decoder. */ /** * @param {SamModelInputs} model_inputs Object containing the model inputs. * @returns {Promise} The output of the model. */ async forward(model_inputs) { if (!model_inputs.image_embeddings || !model_inputs.image_positional_embeddings) { // Compute the image embeddings if they are missing model_inputs = { ...model_inputs, ...(await this.get_image_embeddings(model_inputs)) } } if (!model_inputs.input_labels && model_inputs.input_points) { // Set default input labels if they are missing const shape = model_inputs.input_points.dims.slice(0, -1); const numElements = shape.reduce((a, b) => a * b, 1); model_inputs.input_labels = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( 'int64', new BigInt64Array(numElements).fill(1n), shape ); } const decoder_inputs = { image_embeddings: model_inputs.image_embeddings, image_positional_embeddings: model_inputs.image_positional_embeddings, }; if (model_inputs.input_points) { decoder_inputs.input_points = model_inputs.input_points; } if (model_inputs.input_labels) { decoder_inputs.input_labels = model_inputs.input_labels; } if (model_inputs.input_boxes) { decoder_inputs.input_boxes = model_inputs.input_boxes; } // Returns: // - iou_scores: tensor.float32[batch_size,point_batch_size,3] // - pred_masks: tensor.float32[batch_size,point_batch_size,3,256,256] return await sessionRun(this.sessions['prompt_encoder_mask_decoder'], decoder_inputs); } /** * Runs the model with the provided inputs * @param {Object} model_inputs Model inputs * @returns {Promise} Object containing segmentation outputs */ async _call(model_inputs) { return new SamImageSegmentationOutput(await super._call(model_inputs)); } } /** * Base class for Segment-Anything model's output. */ class SamImageSegmentationOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.iou_scores The output logits of the model. * @param {Tensor} output.pred_masks Predicted boxes. */ constructor({ iou_scores, pred_masks }) { super(); this.iou_scores = iou_scores; this.pred_masks = pred_masks; } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // MarianMT models class MarianPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `MarianMTModel` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.decoder_layers; this.num_decoder_heads = this.config.decoder_attention_heads; this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads; this.num_encoder_layers = this.config.encoder_layers; this.num_encoder_heads = this.config.encoder_attention_heads; this.encoder_dim_kv = this.config.d_model / this.num_encoder_heads; } }; class MarianModel extends MarianPreTrainedModel { } class MarianMTModel extends MarianPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // M2M100 models class M2M100PreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `M2M100ForConditionalGeneration` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.decoder_layers; this.num_decoder_heads = this.config.decoder_attention_heads; this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads; this.num_encoder_layers = this.config.encoder_layers; this.num_encoder_heads = this.config.encoder_attention_heads; this.encoder_dim_kv = this.config.d_model / this.num_encoder_heads; } }; class M2M100Model extends M2M100PreTrainedModel { } class M2M100ForConditionalGeneration extends M2M100PreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Wav2Vec2 models class Wav2Vec2PreTrainedModel extends PreTrainedModel { }; /** * The bare Wav2Vec2 Model transformer outputting raw hidden-states without any specific head on top. * * **Example:** Load and run a `Wav2Vec2Model` for feature extraction. * * ```javascript * import { AutoProcessor, AutoModel, read_audio } from '@xenova/transformers'; * * // Read and preprocess audio * const processor = await AutoProcessor.from_pretrained('Xenova/mms-300m'); * const audio = await read_audio('https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac', 16000); * const inputs = await processor(audio); * * // Run model with inputs * const model = await AutoModel.from_pretrained('Xenova/mms-300m'); * const output = await model(inputs); * // { * // last_hidden_state: Tensor { * // dims: [ 1, 1144, 1024 ], * // type: 'float32', * // data: Float32Array(1171456) [ ... ], * // size: 1171456 * // } * // } * ``` */ class Wav2Vec2Model extends Wav2Vec2PreTrainedModel { } class Wav2Vec2ForCTC extends Wav2Vec2PreTrainedModel { /** * @param {Object} model_inputs * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] */ async _call(model_inputs) { return new CausalLMOutput(await super._call(model_inputs)); } } class Wav2Vec2ForSequenceClassification extends Wav2Vec2PreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * Wav2Vec2 Model with a frame classification head on top for tasks like Speaker Diarization. */ class Wav2Vec2ForAudioFrameClassification extends Wav2Vec2PreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // UniSpeech models class UniSpeechPreTrainedModel extends PreTrainedModel { }; /** * The bare UniSpeech Model transformer outputting raw hidden-states without any specific head on top. */ class UniSpeechModel extends UniSpeechPreTrainedModel { } /** * UniSpeech Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). */ class UniSpeechForCTC extends UniSpeechPreTrainedModel { /** * @param {Object} model_inputs * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] */ async _call(model_inputs) { return new CausalLMOutput(await super._call(model_inputs)); } } /** * UniSpeech Model with a sequence classification head on top (a linear layer over the pooled output). */ class UniSpeechForSequenceClassification extends UniSpeechPreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // UniSpeechSat models class UniSpeechSatPreTrainedModel extends PreTrainedModel { }; /** * The bare UniSpeechSat Model transformer outputting raw hidden-states without any specific head on top. */ class UniSpeechSatModel extends UniSpeechSatPreTrainedModel { } /** * UniSpeechSat Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). */ class UniSpeechSatForCTC extends UniSpeechSatPreTrainedModel { /** * @param {Object} model_inputs * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] */ async _call(model_inputs) { return new CausalLMOutput(await super._call(model_inputs)); } } /** * UniSpeechSat Model with a sequence classification head on top (a linear layer over the pooled output). */ class UniSpeechSatForSequenceClassification extends UniSpeechSatPreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * UniSpeechSat Model with a frame classification head on top for tasks like Speaker Diarization. */ class UniSpeechSatForAudioFrameClassification extends UniSpeechSatPreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Wav2Vec2Bert models class Wav2Vec2BertPreTrainedModel extends PreTrainedModel { }; /** * The bare Wav2Vec2Bert Model transformer outputting raw hidden-states without any specific head on top. */ class Wav2Vec2BertModel extends Wav2Vec2BertPreTrainedModel { } /** * Wav2Vec2Bert Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). */ class Wav2Vec2BertForCTC extends Wav2Vec2BertPreTrainedModel { /** * @param {Object} model_inputs * @param {Tensor} model_inputs.input_features Float values of input mel-spectrogram. * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] */ async _call(model_inputs) { return new CausalLMOutput(await super._call(model_inputs)); } } /** * Wav2Vec2Bert Model with a sequence classification head on top (a linear layer over the pooled output). */ class Wav2Vec2BertForSequenceClassification extends Wav2Vec2BertPreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Hubert models class HubertPreTrainedModel extends PreTrainedModel { } /** * The bare Hubert Model transformer outputting raw hidden-states without any specific head on top. * * **Example:** Load and run a `HubertModel` for feature extraction. * * ```javascript * import { AutoProcessor, AutoModel, read_audio } from '@xenova/transformers'; * * // Read and preprocess audio * const processor = await AutoProcessor.from_pretrained('Xenova/hubert-base-ls960'); * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav', 16000); * const inputs = await processor(audio); * * // Load and run model with inputs * const model = await AutoModel.from_pretrained('Xenova/hubert-base-ls960'); * const output = await model(inputs); * // { * // last_hidden_state: Tensor { * // dims: [ 1, 549, 768 ], * // type: 'float32', * // data: Float32Array(421632) [0.0682469978928566, 0.08104046434164047, -0.4975186586380005, ...], * // size: 421632 * // } * // } * ``` */ class HubertModel extends Wav2Vec2PreTrainedModel { } /** * Hubert Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). */ class HubertForCTC extends Wav2Vec2PreTrainedModel { /** * @param {Object} model_inputs * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] */ async _call(model_inputs) { return new CausalLMOutput(await super._call(model_inputs)); } } /** * Hubert Model with a sequence classification head on top (a linear layer over the pooled output) for tasks like SUPERB Keyword Spotting. */ class HubertForSequenceClassification extends Wav2Vec2PreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // WavLM models /** * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. */ class WavLMPreTrainedModel extends PreTrainedModel { }; /** * The bare WavLM Model transformer outputting raw hidden-states without any specific head on top. * * **Example:** Load and run a `WavLMModel` for feature extraction. * * ```javascript * import { AutoProcessor, AutoModel, read_audio } from '@xenova/transformers'; * * // Read and preprocess audio * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base'); * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav', 16000); * const inputs = await processor(audio); * * // Run model with inputs * const model = await AutoModel.from_pretrained('Xenova/wavlm-base'); * const output = await model(inputs); * // { * // last_hidden_state: Tensor { * // dims: [ 1, 549, 768 ], * // type: 'float32', * // data: Float32Array(421632) [-0.349443256855011, -0.39341306686401367, 0.022836603224277496, ...], * // size: 421632 * // } * // } * ``` */ class WavLMModel extends WavLMPreTrainedModel { } /** * WavLM Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). */ class WavLMForCTC extends WavLMPreTrainedModel { /** * @param {Object} model_inputs * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] */ async _call(model_inputs) { return new CausalLMOutput(await super._call(model_inputs)); } } /** * WavLM Model with a sequence classification head on top (a linear layer over the pooled output). */ class WavLMForSequenceClassification extends WavLMPreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } /** * WavLM Model with an XVector feature extraction head on top for tasks like Speaker Verification. * * **Example:** Extract speaker embeddings with `WavLMForXVector`. * ```javascript * import { AutoProcessor, AutoModel, read_audio } from '@xenova/transformers'; * * // Read and preprocess audio * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base-plus-sv'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; * const audio = await read_audio(url, 16000); * const inputs = await processor(audio); * * // Run model with inputs * const model = await AutoModel.from_pretrained('Xenova/wavlm-base-plus-sv'); * const outputs = await model(inputs); * // { * // logits: Tensor { * // dims: [ 1, 512 ], * // type: 'float32', * // data: Float32Array(512) [0.5847219228744507, ...], * // size: 512 * // }, * // embeddings: Tensor { * // dims: [ 1, 512 ], * // type: 'float32', * // data: Float32Array(512) [-0.09079201519489288, ...], * // size: 512 * // } * // } * ``` */ class WavLMForXVector extends WavLMPreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits and speaker embeddings. */ async _call(model_inputs) { return new XVectorOutput(await super._call(model_inputs)); } } /** * WavLM Model with a frame classification head on top for tasks like Speaker Diarization. * * **Example:** Perform speaker diarization with `WavLMForAudioFrameClassification`. * ```javascript * import { AutoProcessor, AutoModelForAudioFrameClassification, read_audio } from '@xenova/transformers'; * * // Read and preprocess audio * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base-plus-sd'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; * const audio = await read_audio(url, 16000); * const inputs = await processor(audio); * * // Run model with inputs * const model = await AutoModelForAudioFrameClassification.from_pretrained('Xenova/wavlm-base-plus-sd'); * const { logits } = await model(inputs); * // { * // logits: Tensor { * // dims: [ 1, 549, 2 ], // [batch_size, num_frames, num_speakers] * // type: 'float32', * // data: Float32Array(1098) [-3.5301010608673096, ...], * // size: 1098 * // } * // } * * const labels = logits[0].sigmoid().tolist().map( * frames => frames.map(speaker => speaker > 0.5 ? 1 : 0) * ); * console.log(labels); // labels is a one-hot array of shape (num_frames, num_speakers) * // [ * // [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], * // [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], * // [0, 0], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], * // ... * // ] * ``` */ class WavLMForAudioFrameClassification extends WavLMPreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} An object containing the model's output logits for sequence classification. */ async _call(model_inputs) { return new TokenClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// // SpeechT5 models /** * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. */ class SpeechT5PreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `SpeechT5ForTextToSpeech` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; this.num_decoder_layers = this.config.decoder_layers; this.num_decoder_heads = this.config.decoder_attention_heads; this.decoder_dim_kv = this.config.hidden_size / this.num_decoder_heads; this.num_encoder_layers = this.config.encoder_layers; this.num_encoder_heads = this.config.encoder_attention_heads; this.encoder_dim_kv = this.config.hidden_size / this.num_encoder_heads; } }; /** * The bare SpeechT5 Encoder-Decoder Model outputting raw hidden-states without any specific pre- or post-nets. */ class SpeechT5Model extends SpeechT5PreTrainedModel { }; /** * SpeechT5 Model with a speech encoder and a text decoder. * * **Example:** Generate speech from text with `SpeechT5ForSpeechToText`. * ```javascript * import { AutoTokenizer, AutoProcessor, SpeechT5ForTextToSpeech, SpeechT5HifiGan, Tensor } from '@xenova/transformers'; * * // Load the tokenizer and processor * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/speecht5_tts'); * const processor = await AutoProcessor.from_pretrained('Xenova/speecht5_tts'); * * // Load the models * // NOTE: We use the full-precision versions as they are more accurate * const model = await SpeechT5ForTextToSpeech.from_pretrained('Xenova/speecht5_tts', { dtype: 'fp32' }); * const vocoder = await SpeechT5HifiGan.from_pretrained('Xenova/speecht5_hifigan', { dtype: 'fp32' }); * * // Load speaker embeddings from URL * const speaker_embeddings_data = new Float32Array( * await (await fetch('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin')).arrayBuffer() * ); * const speaker_embeddings = new Tensor( * 'float32', * speaker_embeddings_data, * [1, speaker_embeddings_data.length] * ) * * // Run tokenization * const { input_ids } = tokenizer('Hello, my dog is cute'); * * // Generate waveform * const { waveform } = await model.generate_speech(input_ids, speaker_embeddings, { vocoder }); * console.log(waveform) * // Tensor { * // dims: [ 26112 ], * // type: 'float32', * // size: 26112, * // data: Float32Array(26112) [ -0.00043630177970044315, -0.00018082228780258447, ... ], * // } * ``` */ class SpeechT5ForSpeechToText extends SpeechT5PreTrainedModel { } /** * SpeechT5 Model with a text encoder and a speech decoder. */ class SpeechT5ForTextToSpeech extends SpeechT5PreTrainedModel { /** * @typedef {Object} SpeechOutput * @property {Tensor} [spectrogram] The predicted log-mel spectrogram of shape * `(output_sequence_length, config.num_mel_bins)`. Returned when no `vocoder` is provided * @property {Tensor} [waveform] The predicted waveform of shape `(num_frames,)`. Returned when a `vocoder` is provided. * @property {Tensor} [cross_attentions] The outputs of the decoder's cross-attention layers of shape * `(config.decoder_layers, config.decoder_attention_heads, output_sequence_length, input_sequence_length)`. returned when `output_cross_attentions` is `true`. */ /** * Converts a sequence of input tokens into a sequence of mel spectrograms, which are subsequently turned into a speech waveform using a vocoder. * @param {Tensor} input_values Indices of input sequence tokens in the vocabulary. * @param {Tensor} speaker_embeddings Tensor containing the speaker embeddings. * @param {Object} options Optional parameters for generating speech. * @param {number} [options.threshold=0.5] The generated sequence ends when the predicted stop token probability exceeds this value. * @param {number} [options.minlenratio=0.0] Used to calculate the minimum required length for the output sequence. * @param {number} [options.maxlenratio=20.0] Used to calculate the maximum allowed length for the output sequence. * @param {Object} [options.vocoder=null] The vocoder that converts the mel spectrogram into a speech waveform. If `null`, the output is the mel spectrogram. * @param {boolean} [options.output_cross_attentions=false] Whether or not to return the attentions tensors of the decoder's cross-attention layers. * @returns {Promise} A promise which resolves to an object containing the spectrogram, waveform, and cross-attention tensors. */ async generate_speech(input_values, speaker_embeddings, { threshold = 0.5, minlenratio = 0.0, maxlenratio = 20.0, vocoder = null, // output_cross_attentions = false, // TODO add } = {}) { const model_inputs = { input_ids: input_values } const { encoder_outputs, encoder_attention_mask } = await encoderForward(this, model_inputs); const r = encoder_outputs.dims[1] / this.config.reduction_factor; const maxlen = Math.floor(r * maxlenratio); const minlen = Math.floor(r * minlenratio); const num_mel_bins = this.config.num_mel_bins; let spectrogramParts = []; let past_key_values = null; let decoder_outputs = null; let idx = 0; while (true) { ++idx; const use_cache_branch = boolTensor(!!decoder_outputs); let output_sequence; if (decoder_outputs) { output_sequence = decoder_outputs.output_sequence_out; } else { output_sequence = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( 'float32', new Float32Array(num_mel_bins), [1, 1, num_mel_bins], ) } let decoderFeeds = { use_cache_branch, output_sequence, encoder_attention_mask: encoder_attention_mask, speaker_embeddings: speaker_embeddings, encoder_hidden_states: encoder_outputs, }; this.addPastKeyValues(decoderFeeds, past_key_values); decoder_outputs = await sessionRun(this.sessions['decoder_model_merged'], decoderFeeds); past_key_values = this.getPastKeyValues(decoder_outputs, past_key_values); const { prob, spectrum } = decoder_outputs; spectrogramParts.push(spectrum); if (idx >= minlen && ( // Finished when stop token or maximum length is reached. Array.from(prob.data).filter(p => p >= threshold).length > 0 || idx >= maxlen )) { break; } } const spectrogram = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat)(spectrogramParts); const { waveform } = await sessionRun(vocoder.sessions['model'], { spectrogram }); return { spectrogram, waveform, // cross_attentions: null, // TODO add } } } /** * HiFi-GAN vocoder. * * See [SpeechT5ForSpeechToText](./models#module_models.SpeechT5ForSpeechToText) for example usage. */ class SpeechT5HifiGan extends PreTrainedModel { main_input_name = 'spectrogram'; } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // TrOCR models class TrOCRPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `TrOCRPreTrainedModel` class. * @param {Object} config The configuration of the model. * @param {any} session The ONNX session containing the model weights. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, session, generation_config) { super(config, session); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id; this.num_encoder_layers = this.num_decoder_layers = this.config.decoder_layers; this.num_encoder_heads = this.num_decoder_heads = this.config.decoder_attention_heads; this.encoder_dim_kv = this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads; } } /** * The TrOCR Decoder with a language modeling head. */ class TrOCRForCausalLM extends TrOCRPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Mistral models /** * The bare Mistral Model outputting raw hidden-states without any specific head on top. */ class MistralPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `MistralPreTrainedModel` class. * @param {Object} config The configuration of the model. * @param {any} session The ONNX session containing the model weights. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, session, generation_config) { super(config, session); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.num_key_value_heads; this.num_layers = this.config.num_hidden_layers; this.dim_kv = this.config.hidden_size / this.config.num_attention_heads; } } class MistralModel extends MistralPreTrainedModel { } class MistralForCausalLM extends MistralPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Starcoder2 models /** * The bare Starcoder2 Model outputting raw hidden-states without any specific head on top. */ class Starcoder2PreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `Starcoder2PreTrainedModel` class. * @param {Object} config The configuration of the model. * @param {any} session The ONNX session containing the model weights. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, session, generation_config) { super(config, session); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.num_key_value_heads; this.num_layers = this.config.num_hidden_layers; this.dim_kv = this.config.hidden_size / this.config.num_attention_heads; } } class Starcoder2Model extends Starcoder2PreTrainedModel { } class Starcoder2ForCausalLM extends Starcoder2PreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Falcon models /** * The bare Falcon Model outputting raw hidden-states without any specific head on top. */ class FalconPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `FalconPreTrainedModel` class. * @param {Object} config The configuration of the model. * @param {any} session The ONNX session containing the model weights. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, session, generation_config) { super(config, session); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.num_attention_heads; this.num_layers = this.config.num_hidden_layers; this.dim_kv = this.config.hidden_size / this.config.num_attention_heads; } } class FalconModel extends FalconPreTrainedModel { } class FalconForCausalLM extends FalconPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // CLAP models class ClapPreTrainedModel extends PreTrainedModel { } class ClapModel extends ClapPreTrainedModel { } /** * CLAP Text Model with a projection layer on top (a linear layer on top of the pooled output). * * **Example:** Compute text embeddings with `ClapTextModelWithProjection`. * * ```javascript * import { AutoTokenizer, ClapTextModelWithProjection } from '@xenova/transformers'; * * // Load tokenizer and text model * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clap-htsat-unfused'); * const text_model = await ClapTextModelWithProjection.from_pretrained('Xenova/clap-htsat-unfused'); * * // Run tokenization * const texts = ['a sound of a cat', 'a sound of a dog']; * const text_inputs = tokenizer(texts, { padding: true, truncation: true }); * * // Compute embeddings * const { text_embeds } = await text_model(text_inputs); * // Tensor { * // dims: [ 2, 512 ], * // type: 'float32', * // data: Float32Array(1024) [ ... ], * // size: 1024 * // } * ``` */ class ClapTextModelWithProjection extends ClapPreTrainedModel { /** @type {PreTrainedModel.from_pretrained} */ static async from_pretrained(pretrained_model_name_or_path, options = {}) { // Update default model file name if not provided options.model_file_name ??= 'text_model'; return super.from_pretrained(pretrained_model_name_or_path, options); } } /** * CLAP Audio Model with a projection layer on top (a linear layer on top of the pooled output). * * **Example:** Compute audio embeddings with `ClapAudioModelWithProjection`. * * ```javascript * import { AutoProcessor, ClapAudioModelWithProjection, read_audio } from '@xenova/transformers'; * * // Load processor and audio model * const processor = await AutoProcessor.from_pretrained('Xenova/clap-htsat-unfused'); * const audio_model = await ClapAudioModelWithProjection.from_pretrained('Xenova/clap-htsat-unfused'); * * // Read audio and run processor * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cat_meow.wav'); * const audio_inputs = await processor(audio); * * // Compute embeddings * const { audio_embeds } = await audio_model(audio_inputs); * // Tensor { * // dims: [ 1, 512 ], * // type: 'float32', * // data: Float32Array(512) [ ... ], * // size: 512 * // } * ``` */ class ClapAudioModelWithProjection extends ClapPreTrainedModel { /** @type {PreTrainedModel.from_pretrained} */ static async from_pretrained(pretrained_model_name_or_path, options = {}) { // Update default model file name if not provided options.model_file_name ??= 'audio_model'; return super.from_pretrained(pretrained_model_name_or_path, options); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // VITS models class VitsPreTrainedModel extends PreTrainedModel { } /** * The complete VITS model, for text-to-speech synthesis. * * **Example:** Generate speech from text with `VitsModel`. * ```javascript * import { AutoTokenizer, VitsModel } from '@xenova/transformers'; * * // Load the tokenizer and model * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/mms-tts-eng'); * const model = await VitsModel.from_pretrained('Xenova/mms-tts-eng'); * * // Run tokenization * const inputs = tokenizer('I love transformers'); * * // Generate waveform * const { waveform } = await model(inputs); * // Tensor { * // dims: [ 1, 35328 ], * // type: 'float32', * // data: Float32Array(35328) [ ... ], * // size: 35328, * // } * ``` */ class VitsModel extends VitsPreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. * @returns {Promise} The outputs for the VITS model. */ async _call(model_inputs) { return new VitsModelOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Segformer models class SegformerPreTrainedModel extends PreTrainedModel { } /** * The bare SegFormer encoder (Mix-Transformer) outputting raw hidden-states without any specific head on top. */ class SegformerModel extends SegformerPreTrainedModel { } /** * SegFormer Model transformer with an image classification head on top (a linear layer on top of the final hidden states) e.g. for ImageNet. */ class SegformerForImageClassification extends SegformerPreTrainedModel { } /** * SegFormer Model transformer with an all-MLP decode head on top e.g. for ADE20k, CityScapes. */ class SegformerForSemanticSegmentation extends SegformerPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // StableLm models class StableLmPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `StableLmPreTrainedModel` class. * @param {Object} config The configuration of the model. * @param {any} session The ONNX session containing the model weights. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, session, generation_config) { super(config, session); this.generation_config = generation_config; // config doesn't contain pad_token_id, so we assume it is the eos_token_id // this.config.pad_token_id = this.config.eos_token_id this.num_heads = this.config.num_attention_heads; this.num_layers = this.config.num_hidden_layers; this.dim_kv = this.config.hidden_size / this.num_heads; } } /** * The bare StableLm Model transformer outputting raw hidden-states without any specific head on top. */ class StableLmModel extends StableLmPreTrainedModel { } /** * StableLm Model with a `language modeling` head on top for Causal Language Modeling (with past). */ class StableLmForCausalLM extends StableLmPreTrainedModel { } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class EfficientNetPreTrainedModel extends PreTrainedModel { } /** * The bare EfficientNet model outputting raw features without any specific head on top. */ class EfficientNetModel extends EfficientNetPreTrainedModel { } /** * EfficientNet Model with an image classification head on top (a linear layer on top of the pooled features). */ class EfficientNetForImageClassification extends EfficientNetPreTrainedModel { /** * @param {any} model_inputs */ async _call(model_inputs) { return new SequenceClassifierOutput(await super._call(model_inputs)); } } ////////////////////////////////////////////////// ////////////////////////////////////////////////// // Musicgen models class MusicgenPreTrainedModel extends PreTrainedModel { } /** * The bare Musicgen decoder model outputting raw hidden-states without any specific head on top. */ class MusicgenModel extends MusicgenPreTrainedModel { } /** * The MusicGen decoder model with a language modelling head on top. */ class MusicgenForCausalLM extends MusicgenPreTrainedModel { } /** * The composite MusicGen model with a text encoder, audio encoder and Musicgen decoder, * for music generation tasks with one or both of text and audio prompts. * * **Example:** Generate music from text with `Xenova/musicgen-small`. * ```javascript * import { AutoTokenizer, MusicgenForConditionalGeneration } from '@xenova/transformers'; * * // Load tokenizer and model * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/musicgen-small'); * const model = await MusicgenForConditionalGeneration.from_pretrained( * 'Xenova/musicgen-small', { dtype: 'fp32' } * ); * * // Prepare text input * const prompt = '80s pop track with bassy drums and synth'; * const inputs = tokenizer(prompt); * * // Generate audio * const audio_values = await model.generate({ * ...inputs, * max_new_tokens: 512, * do_sample: true, * guidance_scale: 3, * }); * * // (Optional) Write the output to a WAV file * import wavefile from 'wavefile'; * import fs from 'fs'; * * const wav = new wavefile.WaveFile(); * wav.fromScratch(1, model.config.audio_encoder.sampling_rate, '32f', audio_values.data); * fs.writeFileSync('musicgen_out.wav', wav.toBuffer()); * ``` */ class MusicgenForConditionalGeneration extends PreTrainedModel { // NOTE: not MusicgenPreTrainedModel forward_params = ['input_ids', 'attention_mask', 'encoder_outputs', 'decoder_input_ids', 'decoder_attention_mask', 'past_key_values']; /** * Creates a new instance of the `MusicgenForConditionalGeneration` class. * @param {Object} config The model configuration. * @param {Record} sessions The inference sessions for the model. * @param {GenerationConfig} generation_config The generation configuration. */ constructor(config, sessions, generation_config) { super(config, sessions); this.generation_config = generation_config; // decoder const decoderConfig = config.decoder; this.num_encoder_layers = this.num_decoder_layers = decoderConfig.num_hidden_layers; this.num_encoder_heads = this.num_decoder_heads = decoderConfig.num_attention_heads; this.encoder_dim_kv = this.decoder_dim_kv = decoderConfig.hidden_size / this.num_decoder_heads; } /** * Apply the pattern mask to the final ids, * then revert the pattern delay mask by filtering the pad token id in a single step. * @param {Tensor} outputs The output tensor from the model. * @returns {Tensor} The filtered output tensor. */ _apply_and_filter_by_delay_pattern_mask(outputs) { const [bs_x_codebooks, seqLength] = outputs.dims; const num_codebooks = this.config.decoder.num_codebooks; const upperBound = (seqLength - num_codebooks); let newDataSize = 0; for (let i = 0; i < outputs.size; ++i) { if (outputs.data[i] === this.config.decoder.pad_token_id) { continue; } const row = (i % seqLength); const col = Math.floor(i / seqLength) % num_codebooks; const diff = row - col; if (diff > 0 && diff <= upperBound) { outputs.data[newDataSize++] = outputs.data[i]; } } const batch_size = Math.floor(bs_x_codebooks / num_codebooks); const inferred = newDataSize / (batch_size * num_codebooks); // TODO: assert `inferred` is an integer return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( outputs.type, outputs.data.slice(0, newDataSize), [batch_size, num_codebooks, inferred] ); } prepare_inputs_for_generation(input_ids, model_inputs, generation_config) { // apply the delay pattern mask let clonedInputIds = structuredClone(input_ids); for (let i = 0; i < clonedInputIds.length; ++i) { for (let j = 0; j < clonedInputIds[i].length; ++j) { if ((i % this.config.decoder.num_codebooks) >= j) { clonedInputIds[i][j] = BigInt(this.config.decoder.pad_token_id); } } } // for classifier free guidance we need to replicate the decoder args across the batch dim // (we'll split these before sampling) if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { // [batch, seqLength] -> [2 * batch, seqLength] clonedInputIds = clonedInputIds.concat(clonedInputIds); } const prepped = super.prepare_inputs_for_generation(clonedInputIds, model_inputs, generation_config); return prepped; } /** * Generates sequences of token ids for models with a language modeling head. * @param {import('./generation/parameters.js').GenerationFunctionParameters} options * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. */ async generate(options) { const output_ids = await super.generate(options); // apply the pattern mask to the final ids // tensor: int64[1,batch_size,4,chunk_length] const audio_codes = this._apply_and_filter_by_delay_pattern_mask( /** @type {Tensor} */(output_ids) ).unsqueeze_(0); // append the frame dimension back to the audio codes const { audio_values } = await sessionRun(this.sessions['encodec_decode'], { audio_codes }) return audio_values; } } ////////////////////////////////////////////////// // AutoModels, used to simplify construction of PreTrainedModels // (uses config to instantiate correct class) /** * Base class of all AutoModels. Contains the `from_pretrained` function * which is used to instantiate pretrained models. */ class PretrainedMixin { /** * Mapping from model type to model class. * @type {Map[]} */ static MODEL_CLASS_MAPPINGS = null; /** * Whether to attempt to instantiate the base class (`PretrainedModel`) if * the model type is not found in the mapping. */ static BASE_IF_FAIL = false; /** @type {typeof PreTrainedModel.from_pretrained} */ static async from_pretrained(pretrained_model_name_or_path, { progress_callback = null, config = null, cache_dir = null, local_files_only = false, revision = 'main', model_file_name = null, subfolder = 'onnx', device = null, dtype = null, session_options = {}, } = {}) { let options = { progress_callback, config, cache_dir, local_files_only, revision, model_file_name, subfolder, device, dtype, session_options, } config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); if (!options.config) { // If no config was passed, reuse this config for future processing options.config = config; } if (!this.MODEL_CLASS_MAPPINGS) { throw new Error("`MODEL_CLASS_MAPPINGS` not implemented for this type of `AutoClass`: " + this.name); } for (let MODEL_CLASS_MAPPING of this.MODEL_CLASS_MAPPINGS) { const modelInfo = MODEL_CLASS_MAPPING.get(config.model_type); if (!modelInfo) { continue; // Item not found in this mapping } return await modelInfo[1].from_pretrained(pretrained_model_name_or_path, options); } if (this.BASE_IF_FAIL) { console.warn(`Unknown model class "${config.model_type}", attempting to construct from base class.`); return await PreTrainedModel.from_pretrained(pretrained_model_name_or_path, options); } else { throw Error(`Unsupported model type: ${config.model_type}`) } } } const MODEL_MAPPING_NAMES_ENCODER_ONLY = new Map([ ['bert', ['BertModel', BertModel]], ['nomic_bert', ['NomicBertModel', NomicBertModel]], ['roformer', ['RoFormerModel', RoFormerModel]], ['electra', ['ElectraModel', ElectraModel]], ['esm', ['EsmModel', EsmModel]], ['convbert', ['ConvBertModel', ConvBertModel]], ['camembert', ['CamembertModel', CamembertModel]], ['deberta', ['DebertaModel', DebertaModel]], ['deberta-v2', ['DebertaV2Model', DebertaV2Model]], ['mpnet', ['MPNetModel', MPNetModel]], ['albert', ['AlbertModel', AlbertModel]], ['distilbert', ['DistilBertModel', DistilBertModel]], ['roberta', ['RobertaModel', RobertaModel]], ['xlm', ['XLMModel', XLMModel]], ['xlm-roberta', ['XLMRobertaModel', XLMRobertaModel]], ['clap', ['ClapModel', ClapModel]], ['clip', ['CLIPModel', CLIPModel]], ['clipseg', ['CLIPSegModel', CLIPSegModel]], ['chinese_clip', ['ChineseCLIPModel', ChineseCLIPModel]], ['siglip', ['SiglipModel', SiglipModel]], ['mobilebert', ['MobileBertModel', MobileBertModel]], ['squeezebert', ['SqueezeBertModel', SqueezeBertModel]], ['wav2vec2', ['Wav2Vec2Model', Wav2Vec2Model]], ['wav2vec2-bert', ['Wav2Vec2BertModel', Wav2Vec2BertModel]], ['unispeech', ['UniSpeechModel', UniSpeechModel]], ['unispeech-sat', ['UniSpeechSatModel', UniSpeechSatModel]], ['hubert', ['HubertModel', HubertModel]], ['wavlm', ['WavLMModel', WavLMModel]], ['audio-spectrogram-transformer', ['ASTModel', ASTModel]], ['vits', ['VitsModel', VitsModel]], ['detr', ['DetrModel', DetrModel]], ['table-transformer', ['TableTransformerModel', TableTransformerModel]], ['vit', ['ViTModel', ViTModel]], ['mobilevit', ['MobileViTModel', MobileViTModel]], ['owlvit', ['OwlViTModel', OwlViTModel]], ['owlv2', ['Owlv2Model', Owlv2Model]], ['beit', ['BeitModel', BeitModel]], ['deit', ['DeiTModel', DeiTModel]], ['convnext', ['ConvNextModel', ConvNextModel]], ['convnextv2', ['ConvNextV2Model', ConvNextV2Model]], ['dinov2', ['Dinov2Model', Dinov2Model]], ['resnet', ['ResNetModel', ResNetModel]], ['swin', ['SwinModel', SwinModel]], ['swin2sr', ['Swin2SRModel', Swin2SRModel]], ['donut-swin', ['DonutSwinModel', DonutSwinModel]], ['yolos', ['YolosModel', YolosModel]], ['dpt', ['DPTModel', DPTModel]], ['glpn', ['GLPNModel', GLPNModel]], ['hifigan', ['SpeechT5HifiGan', SpeechT5HifiGan]], ['efficientnet', ['EfficientNetModel', EfficientNetModel]], ]); const MODEL_MAPPING_NAMES_ENCODER_DECODER = new Map([ ['t5', ['T5Model', T5Model]], ['longt5', ['LongT5Model', LongT5Model]], ['mt5', ['MT5Model', MT5Model]], ['bart', ['BartModel', BartModel]], ['mbart', ['MBartModel', MBartModel]], ['marian', ['MarianModel', MarianModel]], ['whisper', ['WhisperModel', WhisperModel]], ['m2m_100', ['M2M100Model', M2M100Model]], ['blenderbot', ['BlenderbotModel', BlenderbotModel]], ['blenderbot-small', ['BlenderbotSmallModel', BlenderbotSmallModel]], ]); const MODEL_MAPPING_NAMES_DECODER_ONLY = new Map([ ['bloom', ['BloomModel', BloomModel]], ['gpt2', ['GPT2Model', GPT2Model]], ['gptj', ['GPTJModel', GPTJModel]], ['gpt_bigcode', ['GPTBigCodeModel', GPTBigCodeModel]], ['gpt_neo', ['GPTNeoModel', GPTNeoModel]], ['gpt_neox', ['GPTNeoXModel', GPTNeoXModel]], ['codegen', ['CodeGenModel', CodeGenModel]], ['llama', ['LlamaModel', LlamaModel]], ['qwen2', ['Qwen2Model', Qwen2Model]], ['phi', ['PhiModel', PhiModel]], ['mpt', ['MptModel', MptModel]], ['opt', ['OPTModel', OPTModel]], ['mistral', ['MistralModel', MistralModel]], ['starcoder2', ['Starcoder2Model', Starcoder2Model]], ['falcon', ['FalconModel', FalconModel]], ]); const MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES = new Map([ ['speecht5', ['SpeechT5ForSpeechToText', SpeechT5ForSpeechToText]], ['whisper', ['WhisperForConditionalGeneration', WhisperForConditionalGeneration]], ]); const MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES = new Map([ ['speecht5', ['SpeechT5ForTextToSpeech', SpeechT5ForTextToSpeech]], ]); const MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES = new Map([ ['vits', ['VitsModel', VitsModel]], ['musicgen', ['MusicgenForConditionalGeneration', MusicgenForConditionalGeneration]], ]); const MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES = new Map([ ['bert', ['BertForSequenceClassification', BertForSequenceClassification]], ['roformer', ['RoFormerForSequenceClassification', RoFormerForSequenceClassification]], ['electra', ['ElectraForSequenceClassification', ElectraForSequenceClassification]], ['esm', ['EsmForSequenceClassification', EsmForSequenceClassification]], ['convbert', ['ConvBertForSequenceClassification', ConvBertForSequenceClassification]], ['camembert', ['CamembertForSequenceClassification', CamembertForSequenceClassification]], ['deberta', ['DebertaForSequenceClassification', DebertaForSequenceClassification]], ['deberta-v2', ['DebertaV2ForSequenceClassification', DebertaV2ForSequenceClassification]], ['mpnet', ['MPNetForSequenceClassification', MPNetForSequenceClassification]], ['albert', ['AlbertForSequenceClassification', AlbertForSequenceClassification]], ['distilbert', ['DistilBertForSequenceClassification', DistilBertForSequenceClassification]], ['roberta', ['RobertaForSequenceClassification', RobertaForSequenceClassification]], ['xlm', ['XLMForSequenceClassification', XLMForSequenceClassification]], ['xlm-roberta', ['XLMRobertaForSequenceClassification', XLMRobertaForSequenceClassification]], ['bart', ['BartForSequenceClassification', BartForSequenceClassification]], ['mbart', ['MBartForSequenceClassification', MBartForSequenceClassification]], ['mobilebert', ['MobileBertForSequenceClassification', MobileBertForSequenceClassification]], ['squeezebert', ['SqueezeBertForSequenceClassification', SqueezeBertForSequenceClassification]], ]); const MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES = new Map([ ['bert', ['BertForTokenClassification', BertForTokenClassification]], ['roformer', ['RoFormerForTokenClassification', RoFormerForTokenClassification]], ['electra', ['ElectraForTokenClassification', ElectraForTokenClassification]], ['esm', ['EsmForTokenClassification', EsmForTokenClassification]], ['convbert', ['ConvBertForTokenClassification', ConvBertForTokenClassification]], ['camembert', ['CamembertForTokenClassification', CamembertForTokenClassification]], ['deberta', ['DebertaForTokenClassification', DebertaForTokenClassification]], ['deberta-v2', ['DebertaV2ForTokenClassification', DebertaV2ForTokenClassification]], ['mpnet', ['MPNetForTokenClassification', MPNetForTokenClassification]], ['distilbert', ['DistilBertForTokenClassification', DistilBertForTokenClassification]], ['roberta', ['RobertaForTokenClassification', RobertaForTokenClassification]], ['xlm', ['XLMForTokenClassification', XLMForTokenClassification]], ['xlm-roberta', ['XLMRobertaForTokenClassification', XLMRobertaForTokenClassification]], ]); const MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES = new Map([ ['t5', ['T5ForConditionalGeneration', T5ForConditionalGeneration]], ['longt5', ['LongT5ForConditionalGeneration', LongT5ForConditionalGeneration]], ['mt5', ['MT5ForConditionalGeneration', MT5ForConditionalGeneration]], ['bart', ['BartForConditionalGeneration', BartForConditionalGeneration]], ['mbart', ['MBartForConditionalGeneration', MBartForConditionalGeneration]], ['marian', ['MarianMTModel', MarianMTModel]], ['m2m_100', ['M2M100ForConditionalGeneration', M2M100ForConditionalGeneration]], ['blenderbot', ['BlenderbotForConditionalGeneration', BlenderbotForConditionalGeneration]], ['blenderbot-small', ['BlenderbotSmallForConditionalGeneration', BlenderbotSmallForConditionalGeneration]], ]); const MODEL_FOR_CAUSAL_LM_MAPPING_NAMES = new Map([ ['bloom', ['BloomForCausalLM', BloomForCausalLM]], ['gpt2', ['GPT2LMHeadModel', GPT2LMHeadModel]], ['gptj', ['GPTJForCausalLM', GPTJForCausalLM]], ['gpt_bigcode', ['GPTBigCodeForCausalLM', GPTBigCodeForCausalLM]], ['gpt_neo', ['GPTNeoForCausalLM', GPTNeoForCausalLM]], ['gpt_neox', ['GPTNeoXForCausalLM', GPTNeoXForCausalLM]], ['codegen', ['CodeGenForCausalLM', CodeGenForCausalLM]], ['llama', ['LlamaForCausalLM', LlamaForCausalLM]], ['qwen2', ['Qwen2ForCausalLM', Qwen2ForCausalLM]], ['phi', ['PhiForCausalLM', PhiForCausalLM]], ['mpt', ['MptForCausalLM', MptForCausalLM]], ['opt', ['OPTForCausalLM', OPTForCausalLM]], ['mbart', ['MBartForCausalLM', MBartForCausalLM]], ['mistral', ['MistralForCausalLM', MistralForCausalLM]], ['starcoder2', ['Starcoder2ForCausalLM', Starcoder2ForCausalLM]], ['falcon', ['FalconForCausalLM', FalconForCausalLM]], ['trocr', ['TrOCRForCausalLM', TrOCRForCausalLM]], ['stablelm', ['StableLmForCausalLM', StableLmForCausalLM]], ]); const MODEL_FOR_MASKED_LM_MAPPING_NAMES = new Map([ ['bert', ['BertForMaskedLM', BertForMaskedLM]], ['roformer', ['RoFormerForMaskedLM', RoFormerForMaskedLM]], ['electra', ['ElectraForMaskedLM', ElectraForMaskedLM]], ['esm', ['EsmForMaskedLM', EsmForMaskedLM]], ['convbert', ['ConvBertForMaskedLM', ConvBertForMaskedLM]], ['camembert', ['CamembertForMaskedLM', CamembertForMaskedLM]], ['deberta', ['DebertaForMaskedLM', DebertaForMaskedLM]], ['deberta-v2', ['DebertaV2ForMaskedLM', DebertaV2ForMaskedLM]], ['mpnet', ['MPNetForMaskedLM', MPNetForMaskedLM]], ['albert', ['AlbertForMaskedLM', AlbertForMaskedLM]], ['distilbert', ['DistilBertForMaskedLM', DistilBertForMaskedLM]], ['roberta', ['RobertaForMaskedLM', RobertaForMaskedLM]], ['xlm', ['XLMWithLMHeadModel', XLMWithLMHeadModel]], ['xlm-roberta', ['XLMRobertaForMaskedLM', XLMRobertaForMaskedLM]], ['mobilebert', ['MobileBertForMaskedLM', MobileBertForMaskedLM]], ['squeezebert', ['SqueezeBertForMaskedLM', SqueezeBertForMaskedLM]], ]); const MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES = new Map([ ['bert', ['BertForQuestionAnswering', BertForQuestionAnswering]], ['roformer', ['RoFormerForQuestionAnswering', RoFormerForQuestionAnswering]], ['electra', ['ElectraForQuestionAnswering', ElectraForQuestionAnswering]], ['convbert', ['ConvBertForQuestionAnswering', ConvBertForQuestionAnswering]], ['camembert', ['CamembertForQuestionAnswering', CamembertForQuestionAnswering]], ['deberta', ['DebertaForQuestionAnswering', DebertaForQuestionAnswering]], ['deberta-v2', ['DebertaV2ForQuestionAnswering', DebertaV2ForQuestionAnswering]], ['mpnet', ['MPNetForQuestionAnswering', MPNetForQuestionAnswering]], ['albert', ['AlbertForQuestionAnswering', AlbertForQuestionAnswering]], ['distilbert', ['DistilBertForQuestionAnswering', DistilBertForQuestionAnswering]], ['roberta', ['RobertaForQuestionAnswering', RobertaForQuestionAnswering]], ['xlm', ['XLMForQuestionAnswering', XLMForQuestionAnswering]], ['xlm-roberta', ['XLMRobertaForQuestionAnswering', XLMRobertaForQuestionAnswering]], ['mobilebert', ['MobileBertForQuestionAnswering', MobileBertForQuestionAnswering]], ['squeezebert', ['SqueezeBertForQuestionAnswering', SqueezeBertForQuestionAnswering]], ]); const MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES = new Map([ ['vision-encoder-decoder', ['VisionEncoderDecoderModel', VisionEncoderDecoderModel]], ]); const MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES = new Map([ ['llava', ['LlavaForConditionalGeneration', LlavaForConditionalGeneration]], ]); const MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES = new Map([ ['vision-encoder-decoder', ['VisionEncoderDecoderModel', VisionEncoderDecoderModel]], ]); const MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES = new Map([ ['vit', ['ViTForImageClassification', ViTForImageClassification]], ['mobilevit', ['MobileViTForImageClassification', MobileViTForImageClassification]], ['beit', ['BeitForImageClassification', BeitForImageClassification]], ['deit', ['DeiTForImageClassification', DeiTForImageClassification]], ['convnext', ['ConvNextForImageClassification', ConvNextForImageClassification]], ['convnextv2', ['ConvNextV2ForImageClassification', ConvNextV2ForImageClassification]], ['dinov2', ['Dinov2ForImageClassification', Dinov2ForImageClassification]], ['resnet', ['ResNetForImageClassification', ResNetForImageClassification]], ['swin', ['SwinForImageClassification', SwinForImageClassification]], ['segformer', ['SegformerForImageClassification', SegformerForImageClassification]], ['efficientnet', ['EfficientNetForImageClassification', EfficientNetForImageClassification]], ]); const MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES = new Map([ ['detr', ['DetrForObjectDetection', DetrForObjectDetection]], ['table-transformer', ['TableTransformerForObjectDetection', TableTransformerForObjectDetection]], ['yolos', ['YolosForObjectDetection', YolosForObjectDetection]], ]); const MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES = new Map([ ['owlvit', ['OwlViTForObjectDetection', OwlViTForObjectDetection]], ['owlv2', ['Owlv2ForObjectDetection', Owlv2ForObjectDetection]], ]); const MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES = new Map([ ['detr', ['DetrForSegmentation', DetrForSegmentation]], ['clipseg', ['CLIPSegForImageSegmentation', CLIPSegForImageSegmentation]], ]); const MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES = new Map([ ['segformer', ['SegformerForSemanticSegmentation', SegformerForSemanticSegmentation]], ]); const MODEL_FOR_MASK_GENERATION_MAPPING_NAMES = new Map([ ['sam', ['SamModel', SamModel]], ]); const MODEL_FOR_CTC_MAPPING_NAMES = new Map([ ['wav2vec2', ['Wav2Vec2ForCTC', Wav2Vec2ForCTC]], ['wav2vec2-bert', ['Wav2Vec2BertForCTC', Wav2Vec2BertForCTC]], ['unispeech', ['UniSpeechForCTC', UniSpeechForCTC]], ['unispeech-sat', ['UniSpeechSatForCTC', UniSpeechSatForCTC]], ['wavlm', ['WavLMForCTC', WavLMForCTC]], ['hubert', ['HubertForCTC', HubertForCTC]], ]); const MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES = new Map([ ['wav2vec2', ['Wav2Vec2ForSequenceClassification', Wav2Vec2ForSequenceClassification]], ['wav2vec2-bert', ['Wav2Vec2BertForSequenceClassification', Wav2Vec2BertForSequenceClassification]], ['unispeech', ['UniSpeechForSequenceClassification', UniSpeechForSequenceClassification]], ['unispeech-sat', ['UniSpeechSatForSequenceClassification', UniSpeechSatForSequenceClassification]], ['wavlm', ['WavLMForSequenceClassification', WavLMForSequenceClassification]], ['hubert', ['HubertForSequenceClassification', HubertForSequenceClassification]], ['audio-spectrogram-transformer', ['ASTForAudioClassification', ASTForAudioClassification]], ]); const MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES = new Map([ ['wavlm', ['WavLMForXVector', WavLMForXVector]], ]); const MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES = new Map([ ['unispeech-sat', ['UniSpeechSatForAudioFrameClassification', UniSpeechSatForAudioFrameClassification]], ['wavlm', ['WavLMForAudioFrameClassification', WavLMForAudioFrameClassification]], ['wav2vec2', ['Wav2Vec2ForAudioFrameClassification', Wav2Vec2ForAudioFrameClassification]], ]); const MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES = new Map([ ['vitmatte', ['VitMatteForImageMatting', VitMatteForImageMatting]], ]); const MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES = new Map([ ['swin2sr', ['Swin2SRForImageSuperResolution', Swin2SRForImageSuperResolution]], ]) const MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES = new Map([ ['dpt', ['DPTForDepthEstimation', DPTForDepthEstimation]], ['depth_anything', ['DepthAnythingForDepthEstimation', DepthAnythingForDepthEstimation]], ['glpn', ['GLPNForDepthEstimation', GLPNForDepthEstimation]], ]) // NOTE: This is custom to Transformers.js, and is necessary because certain models // (e.g., CLIP) are split into vision and text components const MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES = new Map([ ['clip', ['CLIPVisionModelWithProjection', CLIPVisionModelWithProjection]], ['siglip', ['SiglipVisionModel', SiglipVisionModel]], ]) const MODEL_CLASS_TYPE_MAPPING = [ [MODEL_MAPPING_NAMES_ENCODER_ONLY, MODEL_TYPES.EncoderOnly], [MODEL_MAPPING_NAMES_ENCODER_DECODER, MODEL_TYPES.EncoderDecoder], [MODEL_MAPPING_NAMES_DECODER_ONLY, MODEL_TYPES.DecoderOnly], [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.DecoderOnly], [MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Vision2Seq], [MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES, MODEL_TYPES.ImageTextToText], [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES, MODEL_TYPES.MaskGeneration], [MODEL_FOR_CTC_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], // Custom: [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], ]; for (const [mappings, type] of MODEL_CLASS_TYPE_MAPPING) { // @ts-ignore for (const [name, model] of mappings.values()) { MODEL_TYPE_MAPPING.set(name, type); MODEL_CLASS_TO_NAME_MAPPING.set(model, name); MODEL_NAME_TO_CLASS_MAPPING.set(name, model); } } const CUSTOM_MAPPING = [ // OVERRIDE: // TODO: Refactor to allow class to specify model ['MusicgenForConditionalGeneration', MusicgenForConditionalGeneration, MODEL_TYPES.Musicgen], ['CLIPTextModelWithProjection', CLIPTextModelWithProjection, MODEL_TYPES.EncoderOnly], ['SiglipTextModel', SiglipTextModel, MODEL_TYPES.EncoderOnly], ['ClapTextModelWithProjection', ClapTextModelWithProjection, MODEL_TYPES.EncoderOnly], ['ClapAudioModelWithProjection', ClapAudioModelWithProjection, MODEL_TYPES.EncoderOnly], ] for (const [name, model, type] of CUSTOM_MAPPING) { MODEL_TYPE_MAPPING.set(name, type); MODEL_CLASS_TO_NAME_MAPPING.set(model, name); MODEL_NAME_TO_CLASS_MAPPING.set(name, model); } /** * Helper class which is used to instantiate pretrained models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModel.from_pretrained('Xenova/bert-base-uncased'); */ class AutoModel extends PretrainedMixin { /** @type {Map[]} */ // @ts-ignore static MODEL_CLASS_MAPPINGS = MODEL_CLASS_TYPE_MAPPING.map(x => x[0]); static BASE_IF_FAIL = true; } /** * Helper class which is used to instantiate pretrained sequence classification models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForSequenceClassification.from_pretrained('Xenova/distilbert-base-uncased-finetuned-sst-2-english'); */ class AutoModelForSequenceClassification extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained token classification models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForTokenClassification.from_pretrained('Xenova/distilbert-base-multilingual-cased-ner-hrl'); */ class AutoModelForTokenClassification extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained sequence-to-sequence models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForSeq2SeqLM.from_pretrained('Xenova/t5-small'); */ class AutoModelForSeq2SeqLM extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained sequence-to-sequence speech-to-text models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForSpeechSeq2Seq.from_pretrained('openai/whisper-tiny.en'); */ class AutoModelForSpeechSeq2Seq extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained sequence-to-sequence text-to-spectrogram models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForTextToSpectrogram.from_pretrained('microsoft/speecht5_tts'); */ class AutoModelForTextToSpectrogram extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained text-to-waveform models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForTextToSpectrogram.from_pretrained('facebook/mms-tts-eng'); */ class AutoModelForTextToWaveform extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained causal language models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForCausalLM.from_pretrained('Xenova/gpt2'); */ class AutoModelForCausalLM extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained masked language models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForMaskedLM.from_pretrained('Xenova/bert-base-uncased'); */ class AutoModelForMaskedLM extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_MASKED_LM_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained question answering models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForQuestionAnswering.from_pretrained('Xenova/distilbert-base-cased-distilled-squad'); */ class AutoModelForQuestionAnswering extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained vision-to-sequence models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForVision2Seq.from_pretrained('Xenova/vit-gpt2-image-captioning'); */ class AutoModelForVision2Seq extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained image classification models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForImageClassification.from_pretrained('Xenova/vit-base-patch16-224'); */ class AutoModelForImageClassification extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained image segmentation models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForImageSegmentation.from_pretrained('Xenova/detr-resnet-50-panoptic'); */ class AutoModelForImageSegmentation extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained image segmentation models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForSemanticSegmentation.from_pretrained('nvidia/segformer-b3-finetuned-cityscapes-1024-1024'); */ class AutoModelForSemanticSegmentation extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained object detection models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForObjectDetection.from_pretrained('Xenova/detr-resnet-50'); */ class AutoModelForObjectDetection extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES]; } class AutoModelForZeroShotObjectDetection extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES]; } /** * Helper class which is used to instantiate pretrained mask generation models with the `from_pretrained` function. * The chosen model class is determined by the type specified in the model config. * * @example * let model = await AutoModelForMaskGeneration.from_pretrained('Xenova/sam-vit-base'); */ class AutoModelForMaskGeneration extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES]; } class AutoModelForCTC extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_CTC_MAPPING_NAMES]; } class AutoModelForAudioClassification extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES]; } class AutoModelForXVector extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES]; } class AutoModelForAudioFrameClassification extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES]; } class AutoModelForDocumentQuestionAnswering extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES]; } class AutoModelForImageMatting extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES]; } class AutoModelForImageToImage extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES]; } class AutoModelForDepthEstimation extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES]; } class AutoModelForImageFeatureExtraction extends PretrainedMixin { static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES]; } ////////////////////////////////////////////////// ////////////////////////////////////////////////// class Seq2SeqLMOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.logits The output logits of the model. * @param {Tensor} output.past_key_values An tensor of key/value pairs that represent the previous state of the model. * @param {Tensor} output.encoder_outputs The output of the encoder in a sequence-to-sequence model. * @param {Tensor} [output.decoder_attentions] Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the self-attention heads. * @param {Tensor} [output.cross_attentions] Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the weighted average in the cross-attention heads. */ constructor({ logits, past_key_values, encoder_outputs, decoder_attentions = null, cross_attentions = null }) { super(); this.logits = logits; this.past_key_values = past_key_values; this.encoder_outputs = encoder_outputs; this.decoder_attentions = decoder_attentions; this.cross_attentions = cross_attentions; } } /** * Base class for outputs of sentence classification models. */ class SequenceClassifierOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.logits classification (or regression if config.num_labels==1) scores (before SoftMax). */ constructor({ logits }) { super(); this.logits = logits; } } /** * Base class for outputs of XVector models. */ class XVectorOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.logits Classification hidden states before AMSoftmax, of shape `(batch_size, config.xvector_output_dim)`. * @param {Tensor} output.embeddings Utterance embeddings used for vector similarity-based retrieval, of shape `(batch_size, config.xvector_output_dim)`. */ constructor({ logits, embeddings }) { super(); this.logits = logits; this.embeddings = embeddings; } } /** * Base class for outputs of token classification models. */ class TokenClassifierOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.logits Classification scores (before SoftMax). */ constructor({ logits }) { super(); this.logits = logits; } } /** * Base class for masked language models outputs. */ class MaskedLMOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). */ constructor({ logits }) { super(); this.logits = logits; } } /** * Base class for outputs of question answering models. */ class QuestionAnsweringModelOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.start_logits Span-start scores (before SoftMax). * @param {Tensor} output.end_logits Span-end scores (before SoftMax). */ constructor({ start_logits, end_logits }) { super(); this.start_logits = start_logits; this.end_logits = end_logits; } } /** * Base class for causal language model (or autoregressive) outputs. */ class CausalLMOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). */ constructor({ logits }) { super(); this.logits = logits; } } /** * Base class for causal language model (or autoregressive) outputs. */ class CausalLMOutputWithPast extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). * @param {Tensor} output.past_key_values Contains pre-computed hidden-states (key and values in the self-attention blocks) * that can be used (see `past_key_values` input) to speed up sequential decoding. */ constructor({ logits, past_key_values }) { super(); this.logits = logits; this.past_key_values = past_key_values; } } class ImageMattingOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.alphas Estimated alpha values, of shape `(batch_size, num_channels, height, width)`. */ constructor({ alphas }) { super(); this.alphas = alphas; } } /** * Describes the outputs for the VITS model. */ class VitsModelOutput extends ModelOutput { /** * @param {Object} output The output of the model. * @param {Tensor} output.waveform The final audio waveform predicted by the model, of shape `(batch_size, sequence_length)`. * @param {Tensor} output.spectrogram The log-mel spectrogram predicted at the output of the flow model. * This spectrogram is passed to the Hi-Fi GAN decoder model to obtain the final audio waveform. */ constructor({ waveform, spectrogram }) { super(); this.waveform = waveform; this.spectrogram = spectrogram; } } /***/ }), /***/ "./src/ops/registry.js": /*!*****************************!*\ !*** ./src/ops/registry.js ***! \*****************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "TensorOpRegistry": () => (/* binding */ TensorOpRegistry) /* harmony export */ }); /* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../backends/onnx.js */ "./src/backends/onnx.js"); /* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); const wrap = async (session_bytes, session_options, name) => { const session = await (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__.createInferenceSession)( session_bytes, session_options, ); return async (inputs) => { const ortFeed = Object.fromEntries(Object.entries(inputs).map(([k, v]) => [k, v.ort_tensor])); const outputs = await session.run(ortFeed); return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[name]); } } // In-memory registry of initialized ONNX operators class TensorOpRegistry { static session_options = { // TODO: Allow for multiple execution providers // executionProviders: ['webgpu'], }; static get bilinear_interpolate_4d() { if (!this._bilinear_interpolate_4d) { this._bilinear_interpolate_4d = wrap( new Uint8Array([8, 9, 18, 0, 58, 128, 1, 10, 40, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 17, 10, 4, 109, 111, 100, 101, 34, 6, 108, 105, 110, 101, 97, 114, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20]), this.session_options, 'y', ); } return this._bilinear_interpolate_4d; } static get bicubic_interpolate_4d() { if (!this._bicubic_interpolate_4d) { this._bicubic_interpolate_4d = wrap( new Uint8Array([8, 9, 18, 0, 58, 127, 10, 39, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 16, 10, 4, 109, 111, 100, 101, 34, 5, 99, 117, 98, 105, 99, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20]), this.session_options, 'y', ); } return this._bicubic_interpolate_4d; } } /***/ }), /***/ "./src/pipelines.js": /*!**************************!*\ !*** ./src/pipelines.js ***! \**************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "AudioClassificationPipeline": () => (/* binding */ AudioClassificationPipeline), /* harmony export */ "AutomaticSpeechRecognitionPipeline": () => (/* binding */ AutomaticSpeechRecognitionPipeline), /* harmony export */ "DepthEstimationPipeline": () => (/* binding */ DepthEstimationPipeline), /* harmony export */ "DocumentQuestionAnsweringPipeline": () => (/* binding */ DocumentQuestionAnsweringPipeline), /* harmony export */ "FeatureExtractionPipeline": () => (/* binding */ FeatureExtractionPipeline), /* harmony export */ "FillMaskPipeline": () => (/* binding */ FillMaskPipeline), /* harmony export */ "ImageClassificationPipeline": () => (/* binding */ ImageClassificationPipeline), /* harmony export */ "ImageFeatureExtractionPipeline": () => (/* binding */ ImageFeatureExtractionPipeline), /* harmony export */ "ImageSegmentationPipeline": () => (/* binding */ ImageSegmentationPipeline), /* harmony export */ "ImageToImagePipeline": () => (/* binding */ ImageToImagePipeline), /* harmony export */ "ImageToTextPipeline": () => (/* binding */ ImageToTextPipeline), /* harmony export */ "ObjectDetectionPipeline": () => (/* binding */ ObjectDetectionPipeline), /* harmony export */ "Pipeline": () => (/* binding */ Pipeline), /* harmony export */ "QuestionAnsweringPipeline": () => (/* binding */ QuestionAnsweringPipeline), /* harmony export */ "SummarizationPipeline": () => (/* binding */ SummarizationPipeline), /* harmony export */ "Text2TextGenerationPipeline": () => (/* binding */ Text2TextGenerationPipeline), /* harmony export */ "TextClassificationPipeline": () => (/* binding */ TextClassificationPipeline), /* harmony export */ "TextGenerationPipeline": () => (/* binding */ TextGenerationPipeline), /* harmony export */ "TextToAudioPipeline": () => (/* binding */ TextToAudioPipeline), /* harmony export */ "TokenClassificationPipeline": () => (/* binding */ TokenClassificationPipeline), /* harmony export */ "TranslationPipeline": () => (/* binding */ TranslationPipeline), /* harmony export */ "ZeroShotAudioClassificationPipeline": () => (/* binding */ ZeroShotAudioClassificationPipeline), /* harmony export */ "ZeroShotClassificationPipeline": () => (/* binding */ ZeroShotClassificationPipeline), /* harmony export */ "ZeroShotImageClassificationPipeline": () => (/* binding */ ZeroShotImageClassificationPipeline), /* harmony export */ "ZeroShotObjectDetectionPipeline": () => (/* binding */ ZeroShotObjectDetectionPipeline), /* harmony export */ "pipeline": () => (/* binding */ pipeline) /* harmony export */ }); /* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tokenizers.js */ "./src/tokenizers.js"); /* harmony import */ var _models_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./models.js */ "./src/models.js"); /* harmony import */ var _processors_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./processors.js */ "./src/processors.js"); /* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); /* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); /* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); /* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); /* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); /* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); /** * @file Pipelines provide a high-level, easy to use, API for running machine learning models. * * **Example:** Instantiate pipeline using the `pipeline` function. * ```javascript * import { pipeline } from '@xenova/transformers'; * * const classifier = await pipeline('sentiment-analysis'); * const output = await classifier('I love transformers!'); * // [{'label': 'POSITIVE', 'score': 0.999817686}] * ``` * * @module pipelines */ /** * @typedef {string | RawImage | URL} ImageInput * @typedef {ImageInput|ImageInput[]} ImagePipelineInputs */ /** * Prepare images for further tasks. * @param {ImagePipelineInputs} images images to prepare. * @returns {Promise} returns processed images. * @private */ async function prepareImages(images) { if (!Array.isArray(images)) { images = [images]; } // Possibly convert any non-images to images return await Promise.all(images.map(x => _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.read(x))); } /** * @typedef {string | URL | Float32Array | Float64Array} AudioInput * @typedef {AudioInput|AudioInput[]} AudioPipelineInputs */ /** * Prepare audios for further tasks. * @param {AudioPipelineInputs} audios audios to prepare. * @param {number} sampling_rate sampling rate of the audios. * @returns {Promise} The preprocessed audio data. * @private */ async function prepareAudios(audios, sampling_rate) { if (!Array.isArray(audios)) { audios = [audios]; } return await Promise.all(audios.map(x => { if (typeof x === 'string' || x instanceof URL) { return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.read_audio)(x, sampling_rate); } else if (x instanceof Float64Array) { return new Float32Array(x); } return x; })); } /** * @typedef {Object} BoundingBox * @property {number} xmin The minimum x coordinate of the bounding box. * @property {number} ymin The minimum y coordinate of the bounding box. * @property {number} xmax The maximum x coordinate of the bounding box. * @property {number} ymax The maximum y coordinate of the bounding box. */ /** * Helper function to convert list [xmin, xmax, ymin, ymax] into object { "xmin": xmin, ... } * @param {number[]} box The bounding box as a list. * @param {boolean} asInteger Whether to cast to integers. * @returns {BoundingBox} The bounding box as an object. * @private */ function get_bounding_box(box, asInteger) { if (asInteger) { box = box.map(x => x | 0); } const [xmin, ymin, xmax, ymax] = box; return { xmin, ymin, xmax, ymax }; } /** * @callback DisposeType Disposes the item. * @returns {Promise} A promise that resolves when the item has been disposed. * * @typedef {Object} Disposable * @property {DisposeType} dispose A promise that resolves when the pipeline has been disposed. */ /** * The Pipeline class is the class from which all pipelines inherit. * Refer to this class for methods shared across different pipelines. * @extends Callable */ class Pipeline extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { /** * Create a new Pipeline. * @param {Object} options An object containing the following properties: * @param {string} [options.task] The task of the pipeline. Useful for specifying subtasks. * @param {PreTrainedModel} [options.model] The model used by the pipeline. * @param {PreTrainedTokenizer} [options.tokenizer=null] The tokenizer used by the pipeline (if any). * @param {Processor} [options.processor=null] The processor used by the pipeline (if any). */ constructor({ task, model, tokenizer = null, processor = null }) { super(); this.task = task; this.model = model; this.tokenizer = tokenizer; this.processor = processor; } /** @type {DisposeType} */ async dispose() { await this.model.dispose(); } } /** * @typedef {Object} ModelTokenizerConstructorArgs * @property {string} task The task of the pipeline. Useful for specifying subtasks. * @property {PreTrainedModel} model The model used by the pipeline. * @property {PreTrainedTokenizer} tokenizer The tokenizer used by the pipeline. * * @typedef {ModelTokenizerConstructorArgs} TextPipelineConstructorArgs An object used to instantiate a text-based pipeline. */ /** * @typedef {Object} ModelProcessorConstructorArgs * @property {string} task The task of the pipeline. Useful for specifying subtasks. * @property {PreTrainedModel} model The model used by the pipeline. * @property {Processor} processor The processor used by the pipeline. * * @typedef {ModelProcessorConstructorArgs} AudioPipelineConstructorArgs An object used to instantiate an audio-based pipeline. * @typedef {ModelProcessorConstructorArgs} ImagePipelineConstructorArgs An object used to instantiate an image-based pipeline. */ /** * @typedef {Object} ModelTokenizerProcessorConstructorArgs * @property {string} task The task of the pipeline. Useful for specifying subtasks. * @property {PreTrainedModel} model The model used by the pipeline. * @property {PreTrainedTokenizer} tokenizer The tokenizer used by the pipeline. * @property {Processor} processor The processor used by the pipeline. * * @typedef {ModelTokenizerProcessorConstructorArgs} TextAudioPipelineConstructorArgs An object used to instantiate a text- and audio-based pipeline. * @typedef {ModelTokenizerProcessorConstructorArgs} TextImagePipelineConstructorArgs An object used to instantiate a text- and image-based pipeline. */ /** * @typedef {Object} TextClassificationSingle * @property {string} label The label predicted. * @property {number} score The corresponding probability. * @typedef {TextClassificationSingle[]} TextClassificationOutput * * @typedef {Object} TextClassificationPipelineOptions Parameters specific to text classification pipelines. * @property {number} [topk=1] The number of top predictions to be returned. * * @callback TextClassificationPipelineCallback Classify the text(s) given as inputs. * @param {string|string[]} texts The input text(s) to be classified. * @param {TextClassificationPipelineOptions} [options] The options to use for text classification. * @returns {Promise} An array or object containing the predicted labels and scores. * * @typedef {TextPipelineConstructorArgs & TextClassificationPipelineCallback & Disposable} TextClassificationPipelineType */ /** * Text classification pipeline using any `ModelForSequenceClassification`. * * **Example:** Sentiment-analysis w/ `Xenova/distilbert-base-uncased-finetuned-sst-2-english`. * ```javascript * const classifier = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english'); * const output = await classifier('I love transformers!'); * // [{ label: 'POSITIVE', score: 0.999788761138916 }] * ``` * * **Example:** Multilingual sentiment-analysis w/ `Xenova/bert-base-multilingual-uncased-sentiment` (and return top 5 classes). * ```javascript * const classifier = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment'); * const output = await classifier('Le meilleur film de tous les temps.', { topk: 5 }); * // [ * // { label: '5 stars', score: 0.9610759615898132 }, * // { label: '4 stars', score: 0.03323351591825485 }, * // { label: '3 stars', score: 0.0036155181005597115 }, * // { label: '1 star', score: 0.0011325967498123646 }, * // { label: '2 stars', score: 0.0009423971059732139 } * // ] * ``` * * **Example:** Toxic comment classification w/ `Xenova/toxic-bert` (and return all classes). * ```javascript * const classifier = await pipeline('text-classification', 'Xenova/toxic-bert'); * const output = await classifier('I hate you!', { topk: null }); * // [ * // { label: 'toxic', score: 0.9593140482902527 }, * // { label: 'insult', score: 0.16187334060668945 }, * // { label: 'obscene', score: 0.03452680632472038 }, * // { label: 'identity_hate', score: 0.0223250575363636 }, * // { label: 'threat', score: 0.019197041168808937 }, * // { label: 'severe_toxic', score: 0.005651099607348442 } * // ] * ``` */ class TextClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TextClassificationPipelineType} */ (Pipeline)) { /** * Create a new TextClassificationPipeline. * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {TextClassificationPipelineCallback} */ async _call(texts, { topk = 1 } = {}) { // Run tokenization const model_inputs = this.tokenizer(texts, { padding: true, truncation: true, }); // Run model const outputs = await this.model(model_inputs) // TODO: Use softmax tensor function const function_to_apply = this.model.config.problem_type === 'multi_label_classification' ? batch => batch.sigmoid().data : batch => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data); // single_label_classification (default) const id2label = this.model.config.id2label; const toReturn = []; for (const batch of outputs.logits) { const output = function_to_apply(batch); const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.getTopItems)(output, topk); const vals = scores.map(x => ({ label: id2label[x[0]], score: x[1], })); if (topk === 1) { toReturn.push(...vals); } else { toReturn.push(vals); } } return Array.isArray(texts) || topk === 1 ? /** @type {TextClassificationOutput} */ (toReturn) : /** @type {TextClassificationOutput[]} */ (toReturn)[0]; } } /** * @typedef {Object} TokenClassificationSingle * @property {string} word The token/word classified. This is obtained by decoding the selected tokens. * @property {number} score The corresponding probability for `entity`. * @property {string} entity The entity predicted for that token/word. * @property {number} index The index of the corresponding token in the sentence. * @property {number} [start] The index of the start of the corresponding entity in the sentence. * @property {number} [end] The index of the end of the corresponding entity in the sentence. * @typedef {TokenClassificationSingle[]} TokenClassificationOutput * * @typedef {Object} TokenClassificationPipelineOptions Parameters specific to token classification pipelines. * @property {string[]} [ignore_labels] A list of labels to ignore. * * @callback TokenClassificationPipelineCallback Classify each token of the text(s) given as inputs. * @param {string|string[]} texts One or several texts (or one list of texts) for token classification. * @param {TokenClassificationPipelineOptions} [options] The options to use for token classification. * @returns {Promise} The result. * * @typedef {TextPipelineConstructorArgs & TokenClassificationPipelineCallback & Disposable} TokenClassificationPipelineType */ /** * Named Entity Recognition pipeline using any `ModelForTokenClassification`. * * **Example:** Perform named entity recognition with `Xenova/bert-base-NER`. * ```javascript * const classifier = await pipeline('token-classification', 'Xenova/bert-base-NER'); * const output = await classifier('My name is Sarah and I live in London'); * // [ * // { entity: 'B-PER', score: 0.9980202913284302, index: 4, word: 'Sarah' }, * // { entity: 'B-LOC', score: 0.9994474053382874, index: 9, word: 'London' } * // ] * ``` * * **Example:** Perform named entity recognition with `Xenova/bert-base-NER` (and return all labels). * ```javascript * const classifier = await pipeline('token-classification', 'Xenova/bert-base-NER'); * const output = await classifier('Sarah lives in the United States of America', { ignore_labels: [] }); * // [ * // { entity: 'B-PER', score: 0.9966587424278259, index: 1, word: 'Sarah' }, * // { entity: 'O', score: 0.9987385869026184, index: 2, word: 'lives' }, * // { entity: 'O', score: 0.9990072846412659, index: 3, word: 'in' }, * // { entity: 'O', score: 0.9988298416137695, index: 4, word: 'the' }, * // { entity: 'B-LOC', score: 0.9995510578155518, index: 5, word: 'United' }, * // { entity: 'I-LOC', score: 0.9990395307540894, index: 6, word: 'States' }, * // { entity: 'I-LOC', score: 0.9986724853515625, index: 7, word: 'of' }, * // { entity: 'I-LOC', score: 0.9975294470787048, index: 8, word: 'America' } * // ] * ``` */ class TokenClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TokenClassificationPipelineType} */ (Pipeline)) { /** * Create a new TokenClassificationPipeline. * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {TokenClassificationPipelineCallback} */ async _call(texts, { ignore_labels = ['O'], } = {}) { const isBatched = Array.isArray(texts); // Run tokenization const model_inputs = this.tokenizer(isBatched ? texts : [texts], { padding: true, truncation: true, }); // Run model const outputs = await this.model(model_inputs) const logits = outputs.logits; const id2label = this.model.config.id2label; const toReturn = []; for (let i = 0; i < logits.dims[0]; ++i) { const ids = model_inputs.input_ids[i]; const batch = logits[i]; // List of tokens that aren't ignored const tokens = []; for (let j = 0; j < batch.dims[0]; ++j) { const tokenData = batch[j]; const topScoreIndex = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(tokenData.data)[1]; const entity = id2label ? id2label[topScoreIndex] : `LABEL_${topScoreIndex}`; if (ignore_labels.includes(entity)) { // We predicted a token that should be ignored. So, we skip it. continue; } // TODO add option to keep special tokens? const word = this.tokenizer.decode([ids[j].item()], { skip_special_tokens: true }); if (word === '') { // Was a special token. So, we skip it. continue; } const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(tokenData.data); tokens.push({ entity: entity, score: scores[topScoreIndex], index: j, word: word, // TODO: null for now, but will add start: null, end: null, }); } toReturn.push(tokens); } return isBatched ? toReturn : toReturn[0]; } } /** * @typedef {Object} QuestionAnsweringOutput * @property {number} score The probability associated to the answer. * @property {number} [start] The character start index of the answer (in the tokenized version of the input). * @property {number} [end] The character end index of the answer (in the tokenized version of the input). * @property {string} answer The answer to the question. * * @typedef {Object} QuestionAnsweringPipelineOptions Parameters specific to question answering pipelines. * @property {number} [topk=1] The number of top answer predictions to be returned. * * @callback QuestionAnsweringPipelineCallback Answer the question(s) given as inputs by using the context(s). * @param {string|string[]} question One or several question(s) (must be used in conjunction with the `context` argument). * @param {string|string[]} context One or several context(s) associated with the question(s) (must be used in conjunction with the `question` argument). * @param {QuestionAnsweringPipelineOptions} [options] The options to use for question answering. * @returns {Promise} An array or object containing the predicted answers and scores. * * @typedef {TextPipelineConstructorArgs & QuestionAnsweringPipelineCallback & Disposable} QuestionAnsweringPipelineType */ /** * Question Answering pipeline using any `ModelForQuestionAnswering`. * * **Example:** Run question answering with `Xenova/distilbert-base-uncased-distilled-squad`. * ```javascript * const answerer = await pipeline('question-answering', 'Xenova/distilbert-base-uncased-distilled-squad'); * const question = 'Who was Jim Henson?'; * const context = 'Jim Henson was a nice puppet.'; * const output = await answerer(question, context); * // { * // answer: "a nice puppet", * // score: 0.5768911502526741 * // } * ``` */ class QuestionAnsweringPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => QuestionAnsweringPipelineType} */ (Pipeline)) { /** * Create a new QuestionAnsweringPipeline. * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {QuestionAnsweringPipelineCallback} */ async _call(question, context, { topk = 1 } = {}) { // Run tokenization const inputs = this.tokenizer(question, { text_pair: context, padding: true, truncation: true, }); const output = await this.model(inputs); /** @type {QuestionAnsweringOutput[]} */ const toReturn = []; for (let j = 0; j < output.start_logits.dims[0]; ++j) { const ids = inputs.input_ids[j]; const sepIndex = ids.indexOf(this.tokenizer.sep_token_id); const s1 = Array.from((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(output.start_logits[j].data)) .map((x, i) => [x, i]) .filter(x => x[1] > sepIndex); const e1 = Array.from((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(output.end_logits[j].data)) .map((x, i) => [x, i]) .filter(x => x[1] > sepIndex); const options = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.product)(s1, e1) .filter(x => x[0][1] <= x[1][1]) .map(x => [x[0][1], x[1][1], x[0][0] * x[1][0]]) .sort((a, b) => b[2] - a[2]); for (let k = 0; k < Math.min(options.length, topk); ++k) { const [start, end, score] = options[k]; const answer_tokens = [...ids].slice(start, end + 1) const answer = this.tokenizer.decode(answer_tokens, { skip_special_tokens: true, }); // TODO add start and end? // NOTE: HF returns character index toReturn.push({ answer, score }); } } // Mimic HF's return type based on topk return (topk === 1) ? toReturn[0] : toReturn; } } /** * @typedef {Object} FillMaskSingle * @property {string} sequence The corresponding input with the mask token prediction. * @property {number} score The corresponding probability. * @property {number} token The predicted token id (to replace the masked one). * @property {string} token_str The predicted token (to replace the masked one). * @typedef {FillMaskSingle[]} FillMaskOutput * * @typedef {Object} FillMaskPipelineOptions Parameters specific to fill mask pipelines. * @property {number} [topk=5] When passed, overrides the number of predictions to return. * * @callback FillMaskPipelineCallback Fill the masked token in the text(s) given as inputs. * @param {string|string[]} texts One or several texts (or one list of prompts) with masked tokens. * @param {FillMaskPipelineOptions} [options] The options to use for masked language modelling. * @returns {Promise} An array of objects containing the score, predicted token, predicted token string, * and the sequence with the predicted token filled in, or an array of such arrays (one for each input text). * If only one input text is given, the output will be an array of objects. * @throws {Error} When the mask token is not found in the input text. * * @typedef {TextPipelineConstructorArgs & FillMaskPipelineCallback & Disposable} FillMaskPipelineType */ /** * Masked language modeling prediction pipeline using any `ModelWithLMHead`. * * **Example:** Perform masked language modelling (a.k.a. "fill-mask") with `Xenova/bert-base-uncased`. * ```javascript * const unmasker = await pipeline('fill-mask', 'Xenova/bert-base-cased'); * const output = await unmasker('The goal of life is [MASK].'); * // [ * // { token_str: 'survival', score: 0.06137419492006302, token: 8115, sequence: 'The goal of life is survival.' }, * // { token_str: 'love', score: 0.03902450203895569, token: 1567, sequence: 'The goal of life is love.' }, * // { token_str: 'happiness', score: 0.03253183513879776, token: 9266, sequence: 'The goal of life is happiness.' }, * // { token_str: 'freedom', score: 0.018736306577920914, token: 4438, sequence: 'The goal of life is freedom.' }, * // { token_str: 'life', score: 0.01859794743359089, token: 1297, sequence: 'The goal of life is life.' } * // ] * ``` * * **Example:** Perform masked language modelling (a.k.a. "fill-mask") with `Xenova/bert-base-cased` (and return top result). * ```javascript * const unmasker = await pipeline('fill-mask', 'Xenova/bert-base-cased'); * const output = await unmasker('The Milky Way is a [MASK] galaxy.', { topk: 1 }); * // [{ token_str: 'spiral', score: 0.6299987435340881, token: 14061, sequence: 'The Milky Way is a spiral galaxy.' }] * ``` */ class FillMaskPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => FillMaskPipelineType} */ (Pipeline)) { /** * Create a new FillMaskPipeline. * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {FillMaskPipelineCallback} */ async _call(texts, { topk = 5 } = {}) { // Run tokenization const model_inputs = this.tokenizer(texts, { padding: true, truncation: true, }); // Run model const outputs = await this.model(model_inputs) const toReturn = []; for (let i = 0; i < model_inputs.input_ids.dims[0]; ++i) { const ids = model_inputs.input_ids[i]; const mask_token_index = ids.indexOf(this.tokenizer.mask_token_id) if (mask_token_index === -1) { throw Error(`Mask token (${this.tokenizer.mask_token}) not found in text.`) } const logits = outputs.logits[i]; const itemLogits = logits[mask_token_index]; const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.getTopItems)((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(itemLogits.data), topk); toReturn.push(scores.map(x => { const sequence = [...ids]; sequence[mask_token_index] = x[0]; return { score: x[1], token: x[0], token_str: this.tokenizer.model.vocab[x[0]], sequence: this.tokenizer.decode(sequence, { skip_special_tokens: true }), } })); } return Array.isArray(texts) ? toReturn : toReturn[0]; } } /** * @typedef {Object} Text2TextGenerationSingle * @property {string} generated_text The generated text. * @typedef {Text2TextGenerationSingle[]} Text2TextGenerationOutput * * @callback Text2TextGenerationPipelineCallback Generate the output text(s) using text(s) given as inputs. * @param {string|string[]} texts Input text for the encoder. * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. * @returns {Promise} * * @typedef {TextPipelineConstructorArgs & Text2TextGenerationPipelineCallback & Disposable} Text2TextGenerationPipelineType */ /** * Text2TextGenerationPipeline class for generating text using a model that performs text-to-text generation tasks. * * **Example:** Text-to-text generation w/ `Xenova/LaMini-Flan-T5-783M`. * ```javascript * const generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M'); * const output = await generator('how can I become more healthy?', { * max_new_tokens: 100, * }); * // [{ generated_text: "To become more healthy, you can: 1. Eat a balanced diet with plenty of fruits, vegetables, whole grains, lean proteins, and healthy fats. 2. Stay hydrated by drinking plenty of water. 3. Get enough sleep and manage stress levels. 4. Avoid smoking and excessive alcohol consumption. 5. Regularly exercise and maintain a healthy weight. 6. Practice good hygiene and sanitation. 7. Seek medical attention if you experience any health issues." }] * ``` */ class Text2TextGenerationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => Text2TextGenerationPipelineType} */ (Pipeline)) { /** @type {'generated_text'} */ _key = 'generated_text'; /** * Create a new Text2TextGenerationPipeline. * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {Text2TextGenerationPipelineCallback} */ async _call(texts, generate_kwargs = {}) { throw new Error('This pipeline is not yet supported in Transformers.js v3.'); // TODO: Remove when implemented if (!Array.isArray(texts)) { texts = [texts]; } // Add global prefix, if present if (this.model.config.prefix) { texts = texts.map(x => this.model.config.prefix + x) } // Handle task specific params: const task_specific_params = this.model.config.task_specific_params if (task_specific_params && task_specific_params[this.task]) { // Add prefixes, if present if (task_specific_params[this.task].prefix) { texts = texts.map(x => task_specific_params[this.task].prefix + x) } // TODO update generation config } const tokenizer = this.tokenizer; const tokenizer_options = { padding: true, truncation: true, } let input_ids; if (this instanceof TranslationPipeline && '_build_translation_inputs' in tokenizer) { // TODO: move to Translation pipeline? // Currently put here to avoid code duplication // @ts-ignore input_ids = tokenizer._build_translation_inputs(texts, tokenizer_options, generate_kwargs).input_ids; } else { input_ids = tokenizer(texts, tokenizer_options).input_ids; } const outputTokenIds = await this.model.generate({ inputs: input_ids, ...generate_kwargs }); return tokenizer.batch_decode(/** @type {Tensor} */(outputTokenIds), { skip_special_tokens: true, }).map(text => ({ [this._key]: text })); } } /** * @typedef {Object} SummarizationSingle * @property {string} summary_text The summary text. * @typedef {SummarizationSingle[]} SummarizationOutput * * @callback SummarizationPipelineCallback Summarize the text(s) given as inputs. * @param {string|string[]} texts One or several articles (or one list of articles) to summarize. * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. * @returns {Promise} * * @typedef {TextPipelineConstructorArgs & SummarizationPipelineCallback & Disposable} SummarizationPipelineType */ /** * A pipeline for summarization tasks, inheriting from Text2TextGenerationPipeline. * * **Example:** Summarization w/ `Xenova/distilbart-cnn-6-6`. * ```javascript * const generator = await pipeline('summarization', 'Xenova/distilbart-cnn-6-6'); * const text = 'The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, ' + * 'and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. ' + * 'During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest ' + * 'man-made structure in the world, a title it held for 41 years until the Chrysler Building in New ' + * 'York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to ' + * 'the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the ' + * 'Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second ' + * 'tallest free-standing structure in France after the Millau Viaduct.'; * const output = await generator(text, { * max_new_tokens: 100, * }); * // [{ summary_text: ' The Eiffel Tower is about the same height as an 81-storey building and the tallest structure in Paris. It is the second tallest free-standing structure in France after the Millau Viaduct.' }] * ``` */ class SummarizationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => SummarizationPipelineType} */ (/** @type {any} */ (Text2TextGenerationPipeline))) { /** @type {'summary_text'} */ _key = 'summary_text'; /** * Create a new SummarizationPipeline. * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } } /** * @typedef {Object} TranslationSingle * @property {string} translation_text The translated text. * @typedef {TranslationSingle[]} TranslationOutput * * @callback TranslationPipelineCallback Translate the text(s) given as inputs. * @param {string|string[]} texts Texts to be translated. * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. * @returns {Promise} * * @typedef {TextPipelineConstructorArgs & TranslationPipelineCallback & Disposable} TranslationPipelineType */ /** * Translates text from one language to another. * * **Example:** Multilingual translation w/ `Xenova/nllb-200-distilled-600M`. * * See [here](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200) * for the full list of languages and their corresponding codes. * * ```javascript * const translator = await pipeline('translation', 'Xenova/nllb-200-distilled-600M'); * const output = await translator('जीवन एक चॉकलेट बॉक्स की तरह है।', { * src_lang: 'hin_Deva', // Hindi * tgt_lang: 'fra_Latn', // French * }); * // [{ translation_text: 'La vie est comme une boîte à chocolat.' }] * ``` * * **Example:** Multilingual translation w/ `Xenova/m2m100_418M`. * * See [here](https://huggingface.co/facebook/m2m100_418M#languages-covered) * for the full list of languages and their corresponding codes. * * ```javascript * const translator = await pipeline('translation', 'Xenova/m2m100_418M'); * const output = await translator('生活就像一盒巧克力。', { * src_lang: 'zh', // Chinese * tgt_lang: 'en', // English * }); * // [{ translation_text: 'Life is like a box of chocolate.' }] * ``` * * **Example:** Multilingual translation w/ `Xenova/mbart-large-50-many-to-many-mmt`. * * See [here](https://huggingface.co/facebook/mbart-large-50-many-to-many-mmt#languages-covered) * for the full list of languages and their corresponding codes. * * ```javascript * const translator = await pipeline('translation', 'Xenova/mbart-large-50-many-to-many-mmt'); * const output = await translator('संयुक्त राष्ट्र के प्रमुख का कहना है कि सीरिया में कोई सैन्य समाधान नहीं है', { * src_lang: 'hi_IN', // Hindi * tgt_lang: 'fr_XX', // French * }); * // [{ translation_text: 'Le chef des Nations affirme qu 'il n 'y a military solution in Syria.' }] * ``` */ class TranslationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TranslationPipelineType} */ (/** @type {any} */ (Text2TextGenerationPipeline))) { /** @type {'translation_text'} */ _key = 'translation_text'; /** * Create a new TranslationPipeline. * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } } function isChat(x) { return Array.isArray(x) && x.every(x => 'role' in x && 'content' in x); } /** * @typedef {import('./tokenizers.js').Message[]} Chat * * @typedef {Object} TextGenerationSingle * @property {string|Chat} generated_text The generated text. * @typedef {TextGenerationSingle[]} TextGenerationOutput * * @typedef {Object} TextGenerationSpecificParams Parameters specific to text-generation pipelines. * @property {boolean} [add_special_tokens] Whether or not to add special tokens when tokenizing the sequences. * @property {boolean} [return_full_text=true] If set to `false` only added text is returned, otherwise the full text is returned. * @typedef {import('./generation/configuration_utils.js').GenerationConfig & TextGenerationSpecificParams} TextGenerationConfig * * @callback TextGenerationPipelineCallback Complete the prompt(s) given as inputs. * @param {string|string[]|Chat|Chat[]} texts One or several prompts (or one list of prompts) to complete. * @param {TextGenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. * @returns {Promise} An array or object containing the generated texts. * * @typedef {TextPipelineConstructorArgs & TextGenerationPipelineCallback & Disposable} TextGenerationPipelineType */ /** * Language generation pipeline using any `ModelWithLMHead` or `ModelForCausalLM`. * This pipeline predicts the words that will follow a specified text prompt. * NOTE: For the full list of generation parameters, see [`GenerationConfig`](./utils/generation#module_utils/generation.GenerationConfig). * * **Example:** Text generation with `Xenova/distilgpt2` (default settings). * ```javascript * const generator = await pipeline('text-generation', 'Xenova/distilgpt2'); * const text = 'I enjoy walking with my cute dog,'; * const output = await generator(text); * // [{ generated_text: "I enjoy walking with my cute dog, and I love to play with the other dogs." }] * ``` * * **Example:** Text generation with `Xenova/distilgpt2` (custom settings). * ```javascript * const generator = await pipeline('text-generation', 'Xenova/distilgpt2'); * const text = 'Once upon a time, there was'; * const output = await generator(text, { * temperature: 2, * max_new_tokens: 10, * repetition_penalty: 1.5, * no_repeat_ngram_size: 2, * num_beams: 2, * num_return_sequences: 2, * }); * // [{ * // "generated_text": "Once upon a time, there was an abundance of information about the history and activities that" * // }, { * // "generated_text": "Once upon a time, there was an abundance of information about the most important and influential" * // }] * ``` * * **Example:** Run code generation with `Xenova/codegen-350M-mono`. * ```javascript * const generator = await pipeline('text-generation', 'Xenova/codegen-350M-mono'); * const text = 'def fib(n):'; * const output = await generator(text, { * max_new_tokens: 44, * }); * // [{ * // generated_text: 'def fib(n):\n' + * // ' if n == 0:\n' + * // ' return 0\n' + * // ' elif n == 1:\n' + * // ' return 1\n' + * // ' else:\n' + * // ' return fib(n-1) + fib(n-2)\n' * // }] * ``` */ class TextGenerationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TextGenerationPipelineType} */ (Pipeline)) { /** * Create a new TextGenerationPipeline. * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {TextGenerationPipelineCallback} */ async _call(texts, generate_kwargs = {}) { throw new Error('This pipeline is not yet supported in Transformers.js v3.'); // TODO: Remove when implemented let isBatched = false; let isChatInput = false; // Normalize inputs /** @type {string[]} */ let inputs; if (typeof texts === 'string') { inputs = texts = [texts]; } else if (Array.isArray(texts) && texts.every(x => typeof x === 'string')) { isBatched = true; inputs = /** @type {string[]} */(texts); } else { if (isChat(texts)) { texts = [/** @type {Chat} */(texts)]; } else if (Array.isArray(texts) && texts.every(isChat)) { isBatched = true; } else { throw new Error('Input must be a string, an array of strings, a Chat, or an array of Chats'); } isChatInput = true; // If the input is a chat, we need to apply the chat template inputs = /** @type {string[]} */(/** @type {Chat[]} */ (texts).map( x => this.tokenizer.apply_chat_template(x, { tokenize: false, add_generation_prompt: true, }) )); } // By default, do not add special tokens const add_special_tokens = generate_kwargs.add_special_tokens ?? false; // By default, return full text const return_full_text = isChatInput ? false : generate_kwargs.return_full_text ?? true; this.tokenizer.padding_side = 'left'; const text_inputs = this.tokenizer(inputs, { add_special_tokens, padding: true, truncation: true, }); const outputTokenIds = /** @type {Tensor} */(await this.model.generate({ // inputs: input_ids, // attention_mask, ...text_inputs, ...generate_kwargs })); let decoded = this.tokenizer.batch_decode(outputTokenIds, { skip_special_tokens: true, }); let promptLengths; if (!return_full_text && input_ids.dims.at(-1) > 0) { promptLengths = this.tokenizer.batch_decode(input_ids, { skip_special_tokens: true, }).map(x => x.length); } /** @type {TextGenerationOutput[]} */ const toReturn = Array.from({ length: texts.length }, _ => []); for (let i = 0; i < decoded.length; ++i) { const textIndex = Math.floor(i / outputTokenIds.length * texts.length); if (promptLengths) { // Trim the decoded text to only include the generated part decoded[i] = decoded[i].slice(promptLengths[textIndex]); } toReturn[textIndex].push({ generated_text: isChatInput ? [ ...((/** @type {Chat[]} */(texts)[textIndex])), { role: 'assistant', content: decoded[i] }, ] : decoded[i] }); } return (!isBatched && toReturn.length === 1) ? toReturn[0] : toReturn; } } /** * @typedef {Object} ZeroShotClassificationOutput * @property {string} sequence The sequence for which this is the output. * @property {string[]} labels The labels sorted by order of likelihood. * @property {number[]} scores The probabilities for each of the labels. * * @typedef {Object} ZeroShotClassificationPipelineOptions Parameters specific to zero-shot classification pipelines. * @property {string} [hypothesis_template="This example is {}."] The template used to turn each * candidate label into an NLI-style hypothesis. The candidate label will replace the {} placeholder. * @property {boolean} [multi_label=false] Whether or not multiple candidate labels can be true. * If `false`, the scores are normalized such that the sum of the label likelihoods for each sequence * is 1. If `true`, the labels are considered independent and probabilities are normalized for each * candidate by doing a softmax of the entailment score vs. the contradiction score. * * @callback ZeroShotClassificationPipelineCallback Classify the sequence(s) given as inputs. * @param {string|string[]} texts The sequence(s) to classify, will be truncated if the model input is too large. * @param {string|string[]} candidate_labels The set of possible class labels to classify each sequence into. * Can be a single label, a string of comma-separated labels, or a list of labels. * @param {ZeroShotClassificationPipelineOptions} [options] The options to use for zero-shot classification. * @returns {Promise} An array or object containing the predicted labels and scores. * * @typedef {TextPipelineConstructorArgs & ZeroShotClassificationPipelineCallback & Disposable} ZeroShotClassificationPipelineType */ /** * NLI-based zero-shot classification pipeline using a `ModelForSequenceClassification` * trained on NLI (natural language inference) tasks. Equivalent of `text-classification` * pipelines, but these models don't require a hardcoded number of potential classes, they * can be chosen at runtime. It usually means it's slower but it is **much** more flexible. * * **Example:** Zero shot classification with `Xenova/mobilebert-uncased-mnli`. * ```javascript * const classifier = await pipeline('zero-shot-classification', 'Xenova/mobilebert-uncased-mnli'); * const text = 'Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.'; * const labels = [ 'mobile', 'billing', 'website', 'account access' ]; * const output = await classifier(text, labels); * // { * // sequence: 'Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.', * // labels: [ 'mobile', 'website', 'billing', 'account access' ], * // scores: [ 0.5562091040482018, 0.1843621307860853, 0.13942646639336376, 0.12000229877234923 ] * // } * ``` * * **Example:** Zero shot classification with `Xenova/nli-deberta-v3-xsmall` (multi-label). * ```javascript * const classifier = await pipeline('zero-shot-classification', 'Xenova/nli-deberta-v3-xsmall'); * const text = 'I have a problem with my iphone that needs to be resolved asap!'; * const labels = [ 'urgent', 'not urgent', 'phone', 'tablet', 'computer' ]; * const output = await classifier(text, labels, { multi_label: true }); * // { * // sequence: 'I have a problem with my iphone that needs to be resolved asap!', * // labels: [ 'urgent', 'phone', 'computer', 'tablet', 'not urgent' ], * // scores: [ 0.9958870956360275, 0.9923963400697035, 0.002333537946160235, 0.0015134138567598765, 0.0010699384208377163 ] * // } * ``` */ class ZeroShotClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => ZeroShotClassificationPipelineType} */ (Pipeline)) { /** * Create a new ZeroShotClassificationPipeline. * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); // Use model config to get label2id mapping this.label2id = Object.fromEntries( Object.entries((/** @type {any} */(this).model).config.label2id).map( ([k, v]) => [k.toLowerCase(), v] ) ); this.entailment_id = this.label2id['entailment']; if (this.entailment_id === undefined) { console.warn("Could not find 'entailment' in label2id mapping. Using 2 as entailment_id."); this.entailment_id = 2; } this.contradiction_id = this.label2id['contradiction'] ?? this.label2id['not_entailment']; if (this.contradiction_id === undefined) { console.warn("Could not find 'contradiction' in label2id mapping. Using 0 as contradiction_id."); this.contradiction_id = 0; } } /** @type {ZeroShotClassificationPipelineCallback} */ async _call(texts, candidate_labels, { hypothesis_template = "This example is {}.", multi_label = false, } = {}) { const isBatched = Array.isArray(texts); if (!isBatched) { texts = [/** @type {string} */ (texts)]; } if (!Array.isArray(candidate_labels)) { candidate_labels = [candidate_labels]; } // Insert labels into hypothesis template const hypotheses = candidate_labels.map( x => hypothesis_template.replace('{}', x) ); // How to perform the softmax over the logits: // - true: softmax over the entailment vs. contradiction dim for each label independently // - false: softmax the "entailment" logits over all candidate labels const softmaxEach = multi_label || candidate_labels.length === 1; /** @type {ZeroShotClassificationOutput[]} */ const toReturn = []; for (const premise of texts) { const entails_logits = []; for (const hypothesis of hypotheses) { const inputs = this.tokenizer(premise, { text_pair: hypothesis, padding: true, truncation: true, }) const outputs = await this.model(inputs) if (softmaxEach) { entails_logits.push([ outputs.logits.data[this.contradiction_id], outputs.logits.data[this.entailment_id] ]) } else { entails_logits.push(outputs.logits.data[this.entailment_id]) } } /** @type {number[]} */ const scores = softmaxEach ? entails_logits.map(x => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(x)[1]) : (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(entails_logits); // Sort by scores (desc) and return scores with indices const scores_sorted = scores .map((x, i) => [x, i]) .sort((a, b) => (b[0] - a[0])); toReturn.push({ sequence: premise, labels: scores_sorted.map(x => candidate_labels[x[1]]), scores: scores_sorted.map(x => x[0]), }); } return isBatched ? toReturn : toReturn[0]; } } /** * @typedef {Object} FeatureExtractionPipelineOptions Parameters specific to feature extraction pipelines. * @property {'none'|'mean'|'cls'} [pooling="none"] The pooling method to use. * @property {boolean} [normalize=false] Whether or not to normalize the embeddings in the last dimension. * @property {boolean} [quantize=false] Whether or not to quantize the embeddings. * @property {'binary'|'ubinary'} [precision='binary'] The precision to use for quantization. * * @callback FeatureExtractionPipelineCallback Extract the features of the input(s). * @param {string|string[]} texts One or several texts (or one list of texts) to get the features of. * @param {FeatureExtractionPipelineOptions} [options] The options to use for feature extraction. * @returns {Promise} The features computed by the model. * * @typedef {TextPipelineConstructorArgs & FeatureExtractionPipelineCallback & Disposable} FeatureExtractionPipelineType */ /** * Feature extraction pipeline using no model head. This pipeline extracts the hidden * states from the base transformer, which can be used as features in downstream tasks. * * **Example:** Run feature extraction with `bert-base-uncased` (without pooling/normalization). * ```javascript * const extractor = await pipeline('feature-extraction', 'Xenova/bert-base-uncased', { revision: 'default' }); * const output = await extractor('This is a simple test.'); * // Tensor { * // type: 'float32', * // data: Float32Array [0.05939924716949463, 0.021655935794115067, ...], * // dims: [1, 8, 768] * // } * ``` * * **Example:** Run feature extraction with `bert-base-uncased` (with pooling/normalization). * ```javascript * const extractor = await pipeline('feature-extraction', 'Xenova/bert-base-uncased', { revision: 'default' }); * const output = await extractor('This is a simple test.', { pooling: 'mean', normalize: true }); * // Tensor { * // type: 'float32', * // data: Float32Array [0.03373778983950615, -0.010106077417731285, ...], * // dims: [1, 768] * // } * ``` * * **Example:** Calculating embeddings with `sentence-transformers` models. * ```javascript * const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2'); * const output = await extractor('This is a simple test.', { pooling: 'mean', normalize: true }); * // Tensor { * // type: 'float32', * // data: Float32Array [0.09094982594251633, -0.014774246141314507, ...], * // dims: [1, 384] * // } * ``` * **Example:** Calculating binary embeddings with `sentence-transformers` models. * ```javascript * const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2'); * const output = await extractor('This is a simple test.', { pooling: 'mean', quantize: true, precision: 'binary' }); * // Tensor { * // type: 'int8', * // data: Int8Array [49, 108, 24, ...], * // dims: [1, 48] * // } * ``` */ class FeatureExtractionPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => FeatureExtractionPipelineType} */ (Pipeline)) { /** * Create a new FeatureExtractionPipeline. * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {FeatureExtractionPipelineCallback} */ async _call(texts, { pooling = /** @type {'none'} */('none'), normalize = false, quantize = false, precision = /** @type {'binary'} */('binary'), } = {}) { // Run tokenization const model_inputs = this.tokenizer(texts, { padding: true, truncation: true, }); // Run model const outputs = await this.model(model_inputs) // TODO: Provide warning to the user that they might be using model which was not exported // specifically for feature extraction // console.log(this.model.config) // console.log(outputs) /** @type {Tensor} */ let result = outputs.last_hidden_state ?? outputs.logits; if (pooling === 'none') { // Skip pooling } else if (pooling === 'mean') { result = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.mean_pooling)(result, model_inputs.attention_mask); } else if (pooling === 'cls') { result = result.slice(null, 0); } else { throw Error(`Pooling method '${pooling}' not supported.`); } if (normalize) { result = result.normalize(2, -1); } if (quantize) { result = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.quantize_embeddings)(result, precision); } return result; } } /** * @typedef {Object} ImageFeatureExtractionPipelineOptions Parameters specific to image feature extraction pipelines. * @property {boolean} [pool=null] Whether or not to return the pooled output. If set to `false`, the model will return the raw hidden states. * * @callback ImageFeatureExtractionPipelineCallback Extract the features of the input(s). * @param {ImagePipelineInputs} images One or several images (or one list of images) to get the features of. * @param {ImageFeatureExtractionPipelineOptions} [options] The options to use for image feature extraction. * @returns {Promise} The image features computed by the model. * * @typedef {ImagePipelineConstructorArgs & ImageFeatureExtractionPipelineCallback & Disposable} ImageFeatureExtractionPipelineType */ /** * Image feature extraction pipeline using no model head. This pipeline extracts the hidden * states from the base transformer, which can be used as features in downstream tasks. * * **Example:** Perform image feature extraction with `Xenova/vit-base-patch16-224-in21k`. * ```javascript * const image_feature_extractor = await pipeline('image-feature-extraction', 'Xenova/vit-base-patch16-224-in21k'); * const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png'; * const features = await image_feature_extractor(url); * // Tensor { * // dims: [ 1, 197, 768 ], * // type: 'float32', * // data: Float32Array(151296) [ ... ], * // size: 151296 * // } * ``` * * **Example:** Compute image embeddings with `Xenova/clip-vit-base-patch32`. * ```javascript * const image_feature_extractor = await pipeline('image-feature-extraction', 'Xenova/clip-vit-base-patch32'); * const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png'; * const features = await image_feature_extractor(url); * // Tensor { * // dims: [ 1, 512 ], * // type: 'float32', * // data: Float32Array(512) [ ... ], * // size: 512 * // } * ``` */ class ImageFeatureExtractionPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageFeatureExtractionPipelineType} */ (Pipeline)) { /** * Create a new ImageFeatureExtractionPipeline. * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {ImageFeatureExtractionPipelineCallback} */ async _call(images, { pool = null, } = {}) { const preparedImages = await prepareImages(images); const { pixel_values } = await this.processor(preparedImages); const outputs = await this.model({ pixel_values }); /** @type {Tensor} */ let result; if (pool) { if (!('pooler_output' in outputs)) { throw Error(`No pooled output was returned. Make sure the model has a 'pooler' layer when using the 'pool' option.`); } result = outputs.pooler_output; } else { result = outputs.last_hidden_state ?? outputs.logits ?? outputs.image_embeds; } return result; } } // TODO // export class SentenceSimilarityPipeline extends Pipeline { // } /** * @typedef {Object} AudioClassificationSingle * @property {string} label The label predicted. * @property {number} score The corresponding probability. * @typedef {AudioClassificationSingle[]} AudioClassificationOutput * * @typedef {Object} AudioClassificationPipelineOptions Parameters specific to audio classification pipelines. * @property {number} [topk=null] The number of top labels that will be returned by the pipeline. * If the provided number is `null` or higher than the number of labels available in the model configuration, * it will default to the number of labels. * * @callback AudioClassificationPipelineCallback Classify the sequence(s) given as inputs. * @param {AudioPipelineInputs} audio The input audio file(s) to be classified. The input is either: * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). * @param {AudioClassificationPipelineOptions} [options] The options to use for audio classification. * @returns {Promise} An array or object containing the predicted labels and scores. * * @typedef {AudioPipelineConstructorArgs & AudioClassificationPipelineCallback & Disposable} AudioClassificationPipelineType */ /** * Audio classification pipeline using any `AutoModelForAudioClassification`. * This pipeline predicts the class of a raw waveform or an audio file. * * **Example:** Perform audio classification with `Xenova/wav2vec2-large-xlsr-53-gender-recognition-librispeech`. * ```javascript * const classifier = await pipeline('audio-classification', 'Xenova/wav2vec2-large-xlsr-53-gender-recognition-librispeech'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; * const output = await classifier(url); * // [ * // { label: 'male', score: 0.9981542229652405 }, * // { label: 'female', score: 0.001845747814513743 } * // ] * ``` * * **Example:** Perform audio classification with `Xenova/ast-finetuned-audioset-10-10-0.4593` and return top 4 results. * ```javascript * const classifier = await pipeline('audio-classification', 'Xenova/ast-finetuned-audioset-10-10-0.4593'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cat_meow.wav'; * const output = await classifier(url, { topk: 4 }); * // [ * // { label: 'Meow', score: 0.5617874264717102 }, * // { label: 'Cat', score: 0.22365376353263855 }, * // { label: 'Domestic animals, pets', score: 0.1141069084405899 }, * // { label: 'Animal', score: 0.08985692262649536 }, * // ] * ``` */ class AudioClassificationPipeline extends (/** @type {new (options: AudioPipelineConstructorArgs) => AudioClassificationPipelineType} */ (Pipeline)) { /** * Create a new AudioClassificationPipeline. * @param {AudioPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {AudioClassificationPipelineCallback} */ async _call(audio, { topk = null } = {}) { const single = !Array.isArray(audio); const sampling_rate = this.processor.feature_extractor.config.sampling_rate; const preparedAudios = await prepareAudios(audio, sampling_rate); const id2label = this.model.config.id2label; const toReturn = []; for (const aud of preparedAudios) { const inputs = await this.processor(aud); const output = await this.model(inputs); const logits = output.logits[0]; const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.getTopItems)((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(logits.data), topk); const vals = scores.map(x => ({ label: /** @type {string} */ (id2label[x[0]]), score: /** @type {number} */ (x[1]), })); if (topk === 1) { toReturn.push(...vals); } else { toReturn.push(vals); } } return !single || topk === 1 ? /** @type {AudioClassificationOutput} */ (toReturn) : /** @type {AudioClassificationOutput[]} */ (toReturn)[0]; } } /** * @typedef {Object} ZeroShotAudioClassificationOutput * @property {string} label The label identified by the model. It is one of the suggested `candidate_label`. * @property {number} score The score attributed by the model for that label (between 0 and 1). * * @typedef {Object} ZeroShotAudioClassificationPipelineOptions Parameters specific to zero-shot audio classification pipelines. * @property {string} [hypothesis_template="This is a sound of {}."] The sentence used in conjunction with `candidate_labels` * to attempt the audio classification by replacing the placeholder with the candidate_labels. * Then likelihood is estimated by using `logits_per_audio`. * * @callback ZeroShotAudioClassificationPipelineCallback Classify the sequence(s) given as inputs. * @param {AudioPipelineInputs} audio The input audio file(s) to be classified. The input is either: * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). * @param {string[]} candidate_labels The candidate labels for this audio. * @param {ZeroShotAudioClassificationPipelineOptions} [options] The options to use for zero-shot audio classification. * @returns {Promise} An array of objects containing the predicted labels and scores. * * @typedef {TextAudioPipelineConstructorArgs & ZeroShotAudioClassificationPipelineCallback & Disposable} ZeroShotAudioClassificationPipelineType */ /** * Zero shot audio classification pipeline using `ClapModel`. This pipeline predicts the class of an audio when you * provide an audio and a set of `candidate_labels`. * * **Example**: Perform zero-shot audio classification with `Xenova/clap-htsat-unfused`. * ```javascript * const classifier = await pipeline('zero-shot-audio-classification', 'Xenova/clap-htsat-unfused'); * const audio = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/dog_barking.wav'; * const candidate_labels = ['dog', 'vaccum cleaner']; * const scores = await classifier(audio, candidate_labels); * // [ * // { score: 0.9993992447853088, label: 'dog' }, * // { score: 0.0006007603369653225, label: 'vaccum cleaner' } * // ] * ``` */ class ZeroShotAudioClassificationPipeline extends (/** @type {new (options: TextAudioPipelineConstructorArgs) => ZeroShotAudioClassificationPipelineType} */ (Pipeline)) { /** * Create a new ZeroShotAudioClassificationPipeline. * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {ZeroShotAudioClassificationPipelineCallback} */ async _call(audio, candidate_labels, { hypothesis_template = "This is a sound of {}." } = {}) { const single = !Array.isArray(audio); if (single) { audio = [/** @type {AudioInput} */ (audio)]; } // Insert label into hypothesis template const texts = candidate_labels.map( x => hypothesis_template.replace('{}', x) ); // Run tokenization const text_inputs = this.tokenizer(texts, { padding: true, truncation: true, }); const sampling_rate = this.processor.feature_extractor.config.sampling_rate; const preparedAudios = await prepareAudios(audio, sampling_rate); const toReturn = []; for (const aud of preparedAudios) { const audio_inputs = await this.processor(aud); // Run model with both text and audio inputs const output = await this.model({ ...text_inputs, ...audio_inputs }); // Compute softmax per audio const probs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(output.logits_per_audio.data); toReturn.push([...probs].map((x, i) => ({ score: x, label: candidate_labels[i] }))); } return single ? toReturn[0] : toReturn; } } /** * @typedef {{stride: number[], input_features: Tensor, is_last: boolean, tokens?: number[], token_timestamps?: number[]}} ChunkCallbackItem * @callback ChunkCallback * @param {ChunkCallbackItem} chunk The chunk to process. */ /** * @typedef {Object} Chunk * @property {[number, number]} timestamp The start and end timestamp of the chunk in seconds. * @property {string} text The recognized text. */ /** * @typedef {Object} AutomaticSpeechRecognitionOutput * @property {string} text The recognized text. * @property {Chunk[]} [chunks] When using `return_timestamps`, the `chunks` will become a list * containing all the various text chunks identified by the model. * * @typedef {Object} AutomaticSpeechRecognitionSpecificParams Parameters specific to automatic-speech-recognition pipelines. * @property {boolean|'word'} [kwargs.return_timestamps] Whether to return timestamps or not. Default is `false`. * @property {number} [kwargs.chunk_length_s] The length of audio chunks to process in seconds. Default is 0 (no chunking). * @property {number} [kwargs.stride_length_s] The length of overlap between consecutive audio chunks in seconds. If not provided, defaults to `chunk_length_s / 6`. * @property {ChunkCallback} [kwargs.chunk_callback] Callback function to be called with each chunk processed. * @property {boolean} [kwargs.force_full_sequences] Whether to force outputting full sequences or not. Default is `false`. * @property {string} [kwargs.language] The source language. Default is `null`, meaning it should be auto-detected. Use this to potentially improve performance if the source language is known. * @property {string} [kwargs.task] The task to perform. Default is `null`, meaning it should be auto-detected. * @property {number[][]} [kwargs.forced_decoder_ids] A list of pairs of integers which indicates a mapping from generation indices to token indices * that will be forced before sampling. For example, [[1, 123]] means the second generated token will always be a token of index 123. * @property {number} [num_frames] The number of frames in the input audio. * @typedef {import('./generation/configuration_utils.js').GenerationConfig & AutomaticSpeechRecognitionSpecificParams} AutomaticSpeechRecognitionConfig * * @callback AutomaticSpeechRecognitionPipelineCallback Transcribe the audio sequence(s) given as inputs to text. * @param {AudioPipelineInputs} audio The input audio file(s) to be transcribed. The input is either: * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). * @param {AutomaticSpeechRecognitionConfig} [options] Additional keyword arguments to pass along to the generate method of the model. * @returns {Promise} An object containing the transcription text and optionally timestamps if `return_timestamps` is `true`. * * @typedef {TextAudioPipelineConstructorArgs & AutomaticSpeechRecognitionPipelineCallback & Disposable} AutomaticSpeechRecognitionPipelineType */ /** * Pipeline that aims at extracting spoken text contained within some audio. * * **Example:** Transcribe English. * ```javascript * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; * const output = await transcriber(url); * // { text: " And so my fellow Americans ask not what your country can do for you, ask what you can do for your country." } * ``` * * **Example:** Transcribe English w/ timestamps. * ```javascript * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; * const output = await transcriber(url, { return_timestamps: true }); * // { * // text: " And so my fellow Americans ask not what your country can do for you, ask what you can do for your country." * // chunks: [ * // { timestamp: [0, 8], text: " And so my fellow Americans ask not what your country can do for you" } * // { timestamp: [8, 11], text: " ask what you can do for your country." } * // ] * // } * ``` * * **Example:** Transcribe English w/ word-level timestamps. * ```javascript * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; * const output = await transcriber(url, { return_timestamps: 'word' }); * // { * // "text": " And so my fellow Americans ask not what your country can do for you ask what you can do for your country.", * // "chunks": [ * // { "text": " And", "timestamp": [0, 0.78] }, * // { "text": " so", "timestamp": [0.78, 1.06] }, * // { "text": " my", "timestamp": [1.06, 1.46] }, * // ... * // { "text": " for", "timestamp": [9.72, 9.92] }, * // { "text": " your", "timestamp": [9.92, 10.22] }, * // { "text": " country.", "timestamp": [10.22, 13.5] } * // ] * // } * ``` * * **Example:** Transcribe French. * ```javascript * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/french-audio.mp3'; * const output = await transcriber(url, { language: 'french', task: 'transcribe' }); * // { text: " J'adore, j'aime, je n'aime pas, je déteste." } * ``` * * **Example:** Translate French to English. * ```javascript * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/french-audio.mp3'; * const output = await transcriber(url, { language: 'french', task: 'translate' }); * // { text: " I love, I like, I don't like, I hate." } * ``` * * **Example:** Transcribe/translate audio longer than 30 seconds. * ```javascript * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/ted_60.wav'; * const output = await transcriber(url, { chunk_length_s: 30, stride_length_s: 5 }); * // { text: " So in college, I was a government major, which means [...] So I'd start off light and I'd bump it up" } * ``` */ class AutomaticSpeechRecognitionPipeline extends (/** @type {new (options: TextAudioPipelineConstructorArgs) => AutomaticSpeechRecognitionPipelineType} */ (Pipeline)) { /** * Create a new AutomaticSpeechRecognitionPipeline. * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {AutomaticSpeechRecognitionPipelineCallback} */ async _call(audio, kwargs = {}) { throw new Error('This pipeline is not yet supported in Transformers.js v3.'); // TODO: Remove when implemented switch (this.model.config.model_type) { case 'whisper': return this._call_whisper(audio, kwargs) case 'wav2vec2': case 'wav2vec2-bert': case 'unispeech': case 'unispeech-sat': case 'hubert': return this._call_wav2vec2(audio, kwargs) default: throw new Error(`AutomaticSpeechRecognitionPipeline does not support model type '${this.model.config.model_type}'.`) } } /** * @type {AutomaticSpeechRecognitionPipelineCallback} * @private */ async _call_wav2vec2(audio, kwargs = {}) { // TODO use kwargs if (kwargs.language) { console.warn('`language` parameter is not yet supported for `wav2vec2` models, defaulting to "English".'); } if (kwargs.task) { console.warn('`task` parameter is not yet supported for `wav2vec2` models, defaulting to "transcribe".'); } const single = !Array.isArray(audio); if (single) { audio = [/** @type {AudioInput} */ (audio)]; } const sampling_rate = this.processor.feature_extractor.config.sampling_rate; const preparedAudios = await prepareAudios(audio, sampling_rate); const toReturn = []; for (const aud of preparedAudios) { const inputs = await this.processor(aud); const output = await this.model(inputs); const logits = output.logits[0]; const predicted_ids = []; for (const item of logits) { predicted_ids.push((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(item.data)[1]) } const predicted_sentences = this.tokenizer.decode(predicted_ids) toReturn.push({ text: predicted_sentences }) } return single ? toReturn[0] : toReturn; } /** * @type {AutomaticSpeechRecognitionPipelineCallback} * @private */ async _call_whisper(audio, kwargs = {}) { const return_timestamps = kwargs.return_timestamps ?? false; const chunk_length_s = kwargs.chunk_length_s ?? 0; const chunk_callback = kwargs.chunk_callback ?? null; const force_full_sequences = kwargs.force_full_sequences ?? false; let stride_length_s = kwargs.stride_length_s ?? null; if (return_timestamps === 'word') { kwargs['return_token_timestamps'] = true; } const language = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pop)(kwargs, 'language', null); const task = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pop)(kwargs, 'task', null); if (language || task || return_timestamps) { if (kwargs.forced_decoder_ids) { throw new Error("Cannot specify `language`/`task`/`return_timestamps` and `forced_decoder_ids` at the same time.") } // @ts-ignore const decoder_prompt_ids = this.tokenizer.get_decoder_prompt_ids({ language, task, no_timestamps: !return_timestamps }) if (decoder_prompt_ids.length > 0) { kwargs.forced_decoder_ids = decoder_prompt_ids; } } const single = !Array.isArray(audio); if (single) { audio = [/** @type {AudioInput} */ (audio)]; } const time_precision = this.processor.feature_extractor.config.chunk_length / this.model.config.max_source_positions; const hop_length = this.processor.feature_extractor.config.hop_length; const sampling_rate = this.processor.feature_extractor.config.sampling_rate; const preparedAudios = await prepareAudios(audio, sampling_rate); const toReturn = []; for (const aud of preparedAudios) { /** @type {ChunkCallbackItem[]} */ let chunks = []; if (chunk_length_s > 0) { if (stride_length_s === null) { stride_length_s = chunk_length_s / 6; } else if (chunk_length_s <= stride_length_s) { throw Error("`chunk_length_s` must be larger than `stride_length_s`.") } // TODO support different stride_length_s (for left and right) const window = sampling_rate * chunk_length_s; const stride = sampling_rate * stride_length_s; const jump = window - 2 * stride; let offset = 0; // Create subarrays of audio with overlaps while (offset < aud.length) { const subarr = aud.subarray(offset, offset + window); const feature = await this.processor(subarr); const isFirst = offset === 0; const isLast = offset + jump >= aud.length; chunks.push({ stride: [ subarr.length, isFirst ? 0 : stride, isLast ? 0 : stride ], input_features: feature.input_features, is_last: isLast }) offset += jump; } } else { chunks = [{ stride: [aud.length, 0, 0], input_features: (await this.processor(aud)).input_features, is_last: true }] } // Generate for each set of input features for (const chunk of chunks) { kwargs.num_frames = Math.floor(chunk.stride[0] / hop_length); // NOTE: doing sequentially for now const data = await this.model.generate({ inputs: chunk.input_features, ...kwargs }); // TODO: Right now we only get top beam if (return_timestamps === 'word') { chunk.tokens = data.sequences[0]; chunk.token_timestamps = data.token_timestamps.tolist()[0].map( (/** @type {number} */ x) => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.round)(x, 2) ); } else { chunk.tokens = data[0]; } // convert stride to seconds chunk.stride = chunk.stride.map(x => x / sampling_rate); if (chunk_callback !== null) { chunk_callback(chunk) } } // Merge text chunks // @ts-ignore const [full_text, optional] = this.tokenizer._decode_asr(chunks, { time_precision, return_timestamps, force_full_sequences }); toReturn.push({ text: full_text, ...optional }) } return single ? toReturn[0] : toReturn; } } /** * @typedef {Object} ImageToTextSingle * @property {string} generated_text The generated text. * @typedef {ImageToTextSingle[]} ImageToTextOutput * * @callback ImageToTextPipelineCallback Assign labels to the image(s) passed as inputs. * @param {ImagePipelineInputs} texts The images to be captioned. * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. * @returns {Promise} An object (or array of objects) containing the generated text(s). * * @typedef {TextImagePipelineConstructorArgs & ImageToTextPipelineCallback & Disposable} ImageToTextPipelineType */ /** * Image To Text pipeline using a `AutoModelForVision2Seq`. This pipeline predicts a caption for a given image. * * **Example:** Generate a caption for an image w/ `Xenova/vit-gpt2-image-captioning`. * ```javascript * const captioner = await pipeline('image-to-text', 'Xenova/vit-gpt2-image-captioning'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; * const output = await captioner(url); * // [{ generated_text: 'a cat laying on a couch with another cat' }] * ``` * * **Example:** Optical Character Recognition (OCR) w/ `Xenova/trocr-small-handwritten`. * ```javascript * const captioner = await pipeline('image-to-text', 'Xenova/trocr-small-handwritten'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/handwriting.jpg'; * const output = await captioner(url); * // [{ generated_text: 'Mr. Brown commented icily.' }] * ``` */ class ImageToTextPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ImageToTextPipelineType} */ (Pipeline)) { /** * Create a new ImageToTextPipeline. * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {ImageToTextPipelineCallback} */ async _call(images, generate_kwargs = {}) { throw new Error('This pipeline is not yet supported in Transformers.js v3.'); // TODO: Remove when implemented const isBatched = Array.isArray(images); const preparedImages = await prepareImages(images); const { pixel_values } = await this.processor(preparedImages); const toReturn = []; for (const batch of pixel_values) { batch.dims = [1, ...batch.dims] const output = await this.model.generate({ inputs: batch, ...generate_kwargs }); const decoded = this.tokenizer.batch_decode(/** @type {Tensor} */(output), { skip_special_tokens: true, }).map(x => ({ generated_text: x.trim() })) toReturn.push(decoded); } return isBatched ? toReturn : toReturn[0]; } } /** * @typedef {Object} ImageClassificationSingle * @property {string} label The label identified by the model. * @property {number} score The score attributed by the model for that label. * @typedef {ImageClassificationSingle[]} ImageClassificationOutput * * @typedef {Object} ImageClassificationPipelineOptions Parameters specific to image classification pipelines. * @property {number} [topk=1] The number of top labels that will be returned by the pipeline. * * @callback ImageClassificationPipelineCallback Assign labels to the image(s) passed as inputs. * @param {ImagePipelineInputs} images The input images(s) to be classified. * @param {ImageClassificationPipelineOptions} [options] The options to use for image classification. * @returns {Promise} An array or object containing the predicted labels and scores. * * @typedef {ImagePipelineConstructorArgs & ImageClassificationPipelineCallback & Disposable} ImageClassificationPipelineType */ /** * Image classification pipeline using any `AutoModelForImageClassification`. * This pipeline predicts the class of an image. * * **Example:** Classify an image. * ```javascript * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; * const output = await classifier(url); * // [ * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, * // ] * ``` * * **Example:** Classify an image and return top `n` classes. * ```javascript * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; * const output = await classifier(url, { topk: 3 }); * // [ * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, * // { label: 'tiger cat', score: 0.3634825646877289 }, * // { label: 'lion, king of beasts, Panthera leo', score: 0.00045060308184474707 }, * // ] * ``` * * **Example:** Classify an image and return all classes. * ```javascript * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; * const output = await classifier(url, { topk: 0 }); * // [ * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, * // { label: 'tiger cat', score: 0.3634825646877289 }, * // { label: 'lion, king of beasts, Panthera leo', score: 0.00045060308184474707 }, * // { label: 'jaguar, panther, Panthera onca, Felis onca', score: 0.00035465499968267977 }, * // ... * // ] * ``` */ class ImageClassificationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageClassificationPipelineType} */ (Pipeline)) { /** * Create a new ImageClassificationPipeline. * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {ImageClassificationPipelineCallback} */ async _call(images, { topk = 1 } = {}) { const isBatched = Array.isArray(images); const preparedImages = await prepareImages(images); const { pixel_values } = await this.processor(preparedImages); const output = await this.model({ pixel_values }); const id2label = this.model.config.id2label; const toReturn = []; for (const batch of output.logits) { const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.getTopItems)((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data), topk); const vals = scores.map(x => ({ label: id2label[x[0]], score: x[1], })); if (topk === 1) { toReturn.push(...vals); } else { toReturn.push(vals); } } return isBatched || topk === 1 ? /** @type {ImageClassificationOutput} */ (toReturn) : /** @type {ImageClassificationOutput[]} */ (toReturn)[0]; } } /** * @typedef {Object} ImageSegmentationPipelineOutput * @property {string} label The label of the segment. * @property {number|null} score The score of the segment. * @property {RawImage} mask The mask of the segment. * * @typedef {Object} ImageSegmentationPipelineOptions Parameters specific to image segmentation pipelines. * @property {number} [threshold=0.5] Probability threshold to filter out predicted masks. * @property {number} [mask_threshold=0.5] Threshold to use when turning the predicted masks into binary values. * @property {number} [overlap_mask_area_threshold=0.8] Mask overlap threshold to eliminate small, disconnected segments. * @property {null|string} [subtask=null] Segmentation task to be performed. One of [`panoptic`, `instance`, and `semantic`], * depending on model capabilities. If not set, the pipeline will attempt to resolve (in that order). * @property {number[]} [label_ids_to_fuse=null] List of label ids to fuse. If not set, do not fuse any labels. * @property {number[][]} [target_sizes=null] List of target sizes for the input images. If not set, use the original image sizes. * * @callback ImageSegmentationPipelineCallback Segment the input images. * @param {ImagePipelineInputs} images The input images. * @param {ImageSegmentationPipelineOptions} [options] The options to use for image segmentation. * @returns {Promise} The annotated segments. * * @typedef {ImagePipelineConstructorArgs & ImageSegmentationPipelineCallback & Disposable} ImageSegmentationPipelineType */ /** * Image segmentation pipeline using any `AutoModelForXXXSegmentation`. * This pipeline predicts masks of objects and their classes. * * **Example:** Perform image segmentation with `Xenova/detr-resnet-50-panoptic`. * ```javascript * const segmenter = await pipeline('image-segmentation', 'Xenova/detr-resnet-50-panoptic'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; * const output = await segmenter(url); * // [ * // { label: 'remote', score: 0.9984649419784546, mask: RawImage { ... } }, * // { label: 'cat', score: 0.9994316101074219, mask: RawImage { ... } } * // ] * ``` */ class ImageSegmentationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageSegmentationPipelineType} */ (Pipeline)) { /** * Create a new ImageSegmentationPipeline. * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); this.subtasks_mapping = { // Mapping of subtasks to their corresponding post-processing function names. panoptic: 'post_process_panoptic_segmentation', instance: 'post_process_instance_segmentation', semantic: 'post_process_semantic_segmentation' } } /** @type {ImageSegmentationPipelineCallback} */ async _call(images, { threshold = 0.5, mask_threshold = 0.5, overlap_mask_area_threshold = 0.8, label_ids_to_fuse = null, target_sizes = null, subtask = null, } = {}) { const isBatched = Array.isArray(images); if (isBatched && images.length !== 1) { throw Error("Image segmentation pipeline currently only supports a batch size of 1."); } const preparedImages = await prepareImages(images); const imageSizes = preparedImages.map(x => [x.height, x.width]); const { pixel_values, pixel_mask } = await this.processor(preparedImages); const output = await this.model({ pixel_values, pixel_mask }); let fn = null; if (subtask !== null) { fn = this.subtasks_mapping[subtask]; } else { for (let [task, func] of Object.entries(this.subtasks_mapping)) { if (func in this.processor.feature_extractor) { fn = this.processor.feature_extractor[func].bind(this.processor.feature_extractor); subtask = task; break; } } } const id2label = this.model.config.id2label; /** @type {ImageSegmentationPipelineOutput[]} */ const annotation = []; if (subtask === 'panoptic' || subtask === 'instance') { const processed = fn( output, threshold, mask_threshold, overlap_mask_area_threshold, label_ids_to_fuse, target_sizes ?? imageSizes, // TODO FIX? )[0]; const segmentation = processed.segmentation; for (const segment of processed.segments_info) { const maskData = new Uint8ClampedArray(segmentation.data.length); for (let i = 0; i < segmentation.data.length; ++i) { if (segmentation.data[i] === segment.id) { maskData[i] = 255; } } const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1) annotation.push({ score: segment.score, label: id2label[segment.label_id], mask: mask }) } } else if (subtask === 'semantic') { const { segmentation, labels } = fn(output, target_sizes ?? imageSizes)[0]; for (const label of labels) { const maskData = new Uint8ClampedArray(segmentation.data.length); for (let i = 0; i < segmentation.data.length; ++i) { if (segmentation.data[i] === label) { maskData[i] = 255; } } const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1); annotation.push({ score: null, label: id2label[label], mask: mask }); } } else { throw Error(`Subtask ${subtask} not supported.`); } return annotation; } } /** * @typedef {Object} ZeroShotImageClassificationOutput * @property {string} label The label identified by the model. It is one of the suggested `candidate_label`. * @property {number} score The score attributed by the model for that label (between 0 and 1). * * @typedef {Object} ZeroShotImageClassificationPipelineOptions Parameters specific to zero-shot image classification pipelines. * @property {string} [hypothesis_template="This is a photo of {}"] The sentence used in conjunction with `candidate_labels` * to attempt the image classification by replacing the placeholder with the candidate_labels. * Then likelihood is estimated by using `logits_per_image`. * * @callback ZeroShotImageClassificationPipelineCallback Assign labels to the image(s) passed as inputs. * @param {ImagePipelineInputs} images The input images. * @param {string[]} candidate_labels The candidate labels for this image. * @param {ZeroShotImageClassificationPipelineOptions} [options] The options to use for zero-shot image classification. * @returns {Promise} An array of objects containing the predicted labels and scores. * * @typedef {TextImagePipelineConstructorArgs & ZeroShotImageClassificationPipelineCallback & Disposable} ZeroShotImageClassificationPipelineType */ /** * Zero shot image classification pipeline. This pipeline predicts the class of * an image when you provide an image and a set of `candidate_labels`. * * **Example:** Zero shot image classification w/ `Xenova/clip-vit-base-patch32`. * ```javascript * const classifier = await pipeline('zero-shot-image-classification', 'Xenova/clip-vit-base-patch32'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; * const output = await classifier(url, ['tiger', 'horse', 'dog']); * // [ * // { score: 0.9993917942047119, label: 'tiger' }, * // { score: 0.0003519294841680676, label: 'horse' }, * // { score: 0.0002562698791734874, label: 'dog' } * // ] * ``` */ class ZeroShotImageClassificationPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotImageClassificationPipelineType} */ (Pipeline)) { /** * Create a new ZeroShotImageClassificationPipeline. * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {ZeroShotImageClassificationPipelineCallback} */ async _call(images, candidate_labels, { hypothesis_template = "This is a photo of {}" } = {}) { const isBatched = Array.isArray(images); const preparedImages = await prepareImages(images); // Insert label into hypothesis template const texts = candidate_labels.map( x => hypothesis_template.replace('{}', x) ); // Run tokenization const text_inputs = this.tokenizer(texts, { padding: this.model.config.model_type === 'siglip' ? 'max_length' : true, truncation: true, }); // Run processor const { pixel_values } = await this.processor(preparedImages); // Run model with both text and pixel inputs const output = await this.model({ ...text_inputs, pixel_values }); const function_to_apply = this.model.config.model_type === 'siglip' ? batch => batch.sigmoid().data : batch => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data); // Compare each image with each candidate label const toReturn = []; for (const batch of output.logits_per_image) { // Compute softmax per image const probs = function_to_apply(batch); const result = [...probs].map((x, i) => ({ score: x, label: candidate_labels[i] })); result.sort((a, b) => b.score - a.score); // sort by score in descending order toReturn.push(result); } return isBatched ? toReturn : toReturn[0]; } } /** * @typedef {Object} ObjectDetectionPipelineSingle * @property {string} label The class label identified by the model. * @property {number} score The score attributed by the model for that label. * @property {BoundingBox} box The bounding box of detected object in image's original size, or as a percentage if `percentage` is set to true. * @typedef {ObjectDetectionPipelineSingle[]} ObjectDetectionPipelineOutput * * @typedef {Object} ObjectDetectionPipelineOptions Parameters specific to object detection pipelines. * @property {number} [threshold=0.9] The threshold used to filter boxes by score. * @property {boolean} [percentage=false] Whether to return the boxes coordinates in percentage (true) or in pixels (false). * * @callback ObjectDetectionPipelineCallback Detect objects (bounding boxes & classes) in the image(s) passed as inputs. * @param {ImagePipelineInputs} images The input images. * @param {ObjectDetectionPipelineOptions} [options] The options to use for object detection. * @returns {Promise} A list of objects or a list of list of objects. * * @typedef {ImagePipelineConstructorArgs & ObjectDetectionPipelineCallback & Disposable} ObjectDetectionPipelineType */ /** * Object detection pipeline using any `AutoModelForObjectDetection`. * This pipeline predicts bounding boxes of objects and their classes. * * **Example:** Run object-detection with `Xenova/detr-resnet-50`. * ```javascript * const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50'); * const img = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; * const output = await detector(img, { threshold: 0.9 }); * // [{ * // score: 0.9976370930671692, * // label: "remote", * // box: { xmin: 31, ymin: 68, xmax: 190, ymax: 118 } * // }, * // ... * // { * // score: 0.9984092116355896, * // label: "cat", * // box: { xmin: 331, ymin: 19, xmax: 649, ymax: 371 } * // }] * ``` */ class ObjectDetectionPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ObjectDetectionPipelineType} */ (Pipeline)) { /** * Create a new ObjectDetectionPipeline. * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {ObjectDetectionPipelineCallback} */ async _call(images, { threshold = 0.9, percentage = false, } = {}) { const isBatched = Array.isArray(images); if (isBatched && images.length !== 1) { throw Error("Object detection pipeline currently only supports a batch size of 1."); } const preparedImages = await prepareImages(images); const imageSizes = percentage ? null : preparedImages.map(x => [x.height, x.width]); const { pixel_values, pixel_mask } = await this.processor(preparedImages); const output = await this.model({ pixel_values, pixel_mask }); // @ts-ignore const processed = this.processor.feature_extractor.post_process_object_detection(output, threshold, imageSizes); // Add labels const id2label = this.model.config.id2label; // Format output /** @type {ObjectDetectionPipelineOutput[]} */ const result = processed.map(batch => ( batch.boxes.map((box, i) => ({ score: batch.scores[i], label: id2label[batch.classes[i]], box: get_bounding_box(box, !percentage), })) )) return isBatched ? result : result[0]; } } /** * @typedef {Object} ZeroShotObjectDetectionOutput * @property {string} label Text query corresponding to the found object. * @property {number} score Score corresponding to the object (between 0 and 1). * @property {BoundingBox} box Bounding box of the detected object in image's original size, or as a percentage if `percentage` is set to true. * * @typedef {Object} ZeroShotObjectDetectionPipelineOptions Parameters specific to zero-shot object detection pipelines. * @property {number} [threshold=0.1] The probability necessary to make a prediction. * @property {number} [topk=null] The number of top predictions that will be returned by the pipeline. * If the provided number is `null` or higher than the number of predictions available, it will default * to the number of predictions. * @property {boolean} [percentage=false] Whether to return the boxes coordinates in percentage (true) or in pixels (false). * * @callback ZeroShotObjectDetectionPipelineCallback Detect objects (bounding boxes & classes) in the image(s) passed as inputs. * @param {ImagePipelineInputs} images The input images. * @param {string[]} candidate_labels What the model should recognize in the image. * @param {ZeroShotObjectDetectionPipelineOptions} [options] The options to use for zero-shot object detection. * @returns {Promise} An array of objects containing the predicted labels, scores, and bounding boxes. * * @typedef {TextImagePipelineConstructorArgs & ZeroShotObjectDetectionPipelineCallback & Disposable} ZeroShotObjectDetectionPipelineType */ /** * Zero-shot object detection pipeline. This pipeline predicts bounding boxes of * objects when you provide an image and a set of `candidate_labels`. * * **Example:** Zero-shot object detection w/ `Xenova/owlvit-base-patch32`. * ```javascript * const detector = await pipeline('zero-shot-object-detection', 'Xenova/owlvit-base-patch32'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/astronaut.png'; * const candidate_labels = ['human face', 'rocket', 'helmet', 'american flag']; * const output = await detector(url, candidate_labels); * // [ * // { * // score: 0.24392342567443848, * // label: 'human face', * // box: { xmin: 180, ymin: 67, xmax: 274, ymax: 175 } * // }, * // { * // score: 0.15129457414150238, * // label: 'american flag', * // box: { xmin: 0, ymin: 4, xmax: 106, ymax: 513 } * // }, * // { * // score: 0.13649864494800568, * // label: 'helmet', * // box: { xmin: 277, ymin: 337, xmax: 511, ymax: 511 } * // }, * // { * // score: 0.10262022167444229, * // label: 'rocket', * // box: { xmin: 352, ymin: -1, xmax: 463, ymax: 287 } * // } * // ] * ``` * * **Example:** Zero-shot object detection w/ `Xenova/owlvit-base-patch32` (returning top 4 matches and setting a threshold). * ```javascript * const detector = await pipeline('zero-shot-object-detection', 'Xenova/owlvit-base-patch32'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/beach.png'; * const candidate_labels = ['hat', 'book', 'sunglasses', 'camera']; * const output = await detector(url, candidate_labels, { topk: 4, threshold: 0.05 }); * // [ * // { * // score: 0.1606510728597641, * // label: 'sunglasses', * // box: { xmin: 347, ymin: 229, xmax: 429, ymax: 264 } * // }, * // { * // score: 0.08935828506946564, * // label: 'hat', * // box: { xmin: 38, ymin: 174, xmax: 258, ymax: 364 } * // }, * // { * // score: 0.08530698716640472, * // label: 'camera', * // box: { xmin: 187, ymin: 350, xmax: 260, ymax: 411 } * // }, * // { * // score: 0.08349756896495819, * // label: 'book', * // box: { xmin: 261, ymin: 280, xmax: 494, ymax: 425 } * // } * // ] * ``` */ class ZeroShotObjectDetectionPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotObjectDetectionPipelineType} */ (Pipeline)) { /** * Create a new ZeroShotObjectDetectionPipeline. * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {ZeroShotObjectDetectionPipelineCallback} */ async _call(images, candidate_labels, { threshold = 0.1, topk = null, percentage = false, } = {}) { const isBatched = Array.isArray(images); const preparedImages = await prepareImages(images); // Run tokenization const text_inputs = this.tokenizer(candidate_labels, { padding: true, truncation: true, }); // Run processor const model_inputs = await this.processor(preparedImages); // Since non-maximum suppression is performed for exporting, we need to // process each image separately. For more information, see: // https://github.com/huggingface/optimum/blob/e3b7efb1257c011db907ef40ab340e795cc5684c/optimum/exporters/onnx/model_configs.py#L1028-L1032 const toReturn = []; for (let i = 0; i < preparedImages.length; ++i) { const image = preparedImages[i]; const imageSize = percentage ? null : [[image.height, image.width]]; const pixel_values = model_inputs.pixel_values[i].unsqueeze_(0); // Run model with both text and pixel inputs const output = await this.model({ ...text_inputs, pixel_values }); // @ts-ignore const processed = this.processor.feature_extractor.post_process_object_detection(output, threshold, imageSize, true)[0]; let result = processed.boxes.map((box, i) => ({ score: processed.scores[i], label: candidate_labels[processed.classes[i]], box: get_bounding_box(box, !percentage), })).sort((a, b) => b.score - a.score); if (topk !== null) { result = result.slice(0, topk); } toReturn.push(result) } return isBatched ? toReturn : toReturn[0]; } } /** * @typedef {Object} DocumentQuestionAnsweringSingle * @property {string} answer The generated text. * @typedef {DocumentQuestionAnsweringSingle[]} DocumentQuestionAnsweringOutput * * @callback DocumentQuestionAnsweringPipelineCallback Answer the question given as input by using the document. * @param {ImageInput} image The image of the document to use. * @param {string} question A question to ask of the document. * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. * @returns {Promise} An object (or array of objects) containing the answer(s). * * @typedef {TextImagePipelineConstructorArgs & DocumentQuestionAnsweringPipelineCallback & Disposable} DocumentQuestionAnsweringPipelineType */ /** * Document Question Answering pipeline using any `AutoModelForDocumentQuestionAnswering`. * The inputs/outputs are similar to the (extractive) question answering pipeline; however, * the pipeline takes an image (and optional OCR'd words/boxes) as input instead of text context. * * **Example:** Answer questions about a document with `Xenova/donut-base-finetuned-docvqa`. * ```javascript * const qa_pipeline = await pipeline('document-question-answering', 'Xenova/donut-base-finetuned-docvqa'); * const image = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/invoice.png'; * const question = 'What is the invoice number?'; * const output = await qa_pipeline(image, question); * // [{ answer: 'us-001' }] * ``` */ class DocumentQuestionAnsweringPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => DocumentQuestionAnsweringPipelineType} */ (Pipeline)) { /** * Create a new DocumentQuestionAnsweringPipeline. * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {DocumentQuestionAnsweringPipelineCallback} */ async _call(image, question, generate_kwargs = {}) { throw new Error('This pipeline is not yet supported in Transformers.js v3.'); // TODO: Remove when implemented // NOTE: For now, we only support a batch size of 1 // Preprocess image const preparedImage = (await prepareImages(image))[0]; const { pixel_values } = await this.processor(preparedImage); // Run tokenization const task_prompt = `${question}`; const decoder_input_ids = this.tokenizer(task_prompt, { add_special_tokens: false, padding: true, truncation: true, }).input_ids; // Run model const output = await this.model.generate({ inputs: pixel_values, max_length: this.model.config.decoder.max_position_embeddings, decoder_input_ids, ...generate_kwargs, }); // Decode output const decoded = this.tokenizer.batch_decode(/** @type {Tensor} */(output))[0]; // Parse answer const match = decoded.match(/(.*?)<\/s_answer>/); let answer = null; if (match && match.length >= 2) { answer = match[1].trim(); } return [{ answer }]; } } /** * @typedef {Object} VocoderOptions * @property {PreTrainedModel} [vocoder] The vocoder used by the pipeline (if the model uses one). If not provided, use the default HifiGan vocoder. * @typedef {TextAudioPipelineConstructorArgs & VocoderOptions} TextToAudioPipelineConstructorArgs */ /** * @typedef {Object} TextToAudioOutput * @property {Float32Array} audio The generated audio waveform. * @property {number} sampling_rate The sampling rate of the generated audio waveform. * * @typedef {Object} TextToAudioPipelineOptions Parameters specific to text-to-audio pipelines. * @property {Tensor|Float32Array|string|URL} [speaker_embeddings=null] The speaker embeddings (if the model requires it). * * @callback TextToAudioPipelineCallback Generates speech/audio from the inputs. * @param {string|string[]} texts The text(s) to generate. * @param {TextToAudioPipelineOptions} options Parameters passed to the model generation/forward method. * @returns {Promise} An object containing the generated audio and sampling rate. * * @typedef {TextToAudioPipelineConstructorArgs & TextToAudioPipelineCallback & Disposable} TextToAudioPipelineType */ /** * Text-to-audio generation pipeline using any `AutoModelForTextToWaveform` or `AutoModelForTextToSpectrogram`. * This pipeline generates an audio file from an input text and optional other conditional inputs. * * **Example:** Generate audio from text with `Xenova/speecht5_tts`. * ```javascript * const synthesizer = await pipeline('text-to-speech', 'Xenova/speecht5_tts', { quantized: false }); * const speaker_embeddings = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin'; * const out = await synthesizer('Hello, my dog is cute', { speaker_embeddings }); * // { * // audio: Float32Array(26112) [-0.00005657337896991521, 0.00020583874720614403, ...], * // sampling_rate: 16000 * // } * ``` * * You can then save the audio to a .wav file with the `wavefile` package: * ```javascript * import wavefile from 'wavefile'; * import fs from 'fs'; * * const wav = new wavefile.WaveFile(); * wav.fromScratch(1, out.sampling_rate, '32f', out.audio); * fs.writeFileSync('out.wav', wav.toBuffer()); * ``` * * **Example:** Multilingual speech generation with `Xenova/mms-tts-fra`. See [here](https://huggingface.co/models?pipeline_tag=text-to-speech&other=vits&sort=trending) for the full list of available languages (1107). * ```javascript * const synthesizer = await pipeline('text-to-speech', 'Xenova/mms-tts-fra'); * const out = await synthesizer('Bonjour'); * // { * // audio: Float32Array(23808) [-0.00037693005288019776, 0.0003325853613205254, ...], * // sampling_rate: 16000 * // } * ``` */ class TextToAudioPipeline extends (/** @type {new (options: TextToAudioPipelineConstructorArgs) => TextToAudioPipelineType} */ (Pipeline)) { DEFAULT_VOCODER_ID = "Xenova/speecht5_hifigan" /** * Create a new TextToAudioPipeline. * @param {TextToAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); // TODO: Find a better way for `pipeline` to set the default vocoder this.vocoder = options.vocoder ?? null; } /** @type {TextToAudioPipelineCallback} */ async _call(text_inputs, { speaker_embeddings = null, } = {}) { throw new Error('This pipeline is not yet supported in Transformers.js v3.'); // TODO: Remove when implemented // If this.processor is not set, we are using a `AutoModelForTextToWaveform` model if (this.processor) { return this._call_text_to_spectrogram(text_inputs, { speaker_embeddings }); } else { return this._call_text_to_waveform(text_inputs); } } async _call_text_to_waveform(text_inputs) { // Run tokenization const inputs = this.tokenizer(text_inputs, { padding: true, truncation: true, }); // Generate waveform const { waveform } = await this.model(inputs); const sampling_rate = this.model.config.sampling_rate; return { audio: waveform.data, sampling_rate, } } async _call_text_to_spectrogram(text_inputs, { speaker_embeddings }) { // Load vocoder, if not provided if (!this.vocoder) { console.log('No vocoder specified, using default HifiGan vocoder.'); this.vocoder = await _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel.from_pretrained(this.DEFAULT_VOCODER_ID, { dtype: 'fp32' }); } // Load speaker embeddings as Float32Array from path/URL if (typeof speaker_embeddings === 'string' || speaker_embeddings instanceof URL) { // Load from URL with fetch speaker_embeddings = new Float32Array( await (await fetch(speaker_embeddings)).arrayBuffer() ); } if (speaker_embeddings instanceof Float32Array) { speaker_embeddings = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( 'float32', speaker_embeddings, [1, speaker_embeddings.length] ) } else if (!(speaker_embeddings instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor)) { throw new Error("Speaker embeddings must be a `Tensor`, `Float32Array`, `string`, or `URL`.") } // Run tokenization const { input_ids } = this.tokenizer(text_inputs, { padding: true, truncation: true, }); // NOTE: At this point, we are guaranteed that `speaker_embeddings` is a `Tensor` // @ts-ignore const { waveform } = await this.model.generate_speech(input_ids, speaker_embeddings, { vocoder: this.vocoder }); const sampling_rate = this.processor.feature_extractor.config.sampling_rate; return { audio: waveform.data, sampling_rate, } } } /** * @callback ImageToImagePipelineCallback Transform the image(s) passed as inputs. * @param {ImagePipelineInputs} images The images to transform. * @returns {Promise} The transformed image or list of images. * * @typedef {ImagePipelineConstructorArgs & ImageToImagePipelineCallback & Disposable} ImageToImagePipelineType */ /** * Image to Image pipeline using any `AutoModelForImageToImage`. This pipeline generates an image based on a previous image input. * * **Example:** Super-resolution w/ `Xenova/swin2SR-classical-sr-x2-64` * ```javascript * const upscaler = await pipeline('image-to-image', 'Xenova/swin2SR-classical-sr-x2-64'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/butterfly.jpg'; * const output = await upscaler(url); * // RawImage { * // data: Uint8Array(786432) [ 41, 31, 24, 43, ... ], * // width: 512, * // height: 512, * // channels: 3 * // } * ``` */ class ImageToImagePipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageToImagePipelineType} */ (Pipeline)) { /** * Create a new ImageToImagePipeline. * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {ImageToImagePipelineCallback} */ async _call(images) { const preparedImages = await prepareImages(images); const inputs = await this.processor(preparedImages); const outputs = await this.model(inputs); /** @type {RawImage[]} */ const toReturn = []; for (const batch of outputs.reconstruction) { const output = batch.squeeze().clamp_(0, 1).mul_(255).round_().to('uint8'); toReturn.push(_utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.fromTensor(output)); } return toReturn.length > 1 ? toReturn : toReturn[0]; } } /** * @typedef {Object} DepthEstimationPipelineOutput * @property {Tensor} predicted_depth The raw depth map predicted by the model. * @property {RawImage} depth The processed depth map as an image (with the same size as the input image). * * @callback DepthEstimationPipelineCallback Predicts the depth for the image(s) passed as inputs. * @param {ImagePipelineInputs} images The images to compute depth for. * @returns {Promise} An image or a list of images containing result(s). * * @typedef {ImagePipelineConstructorArgs & DepthEstimationPipelineCallback & Disposable} DepthEstimationPipelineType */ /** * Depth estimation pipeline using any `AutoModelForDepthEstimation`. This pipeline predicts the depth of an image. * * **Example:** Depth estimation w/ `Xenova/dpt-hybrid-midas` * ```javascript * const depth_estimator = await pipeline('depth-estimation', 'Xenova/dpt-hybrid-midas'); * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; * const out = await depth_estimator(url); * // { * // predicted_depth: Tensor { * // dims: [ 384, 384 ], * // type: 'float32', * // data: Float32Array(147456) [ 542.859130859375, 545.2833862304688, 546.1649169921875, ... ], * // size: 147456 * // }, * // depth: RawImage { * // data: Uint8Array(307200) [ 86, 86, 86, ... ], * // width: 640, * // height: 480, * // channels: 1 * // } * // } * ``` */ class DepthEstimationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => DepthEstimationPipelineType} */ (Pipeline)) { /** * Create a new DepthEstimationPipeline. * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. */ constructor(options) { super(options); } /** @type {DepthEstimationPipelineCallback} */ async _call(images) { const preparedImages = await prepareImages(images); const inputs = await this.processor(preparedImages); const { predicted_depth } = await this.model(inputs); const toReturn = []; for (let i = 0; i < preparedImages.length; ++i) { const prediction = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.interpolate)(predicted_depth[i], preparedImages[i].size.reverse(), 'bilinear', false); const formatted = prediction.mul_(255 / (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(prediction.data)[0]).to('uint8'); toReturn.push({ predicted_depth: predicted_depth[i], depth: _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.fromTensor(formatted), }); } return toReturn.length > 1 ? toReturn : toReturn[0]; } } const SUPPORTED_TASKS = Object.freeze({ "text-classification": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": TextClassificationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, "default": { // TODO: replace with original // "model": "distilbert-base-uncased-finetuned-sst-2-english", "model": "Xenova/distilbert-base-uncased-finetuned-sst-2-english", }, "type": "text", }, "token-classification": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": TokenClassificationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTokenClassification, "default": { // TODO: replace with original // "model": "Davlan/bert-base-multilingual-cased-ner-hrl", "model": "Xenova/bert-base-multilingual-cased-ner-hrl", }, "type": "text", }, "question-answering": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": QuestionAnsweringPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForQuestionAnswering, "default": { // TODO: replace with original // "model": "distilbert-base-cased-distilled-squad", "model": "Xenova/distilbert-base-cased-distilled-squad", }, "type": "text", }, "fill-mask": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": FillMaskPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForMaskedLM, "default": { // TODO: replace with original // "model": "bert-base-uncased", "model": "Xenova/bert-base-uncased", }, "type": "text", }, "summarization": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": SummarizationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, "default": { // TODO: replace with original // "model": "sshleifer/distilbart-cnn-6-6", "model": "Xenova/distilbart-cnn-6-6", }, "type": "text", }, "translation": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": TranslationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, "default": { // TODO: replace with original // "model": "t5-small", "model": "Xenova/t5-small", }, "type": "text", }, "text2text-generation": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": Text2TextGenerationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, "default": { // TODO: replace with original // "model": "google/flan-t5-small", "model": "Xenova/flan-t5-small", }, "type": "text", }, "text-generation": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": TextGenerationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCausalLM, "default": { // TODO: replace with original // "model": "gpt2", "model": "Xenova/gpt2", }, "type": "text", }, "zero-shot-classification": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": ZeroShotClassificationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, "default": { // TODO: replace with original // "model": "typeform/distilbert-base-uncased-mnli", "model": "Xenova/distilbert-base-uncased-mnli", }, "type": "text", }, "audio-classification": { "pipeline": AudioClassificationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForAudioClassification, "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "superb/wav2vec2-base-superb-ks", "model": "Xenova/wav2vec2-base-superb-ks", }, "type": "audio", }, "zero-shot-audio-classification": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": ZeroShotAudioClassificationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "laion/clap-htsat-fused", "model": "Xenova/clap-htsat-unfused", }, "type": "multimodal", }, "automatic-speech-recognition": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": AutomaticSpeechRecognitionPipeline, "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSpeechSeq2Seq, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCTC], "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "openai/whisper-tiny.en", "model": "Xenova/whisper-tiny.en", }, "type": "multimodal", }, "text-to-audio": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": TextToAudioPipeline, "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToWaveform, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToSpectrogram], "processor": [_processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, /* Some don't use a processor */ null], "default": { // TODO: replace with original // "model": "microsoft/speecht5_tts", "model": "Xenova/speecht5_tts", }, "type": "text", }, "image-to-text": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": ImageToTextPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForVision2Seq, "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "nlpconnect/vit-gpt2-image-captioning", "model": "Xenova/vit-gpt2-image-captioning", }, "type": "multimodal", }, "image-classification": { // no tokenizer "pipeline": ImageClassificationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageClassification, "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "google/vit-base-patch16-224", "model": "Xenova/vit-base-patch16-224", }, "type": "multimodal", }, "image-segmentation": { // no tokenizer "pipeline": ImageSegmentationPipeline, "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSemanticSegmentation], "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "facebook/detr-resnet-50-panoptic", "model": "Xenova/detr-resnet-50-panoptic", }, "type": "multimodal", }, "zero-shot-image-classification": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": ZeroShotImageClassificationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "openai/clip-vit-base-patch32", "model": "Xenova/clip-vit-base-patch32", }, "type": "multimodal", }, "object-detection": { // no tokenizer "pipeline": ObjectDetectionPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForObjectDetection, "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "facebook/detr-resnet-50", "model": "Xenova/detr-resnet-50", }, "type": "multimodal", }, "zero-shot-object-detection": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": ZeroShotObjectDetectionPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForZeroShotObjectDetection, "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "google/owlvit-base-patch32", "model": "Xenova/owlvit-base-patch32", }, "type": "multimodal", }, "document-question-answering": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": DocumentQuestionAnsweringPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDocumentQuestionAnswering, "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "naver-clova-ix/donut-base-finetuned-docvqa", "model": "Xenova/donut-base-finetuned-docvqa", }, "type": "multimodal", }, "image-to-image": { // no tokenizer "pipeline": ImageToImagePipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageToImage, "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "caidas/swin2SR-classical-sr-x2-64", "model": "Xenova/swin2SR-classical-sr-x2-64", }, "type": "image", }, "depth-estimation": { // no tokenizer "pipeline": DepthEstimationPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDepthEstimation, "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "default": { // TODO: replace with original // "model": "Intel/dpt-large", "model": "Xenova/dpt-large", }, "type": "image", }, // This task serves as a useful interface for dealing with sentence-transformers (https://huggingface.co/sentence-transformers). "feature-extraction": { "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, "pipeline": FeatureExtractionPipeline, "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, "default": { // TODO: replace with original // "model": "sentence-transformers/all-MiniLM-L6-v2", "model": "Xenova/all-MiniLM-L6-v2", }, "type": "text", }, "image-feature-extraction": { "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, "pipeline": ImageFeatureExtractionPipeline, "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageFeatureExtraction, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel], "default": { // TODO: replace with original // "model": "google/vit-base-patch16-224", "model": "Xenova/vit-base-patch16-224-in21k", }, "type": "image", }, }) // TODO: Add types for TASK_ALIASES const TASK_ALIASES = Object.freeze({ "sentiment-analysis": "text-classification", "ner": "token-classification", // "vqa": "visual-question-answering", // TODO: Add "asr": "automatic-speech-recognition", "text-to-speech": "text-to-audio", // Add for backwards compatibility "embeddings": "feature-extraction", }); /** * @typedef {keyof typeof SUPPORTED_TASKS} TaskType * @typedef {keyof typeof TASK_ALIASES} AliasType * @typedef {TaskType | AliasType} PipelineType All possible pipeline types. * @typedef {{[K in TaskType]: InstanceType}} SupportedTasks A mapping of pipeline names to their corresponding pipeline classes. * @typedef {{[K in AliasType]: InstanceType}} AliasTasks A mapping from pipeline aliases to their corresponding pipeline classes. * @typedef {SupportedTasks & AliasTasks} AllTasks A mapping from all pipeline names and aliases to their corresponding pipeline classes. */ /** * Utility factory method to build a `Pipeline` object. * * @template {PipelineType} T The type of pipeline to return. * @param {T} task The task defining which pipeline will be returned. Currently accepted tasks are: * - `"audio-classification"`: will return a `AudioClassificationPipeline`. * - `"automatic-speech-recognition"`: will return a `AutomaticSpeechRecognitionPipeline`. * - `"depth-estimation"`: will return a `DepthEstimationPipeline`. * - `"document-question-answering"`: will return a `DocumentQuestionAnsweringPipeline`. * - `"feature-extraction"`: will return a `FeatureExtractionPipeline`. * - `"fill-mask"`: will return a `FillMaskPipeline`. * - `"image-classification"`: will return a `ImageClassificationPipeline`. * - `"image-segmentation"`: will return a `ImageSegmentationPipeline`. * - `"image-to-text"`: will return a `ImageToTextPipeline`. * - `"object-detection"`: will return a `ObjectDetectionPipeline`. * - `"question-answering"`: will return a `QuestionAnsweringPipeline`. * - `"summarization"`: will return a `SummarizationPipeline`. * - `"text2text-generation"`: will return a `Text2TextGenerationPipeline`. * - `"text-classification"` (alias "sentiment-analysis" available): will return a `TextClassificationPipeline`. * - `"text-generation"`: will return a `TextGenerationPipeline`. * - `"token-classification"` (alias "ner" available): will return a `TokenClassificationPipeline`. * - `"translation"`: will return a `TranslationPipeline`. * - `"translation_xx_to_yy"`: will return a `TranslationPipeline`. * - `"zero-shot-classification"`: will return a `ZeroShotClassificationPipeline`. * - `"zero-shot-audio-classification"`: will return a `ZeroShotAudioClassificationPipeline`. * - `"zero-shot-image-classification"`: will return a `ZeroShotImageClassificationPipeline`. * - `"zero-shot-object-detection"`: will return a `ZeroShotObjectDetectionPipeline`. * @param {string} [model=null] The name of the pre-trained model to use. If not specified, the default model for the task will be used. * @param {import('./utils/hub.js').PretrainedModelOptions} [options] Optional parameters for the pipeline. * @returns {Promise} A Pipeline object for the specified task. * @throws {Error} If an unsupported pipeline is requested. */ async function pipeline( task, model = null, { progress_callback = null, config = null, cache_dir = null, local_files_only = false, revision = 'main', device = null, dtype = null, model_file_name = null, session_options = {}, } = {} ) { // Helper method to construct pipeline // Apply aliases // @ts-ignore task = TASK_ALIASES[task] ?? task; // Get pipeline info const pipelineInfo = SUPPORTED_TASKS[task.split('_', 1)[0]]; if (!pipelineInfo) { throw Error(`Unsupported pipeline: ${task}. Must be one of [${Object.keys(SUPPORTED_TASKS)}]`) } // Use model if specified, otherwise, use default if (!model) { model = pipelineInfo.default.model console.log(`No model specified. Using default model: "${model}".`); } const pretrainedOptions = { progress_callback, config, cache_dir, local_files_only, revision, device, dtype, model_file_name, session_options, } const classes = new Map([ ['tokenizer', pipelineInfo.tokenizer], ['model', pipelineInfo.model], ['processor', pipelineInfo.processor], ]); // Load model, tokenizer, and processor (if they exist) const results = await loadItems(classes, model, pretrainedOptions); results.task = task; (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.dispatchCallback)(progress_callback, { 'status': 'ready', 'task': task, 'model': model, }); const pipelineClass = pipelineInfo.pipeline; return new pipelineClass(results); } /** * Helper function to get applicable model, tokenizer, or processor classes for a given model. * @param {Map} mapping The mapping of names to classes, arrays of classes, or null. * @param {string} model The name of the model to load. * @param {import('./utils/hub.js').PretrainedOptions} pretrainedOptions The options to pass to the `from_pretrained` method. * @private */ async function loadItems(mapping, model, pretrainedOptions) { const result = Object.create(null); /**@type {Promise[]} */ const promises = []; for (let [name, cls] of mapping.entries()) { if (!cls) continue; /**@type {Promise} */ let promise; if (Array.isArray(cls)) { promise = new Promise(async (resolve, reject) => { let e; for (let c of cls) { if (c === null) { // If null, we resolve it immediately, meaning the relevant // class was not found, but it is optional. resolve(null); return; } try { resolve(await c.from_pretrained(model, pretrainedOptions)); return; } catch (err) { if (err.message?.includes('Unsupported model type')) { // If the error is due to an unsupported model type, we // save the error and try the next class. e = err; } else { reject(err); return; } } } reject(e); }) } else { promise = cls.from_pretrained(model, pretrainedOptions); } result[name] = promise; promises.push(promise); } // Wait for all promises to resolve (in parallel) await Promise.all(promises); // Then assign to result for (let [name, promise] of Object.entries(result)) { result[name] = await promise; } return result; } /***/ }), /***/ "./src/processors.js": /*!***************************!*\ !*** ./src/processors.js ***! \***************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "ASTFeatureExtractor": () => (/* binding */ ASTFeatureExtractor), /* harmony export */ "AutoProcessor": () => (/* binding */ AutoProcessor), /* harmony export */ "BeitFeatureExtractor": () => (/* binding */ BeitFeatureExtractor), /* harmony export */ "BitImageProcessor": () => (/* binding */ BitImageProcessor), /* harmony export */ "CLIPFeatureExtractor": () => (/* binding */ CLIPFeatureExtractor), /* harmony export */ "CLIPImageProcessor": () => (/* binding */ CLIPImageProcessor), /* harmony export */ "ChineseCLIPFeatureExtractor": () => (/* binding */ ChineseCLIPFeatureExtractor), /* harmony export */ "ClapFeatureExtractor": () => (/* binding */ ClapFeatureExtractor), /* harmony export */ "ConvNextFeatureExtractor": () => (/* binding */ ConvNextFeatureExtractor), /* harmony export */ "ConvNextImageProcessor": () => (/* binding */ ConvNextImageProcessor), /* harmony export */ "DPTFeatureExtractor": () => (/* binding */ DPTFeatureExtractor), /* harmony export */ "DPTImageProcessor": () => (/* binding */ DPTImageProcessor), /* harmony export */ "DeiTFeatureExtractor": () => (/* binding */ DeiTFeatureExtractor), /* harmony export */ "DetrFeatureExtractor": () => (/* binding */ DetrFeatureExtractor), /* harmony export */ "DonutFeatureExtractor": () => (/* binding */ DonutFeatureExtractor), /* harmony export */ "EfficientNetImageProcessor": () => (/* binding */ EfficientNetImageProcessor), /* harmony export */ "FeatureExtractor": () => (/* binding */ FeatureExtractor), /* harmony export */ "GLPNFeatureExtractor": () => (/* binding */ GLPNFeatureExtractor), /* harmony export */ "ImageFeatureExtractor": () => (/* binding */ ImageFeatureExtractor), /* harmony export */ "MobileViTFeatureExtractor": () => (/* binding */ MobileViTFeatureExtractor), /* harmony export */ "NougatImageProcessor": () => (/* binding */ NougatImageProcessor), /* harmony export */ "OwlViTFeatureExtractor": () => (/* binding */ OwlViTFeatureExtractor), /* harmony export */ "OwlViTProcessor": () => (/* binding */ OwlViTProcessor), /* harmony export */ "Owlv2ImageProcessor": () => (/* binding */ Owlv2ImageProcessor), /* harmony export */ "Processor": () => (/* binding */ Processor), /* harmony export */ "SamImageProcessor": () => (/* binding */ SamImageProcessor), /* harmony export */ "SamProcessor": () => (/* binding */ SamProcessor), /* harmony export */ "SeamlessM4TFeatureExtractor": () => (/* binding */ SeamlessM4TFeatureExtractor), /* harmony export */ "SegformerFeatureExtractor": () => (/* binding */ SegformerFeatureExtractor), /* harmony export */ "SiglipImageProcessor": () => (/* binding */ SiglipImageProcessor), /* harmony export */ "SpeechT5FeatureExtractor": () => (/* binding */ SpeechT5FeatureExtractor), /* harmony export */ "SpeechT5Processor": () => (/* binding */ SpeechT5Processor), /* harmony export */ "Swin2SRImageProcessor": () => (/* binding */ Swin2SRImageProcessor), /* harmony export */ "ViTFeatureExtractor": () => (/* binding */ ViTFeatureExtractor), /* harmony export */ "ViTImageProcessor": () => (/* binding */ ViTImageProcessor), /* harmony export */ "VitMatteImageProcessor": () => (/* binding */ VitMatteImageProcessor), /* harmony export */ "Wav2Vec2FeatureExtractor": () => (/* binding */ Wav2Vec2FeatureExtractor), /* harmony export */ "Wav2Vec2ProcessorWithLM": () => (/* binding */ Wav2Vec2ProcessorWithLM), /* harmony export */ "WhisperFeatureExtractor": () => (/* binding */ WhisperFeatureExtractor), /* harmony export */ "WhisperProcessor": () => (/* binding */ WhisperProcessor), /* harmony export */ "YolosFeatureExtractor": () => (/* binding */ YolosFeatureExtractor) /* harmony export */ }); /* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); /* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); /* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); /* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); /* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); /* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); /* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); /** * @file Processors are used to prepare non-textual inputs (e.g., image or audio) for a model. * * **Example:** Using a `WhisperProcessor` to prepare an audio input for a model. * ```javascript * import { AutoProcessor, read_audio } from '@xenova/transformers'; * * let processor = await AutoProcessor.from_pretrained('openai/whisper-tiny.en'); * let audio = await read_audio('https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac', 16000); * let { input_features } = await processor(audio); * // Tensor { * // data: Float32Array(240000) [0.4752984642982483, 0.5597258806228638, 0.56434166431427, ...], * // dims: [1, 80, 3000], * // type: 'float32', * // size: 240000, * // } * ``` * * @module processors */ // Helper functions /** * Converts bounding boxes from center format to corners format. * * @param {number[]} arr The coordinate for the center of the box and its width, height dimensions (center_x, center_y, width, height) * @returns {number[]} The coodinates for the top-left and bottom-right corners of the box (top_left_x, top_left_y, bottom_right_x, bottom_right_y) */ function center_to_corners_format([centerX, centerY, width, height]) { return [ centerX - width / 2, centerY - height / 2, centerX + width / 2, centerY + height / 2 ]; } /** * Post-processes the outputs of the model (for object detection). * @param {Object} outputs The outputs of the model that must be post-processed * @param {Tensor} outputs.logits The logits * @param {Tensor} outputs.pred_boxes The predicted boxes. * @param {number} [threshold=0.5] The threshold to use for the scores. * @param {number[][]} [target_sizes=null] The sizes of the original images. * @param {boolean} [is_zero_shot=false] Whether zero-shot object detection was performed. * @return {Object[]} An array of objects containing the post-processed outputs. * @private */ function post_process_object_detection(outputs, threshold = 0.5, target_sizes = null, is_zero_shot = false) { const out_logits = outputs.logits; const out_bbox = outputs.pred_boxes; const [batch_size, num_boxes, num_classes] = out_logits.dims; if (target_sizes !== null && target_sizes.length !== batch_size) { throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") } let toReturn = []; for (let i = 0; i < batch_size; ++i) { let target_size = target_sizes !== null ? target_sizes[i] : null; let info = { boxes: [], classes: [], scores: [] } let logits = out_logits[i]; let bbox = out_bbox[i]; for (let j = 0; j < num_boxes; ++j) { let logit = logits[j]; let indices = []; let probs; if (is_zero_shot) { // Get indices of classes with high enough probability probs = logit.sigmoid().data; for (let k = 0; k < probs.length; ++k) { if (probs[k] > threshold) { indices.push(k); } } } else { // Get most probable class let maxIndex = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(logit.data)[1]; if (maxIndex === num_classes - 1) { // This is the background class, skip it continue; } // Compute softmax over classes probs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(logit.data); if (probs[maxIndex] < threshold) { continue; } indices.push(maxIndex); } for (const index of indices) { // Some class has a high enough probability /** @type {number[]} */ let box = bbox[j].data; // convert to [x0, y0, x1, y1] format box = center_to_corners_format(box) if (target_size !== null) { box = box.map((x, i) => x * target_size[(i + 1) % 2]) } info.boxes.push(box); info.classes.push(index); info.scores.push(probs[index]); } } toReturn.push(info); } return toReturn; } /** * Named tuple to indicate the order we are using is (height x width), even though * the Graphics’ industry standard is (width x height). * @typedef {[height: number, width: number]} HeightWidth */ /** * Helper function to validate audio inputs. * @param {any} audio The audio data. * @param {string} feature_extractor The name of the feature extractor. * @private */ function validate_audio_inputs(audio, feature_extractor) { if (!(audio instanceof Float32Array || audio instanceof Float64Array)) { throw new Error( `${feature_extractor} expects input to be a Float32Array or a Float64Array, but got ${audio?.constructor?.name ?? typeof audio} instead. ` + `If using the feature extractor directly, remember to use \`read_audio(url, sampling_rate)\` to obtain the raw audio data of the file/url.` ) } } /** * Helper function to constrain a value to be a multiple of a number. * @param {number} val The value to constrain. * @param {number} multiple The number to constrain to. * @param {number} [minVal=0] The minimum value to constrain to. * @param {number} [maxVal=null] The maximum value to constrain to. * @returns {number} The constrained value. * @private */ function constraint_to_multiple_of(val, multiple, minVal = 0, maxVal = null) { const a = val / multiple; let x = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.bankers_round)(a) * multiple; if (maxVal !== null && x > maxVal) { x = Math.floor(a) * multiple; } if (x < minVal) { x = Math.ceil(a) * multiple; } return x; } /** * Rounds the height and width down to the closest multiple of size_divisibility * @param {[number, number]} size The size of the image * @param {number} divisor The divisor to use. * @returns {[number, number]} The rounded size. */ function enforce_size_divisibility([width, height], divisor) { return [ Math.max(Math.floor(width / divisor), 1) * divisor, Math.max(Math.floor(height / divisor), 1) * divisor ]; } /** * Base class for feature extractors. * * @extends Callable */ class FeatureExtractor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * Constructs a new FeatureExtractor instance. * * @param {Object} config The configuration for the feature extractor. */ constructor(config) { super(); this.config = config } } /** * @typedef {object} ImageFeatureExtractorResult * @property {Tensor} pixel_values The pixel values of the batched preprocessed images. * @property {HeightWidth[]} original_sizes Array of two-dimensional tuples like [[480, 640]]. * @property {HeightWidth[]} reshaped_input_sizes Array of two-dimensional tuples like [[1000, 1330]]. */ /** * Feature extractor for image models. * * @extends FeatureExtractor */ class ImageFeatureExtractor extends FeatureExtractor { /** * Constructs a new ImageFeatureExtractor instance. * * @param {Object} config The configuration for the feature extractor. * @param {number[]} config.image_mean The mean values for image normalization. * @param {number[]} config.image_std The standard deviation values for image normalization. * @param {boolean} config.do_rescale Whether to rescale the image pixel values to the [0,1] range. * @param {number} config.rescale_factor The factor to use for rescaling the image pixel values. * @param {boolean} config.do_normalize Whether to normalize the image pixel values. * @param {boolean} config.do_resize Whether to resize the image. * @param {number} config.resample What method to use for resampling. * @param {number|Object} config.size The size to resize the image to. */ constructor(config) { super(config); this.image_mean = this.config.image_mean ?? this.config.mean; this.image_std = this.config.image_std ?? this.config.std; this.resample = this.config.resample ?? 2; // 2 => bilinear this.do_rescale = this.config.do_rescale ?? true; this.rescale_factor = this.config.rescale_factor ?? (1 / 255); this.do_normalize = this.config.do_normalize; this.do_resize = this.config.do_resize; this.do_thumbnail = this.config.do_thumbnail; this.size = this.config.size; this.size_divisibility = this.config.size_divisibility ?? this.config.size_divisor; this.do_center_crop = this.config.do_center_crop; this.crop_size = this.config.crop_size; this.do_convert_rgb = this.config.do_convert_rgb ?? true; this.do_crop_margin = this.config.do_crop_margin; this.pad_size = this.config.pad_size; this.do_pad = this.config.do_pad; if (this.do_pad && !this.pad_size && this.size && this.size.width !== undefined && this.size.height !== undefined) { // Should pad, but no pad size specified // We infer the pad size from the resize size this.pad_size = this.size } } /** * Resize the image to make a thumbnail. The image is resized so that no dimension is larger than any * corresponding dimension of the specified size. * @param {RawImage} image The image to be resized. * @param {{height:number, width:number}} size The size `{"height": h, "width": w}` to resize the image to. * @param {string | 0 | 1 | 2 | 3 | 4 | 5} [resample=2] The resampling filter to use. * @returns {Promise} The resized image. */ async thumbnail(image, size, resample = 2) { const input_height = image.height; const input_width = image.width; const output_height = size.height; const output_width = size.width; // We always resize to the smallest of either the input or output size. let height = Math.min(input_height, output_height) let width = Math.min(input_width, output_width) if (height === input_height && width === input_width) { return image; } if (input_height > input_width) { width = Math.floor(input_width * height / input_height); } else if (input_width > input_height) { height = Math.floor(input_height * width / input_width); } return await image.resize(width, height, { resample }); } /** * Crops the margin of the image. Gray pixels are considered margin (i.e., pixels with a value below the threshold). * @param {RawImage} image The image to be cropped. * @param {number} gray_threshold Value below which pixels are considered to be gray. * @returns {Promise} The cropped image. */ async crop_margin(image, gray_threshold = 200) { const gray_image = image.clone().grayscale(); const minValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(gray_image.data)[0]; const maxValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(gray_image.data)[0]; const diff = maxValue - minValue; if (diff === 0) { return image; } const threshold = gray_threshold / 255; let x_min = gray_image.width, y_min = gray_image.height, x_max = 0, y_max = 0; const gray_image_data = gray_image.data; for (let j = 0; j < gray_image.height; ++j) { const row = j * gray_image.width; for (let i = 0; i < gray_image.width; ++i) { if ((gray_image_data[row + i] - minValue) / diff < threshold) { // We have a non-zero pixel, so we update the min/max values accordingly x_min = Math.min(x_min, i); y_min = Math.min(y_min, j); x_max = Math.max(x_max, i); y_max = Math.max(y_max, j); } } } image = await image.crop([x_min, y_min, x_max, y_max]); return image; } /** * Pad the image by a certain amount. * @param {Float32Array} pixelData The pixel data to pad. * @param {number[]} imgDims The dimensions of the image (height, width, channels). * @param {{width:number; height:number}|number} padSize The dimensions of the padded image. * @param {Object} options The options for padding. * @param {'constant'|'symmetric'} [options.mode='constant'] The type of padding to add. * @param {boolean} [options.center=false] Whether to center the image. * @param {number} [options.constant_values=0] The constant value to use for padding. * @returns {[Float32Array, number[]]} The padded pixel data and image dimensions. */ pad_image(pixelData, imgDims, padSize, { mode = 'constant', center = false, constant_values = 0, } = {}) { const [imageHeight, imageWidth, imageChannels] = imgDims; let paddedImageWidth, paddedImageHeight; if (typeof padSize === 'number') { paddedImageWidth = padSize; paddedImageHeight = padSize; } else { paddedImageWidth = padSize.width; paddedImageHeight = padSize.height; } // Only add padding if there is a difference in size if (paddedImageWidth !== imageWidth || paddedImageHeight !== imageHeight) { const paddedPixelData = new Float32Array(paddedImageWidth * paddedImageHeight * imageChannels); if (Array.isArray(constant_values)) { // Fill with constant values, cycling through the array for (let i = 0; i < paddedPixelData.length; ++i) { paddedPixelData[i] = constant_values[i % imageChannels]; } } else if (constant_values !== 0) { paddedPixelData.fill(constant_values); } const [left, top] = center ? [Math.floor((paddedImageWidth - imageWidth) / 2), Math.floor((paddedImageHeight - imageHeight) / 2)] : [0, 0]; // Copy the original image into the padded image for (let i = 0; i < imageHeight; ++i) { const a = (i + top) * paddedImageWidth; const b = i * imageWidth; for (let j = 0; j < imageWidth; ++j) { const c = (a + j + left) * imageChannels; const d = (b + j) * imageChannels; for (let k = 0; k < imageChannels; ++k) { paddedPixelData[c + k] = pixelData[d + k]; } } } if (mode === 'symmetric') { if (center) { throw new Error('`center` padding is not supported when `mode` is set to `symmetric`.'); // TODO: Implement this } const h1 = imageHeight - 1; const w1 = imageWidth - 1; for (let i = 0; i < paddedImageHeight; ++i) { const a = i * paddedImageWidth; const b = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateReflectOffset)(i, h1) * imageWidth; for (let j = 0; j < paddedImageWidth; ++j) { if (i < imageHeight && j < imageWidth) continue; // Do not overwrite original image const c = (a + j) * imageChannels; const d = (b + (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateReflectOffset)(j, w1)) * imageChannels; // Copy channel-wise for (let k = 0; k < imageChannels; ++k) { paddedPixelData[c + k] = pixelData[d + k]; } } } } // Update pixel data and image dimensions pixelData = paddedPixelData; imgDims = [paddedImageHeight, paddedImageWidth, imageChannels] } return [pixelData, imgDims]; } /** * Rescale the image' pixel values by `this.rescale_factor`. * @param {Float32Array} pixelData The pixel data to rescale. * @returns {void} */ rescale(pixelData) { for (let i = 0; i < pixelData.length; ++i) { pixelData[i] = this.rescale_factor * pixelData[i]; } } /** * Find the target (width, height) dimension of the output image after * resizing given the input image and the desired size. * @param {RawImage} image The image to resize. * @param {any} size The size to use for resizing the image. * @returns {[number, number]} The target (width, height) dimension of the output image after resizing. */ get_resize_output_image_size(image, size) { // `size` comes in many forms, so we need to handle them all here: // 1. `size` is an integer, in which case we resize the image to be a square const [srcWidth, srcHeight] = image.size; let shortest_edge; let longest_edge; if (this.do_thumbnail) { // NOTE: custom logic for `Donut` models const { height, width } = size; shortest_edge = Math.min(height, width) } // Support both formats for backwards compatibility else if (Number.isInteger(size)) { shortest_edge = size; longest_edge = this.config.max_size ?? shortest_edge; } else if (size !== undefined) { // Extract known properties from `size` shortest_edge = size.shortest_edge; longest_edge = size.longest_edge; } // If `longest_edge` and `shortest_edge` are set, maintain aspect ratio and resize to `shortest_edge` // while keeping the largest dimension <= `longest_edge` if (shortest_edge !== undefined || longest_edge !== undefined) { // http://opensourcehacker.com/2011/12/01/calculate-aspect-ratio-conserving-resize-for-images-in-javascript/ // Try resize so that shortest edge is `shortest_edge` (target) const shortResizeFactor = shortest_edge === undefined ? 1 // If `shortest_edge` is not set, don't upscale : Math.max(shortest_edge / srcWidth, shortest_edge / srcHeight); const newWidth = srcWidth * shortResizeFactor; const newHeight = srcHeight * shortResizeFactor; // The new width and height might be greater than `longest_edge`, so // we downscale again to ensure the largest dimension is `longest_edge` const longResizeFactor = longest_edge === undefined ? 1 // If `longest_edge` is not set, don't downscale : Math.min(longest_edge / newWidth, longest_edge / newHeight); // To avoid certain floating point precision issues, we round to 2 decimal places let finalWidth = Math.floor(Number((newWidth * longResizeFactor).toFixed(2))); let finalHeight = Math.floor(Number((newHeight * longResizeFactor).toFixed(2))); if (this.size_divisibility !== undefined) { [finalWidth, finalHeight] = enforce_size_divisibility([finalWidth, finalHeight], this.size_divisibility) } return [finalWidth, finalHeight]; } else if (size !== undefined && size.width !== undefined && size.height !== undefined) { // If `width` and `height` are set, resize to those dimensions let newWidth = size.width; let newHeight = size.height; // Custom for DPT models if (this.config.keep_aspect_ratio && this.config.ensure_multiple_of) { // determine new height and width let scale_height = newHeight / srcHeight; let scale_width = newWidth / srcWidth; // scale as little as possible if (Math.abs(1 - scale_width) < Math.abs(1 - scale_height)) { // fit width scale_height = scale_width; } else { // fit height scale_width = scale_height; } newHeight = constraint_to_multiple_of(scale_height * srcHeight, this.config.ensure_multiple_of); newWidth = constraint_to_multiple_of(scale_width * srcWidth, this.config.ensure_multiple_of); } return [newWidth, newHeight]; } else if (this.size_divisibility !== undefined) { return enforce_size_divisibility([srcWidth, srcHeight], this.size_divisibility); } else { throw new Error(`Could not resize image due to unsupported \`this.size\` option in config: ${JSON.stringify(size)}`); } } /** * Resizes the image. * @param {RawImage} image The image to resize. * @returns {Promise} The resized image. */ async resize(image) { const [newWidth, newHeight] = this.get_resize_output_image_size(image, this.size); return await image.resize(newWidth, newHeight, { resample: this.resample, }); } /** * @typedef {object} PreprocessedImage * @property {HeightWidth} original_size The original size of the image. * @property {HeightWidth} reshaped_input_size The reshaped input size of the image. * @property {Tensor} pixel_values The pixel values of the preprocessed image. */ /** * Preprocesses the given image. * * @param {RawImage} image The image to preprocess. * @param {Object} overrides The overrides for the preprocessing options. * @returns {Promise} The preprocessed image. */ async preprocess(image, { do_normalize = null, do_pad = null, do_convert_rgb = null, do_convert_grayscale = null, } = {}) { if (this.do_crop_margin) { // NOTE: Specific to nougat processors. This is done before resizing, // and can be interpreted as a pre-preprocessing step. image = await this.crop_margin(image); } const [srcWidth, srcHeight] = image.size; // original image size // Convert image to RGB if specified in config. if (do_convert_rgb ?? this.do_convert_rgb) { image = image.rgb(); } else if (do_convert_grayscale) { image = image.grayscale(); } // TODO: // For efficiency reasons, it might be best to merge the resize and center crop operations into one. // Resize all images if (this.do_resize) { image = await this.resize(image); } // Resize the image using thumbnail method. if (this.do_thumbnail) { image = await this.thumbnail(image, this.size, this.resample); } if (this.do_center_crop) { let crop_width; let crop_height; if (Number.isInteger(this.crop_size)) { crop_width = this.crop_size; crop_height = this.crop_size; } else { crop_width = this.crop_size.width; crop_height = this.crop_size.height; } image = await image.center_crop(crop_width, crop_height); } /** @type {HeightWidth} */ const reshaped_input_size = [image.height, image.width]; // NOTE: All pixel-level manipulation (i.e., modifying `pixelData`) // occurs with data in the hwc format (height, width, channels), // to emulate the behavior of the original Python code (w/ numpy). let pixelData = Float32Array.from(image.data); let imgDims = [image.height, image.width, image.channels]; if (this.do_rescale) { this.rescale(pixelData); } if (do_normalize ?? this.do_normalize) { let image_mean = this.image_mean; if (!Array.isArray(this.image_mean)) { image_mean = new Array(image.channels).fill(image_mean); } let image_std = this.image_std; if (!Array.isArray(this.image_std)) { image_std = new Array(image.channels).fill(image_mean); } if (image_mean.length !== image.channels || image_std.length !== image.channels) { throw new Error(`When set to arrays, the length of \`image_mean\` (${image_mean.length}) and \`image_std\` (${image_std.length}) must match the number of channels in the image (${image.channels}).`); } for (let i = 0; i < pixelData.length; i += image.channels) { for (let j = 0; j < image.channels; ++j) { pixelData[i + j] = (pixelData[i + j] - image_mean[j]) / image_std[j]; } } } // do padding after rescaling/normalizing if (do_pad ?? this.do_pad) { if (this.pad_size) { const padded = this.pad_image(pixelData, [image.height, image.width, image.channels], this.pad_size); [pixelData, imgDims] = padded; // Update pixel data and image dimensions } else if (this.size_divisibility) { const [paddedWidth, paddedHeight] = enforce_size_divisibility([imgDims[1], imgDims[0]], this.size_divisibility); [pixelData, imgDims] = this.pad_image(pixelData, imgDims, { width: paddedWidth, height: paddedHeight }); } } const pixel_values = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', pixelData, imgDims) .permute(2, 0, 1); // convert to channel dimension format (hwc -> chw) return { original_size: [srcHeight, srcWidth], reshaped_input_size: reshaped_input_size, pixel_values, } } /** * Calls the feature extraction process on an array of images, * preprocesses each image, and concatenates the resulting * features into a single Tensor. * @param {RawImage[]} images The image(s) to extract features from. * @param {...any} args Additional arguments. * @returns {Promise} An object containing the concatenated pixel values (and other metadata) of the preprocessed images. */ async _call(images, ...args) { if (!Array.isArray(images)) { images = [images]; } /** @type {PreprocessedImage[]} */ const imageData = await Promise.all(images.map(x => this.preprocess(x))); // Stack pixel values const pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.stack)(imageData.map(x => x.pixel_values), 0); return { pixel_values, // Original sizes of images original_sizes: imageData.map(x => x.original_size), // Reshaped sizes of images, before padding or cropping reshaped_input_sizes: imageData.map(x => x.reshaped_input_size), } } } class SegformerFeatureExtractor extends ImageFeatureExtractor { /** * Converts the output of `SegformerForSemanticSegmentation` into semantic segmentation maps. * @param {*} outputs Raw outputs of the model. * @param {number[][]} [target_sizes=null] List of tuples corresponding to the requested final size * (height, width) of each prediction. If unset, predictions will not be resized. * @returns {{segmentation: Tensor; labels: number[]}[]} The semantic segmentation maps. */ post_process_semantic_segmentation(outputs, target_sizes = null) { const logits = outputs.logits; const batch_size = logits.dims[0]; if (target_sizes !== null && target_sizes.length !== batch_size) { throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") } const toReturn = []; for (let i = 0; i < batch_size; ++i) { const target_size = target_sizes !== null ? target_sizes[i] : null; let data = logits[i]; // 1. If target_size is not null, we need to resize the masks to the target size if (target_size !== null) { // resize the masks to the target size data = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate)(data, target_size, 'bilinear', false); } const [height, width] = target_size ?? data.dims.slice(-2); const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( 'int32', new Int32Array(height * width), [height, width] ); // Buffer to store current largest value const buffer = data[0].data; const segmentation_data = segmentation.data; for (let j = 1; j < data.dims[0]; ++j) { const row = data[j].data; for (let k = 0; k < row.length; ++k) { if (row[k] > buffer[k]) { buffer[k] = row[k]; segmentation_data[k] = j; } } } // Store which objects have labels // This is much more efficient that creating a set of the final values const hasLabel = new Array(data.dims[0]); const out = segmentation.data; for (let j = 0; j < out.length; ++j) { const index = out[j]; hasLabel[index] = index; } /** @type {number[]} The unique list of labels that were detected */ const labels = hasLabel.filter(x => x !== undefined); toReturn.push({ segmentation, labels }); } return toReturn; } } class DPTFeatureExtractor extends ImageFeatureExtractor { } class DPTImageProcessor extends DPTFeatureExtractor { } // NOTE: extends DPTFeatureExtractor class BitImageProcessor extends ImageFeatureExtractor { } class GLPNFeatureExtractor extends ImageFeatureExtractor { } class CLIPFeatureExtractor extends ImageFeatureExtractor { } class CLIPImageProcessor extends CLIPFeatureExtractor { } // NOTE: extends CLIPFeatureExtractor class ChineseCLIPFeatureExtractor extends ImageFeatureExtractor { } class SiglipImageProcessor extends ImageFeatureExtractor { } class ConvNextFeatureExtractor extends ImageFeatureExtractor { constructor(config) { super(config); /** * Percentage of the image to crop. Only has an effect if this.size < 384. */ this.crop_pct = this.config.crop_pct ?? (224 / 256); } async resize(image) { const shortest_edge = this.size?.shortest_edge; if (shortest_edge === undefined) { throw new Error(`Size dictionary must contain 'shortest_edge' key.`); } if (shortest_edge < 384) { // maintain same ratio, resizing shortest edge to shortest_edge/crop_pct const resize_shortest_edge = Math.floor(shortest_edge / this.crop_pct); const [newWidth, newHeight] = this.get_resize_output_image_size(image, { shortest_edge: resize_shortest_edge, }); image = await image.resize(newWidth, newHeight, { resample: this.resample, }); // then crop to (shortest_edge, shortest_edge) image = await image.center_crop(shortest_edge, shortest_edge); } else { // warping (no cropping) when evaluated at 384 or larger image = await image.resize(shortest_edge, shortest_edge, { resample: this.resample, }); } return image; } } class ConvNextImageProcessor extends ConvNextFeatureExtractor { } // NOTE extends ConvNextFeatureExtractor class ViTFeatureExtractor extends ImageFeatureExtractor { } class ViTImageProcessor extends ImageFeatureExtractor { } class EfficientNetImageProcessor extends ImageFeatureExtractor { constructor(config) { super(config); this.include_top = this.config.include_top ?? true; if (this.include_top) { this.image_std = this.image_std.map(x => x * x); } } } class MobileViTFeatureExtractor extends ImageFeatureExtractor { } class OwlViTFeatureExtractor extends ImageFeatureExtractor { /** @type {post_process_object_detection} */ post_process_object_detection(...args) { return post_process_object_detection(...args); } } class Owlv2ImageProcessor extends OwlViTFeatureExtractor { } // NOTE extends OwlViTFeatureExtractor class DeiTFeatureExtractor extends ImageFeatureExtractor { } class BeitFeatureExtractor extends ImageFeatureExtractor { } class DonutFeatureExtractor extends ImageFeatureExtractor { pad_image(pixelData, imgDims, padSize, options = {}) { const [imageHeight, imageWidth, imageChannels] = imgDims; let image_mean = this.image_mean; if (!Array.isArray(this.image_mean)) { image_mean = new Array(imageChannels).fill(image_mean); } let image_std = this.image_std; if (!Array.isArray(image_std)) { image_std = new Array(imageChannels).fill(image_mean); } const constant_values = image_mean.map((x, i) => - x / image_std[i]); return super.pad_image(pixelData, imgDims, padSize, { center: true, // Since normalization is done after padding, we need to use certain constant values to ensure the same behaviour is observed. // For more information, see https://github.com/huggingface/transformers/blob/main/src/transformers/models/donut/image_processing_donut.py#L433-L451 constant_values: constant_values, ...options, }); } } class NougatImageProcessor extends DonutFeatureExtractor { } // NOTE extends DonutFeatureExtractor /** * @typedef {object} DetrFeatureExtractorResultProps * @property {Tensor} pixel_mask * @typedef {ImageFeatureExtractorResult & DetrFeatureExtractorResultProps} DetrFeatureExtractorResult */ /** * Detr Feature Extractor. * * @extends ImageFeatureExtractor */ class DetrFeatureExtractor extends ImageFeatureExtractor { /** * Calls the feature extraction process on an array of images, preprocesses * each image, and concatenates the resulting features into a single Tensor. * @param {RawImage[]} images The image(s) to extract features from. * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. */ async _call(images) { const result = await super._call(images); // TODO support differently-sized images, for now assume all images are the same size. // TODO support different mask sizes (not just 64x64) // Currently, just fill pixel mask with 1s const maskSize = [result.pixel_values.dims[0], 64, 64]; const pixel_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( 'int64', new BigInt64Array(maskSize.reduce((a, b) => a * b)).fill(1n), maskSize ); return { ...result, pixel_mask }; } /** * Post-processes the outputs of the model (for object detection). * @param {Object} outputs The outputs of the model that must be post-processed * @param {Tensor} outputs.logits The logits * @param {Tensor} outputs.pred_boxes The predicted boxes. * @return {Object[]} An array of objects containing the post-processed outputs. */ /** @type {post_process_object_detection} */ post_process_object_detection(...args) { return post_process_object_detection(...args); } /** * Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and `labels`. * @param {Tensor} class_logits The class logits. * @param {Tensor} mask_logits The mask logits. * @param {number} object_mask_threshold A number between 0 and 1 used to binarize the masks. * @param {number} num_labels The number of labels. * @returns {[Tensor[], number[], number[]]} The binarized masks, the scores, and the labels. */ remove_low_and_no_objects(class_logits, mask_logits, object_mask_threshold, num_labels) { let mask_probs_item = []; let pred_scores_item = []; let pred_labels_item = []; for (let j = 0; j < class_logits.dims[0]; ++j) { let cls = class_logits[j]; let mask = mask_logits[j]; let pred_label = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(cls.data)[1]; if (pred_label === num_labels) { // Is the background, so we ignore it continue; } let scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(cls.data); let pred_score = scores[pred_label]; if (pred_score > object_mask_threshold) { mask_probs_item.push(mask); pred_scores_item.push(pred_score); pred_labels_item.push(pred_label); } } return [mask_probs_item, pred_scores_item, pred_labels_item]; } /** * Checks whether the segment is valid or not. * @param {Int32Array} mask_labels Labels for each pixel in the mask. * @param {Tensor[]} mask_probs Probabilities for each pixel in the masks. * @param {number} k The class id of the segment. * @param {number} mask_threshold The mask threshold. * @param {number} overlap_mask_area_threshold The overlap mask area threshold. * @returns {[boolean, number[]]} Whether the segment is valid or not, and the indices of the valid labels. */ check_segment_validity( mask_labels, mask_probs, k, mask_threshold = 0.5, overlap_mask_area_threshold = 0.8 ) { // mask_k is a 1D array of indices, indicating where the mask is equal to k let mask_k = []; let mask_k_area = 0; let original_area = 0; const mask_probs_k_data = mask_probs[k].data; // Compute the area of all the stuff in query k for (let i = 0; i < mask_labels.length; ++i) { if (mask_labels[i] === k) { mask_k.push(i); ++mask_k_area; } if (mask_probs_k_data[i] >= mask_threshold) { ++original_area; } } let mask_exists = mask_k_area > 0 && original_area > 0; // Eliminate disconnected tiny segments if (mask_exists) { // Perform additional check let area_ratio = mask_k_area / original_area; mask_exists = area_ratio > overlap_mask_area_threshold; } return [mask_exists, mask_k] } /** * Computes the segments. * @param {Tensor[]} mask_probs The mask probabilities. * @param {number[]} pred_scores The predicted scores. * @param {number[]} pred_labels The predicted labels. * @param {number} mask_threshold The mask threshold. * @param {number} overlap_mask_area_threshold The overlap mask area threshold. * @param {Set} label_ids_to_fuse The label ids to fuse. * @param {number[]} target_size The target size of the image. * @returns {[Tensor, Array<{id: number, label_id: number, score: number}>]} The computed segments. */ compute_segments( mask_probs, pred_scores, pred_labels, mask_threshold, overlap_mask_area_threshold, label_ids_to_fuse = null, target_size = null, ) { let [height, width] = target_size ?? mask_probs[0].dims; let segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( 'int32', new Int32Array(height * width), [height, width] ); let segments = []; // 1. If target_size is not null, we need to resize the masks to the target size if (target_size !== null) { // resize the masks to the target size for (let i = 0; i < mask_probs.length; ++i) { mask_probs[i] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate)(mask_probs[i], target_size, 'bilinear', false); } } // 2. Weigh each mask by its prediction score // NOTE: `mask_probs` is updated in-place // // Temporary storage for the best label/scores for each pixel ([height, width]): let mask_labels = new Int32Array(mask_probs[0].data.length); let bestScores = new Float32Array(mask_probs[0].data.length); for (let i = 0; i < mask_probs.length; ++i) { let score = pred_scores[i]; const mask_probs_i_data = mask_probs[i].data; for (let j = 0; j < mask_probs_i_data.length; ++j) { mask_probs_i_data[j] *= score if (mask_probs_i_data[j] > bestScores[j]) { mask_labels[j] = i; bestScores[j] = mask_probs_i_data[j]; } } } let current_segment_id = 0; // let stuff_memory_list = {} const segmentation_data = segmentation.data; for (let k = 0; k < pred_labels.length; ++k) { let pred_class = pred_labels[k]; // TODO add `should_fuse` // let should_fuse = pred_class in label_ids_to_fuse // Check if mask exists and large enough to be a segment let [mask_exists, mask_k] = this.check_segment_validity( mask_labels, mask_probs, k, mask_threshold, overlap_mask_area_threshold ) if (!mask_exists) { // Nothing to see here continue; } // TODO // if (pred_class in stuff_memory_list) { // current_segment_id = stuff_memory_list[pred_class] // } else { // current_segment_id += 1; // } ++current_segment_id; // Add current object segment to final segmentation map for (let index of mask_k) { segmentation_data[index] = current_segment_id; } segments.push({ id: current_segment_id, label_id: pred_class, // was_fused: should_fuse, TODO score: pred_scores[k], }) // TODO // if(should_fuse){ // stuff_memory_list[pred_class] = current_segment_id // } } return [segmentation, segments]; } /** * Post-process the model output to generate the final panoptic segmentation. * @param {*} outputs The model output to post process * @param {number} [threshold=0.5] The probability score threshold to keep predicted instance masks. * @param {number} [mask_threshold=0.5] Threshold to use when turning the predicted masks into binary values. * @param {number} [overlap_mask_area_threshold=0.8] The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. * @param {Set} [label_ids_to_fuse=null] The labels in this state will have all their instances be fused together. * @param {number[][]} [target_sizes=null] The target sizes to resize the masks to. * @returns {Array<{ segmentation: Tensor, segments_info: Array<{id: number, label_id: number, score: number}>}>} */ post_process_panoptic_segmentation( outputs, threshold = 0.5, mask_threshold = 0.5, overlap_mask_area_threshold = 0.8, label_ids_to_fuse = null, target_sizes = null, ) { if (label_ids_to_fuse === null) { console.warn("`label_ids_to_fuse` unset. No instance will be fused.") label_ids_to_fuse = new Set(); } const class_queries_logits = outputs.logits; // [batch_size, num_queries, num_classes+1] const masks_queries_logits = outputs.pred_masks; // [batch_size, num_queries, height, width] const mask_probs = masks_queries_logits.sigmoid() // [batch_size, num_queries, height, width] let [batch_size, num_queries, num_labels] = class_queries_logits.dims; num_labels -= 1; // Remove last class (background) if (target_sizes !== null && target_sizes.length !== batch_size) { throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") } let toReturn = []; for (let i = 0; i < batch_size; ++i) { let target_size = target_sizes !== null ? target_sizes[i] : null; let class_logits = class_queries_logits[i]; let mask_logits = mask_probs[i]; let [mask_probs_item, pred_scores_item, pred_labels_item] = this.remove_low_and_no_objects(class_logits, mask_logits, threshold, num_labels); if (pred_labels_item.length === 0) { // No mask found let [height, width] = target_size ?? mask_logits.dims.slice(-2); let segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( 'int32', new Int32Array(height * width).fill(-1), [height, width] ) toReturn.push({ segmentation: segmentation, segments_info: [] }); continue; } // Get segmentation map and segment information of batch item let [segmentation, segments] = this.compute_segments( mask_probs_item, pred_scores_item, pred_labels_item, mask_threshold, overlap_mask_area_threshold, label_ids_to_fuse, target_size, ) toReturn.push({ segmentation: segmentation, segments_info: segments }) } return toReturn; } post_process_instance_segmentation() { // TODO throw Error("Not implemented yet"); } } class YolosFeatureExtractor extends ImageFeatureExtractor { /** @type {post_process_object_detection} */ post_process_object_detection(...args) { return post_process_object_detection(...args); } } /** * @typedef {object} SamImageProcessorResult * @property {Tensor} pixel_values * @property {HeightWidth[]} original_sizes * @property {HeightWidth[]} reshaped_input_sizes * @property {Tensor} [input_points] * @property {Tensor} [input_labels] * @property {Tensor} [input_boxes] */ class SamImageProcessor extends ImageFeatureExtractor { /** * * @param {any} input_points * @param {HeightWidth[]} original_sizes * @param {HeightWidth[]} reshaped_input_sizes * @returns {Tensor} */ reshape_input_points(input_points, original_sizes, reshaped_input_sizes, is_bounding_box = false) { // Make deep copy to avoid altering user's input input_points = structuredClone(input_points); let shape = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_points); // TODO: add support for 2D input_points if (shape.length === 3) { // Correct user's input if (!is_bounding_box) { shape = [1, ...shape]; } input_points = [input_points]; } else if (shape.length !== 4) { throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`.") } // Reshape input points for (let i = 0; i < input_points.length; ++i) { // batch_size let originalImageSize = original_sizes[i]; let reshapedImageSize = reshaped_input_sizes[i]; let resizeFactors = [ reshapedImageSize[0] / originalImageSize[0], reshapedImageSize[1] / originalImageSize[1] ] for (let j = 0; j < input_points[i].length; ++j) { // point_batch_size for (let k = 0; k < input_points[i][j].length; ++k) { // nb_points_per_image for (let w = 0; w < input_points[i][j][k].length; ++w) { // 2 or 4 input_points[i][j][k][w] *= resizeFactors[w % 2]; } } } } return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( 'float32', Float32Array.from(input_points.flat(Infinity)), shape ) } /** * * @param {any} input_labels * @param {Tensor} input_points * @returns {Tensor} */ add_input_labels(input_labels, input_points) { let shape = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_labels); if (shape.length === 2) { // Correct user's input shape = [1, ...shape]; input_labels = [input_labels]; } else if (shape.length !== 3) { throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`.") } if (shape.some((x, i) => x !== input_points.dims[i])) { throw Error(`The first ${shape.length} dimensions of 'input_points' and 'input_labels' must be the same.`) } return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( 'int64', input_labels.flat(Infinity).map(BigInt), shape, ) } /** * @param {any[]} images The URL(s) of the image(s) to extract features from. * @param {any} [input_points] A 3D or 4D array, representing the input points provided by the user. * - 3D: `[point_batch_size, nb_points_per_image, 2]`. In this case, `batch_size` is assumed to be 1. * - 4D: `[batch_size, point_batch_size, nb_points_per_image, 2]`. * @param {any} [input_labels] A 2D or 3D array, representing the input labels for the points, used by the prompt encoder to encode the prompt. * - 2D: `[point_batch_size, nb_points_per_image]`. In this case, `batch_size` is assumed to be 1. * - 3D: `[batch_size, point_batch_size, nb_points_per_image]`. * @param {any} [input_boxes] A 3D array of shape `(batch_size, num_boxes, 4)`, representing the input boxes provided by the user. * This is used by the prompt encoder to encode the prompt. Generally yields to much better generated masks. * The processor will generate a tensor, with each dimension corresponding respectively to the image batch size, * the number of boxes per image and the coordinates of the top left and botton right point of the box. * In the order (`x1`, `y1`, `x2`, `y2`): * - `x1`: the x coordinate of the top left point of the input box * - `y1`: the y coordinate of the top left point of the input box * - `x2`: the x coordinate of the bottom right point of the input box * - `y2`: the y coordinate of the bottom right point of the input box * @returns {Promise} */ async _call(images, input_points = null, input_labels = null, input_boxes = null) { // TODO allow user to use preprocessed images /** @type {SamImageProcessorResult} */ const processed = await super._call(images); if (input_points) { processed.input_points = this.reshape_input_points( input_points, processed.original_sizes, processed.reshaped_input_sizes ); } if (input_labels) { if (!processed.input_points) { throw Error("`input_points` must be provided if `input_labels` are provided.") } processed.input_labels = this.add_input_labels(input_labels, processed.input_points); } if (input_boxes) { processed.input_boxes = this.reshape_input_points( input_boxes, processed.original_sizes, processed.reshaped_input_sizes, true, ); } return processed; } /** * Remove padding and upscale masks to the original image size. * @param {Tensor} masks Batched masks from the mask_decoder in (batch_size, num_channels, height, width) format. * @param {[number, number][]} original_sizes The original sizes of each image before it was resized to the model's expected input shape, in (height, width) format. * @param {[number, number][]} reshaped_input_sizes The size of each image as it is fed to the model, in (height, width) format. Used to remove padding. * @param {Object} options Optional parameters for post-processing. * @param {number} [options.mask_threshold] The threshold to use for binarizing the masks. * @param {boolean} [options.binarize] Whether to binarize the masks. * @param {Object} [options.pad_size] The target size the images were padded to before being passed to the model. If `null`, the target size is assumed to be the processor's `pad_size`. * @param {number} [options.pad_size.height] The height the images were padded to. * @param {number} [options.pad_size.width] The width the images were padded to. * @returns {Promise} Batched masks in batch_size, num_channels, height, width) format, where (height, width) is given by original_size. */ async post_process_masks(masks, original_sizes, reshaped_input_sizes, { mask_threshold = 0.0, binarize = true, pad_size = null, } = {}) { // masks: [1, 1, 3, 256, 256] const output_masks = []; pad_size = pad_size ?? this.pad_size; /** @type {[number, number]} */ const target_image_size = [pad_size.height, pad_size.width]; for (let i = 0; i < original_sizes.length; ++i) { const original_size = original_sizes[i]; const reshaped_input_size = reshaped_input_sizes[i]; // Upscale mask to padded size let interpolated_mask = (await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate_4d)( masks[i], { mode: 'bilinear', size: target_image_size } )); // Crop mask interpolated_mask = interpolated_mask.slice(null, null, [0, reshaped_input_size[0]], [0, reshaped_input_size[1]]); // Downscale mask interpolated_mask = (await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate_4d)( interpolated_mask, { mode: 'bilinear', size: original_size } )); if (binarize) { const data = interpolated_mask.data; const binarizedMaskData = new Uint8Array(data.length); for (let i = 0; i < data.length; ++i) { if (data[i] > mask_threshold) { binarizedMaskData[i] = 1; } } interpolated_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( 'bool', binarizedMaskData, interpolated_mask.dims ) } output_masks.push(interpolated_mask); } return output_masks; } /** * Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer. * @param {RawImage} image Input original image * @param {number} target_size Target size of the resized image * @param {Object} options Options for generating crop boxes * @param {number} [options.crop_n_layers] If >0, mask prediction will be run again on crops of the image. * Sets the number of layers to run, where each layer has 2**i_layer number of image crops. * @param {number} [options.overlap_ratio] Sets the degree to which crops overlap. In the first crop layer, * crops will overlap by this fraction of the image length. Later layers with more crops scale down this overlap. * @param {number} [options.points_per_crop] Number of points to sample from each crop. * @param {number} [options.crop_n_points_downscale_factor] The number of points-per-side sampled in layer n is * scaled down by crop_n_points_downscale_factor**n. * @returns {Object} An object containing the crop boxes, number of points per crop, cropped images, and input labels. */ generate_crop_boxes(image, target_size, { crop_n_layers = 0, overlap_ratio = 512 / 1500, points_per_crop = 32, crop_n_points_downscale_factor = 1, } = {}) { // TODO: Implement // return { crop_boxes, points_per_crop, cropped_images, input_labels } } } class Swin2SRImageProcessor extends ImageFeatureExtractor { pad_image(pixelData, imgDims, padSize, options = {}) { // NOTE: In this case, `padSize` represents the size of the sliding window for the local attention. // In other words, the image is padded so that its width and height are multiples of `padSize`. const [imageHeight, imageWidth, imageChannels] = imgDims; return super.pad_image(pixelData, imgDims, { // NOTE: For Swin2SR models, the original python implementation adds padding even when the image's width/height is already // a multiple of `pad_size`. However, this is most likely a bug (PR: https://github.com/mv-lab/swin2sr/pull/19). // For this reason, we only add padding when the image's width/height is not a multiple of `pad_size`. width: imageWidth + (padSize - imageWidth % padSize) % padSize, height: imageHeight + (padSize - imageHeight % padSize) % padSize, }, { mode: 'symmetric', center: false, constant_values: -1, ...options, }) } } class VitMatteImageProcessor extends ImageFeatureExtractor { /** * Calls the feature extraction process on an array of images, preprocesses * each image, and concatenates the resulting features into a single Tensor. * @param {RawImage[]} images The image(s) to extract features from. * @param {RawImage[]} trimaps The trimaps(s) to extract features from. * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. */ async _call(images, trimaps) { if (!Array.isArray(images)) { images = [images]; } if (!Array.isArray(trimaps)) { trimaps = [trimaps]; } const imageData = await Promise.all(images.map(x => this.preprocess(x))); const trimapData = await Promise.all(trimaps.map(x => this.preprocess(x, { do_normalize: false, do_convert_rgb: false, do_convert_grayscale: true, }))); // Stack pixel values const pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.stack)(imageData.map( // Concatenate images and trimaps (x, i) => (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.cat)([x.pixel_values, trimapData[i].pixel_values], 0) ), 0); return { pixel_values, // Original sizes of images original_sizes: imageData.map(x => x.original_size), // Reshaped sizes of images, before padding or cropping reshaped_input_sizes: imageData.map(x => x.reshaped_input_size), } } } class WhisperFeatureExtractor extends FeatureExtractor { constructor(config) { super(config); // Prefer given `mel_filters` from preprocessor_config.json, or calculate them if they don't exist. this.config.mel_filters ??= (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( Math.floor(1 + this.config.n_fft / 2), // num_frequency_bins this.config.feature_size, // num_mel_filters 0.0, // min_frequency 8000.0, // max_frequency this.config.sampling_rate, // sampling_rate "slaney", // norm "slaney", // mel_scale ); this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(this.config.n_fft, 'hann'); } /** * Computes the log-Mel spectrogram of the provided audio waveform. * @param {Float32Array|Float64Array} waveform The audio waveform to process. * @returns {{data: Float32Array, dims: number[]}} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. */ _extract_fbank_features(waveform) { const { data, dims } = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( waveform, this.window, // window this.config.n_fft, // frame_length this.config.hop_length, // hop_length { power: 2.0, mel_filters: this.config.mel_filters, log_mel: 'log10', // Custom max_num_frames: this.config.nb_max_frames, // 3000 } ) const maxValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(data)[0]; for (let i = 0; i < data.length; ++i) { data[i] = (Math.max(data[i], maxValue - 8.0) + 4.0) / 4.0; } return { data, dims }; } /** * Asynchronously extracts features from a given audio using the provided configuration. * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. */ async _call(audio) { validate_audio_inputs(audio, 'WhisperFeatureExtractor'); let waveform; if (audio.length > this.config.n_samples) { console.warn( "Attempting to extract features for audio longer than 30 seconds. " + "If using a pipeline to extract transcript from a long audio clip, " + "remember to specify `chunk_length_s` and/or `stride_length_s`." ); waveform = audio.slice(0, this.config.n_samples); } else { // pad with zeros waveform = new Float32Array(this.config.n_samples); waveform.set(audio); } const { data, dims } = this._extract_fbank_features(waveform); return { input_features: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', data, [1, ...dims] ) }; } } class Wav2Vec2FeatureExtractor extends FeatureExtractor { /** * @param {Float32Array} input_values * @returns {Float32Array} */ _zero_mean_unit_var_norm(input_values) { // TODO support batch? const sum = input_values.reduce((a, b) => a + b, 0); const mean = sum / input_values.length; const variance = input_values.reduce((a, b) => a + (b - mean) ** 2, 0) / input_values.length; return input_values.map(x => (x - mean) / Math.sqrt(variance + 1e-7)); } /** * Asynchronously extracts features from a given audio using the provided configuration. * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. * @returns {Promise<{ input_values: Tensor; attention_mask: Tensor }>} A Promise resolving to an object containing the extracted input features and attention mask as Tensors. */ async _call(audio) { validate_audio_inputs(audio, 'Wav2Vec2FeatureExtractor'); if (audio instanceof Float64Array) { audio = new Float32Array(audio); } let input_values = audio; // zero-mean and unit-variance normalization if (this.config.do_normalize) { input_values = this._zero_mean_unit_var_norm(input_values); } // TODO: allow user to pass in attention mask const shape = [1, input_values.length]; return { input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', input_values, shape), attention_mask: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('int64', new BigInt64Array(input_values.length).fill(1n), shape) }; } } class SeamlessM4TFeatureExtractor extends FeatureExtractor { constructor(config) { super(config); const sampling_rate = this.config.sampling_rate; const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( 256, // num_frequency_bins this.config.num_mel_bins, // num_mel_filters 20, // min_frequency Math.floor(sampling_rate / 2), // max_frequency sampling_rate, // sampling_rate null, // norm "kaldi", // mel_scale true, // triangularize_in_mel_space ); // Do padding: for (let i = 0; i < mel_filters.length; ++i) { mel_filters[i].push(0); } this.mel_filters = mel_filters; this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(400, 'povey', { periodic: false, }) } /** * Computes the log-Mel spectrogram of the provided audio waveform. * @param {Float32Array|Float64Array} waveform The audio waveform to process. * @param {number} max_length The maximum number of frames to return. * @returns {{data: Float32Array, dims: number[]}} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. */ _extract_fbank_features(waveform, max_length) { // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` // Kaldi compliance: 16-bit signed integers // 32768 == 2 ** 15 waveform = waveform.map((/** @type {number} */ x) => x * 32768) return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( waveform, this.window, // window 400, // frame_length 160, // hop_length { fft_length: 512, power: 2.0, center: false, preemphasis: 0.97, mel_filters: this.mel_filters, log_mel: 'log', mel_floor: 1.192092955078125e-07, remove_dc_offset: true, // Custom max_num_frames: max_length, transpose: true, } ) } /** * Asynchronously extracts features from a given audio using the provided configuration. * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. * @param {Object} options Optional parameters for feature extraction. * @param {boolean} [options.padding=true] Whether to pad the sequence to a multiple of `pad_to_multiple_of`. * @param {number} [options.pad_to_multiple_of=2] The number to pad the sequence to a multiple of. * @param {boolean} [options.do_normalize_per_mel_bins=true] Whether or not to zero-mean unit-variance normalize the input per mel-channel. * @param {boolean} [options.return_attention_mask=true] Whether to return the attention mask. * @returns {Promise<{ input_features: Tensor, attention_mask?: Tensor }>} A Promise resolving to an object containing the extracted input features and attention masks as Tensors. */ async _call(audio, { padding = true, pad_to_multiple_of = 2, do_normalize_per_mel_bins = true, return_attention_mask = true, } = {}) { validate_audio_inputs(audio, 'SeamlessM4TFeatureExtractor'); let features = this._extract_fbank_features(audio, this.config.max_length); const features_data = features.data; if (do_normalize_per_mel_bins) { const [num_features, feature_size] = features.dims; for (let i = 0; i < feature_size; ++i) { let sum = 0; for (let j = 0; j < num_features; ++j) { sum += features_data[j * feature_size + i]; } const mean = sum / num_features; let variance = 0; for (let j = 0; j < num_features; ++j) { variance += (features_data[j * feature_size + i] - mean) ** 2; } variance /= num_features - 1; // NOTE: We use ddof=1 const std = Math.sqrt(variance + 1e-7); for (let j = 0; j < num_features; ++j) { const index = j * feature_size + i; features_data[index] = (features_data[index] - mean) / std; } } } let padded_attention_mask; if (padding) { const [num_frames, num_channels] = features.dims; const pad_size = num_frames % pad_to_multiple_of; if (pad_size > 0) { const padded_data = new Float32Array(num_channels * (num_frames + pad_size)); padded_data.set(features_data) padded_data.fill(this.config.padding_value, features_data.length) const numPaddedFrames = num_frames + pad_size; features = { data: padded_data, dims: [numPaddedFrames, num_channels], } if (return_attention_mask) { padded_attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( 'int64', new BigInt64Array(numPaddedFrames), [1, numPaddedFrames], ) padded_attention_mask.data.fill(1n, 0, num_frames); } } } const [num_frames, num_channels] = features.dims; const stride = this.config.stride; const remainder = num_frames % stride; if (remainder !== 0) { throw new Error(`The number of frames (${num_frames}) must be a multiple of the stride (${stride}).`) } const input_features = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', features_data, features.dims, ).view( 1, Math.floor(num_frames / stride), num_channels * stride, ); const result = { input_features } if (return_attention_mask) { const reshapedNumFrames = input_features.dims[1]; const attention_mask_data = new BigInt64Array(reshapedNumFrames); if (padded_attention_mask) { const padded_attention_mask_data = padded_attention_mask.data; for (let i = 1, j = 0; i < num_frames; i += stride, ++j) { attention_mask_data[j] = padded_attention_mask_data[i]; } } else { attention_mask_data.fill(1n); } result.attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( 'int64', attention_mask_data, [1, reshapedNumFrames], ); } return result; } } class ASTFeatureExtractor extends FeatureExtractor { constructor(config) { super(config); const sampling_rate = this.config.sampling_rate; const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( 256, // num_frequency_bins this.config.num_mel_bins, // num_mel_filters 20, // min_frequency Math.floor(sampling_rate / 2), // max_frequency sampling_rate, // sampling_rate null, // norm "kaldi", // mel_scale true, // triangularize_in_mel_space ); // Do padding: for (let i = 0; i < mel_filters.length; ++i) { mel_filters[i].push(0); } this.mel_filters = mel_filters; this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(400, 'hann', { periodic: false, }) this.mean = this.config.mean; this.std = this.config.std; } /** * Computes the log-Mel spectrogram of the provided audio waveform. * @param {Float32Array|Float64Array} waveform The audio waveform to process. * @param {number} max_length The maximum number of frames to return. * @returns {{data: Float32Array, dims: number[]}} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. */ _extract_fbank_features(waveform, max_length) { // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( waveform, this.window, // window 400, // frame_length 160, // hop_length { fft_length: 512, power: 2.0, center: false, preemphasis: 0.97, mel_filters: this.mel_filters, log_mel: 'log', mel_floor: 1.192092955078125e-07, remove_dc_offset: true, // Custom max_num_frames: max_length, transpose: true, } ) } /** * Asynchronously extracts features from a given audio using the provided configuration. * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. * @returns {Promise<{ input_values: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. */ async _call(audio) { validate_audio_inputs(audio, 'ASTFeatureExtractor'); const features = this._extract_fbank_features(audio, this.config.max_length); if (this.config.do_normalize) { // Normalize the input audio spectrogram to have mean=0, std=0.5 const denom = this.std * 2; const features_data = features.data; for (let i = 0; i < features_data.length; ++i) { features_data[i] = (features_data[i] - this.mean) / denom; } } return { input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', features.data, [1, ...features.dims] ) }; } } class ClapFeatureExtractor extends FeatureExtractor { constructor(config) { super(config); this.mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( this.config.nb_frequency_bins, // num_frequency_bins this.config.feature_size, // num_mel_filters this.config.frequency_min, // min_frequency this.config.frequency_max, // max_frequency this.config.sampling_rate, // sampling_rate null, // norm "htk", // mel_scale ); this.mel_filters_slaney = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( this.config.nb_frequency_bins, // num_frequency_bins this.config.feature_size, // num_mel_filters this.config.frequency_min, // min_frequency this.config.frequency_max, // max_frequency this.config.sampling_rate, // sampling_rate "slaney", // norm "slaney", // mel_scale ); this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(this.config.fft_window_size, 'hann') } /** * Extracts the mel spectrogram and prepares it for the mode based on the `truncation` and `padding` arguments. * * Four different path are possible: * - `truncation="fusion"` and the length of the waveform is greater than the max length: the mel spectrogram * will be computed on the entire audio. 3 random crops and a dowsampled version of the full mel spectrogram * are then stacked together. They will later be used for `feature_fusion`. * - `truncation="rand_trunc"` and the length of the waveform is smaller than the max length: the audio is * padded based on `padding`. * - `truncation="fusion"` and the length of the waveform is smaller than the max length: the audio is padded * based on `padding`, and is repeated `4` times. * - `truncation="rand_trunc"` and the length of the waveform is greater than the max length: the mel * spectrogram will be computed on a random crop of the waveform. * * @param {Float32Array|Float64Array} waveform The input waveform. * @param {number} max_length The maximum length of the waveform. * @param {string} truncation The truncation strategy to use. * @param {string} padding The padding strategy to use. * @returns {{ data: Float32Array; dims: number[]; longer: boolean; }} An object containing the mel spectrogram data as a Float32Array, its dimensions as an array of numbers, and a boolean indicating whether the waveform was longer than the max length. */ _get_input_mel(waveform, max_length, truncation, padding) { /** @type {{ data: Float32Array; dims: number[]}} */ let input_mel; let longer = false; const diff = waveform.length - max_length; if (diff > 0) { if (truncation === 'rand_trunc') { longer = true; const idx = Math.floor(Math.random() * (diff + 1)); waveform = waveform.subarray(idx, idx + max_length); input_mel = this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); input_mel.dims = [1, ...input_mel.dims]; // "unsqueeze" } else { // TODO implement fusion strategy throw new Error(`Truncation strategy "${truncation}" not implemented`) } } else { if (diff < 0) { let padded = new Float64Array(max_length); // already padded with zeros padded.set(waveform); if (padding === 'repeat') { for (let i = waveform.length; i < max_length; i += waveform.length) { padded.set(waveform.subarray(0, Math.min(waveform.length, max_length - i)), i); } } else if (padding === 'repeatpad') { for (let i = waveform.length; i < -diff; i += waveform.length) { padded.set(waveform, i); } } waveform = padded; } if (truncation === 'fusion') { throw new Error(`Truncation strategy "${truncation}" not implemented`) } input_mel = this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); input_mel.dims = [1, ...input_mel.dims]; // "unsqueeze" } return { ...input_mel, longer, } } /** * Compute the log-mel spectrogram of the provided `waveform` using the Hann window. * In CLAP, two different filter banks are used depending on the truncation pattern: * - `self.mel_filters`: they correspond to the default parameters of `torchaudio` which can be obtained from * calling `torchaudio.transforms.MelSpectrogram().mel_scale.fb`. These filters are used when `truncation` * is set to `"fusion"`. * - `self.mel_filteres_slaney` : they correspond to the default parameters of `librosa` which used * `librosa.filters.mel` when computing the mel spectrogram. These filters were only used in the original * implementation when the truncation mode is not `"fusion"`. * * @param {Float32Array|Float64Array} waveform The audio waveform to process. * @param {number[][]} mel_filters The mel filters to use. * @param {number} [max_length=null] The maximum number of frames to return. * @returns {{data: Float32Array, dims: number[]}} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. */ _extract_fbank_features(waveform, mel_filters, max_length = null) { // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( waveform, this.window, // window this.config.fft_window_size, // frame_length this.config.hop_length, // hop_length { power: 2.0, mel_filters, log_mel: 'dB', // Custom max_num_frames: max_length, do_pad: false, transpose: true, } ) } /** * Asynchronously extracts features from a given audio using the provided configuration. * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. */ async _call(audio, { max_length = null, } = {}) { validate_audio_inputs(audio, 'ClapFeatureExtractor'); // convert to mel spectrogram, truncate and pad if needed. const padded_inputs = this._get_input_mel( audio, max_length ?? this.config.nb_max_samples, this.config.truncation, this.config.padding, ); return { input_features: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', padded_inputs.data, [1, ...padded_inputs.dims] ) }; } } class SpeechT5FeatureExtractor extends FeatureExtractor { } /** * Represents a Processor that extracts features from an input. * @extends Callable */ class Processor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * Creates a new Processor with the given feature extractor. * @param {FeatureExtractor} feature_extractor The function used to extract features from the input. */ constructor(feature_extractor) { super(); this.feature_extractor = feature_extractor; // TODO use tokenizer here? } /** * Calls the feature_extractor function with the given input. * @param {any} input The input to extract features from. * @param {...any} args Additional arguments. * @returns {Promise} A Promise that resolves with the extracted features. */ async _call(input, ...args) { return await this.feature_extractor(input, ...args); } } class SamProcessor extends Processor { /** * @borrows SamImageProcessor#_call as _call */ async _call(...args) { return await this.feature_extractor(...args); } /** * @borrows SamImageProcessor#post_process_masks as post_process_masks */ post_process_masks(...args) { // @ts-ignore return this.feature_extractor.post_process_masks(...args); } /** * @borrows SamImageProcessor#reshape_input_points as reshape_input_points */ reshape_input_points(...args) { // @ts-ignore return this.feature_extractor.reshape_input_points(...args); } } /** * Represents a WhisperProcessor that extracts features from an audio input. * @extends Processor */ class WhisperProcessor extends Processor { /** * Calls the feature_extractor function with the given audio input. * @param {any} audio The audio input to extract features from. * @returns {Promise} A Promise that resolves with the extracted features. */ async _call(audio) { return await this.feature_extractor(audio) } } class Wav2Vec2ProcessorWithLM extends Processor { /** * Calls the feature_extractor function with the given audio input. * @param {any} audio The audio input to extract features from. * @returns {Promise} A Promise that resolves with the extracted features. */ async _call(audio) { return await this.feature_extractor(audio) } } class SpeechT5Processor extends Processor { /** * Calls the feature_extractor function with the given input. * @param {any} input The input to extract features from. * @returns {Promise} A Promise that resolves with the extracted features. */ async _call(input) { return await this.feature_extractor(input) } } class OwlViTProcessor extends Processor { } ////////////////////////////////////////////////// /** * Helper class which is used to instantiate pretrained processors with the `from_pretrained` function. * The chosen processor class is determined by the type specified in the processor config. * * **Example:** Load a processor using `from_pretrained`. * ```javascript * let processor = await AutoProcessor.from_pretrained('openai/whisper-tiny.en'); * ``` * * **Example:** Run an image through a processor. * ```javascript * let processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); * let image_inputs = await processor(image); * // { * // "pixel_values": { * // "dims": [ 1, 3, 224, 224 ], * // "type": "float32", * // "data": Float32Array [ -1.558687686920166, -1.558687686920166, -1.5440893173217773, ... ], * // "size": 150528 * // }, * // "original_sizes": [ * // [ 533, 800 ] * // ], * // "reshaped_input_sizes": [ * // [ 224, 224 ] * // ] * // } * ``` */ class AutoProcessor { static FEATURE_EXTRACTOR_CLASS_MAPPING = { ImageFeatureExtractor, WhisperFeatureExtractor, ViTFeatureExtractor, MobileViTFeatureExtractor, OwlViTFeatureExtractor, Owlv2ImageProcessor, CLIPFeatureExtractor, CLIPImageProcessor, ChineseCLIPFeatureExtractor, SiglipImageProcessor, ConvNextFeatureExtractor, ConvNextImageProcessor, SegformerFeatureExtractor, BitImageProcessor, DPTImageProcessor, DPTFeatureExtractor, GLPNFeatureExtractor, BeitFeatureExtractor, DeiTFeatureExtractor, DetrFeatureExtractor, YolosFeatureExtractor, DonutFeatureExtractor, NougatImageProcessor, EfficientNetImageProcessor, ViTImageProcessor, VitMatteImageProcessor, SamImageProcessor, Swin2SRImageProcessor, Wav2Vec2FeatureExtractor, SeamlessM4TFeatureExtractor, SpeechT5FeatureExtractor, ASTFeatureExtractor, ClapFeatureExtractor, } static PROCESSOR_CLASS_MAPPING = { WhisperProcessor, Wav2Vec2ProcessorWithLM, SamProcessor, SpeechT5Processor, OwlViTProcessor, } /** * Instantiate one of the processor classes of the library from a pretrained model. * * The processor class to instantiate is selected based on the `feature_extractor_type` property of the config object * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) * * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: * - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co. * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a * user or organization name, like `dbmdz/bert-base-german-cased`. * - A path to a *directory* containing processor files, e.g., `./my_model_directory/`. * @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the processor. * * @returns {Promise} A new instance of the Processor class. */ static async from_pretrained(pretrained_model_name_or_path, { progress_callback = null, config = null, cache_dir = null, local_files_only = false, revision = 'main', } = {}) { let preprocessorConfig = config ?? await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'preprocessor_config.json', true, { progress_callback, config, cache_dir, local_files_only, revision, }) // Determine feature extractor class // TODO: Ensure backwards compatibility with old configs let key = preprocessorConfig.feature_extractor_type ?? preprocessorConfig.image_processor_type; let feature_extractor_class = this.FEATURE_EXTRACTOR_CLASS_MAPPING[key]; if (!feature_extractor_class) { if (preprocessorConfig.size !== undefined) { // Assume ImageFeatureExtractor console.warn(`Feature extractor type "${key}" not found, assuming ImageFeatureExtractor due to size parameter in config.`); feature_extractor_class = ImageFeatureExtractor; } else { throw new Error(`Unknown Feature Extractor type: ${key}`); } } // If no associated processor class, use default let processor_class = this.PROCESSOR_CLASS_MAPPING[preprocessorConfig.processor_class] ?? Processor; // Instantiate processor and feature extractor let feature_extractor = new feature_extractor_class(preprocessorConfig); return new processor_class(feature_extractor); } } ////////////////////////////////////////////////// /***/ }), /***/ "./src/tokenizers.js": /*!***************************!*\ !*** ./src/tokenizers.js ***! \***************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "AlbertTokenizer": () => (/* binding */ AlbertTokenizer), /* harmony export */ "AutoTokenizer": () => (/* binding */ AutoTokenizer), /* harmony export */ "BartTokenizer": () => (/* binding */ BartTokenizer), /* harmony export */ "BertTokenizer": () => (/* binding */ BertTokenizer), /* harmony export */ "BlenderbotSmallTokenizer": () => (/* binding */ BlenderbotSmallTokenizer), /* harmony export */ "BlenderbotTokenizer": () => (/* binding */ BlenderbotTokenizer), /* harmony export */ "BloomTokenizer": () => (/* binding */ BloomTokenizer), /* harmony export */ "CLIPTokenizer": () => (/* binding */ CLIPTokenizer), /* harmony export */ "CamembertTokenizer": () => (/* binding */ CamembertTokenizer), /* harmony export */ "CodeGenTokenizer": () => (/* binding */ CodeGenTokenizer), /* harmony export */ "CodeLlamaTokenizer": () => (/* binding */ CodeLlamaTokenizer), /* harmony export */ "CohereTokenizer": () => (/* binding */ CohereTokenizer), /* harmony export */ "ConvBertTokenizer": () => (/* binding */ ConvBertTokenizer), /* harmony export */ "DebertaTokenizer": () => (/* binding */ DebertaTokenizer), /* harmony export */ "DebertaV2Tokenizer": () => (/* binding */ DebertaV2Tokenizer), /* harmony export */ "DistilBertTokenizer": () => (/* binding */ DistilBertTokenizer), /* harmony export */ "ElectraTokenizer": () => (/* binding */ ElectraTokenizer), /* harmony export */ "EsmTokenizer": () => (/* binding */ EsmTokenizer), /* harmony export */ "FalconTokenizer": () => (/* binding */ FalconTokenizer), /* harmony export */ "GPT2Tokenizer": () => (/* binding */ GPT2Tokenizer), /* harmony export */ "GPTNeoXTokenizer": () => (/* binding */ GPTNeoXTokenizer), /* harmony export */ "GemmaTokenizer": () => (/* binding */ GemmaTokenizer), /* harmony export */ "Grok1Tokenizer": () => (/* binding */ Grok1Tokenizer), /* harmony export */ "HerbertTokenizer": () => (/* binding */ HerbertTokenizer), /* harmony export */ "LlamaTokenizer": () => (/* binding */ LlamaTokenizer), /* harmony export */ "M2M100Tokenizer": () => (/* binding */ M2M100Tokenizer), /* harmony export */ "MBart50Tokenizer": () => (/* binding */ MBart50Tokenizer), /* harmony export */ "MBartTokenizer": () => (/* binding */ MBartTokenizer), /* harmony export */ "MPNetTokenizer": () => (/* binding */ MPNetTokenizer), /* harmony export */ "MarianTokenizer": () => (/* binding */ MarianTokenizer), /* harmony export */ "MobileBertTokenizer": () => (/* binding */ MobileBertTokenizer), /* harmony export */ "NllbTokenizer": () => (/* binding */ NllbTokenizer), /* harmony export */ "NougatTokenizer": () => (/* binding */ NougatTokenizer), /* harmony export */ "PreTrainedTokenizer": () => (/* binding */ PreTrainedTokenizer), /* harmony export */ "Qwen2Tokenizer": () => (/* binding */ Qwen2Tokenizer), /* harmony export */ "RoFormerTokenizer": () => (/* binding */ RoFormerTokenizer), /* harmony export */ "RobertaTokenizer": () => (/* binding */ RobertaTokenizer), /* harmony export */ "SiglipTokenizer": () => (/* binding */ SiglipTokenizer), /* harmony export */ "SpeechT5Tokenizer": () => (/* binding */ SpeechT5Tokenizer), /* harmony export */ "SqueezeBertTokenizer": () => (/* binding */ SqueezeBertTokenizer), /* harmony export */ "T5Tokenizer": () => (/* binding */ T5Tokenizer), /* harmony export */ "TokenizerModel": () => (/* binding */ TokenizerModel), /* harmony export */ "VitsTokenizer": () => (/* binding */ VitsTokenizer), /* harmony export */ "Wav2Vec2CTCTokenizer": () => (/* binding */ Wav2Vec2CTCTokenizer), /* harmony export */ "WhisperTokenizer": () => (/* binding */ WhisperTokenizer), /* harmony export */ "XLMRobertaTokenizer": () => (/* binding */ XLMRobertaTokenizer), /* harmony export */ "XLMTokenizer": () => (/* binding */ XLMTokenizer) /* harmony export */ }); /* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); /* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); /* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); /* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); /* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); /* harmony import */ var _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/data-structures.js */ "./src/utils/data-structures.js"); /* harmony import */ var _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! @huggingface/jinja */ "./node_modules/@huggingface/jinja/dist/index.js"); /** * @file Tokenizers are used to prepare textual inputs for a model. * * **Example:** Create an `AutoTokenizer` and use it to tokenize a sentence. * This will automatically detect the tokenizer type based on the tokenizer class defined in `tokenizer.json`. * ```javascript * import { AutoTokenizer } from '@xenova/transformers'; * * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); * const { input_ids } = await tokenizer('I love transformers!'); * // Tensor { * // data: BigInt64Array(6) [101n, 1045n, 2293n, 19081n, 999n, 102n], * // dims: [1, 6], * // type: 'int64', * // size: 6, * // } * ``` * * @module tokenizers */ /** * @typedef {Object} TokenizerProperties Additional tokenizer-specific properties. * @property {boolean} [legacy=false] Whether or not the `legacy` behavior of the tokenizer should be used. * @typedef {import('./utils/hub.js').PretrainedOptions & TokenizerProperties} PretrainedTokenizerOptions */ /** * Loads a tokenizer from the specified path. * @param {string} pretrained_model_name_or_path The path to the tokenizer directory. * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. * @returns {Promise} A promise that resolves with information about the loaded tokenizer. */ async function loadTokenizer(pretrained_model_name_or_path, options) { const info = await Promise.all([ (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'tokenizer.json', true, options), (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'tokenizer_config.json', true, options), ]) // Override legacy option if `options.legacy` is not null if (options.legacy !== null) { info[1].legacy = options.legacy; } return info; } /** * Helper function to split a string on a regex, but keep the delimiters. * This is required, because the JavaScript `.split()` method does not keep the delimiters, * and wrapping in a capturing group causes issues with existing capturing groups (due to nesting). * @param {string} text The text to split. * @param {RegExp} regex The regex to split on. * @returns {string[]} The split string. */ function regexSplit(text, regex) { const result = []; let prev = 0; for (const match of text.matchAll(regex)) { const fullMatch = match[0]; if (prev < match.index) { result.push(text.slice(prev, match.index)); } if (fullMatch.length > 0) { result.push(fullMatch); } prev = match.index + fullMatch.length; } if (prev < text.length) { result.push(text.slice(prev)); } return result; } /** * Helper method to construct a pattern from a config object. * @param {Object} pattern The pattern object. * @param {boolean} invert Whether to invert the pattern. * @returns {RegExp|null} The compiled pattern. */ function createPattern(pattern, invert = true) { if (pattern.Regex !== undefined) { // In certain cases, the pattern may contain unnecessary escape sequences (e.g., \# or \& or \~). // i.e., valid in Python (where the patterns are exported from) but invalid in JavaScript (where the patterns are parsed). // This isn't an issue when creating the regex w/o the 'u' flag, but it is when the 'u' flag is used. // For this reason, it is necessary to remove these backslashes before creating the regex. // See https://stackoverflow.com/a/63007777/13989043 for more information let regex = pattern.Regex.replace(/\\([#&~])/g, '$1'); // TODO: add more characters to this list if necessary // We also handle special cases where the regex contains invalid (non-JS compatible) syntax. for (const [key, value] of PROBLEMATIC_REGEX_MAP) { regex = regex.replaceAll(key, value); } return new RegExp(regex, 'gu'); } else if (pattern.String !== undefined) { const escaped = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(pattern.String); // NOTE: if invert is true, we wrap the pattern in a group so that it is kept when performing .split() return new RegExp(invert ? escaped : `(${escaped})`, 'gu'); } else { console.warn('Unknown pattern type:', pattern) return null; } } /** * Helper function to convert an Object to a Map * @param {Object} obj The object to convert. * @returns {Map} The map. */ function objectToMap(obj) { return new Map(Object.entries(obj)); } /** * Helper function to convert a tensor to a list before decoding. * @param {Tensor} tensor The tensor to convert. * @returns {number[]} The tensor as a list. */ function prepareTensorForDecode(tensor) { const dims = tensor.dims; switch (dims.length) { case 1: return tensor.tolist(); case 2: if (dims[0] !== 1) { throw new Error('Unable to decode tensor with `batch size !== 1`. Use `tokenizer.batch_decode(...)` for batched inputs.'); } return tensor.tolist()[0]; default: throw new Error(`Expected tensor to have 1-2 dimensions, got ${dims.length}.`) } } /** * Clean up a list of simple English tokenization artifacts like spaces before punctuations and abbreviated forms * @param {string} text The text to clean up. * @returns {string} The cleaned up text. */ function clean_up_tokenization(text) { // Clean up a list of simple English tokenization artifacts // like spaces before punctuations and abbreviated forms return text.replace(/ \./g, '.') .replace(/ \?/g, '?') .replace(/ \!/g, '!') .replace(/ ,/g, ',') .replace(/ \' /g, "'") .replace(/ n\'t/g, "n't") .replace(/ \'m/g, "'m") .replace(/ \'s/g, "'s") .replace(/ \'ve/g, "'ve") .replace(/ \'re/g, "'re"); } /** * Helper function to remove accents from a string. * @param {string} text The text to remove accents from. * @returns {string} The text with accents removed. */ function remove_accents(text) { return text.replace(/[\u0300-\u036f]/g, ''); } /** * Helper function to lowercase a string and remove accents. * @param {string} text The text to lowercase and remove accents from. * @returns {string} The lowercased text with accents removed. */ function lowercase_and_remove_accent(text) { return remove_accents(text.toLowerCase()); } /** * Helper function to fuse consecutive values in an array equal to the specified value. * @param {string[]} arr The input array * @param {any} value The value to fuse on. * @param {Map} mapping The mapping from input domain to value. */ function fuse(arr, value, mapping) { const fused = []; let i = 0; while (i < arr.length) { fused.push(arr[i]) if ((mapping.get(arr[i]) ?? value) !== value) { ++i; continue; } while (i < arr.length && (mapping.get(arr[i]) ?? value) === value) { ++i; } } return fused; } /** * Split a string on whitespace. * @param {string} text The text to split. * @returns {string[]} The split string. */ function whitespace_split(text) { return text.match(/\S+/g) || []; } const PUNCTUATION_REGEX = '\\p{P}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E'; // A mapping of regex patterns to their equivalent (but longer) JS-compatible versions. const PROBLEMATIC_REGEX_MAP = new Map([ // This uses the case insensitive group modifier, which is not supported in JavaScript. // When parsing the regex, an "Invalid group" error is thrown. ["(?i:'s|'t|'re|'ve|'m|'ll|'d)", "(?:'([sS]|[tT]|[rR][eE]|[vV][eE]|[mM]|[lL][lL]|[dD]))"], ]) /** * Represent a token added by the user on top of the existing Model vocabulary. * AddedToken can be configured to specify the behavior they should have in various situations like: * - Whether they should only match single words * - Whether to include any whitespace on its left or right */ class AddedToken { /** * Creates a new instance of AddedToken. * @param {Object} config Added token configuration object. * @param {string} config.content The content of the added token. * @param {number} config.id The id of the added token. * @param {boolean} [config.single_word=false] Whether this token must be a single word or can break words. * @param {boolean} [config.lstrip=false] Whether this token should strip whitespaces on its left. * @param {boolean} [config.rstrip=false] Whether this token should strip whitespaces on its right. * @param {boolean} [config.normalized=false] Whether this token should be normalized. * @param {boolean} [config.special=false] Whether this token is special. */ constructor(config) { this.content = config.content; this.id = config.id; this.single_word = config.single_word ?? false; this.lstrip = config.lstrip ?? false; this.rstrip = config.rstrip ?? false; this.special = config.special ?? false; this.normalized = config.normalized ?? null; } } /** * Abstract base class for tokenizer models. * * @extends Callable */ class TokenizerModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * Creates a new instance of TokenizerModel. * @param {Object} config The configuration object for the TokenizerModel. */ constructor(config) { super(); this.config = config; /** @type {string[]} */ this.vocab = []; /** * A mapping of tokens to ids. * @type {Map} */ this.tokens_to_ids = new Map(); this.unk_token_id = undefined; this.unk_token = undefined; this.end_of_word_suffix = undefined; /** @type {boolean} Whether to fuse unknown tokens when encoding. Defaults to false. */ this.fuse_unk = this.config.fuse_unk ?? false; } /** * Instantiates a new TokenizerModel instance based on the configuration object provided. * @param {Object} config The configuration object for the TokenizerModel. * @param {...*} args Optional arguments to pass to the specific TokenizerModel constructor. * @returns {TokenizerModel} A new instance of a TokenizerModel. * @throws Will throw an error if the TokenizerModel type in the config is not recognized. */ static fromConfig(config, ...args) { switch (config.type) { case 'WordPiece': return new WordPieceTokenizer(config); case 'Unigram': // @ts-ignore return new Unigram(config, ...args); case 'BPE': return new BPE(config); default: if (config.vocab) { // @ts-ignore return new LegacyTokenizerModel(config, ...args); } throw new Error(`Unknown TokenizerModel type: ${config.type}`); } } /** * Internal function to call the TokenizerModel instance. * @param {string[]} tokens The tokens to encode. * @returns {string[]} The encoded token IDs. */ _call(tokens) { let ids = this.encode(tokens); if (this.fuse_unk) { // Fuse unknown tokens ids = fuse(ids, this.unk_token_id, this.tokens_to_ids); } return ids; } /** * Encodes a list of tokens into a list of token IDs. * @param {string[]} tokens The tokens to encode. * @returns {string[]} The encoded tokens. * @throws Will throw an error if not implemented in a subclass. */ encode(tokens) { throw Error("encode should be implemented in subclass.") } /** * Converts a list of tokens into a list of token IDs. * @param {string[]} tokens The tokens to convert. * @returns {number[]} The converted token IDs. */ convert_tokens_to_ids(tokens) { return tokens.map(t => this.tokens_to_ids.get(t) ?? this.unk_token_id); } /** * Converts a list of token IDs into a list of tokens. * @param {number[]} ids The token IDs to convert. * @returns {string[]} The converted tokens. */ convert_ids_to_tokens(ids) { return ids.map(i => this.vocab[i] ?? this.unk_token); } } /** * A subclass of TokenizerModel that uses WordPiece encoding to encode tokens. * @extends TokenizerModel */ class WordPieceTokenizer extends TokenizerModel { /** * @param {Object} config The configuration object. * @param {Object} config.vocab A mapping of tokens to ids. * @param {string} config.unk_token The unknown token string. * @param {string} config.continuing_subword_prefix The prefix to use for continuing subwords. * @param {number} [config.max_input_chars_per_word=100] The maximum number of characters per word. */ constructor(config) { super(config); /** * A mapping of tokens to ids. * @type {Map} */ this.tokens_to_ids = objectToMap(config.vocab); /** * The id of the unknown token. * @type {number} */ this.unk_token_id = this.tokens_to_ids.get(config.unk_token); /** * The unknown token string. * @type {string} */ this.unk_token = config.unk_token; /** * The maximum number of characters allowed per word. * @type {number} */ this.max_input_chars_per_word = config.max_input_chars_per_word ?? 100; /** * An array of tokens. * @type {string[]} */ this.vocab = new Array(this.tokens_to_ids.size); for (const [key, value] of this.tokens_to_ids) { this.vocab[value] = key; } } /** * Encodes an array of tokens using WordPiece encoding. * @param {string[]} tokens The tokens to encode. * @returns {string[]} An array of encoded tokens. */ encode(tokens) { const outputTokens = []; for (const token of tokens) { const chars = [...token]; if (chars.length > this.max_input_chars_per_word) { outputTokens.push(this.unk_token); continue; } let isUnknown = false; let start = 0; const subTokens = []; while (start < chars.length) { let end = chars.length; let currentSubstring = null; while (start < end) { let substr = chars.slice(start, end).join(''); if (start > 0) { substr = this.config.continuing_subword_prefix + substr; } if (this.tokens_to_ids.has(substr)) { currentSubstring = substr; break; } --end; } if (currentSubstring === null) { isUnknown = true; break; } subTokens.push(currentSubstring); start = end; } if (isUnknown) { outputTokens.push(this.unk_token); } else { outputTokens.push(...subTokens); } } return outputTokens; } } /** * Class representing a Unigram tokenizer model. * @extends TokenizerModel */ class Unigram extends TokenizerModel { /** * Create a new Unigram tokenizer model. * @param {Object} config The configuration object for the Unigram model. * @param {number} config.unk_id The ID of the unknown token * @param {any[][]} config.vocab A 2D array representing a mapping of tokens to scores. * @param {Object} moreConfig Additional configuration object for the Unigram model. */ constructor(config, moreConfig) { super(config); const vocabSize = config.vocab.length; this.vocab = new Array(vocabSize); this.scores = new Array(vocabSize); for (let i = 0; i < vocabSize; ++i) { const piece = config.vocab[i]; this.vocab[i] = piece[0]; this.scores[i] = piece[1]; } this.unk_token_id = config.unk_id; this.unk_token = this.vocab[config.unk_id]; this.tokens_to_ids = new Map(this.vocab.map((x, i) => [x, i])); this.bosToken = ' '; // beginning of a sentence token this.bosTokenId = this.tokens_to_ids.get(this.bosToken); // NOTE: may be undefined this.eosToken = moreConfig.eos_token; this.eosTokenId = this.tokens_to_ids.get(this.eosToken); this.unkToken = this.vocab[this.unk_token_id]; this.minScore = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(this.scores)[0]; this.unkScore = this.minScore - 10.0; this.scores[this.unk_token_id] = this.unkScore; this.trie = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.CharTrie(); this.trie.extend(this.vocab); // NOTE: `fuse_unk` is hardcoded to true for Unigram models // See: https://github.com/huggingface/tokenizers/blob/b58227c7f1ccf8b73ee2268354336da56d91e492/tokenizers/src/models/unigram/model.rs#L119 this.fuse_unk = true; } /** * Populates lattice nodes. * @param {TokenLattice} lattice The token lattice to populate with nodes. */ populateNodes(lattice) { const sentence = lattice.sentence; const len = sentence.length; let beginPos = 0; while (beginPos < len) { const mblen = 1; let hasSingleNode = false; const tokens = []; for (let token of this.trie.commonPrefixSearch(sentence.slice(beginPos))) { tokens.push(token); const tokenId = this.tokens_to_ids.get(token); const tokenScore = this.scores[tokenId]; const n = token.length; lattice.insert(beginPos, n, tokenScore, tokenId); if (!hasSingleNode && n === mblen) { hasSingleNode = true; } } if (!hasSingleNode) { lattice.insert(beginPos, mblen, this.unkScore, this.unk_token_id); } beginPos += mblen; } } /** * Encodes an array of tokens into an array of subtokens using the unigram model. * * @param {string} normalized The normalized string. * @returns {string[]} An array of subtokens obtained by encoding the input tokens using the unigram model. */ tokenize(normalized) { const lattice = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.TokenLattice(normalized, this.bosTokenId, this.eosTokenId); this.populateNodes(lattice); return lattice.tokens(); } /** * Encodes an array of tokens using Unigram encoding. * @param {string[]} tokens The tokens to encode. * @returns {string[]} An array of encoded tokens. */ encode(tokens) { const toReturn = []; for (const token of tokens) { const tokenized = this.tokenize(token); toReturn.push(...tokenized); } return toReturn; } } /** * Returns list of utf-8 byte and a mapping to unicode strings. * Specifically avoids mapping to whitespace/control characters the BPE code barfs on. * @returns {Object} Object with utf-8 byte keys and unicode string values. */ const BYTES_TO_UNICODE = (() => { // Returns list of utf-8 byte and a mapping to unicode strings. // We specifically avoids mapping to whitespace/control characters // the bpe code barfs on. const bs = [ ...Array.from({ length: "~".charCodeAt(0) - "!".charCodeAt(0) + 1 }, (_, i) => i + "!".charCodeAt(0)), ...Array.from({ length: "¬".charCodeAt(0) - "¡".charCodeAt(0) + 1 }, (_, i) => i + "¡".charCodeAt(0)), ...Array.from({ length: "ÿ".charCodeAt(0) - "®".charCodeAt(0) + 1 }, (_, i) => i + "®".charCodeAt(0)), ]; const cs = bs.slice(); let n = 0; for (let b = 0; b < 256; ++b) { if (!bs.includes(b)) { bs.push(b); cs.push(256 + n); n += 1; } } const ccs = cs.map(n => String.fromCharCode(n)); return Object.fromEntries(bs.map((b, i) => [b, ccs[i]])); })(); const UNICODE_TO_BYTES = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.reverseDictionary)(BYTES_TO_UNICODE); /** * @typedef {Object} BPENode * @property {string} token The token associated with the node * @property {number} bias A positional bias for the node. * @property {number} [score] The score of the node. * @property {BPENode} [prev] The previous node in the linked list. * @property {BPENode} [next] The next node in the linked list. */ /** * BPE class for encoding text into Byte-Pair-Encoding (BPE) tokens. * @extends TokenizerModel */ class BPE extends TokenizerModel { /** * Create a BPE instance. * @param {Object} config The configuration object for BPE. * @param {Object} config.vocab A mapping of tokens to ids. * @param {string} config.unk_token The unknown token used for out of vocabulary words. * @param {string} config.end_of_word_suffix The suffix to place at the end of each word. * @param {string} [config.continuing_subword_suffix] The suffix to insert between words. * @param {Array} config.merges An array of BPE merges as strings. */ constructor(config) { super(config); this.BPE_SPLIT_TOKEN = ' '; /** @type {Map} */ this.tokens_to_ids = objectToMap(config.vocab); this.unk_token_id = this.tokens_to_ids.get(config.unk_token); this.unk_token = config.unk_token; this.vocab = new Array(this.tokens_to_ids.size); for (const [key, value] of this.tokens_to_ids) { this.vocab[value] = key; } this.bpe_ranks = new Map(config.merges.map((x, i) => [x, i])); this.merges = config.merges.map(x => x.split(this.BPE_SPLIT_TOKEN)); this.end_of_word_suffix = config.end_of_word_suffix; // NOTE: `continuing_subword_suffix` is custom (to support `BlenderbotSmallTokenizer`) this.continuing_subword_suffix = config.continuing_subword_suffix ?? null; this.byte_fallback = this.config.byte_fallback ?? false; if (this.byte_fallback) { this.text_encoder = new TextEncoder(); } /** @type {Map} */ this.cache = new Map(); } /** * Apply Byte-Pair-Encoding (BPE) to a given token. Efficient heap-based priority * queue implementation adapted from https://github.com/belladoreai/llama-tokenizer-js. * @param {string} token The token to encode. * @returns {string[]} The BPE encoded tokens. */ bpe(token) { if (token.length === 0) { return []; } const cached = this.cache.get(token); if (cached !== undefined) { return cached; } const word = Array.from(token); if (this.end_of_word_suffix) { word[word.length - 1] += this.end_of_word_suffix; } let result = []; if (word.length > 1) { // Create a priority queue to store the nodes that will be merged. // The comparator function compares the scores of the nodes. const queue = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.PriorityQueue((a, b) => a.score < b.score); // Construct a doubly-linked list of nodes that will be inserted into the priority queue, // starting with the individual characters. We also populate each node with a positional // bias to break ties in the priority queue. let startingNode = { token: word[0], bias: 0, prev: null, next: null, } let previousNode = startingNode for (let i = 1; i < word.length; ++i) { const currentNode = { bias: i / word.length, // Add fractional component to break ties token: word[i], prev: previousNode, next: null, } previousNode.next = currentNode this._add_node(queue, previousNode) previousNode = currentNode } while (!queue.isEmpty()) { // Get the next node with the highest priority const node = queue.pop(); // Check that this merge is still possible if (node.deleted || !node.next || node.next.deleted) continue; // Here, we mark the current node (left side of the merge) and the next node (right side of the merge) as deleted. // This is because they will both be replaced by a new node representing the merge result. node.deleted = true; node.next.deleted = true; // Next, we fix the node that comes before the current node (i.e., left side of the merge). if (node.prev) { // Make a shallow copy of the previous node const newPreviousNode = { ...node.prev }; // Mark the old previous node as deleted. This avoids erroneous merges later, // because there may still be references to this node in the priority queue. node.prev.deleted = true; node.prev = newPreviousNode; // Update the reference of the previous node, by pointing its previous node to this new previous node. if (newPreviousNode.prev) { newPreviousNode.prev.next = newPreviousNode; } else { // If the previous of the previous node does not exist, it means that // `newPreviousNode` must be the new `startingNode`. startingNode = newPreviousNode; } } // Create a new node which represents the result of the merge. const merged = { token: node.token + node.next.token, bias: node.bias, prev: node.prev, next: node.next.next, } // We now consider where we can add the new merged node to the priority queue: // 1. prev <-> merged if (merged.prev) { merged.prev.next = merged; this._add_node(queue, merged.prev); } else { // If `merged.prev` does not exist, then `merged` must be the new `startingNode`. startingNode = merged; } // 2. merged <-> next if (merged.next) { merged.next.prev = merged; this._add_node(queue, merged); } } // Traverse the linked list, starting from the `startingNode`, and collect the tokens. for (let currentNode = startingNode; currentNode !== null; currentNode = currentNode.next) { result.push(currentNode.token); } } else { result = word; } // Possibly append suffix if (this.continuing_subword_suffix) { // Do not append suffix to the last token for (let i = 0; i < result.length - 1; ++i) { result[i] += this.continuing_subword_suffix; } } // Save the result to the cache this.cache.set(token, result); return result; } /** * Helper function to add a node to the priority queue. * @param {PriorityQueue} queue * @param {BPENode} node * @private */ _add_node(queue, node) { // `score` is a measure of the merge priority: lower means higher priority // We use the BPE rank as a measure of priority (i.e., the local of the merge in the merges list) // We also add a fractional component to the score to break ties (with the earlier character having higher priority) const rank = this.bpe_ranks.get(node.token + this.BPE_SPLIT_TOKEN + node.next.token); if (rank !== undefined) { node.score = rank + node.bias; queue.push(node); } } /** * Encodes the input sequence of tokens using the BPE algorithm and returns the resulting subword tokens. * @param {string[]} tokens The input sequence of tokens to encode. * @returns {string[]} The resulting subword tokens after applying the BPE algorithm to the input sequence of tokens. */ encode(tokens) { const outputTokens = []; for (const token of tokens) { const bpe_token_list = this.bpe(token); for (const t of bpe_token_list) { if (this.tokens_to_ids.has(t)) { outputTokens.push(t); } else { if (this.byte_fallback) { outputTokens.push( ...Array.from(this.text_encoder.encode(t)) .map(x => `<0x${x.toString(16).toUpperCase().padStart(2, '0')}>`) ); } else { outputTokens.push(this.unk_token); } } } } return outputTokens; } } /** * Legacy tokenizer class for tokenizers with only a vocabulary. */ class LegacyTokenizerModel extends TokenizerModel { /** * Create a LegacyTokenizerModel instance. * @param {Object} config The configuration object for LegacyTokenizerModel. * @param {Object} config.vocab A (possibly nested) mapping of tokens to ids. * @param {Object} moreConfig Additional configuration object for the LegacyTokenizerModel model. */ constructor(config, moreConfig) { super(config); /**@type {Map} */ this.tokens_to_ids = objectToMap( moreConfig.target_lang ? config.vocab[moreConfig.target_lang] : config.vocab ); this.bos_token = moreConfig.bos_token; this.bos_token_id = this.tokens_to_ids.get(this.bos_token); this.eos_token = moreConfig.eos_token; this.eos_token_id = this.tokens_to_ids.get(this.eos_token); this.pad_token = moreConfig.pad_token; this.pad_token_id = this.tokens_to_ids.get(this.pad_token); this.unk_token = moreConfig.unk_token; this.unk_token_id = this.tokens_to_ids.get(this.unk_token); this.vocab = new Array(this.tokens_to_ids.size); for (const [key, value] of this.tokens_to_ids) { this.vocab[value] = key; } } encode(tokens) { return tokens; } } /** * A base class for text normalization. * @abstract */ class Normalizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * @param {Object} config The configuration object for the normalizer. */ constructor(config) { super(); this.config = config; } /** * Factory method for creating normalizers from config objects. * @static * @param {Object} config The configuration object for the normalizer. * @returns {Normalizer} A Normalizer object. * @throws {Error} If an unknown Normalizer type is specified in the config. */ static fromConfig(config) { if (config === null) return null; switch (config.type) { case 'BertNormalizer': return new BertNormalizer(config); case 'Precompiled': return new Precompiled(config); case 'Sequence': return new NormalizerSequence(config); case 'Replace': return new Replace(config); case 'NFC': return new NFC(config); case 'NFKC': return new NFKC(config); case 'NFKD': return new NFKD(config); case 'Strip': return new StripNormalizer(config); case 'StripAccents': return new StripAccents(config); case 'Lowercase': return new Lowercase(config); case 'Prepend': return new Prepend(config); default: throw new Error(`Unknown Normalizer type: ${config.type}`); } } /** * Normalize the input text. * @abstract * @param {string} text The text to normalize. * @returns {string} The normalized text. * @throws {Error} If this method is not implemented in a subclass. */ normalize(text) { throw Error("normalize should be implemented in subclass.") } /** * Alias for {@link Normalizer#normalize}. * @param {string} text The text to normalize. * @returns {string} The normalized text. */ _call(text) { return this.normalize(text); } } /** * Replace normalizer that replaces occurrences of a pattern with a given string or regular expression. * @extends Normalizer */ class Replace extends Normalizer { /** * Normalize the input text by replacing the pattern with the content. * @param {string} text The input text to be normalized. * @returns {string} The normalized text after replacing the pattern with the content. */ normalize(text) { const pattern = createPattern(this.config.pattern); return pattern === null ? text : text.replaceAll(pattern, this.config.content); } } /** * A normalizer that applies Unicode normalization form C (NFC) to the input text. * @extends Normalizer */ class NFC extends Normalizer { /** * Normalize the input text by applying Unicode normalization form C (NFC). * @param {string} text The input text to be normalized. * @returns {string} The normalized text. */ normalize(text) { text = text.normalize('NFC') return text; } } /** * NFKC Normalizer. * @extends Normalizer */ class NFKC extends Normalizer { /** * Normalize text using NFKC normalization. * @param {string} text The text to be normalized. * @returns {string} The normalized text. */ normalize(text) { text = text.normalize('NFKC') return text; } } /** * NFKD Normalizer. * @extends Normalizer */ class NFKD extends Normalizer { /** * Normalize text using NFKD normalization. * @param {string} text The text to be normalized. * @returns {string} The normalized text. */ normalize(text) { text = text.normalize('NFKD') return text; } } /** * A normalizer that strips leading and/or trailing whitespace from the input text. */ class StripNormalizer extends Normalizer { /** * Strip leading and/or trailing whitespace from the input text. * @param {string} text The input text. * @returns {string} The normalized text. */ normalize(text) { if (this.config.strip_left && this.config.strip_right) { // Fast path to avoid an extra trim call text = text.trim(); } else { if (this.config.strip_left) { text = text.trimStart(); } if (this.config.strip_right) { text = text.trimEnd(); } } return text; } } /** * StripAccents normalizer removes all accents from the text. * @extends Normalizer */ class StripAccents extends Normalizer { /** * Remove all accents from the text. * @param {string} text The input text. * @returns {string} The normalized text without accents. */ normalize(text) { text = remove_accents(text); return text; } } /** * A Normalizer that lowercases the input string. * @extends Normalizer */ class Lowercase extends Normalizer { /** * Lowercases the input string. * @param {string} text The text to normalize. * @returns {string} The normalized text. */ normalize(text) { text = text.toLowerCase(); return text; } } /** * A Normalizer that prepends a string to the input string. * @extends Normalizer */ class Prepend extends Normalizer { /** * Prepends the input string. * @param {string} text The text to normalize. * @returns {string} The normalized text. */ normalize(text) { text = this.config.prepend + text; return text; } } /** * A Normalizer that applies a sequence of Normalizers. * @extends Normalizer */ class NormalizerSequence extends Normalizer { /** * Create a new instance of NormalizerSequence. * @param {Object} config The configuration object. * @param {Object[]} config.normalizers An array of Normalizer configuration objects. */ constructor(config) { super(config); this.normalizers = config.normalizers.map(x => Normalizer.fromConfig(x)); } /** * Apply a sequence of Normalizers to the input text. * @param {string} text The text to normalize. * @returns {string} The normalized text. */ normalize(text) { return this.normalizers.reduce((t, normalizer) => { return normalizer.normalize(t); }, text); } } /** * A class representing a normalizer used in BERT tokenization. * @extends Normalizer */ class BertNormalizer extends Normalizer { /** * Adds whitespace around any CJK (Chinese, Japanese, or Korean) character in the input text. * * @param {string} text The input text to tokenize. * @returns {string} The tokenized text with whitespace added around CJK characters. */ _tokenize_chinese_chars(text) { /* Adds whitespace around any CJK character. */ const output = []; for (let i = 0; i < text.length; ++i) { const char = text[i]; const cp = char.charCodeAt(0); if (this._is_chinese_char(cp)) { output.push(" "); output.push(char); output.push(" "); } else { output.push(char); } } return output.join(""); } /** * Checks whether the given Unicode codepoint represents a CJK (Chinese, Japanese, or Korean) character. * * A "chinese character" is defined as anything in the CJK Unicode block: * https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) * * Note that the CJK Unicode block is NOT all Japanese and Korean characters, despite its name. * The modern Korean Hangul alphabet is a different block, as is Japanese Hiragana and Katakana. * Those alphabets are used to write space-separated words, so they are not treated specially * and are handled like all other languages. * * @param {number} cp The Unicode codepoint to check. * @returns {boolean} True if the codepoint represents a CJK character, false otherwise. */ _is_chinese_char(cp) { return ( (cp >= 0x4E00 && cp <= 0x9FFF) || (cp >= 0x3400 && cp <= 0x4DBF) || (cp >= 0x20000 && cp <= 0x2A6DF) || (cp >= 0x2A700 && cp <= 0x2B73F) || (cp >= 0x2B740 && cp <= 0x2B81F) || (cp >= 0x2B820 && cp <= 0x2CEAF) || (cp >= 0xF900 && cp <= 0xFAFF) || (cp >= 0x2F800 && cp <= 0x2FA1F) ) } /** * Strips accents from the given text. * @param {string} text The text to strip accents from. * @returns {string} The text with accents removed. */ stripAccents(text) { return text.normalize('NFD').replace(/[\u0300-\u036f]/g, ''); } /** * Checks whether `char` is a control character. * @param {string} char The character to check. * @returns {boolean} Whether `char` is a control character. * @private */ _is_control(char) { switch (char) { case '\t': case '\n': case '\r': // These are technically control characters but we count them as whitespace characters. return false; default: // Check if unicode category starts with C: // Cc - Control // Cf - Format // Co - Private Use // Cs - Surrogate return /^\p{Cc}|\p{Cf}|\p{Co}|\p{Cs}$/u.test(char); } } /** * Performs invalid character removal and whitespace cleanup on text. * @param {string} text The text to clean. * @returns {string} The cleaned text. * @private */ _clean_text(text) { const output = []; for (const char of text) { const cp = char.charCodeAt(0); if (cp === 0 || cp === 0xFFFD || this._is_control(char)) { continue; } if (/^\s$/.test(char)) { // is whitespace output.push(" "); } else { output.push(char); } } return output.join(""); } /** * Normalizes the given text based on the configuration. * @param {string} text The text to normalize. * @returns {string} The normalized text. */ normalize(text) { if (this.config.clean_text) { text = this._clean_text(text); } if (this.config.handle_chinese_chars) { text = this._tokenize_chinese_chars(text); } if (this.config.lowercase) { text = text.toLowerCase(); if (this.config.strip_accents !== false) { text = this.stripAccents(text); } } else if (this.config.strip_accents) { text = this.stripAccents(text); } return text; } } /** * A callable class representing a pre-tokenizer used in tokenization. Subclasses * should implement the `pre_tokenize_text` method to define the specific pre-tokenization logic. * @extends Callable */ class PreTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * Factory method that returns an instance of a subclass of `PreTokenizer` based on the provided configuration. * * @static * @param {Object} config A configuration object for the pre-tokenizer. * @returns {PreTokenizer} An instance of a subclass of `PreTokenizer`. * @throws {Error} If the provided configuration object does not correspond to any known pre-tokenizer. */ static fromConfig(config) { if (config === null) return null; switch (config.type) { case 'BertPreTokenizer': return new BertPreTokenizer(config); case 'Sequence': return new PreTokenizerSequence(config); case 'Whitespace': return new WhitespacePreTokenizer(config); case 'WhitespaceSplit': return new WhitespaceSplit(config); case 'Metaspace': return new MetaspacePreTokenizer(config); case 'ByteLevel': return new ByteLevelPreTokenizer(config); case 'Split': return new SplitPreTokenizer(config); case 'Punctuation': return new PunctuationPreTokenizer(config); case 'Digits': return new DigitsPreTokenizer(config); case 'Replace': return new ReplacePreTokenizer(config); default: throw new Error(`Unknown PreTokenizer type: ${config.type}`); } } /** * Method that should be implemented by subclasses to define the specific pre-tokenization logic. * * @abstract * @param {string} text The text to pre-tokenize. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} The pre-tokenized text. * @throws {Error} If the method is not implemented in the subclass. */ pre_tokenize_text(text, options) { throw Error("pre_tokenize_text should be implemented in subclass.") } /** * Tokenizes the given text into pre-tokens. * @param {string|string[]} text The text or array of texts to pre-tokenize. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} An array of pre-tokens. */ pre_tokenize(text, options) { return (Array.isArray(text) ? text.map(x => this.pre_tokenize_text(x, options)) : this.pre_tokenize_text(text, options) ).flat(); } /** * Alias for {@link PreTokenizer#pre_tokenize}. * @param {string|string[]} text The text or array of texts to pre-tokenize. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} An array of pre-tokens. */ _call(text, options) { return this.pre_tokenize(text, options); } } /** * @extends PreTokenizer */ class BertPreTokenizer extends PreTokenizer { /** * A PreTokenizer that splits text into wordpieces using a basic tokenization scheme * similar to that used in the original implementation of BERT. * * @param {Object} config The configuration object. */ constructor(config) { super(); // Construct a pattern which matches the rust implementation: // https://github.com/huggingface/tokenizers/blob/b4fcc9ce6e4ad5806e82826f816acfdfdc4fcc67/tokenizers/src/pre_tokenizers/bert.rs#L11 // Equivalent to removing whitespace and splitting on punctuation (both \p{P} and other ascii characters) this.pattern = new RegExp(`[^\\s${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]`, 'gu'); } /** * Tokenizes a single text using the BERT pre-tokenization scheme. * * @param {string} text The text to tokenize. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} An array of tokens. */ pre_tokenize_text(text, options) { return text.trim().match(this.pattern) || []; } } /** * A pre-tokenizer that splits text into Byte-Pair-Encoding (BPE) subwords. * @extends PreTokenizer */ class ByteLevelPreTokenizer extends PreTokenizer { /** * Creates a new instance of the `ByteLevelPreTokenizer` class. * @param {Object} config The configuration object. */ constructor(config) { super(); this.config = config; /** * @type {boolean} Whether to add a leading space to the first word. * This allows to treat the leading word just as any other word. */ this.add_prefix_space = this.config.add_prefix_space; /** * @type {boolean} Whether the post processing step should trim offsets * to avoid including whitespaces. * @todo Use this in the pretokenization step. */ this.trim_offsets = this.config.trim_offsets; /** * @type {boolean} Whether to use the standard GPT2 regex for whitespace splitting. * Set it to False if you want to use your own splitting. Defaults to true. */ this.use_regex = this.config.use_regex ?? true; this.pattern = /'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu; this.byte_encoder = BYTES_TO_UNICODE; this.text_encoder = new TextEncoder(); } /** * Tokenizes a single piece of text using byte-level tokenization. * @param {string} text The text to tokenize. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} An array of tokens. */ pre_tokenize_text(text, options) { // Add a leading space if the option is enabled if (this.add_prefix_space && !text.startsWith(' ')) { text = ' ' + text; } // Split on whitespace and punctuation const tokens = this.use_regex ? (text.match(this.pattern) || []) : [text]; // Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) return tokens.map( token => Array.from(this.text_encoder.encode(token), byte => this.byte_encoder[byte]).join('') ); } } /** * @typedef {'removed'|'isolated'|'mergedWithPrevious'|'mergedWithNext'|'contiguous'} SplitDelimiterBehavior */ /** * Splits text using a given pattern. * @extends PreTokenizer */ class SplitPreTokenizer extends PreTokenizer { /** * @param {Object} config The configuration options for the pre-tokenizer. * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. * @param {string|undefined} config.pattern.String The string to use for splitting. Only defined if the pattern is a string. * @param {string|undefined} config.pattern.Regex The regex to use for splitting. Only defined if the pattern is a regex. * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. * @param {boolean} config.invert Whether to split (invert=false) or match (invert=true) the pattern. */ constructor(config) { super(); this.config = config; // TODO support all behaviours (config.behavior) this.pattern = createPattern(this.config.pattern, this.config.invert); } /** * Tokenizes text by splitting it using the given pattern. * @param {string} text The text to tokenize. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} An array of tokens. */ pre_tokenize_text(text, options) { if (this.pattern === null) { return []; } if (this.config.invert) { return text.match(this.pattern) || []; } else { return regexSplit(text, this.pattern); } } } /** * Splits text based on punctuation. * @extends PreTokenizer */ class PunctuationPreTokenizer extends PreTokenizer { /** * @param {Object} config The configuration options for the pre-tokenizer. * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. */ constructor(config) { super(); this.config = config; this.pattern = new RegExp(`[^${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]+`, 'gu'); } /** * Tokenizes text by splitting it using the given pattern. * @param {string} text The text to tokenize. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} An array of tokens. */ pre_tokenize_text(text, options) { return text.match(this.pattern) || []; } } /** * Splits text based on digits. * @extends PreTokenizer */ class DigitsPreTokenizer extends PreTokenizer { /** * @param {Object} config The configuration options for the pre-tokenizer. * @param {boolean} config.individual_digits Whether to split on individual digits. */ constructor(config) { super(); this.config = config; // Construct a pattern which matches the rust implementation: const digit_pattern = `[^\\d]+|\\d${this.config.individual_digits ? '' : '+'}`; this.pattern = new RegExp(digit_pattern, 'gu'); } /** * Tokenizes text by splitting it using the given pattern. * @param {string} text The text to tokenize. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} An array of tokens. */ pre_tokenize_text(text, options) { return text.match(this.pattern) || []; } } /** * @typedef {Object} PostProcessedOutput * @property {string[]} tokens List of token produced by the post-processor. * @property {number[]} [token_type_ids] List of token type ids produced by the post-processor. */ /** * @typedef {Object} EncodingSingle * @property {number[]} input_ids List of token ids to be fed to a model. * @property {number[]} attention_mask List of token type ids to be fed to a model * @property {number[]} [token_type_ids] List of indices specifying which tokens should be attended to by the model */ /** * @extends Callable */ class PostProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * @param {Object} config The configuration for the post-processor. */ constructor(config) { super(); this.config = config; } /** * Factory method to create a PostProcessor object from a configuration object. * * @param {Object} config Configuration object representing a PostProcessor. * @returns {PostProcessor} A PostProcessor object created from the given configuration. * @throws {Error} If an unknown PostProcessor type is encountered. */ static fromConfig(config) { if (config === null) return null; switch (config.type) { case 'TemplateProcessing': return new TemplateProcessing(config); case 'ByteLevel': return new ByteLevelPostProcessor(config); case 'RobertaProcessing': return new RobertaProcessing(config); case 'BertProcessing': return new BertProcessing(config); default: throw new Error(`Unknown PostProcessor type: ${config.type}`); } } /** * Method to be implemented in subclass to apply post-processing on the given tokens. * * @param {Array} tokens The input tokens to be post-processed. * @param {...*} args Additional arguments required by the post-processing logic. * @returns {PostProcessedOutput} The post-processed tokens. * @throws {Error} If the method is not implemented in subclass. */ post_process(tokens, ...args) { throw Error("post_process should be implemented in subclass.") } /** * Alias for {@link PostProcessor#post_process}. * @param {Array} tokens The text or array of texts to post-process. * @param {...*} args Additional arguments required by the post-processing logic. * @returns {PostProcessedOutput} The post-processed tokens. */ _call(tokens, ...args) { return this.post_process(tokens, ...args); } } /** * A post-processor that adds special tokens to the beginning and end of the input. */ class BertProcessing extends PostProcessor { /** * @param {Object} config The configuration for the post-processor. * @param {string[]} config.cls The special tokens to add to the beginning of the input. * @param {string[]} config.sep The special tokens to add to the end of the input. */ constructor(config) { super(config); // TODO use all of config: add_prefix_space, trim_offsets this.cls = config.cls[0]; this.sep = config.sep[0]; } /** * Adds the special tokens to the beginning and end of the input. * @param {string[]} tokens The input tokens. * @param {string[]} [tokens_pair=null] An optional second set of input tokens. * @returns {PostProcessedOutput} The post-processed tokens with the special tokens added to the beginning and end. */ post_process(tokens, tokens_pair = null, { add_special_tokens = true, } = {}) { if (add_special_tokens) { tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([this.cls], tokens, [this.sep]); } let token_type_ids = new Array(tokens.length).fill(0); if (tokens_pair !== null) { // NOTE: It is intended to add 2 EOS tokens after the first set of tokens // https://github.com/huggingface/tokenizers/issues/983 const middle = (add_special_tokens && this instanceof RobertaProcessing) ? [this.sep] : []; const after = add_special_tokens ? [this.sep] : []; tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, middle, tokens_pair, after); token_type_ids = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(token_type_ids, new Array(tokens_pair.length + middle.length + after.length).fill(1)); } return { tokens, token_type_ids }; } } class RobertaProcessing extends BertProcessing { } // NOTE: extends BertProcessing /** * Post processor that replaces special tokens in a template with actual tokens. * @extends PostProcessor */ class TemplateProcessing extends PostProcessor { /** * Creates a new instance of `TemplateProcessing`. * @param {Object} config The configuration options for the post processor. * @param {Array} config.single The template for a single sequence of tokens. * @param {Array} config.pair The template for a pair of sequences of tokens. */ constructor(config) { super(config); this.single = config.single; this.pair = config.pair; } /** * Replaces special tokens in the template with actual tokens. * @param {string[]} tokens The list of tokens for the first sequence. * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). * @returns {PostProcessedOutput} An object containing the list of tokens with the special tokens replaced with actual tokens. */ post_process(tokens, tokens_pair = null, { add_special_tokens = true, } = {}) { const type = tokens_pair === null ? this.single : this.pair let processedTokens = []; let types = []; for (const item of type) { if ('SpecialToken' in item) { if (add_special_tokens) { processedTokens.push(item.SpecialToken.id); types.push(item.SpecialToken.type_id); } } else if ('Sequence' in item) { if (item.Sequence.id === 'A') { processedTokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens); types = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens.length).fill(item.Sequence.type_id)); } else if (item.Sequence.id === 'B') { processedTokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens_pair); types = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens_pair.length).fill(item.Sequence.type_id)); } } } return { tokens: processedTokens, token_type_ids: types }; } } /** * A PostProcessor that returns the given tokens as is. * @extends PostProcessor */ class ByteLevelPostProcessor extends PostProcessor { /** * Post process the given tokens. * @param {string[]} tokens The list of tokens for the first sequence. * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). * @returns {PostProcessedOutput} An object containing the post-processed tokens. */ post_process(tokens, tokens_pair = null) { if (tokens_pair) { tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, tokens_pair); } return { tokens }; } } /** * The base class for token decoders. * @extends Callable */ class Decoder extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { /** * Creates an instance of `Decoder`. * * @param {Object} config The configuration object. */ constructor(config) { super(); this.config = config; /** @type {AddedToken[]} */ this.added_tokens = []; this.end_of_word_suffix = null; this.trim_offsets = config.trim_offsets; } /** * Creates a decoder instance based on the provided configuration. * * @param {Object} config The configuration object. * @returns {Decoder} A decoder instance. * @throws {Error} If an unknown decoder type is provided. */ static fromConfig(config) { if (config === null) return null; switch (config.type) { case 'WordPiece': return new WordPieceDecoder(config); case 'Metaspace': return new MetaspaceDecoder(config); case 'ByteLevel': return new ByteLevelDecoder(config); case 'Replace': return new ReplaceDecoder(config); case 'ByteFallback': return new ByteFallback(config); case 'Fuse': return new FuseDecoder(config); case 'Strip': return new StripDecoder(config); case 'Sequence': return new DecoderSequence(config); case 'CTC': return new CTCDecoder(config); case 'BPEDecoder': return new BPEDecoder(config); default: throw new Error(`Unknown Decoder type: ${config.type}`); } } /** * Calls the `decode` method. * * @param {string[]} tokens The list of tokens. * @returns {string} The decoded string. */ _call(tokens) { return this.decode(tokens); } /** * Decodes a list of tokens. * @param {string[]} tokens The list of tokens. * @returns {string} The decoded string. */ decode(tokens) { return this.decode_chain(tokens).join(''); } /** * Apply the decoder to a list of tokens. * * @param {string[]} tokens The list of tokens. * @returns {string[]} The decoded list of tokens. * @throws {Error} If the `decode_chain` method is not implemented in the subclass. */ decode_chain(tokens) { throw Error("`decode_chain` should be implemented in subclass.") } } class ReplaceDecoder extends Decoder { /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { const pattern = createPattern(this.config.pattern); return pattern === null ? tokens : tokens.map(token => token.replaceAll(pattern, this.config.content)) } } class ByteFallback extends Decoder { constructor(config) { super(config); this.text_decoder = new TextDecoder(); } /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { const new_tokens = []; let previous_byte_tokens = []; for (const token of tokens) { let bytes = null; if (token.length === 6 && token.startsWith('<0x') && token.endsWith('>')) { const byte = parseInt(token.slice(3, 5), 16); if (!isNaN(byte)) { bytes = byte; } } if (bytes !== null) { previous_byte_tokens.push(bytes); } else { if (previous_byte_tokens.length > 0) { const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); new_tokens.push(string); previous_byte_tokens = []; } new_tokens.push(token); } } if (previous_byte_tokens.length > 0) { const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); new_tokens.push(string); previous_byte_tokens = []; } return new_tokens; } } /** * Fuse simply fuses all tokens into one big string. * It's usually the last decoding step anyway, but this decoder * exists incase some decoders need to happen after that step */ class FuseDecoder extends Decoder { /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { return [tokens.join('')]; } } class StripDecoder extends Decoder { constructor(config) { super(config); this.content = this.config.content; this.start = this.config.start; this.stop = this.config.stop; } /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { return tokens.map(token => { let start_cut = 0; for (let i = 0; i < this.start; ++i) { if (token[i] === this.content) { start_cut = i + 1; continue; } else { break; } } let stop_cut = token.length; for (let i = 0; i < this.stop; ++i) { const index = token.length - i - 1; if (token[index] === this.content) { stop_cut = index; continue; } else { break; } } return token.slice(start_cut, stop_cut) }); } } /** * A decoder that decodes a list of WordPiece tokens into a single string. * @extends Decoder */ class WordPieceDecoder extends Decoder { /** * Creates a new instance of WordPieceDecoder. * @param {Object} config The configuration object. * @param {string} config.prefix The prefix used for WordPiece encoding. * @param {boolean} config.cleanup Whether to cleanup the decoded string. */ constructor(config) { super(config); this.cleanup = config.cleanup; } /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { return tokens.map((token, i) => { if (i !== 0) { if (token.startsWith(this.config.prefix)) { // NOTE: .replace() is intended; only replace first occurrence token = token.replace(this.config.prefix, ''); } else { token = ' ' + token; } } if (this.cleanup) { token = clean_up_tokenization(token) } return token; }); } } /** * Byte-level decoder for tokenization output. Inherits from the `Decoder` class. * @extends Decoder */ class ByteLevelDecoder extends Decoder { /** * Create a `ByteLevelDecoder` object. * @param {Object} config Configuration object. */ constructor(config) { super(config); this.byte_decoder = UNICODE_TO_BYTES; this.text_decoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true, }); this.end_of_word_suffix = null; } /** * Convert an array of tokens to string by decoding each byte. * @param {string[]} tokens Array of tokens to be decoded. * @returns {string} The decoded string. */ convert_tokens_to_string(tokens) { const text = tokens.join(''); const byteArray = new Uint8Array([...text].map(c => this.byte_decoder[c])); const decoded_text = this.text_decoder.decode(byteArray); return decoded_text; } /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { // TODO move to base class (like HF) // tokens === filtered_tokens // To avoid mixing byte-level and unicode for byte-level BPT // we need to build string separately for added tokens and byte-level tokens // cf. https://github.com/huggingface/transformers/issues/1133 const sub_texts = []; let current_sub_text = []; for (const token of tokens) { // tokens sent here are already filtered, so we don't need to do this // if (skip_special_tokens && this.all_special_ids.includes(token)) { // continue; // } if (this.added_tokens.find(x => x.content === token) !== undefined) { if (current_sub_text.length > 0) { sub_texts.push(this.convert_tokens_to_string(current_sub_text)); current_sub_text = []; } sub_texts.push(token); } else { current_sub_text.push(token); } } if (current_sub_text.length > 0) { sub_texts.push(this.convert_tokens_to_string(current_sub_text)); } // TODO add spaces_between_special_tokens and clean_up_tokenization_spaces options return sub_texts; } } /** * The CTC (Connectionist Temporal Classification) decoder. * See https://github.com/huggingface/tokenizers/blob/bb38f390a61883fc2f29d659af696f428d1cda6b/tokenizers/src/decoders/ctc.rs */ class CTCDecoder extends Decoder { constructor(config) { super(config); this.pad_token = this.config.pad_token; this.word_delimiter_token = this.config.word_delimiter_token; this.cleanup = this.config.cleanup; } /** * Converts a connectionist-temporal-classification (CTC) output tokens into a single string. * @param {string[]} tokens Array of tokens to be decoded. * @returns {string} The decoded string. */ convert_tokens_to_string(tokens) { if (tokens.length === 0) return ''; // group same tokens into non-repeating tokens in CTC style decoding const grouped_tokens = [tokens[0]]; for (let i = 1; i < tokens.length; ++i) { if (tokens[i] !== grouped_tokens.at(-1)) { grouped_tokens.push(tokens[i]); } } // filter self.pad_token which is used as CTC-blank token const filtered_tokens = grouped_tokens.filter(token => token !== this.pad_token); let text = filtered_tokens.join(''); if (this.cleanup) { // cleanup and replace delimiter token text = clean_up_tokenization(text) .replaceAll(this.word_delimiter_token, ' ') .trim(); } return text; } /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { return [this.convert_tokens_to_string(tokens)]; } } /** * Apply a sequence of decoders. * @extends Decoder */ class DecoderSequence extends Decoder { /** * Creates a new instance of DecoderSequence. * @param {Object} config The configuration object. * @param {Decoder[]} config.decoders The list of decoders to apply. */ constructor(config) { super(config); this.decoders = config.decoders.map(x => Decoder.fromConfig(x)); } /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { // Use reduce to apply each decoder to the tokens return this.decoders.reduce((toks, decoder) => { return decoder.decode_chain(toks); }, tokens); } } class BPEDecoder extends Decoder { constructor(config) { super(config); this.suffix = this.config.suffix; } /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { return tokens.map((token, i) => { return token.replaceAll(this.suffix, (i === tokens.length - 1) ? '' : ' ') }); } } // Custom decoder for VITS class VitsDecoder extends Decoder { /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { let decoded = ''; for (let i = 1; i < tokens.length; i += 2) { decoded += tokens[i]; } return [decoded]; } } /** * This PreTokenizer replaces spaces with the given replacement character, adds a prefix space if requested, * and returns a list of tokens. * @extends PreTokenizer */ class MetaspacePreTokenizer extends PreTokenizer { /** * @param {Object} config The configuration object for the MetaspacePreTokenizer. * @param {boolean} config.add_prefix_space Whether to add a prefix space to the first token. * @param {string} config.replacement The character to replace spaces with. * @param {string} [config.str_rep=config.replacement] An optional string representation of the replacement character. * @param {'first'|'never'|'always'} [config.prepend_scheme='always'] The metaspace prepending scheme. */ constructor(config) { super(); this.addPrefixSpace = config.add_prefix_space; this.replacement = config.replacement; this.strRep = config.str_rep || this.replacement; this.prepend_scheme = config.prepend_scheme ?? 'always'; } /** * This method takes a string, replaces spaces with the replacement character, * adds a prefix space if requested, and returns a new list of tokens. * @param {string} text The text to pre-tokenize. * @param {Object} [options] The options for the pre-tokenization. * @param {number} [options.section_index] The index of the section to pre-tokenize. * @returns {string[]} A new list of pre-tokenized tokens. */ pre_tokenize_text(text, { section_index = undefined, } = {}) { let normalized = text.replaceAll(' ', this.strRep); if ( // We add a prefix space if: // (1) The addPrefixSpace option is enabled and the normalized // token does not already start with the replacement character. (this.addPrefixSpace && !normalized.startsWith(this.replacement)) // and (2) either: // (a) prepend_scheme is 'always' // (b) prepend_scheme is 'first' and this is the first section && ( this.prepend_scheme === 'always' || (this.prepend_scheme === 'first' && section_index === 0) ) ) { normalized = this.strRep + normalized; } return [normalized]; } } /** * MetaspaceDecoder class extends the Decoder class and decodes Metaspace tokenization. * @extends Decoder */ class MetaspaceDecoder extends Decoder { /** * Constructs a new MetaspaceDecoder object. * @param {Object} config The configuration object for the MetaspaceDecoder. * @param {boolean} config.add_prefix_space Whether to add a prefix space to the decoded string. * @param {string} config.replacement The string to replace spaces with. */ constructor(config) { super(config); this.addPrefixSpace = config.add_prefix_space; this.replacement = config.replacement; } /** @type {Decoder['decode_chain']} */ decode_chain(tokens) { const result = []; for (let i = 0; i < tokens.length; ++i) { let normalized = tokens[i].replaceAll(this.replacement, ' '); if (this.addPrefixSpace && i == 0 && normalized.startsWith(' ')) { normalized = normalized.substring(1); } result.push(normalized); } return result; } } /** * A normalizer that applies a precompiled charsmap. * This is useful for applying complex normalizations in C++ and exposing them to JavaScript. * @extends Normalizer * @param {Object} config The configuration object for the Precompiled normalizer. * @param {Object} config.precompiled_charsmap The precompiled charsmap object. */ class Precompiled extends Normalizer { /** * Create a new instance of Precompiled normalizer. * @param {Object} config The configuration object. * @param {any} config.precompiled_charsmap Precompiled chars mapping. */ constructor(config) { super(config); this.charsmap = config.precompiled_charsmap; } /** * Normalizes the given text by applying the precompiled charsmap. * @param {string} text The text to normalize. * @returns {string} The normalized text. */ normalize(text) { // As stated in the sentencepiece normalization docs (https://github.com/google/sentencepiece/blob/master/doc/normalization.md#use-pre-defined-normalization-rule), // there are 5 pre-defined normalization rules: // 1. nmt_nfkc: NFKC normalization with some additional normalization around spaces. (default) // 2. nfkc: original NFKC normalization. // 3. nmt_nfkc_cf: nmt_nfkc + Unicode case folding (mostly lower casing) // 4. nfkc_cf: nfkc + Unicode case folding. // 5. identity: no normalization // // For now, we only implement the default (nmt_nfkc). // See https://raw.githubusercontent.com/google/sentencepiece/master/data/nmt_nfkc.tsv for the full list of rules. // TODO: detect when a different `this.charsmap` is used. text = text.replace(/[\u0001-\u0008\u000B\u000E-\u001F\u007F\u008F\u009F]/gm, ''); // Remove control characters text = text.replace(/[\u0009\u000A\u000C\u000D\u1680\u200B\u200C\u200E\u200F\u2028\u2029\u2581\uFEFF\uFFFD]/gm, '\u0020'); // Replace certain characters with a space if (text.includes('\uFF5E')) { // To match the sentencepiece implementation 100%, we must handle a very strange edge-case. // For some reason, the "Fullwidth Tilde" character (\uFF5E) should not be converted to the standard Tilde character (\u007E). // However, NFKC normalization does do this conversion. As a result, we split the string on the Fullwidth Tilde character, // perform NFKC normalization on each substring, and then join them back together with the Fullwidth Tilde character. const parts = text.split('\uFF5E'); text = parts.map(part => part.normalize('NFKC')).join('\uFF5E'); } else { text = text.normalize('NFKC'); } return text; } } /** * A pre-tokenizer that applies a sequence of pre-tokenizers to the input text. * @extends PreTokenizer */ class PreTokenizerSequence extends PreTokenizer { /** * Creates an instance of PreTokenizerSequence. * @param {Object} config The configuration object for the pre-tokenizer sequence. * @param {Object[]} config.pretokenizers An array of pre-tokenizer configurations. */ constructor(config) { super(); this.tokenizers = config.pretokenizers.map(x => PreTokenizer.fromConfig(x)); } /** * Applies each pre-tokenizer in the sequence to the input text in turn. * @param {string} text The text to pre-tokenize. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} The pre-tokenized text. */ pre_tokenize_text(text, options) { // Use reduce to apply each tokenizer to the text return this.tokenizers.reduce((preTokenizedText, tokenizer) => { return tokenizer.pre_tokenize(preTokenizedText, options); }, [text]); } } /** * Splits on word boundaries (using the following regular expression: `\w+|[^\w\s]+`). */ class WhitespacePreTokenizer extends PreTokenizer { /** * Creates an instance of WhitespacePreTokenizer. * @param {Object} config The configuration object for the pre-tokenizer. */ constructor(config) { super(); } /** * Pre-tokenizes the input text by splitting it on word boundaries. * @param {string} text The text to be pre-tokenized. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. */ pre_tokenize_text(text, options) { return text.match(/\w+|[^\w\s]+/g) || []; } } /** * Splits a string of text by whitespace characters into individual tokens. * @extends PreTokenizer */ class WhitespaceSplit extends PreTokenizer { /** * Creates an instance of WhitespaceSplit. * @param {Object} config The configuration object for the pre-tokenizer. */ constructor(config) { super(); } /** * Pre-tokenizes the input text by splitting it on whitespace characters. * @param {string} text The text to be pre-tokenized. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. */ pre_tokenize_text(text, options) { return whitespace_split(text); } } // NOTE: `ReplacePreTokenizer` is custom (to support `BlenderbotSmallTokenizer`) class ReplacePreTokenizer extends PreTokenizer { /** * @param {Object} config The configuration options for the pre-tokenizer. * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. * @param {string} config.content What to replace the pattern with. */ constructor(config) { super(); this.config = config; this.pattern = createPattern(this.config.pattern); this.content = this.config.content; } /** * Pre-tokenizes the input text by replacing certain characters. * @param {string} text The text to be pre-tokenized. * @param {Object} [options] Additional options for the pre-tokenization logic. * @returns {string[]} An array of tokens produced by replacing certain characters. */ pre_tokenize_text(text, options) { if (this.pattern === null) { return [text]; } return [text.replaceAll(this.pattern, this.config.content)]; } } const SPECIAL_TOKEN_ATTRIBUTES = [ 'bos_token', 'eos_token', 'unk_token', 'sep_token', 'pad_token', 'cls_token', 'mask_token', // additional_special_tokens (TODO) ] /** * * Helper function for padding values of an object, which are each arrays. * NOTE: No additional checks are made here for validity of arguments. * @param {Record} item The input object. * @param {number} length The length to pad to. * @param {(key: string) => any} value_fn Determine the value to fill the array, based on its key. * @param {string} side Which side to pad the array. * @private */ function padHelper(item, length, value_fn, side) { for (const key of Object.keys(item)) { const diff = length - item[key].length; const value = value_fn(key); const padData = new Array(diff).fill(value); item[key] = side === 'right' ? (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(item[key], padData) : (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(padData, item[key]); } } /** * Helper function for truncating values of an object, which are each arrays. * NOTE: No additional checks are made here for validity of arguments. * @param {Record} item The input object. * @param {number} length The length to truncate to. * @private */ function truncateHelper(item, length) { // Setting .length to a lower value truncates the array in-place: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length for (const key of Object.keys(item)) { item[key].length = length; } } /** * @typedef {Object} Message * @property {string} role The role of the message (e.g., "user" or "assistant" or "system"). * @property {string} content The content of the message. */ class PreTrainedTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { return_token_type_ids = false; _default_chat_template = `{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}`; padding_side = 'right'; /** * Create a new PreTrainedTokenizer instance. * @param {Object} tokenizerJSON The JSON of the tokenizer. * @param {Object} tokenizerConfig The config of the tokenizer. */ constructor(tokenizerJSON, tokenizerConfig) { super(); this._tokenizer_config = tokenizerConfig; // Construct parts of the tokenizer from the JSON this.normalizer = Normalizer.fromConfig(tokenizerJSON.normalizer); this.pre_tokenizer = PreTokenizer.fromConfig(tokenizerJSON.pre_tokenizer); this.model = TokenizerModel.fromConfig(tokenizerJSON.model, tokenizerConfig); this.post_processor = PostProcessor.fromConfig(tokenizerJSON.post_processor); this.decoder = Decoder.fromConfig(tokenizerJSON.decoder); // Add added_tokens to model this.special_tokens = []; this.all_special_ids = []; /** @type {AddedToken[]} */ this.added_tokens = []; for (const addedToken of tokenizerJSON.added_tokens) { const token = new AddedToken(addedToken); this.added_tokens.push(token); this.model.tokens_to_ids.set(token.content, token.id); this.model.vocab[token.id] = token.content; if (token.special) { this.special_tokens.push(token.content); this.all_special_ids.push(token.id); } } // Update additional_special_tokens this.additional_special_tokens = tokenizerConfig.additional_special_tokens ?? []; this.special_tokens.push(...this.additional_special_tokens); this.special_tokens = [...new Set(this.special_tokens)]; // Remove duplicates if (this.decoder) { // Slight hack, but it prevents code duplication: this.decoder.added_tokens = this.added_tokens; // Another slight hack to add `end_of_word_suffix` (if present) to the decoder // This is needed for cases where BPE model and ByteLevel decoder are used // For more information, see https://github.com/xenova/transformers.js/issues/74 // TODO: save this to the decoder when exporting? this.decoder.end_of_word_suffix = this.model.end_of_word_suffix; } this.added_tokens_regex = this.added_tokens.length > 0 ? new RegExp( this.added_tokens.map(x => `${x.lstrip ? '\\s*' : ''}(${(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(x.content)})${x.rstrip ? '\\s*' : ''}`).join('|') ) : null; // Set mask token if present (otherwise will be undefined, which is fine) this.mask_token = this.getToken('mask_token'); this.mask_token_id = this.model.tokens_to_ids.get(this.mask_token); this.pad_token = this.getToken('pad_token', 'eos_token'); this.pad_token_id = this.model.tokens_to_ids.get(this.pad_token); this.sep_token = this.getToken('sep_token'); this.sep_token_id = this.model.tokens_to_ids.get(this.sep_token); this.unk_token = this.getToken('unk_token'); this.unk_token_id = this.model.tokens_to_ids.get(this.unk_token); this.model_max_length = tokenizerConfig.model_max_length; /** @type {boolean} Whether or not to strip the text when tokenizing (removing excess spaces before and after the string). */ this.remove_space = tokenizerConfig.remove_space; this.clean_up_tokenization_spaces = tokenizerConfig.clean_up_tokenization_spaces ?? true; this.do_lowercase_and_remove_accent = tokenizerConfig.do_lowercase_and_remove_accent ?? false; if (tokenizerConfig.padding_side) { this.padding_side = tokenizerConfig.padding_side; } this.legacy = false; this.chat_template = tokenizerConfig.chat_template ?? null; if (Array.isArray(this.chat_template)) { // Chat templates are stored as lists of dicts with fixed key names, // we reconstruct that into a single dict while loading them. const chat_template = Object.create(null); for (const { name, template } of this.chat_template) { if (typeof name !== 'string' || typeof template !== 'string') { throw new Error('Chat template must be a list of objects with "name" and "template" properties'); } chat_template[name] = template; } this.chat_template = chat_template; } this._compiled_template_cache = new Map(); } /** * Returns the value of the first matching key in the tokenizer config object. * @param {...string} keys One or more keys to search for in the tokenizer config object. * @returns {string|null} The value associated with the first matching key, or null if no match is found. * @throws {Error} If an object is found for a matching key and its __type property is not "AddedToken". * @private */ getToken(...keys) { for (const key of keys) { const item = this._tokenizer_config[key]; if (!item) continue; if (typeof item === 'object') { if (item.__type === 'AddedToken') { return item.content; } else { throw Error(`Unknown token: ${item}`); } } else { return item; } } return null; } /** * Loads a pre-trained tokenizer from the given `pretrained_model_name_or_path`. * * @param {string} pretrained_model_name_or_path The path to the pre-trained tokenizer. * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. * * @throws {Error} Throws an error if the tokenizer.json or tokenizer_config.json files are not found in the `pretrained_model_name_or_path`. * @returns {Promise} A new instance of the `PreTrainedTokenizer` class. */ static async from_pretrained(pretrained_model_name_or_path, { progress_callback = null, config = null, cache_dir = null, local_files_only = false, revision = 'main', legacy = null, } = {}) { const info = await loadTokenizer(pretrained_model_name_or_path, { progress_callback, config, cache_dir, local_files_only, revision, legacy, }) // @ts-ignore return new this(...info); } /** * @typedef {number[]|number[][]|Tensor} BatchEncodingItem * * @typedef {Object} BatchEncoding Holds the output of the tokenizer's call function. * @property {BatchEncodingItem} input_ids List of token ids to be fed to a model. * @property {BatchEncodingItem} attention_mask List of indices specifying which tokens should be attended to by the model. * @property {BatchEncodingItem} [token_type_ids] List of token type ids to be fed to a model. */ /** * Encode/tokenize the given text(s). * @param {string|string[]} text The text to tokenize. * @param {Object} options An optional object containing the following properties: * @param {string|string[]} [options.text_pair=null] Optional second sequence to be encoded. If set, must be the same type as text. * @param {boolean|'max_length'} [options.padding=false] Whether to pad the input sequences. * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. * @param {boolean} [options.truncation=null] Whether to truncate the input sequences. * @param {number} [options.max_length=null] Maximum length of the returned list and optionally padding length. * @param {boolean} [options.return_tensor=true] Whether to return the results as Tensors or arrays. * @returns {BatchEncoding} Object to be passed to the model. */ _call( // Required positional arguments text, // Optional keyword arguments { text_pair = null, add_special_tokens = true, padding = false, truncation = null, max_length = null, return_tensor = true, // Different to HF } = {}, ) { const isBatched = Array.isArray(text); /** @type {EncodingSingle[]} */ let encodedTokens; if (isBatched) { if (text.length === 0) { throw Error('text array must be non-empty') } if (text_pair !== null) { if (!Array.isArray(text_pair)) { throw Error('text_pair must also be an array') } else if (text.length !== text_pair.length) { throw Error('text and text_pair must have the same length') } encodedTokens = text.map( (t, i) => this._encode_plus(t, { text_pair: text_pair[i], add_special_tokens }) ) } else { encodedTokens = text.map(x => this._encode_plus(x, { add_special_tokens })); } } else { if (text === null || text === undefined) { throw Error('text may not be null or undefined') } if (Array.isArray(text_pair)) { throw Error('When specifying `text_pair`, since `text` is a string, `text_pair` must also be a string (i.e., not an array).') } // For single input, we just wrap in an array, and then unwrap later. encodedTokens = [this._encode_plus(text, { text_pair, add_special_tokens })]; } // At this point, tokens is batched: [batch_size, tokens] // However, array may be jagged. So, we pad to max_length if (max_length === null) { if (padding === 'max_length') { max_length = this.model_max_length; } else { // Calculate max length from sequences max_length = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(encodedTokens.map(x => x.input_ids.length))[0]; } } else { if (!truncation) { console.warn(`Truncation was not explicitly activated but \`max_length\` is provided a specific value, please use \`truncation=true\` to explicitly truncate examples to max length.`) } } // Ensure it is less than model max length max_length = Math.min(max_length, this.model_max_length) if (padding || truncation) { // Perform padding and/or truncation for (let i = 0; i < encodedTokens.length; ++i) { if (encodedTokens[i].input_ids.length === max_length) { continue; } else if (encodedTokens[i].input_ids.length > max_length) { // possibly truncate if (truncation) { truncateHelper(encodedTokens[i], max_length); } } else { // t.length < max_length // possibly pad if (padding) { padHelper( encodedTokens[i], max_length, key => key === 'input_ids' ? this.pad_token_id : 0, this.padding_side ); } } } } const result = {}; if (return_tensor) { if (!(padding && truncation)) { // Not, guaranteed that all items have same length, so // we perform additional check if ( encodedTokens.some(x => { for (const key of Object.keys(x)) { if (x[key].length !== encodedTokens[0][key]?.length) { return true; } } return false; }) ) { throw Error( "Unable to create tensor, you should probably activate truncation and/or padding " + "with 'padding=true' and 'truncation=true' to have batched tensors with the same length." ) } } // Now we actually convert to tensor // NOTE: In the same way as the python library, we return a batched tensor, regardless of // whether we have a single input or multiple inputs. const dims = [encodedTokens.length, encodedTokens[0].input_ids.length]; for (const key of Object.keys(encodedTokens[0])) { result[key] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('int64', BigInt64Array.from(encodedTokens.flatMap(x => x[key]).map(BigInt)), dims ); } } else { for (const key of Object.keys(encodedTokens[0])) { result[key] = encodedTokens.map(x => x[key]); } // If not returning a tensor, we match the input type if (!isBatched) { // Input was not batched, so we unwrap for (const key of Object.keys(result)) { result[key] = result[key][0]; } } } return /** @type {BatchEncoding} */(result); } /** * Encodes a single text using the preprocessor pipeline of the tokenizer. * * @param {string|null} text The text to encode. * @returns {string[]|null} The encoded tokens. */ _encode_text(text) { if (text === null) return null; // Actual function which does encoding, for a single text // First, we take care of special tokens. Needed to avoid issues arising from // normalization and/or pretokenization (which may not preserve special tokens) const sections = this.added_tokens_regex ? text.split(this.added_tokens_regex).filter(x => x) : [text]; const tokens = sections.map((x, section_index) => { const addedToken = this.added_tokens.find(t => t.content === x); if (addedToken !== undefined) { // Ignore added tokens return x } else { if (this.remove_space === true) { x = x.trim().split(/\s+/).join(' '); } if (this.do_lowercase_and_remove_accent) { x = lowercase_and_remove_accent(x); } if (this.normalizer !== null) { x = this.normalizer(x); } // If, after normalization, this section is empty (e.g., trimming whitespace), // we return an empty array if (x.length === 0) { return []; } const sectionTokens = (this.pre_tokenizer !== null) ? this.pre_tokenizer(x, { section_index, }) : [x]; const tokens = this.model(sectionTokens); return tokens; } }).flat(); return tokens; } /** * Encodes a single text or a pair of texts using the model's tokenizer. * * @param {string} text The text to encode. * @param {Object} options An optional object containing the following properties: * @param {string} [options.text_pair=null] The optional second text to encode. * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. * @returns {EncodingSingle} An object containing the encoded text. * @private */ _encode_plus(text, { text_pair = null, add_special_tokens = true, } = {}) { const { tokens, token_type_ids } = this._tokenize_helper(text, { pair: text_pair, add_special_tokens }); const input_ids = this.model.convert_tokens_to_ids(tokens); const result = { input_ids, attention_mask: new Array(input_ids.length).fill(1), } if (this.return_token_type_ids && token_type_ids) { result.token_type_ids = token_type_ids; } return result; } /** * Internal helper function to tokenize a text, and optionally a pair of texts. * @param {string} text The text to tokenize. * @param {Object} options An optional object containing the following properties: * @param {string} [options.pair=null] The optional second text to tokenize. * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. * @returns {{tokens: string[], token_type_ids?: number[]}} An object containing the tokens and optionally the token type IDs. */ _tokenize_helper(text, { pair = null, add_special_tokens = false, } = {}) { const tokens = this._encode_text(text); const tokens2 = this._encode_text(pair); return this.post_processor ? this.post_processor(tokens, tokens2, { add_special_tokens }) : { tokens: (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens ?? [], tokens2 ?? []) }; } /** * Converts a string into a sequence of tokens. * @param {string} text The sequence to be encoded. * @param {Object} options An optional object containing the following properties: * @param {string} [options.pair] A second sequence to be encoded with the first. * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. * @returns {string[]} The list of tokens. */ tokenize(text, { pair = null, add_special_tokens = false, } = {}) { return this._tokenize_helper(text, { pair, add_special_tokens }).tokens; } /** * Encodes a single text or a pair of texts using the model's tokenizer. * * @param {string} text The text to encode. * @param {Object} options An optional object containing the following properties: * @param {string} [options.text_pair=null] The optional second text to encode. * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. * @returns {number[]} An array of token IDs representing the encoded text(s). */ encode(text, { text_pair = null, add_special_tokens = true, } = {}) { return this._encode_plus(text, { text_pair, add_special_tokens, }).input_ids; } /** * Decode a batch of tokenized sequences. * @param {number[][]|Tensor} batch List/Tensor of tokenized input sequences. * @param {Object} decode_args (Optional) Object with decoding arguments. * @returns {string[]} List of decoded sequences. */ batch_decode(batch, decode_args = {}) { if (batch instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { batch = batch.tolist(); } return batch.map(x => this.decode(x, decode_args)); } /** * Decodes a sequence of token IDs back to a string. * * @param {number[]|Tensor} token_ids List/Tensor of token IDs to decode. * @param {Object} [decode_args={}] * @param {boolean} [decode_args.skip_special_tokens=false] If true, special tokens are removed from the output string. * @param {boolean} [decode_args.clean_up_tokenization_spaces=true] If true, spaces before punctuations and abbreviated forms are removed. * * @returns {string} The decoded string. * @throws {Error} If `token_ids` is not a non-empty array of integers. */ decode( token_ids, decode_args = {}, ) { if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { token_ids = prepareTensorForDecode(token_ids); } if (!Array.isArray(token_ids) || token_ids.length === 0 || !(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.isIntegralNumber)(token_ids[0])) { throw Error("token_ids must be a non-empty array of integers."); } return this.decode_single(token_ids, decode_args) } /** * Decode a single list of token ids to a string. * @param {number[]} token_ids List of token ids to decode * @param {Object} decode_args Optional arguments for decoding * @param {boolean} [decode_args.skip_special_tokens=false] Whether to skip special tokens during decoding * @param {boolean} [decode_args.clean_up_tokenization_spaces=null] Whether to clean up tokenization spaces during decoding. * If null, the value is set to `this.decoder.cleanup` if it exists, falling back to `this.clean_up_tokenization_spaces` if it exists, falling back to `true`. * @returns {string} The decoded string */ decode_single( token_ids, { skip_special_tokens = false, clean_up_tokenization_spaces = null, } ) { let tokens = this.model.convert_ids_to_tokens(token_ids); if (skip_special_tokens) { tokens = tokens.filter(x => !this.special_tokens.includes(x)); } // If `this.decoder` is null, we just join tokens with a space: // https://github.com/huggingface/tokenizers/blob/8edec536a737cb04494b454805be16c020abb14f/tokenizers/src/tokenizer/mod.rs#L835 /** @type {string} */ let decoded = this.decoder ? this.decoder(tokens) : tokens.join(' '); // Slight hack, but prevents having to pass `skip_special_tokens` to // each call to `decode`, which would lead to code duplication. if (this.decoder && this.decoder.end_of_word_suffix) { decoded = decoded.replaceAll(this.decoder.end_of_word_suffix, ' '); if (skip_special_tokens) { decoded = decoded.trim(); } } if (clean_up_tokenization_spaces ?? this.clean_up_tokenization_spaces) { decoded = clean_up_tokenization(decoded); } return decoded; } get default_chat_template() { if (!this._warned_about_chat_template) { console.warn( "No chat template is defined for this tokenizer - using a default chat template " + "that implements the ChatML format. If the default is not appropriate for " + "your model, please set `tokenizer.chat_template` to an appropriate template. " + "See https://huggingface.co/docs/transformers/main/chat_templating for more information." ) this._warned_about_chat_template = true; // TODO move to logger.warning_once() } return this._default_chat_template; } /** * Converts a list of message objects with `"role"` and `"content"` keys to a list of token * ids. This method is intended for use with chat models, and will read the tokenizer's chat_template attribute to * determine the format and control tokens to use when converting. When chat_template is None, it will fall back * to the default_chat_template specified at the class level. * * See [here](https://huggingface.co/docs/transformers/chat_templating) for more information. * * **Example:** Applying a chat template to a conversation. * * ```javascript * import { AutoTokenizer } from "@xenova/transformers"; * * const tokenizer = await AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1"); * * const chat = [ * { "role": "user", "content": "Hello, how are you?" }, * { "role": "assistant", "content": "I'm doing great. How can I help you today?" }, * { "role": "user", "content": "I'd like to show off how chat templating works!" }, * ] * * const text = tokenizer.apply_chat_template(chat, { tokenize: false }); * // "[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today? [INST] I'd like to show off how chat templating works! [/INST]" * * const input_ids = tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false }); * // [1, 733, 16289, 28793, 22557, 28725, 910, 460, 368, 28804, 733, 28748, 16289, 28793, 28737, 28742, 28719, 2548, 1598, 28723, 1602, 541, 315, 1316, 368, 3154, 28804, 2, 28705, 733, 16289, 28793, 315, 28742, 28715, 737, 298, 1347, 805, 910, 10706, 5752, 1077, 3791, 28808, 733, 28748, 16289, 28793] * ``` * * @param {Message[]} conversation A list of message objects with `"role"` and `"content"` keys. * @param {Object} options An optional object containing the following properties: * @param {string} [options.chat_template=null] A Jinja template to use for this conversion. If * this is not passed, the model's default chat template will be used instead. * @param {boolean} [options.add_generation_prompt=false] Whether to end the prompt with the token(s) that indicate * the start of an assistant message. This is useful when you want to generate a response from the model. * Note that this argument will be passed to the chat template, and so it must be supported in the * template for this argument to have any effect. * @param {boolean} [options.tokenize=true] Whether to tokenize the output. If false, the output will be a string. * @param {boolean} [options.padding=false] Whether to pad sequences to the maximum length. Has no effect if tokenize is false. * @param {boolean} [options.truncation=false] Whether to truncate sequences to the maximum length. Has no effect if tokenize is false. * @param {number} [options.max_length=null] Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is false. * If not specified, the tokenizer's `max_length` attribute will be used as a default. * @param {boolean} [options.return_tensor=true] Whether to return the output as a Tensor or an Array. Has no effect if tokenize is false. * @param {Object} [options.tokenizer_kwargs={}] Additional options to pass to the tokenizer. * @returns {string | Tensor | number[]| number[][]} The tokenized output. */ apply_chat_template(conversation, { chat_template = null, add_generation_prompt = false, tokenize = true, padding = false, truncation = false, max_length = null, return_tensor = true, tokenizer_kwargs = {}, ...kwargs } = {}) { // First, handle the cases when the model has a dict of multiple templates if ( (this.chat_template && typeof this.chat_template === 'object') || (this.chat_template === null && this.default_chat_template && typeof this.default_chat_template === 'object') ) { const template_dict = this.chat_template ?? this.default_chat_template; // Guaranteed to be a non-null object if (chat_template !== null && Object.hasOwn(template_dict, chat_template)) { // The user can pass the name of a template to the chat template argument instead of an entire template chat_template = template_dict[chat_template]; } else if (chat_template === null && 'default' in template_dict) { chat_template = template_dict['default']; } else if (chat_template === null) { throw Error( `This model has multiple chat templates with no default specified! Please either pass a chat ` + `template or the name of the template you wish to use to the 'chat_template' argument. Available ` + `template names are ${Object.keys(template_dict).sort()}.` ) } } else { // These are the cases when the model has a single template // priority: `chat_template` argument > `tokenizer.chat_template` > `tokenizer.default_chat_template chat_template ??= this.chat_template ?? this.default_chat_template; } if (typeof chat_template !== 'string') { throw Error(`chat_template must be a string, but got ${typeof chat_template}`); } // Compilation function uses a cache to avoid recompiling the same template let compiledTemplate = this._compiled_template_cache.get(chat_template); if (compiledTemplate === undefined) { compiledTemplate = new _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__.Template(chat_template); this._compiled_template_cache.set(chat_template, compiledTemplate); } const special_tokens_map = Object.create(null); for (const key of SPECIAL_TOKEN_ATTRIBUTES) { const value = this.getToken(key); if (value) { special_tokens_map[key] = value; } } const rendered = compiledTemplate.render({ messages: conversation, add_generation_prompt: add_generation_prompt, ...special_tokens_map, ...kwargs, }); if (tokenize) { return this._call(rendered, { add_special_tokens: false, padding, truncation, max_length, return_tensor, ...tokenizer_kwargs, }).input_ids; } return rendered; } } /** * BertTokenizer is a class used to tokenize text for BERT models. * @extends PreTrainedTokenizer */ class BertTokenizer extends PreTrainedTokenizer { return_token_type_ids = true; } /** * Albert tokenizer * @extends PreTrainedTokenizer */ class AlbertTokenizer extends PreTrainedTokenizer { return_token_type_ids = true; } class MobileBertTokenizer extends PreTrainedTokenizer { return_token_type_ids = true; } class SqueezeBertTokenizer extends PreTrainedTokenizer { return_token_type_ids = true; } class DebertaTokenizer extends PreTrainedTokenizer { return_token_type_ids = true; } class DebertaV2Tokenizer extends PreTrainedTokenizer { return_token_type_ids = true; } class HerbertTokenizer extends PreTrainedTokenizer { return_token_type_ids = true; } class ConvBertTokenizer extends PreTrainedTokenizer { return_token_type_ids = true; } class RoFormerTokenizer extends PreTrainedTokenizer { return_token_type_ids = true; } class DistilBertTokenizer extends PreTrainedTokenizer { } class CamembertTokenizer extends PreTrainedTokenizer { } class XLMTokenizer extends PreTrainedTokenizer { return_token_type_ids = true; constructor(tokenizerJSON, tokenizerConfig) { super(tokenizerJSON, tokenizerConfig); console.warn('WARNING: `XLMTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.') } } class ElectraTokenizer extends PreTrainedTokenizer { return_token_type_ids = true; } class T5Tokenizer extends PreTrainedTokenizer { } class GPT2Tokenizer extends PreTrainedTokenizer { _default_chat_template = `{% for message in messages %}" "{{ message.content }}{{ eos_token }}" "{% endfor %}` } class BartTokenizer extends PreTrainedTokenizer { } class MBartTokenizer extends PreTrainedTokenizer { constructor(tokenizerJSON, tokenizerConfig) { super(tokenizerJSON, tokenizerConfig); this.languageRegex = /^[a-z]{2}_[A-Z]{2}$/; this.language_codes = this.special_tokens.filter(x => this.languageRegex.test(x)); this.lang_to_token = x => x; // Identity function } /** * Helper function to build translation inputs for an `MBartTokenizer`. * @param {string|string[]} raw_inputs The text to tokenize. * @param {Object} tokenizer_options Options to be sent to the tokenizer * @param {Object} generate_kwargs Generation options. * @returns {Object} Object to be passed to the model. */ _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); } } class MBart50Tokenizer extends MBartTokenizer { } // NOTE: extends MBartTokenizer class RobertaTokenizer extends PreTrainedTokenizer { } class BloomTokenizer extends GPT2Tokenizer { // NOTE: `GPT2Tokenizer` to get the correct chat template constructor(tokenizerJSON, tokenizerConfig) { // Override the default (invalid) regex of the pretokenizer. // For more information, see https://github.com/xenova/transformers.js/issues/94 const splitChars = '.,!?\u2026\u3002\uff0c\u3001\u0964\u06d4\u060c'; const patternObject = tokenizerJSON.pre_tokenizer?.pretokenizers[0]?.pattern; if (patternObject && patternObject.Regex === ` ?[^(\\s|[${splitChars}])]+`) { patternObject.Regex = ` ?[^\\s${splitChars}]+`; } super(tokenizerJSON, tokenizerConfig); } } const SPIECE_UNDERLINE = "▁"; class LlamaTokenizer extends PreTrainedTokenizer { _default_chat_template = `{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}{% set system_message = messages[0]['content'] %}{% elif USE_DEFAULT_PROMPT == true and not '<>' in messages[0]['content'] %}{% set loop_messages = messages %}{% set system_message = 'DEFAULT_SYSTEM_MESSAGE' %}{% else %}{% set loop_messages = messages %}{% set system_message = false %}{% endif %}{% for message in loop_messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if loop.index0 == 0 and system_message != false %}{% set content = '<>\n' + system_message + '\n<>\n\n' + message['content'] %}{% else %}{% set content = message['content'] %}{% endif %}{% if message['role'] == 'user' %}{{ bos_token + '[INST] ' + content.strip() + ' [/INST]' }}{% elif message['role'] == 'system' %}{{ '<>\n' + content.strip() + '\n<>\n\n' }}{% elif message['role'] == 'assistant' %}{{ ' ' + content.strip() + ' ' + eos_token }}{% endif %}{% endfor %}` DEFAULT_SYSTEM_PROMPT = "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your " + "answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure " + "that your responses are socially unbiased and positive in nature.\n\n" + "If a question does not make any sense, or is not factually coherent, explain why instead of answering something not " + "correct. If you don't know the answer to a question, please don't share false information." padding_side = 'left'; constructor(tokenizerJSON, tokenizerConfig) { super(tokenizerJSON, tokenizerConfig); this.use_default_system_prompt = tokenizerConfig.use_default_system_prompt ?? false; this.legacy = tokenizerConfig.legacy ?? true; if (!this.legacy) { // See https://github.com/huggingface/transformers/pull/24565 for more information this.normalizer = null; this.pre_tokenizer = new MetaspacePreTokenizer({ replacement: SPIECE_UNDERLINE, add_prefix_space: true, prepend_scheme: "first", }); } } /** * Helper function to handle legacy encoding of SPM tokenizers. * Adapted from https://github.com/huggingface/transformers/blob/e6dcf8abd6f65bb4b6dfc1831b20d9ba49ce00e2/src/transformers/models/t5/tokenization_t5.py#L374-L387 * @param {string} text The text to encode. * @returns {string[]} The encoded tokens. */ _encode_text(text) { if (text === null) return null; if (this.legacy || text.length === 0) { return super._encode_text(text); } let tokens = super._encode_text(SPIECE_UNDERLINE + text.replaceAll(SPIECE_UNDERLINE, " ")); if (tokens.length > 1 && tokens[0] === SPIECE_UNDERLINE && this.special_tokens.includes(tokens[1])) { tokens = tokens.slice(1); } return tokens; } get default_chat_template() { return super.default_chat_template .replaceAll('USE_DEFAULT_PROMPT', this.use_default_system_prompt ? 'true' : 'false') .replaceAll('DEFAULT_SYSTEM_MESSAGE', this.DEFAULT_SYSTEM_PROMPT.replaceAll("\n", "\\n").replaceAll("'", "\\'")); } } class CodeLlamaTokenizer extends LlamaTokenizer { } // NOTE: `LlamaTokenizer` to get the correct chat template class XLMRobertaTokenizer extends PreTrainedTokenizer { } class MPNetTokenizer extends PreTrainedTokenizer { } class FalconTokenizer extends PreTrainedTokenizer { } class GPTNeoXTokenizer extends PreTrainedTokenizer { } class EsmTokenizer extends PreTrainedTokenizer { } class Qwen2Tokenizer extends PreTrainedTokenizer { } class GemmaTokenizer extends PreTrainedTokenizer { _default_chat_template = "{% if messages[0]['role'] == 'system' %}{{ raise_exception('System role not supported') }}{% endif %}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if (message['role'] == 'assistant') %}{% set role = 'model' %}{% else %}{% set role = message['role'] %}{% endif %}{{ '' + role + '\n' + message['content'] | trim + '\n' }}{% endfor %}{% if add_generation_prompt %}{{'model\n'}}{% endif %}" } class Grok1Tokenizer extends PreTrainedTokenizer { } /** * Helper function to build translation inputs for an `NllbTokenizer` or `M2M100Tokenizer`. * @param {PreTrainedTokenizer} self The tokenizer instance. * @param {string|string[]} raw_inputs The text to tokenize. * @param {Object} tokenizer_options Options to be sent to the tokenizer * @param {Object} generate_kwargs Generation options. * @returns {Object} Object to be passed to the model. * @private */ function _build_translation_inputs(self, raw_inputs, tokenizer_options, generate_kwargs) { if (!('language_codes' in self) || !Array.isArray(self.language_codes)) { throw new Error('Tokenizer must have `language_codes` attribute set and it should be an array of language ids.') } if (!('languageRegex' in self) || !(self.languageRegex instanceof RegExp)) { throw new Error('Tokenizer must have `languageRegex` attribute set and it should be a regular expression.') } if (!('lang_to_token' in self) || typeof self.lang_to_token !== 'function') { throw new Error('Tokenizer must have `lang_to_token` attribute set and it should be a function.') } const src_lang_token = generate_kwargs.src_lang; const tgt_lang_token = generate_kwargs.tgt_lang; // Check that the target language is valid: if (!self.language_codes.includes(tgt_lang_token)) { throw new Error(`Target language code "${tgt_lang_token}" is not valid. Must be one of: {${self.language_codes.join(', ')}}`); } // Allow `src_lang` to be optional. If not set, we'll use the tokenizer's default. if (src_lang_token !== undefined) { // Check that the source language is valid: if (!self.language_codes.includes(src_lang_token)) { throw new Error(`Source language code "${src_lang_token}" is not valid. Must be one of: {${self.language_codes.join(', ')}}`); } // In the same way as the Python library, we override the post-processor // to force the source language to be first: for (const item of self.post_processor.config.single) { if ('SpecialToken' in item && self.languageRegex.test(item.SpecialToken.id)) { item.SpecialToken.id = self.lang_to_token(src_lang_token); break; } } // TODO: Do the same for pair? } // Override the `forced_bos_token_id` to force the correct language generate_kwargs.forced_bos_token_id = self.model.convert_tokens_to_ids([self.lang_to_token(tgt_lang_token)])[0]; return self._call(raw_inputs, tokenizer_options); } /** * The NllbTokenizer class is used to tokenize text for NLLB ("No Language Left Behind") models. * * No Language Left Behind (NLLB) is a first-of-its-kind, AI breakthrough project * that open-sources models capable of delivering high-quality translations directly * between any pair of 200+ languages — including low-resource languages like Asturian, * Luganda, Urdu and more. It aims to help people communicate with anyone, anywhere, * regardless of their language preferences. For more information, check out their * [paper](https://arxiv.org/abs/2207.04672). * * For a list of supported languages (along with their language codes), * @see {@link https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200} */ class NllbTokenizer extends PreTrainedTokenizer { constructor(tokenizerJSON, tokenizerConfig) { super(tokenizerJSON, tokenizerConfig); this.languageRegex = /^[a-z]{3}_[A-Z][a-z]{3}$/; this.language_codes = this.special_tokens.filter(x => this.languageRegex.test(x)); this.lang_to_token = x => x; // Identity function } /** * Helper function to build translation inputs for an `NllbTokenizer`. * @param {string|string[]} raw_inputs The text to tokenize. * @param {Object} tokenizer_options Options to be sent to the tokenizer * @param {Object} generate_kwargs Generation options. * @returns {Object} Object to be passed to the model. */ _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); } } /** * The M2M100Tokenizer class is used to tokenize text for M2M100 ("Many-to-Many") models. * * M2M100 is a multilingual encoder-decoder (seq-to-seq) model trained for Many-to-Many * multilingual translation. It was introduced in this [paper](https://arxiv.org/abs/2010.11125) * and first released in [this](https://github.com/pytorch/fairseq/tree/master/examples/m2m_100) repository. * * For a list of supported languages (along with their language codes), * @see {@link https://huggingface.co/facebook/m2m100_418M#languages-covered} */ class M2M100Tokenizer extends PreTrainedTokenizer { constructor(tokenizerJSON, tokenizerConfig) { super(tokenizerJSON, tokenizerConfig); this.languageRegex = /^__[a-z]{2,3}__$/; this.language_codes = this.special_tokens .filter(x => this.languageRegex.test(x)) .map(x => x.slice(2, -2)); this.lang_to_token = x => `__${x}__`; } /** * Helper function to build translation inputs for an `M2M100Tokenizer`. * @param {string|string[]} raw_inputs The text to tokenize. * @param {Object} tokenizer_options Options to be sent to the tokenizer * @param {Object} generate_kwargs Generation options. * @returns {Object} Object to be passed to the model. */ _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); } } const WHISPER_LANGUAGES = [ ["en", "english"], ["zh", "chinese"], ["de", "german"], ["es", "spanish"], ["ru", "russian"], ["ko", "korean"], ["fr", "french"], ["ja", "japanese"], ["pt", "portuguese"], ["tr", "turkish"], ["pl", "polish"], ["ca", "catalan"], ["nl", "dutch"], ["ar", "arabic"], ["sv", "swedish"], ["it", "italian"], ["id", "indonesian"], ["hi", "hindi"], ["fi", "finnish"], ["vi", "vietnamese"], ["he", "hebrew"], ["uk", "ukrainian"], ["el", "greek"], ["ms", "malay"], ["cs", "czech"], ["ro", "romanian"], ["da", "danish"], ["hu", "hungarian"], ["ta", "tamil"], ["no", "norwegian"], ["th", "thai"], ["ur", "urdu"], ["hr", "croatian"], ["bg", "bulgarian"], ["lt", "lithuanian"], ["la", "latin"], ["mi", "maori"], ["ml", "malayalam"], ["cy", "welsh"], ["sk", "slovak"], ["te", "telugu"], ["fa", "persian"], ["lv", "latvian"], ["bn", "bengali"], ["sr", "serbian"], ["az", "azerbaijani"], ["sl", "slovenian"], ["kn", "kannada"], ["et", "estonian"], ["mk", "macedonian"], ["br", "breton"], ["eu", "basque"], ["is", "icelandic"], ["hy", "armenian"], ["ne", "nepali"], ["mn", "mongolian"], ["bs", "bosnian"], ["kk", "kazakh"], ["sq", "albanian"], ["sw", "swahili"], ["gl", "galician"], ["mr", "marathi"], ["pa", "punjabi"], ["si", "sinhala"], ["km", "khmer"], ["sn", "shona"], ["yo", "yoruba"], ["so", "somali"], ["af", "afrikaans"], ["oc", "occitan"], ["ka", "georgian"], ["be", "belarusian"], ["tg", "tajik"], ["sd", "sindhi"], ["gu", "gujarati"], ["am", "amharic"], ["yi", "yiddish"], ["lo", "lao"], ["uz", "uzbek"], ["fo", "faroese"], ["ht", "haitian creole"], ["ps", "pashto"], ["tk", "turkmen"], ["nn", "nynorsk"], ["mt", "maltese"], ["sa", "sanskrit"], ["lb", "luxembourgish"], ["my", "myanmar"], ["bo", "tibetan"], ["tl", "tagalog"], ["mg", "malagasy"], ["as", "assamese"], ["tt", "tatar"], ["haw", "hawaiian"], ["ln", "lingala"], ["ha", "hausa"], ["ba", "bashkir"], ["jw", "javanese"], ["su", "sundanese"], ] // @ts-ignore const WHISPER_LANGUAGE_MAPPING = new Map(WHISPER_LANGUAGES); // @ts-ignore const WHISPER_TO_LANGUAGE_CODE_MAPPING = new Map([ ...WHISPER_LANGUAGES.map(([k, v]) => [v, k]), ...[ ["burmese", "my"], ["valencian", "ca"], ["flemish", "nl"], ["haitian", "ht"], ["letzeburgesch", "lb"], ["pushto", "ps"], ["panjabi", "pa"], ["moldavian", "ro"], ["moldovan", "ro"], ["sinhalese", "si"], ["castilian", "es"], ] ]); /** * WhisperTokenizer tokenizer * @extends PreTrainedTokenizer */ class WhisperTokenizer extends PreTrainedTokenizer { _default_chat_template = `{% for message in messages %}" "{{ message.content }}{{ eos_token }}" "{% endfor %}`; /** * Decodes automatic speech recognition (ASR) sequences. * @param {Array<{tokens: number[], token_timestamps?: number[], stride: number[]}>} sequences The sequences to decode. * @param {Object} options The options to use for decoding. * @returns {Array, text: string}>}>} The decoded sequences. */ _decode_asr(sequences, { return_timestamps = false, return_language = false, time_precision = null, force_full_sequences = true } = {}) { // Set force_full_sequences=false if you want streaming // TODO add support for `return_language` // Internal method meant to only be used by asr pipeline. // Handles all the little quirks specific to whisper to handle // the various options not allowed in other seq2seq models // =========== Overview ============ // - iterate over all outputs // - all tokens within output // - Each token can be // - language token // - special token // - timestamp token // - text token // - We accumulate the text tokens. // - We split on end timestamps // - Lots of complexity comes from stride and timestamps if (time_precision === null) { throw Error("Must specify time_precision") } let last_language = null; const returnWordTimestamps = return_timestamps === "word"; function new_chunk() { return { "language": last_language, "timestamp": [null, null], "text": "" }; } // Welcome to the state machine! const chunks = []; let chunk = new_chunk(); let time_offset = 0.0; const timestamp_begin = this.model.convert_tokens_to_ids(["<|notimestamps|>"])[0] + 1; let previous_tokens = []; let previous_token_timestamps = []; let skip = false; let right_stride_start = null; const all_special_ids = new Set(this.all_special_ids); for (const output of sequences) { // NOTE: python version has batches, so it uses [0] const token_ids = output.tokens; const token_timestamps = returnWordTimestamps ? output.token_timestamps : null; // These keep track of timestamps within strides, which need // to be skipped and resolve all tokens in a single chunk. let last_timestamp = null; let first_timestamp = timestamp_begin; if ("stride" in output) { const [chunk_len, stride_left, stride_right] = output.stride; // Offset the timings to account for the other `model_outputs`. time_offset -= stride_left; right_stride_start = chunk_len - stride_right; // Keeping track of timestamps within strides // We're going to NOT split on those, and delay until we're // out of BOTH stride. Otherwise lots of issues occur and // corner cases if (stride_left) { first_timestamp = stride_left / time_precision + timestamp_begin; } if (stride_right) { for (let i = token_ids.length - 1; i >= 0; --i) { const token = token_ids[i]; if (token >= timestamp_begin) { // There can be several token in the right stride // But the last one is ALWAYS going to be skipped if (last_timestamp !== null && (token - timestamp_begin) * time_precision < right_stride_start) { break; } last_timestamp = token; } } } } let current_tokens = []; let current_token_timestamps = []; // - all tokens within output for (let i = 0; i < token_ids.length; ++i) { const token = token_ids[i]; // 4 possible states for each token // - 1/ Language code // - 2/ all other special tokens (which we ignore) // - 3/ Timestamp // - 4/ Regular text if (all_special_ids.has(token)) { const text = this.decode([token]); const language = WHISPER_LANGUAGE_MAPPING.get(text.slice(2, -2)); if (language !== undefined) { // 1/ Indeed some language // TODO Handle when language is different from the previous // one, and we cannot use timestamped tokens to create chunks if (last_language !== null && language !== last_language && !return_timestamps) { previous_tokens.push(current_tokens); const resolved_tokens = this.findLongestCommonSequence(previous_tokens)[0]; const resolved_text = this.decode(resolved_tokens); chunk.text = resolved_text; chunks.push(chunk); // Flush all our temporary context previous_tokens = []; current_tokens = []; chunk = new_chunk(); } last_language = chunk.language = language; } else { // 2/ This is a regular special token, ignoring it } } else if (token >= timestamp_begin) { // 3/ Timestamp token const time = (token - timestamp_begin) * time_precision + time_offset; const rounded_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(time, 2); if (last_timestamp !== null && token >= last_timestamp) { // Whisper outputted a timestamp token, but it falls within // our stride, so we're going to skip it for the time being // and resolve this later // Skip is necessary because timestamp tokens always come // by pair, so we need to skip the next one too (which would mark the start of another chunk). skip = true; } else if (skip || (previous_tokens.length > 0 && token < first_timestamp)) { skip = false; } else if (chunk.timestamp[0] === null) { chunk.timestamp[0] = rounded_time; } else { // This is the end of the timestamp chunk if (rounded_time === chunk.timestamp[0]) { // This is a bug in timestamp token output // where we're taking the duplicate token // as a stop where it should be a start. // This is an issue in the underlying model output // Let's just skip it so it becomes de-factor a start agin } else { chunk.timestamp[1] = rounded_time; // Handling merges previous_tokens.push(current_tokens) if (returnWordTimestamps) { previous_token_timestamps.push(current_token_timestamps); } const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence( previous_tokens, previous_token_timestamps ) const resolved_text = this.decode(resolved_tokens) chunk.text = resolved_text if (returnWordTimestamps) { chunk.words = this.collateWordTimestamps( resolved_tokens, resolved_token_timestamps, last_language, ) } chunks.push(chunk) // Flush all our temporary context previous_tokens = [] current_tokens = [] previous_token_timestamps = [] current_token_timestamps = [] chunk = new_chunk() } } } else { // 4/ Regular token // We just append to the list of all tokens so we can handle // merges later and decode into text. current_tokens.push(token) if (returnWordTimestamps) { let start_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i] + time_offset, 2); let end_time; if (i + 1 < token_timestamps.length) { end_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i + 1] + time_offset, 2); } else { // should never happen end_time = null; } current_token_timestamps.push([start_time, end_time]); } } } if ('stride' in output) { const [chunk_len, stride_left, stride_right] = output.stride; time_offset += chunk_len - stride_right } // Leftover tokens if (current_tokens.length > 0) { previous_tokens.push(current_tokens) if (returnWordTimestamps) { previous_token_timestamps.push(current_token_timestamps); } } else if (previous_tokens.every(p => p.length === 0)) { // Flushing previous tokens (END)" chunk = new_chunk() previous_tokens = [] current_tokens = [] previous_token_timestamps = []; current_token_timestamps = []; } } if (previous_tokens.length > 0) { if (force_full_sequences && return_timestamps) { // Last token should always be timestamps, so there shouldn't be // leftover throw new Error( "Whisper did not predict an ending timestamp, which can happen if audio is cut off in the middle of a word. " + "Also make sure WhisperTimeStampLogitsProcessor was used during generation." ); } // Happens when we don't use timestamps const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence(previous_tokens, previous_token_timestamps); // Flushing previous tokens (FINAL) const resolved_text = this.decode(resolved_tokens); chunk.text = resolved_text; if (returnWordTimestamps) { chunk.words = this.collateWordTimestamps( resolved_tokens, resolved_token_timestamps, last_language, ) } chunks.push(chunk); } let optional = Object.create(null); // Preparing and cleaning up the pipeline output const full_text = chunks.map(chunk => chunk.text).join(''); if (return_timestamps || return_language) { for (let i = 0; i < chunks.length; ++i) { const chunk = chunks[i]; if (!return_timestamps) { delete chunk["timestamp"]; } if (!return_language) { delete chunk["language"]; } } if (returnWordTimestamps) { const new_chunks = []; for (const chunk of chunks) { for (const word of chunk.words) { new_chunks.push(word); } } optional = { "chunks": new_chunks }; } else { optional = { "chunks": chunks }; } } return [full_text, optional]; } /** * Finds the longest common sequence among the provided sequences. * @param {number[][]} sequences An array of sequences of token ids to compare. * @returns {number[][]} The longest common sequence found. * @throws {Error} If there is a bug within the function. * @private */ findLongestCommonSequence(sequences, token_timestamp_sequences = null) { // It would be much harder to do O(n) because of fault tolerance. // We actually have a really good property which is that the total sequence // MUST be those subsequences in order. // If token_timestamp_sequences is provided, will split those sequences in // exactly the same way. let leftSequence = sequences[0]; let leftLength = leftSequence.length; let totalSequence = []; const use_token_timestamp_sequences = Array.isArray(token_timestamp_sequences) && token_timestamp_sequences.length > 0; let total_token_timestamp_sequence = use_token_timestamp_sequences ? [] : null; let left_token_timestamp_sequence = use_token_timestamp_sequences ? token_timestamp_sequences[0] : null; for (let i = 1; i < sequences.length; ++i) { const rightSequence = sequences[i]; let max = 0.0; let maxIndices = [leftLength, leftLength, 0, 0]; // Here we're sliding matches // [a, b, c, d] // [c, d, f] // = [c] == [d] // [a, b, c, d] // [c, d, f] // = [c, d] == [c, d] // [a, b, c, d] // [c, d, f] // = [b, c, d] == [c, d, f] // [a, b, c, d] // [c, d, f] // [a, b, c] == [c, d, f] // [a, b, c, d] // [d, f] // [a, b] == [d, f] // [a, b, c, d] // [f] // [a] == [f] const rightLength = rightSequence.length; for (let j = 1; j < leftLength + rightLength; ++j) { const eps = j / 10000.0; const leftStart = Math.max(0, leftLength - j); const leftStop = Math.min(leftLength, leftLength + rightLength - j); const left = leftSequence.slice(leftStart, leftStop); const rightStart = Math.max(0, j - leftLength); const rightStop = Math.min(rightLength, j); const right = rightSequence.slice(rightStart, rightStop); if (left.length !== right.length) { throw new Error("There is a bug within whisper `decode_asr` function, please report it. Dropping to prevent bad inference."); } const matches = left.filter((elem, idx) => elem === right[idx]).length; const matching = matches / j + eps; if (matches > 1 && matching > max) { max = matching; maxIndices = [leftStart, leftStop, rightStart, rightStop]; } } const [leftStart, leftStop, rightStart, rightStop] = maxIndices; const leftMid = Math.floor((leftStop + leftStart) / 2); const rightMid = Math.floor((rightStop + rightStart) / 2); totalSequence.push(...leftSequence.slice(0, leftMid)); leftSequence = rightSequence.slice(rightMid); leftLength = leftSequence.length; if (use_token_timestamp_sequences) { total_token_timestamp_sequence.push(...left_token_timestamp_sequence.slice(0, leftMid)); left_token_timestamp_sequence = token_timestamp_sequences[i].slice(rightMid); } } totalSequence.push(...leftSequence); if (use_token_timestamp_sequences) { total_token_timestamp_sequence.push(...left_token_timestamp_sequence); return [totalSequence, total_token_timestamp_sequence]; } else { return [totalSequence, []]; } } /** @private */ collateWordTimestamps(tokens, token_timestamps, language) { const [words, _, token_indices] = this.combineTokensIntoWords(tokens, language); const timings = []; for (let i = 0; i < words.length; ++i) { const indices = token_indices[i]; timings.push({ text: words[i], timestamp: [ token_timestamps[indices.at(0)][0], token_timestamps[indices.at(-1)][1], ], }); } return timings; } /** * Groups tokens by word. Returns a tuple containing a list of strings with the words, * and a list of `token_id` sequences with the tokens making up each word. * @param {number[]} tokens * @param {string} [language] * @param {string} prepend_punctionations * @param {string} append_punctuations * * @private */ combineTokensIntoWords(tokens, language, prepend_punctionations = "\"'“¡¿([{-", append_punctuations = "\"'.。,,!!??::”)]}、") { language = language ?? 'english'; let words, word_tokens, token_indices; if (["chinese", "japanese", "thai", "lao", "myanmar"].includes(language)) { // These languages don't typically use spaces. [words, word_tokens, token_indices] = this.splitTokensOnUnicode(tokens) } else { [words, word_tokens, token_indices] = this.splitTokensOnSpaces(tokens) } return this.mergePunctuations(words, word_tokens, token_indices, prepend_punctionations, append_punctuations); } /** @type {PreTrainedTokenizer['decode']} */ decode( token_ids, decode_args, ) { let text; // @ts-ignore if (decode_args && decode_args.decode_with_timestamps) { if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { token_ids = prepareTensorForDecode(token_ids); } text = this.decodeWithTimestamps(token_ids, decode_args); } else { text = super.decode(token_ids, decode_args); } // TODO: implement offsets // if (decode_args.output_offsets) { // let offsets = this.computeOffsets // } return text; } /** * @param {number[]} token_ids List of token IDs to decode. * @param {Object} decode_args Optional arguments for decoding * @private */ decodeWithTimestamps(token_ids, decode_args) { const time_precision = decode_args?.time_precision ?? 0.02; const timestamp_begin = Array.from(this.all_special_ids).at(-1) + 1; /**@type {Array} */ let outputs = [[]]; for (const token of token_ids) { if (token >= timestamp_begin) { const timestamp = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)((token - timestamp_begin) * time_precision, 2); outputs.push(`<|${timestamp}|>`); outputs.push([]); } else { outputs[outputs.length - 1].push(token); } } outputs = outputs.map( s => { if (typeof s === 'string') { return s; } else { return super.decode(s, decode_args); } } ) return outputs.join(''); } /** * Combine tokens into words by splitting at any position where the tokens are decoded as valid unicode points. * @param {number[]} tokens * @returns {*} * @private */ splitTokensOnUnicode(tokens) { const decoded_full = this.decode(tokens, { // @ts-ignore decode_with_timestamps: true, }); const replacement_char = '\uFFFD'; const words = [] const word_tokens = [] const token_indices = [] let current_tokens = [] let current_indices = [] let unicode_offset = 0 for (let token_idx = 0; token_idx < tokens.length; ++token_idx) { const token = tokens[token_idx]; current_tokens.push(token); current_indices.push(token_idx); const decoded = this.decode(current_tokens, { // @ts-ignore decode_with_timestamps: true, }); if (!decoded.includes(replacement_char) || decoded_full[unicode_offset + decoded.indexOf(replacement_char)] === replacement_char) { words.push(decoded) word_tokens.push(current_tokens) token_indices.push(current_indices) current_tokens = [] current_indices = [] unicode_offset += decoded.length; } } return [words, word_tokens, token_indices] } /** * Combine tokens into words by splitting at whitespace and punctuation tokens. * @param {number[]} tokens * @private */ splitTokensOnSpaces(tokens) { const [subwords, subword_tokens_list, subword_indices_list] = this.splitTokensOnUnicode(tokens); const words = [] const word_tokens = [] const token_indices = [] const punctuationRegex = new RegExp(`^[${PUNCTUATION_REGEX}]$`, 'gu'); for (let i = 0; i < subwords.length; ++i) { const subword = subwords[i]; const subword_tokens = subword_tokens_list[i]; const subword_indices = subword_indices_list[i]; // @ts-ignore const special = subword_tokens[0] >= this.model.tokens_to_ids.get('<|endoftext|>'); const with_space = subword.startsWith(' '); const trimmed = subword.trim(); const punctuation = punctuationRegex.test(trimmed); if (special || with_space || punctuation || words.length === 0) { words.push(subword); word_tokens.push(subword_tokens); token_indices.push(subword_indices); } else { const ix = words.length - 1; words[ix] += subword; word_tokens[ix].push(...subword_tokens); token_indices[ix].push(...subword_indices); } } return [words, word_tokens, token_indices]; } /** * Merges punctuation tokens with neighboring words. * @param {string[]} words * @param {number[][]} tokens * @param {number[][]} indices * @param {string} prepended * @param {string} appended * @private */ mergePunctuations(words, tokens, indices, prepended, appended) { const newWords = structuredClone(words); const newTokens = structuredClone(tokens); const newIndices = structuredClone(indices); // prepend punctuations let i = newWords.length - 2; let j = newWords.length - 1; while (i >= 0) { if (newWords[i].startsWith(' ') && prepended.includes(newWords[i].trim())) { newWords[j] = newWords[i] + newWords[j]; newTokens[j] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); newIndices[j] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); newWords[i] = ''; newTokens[i] = []; newIndices[i] = []; } else { j = i; } --i; } // append punctuations i = 0; j = 1; while (j < newWords.length) { if (!newWords[i].endsWith(' ') && appended.includes(newWords[j])) { newWords[i] += newWords[j]; newTokens[i] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); newIndices[i] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); newWords[j] = ''; newTokens[j] = []; newIndices[j] = []; } else { i = j; } ++j; } return [ newWords.filter(x => x), newTokens.filter(x => x.length > 0), newIndices.filter(x => x.length > 0), ] } /** * Helper function to build translation inputs for a `WhisperTokenizer`, * depending on the language, task, and whether to predict timestamp tokens. * * Used to override the prefix tokens appended to the start of the label sequence. * * **Example: Get ids for a language** * ```javascript * // instantiate the tokenizer and set the prefix token to Spanish * const tokenizer = await WhisperTokenizer.from_pretrained('Xenova/whisper-tiny'); * const forced_decoder_ids = tokenizer.get_decoder_prompt_ids({ language: 'spanish' }); * // [(1, 50262), (2, 50363)] * ``` * * @param {Object} options Options to generate the decoder prompt. * @param {string} [options.language] The language of the transcription text. * The corresponding language id token is appended to the start of the sequence for multilingual * speech recognition and speech translation tasks, e.g. for "Spanish" the token "<|es|>" is appended * to the start of sequence. * @param {string} [options.task] Task identifier to append at the start of sequence (if any). * This should be used for mulitlingual fine-tuning, with "transcribe" for speech recognition and * "translate" for speech translation. * @param {boolean} [options.no_timestamps] Whether to add the <|notimestamps|> token at the start of the sequence. * @returns {number[][]} The decoder prompt ids. */ get_decoder_prompt_ids({ language = null, task = null, no_timestamps = true, } = {}) { // <|lang_id|> <|task|> <|notimestamps|> const forced_decoder_ids = []; if (language) { // User wishes to specify the language language = language.toLowerCase(); // Map to code from user-friendly name (e.g., "english" -> "en") let language_code = WHISPER_TO_LANGUAGE_CODE_MAPPING.get(language); if (language_code === undefined) { // User provided something that is not a language name if (WHISPER_LANGUAGE_MAPPING.has(language)) { // User provided the language code directly (e.g., "en") language_code = language; } else { // User provided something that is not a language code or name const is_language_code = language.length === 2; const langs = is_language_code ? WHISPER_LANGUAGE_MAPPING.keys() : WHISPER_LANGUAGE_MAPPING.values(); throw new Error(`Language "${language}" is not supported. Must be one of: ${JSON.stringify(langs)}`); } } const language_token_id = this.model.tokens_to_ids.get(`<|${language_code}|>`); if (language_token_id === undefined) { throw new Error(`Unable to find language "${language_code}" in model vocabulary. Please report this issue at https://github.com/xenova/transformers.js/issues/new/choose.`) } forced_decoder_ids.push(language_token_id); } else { // No token will be forced, which leaves the model to predict the language forced_decoder_ids.push(null); } if (task) { task = task.toLowerCase(); if (task !== 'transcribe' && task !== 'translate') { throw new Error(`Task "${task}" is not supported. Must be one of: ["transcribe", "translate"]`); } const task_token_id = this.model.tokens_to_ids.get(`<|${task}|>`); if (task_token_id === undefined) { throw new Error(`Unable to find task "${task}" in model vocabulary. Please report this issue at https://github.com/xenova/transformers.js/issues/new/choose.`) } forced_decoder_ids.push(task_token_id); } else { // No token will be forced, which leaves the model to predict the task forced_decoder_ids.push(null); } if (no_timestamps) { const no_timestamps_id = this.model.tokens_to_ids.get(`<|notimestamps|>`); if (no_timestamps_id === undefined) { throw new Error('Unable to find "<|notimestamps|>" in model vocabulary. Please report this issue at https://github.com/xenova/transformers.js/issues/new/choose.') } forced_decoder_ids.push(no_timestamps_id); } return forced_decoder_ids.map((x, i) => [i + 1, x]).filter(x => x[1] !== null); } } class CodeGenTokenizer extends PreTrainedTokenizer { } class CLIPTokenizer extends PreTrainedTokenizer { } class SiglipTokenizer extends PreTrainedTokenizer { } /** * @todo This model is not yet supported by Hugging Face's "fast" tokenizers library (https://github.com/huggingface/tokenizers). * Therefore, this implementation (which is based on fast tokenizers) may produce slightly inaccurate results. */ class MarianTokenizer extends PreTrainedTokenizer { /** * Create a new MarianTokenizer instance. * @param {Object} tokenizerJSON The JSON of the tokenizer. * @param {Object} tokenizerConfig The config of the tokenizer. */ constructor(tokenizerJSON, tokenizerConfig) { super(tokenizerJSON, tokenizerConfig); this.languageRegex = /^(>>\w+<<)\s*/g; this.supported_language_codes = this.model.vocab.filter( x => this.languageRegex.test(x) ); console.warn('WARNING: `MarianTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.') } /** * Encodes a single text. Overriding this method is necessary since the language codes * must be removed before encoding with sentencepiece model. * @see https://github.com/huggingface/transformers/blob/12d51db243a00726a548a43cc333390ebae731e3/src/transformers/models/marian/tokenization_marian.py#L204-L213 * * @param {string|null} text The text to encode. * @returns {Array} The encoded tokens. */ _encode_text(text) { if (text === null) return null; // Check if text starts with language code: const [matchInfo, ...remainder] = text.trim().split(this.languageRegex); if (remainder.length === 0) { // No language code, encode normally return super._encode_text(matchInfo); } else if (remainder.length === 2) { // Text starts with language code, so we do not encode it with sentencepiece. const [language, text] = remainder; if (!this.supported_language_codes.includes(language)) { console.warn(`Unsupported language code "${language}" detected, which may lead to unexpected behavior. Should be one of: ${JSON.stringify(this.supported_language_codes)}`) } return (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([language], super._encode_text(text)); } } } class Wav2Vec2CTCTokenizer extends PreTrainedTokenizer { } class BlenderbotTokenizer extends PreTrainedTokenizer { _default_chat_template = `{% for message in messages %}{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}{{ message['content'] }}{% if not loop.last %}{{ ' ' }}{% endif %}{% endfor %}{{ eos_token }}`; } class BlenderbotSmallTokenizer extends BlenderbotTokenizer { } // NOTE `BlenderbotTokenizer` to get the correct chat template class SpeechT5Tokenizer extends PreTrainedTokenizer { } class NougatTokenizer extends PreTrainedTokenizer { } class VitsTokenizer extends PreTrainedTokenizer { constructor(tokenizerJSON, tokenizerConfig) { super(tokenizerJSON, tokenizerConfig); // Custom decoder function this.decoder = new VitsDecoder({}); } } class CohereTokenizer extends PreTrainedTokenizer { } /** * Helper class which is used to instantiate pretrained tokenizers with the `from_pretrained` function. * The chosen tokenizer class is determined by the type specified in the tokenizer config. * * @example * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); */ class AutoTokenizer { static TOKENIZER_CLASS_MAPPING = { T5Tokenizer, DistilBertTokenizer, CamembertTokenizer, DebertaTokenizer, DebertaV2Tokenizer, BertTokenizer, HerbertTokenizer, ConvBertTokenizer, RoFormerTokenizer, XLMTokenizer, ElectraTokenizer, MobileBertTokenizer, SqueezeBertTokenizer, AlbertTokenizer, GPT2Tokenizer, BartTokenizer, MBartTokenizer, MBart50Tokenizer, RobertaTokenizer, WhisperTokenizer, CodeGenTokenizer, CLIPTokenizer, SiglipTokenizer, MarianTokenizer, BloomTokenizer, NllbTokenizer, M2M100Tokenizer, LlamaTokenizer, CodeLlamaTokenizer, XLMRobertaTokenizer, MPNetTokenizer, FalconTokenizer, GPTNeoXTokenizer, EsmTokenizer, Wav2Vec2CTCTokenizer, BlenderbotTokenizer, BlenderbotSmallTokenizer, SpeechT5Tokenizer, NougatTokenizer, VitsTokenizer, Qwen2Tokenizer, GemmaTokenizer, Grok1Tokenizer, CohereTokenizer, // Base case: PreTrainedTokenizer, } /** * Instantiate one of the tokenizer classes of the library from a pretrained model. * * The tokenizer class to instantiate is selected based on the `tokenizer_class` property of the config object * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) * * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: * - A string, the *model id* of a pretrained tokenizer hosted inside a model repo on huggingface.co. * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a * user or organization name, like `dbmdz/bert-base-german-cased`. * - A path to a *directory* containing tokenizer files, e.g., `./my_model_directory/`. * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. * * @returns {Promise} A new instance of the PreTrainedTokenizer class. */ static async from_pretrained(pretrained_model_name_or_path, { progress_callback = null, config = null, cache_dir = null, local_files_only = false, revision = 'main', legacy = null, } = {}) { const [tokenizerJSON, tokenizerConfig] = await loadTokenizer(pretrained_model_name_or_path, { progress_callback, config, cache_dir, local_files_only, revision, legacy, }) // Some tokenizers are saved with the "Fast" suffix, so we remove that if present. const tokenizerName = tokenizerConfig.tokenizer_class?.replace(/Fast$/, '') ?? 'PreTrainedTokenizer'; let cls = this.TOKENIZER_CLASS_MAPPING[tokenizerName]; if (!cls) { console.warn(`Unknown tokenizer class "${tokenizerName}", attempting to construct from base class.`); cls = PreTrainedTokenizer; } return new cls(tokenizerJSON, tokenizerConfig); } } /***/ }), /***/ "./src/utils/audio.js": /*!****************************!*\ !*** ./src/utils/audio.js ***! \****************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "hanning": () => (/* binding */ hanning), /* harmony export */ "mel_filter_bank": () => (/* binding */ mel_filter_bank), /* harmony export */ "read_audio": () => (/* binding */ read_audio), /* harmony export */ "spectrogram": () => (/* binding */ spectrogram), /* harmony export */ "window_function": () => (/* binding */ window_function) /* harmony export */ }); /* harmony import */ var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./hub.js */ "./src/utils/hub.js"); /* harmony import */ var _maths_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./maths.js */ "./src/utils/maths.js"); /* harmony import */ var _core_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./core.js */ "./src/utils/core.js"); /** * @file Helper module for audio processing. * * These functions and classes are only used internally, * meaning an end-user shouldn't need to access anything here. * * @module utils/audio */ /** * Helper function to read audio from a path/URL. * @param {string|URL} url The path/URL to load the audio from. * @param {number} sampling_rate The sampling rate to use when decoding the audio. * @returns {Promise} The decoded audio as a `Float32Array`. */ async function read_audio(url, sampling_rate) { if (typeof AudioContext === 'undefined') { // Running in node or an environment without AudioContext throw Error( "Unable to load audio from path/URL since `AudioContext` is not available in your environment. " + "Instead, audio data should be passed directly to the pipeline/processor. " + "For more information and some example code, see https://huggingface.co/docs/transformers.js/guides/node-audio-processing." ) } const response = await (await (0,_hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url)).arrayBuffer(); const audioCTX = new AudioContext({ sampleRate: sampling_rate }); if (typeof sampling_rate === 'undefined') { console.warn(`No sampling rate provided, using default of ${audioCTX.sampleRate}Hz.`) } const decoded = await audioCTX.decodeAudioData(response); /** @type {Float32Array} */ let audio; // We now replicate HuggingFace's `ffmpeg_read` method: if (decoded.numberOfChannels === 2) { // When downmixing a stereo audio file to mono using the -ac 1 option in FFmpeg, // the audio signal is summed across both channels to create a single mono channel. // However, if the audio is at full scale (i.e. the highest possible volume level), // the summing of the two channels can cause the audio signal to clip or distort. // To prevent this clipping, FFmpeg applies a scaling factor of 1/sqrt(2) (~ 0.707) // to the audio signal before summing the two channels. This scaling factor ensures // that the combined audio signal will not exceed the maximum possible level, even // if both channels are at full scale. // After applying this scaling factor, the audio signal from both channels is summed // to create a single mono channel. It's worth noting that this scaling factor is // only applied when downmixing stereo audio to mono using the -ac 1 option in FFmpeg. // If you're using a different downmixing method, or if you're not downmixing the // audio at all, this scaling factor may not be needed. const SCALING_FACTOR = Math.sqrt(2); const left = decoded.getChannelData(0); const right = decoded.getChannelData(1); audio = new Float32Array(left.length); for (let i = 0; i < decoded.length; ++i) { audio[i] = SCALING_FACTOR * (left[i] + right[i]) / 2; } } else { // If the audio is not stereo, we can just use the first channel: audio = decoded.getChannelData(0); } return audio; } /** * Generates a Hanning window of length M. * * @param {number} M The length of the Hanning window to generate. * @returns {Float64Array} The generated Hanning window. */ function hanning(M) { if (M < 1) { return new Float64Array(); } if (M === 1) { return new Float64Array([1]); } const denom = M - 1; const factor = Math.PI / denom; const cos_vals = new Float64Array(M); for (let i = 0; i < M; ++i) { const n = 2 * i - denom; cos_vals[i] = 0.5 + 0.5 * Math.cos(factor * n); } return cos_vals; } const HERTZ_TO_MEL_MAPPING = { "htk": (/** @type {number} */ freq) => 2595.0 * Math.log10(1.0 + (freq / 700.0)), "kaldi": (/** @type {number} */ freq) => 1127.0 * Math.log(1.0 + (freq / 700.0)), "slaney": (/** @type {number} */ freq, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = 27.0 / Math.log(6.4)) => freq >= min_log_hertz ? min_log_mel + Math.log(freq / min_log_hertz) * logstep : 3.0 * freq / 200.0, } /** * @template {Float32Array|Float64Array|number} T * @param {T} freq * @param {string} [mel_scale] * @returns {T} */ function hertz_to_mel(freq, mel_scale = "htk") { const fn = HERTZ_TO_MEL_MAPPING[mel_scale]; if (!fn) { throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); } return typeof freq === 'number' ? fn(freq) : freq.map(x => fn(x)); } const MEL_TO_HERTZ_MAPPING = { "htk": (/** @type {number} */ mels) => 700.0 * (10.0 ** (mels / 2595.0) - 1.0), "kaldi": (/** @type {number} */ mels) => 700.0 * (Math.exp(mels / 1127.0) - 1.0), "slaney": (/** @type {number} */ mels, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = Math.log(6.4) / 27.0) => mels >= min_log_mel ? min_log_hertz * Math.exp(logstep * (mels - min_log_mel)) : 200.0 * mels / 3.0, } /** * @template {Float32Array|Float64Array|number} T * @param {T} mels * @param {string} [mel_scale] * @returns {T} */ function mel_to_hertz(mels, mel_scale = "htk") { const fn = MEL_TO_HERTZ_MAPPING[mel_scale]; if (!fn) { throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); } return typeof mels === 'number' ? fn(mels) : mels.map(x => fn(x)); } /** * Creates a triangular filter bank. * * Adapted from torchaudio and librosa. * * @param {Float64Array} fft_freqs Discrete frequencies of the FFT bins in Hz, of shape `(num_frequency_bins,)`. * @param {Float64Array} filter_freqs Center frequencies of the triangular filters to create, in Hz, of shape `(num_mel_filters,)`. * @returns {number[][]} of shape `(num_frequency_bins, num_mel_filters)`. */ function _create_triangular_filter_bank(fft_freqs, filter_freqs) { const filter_diff = Float64Array.from( { length: filter_freqs.length - 1 }, (_, i) => filter_freqs[i + 1] - filter_freqs[i] ); const slopes = Array.from({ length: fft_freqs.length }, () => new Array(filter_freqs.length)); for (let j = 0; j < fft_freqs.length; ++j) { const slope = slopes[j]; for (let i = 0; i < filter_freqs.length; ++i) { slope[i] = filter_freqs[i] - fft_freqs[j]; } } const numFreqs = filter_freqs.length - 2; const ret = Array.from({ length: numFreqs }, () => new Array(fft_freqs.length)); for (let j = 0; j < fft_freqs.length; ++j) { // 201 const slope = slopes[j]; for (let i = 0; i < numFreqs; ++i) { // 80 const down = -slope[i] / filter_diff[i]; const up = slope[i + 2] / filter_diff[i + 1]; ret[i][j] = Math.max(0, Math.min(down, up)); } } return ret; } /** * Return evenly spaced numbers over a specified interval. * @param {number} start The starting value of the sequence. * @param {number} end The end value of the sequence. * @param {number} num Number of samples to generate. * @returns `num` evenly spaced samples, calculated over the interval `[start, stop]`. */ function linspace(start, end, num) { const step = (end - start) / (num - 1); return Float64Array.from({ length: num }, (_, i) => start + step * i); } /** * Creates a frequency bin conversion matrix used to obtain a mel spectrogram. This is called a *mel filter bank*, and * various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters * are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these * features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency. * @param {number} num_frequency_bins Number of frequencies used to compute the spectrogram (should be the same as in `stft`). * @param {number} num_mel_filters Number of mel filters to generate. * @param {number} min_frequency Lowest frequency of interest in Hz. * @param {number} max_frequency Highest frequency of interest in Hz. This should not exceed `sampling_rate / 2`. * @param {number} sampling_rate Sample rate of the audio waveform. * @param {string} [norm] If `"slaney"`, divide the triangular mel weights by the width of the mel band (area normalization). * @param {string} [mel_scale] The mel frequency scale to use, `"htk"` or `"slaney"`. * @param {boolean} [triangularize_in_mel_space] If this option is enabled, the triangular filter is applied in mel space rather than frequency space. * This should be set to `true` in order to get the same results as `torchaudio` when computing mel filters. * @returns {number[][]} Triangular filter bank matrix, which is a 2D array of shape (`num_frequency_bins`, `num_mel_filters`). * This is a projection matrix to go from a spectrogram to a mel spectrogram. */ function mel_filter_bank( num_frequency_bins, num_mel_filters, min_frequency, max_frequency, sampling_rate, norm = null, mel_scale = "htk", triangularize_in_mel_space = false, ) { if (norm !== null && norm !== "slaney") { throw new Error('norm must be one of null or "slaney"'); } const mel_min = hertz_to_mel(min_frequency, mel_scale); const mel_max = hertz_to_mel(max_frequency, mel_scale); const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2); let filter_freqs = mel_to_hertz(mel_freqs, mel_scale); let fft_freqs; // frequencies of FFT bins in Hz if (triangularize_in_mel_space) { const fft_bin_width = sampling_rate / (num_frequency_bins * 2); fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale); filter_freqs = mel_freqs; } else { fft_freqs = linspace(0, Math.floor(sampling_rate / 2), num_frequency_bins); } const mel_filters = _create_triangular_filter_bank(fft_freqs, filter_freqs); if (norm !== null && norm === "slaney") { // Slaney-style mel is scaled to be approx constant energy per channel for (let i = 0; i < num_mel_filters; ++i) { const filter = mel_filters[i]; const enorm = 2.0 / (filter_freqs[i + 2] - filter_freqs[i]); for (let j = 0; j < num_frequency_bins; ++j) { // Apply this enorm to all frequency bins filter[j] *= enorm; } } } // TODO warn if there is a zero row return mel_filters; } /** * @template {Float32Array|Float64Array} T * Pads an array with a reflected version of itself on both ends. * @param {T} array The array to pad. * @param {number} left The amount of padding to add to the left. * @param {number} right The amount of padding to add to the right. * @returns {T} The padded array. */ function padReflect(array, left, right) { // @ts-ignore const padded = new array.constructor(array.length + left + right); const w = array.length - 1; for (let i = 0; i < array.length; ++i) { padded[left + i] = array[i]; } for (let i = 1; i <= left; ++i) { padded[left - i] = array[(0,_core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(i, w)]; } for (let i = 1; i <= right; ++i) { padded[w + left + i] = array[(0,_core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(w - i, w)]; } return padded; } /** * Helper function to compute `amplitude_to_db` and `power_to_db`. * @template {Float32Array|Float64Array} T * @param {T} spectrogram * @param {number} factor * @param {number} reference * @param {number} min_value * @param {number} db_range * @returns {T} */ function _db_conversion_helper(spectrogram, factor, reference, min_value, db_range) { if (reference <= 0) { throw new Error('reference must be greater than zero'); } if (min_value <= 0) { throw new Error('min_value must be greater than zero'); } reference = Math.max(min_value, reference); const logReference = Math.log10(reference); for (let i = 0; i < spectrogram.length; ++i) { spectrogram[i] = factor * Math.log10(Math.max(min_value, spectrogram[i]) - logReference) } if (db_range !== null) { if (db_range <= 0) { throw new Error('db_range must be greater than zero'); } const maxValue = (0,_maths_js__WEBPACK_IMPORTED_MODULE_1__.max)(spectrogram)[0] - db_range; for (let i = 0; i < spectrogram.length; ++i) { spectrogram[i] = Math.max(spectrogram[i], maxValue); } } return spectrogram; } /** * Converts an amplitude spectrogram to the decibel scale. This computes `20 * log10(spectrogram / reference)`, * using basic logarithm properties for numerical stability. NOTE: Operates in-place. * * The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a * linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it. * This means that large variations in energy may not sound all that different if the sound is loud to begin with. * This compression operation makes the (mel) spectrogram features match more closely what humans actually hear. * * @template {Float32Array|Float64Array} T * @param {T} spectrogram The input amplitude (mel) spectrogram. * @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. * For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero. * @param {number} [min_value=1e-5] The spectrogram will be clipped to this minimum value before conversion to decibels, * to avoid taking `log(0)`. The default of `1e-5` corresponds to a minimum of -100 dB. Must be greater than zero. * @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the * difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero. * @returns {T} The modified spectrogram in decibels. */ function amplitude_to_db(spectrogram, reference = 1.0, min_value = 1e-5, db_range = null) { return _db_conversion_helper(spectrogram, 20.0, reference, min_value, db_range); } /** * Converts a power spectrogram to the decibel scale. This computes `10 * log10(spectrogram / reference)`, * using basic logarithm properties for numerical stability. NOTE: Operates in-place. * * The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a * linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it. * This means that large variations in energy may not sound all that different if the sound is loud to begin with. * This compression operation makes the (mel) spectrogram features match more closely what humans actually hear. * * Based on the implementation of `librosa.power_to_db`. * * @template {Float32Array|Float64Array} T * @param {T} spectrogram The input power (mel) spectrogram. Note that a power spectrogram has the amplitudes squared! * @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. * For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero. * @param {number} [min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, * to avoid taking `log(0)`. The default of `1e-10` corresponds to a minimum of -100 dB. Must be greater than zero. * @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the * difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero. * @returns {T} The modified spectrogram in decibels. */ function power_to_db(spectrogram, reference = 1.0, min_value = 1e-10, db_range = null) { return _db_conversion_helper(spectrogram, 10.0, reference, min_value, db_range); } /** * Calculates a spectrogram over one waveform using the Short-Time Fourier Transform. * * This function can create the following kinds of spectrograms: * - amplitude spectrogram (`power = 1.0`) * - power spectrogram (`power = 2.0`) * - complex-valued spectrogram (`power = None`) * - log spectrogram (use `log_mel` argument) * - mel spectrogram (provide `mel_filters`) * - log-mel spectrogram (provide `mel_filters` and `log_mel`) * * In this implementation, the window is assumed to be zero-padded to have the same size as the analysis frame. * A padded window can be obtained from `window_function()`. The FFT input buffer may be larger than the analysis frame, * typically the next power of two. * * @param {Float32Array|Float64Array} waveform The input waveform of shape `(length,)`. This must be a single real-valued, mono waveform. * @param {Float32Array|Float64Array} window The windowing function to apply of shape `(frame_length,)`, including zero-padding if necessary. The actual window length may be * shorter than `frame_length`, but we're assuming the array has already been zero-padded. * @param {number} frame_length The length of the analysis frames in samples (a.k.a., `fft_length`). * @param {number} hop_length The stride between successive analysis frames in samples. * @param {Object} options * @param {number} [options.fft_length=null] The size of the FFT buffer in samples. This determines how many frequency bins the spectrogram will have. * For optimal speed, this should be a power of two. If `null`, uses `frame_length`. * @param {number} [options.power=1.0] If 1.0, returns the amplitude spectrogram. If 2.0, returns the power spectrogram. If `null`, returns complex numbers. * @param {boolean} [options.center=true] Whether to pad the waveform so that frame `t` is centered around time `t * hop_length`. If `false`, frame * `t` will start at time `t * hop_length`. * @param {string} [options.pad_mode="reflect"] Padding mode used when `center` is `true`. Possible values are: `"constant"` (pad with zeros), * `"edge"` (pad with edge values), `"reflect"` (pads with mirrored values). * @param {boolean} [options.onesided=true] If `true`, only computes the positive frequencies and returns a spectrogram containing `fft_length // 2 + 1` * frequency bins. If `false`, also computes the negative frequencies and returns `fft_length` frequency bins. * @param {number} [options.preemphasis=null] Coefficient for a low-pass filter that applies pre-emphasis before the DFT. * @param {number[][]} [options.mel_filters=null] The mel filter bank of shape `(num_freq_bins, num_mel_filters)`. * If supplied, applies this filter bank to create a mel spectrogram. * @param {number} [options.mel_floor=1e-10] Minimum value of mel frequency banks. * @param {string} [options.log_mel=null] How to convert the spectrogram to log scale. Possible options are: * `null` (don't convert), `"log"` (take the natural logarithm) `"log10"` (take the base-10 logarithm), `"dB"` (convert to decibels). * Can only be used when `power` is not `null`. * @param {number} [options.reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. For example, use `max(spectrogram)[0]` to set * the loudest part to 0 dB. Must be greater than zero. * @param {number} [options.min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, to avoid taking `log(0)`. * For a power spectrogram, the default of `1e-10` corresponds to a minimum of -100 dB. For an amplitude spectrogram, the value `1e-5` corresponds to -100 dB. * Must be greater than zero. * @param {number} [options.db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the difference between the * peak value and the smallest value will never be more than 80 dB. Must be greater than zero. * @param {boolean} [options.remove_dc_offset=null] Subtract mean from waveform on each frame, applied before pre-emphasis. This should be set to `true` in * order to get the same results as `torchaudio.compliance.kaldi.fbank` when computing mel filters. * @param {number} [options.max_num_frames=null] If provided, limits the number of frames to compute to this value. * @param {boolean} [options.do_pad=true] If `true`, pads the output spectrogram to have `max_num_frames` frames. * @param {boolean} [options.transpose=false] If `true`, the returned spectrogram will have shape `(num_frames, num_frequency_bins/num_mel_filters)`. If `false`, the returned spectrogram will have shape `(num_frequency_bins/num_mel_filters, num_frames)`. * @returns {{data: Float32Array, dims: number[]}} Spectrogram of shape `(num_frequency_bins, length)` (regular spectrogram) or shape `(num_mel_filters, length)` (mel spectrogram). */ function spectrogram( waveform, window, frame_length, hop_length, { fft_length = null, power = 1.0, center = true, pad_mode = "reflect", onesided = true, preemphasis = null, mel_filters = null, mel_floor = 1e-10, log_mel = null, reference = 1.0, min_value = 1e-10, db_range = null, remove_dc_offset = null, // Custom parameters for efficiency reasons max_num_frames = null, do_pad = true, transpose = false, } = {} ) { const window_length = window.length; if (fft_length === null) { fft_length = frame_length; } if (frame_length > fft_length) { throw Error(`frame_length (${frame_length}) may not be larger than fft_length (${fft_length})`) } if (window_length !== frame_length) { throw new Error(`Length of the window (${window_length}) must equal frame_length (${frame_length})`); } if (hop_length <= 0) { throw new Error("hop_length must be greater than zero"); } if (center) { if (pad_mode !== 'reflect') { throw new Error(`pad_mode="${pad_mode}" not implemented yet.`) } const half_window = Math.floor((fft_length - 1) / 2) + 1; waveform = padReflect(waveform, half_window, half_window); } // split waveform into frames of frame_length size const num_frames = Math.floor(1 + Math.floor((waveform.length - frame_length) / hop_length)) const num_frequency_bins = onesided ? Math.floor(fft_length / 2) + 1 : fft_length let d1 = num_frames; let d1Max = num_frames; // If maximum number of frames is provided, we must either pad or truncate if (max_num_frames !== null) { if (max_num_frames > num_frames) { // input is too short, so we pad if (do_pad) { d1Max = max_num_frames; } } else { // input is too long, so we truncate d1Max = d1 = max_num_frames; } } // Preallocate arrays to store output. const fft = new _maths_js__WEBPACK_IMPORTED_MODULE_1__.FFT(fft_length); const inputBuffer = new Float64Array(fft_length); const outputBuffer = new Float64Array(fft.outputBufferSize); const magnitudes = new Array(d1); for (let i = 0; i < d1; ++i) { // Populate buffer with waveform data const offset = i * hop_length; for (let j = 0; j < frame_length; ++j) { inputBuffer[j] = waveform[offset + j]; } if (remove_dc_offset) { let sum = 0; for (let j = 0; j < frame_length; ++j) { sum += inputBuffer[j]; } const mean = sum / frame_length; for (let j = 0; j < frame_length; ++j) { inputBuffer[j] -= mean; } } if (preemphasis !== null) { // Done in reverse to avoid copies and distructive modification for (let j = frame_length - 1; j >= 1; --j) { inputBuffer[j] -= preemphasis * inputBuffer[j - 1]; } inputBuffer[0] *= 1 - preemphasis; } for (let j = 0; j < window.length; ++j) { inputBuffer[j] *= window[j]; } fft.realTransform(outputBuffer, inputBuffer); // compute magnitudes const row = new Array(num_frequency_bins); for (let j = 0; j < row.length; ++j) { const j2 = j << 1; row[j] = outputBuffer[j2] ** 2 + outputBuffer[j2 + 1] ** 2; } magnitudes[i] = row; } // TODO what should happen if power is None? // https://github.com/huggingface/transformers/issues/27772 if (power !== null && power !== 2) { // slight optimization to not sqrt const pow = 2 / power; // we use 2 since we already squared for (let i = 0; i < magnitudes.length; ++i) { const magnitude = magnitudes[i]; for (let j = 0; j < magnitude.length; ++j) { magnitude[j] **= pow; } } } // TODO: What if `mel_filters` is null? const num_mel_filters = mel_filters.length; // Only here do we create Float32Array const mel_spec = new Float32Array(num_mel_filters * d1Max); // Perform matrix muliplication: // mel_spec = mel_filters @ magnitudes.T // - mel_filters.shape=(80, 201) // - magnitudes.shape=(3000, 201) => - magnitudes.T.shape=(201, 3000) // - mel_spec.shape=(80, 3000) const dims = transpose ? [d1Max, num_mel_filters] : [num_mel_filters, d1Max]; for (let i = 0; i < num_mel_filters; ++i) { // num melfilters (e.g., 80) const filter = mel_filters[i]; for (let j = 0; j < d1; ++j) { // num frames (e.g., 3000) const magnitude = magnitudes[j]; let sum = 0; for (let k = 0; k < num_frequency_bins; ++k) { // num frequency bins (e.g., 201) sum += filter[k] * magnitude[k]; } mel_spec[ transpose ? j * num_mel_filters + i : i * d1 + j ] = Math.max(mel_floor, sum); } } if (power !== null && log_mel !== null) { const o = Math.min(mel_spec.length, d1 * num_mel_filters); switch (log_mel) { case 'log': for (let i = 0; i < o; ++i) { mel_spec[i] = Math.log(mel_spec[i]); } break; case 'log10': for (let i = 0; i < o; ++i) { mel_spec[i] = Math.log10(mel_spec[i]); } break; case 'dB': if (power === 1.0) { // NOTE: operates in-place amplitude_to_db(mel_spec, reference, min_value, db_range); } else if (power === 2.0) { power_to_db(mel_spec, reference, min_value, db_range); } else { throw new Error(`Cannot use log_mel option '${log_mel}' with power ${power}`) } break; default: throw new Error(`log_mel must be one of null, 'log', 'log10' or 'dB'. Got '${log_mel}'`); } } return { data: mel_spec, dims }; } /** * Returns an array containing the specified window. * @param {number} window_length The length of the window in samples. * @param {string} name The name of the window function. * @param {Object} options Additional options. * @param {boolean} [options.periodic=true] Whether the window is periodic or symmetric. * @param {number} [options.frame_length=null] The length of the analysis frames in samples. * Provide a value for `frame_length` if the window is smaller than the frame length, so that it will be zero-padded. * @param {boolean} [options.center=true] Whether to center the window inside the FFT buffer. Only used when `frame_length` is provided. * @returns {Float64Array} The window of shape `(window_length,)` or `(frame_length,)`. */ function window_function(window_length, name, { periodic = true, frame_length = null, center = true, } = {}) { const length = periodic ? window_length + 1 : window_length; let window; switch (name) { case 'boxcar': window = new Float64Array(length).fill(1.0); break; case 'hann': case 'hann_window': window = hanning(length); break; case 'povey': window = hanning(length).map(x => Math.pow(x, 0.85)); break; default: throw new Error(`Unknown window type ${name}.`); } if (periodic) { window = window.subarray(0, window_length); } if (frame_length === null) { return window; } if (window_length > frame_length) { throw new Error(`Length of the window (${window_length}) may not be larger than frame_length (${frame_length})`); } return window; } /***/ }), /***/ "./src/utils/core.js": /*!***************************!*\ !*** ./src/utils/core.js ***! \***************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "calculateDimensions": () => (/* binding */ calculateDimensions), /* harmony export */ "calculateReflectOffset": () => (/* binding */ calculateReflectOffset), /* harmony export */ "dispatchCallback": () => (/* binding */ dispatchCallback), /* harmony export */ "escapeRegExp": () => (/* binding */ escapeRegExp), /* harmony export */ "isIntegralNumber": () => (/* binding */ isIntegralNumber), /* harmony export */ "isTypedArray": () => (/* binding */ isTypedArray), /* harmony export */ "mergeArrays": () => (/* binding */ mergeArrays), /* harmony export */ "pick": () => (/* binding */ pick), /* harmony export */ "pop": () => (/* binding */ pop), /* harmony export */ "product": () => (/* binding */ product), /* harmony export */ "reverseDictionary": () => (/* binding */ reverseDictionary) /* harmony export */ }); /** * @file Core utility functions/classes for Transformers.js. * * These are only used internally, meaning an end-user shouldn't * need to access anything here. * * @module utils/core */ /** * Helper function to dispatch progress callbacks. * * @param {Function} progress_callback The progress callback function to dispatch. * @param {any} data The data to pass to the progress callback function. * @returns {void} * @private */ function dispatchCallback(progress_callback, data) { if (progress_callback) progress_callback(data); } /** * Reverses the keys and values of an object. * * @param {Object} data The object to reverse. * @returns {Object} The reversed object. * @see https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript */ function reverseDictionary(data) { // https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript return Object.fromEntries(Object.entries(data).map(([key, value]) => [value, key])); } /** * Escapes regular expression special characters from a string by replacing them with their escaped counterparts. * * @param {string} string The string to escape. * @returns {string} The escaped string. */ function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } /** * Check if a value is a typed array. * @param {*} val The value to check. * @returns {boolean} True if the value is a `TypedArray`, false otherwise. * * Adapted from https://stackoverflow.com/a/71091338/13989043 */ function isTypedArray(val) { return val?.prototype?.__proto__?.constructor?.name === 'TypedArray'; } /** * Check if a value is an integer. * @param {*} x The value to check. * @returns {boolean} True if the value is a string, false otherwise. */ function isIntegralNumber(x) { return Number.isInteger(x) || typeof x === 'bigint' } /** * Calculates the dimensions of a nested array. * * @param {any[]} arr The nested array to calculate dimensions for. * @returns {number[]} An array containing the dimensions of the input array. */ function calculateDimensions(arr) { const dimensions = []; let current = arr; while (Array.isArray(current)) { dimensions.push(current.length); current = current[0]; } return dimensions; } /** * Replicate python's .pop() method for objects. * @param {Object} obj The object to pop from. * @param {string} key The key to pop. * @param {*} defaultValue The default value to return if the key does not exist. * @returns {*} The value of the popped key. * @throws {Error} If the key does not exist and no default value is provided. */ function pop(obj, key, defaultValue = undefined) { const value = obj[key]; if (value !== undefined) { delete obj[key]; return value; } if (defaultValue === undefined) { throw Error(`Key ${key} does not exist in object.`) } return defaultValue; } /** * Efficiently merge arrays, creating a new copy. * Adapted from https://stackoverflow.com/a/6768642/13989043 * @param {Array[]} arrs Arrays to merge. * @returns {Array} The merged array. */ function mergeArrays(...arrs) { return Array.prototype.concat.apply([], arrs); } /** * Compute the Cartesian product of given arrays * @param {...Array} a Arrays to compute the product * @returns {Array} Returns the computed Cartesian product as an array * @private */ function product(...a) { // Cartesian product of items // Adapted from https://stackoverflow.com/a/43053803 return a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e]))); } /** * Calculates the index offset for a given index and window size. * @param {number} i The index. * @param {number} w The window size. * @returns {number} The index offset. */ function calculateReflectOffset(i, w) { return Math.abs((i + w) % (2 * w) - w); } /** * * @param {Object} o * @param {string[]} props * @returns {Object} */ function pick(o, props) { return Object.assign( {}, ...props.map((prop) => { if (o[prop] !== undefined) { return { [prop]: o[prop] }; } }) ); } /***/ }), /***/ "./src/utils/data-structures.js": /*!**************************************!*\ !*** ./src/utils/data-structures.js ***! \**************************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "CharTrie": () => (/* binding */ CharTrie), /* harmony export */ "PriorityQueue": () => (/* binding */ PriorityQueue), /* harmony export */ "TokenLattice": () => (/* binding */ TokenLattice) /* harmony export */ }); /** * @file Custom data structures. * * These are only used internally, meaning an end-user shouldn't * need to access anything here. * * @module utils/data-structures */ /** * Efficient Heap-based Implementation of a Priority Queue. * It uses an array-based binary heap, where the root is at index `0`, and the * children of node `i` are located at indices `2i + 1` and `2i + 2`, respectively. * * Adapted from the following sources: * - https://stackoverflow.com/a/42919752/13989043 (original) * - https://github.com/belladoreai/llama-tokenizer-js (minor improvements) */ class PriorityQueue { /** * Create a new PriorityQueue. * @param {Function} comparator Comparator function to determine priority. Defaults to a MaxHeap. */ constructor(comparator = (a, b) => a > b) { this._heap = []; this._comparator = comparator; } /** * The size of the queue */ get size() { return this._heap.length; } /** * Check if the queue is empty. * @returns {boolean} `true` if the queue is empty, `false` otherwise. */ isEmpty() { return this.size === 0; } /** * Return the element with the highest priority in the queue. * @returns {any} The highest priority element in the queue. */ peek() { return this._heap[0]; } /** * Add one or more elements to the queue. * @param {...any} values The values to push into the queue. * @returns {number} The new size of the queue. */ push(...values) { return this.extend(values); } /** * Add multiple elements to the queue. * @param {any[]} values The values to push into the queue. * @returns {number} The new size of the queue. */ extend(values) { for (const value of values) { this._heap.push(value); this._siftUp(); } return this.size; } /** * Remove and return the element with the highest priority in the queue. * @returns {any} The element with the highest priority in the queue. */ pop() { const poppedValue = this.peek(); const bottom = this.size - 1; if (bottom > 0) { this._swap(0, bottom); } this._heap.pop(); this._siftDown(); return poppedValue; } /** * Replace the element with the highest priority in the queue with a new value. * @param {*} value The new value. * @returns {*} The replaced value. */ replace(value) { const replacedValue = this.peek(); this._heap[0] = value; this._siftDown(); return replacedValue; } /** * Compute the index for the parent of the node at index `i`. * @param {number} i The index of the node to get the parent of. * @returns {number} The index of the parent node. * @private */ _parent(i) { return ((i + 1) >>> 1) - 1; } /** * Compute the index for the left child of the node at index `i`. * @param {number} i The index of the node to get the left child of. * @returns {number} The index of the left child. * @private */ _left(i) { return (i << 1) + 1; } /** * Compute the index for the right child of the node at index `i`. * @param {number} i The index of the node to get the right child of. * @returns {number} The index of the right child. * @private */ _right(i) { return (i + 1) << 1; } /** * Check if the element at index `i` is greater than the element at index `j`. * @param {number} i The index of the first element to compare. * @param {number} j The index of the second element to compare. * @returns {boolean} `true` if the element at index `i` is greater than the element at index `j`, `false` otherwise. * @private */ _greater(i, j) { return this._comparator(this._heap[i], this._heap[j]); } /** * Swap the elements at indices `i` and `j`. * @param {number} i The index of the first element to swap. * @param {number} j The index of the second element to swap. * @private */ _swap(i, j) { const temp = this._heap[i]; this._heap[i] = this._heap[j]; this._heap[j] = temp; } /** * Maintain the heap property by updating positions in the heap, * starting at the last element and moving up the heap. * @private */ _siftUp() { let node = this.size - 1; while (node > 0 && this._greater(node, this._parent(node))) { this._swap(node, this._parent(node)); node = this._parent(node); } } /** * Maintain the heap property by updating positions in the heap, * starting at the first element and moving down the heap. * @private */ _siftDown() { let node = 0; while ( (this._left(node) < this.size && this._greater(this._left(node), node)) || (this._right(node) < this.size && this._greater(this._right(node), node)) ) { const maxChild = (this._right(node) < this.size && this._greater(this._right(node), this._left(node))) ? this._right(node) : this._left(node); this._swap(node, maxChild); node = maxChild; } } } /** * A trie structure to efficiently store and search for strings. */ class CharTrie { constructor() { this.root = CharTrieNode.default(); } /** * Adds one or more `texts` to the trie. * @param {string[]} texts The strings to add to the trie. */ extend(texts) { for (let text of texts) { this.push(text); } } /** * Adds text to the trie. * @param {string} text The string to add to the trie. */ push(text) { let node = this.root; for (let ch of text) { let child = node.children.get(ch); if (child === undefined) { child = CharTrieNode.default(); node.children.set(ch, child); } node = child; } node.isLeaf = true; } /** * Searches the trie for all strings with a common prefix of `text`. * @param {string} text The common prefix to search for. * @yields {string} Each string in the trie that has `text` as a prefix. */ *commonPrefixSearch(text) { let node = this.root; let prefix = ""; for (let i = 0; i < text.length && node !== undefined; ++i) { const ch = text[i]; prefix += ch; node = node.children.get(ch); if (node !== undefined && node.isLeaf) { yield prefix; } } } } /** * Represents a node in a character trie. */ class CharTrieNode { /** * Create a new CharTrieNode. * @param {boolean} isLeaf Whether the node is a leaf node or not. * @param {Map} children A map containing the node's children, where the key is a character and the value is a `CharTrieNode`. */ constructor(isLeaf, children) { this.isLeaf = isLeaf; this.children = children; } /** * Returns a new `CharTrieNode` instance with default values. * @returns {CharTrieNode} A new `CharTrieNode` instance with `isLeaf` set to `false` and an empty `children` map. */ static default() { return new CharTrieNode(false, new Map()); } } /** * A lattice data structure to be used for tokenization. */ class TokenLattice { /** * Creates a new TokenLattice instance. * * @param {string} sentence The input sentence to be tokenized. * @param {number} bosTokenId The beginning-of-sequence token ID. * @param {number} eosTokenId The end-of-sequence token ID. */ constructor(sentence, bosTokenId, eosTokenId) { this.sentence = sentence; this.len = sentence.length; this.bosTokenId = bosTokenId; this.eosTokenId = eosTokenId; this.nodes = []; this.beginNodes = Array.from({ length: this.len + 1 }, () => []); this.endNodes = Array.from({ length: this.len + 1 }, () => []); const bos = new TokenLatticeNode(this.bosTokenId, 0, 0, 0, 0.0); const eos = new TokenLatticeNode(this.eosTokenId, 1, this.len, 0, 0.0); this.nodes.push(bos.clone()); this.nodes.push(eos.clone()); this.beginNodes[this.len].push(eos); this.endNodes[0].push(bos); } /** * Inserts a new token node into the token lattice. * * @param {number} pos The starting position of the token. * @param {number} length The length of the token. * @param {number} score The score of the token. * @param {number} tokenId The token ID of the token. */ insert(pos, length, score, tokenId) { const nodeId = this.nodes.length; const node = new TokenLatticeNode(tokenId, nodeId, pos, length, score); this.beginNodes[pos].push(node); this.endNodes[pos + length].push(node); this.nodes.push(node); } /** * Implements the Viterbi algorithm to compute the most likely sequence of tokens. * * @returns {TokenLatticeNode[]} The array of nodes representing the most likely sequence of tokens. */ viterbi() { const len = this.len; let pos = 0; while (pos <= len) { if (this.beginNodes[pos].length == 0) { return []; } for (let rnode of this.beginNodes[pos]) { rnode.prev = null; let bestScore = 0.0; let bestNode = null; for (let lnode of this.endNodes[pos]) { const score = lnode.backtraceScore + rnode.score; if (bestNode === null || score > bestScore) { bestNode = lnode.clone(); bestScore = score; } } if (bestNode !== null) { rnode.prev = bestNode; rnode.backtraceScore = bestScore; } else { return []; } } ++pos; } const results = []; const root = this.beginNodes[len][0]; const prev = root.prev; if (prev === null) { return []; } let node = prev.clone(); while (node.prev !== null) { results.push(node.clone()); const n = node.clone(); node = n.prev.clone(); } results.reverse(); return results; } /** * @param {TokenLatticeNode} node * @returns {string} The array of nodes representing the most likely sequence of tokens. */ piece(node) { return this.sentence.slice(node.pos, node.pos + node.length); } /** * @returns {Array} The array of nodes representing the most likely sequence of tokens. */ tokens() { const nodes = this.viterbi(); return nodes.map(x => this.piece(x)); } /** * @returns {Array} The array of nodes representing the most likely sequence of tokens. */ tokenIds() { const nodes = this.viterbi(); return nodes.map(x => x.tokenId); } } class TokenLatticeNode { /** * Represents a node in a token lattice for a given sentence. * @param {number} tokenId The ID of the token associated with this node. * @param {number} nodeId The ID of this node. * @param {number} pos The starting position of the token in the sentence. * @param {number} length The length of the token. * @param {number} score The score associated with the token. */ constructor(tokenId, nodeId, pos, length, score) { this.tokenId = tokenId; this.nodeId = nodeId; this.pos = pos; this.length = length; this.score = score; this.prev = null; this.backtraceScore = 0.0; } /** * Returns a clone of this node. * @returns {TokenLatticeNode} A clone of this node. */ clone() { const n = new TokenLatticeNode(this.tokenId, this.nodeId, this.pos, this.length, this.score); n.prev = this.prev; n.backtraceScore = this.backtraceScore; return n; } } /***/ }), /***/ "./src/utils/devices.js": /*!******************************!*\ !*** ./src/utils/devices.js ***! \******************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "DEVICE_TYPES": () => (/* binding */ DEVICE_TYPES) /* harmony export */ }); const DEVICE_TYPES = Object.freeze({ cpu: 'cpu', gpu: 'gpu', wasm: 'wasm', webgpu: 'webgpu', }); /** * @typedef {keyof typeof DEVICE_TYPES} DeviceType */ /***/ }), /***/ "./src/utils/dtypes.js": /*!*****************************!*\ !*** ./src/utils/dtypes.js ***! \*****************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "DATA_TYPES": () => (/* binding */ DATA_TYPES), /* harmony export */ "DEFAULT_DEVICE_DTYPE_MAPPING": () => (/* binding */ DEFAULT_DEVICE_DTYPE_MAPPING), /* harmony export */ "DEFAULT_DTYPE_SUFFIX_MAPPING": () => (/* binding */ DEFAULT_DTYPE_SUFFIX_MAPPING), /* harmony export */ "isFp16Supported": () => (/* binding */ isFp16Supported) /* harmony export */ }); /* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); /* harmony import */ var _devices_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./devices.js */ "./src/utils/devices.js"); // TODO: Use the adapter from `env.backends.onnx.webgpu.adapter` to check for `shader-f16` support, // when available in https://github.com/microsoft/onnxruntime/pull/19940. // For more information, see https://github.com/microsoft/onnxruntime/pull/19857#issuecomment-1999984753 /** * Checks if fp16 support is available in the current environment. */ const isFp16Supported = (function () { /** @type {boolean} */ let cachedResult; return async function () { if (cachedResult === undefined) { if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_NODE_ENV) { cachedResult = true; } else if (!_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBGPU_AVAILABLE) { cachedResult = false; } else { try { const adapter = await navigator.gpu.requestAdapter(); cachedResult = adapter.features.has('shader-f16'); } catch (e) { cachedResult = false; } } } return cachedResult; }; })(); const DATA_TYPES = Object.freeze({ fp32: 'fp32', fp16: 'fp16', q8: 'q8', int8: 'int8', uint8: 'uint8', }); /** @typedef {keyof typeof DATA_TYPES} DataType */ const DEFAULT_DEVICE_DTYPE_MAPPING = Object.freeze({ [_devices_js__WEBPACK_IMPORTED_MODULE_1__.DEVICE_TYPES.cpu]: DATA_TYPES.q8, [_devices_js__WEBPACK_IMPORTED_MODULE_1__.DEVICE_TYPES.gpu]: DATA_TYPES.fp32, [_devices_js__WEBPACK_IMPORTED_MODULE_1__.DEVICE_TYPES.wasm]: DATA_TYPES.q8, [_devices_js__WEBPACK_IMPORTED_MODULE_1__.DEVICE_TYPES.webgpu]: DATA_TYPES.fp32, }); /** @type {Record} */ const DEFAULT_DTYPE_SUFFIX_MAPPING = Object.freeze({ [DATA_TYPES.fp32]: '', [DATA_TYPES.fp16]: '_fp16', [DATA_TYPES.int8]: '_int8', [DATA_TYPES.uint8]: '_uint8', [DATA_TYPES.q8]: '_quantized', }); /***/ }), /***/ "./src/utils/generic.js": /*!******************************!*\ !*** ./src/utils/generic.js ***! \******************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "Callable": () => (/* binding */ Callable) /* harmony export */ }); /** * A base class for creating callable objects. * See [here](https://stackoverflow.com/q/76073890) for more information. * * @type {new () => {(...args: any[]): any, _call(...args: any[]): any}} */ const Callable = /** @type {any} */ (class { /** * Creates a new instance of the Callable class. */ constructor() { /** * Creates a closure that delegates to a private method '_call' with the given arguments. * @type {any} * @param {...any} args Zero or more arguments to pass to the '_call' method. * @returns {*} The result of calling the '_call' method. */ let closure = function (...args) { return closure._call(...args) } return Object.setPrototypeOf(closure, new.target.prototype) } /** * This method should be implemented in subclasses to provide the * functionality of the callable object. * * @param {any[]} args * @throws {Error} If the subclass does not implement the `_call` method. */ _call(...args) { throw Error('Must implement _call method in subclass') } }); /***/ }), /***/ "./src/utils/hub.js": /*!**************************!*\ !*** ./src/utils/hub.js ***! \**************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "getFile": () => (/* binding */ getFile), /* harmony export */ "getModelFile": () => (/* binding */ getModelFile), /* harmony export */ "getModelJSON": () => (/* binding */ getModelJSON) /* harmony export */ }); /* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! fs */ "?7a2c"); /* harmony import */ var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! path */ "?a42a"); /* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); /* harmony import */ var _core_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./core.js */ "./src/utils/core.js"); /** * @file Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models) * * @module utils/hub */ /** * @typedef {Object} PretrainedOptions Options for loading a pretrained model. * @property {function} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates. * @property {Object} [config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when: * - The model is a model provided by the library (loaded with the *model id* string of a pretrained model). * - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a configuration JSON file named *config.json* is found in the directory. * @property {string} [cache_dir=null] Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used. * @property {boolean} [local_files_only=false] Whether or not to only look at local files (e.g., not try downloading the model). * @property {string} [revision='main'] The specific model version to use. It can be a branch name, a tag name, or a commit id, * since we use a git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier allowed by git. * NOTE: This setting is ignored for local requests. */ /** * @typedef {Object} ModelSpecificPretrainedOptions Options for loading a pretrained model. * @property {string} [subfolder='onnx'] In case the relevant files are located inside a subfolder of the model repo on huggingface.co, * you can specify the folder name here. * @property {string} [model_file_name=null] If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models. * @property {import("./devices.js").DeviceType|Record} [device=null] The device to run the model on. If not specified, the device will be chosen from the environment settings. * @property {import("./dtypes.js").DataType|Record} [dtype=null] The data type to use for the model. If not specified, the data type will be chosen from the environment settings. * @property {Object} [session_options] (Optional) User-specified session options passed to the runtime. If not provided, suitable defaults will be chosen. */ /** * @typedef {PretrainedOptions & ModelSpecificPretrainedOptions} PretrainedModelOptions Options for loading a pretrained model. */ class FileResponse { /** * Mapping from file extensions to MIME types. */ _CONTENT_TYPE_MAP = { 'txt': 'text/plain', 'html': 'text/html', 'css': 'text/css', 'js': 'text/javascript', 'json': 'application/json', 'png': 'image/png', 'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'gif': 'image/gif', } /** * Creates a new `FileResponse` object. * @param {string|URL} filePath */ constructor(filePath) { this.filePath = filePath; this.headers = new Headers(); this.exists = fs__WEBPACK_IMPORTED_MODULE_0__.existsSync(filePath); if (this.exists) { this.status = 200; this.statusText = 'OK'; let stats = fs__WEBPACK_IMPORTED_MODULE_0__.statSync(filePath); this.headers.set('content-length', stats.size.toString()); this.updateContentType(); let self = this; this.body = new ReadableStream({ start(controller) { self.arrayBuffer().then(buffer => { controller.enqueue(new Uint8Array(buffer)); controller.close(); }) } }); } else { this.status = 404; this.statusText = 'Not Found'; this.body = null; } } /** * Updates the 'content-type' header property of the response based on the extension of * the file specified by the filePath property of the current object. * @returns {void} */ updateContentType() { // Set content-type header based on file extension const extension = this.filePath.toString().split('.').pop().toLowerCase(); this.headers.set('content-type', this._CONTENT_TYPE_MAP[extension] ?? 'application/octet-stream'); } /** * Clone the current FileResponse object. * @returns {FileResponse} A new FileResponse object with the same properties as the current object. */ clone() { let response = new FileResponse(this.filePath); response.exists = this.exists; response.status = this.status; response.statusText = this.statusText; response.headers = new Headers(this.headers); return response; } /** * Reads the contents of the file specified by the filePath property and returns a Promise that * resolves with an ArrayBuffer containing the file's contents. * @returns {Promise} A Promise that resolves with an ArrayBuffer containing the file's contents. * @throws {Error} If the file cannot be read. */ async arrayBuffer() { const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath); return data.buffer; } /** * Reads the contents of the file specified by the filePath property and returns a Promise that * resolves with a Blob containing the file's contents. * @returns {Promise} A Promise that resolves with a Blob containing the file's contents. * @throws {Error} If the file cannot be read. */ async blob() { const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath); return new Blob([data], { type: this.headers.get('content-type') }); } /** * Reads the contents of the file specified by the filePath property and returns a Promise that * resolves with a string containing the file's contents. * @returns {Promise} A Promise that resolves with a string containing the file's contents. * @throws {Error} If the file cannot be read. */ async text() { const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath, 'utf8'); return data; } /** * Reads the contents of the file specified by the filePath property and returns a Promise that * resolves with a parsed JavaScript object containing the file's contents. * * @returns {Promise} A Promise that resolves with a parsed JavaScript object containing the file's contents. * @throws {Error} If the file cannot be read. */ async json() { return JSON.parse(await this.text()); } } /** * Determines whether the given string is a valid HTTP or HTTPS URL. * @param {string|URL} string The string to test for validity as an HTTP or HTTPS URL. * @param {string[]} [validHosts=null] A list of valid hostnames. If specified, the URL's hostname must be in this list. * @returns {boolean} True if the string is a valid HTTP or HTTPS URL, false otherwise. */ function isValidHttpUrl(string, validHosts = null) { // https://stackoverflow.com/a/43467144 let url; try { url = new URL(string); } catch (_) { return false; } if (validHosts && !validHosts.includes(url.hostname)) { return false; } return url.protocol === "http:" || url.protocol === "https:"; } /** * Helper function to get a file, using either the Fetch API or FileSystem API. * * @param {URL|string} urlOrPath The URL/path of the file to get. * @returns {Promise} A promise that resolves to a FileResponse object (if the file is retrieved using the FileSystem API), or a Response object (if the file is retrieved using the Fetch API). */ async function getFile(urlOrPath) { if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFS && !isValidHttpUrl(urlOrPath)) { return new FileResponse(urlOrPath); } else if (typeof process !== 'undefined' && process?.release?.name === 'node') { const IS_CI = !!process.env?.TESTING_REMOTELY; const version = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.version; const headers = new Headers(); headers.set('User-Agent', `transformers.js/${version}; is_ci/${IS_CI};`); // Check whether we are making a request to the Hugging Face Hub. const isHFURL = isValidHttpUrl(urlOrPath, ['huggingface.co', 'hf.co']); if (isHFURL) { // If an access token is present in the environment variables, // we add it to the request headers. // NOTE: We keep `HF_ACCESS_TOKEN` for backwards compatibility (as a fallback). const token = process.env?.HF_TOKEN ?? process.env?.HF_ACCESS_TOKEN; if (token) { headers.set('Authorization', `Bearer ${token}`); } } return fetch(urlOrPath, { headers }); } else { // Running in a browser-environment, so we use default headers // NOTE: We do not allow passing authorization headers in the browser, // since this would require exposing the token to the client. return fetch(urlOrPath); } } const ERROR_MAPPING = { // 4xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses) 400: 'Bad request error occurred while trying to load file', 401: 'Unauthorized access to file', 403: 'Forbidden access to file', 404: 'Could not locate file', 408: 'Request timeout error occurred while trying to load file', // 5xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses) 500: 'Internal server error error occurred while trying to load file', 502: 'Bad gateway error occurred while trying to load file', 503: 'Service unavailable error occurred while trying to load file', 504: 'Gateway timeout error occurred while trying to load file', } /** * Helper method to handle fatal errors that occur while trying to load a file from the Hugging Face Hub. * @param {number} status The HTTP status code of the error. * @param {string} remoteURL The URL of the file that could not be loaded. * @param {boolean} fatal Whether to raise an error if the file could not be loaded. * @returns {null} Returns `null` if `fatal = true`. * @throws {Error} If `fatal = false`. */ function handleError(status, remoteURL, fatal) { if (!fatal) { // File was not loaded correctly, but it is optional. // TODO in future, cache the response? return null; } const message = ERROR_MAPPING[status] ?? `Error (${status}) occurred while trying to load file`; throw Error(`${message}: "${remoteURL}".`); } class FileCache { /** * Instantiate a `FileCache` object. * @param {string} path */ constructor(path) { this.path = path; } /** * Checks whether the given request is in the cache. * @param {string} request * @returns {Promise} */ async match(request) { let filePath = path__WEBPACK_IMPORTED_MODULE_1__.join(this.path, request); let file = new FileResponse(filePath); if (file.exists) { return file; } else { return undefined; } } /** * Adds the given response to the cache. * @param {string} request * @param {Response|FileResponse} response * @returns {Promise} */ async put(request, response) { const buffer = Buffer.from(await response.arrayBuffer()); let outputPath = path__WEBPACK_IMPORTED_MODULE_1__.join(this.path, request); try { await fs__WEBPACK_IMPORTED_MODULE_0__.promises.mkdir(path__WEBPACK_IMPORTED_MODULE_1__.dirname(outputPath), { recursive: true }); await fs__WEBPACK_IMPORTED_MODULE_0__.promises.writeFile(outputPath, buffer); } catch (err) { console.warn('An error occurred while writing the file to cache:', err) } } // TODO add the rest? // addAll(requests: RequestInfo[]): Promise; // delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; // keys(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; // match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; // matchAll(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; } /** * * @param {FileCache|Cache} cache The cache to search * @param {string[]} names The names of the item to search for * @returns {Promise} The item from the cache, or undefined if not found. */ async function tryCache(cache, ...names) { for (let name of names) { try { let result = await cache.match(name); if (result) return result; } catch (e) { continue; } } return undefined; } /** * * Retrieves a file from either a remote URL using the Fetch API or from the local file system using the FileSystem API. * If the filesystem is available and `env.useCache = true`, the file will be downloaded and cached. * * @param {string} path_or_repo_id This can be either: * - a string, the *model id* of a model repo on huggingface.co. * - a path to a *directory* potentially containing the file. * @param {string} filename The name of the file to locate in `path_or_repo`. * @param {boolean} [fatal=true] Whether to throw an error if the file is not found. * @param {PretrainedOptions} [options] An object containing optional parameters. * * @throws Will throw an error if the file is not found and `fatal` is true. * @returns {Promise} A Promise that resolves with the file content as a buffer. */ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) { if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { // User has disabled local models, so we just make sure other settings are correct. if (options.local_files_only) { throw Error("Invalid configuration detected: local models are disabled (`env.allowLocalModels=false`) but you have requested to only use local models (`local_files_only=true`).") } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { throw Error("Invalid configuration detected: both local and remote models are disabled. Fix by setting `env.allowLocalModels` or `env.allowRemoteModels` to `true`.") } } // Initiate file retrieval (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { status: 'initiate', name: path_or_repo_id, file: filename }) // First, check if the a caching backend is available // If no caching mechanism available, will download the file every time let cache; if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useBrowserCache) { if (typeof caches === 'undefined') { throw Error('Browser cache is not available in this environment.') } try { // In some cases, the browser cache may be visible, but not accessible due to security restrictions. // For example, when running an application in an iframe, if a user attempts to load the page in // incognito mode, the following error is thrown: `DOMException: Failed to execute 'open' on 'CacheStorage': // An attempt was made to break through the security policy of the user agent.` // So, instead of crashing, we just ignore the error and continue without using the cache. cache = await caches.open('transformers-cache'); } catch (e) { console.warn('An error occurred while opening the browser cache:', e); } } if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFSCache) { // TODO throw error if not available // If `cache_dir` is not specified, use the default cache directory cache = new FileCache(options.cache_dir ?? _env_js__WEBPACK_IMPORTED_MODULE_2__.env.cacheDir); } if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) { // Allow the user to specify a custom cache system. if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) { throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.') } // Check that the required methods are defined: if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) { throw new Error( "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " + "For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache" ) } cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache; } const revision = options.revision ?? 'main'; let requestURL = pathJoin(path_or_repo_id, filename); let localPath = pathJoin(_env_js__WEBPACK_IMPORTED_MODULE_2__.env.localModelPath, requestURL); let remoteURL = pathJoin( _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remoteHost, _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remotePathTemplate.replaceAll('{model}', path_or_repo_id) .replaceAll('{revision}', encodeURIComponent(revision)), filename ); // Choose cache key for filesystem cache // When using the main revision (default), we use the request URL as the cache key. // If a specific revision is requested, we account for this in the cache key. let fsCacheKey = revision === 'main' ? requestURL : pathJoin(path_or_repo_id, revision, filename); /** @type {string} */ let cacheKey; let proposedCacheKey = cache instanceof FileCache ? fsCacheKey : remoteURL; // Whether to cache the final response in the end. let toCacheResponse = false; /** @type {Response|FileResponse|undefined} */ let response; if (cache) { // A caching system is available, so we try to get the file from it. // 1. We first try to get from cache using the local path. In some environments (like deno), // non-URL cache keys are not allowed. In these cases, `response` will be undefined. // 2. If no response is found, we try to get from cache using the remote URL or file system cache. response = await tryCache(cache, localPath, proposedCacheKey); } const cacheHit = response !== undefined; if (response === undefined) { // Caching not available, or file is not cached, so we perform the request if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { // Accessing local models is enabled, so we try to get the file locally. // If request is a valid HTTP URL, we skip the local file check. Otherwise, we try to get the file locally. const isURL = isValidHttpUrl(requestURL); if (!isURL) { try { response = await getFile(localPath); cacheKey = localPath; // Update the cache key to be the local path } catch (e) { // Something went wrong while trying to get the file locally. // NOTE: error handling is done in the next step (since `response` will be undefined) console.warn(`Unable to load from local path "${localPath}": "${e}"`); } } else if (options.local_files_only) { throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`); } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${requestURL}.`); } } if (response === undefined || response.status === 404) { // File not found locally. This means either: // - The user has disabled local file access (`env.allowLocalModels=false`) // - the path is a valid HTTP url (`response === undefined`) // - the path is not a valid HTTP url and the file is not present on the file system or local server (`response.status === 404`) if (options.local_files_only || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { // User requested local files only, but the file is not found locally. if (fatal) { throw Error(`\`local_files_only=true\` or \`env.allowRemoteModels=false\` and file was not found locally at "${localPath}".`); } else { // File not found, but this file is optional. // TODO in future, cache the response? return null; } } // File not found locally, so we try to download it from the remote server response = await getFile(remoteURL); if (response.status !== 200) { return handleError(response.status, remoteURL, fatal); } // Success! We use the proposed cache key from earlier cacheKey = proposedCacheKey; } // Only cache the response if: toCacheResponse = cache // 1. A caching system is available && typeof Response !== 'undefined' // 2. `Response` is defined (i.e., we are in a browser-like environment) && response instanceof Response // 3. result is a `Response` object (i.e., not a `FileResponse`) && response.status === 200 // 4. request was successful (status code 200) } // Start downloading (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { status: 'download', name: path_or_repo_id, file: filename }) const progressInfo = { status: 'progress', name: path_or_repo_id, file: filename } /** @type {Uint8Array} */ let buffer; if (!options.progress_callback) { // If no progress callback is specified, we can use the `.arrayBuffer()` // method to read the response. buffer = new Uint8Array(await response.arrayBuffer()); } else if ( cacheHit // The item is being read from the cache && typeof navigator !== 'undefined' && /firefox/i.test(navigator.userAgent) // We are in Firefox ) { // Due to bug in Firefox, we cannot display progress when loading from cache. // Fortunately, since this should be instantaneous, this should not impact users too much. buffer = new Uint8Array(await response.arrayBuffer()); // For completeness, we still fire the final progress callback (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { ...progressInfo, progress: 100, loaded: buffer.length, total: buffer.length, }) } else { buffer = await readResponse(response, data => { (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { ...progressInfo, ...data, }) }) } if ( // Only cache web responses // i.e., do not cache FileResponses (prevents duplication) toCacheResponse && cacheKey && // Check again whether request is in cache. If not, we add the response to the cache (await cache.match(cacheKey) === undefined) ) { // NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files await cache.put(cacheKey, new Response(buffer, { headers: response.headers })) .catch(err => { // Do not crash if unable to add to cache (e.g., QuotaExceededError). // Rather, log a warning and proceed with execution. console.warn(`Unable to add response to browser cache: ${err}.`); }); } (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { status: 'done', name: path_or_repo_id, file: filename }); return buffer; } /** * Fetches a JSON file from a given path and file name. * * @param {string} modelPath The path to the directory containing the file. * @param {string} fileName The name of the file to fetch. * @param {boolean} [fatal=true] Whether to throw an error if the file is not found. * @param {PretrainedOptions} [options] An object containing optional parameters. * @returns {Promise} The JSON data parsed into a JavaScript object. * @throws Will throw an error if the file is not found and `fatal` is true. */ async function getModelJSON(modelPath, fileName, fatal = true, options = {}) { let buffer = await getModelFile(modelPath, fileName, fatal, options); if (buffer === null) { // Return empty object return {} } let decoder = new TextDecoder('utf-8'); let jsonData = decoder.decode(buffer); return JSON.parse(jsonData); } /** * Read and track progress when reading a Response object * * @param {any} response The Response object to read * @param {function} progress_callback The function to call with progress updates * @returns {Promise} A Promise that resolves with the Uint8Array buffer */ async function readResponse(response, progress_callback) { const contentLength = response.headers.get('Content-Length'); if (contentLength === null) { console.warn('Unable to determine content-length from response headers. Will expand buffer when needed.') } 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; // Adding the new data will overflow buffer. // In this case, we extend the buffer let newBuffer = new Uint8Array(total); // copy contents newBuffer.set(buffer); buffer = newBuffer; } buffer.set(value, loaded) loaded = newLoaded; const progress = (loaded / total) * 100; // Call your function here progress_callback({ progress: progress, loaded: loaded, total: total, }) return read(); } // Actually read await read(); return buffer; } /** * Joins multiple parts of a path into a single path, while handling leading and trailing slashes. * * @param {...string} parts Multiple parts of a path. * @returns {string} A string representing the joined path. */ function pathJoin(...parts) { // https://stackoverflow.com/a/55142565 parts = parts.map((part, index) => { if (index) { part = part.replace(new RegExp('^/'), ''); } if (index !== parts.length - 1) { part = part.replace(new RegExp('/$'), ''); } return part; }) return parts.join('/'); } /***/ }), /***/ "./src/utils/image.js": /*!****************************!*\ !*** ./src/utils/image.js ***! \****************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "RawImage": () => (/* binding */ RawImage) /* harmony export */ }); /* harmony import */ var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./hub.js */ "./src/utils/hub.js"); /* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); /* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./tensor.js */ "./src/utils/tensor.js"); /* harmony import */ var sharp__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! sharp */ "?2b25"); /** * @file Helper module for image processing. * * These functions and classes are only used internally, * meaning an end-user shouldn't need to access anything here. * * @module utils/image */ // Will be empty (or not used) if running in browser or web-worker const BROWSER_ENV = typeof self !== 'undefined'; const WEBWORKER_ENV = BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope'; let createCanvasFunction; let ImageDataClass; let loadImageFunction; if (BROWSER_ENV) { // Running in browser or web-worker createCanvasFunction = (/** @type {number} */ width, /** @type {number} */ height) => { if (!self.OffscreenCanvas) { throw new Error('OffscreenCanvas not supported by this browser.'); } return new self.OffscreenCanvas(width, height) }; loadImageFunction = self.createImageBitmap; ImageDataClass = self.ImageData; } else if (sharp__WEBPACK_IMPORTED_MODULE_3__) { // Running in Node.js, electron, or other non-browser environment loadImageFunction = async (/**@type {sharp.Sharp}*/img) => { const metadata = await img.metadata(); const rawChannels = metadata.channels; const { data, info } = await img.raw().toBuffer({ resolveWithObject: true }); const newImage = new RawImage(new Uint8ClampedArray(data), info.width, info.height, info.channels); if (rawChannels !== undefined && rawChannels !== info.channels) { // Make sure the new image has the same number of channels as the input image. // This is necessary for grayscale images. newImage.convert(rawChannels); } return newImage; } } else { throw new Error('Unable to load image processing library.'); } // Defined here: https://github.com/python-pillow/Pillow/blob/a405e8406b83f8bfb8916e93971edc7407b8b1ff/src/libImaging/Imaging.h#L262-L268 const RESAMPLING_MAPPING = { 0: 'nearest', 1: 'lanczos', 2: 'bilinear', 3: 'bicubic', 4: 'box', 5: 'hamming', } /** * Mapping from file extensions to MIME types. */ const CONTENT_TYPE_MAP = new Map([ ['png', 'image/png'], ['jpg', 'image/jpeg'], ['jpeg', 'image/jpeg'], ['gif', 'image/gif'], ]); class RawImage { /** * Create a new `RawImage` object. * @param {Uint8ClampedArray|Uint8Array} data The pixel data. * @param {number} width The width of the image. * @param {number} height The height of the image. * @param {1|2|3|4} channels The number of channels. */ constructor(data, width, height, channels) { this.data = data; this.width = width; this.height = height; this.channels = channels; } /** * Returns the size of the image (width, height). * @returns {[number, number]} The size of the image (width, height). */ get size() { return [this.width, this.height]; } /** * Helper method for reading an image from a variety of input types. * @param {RawImage|string|URL} input * @returns The image object. * * **Example:** Read image from a URL. * ```javascript * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); * // RawImage { * // "data": Uint8ClampedArray [ 25, 25, 25, 19, 19, 19, ... ], * // "width": 800, * // "height": 533, * // "channels": 3 * // } * ``` */ static async read(input) { if (input instanceof RawImage) { return input; } else if (typeof input === 'string' || input instanceof URL) { return await this.fromURL(input); } else { throw new Error(`Unsupported input type: ${typeof input}`); } } /** * Read an image from a canvas. * @param {HTMLCanvasElement|OffscreenCanvas} canvas The canvas to read the image from. * @returns {RawImage} The image object. */ static fromCanvas(canvas) { if (!BROWSER_ENV) { throw new Error('fromCanvas() is only supported in browser environments.') } const ctx = canvas.getContext('2d'); const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data; return new RawImage(data, canvas.width, canvas.height, 4); } /** * Read an image from a URL or file path. * @param {string|URL} url The URL or file path to read the image from. * @returns {Promise} The image object. */ static async fromURL(url) { const response = await (0,_hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url); if (response.status !== 200) { throw new Error(`Unable to read image from "${url}" (${response.status} ${response.statusText})`); } const blob = await response.blob(); return this.fromBlob(blob); } /** * Helper method to create a new Image from a blob. * @param {Blob} blob The blob to read the image from. * @returns {Promise} The image object. */ static async fromBlob(blob) { if (BROWSER_ENV) { // Running in environment with canvas const img = await loadImageFunction(blob); const ctx = createCanvasFunction(img.width, img.height).getContext('2d'); // Draw image to context ctx.drawImage(img, 0, 0); return new this(ctx.getImageData(0, 0, img.width, img.height).data, img.width, img.height, 4); } else { // Use sharp.js to read (and possible resize) the image. const img = sharp__WEBPACK_IMPORTED_MODULE_3__(await blob.arrayBuffer()); return await loadImageFunction(img); } } /** * Helper method to create a new Image from a tensor * @param {Tensor} tensor */ static fromTensor(tensor, channel_format = 'CHW') { if (tensor.dims.length !== 3) { throw new Error(`Tensor should have 3 dimensions, but has ${tensor.dims.length} dimensions.`); } if (channel_format === 'CHW') { tensor = tensor.transpose(1, 2, 0); } else if (channel_format === 'HWC') { // Do nothing } else { throw new Error(`Unsupported channel format: ${channel_format}`); } if (!(tensor.data instanceof Uint8ClampedArray || tensor.data instanceof Uint8Array)) { throw new Error(`Unsupported tensor type: ${tensor.type}`); } switch (tensor.dims[2]) { case 1: case 2: case 3: case 4: return new RawImage(tensor.data, tensor.dims[1], tensor.dims[0], tensor.dims[2]); default: throw new Error(`Unsupported number of channels: ${tensor.dims[2]}`); } } /** * Convert the image to grayscale format. * @returns {RawImage} `this` to support chaining. */ grayscale() { if (this.channels === 1) { return this; } const newData = new Uint8ClampedArray(this.width * this.height * 1); switch (this.channels) { case 3: // rgb to grayscale case 4: // rgba to grayscale for (let i = 0, offset = 0; i < this.data.length; i += this.channels) { const red = this.data[i]; const green = this.data[i + 1]; const blue = this.data[i + 2]; newData[offset++] = Math.round(0.2989 * red + 0.5870 * green + 0.1140 * blue); } break; default: throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); } return this._update(newData, this.width, this.height, 1); } /** * Convert the image to RGB format. * @returns {RawImage} `this` to support chaining. */ rgb() { if (this.channels === 3) { return this; } const newData = new Uint8ClampedArray(this.width * this.height * 3); switch (this.channels) { case 1: // grayscale to rgb for (let i = 0, offset = 0; i < this.data.length; ++i) { newData[offset++] = this.data[i]; newData[offset++] = this.data[i]; newData[offset++] = this.data[i]; } break; case 4: // rgba to rgb for (let i = 0, offset = 0; i < this.data.length; i += 4) { newData[offset++] = this.data[i]; newData[offset++] = this.data[i + 1]; newData[offset++] = this.data[i + 2]; } break; default: throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); } return this._update(newData, this.width, this.height, 3); } /** * Convert the image to RGBA format. * @returns {RawImage} `this` to support chaining. */ rgba() { if (this.channels === 4) { return this; } const newData = new Uint8ClampedArray(this.width * this.height * 4); switch (this.channels) { case 1: // grayscale to rgba for (let i = 0, offset = 0; i < this.data.length; ++i) { newData[offset++] = this.data[i]; newData[offset++] = this.data[i]; newData[offset++] = this.data[i]; newData[offset++] = 255; } break; case 3: // rgb to rgba for (let i = 0, offset = 0; i < this.data.length; i += 3) { newData[offset++] = this.data[i]; newData[offset++] = this.data[i + 1]; newData[offset++] = this.data[i + 2]; newData[offset++] = 255; } break; default: throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); } return this._update(newData, this.width, this.height, 4); } /** * Resize the image to the given dimensions. This method uses the canvas API to perform the resizing. * @param {number} width The width of the new image. * @param {number} height The height of the new image. * @param {Object} options Additional options for resizing. * @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use. * @returns {Promise} `this` to support chaining. */ async resize(width, height, { resample = 2, } = {}) { // Ensure resample method is a string let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample; if (BROWSER_ENV) { // TODO use `resample` in browser environment // Store number of channels before resizing const numChannels = this.channels; // Create canvas object for this image const canvas = this.toCanvas(); // Actually perform resizing using the canvas API const ctx = createCanvasFunction(width, height).getContext('2d'); // Draw image to context, resizing in the process ctx.drawImage(canvas, 0, 0, width, height); // Create image from the resized data const resizedImage = new RawImage(ctx.getImageData(0, 0, width, height).data, width, height, 4); // Convert back so that image has the same number of channels as before return resizedImage.convert(numChannels); } else { // Create sharp image from raw data, and resize let img = this.toSharp(); switch (resampleMethod) { case 'box': case 'hamming': if (resampleMethod === 'box' || resampleMethod === 'hamming') { console.warn(`Resampling method ${resampleMethod} is not yet supported. Using bilinear instead.`); resampleMethod = 'bilinear'; } case 'nearest': case 'bilinear': case 'bicubic': // Perform resizing using affine transform. // This matches how the python Pillow library does it. img = img.affine([width / this.width, 0, 0, height / this.height], { interpolator: resampleMethod }); break; case 'lanczos': // https://github.com/python-pillow/Pillow/discussions/5519 // https://github.com/lovell/sharp/blob/main/docs/api-resize.md img = img.resize({ width, height, fit: 'fill', kernel: 'lanczos3', // PIL Lanczos uses a kernel size of 3 }); break; default: throw new Error(`Resampling method ${resampleMethod} is not supported.`); } return await loadImageFunction(img); } } async pad([left, right, top, bottom]) { left = Math.max(left, 0); right = Math.max(right, 0); top = Math.max(top, 0); bottom = Math.max(bottom, 0); if (left === 0 && right === 0 && top === 0 && bottom === 0) { // No padding needed return this; } if (BROWSER_ENV) { // Store number of channels before padding const numChannels = this.channels; // Create canvas object for this image const canvas = this.toCanvas(); const newWidth = this.width + left + right; const newHeight = this.height + top + bottom; // Create a new canvas of the desired size. const ctx = createCanvasFunction(newWidth, newHeight).getContext('2d'); // Draw image to context, padding in the process ctx.drawImage(canvas, 0, 0, this.width, this.height, left, top, newWidth, newHeight ); // Create image from the padded data const paddedImage = new RawImage( ctx.getImageData(0, 0, newWidth, newHeight).data, newWidth, newHeight, 4); // Convert back so that image has the same number of channels as before return paddedImage.convert(numChannels); } else { const img = this.toSharp().extend({ left, right, top, bottom }); return await loadImageFunction(img); } } async crop([x_min, y_min, x_max, y_max]) { // Ensure crop bounds are within the image x_min = Math.max(x_min, 0); y_min = Math.max(y_min, 0); x_max = Math.min(x_max, this.width - 1); y_max = Math.min(y_max, this.height - 1); // Do nothing if the crop is the entire image if (x_min === 0 && y_min === 0 && x_max === this.width - 1 && y_max === this.height - 1) { return this; } const crop_width = x_max - x_min + 1; const crop_height = y_max - y_min + 1; if (BROWSER_ENV) { // Store number of channels before resizing const numChannels = this.channels; // Create canvas object for this image const canvas = this.toCanvas(); // Create a new canvas of the desired size. This is needed since if the // image is too small, we need to pad it with black pixels. const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d'); // Draw image to context, cropping in the process ctx.drawImage(canvas, x_min, y_min, crop_width, crop_height, 0, 0, crop_width, crop_height ); // Create image from the resized data const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); // Convert back so that image has the same number of channels as before return resizedImage.convert(numChannels); } else { // Create sharp image from raw data const img = this.toSharp().extract({ left: x_min, top: y_min, width: crop_width, height: crop_height, }); return await loadImageFunction(img); } } async center_crop(crop_width, crop_height) { // If the image is already the desired size, return it if (this.width === crop_width && this.height === crop_height) { return this; } // Determine bounds of the image in the new canvas const width_offset = (this.width - crop_width) / 2; const height_offset = (this.height - crop_height) / 2; if (BROWSER_ENV) { // Store number of channels before resizing const numChannels = this.channels; // Create canvas object for this image const canvas = this.toCanvas(); // Create a new canvas of the desired size. This is needed since if the // image is too small, we need to pad it with black pixels. const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d'); let sourceX = 0; let sourceY = 0; let destX = 0; let destY = 0; if (width_offset >= 0) { sourceX = width_offset; } else { destX = -width_offset; } if (height_offset >= 0) { sourceY = height_offset; } else { destY = -height_offset; } // Draw image to context, cropping in the process ctx.drawImage(canvas, sourceX, sourceY, crop_width, crop_height, destX, destY, crop_width, crop_height ); // Create image from the resized data const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); // Convert back so that image has the same number of channels as before return resizedImage.convert(numChannels); } else { // Create sharp image from raw data let img = this.toSharp(); if (width_offset >= 0 && height_offset >= 0) { // Cropped image lies entirely within the original image img = img.extract({ left: Math.floor(width_offset), top: Math.floor(height_offset), width: crop_width, height: crop_height, }) } else if (width_offset <= 0 && height_offset <= 0) { // Cropped image lies entirely outside the original image, // so we add padding const top = Math.floor(-height_offset); const left = Math.floor(-width_offset); img = img.extend({ top: top, left: left, // Ensures the resulting image has the desired dimensions right: crop_width - this.width - left, bottom: crop_height - this.height - top, }); } else { // Cropped image lies partially outside the original image. // We first pad, then crop. let y_padding = [0, 0]; let y_extract = 0; if (height_offset < 0) { y_padding[0] = Math.floor(-height_offset); y_padding[1] = crop_height - this.height - y_padding[0]; } else { y_extract = Math.floor(height_offset); } let x_padding = [0, 0]; let x_extract = 0; if (width_offset < 0) { x_padding[0] = Math.floor(-width_offset); x_padding[1] = crop_width - this.width - x_padding[0]; } else { x_extract = Math.floor(width_offset); } img = img.extend({ top: y_padding[0], bottom: y_padding[1], left: x_padding[0], right: x_padding[1], }).extract({ left: x_extract, top: y_extract, width: crop_width, height: crop_height, }) } return await loadImageFunction(img); } } async toBlob(type = 'image/png', quality = 1) { if (!BROWSER_ENV) { throw new Error('toBlob() is only supported in browser environments.') } const canvas = this.toCanvas(); return await canvas.convertToBlob({ type, quality }); } toTensor(channel_format = 'CHW') { let tensor = new _tensor_js__WEBPACK_IMPORTED_MODULE_2__.Tensor( 'uint8', new Uint8Array(this.data), [this.height, this.width, this.channels] ); if (channel_format === 'HWC') { // Do nothing } else if (channel_format === 'CHW') { // hwc -> chw tensor = tensor.permute(2, 0, 1); } else { throw new Error(`Unsupported channel format: ${channel_format}`); } return tensor; } toCanvas() { if (!BROWSER_ENV) { throw new Error('toCanvas() is only supported in browser environments.') } // Clone, and convert data to RGBA before drawing to canvas. // This is because the canvas API only supports RGBA const cloned = this.clone().rgba(); // Create canvas object for the cloned image const clonedCanvas = createCanvasFunction(cloned.width, cloned.height); // Draw image to context const data = new ImageDataClass(cloned.data, cloned.width, cloned.height); clonedCanvas.getContext('2d').putImageData(data, 0, 0); return clonedCanvas; } /** * Helper method to update the image data. * @param {Uint8ClampedArray} data The new image data. * @param {number} width The new width of the image. * @param {number} height The new height of the image. * @param {1|2|3|4|null} [channels] The new number of channels of the image. * @private */ _update(data, width, height, channels = null) { this.data = data; this.width = width; this.height = height; if (channels !== null) { this.channels = channels; } return this; } /** * Clone the image * @returns {RawImage} The cloned image */ clone() { return new RawImage(this.data.slice(), this.width, this.height, this.channels); } /** * Helper method for converting image to have a certain number of channels * @param {number} numChannels The number of channels. Must be 1, 3, or 4. * @returns {RawImage} `this` to support chaining. */ convert(numChannels) { if (this.channels === numChannels) return this; // Already correct number of channels switch (numChannels) { case 1: this.grayscale(); break; case 3: this.rgb(); break; case 4: this.rgba(); break; default: throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); } return this; } /** * Save the image to the given path. * @param {string} path The path to save the image to. */ async save(path) { if (BROWSER_ENV) { if (WEBWORKER_ENV) { throw new Error('Unable to save an image from a Web Worker.') } const extension = path.split('.').pop().toLowerCase(); const mime = CONTENT_TYPE_MAP.get(extension) ?? 'image/png'; // Convert image to Blob const blob = await this.toBlob(mime); // Convert the canvas content to a data URL const dataURL = URL.createObjectURL(blob); // Create an anchor element with the data URL as the href attribute const downloadLink = document.createElement('a'); downloadLink.href = dataURL; // Set the download attribute to specify the desired filename for the downloaded image downloadLink.download = path; // Trigger the download downloadLink.click(); // Clean up: remove the anchor element from the DOM downloadLink.remove(); } else if (!_env_js__WEBPACK_IMPORTED_MODULE_1__.env.useFS) { throw new Error('Unable to save the image because filesystem is disabled in this environment.') } else { const img = this.toSharp(); return await img.toFile(path); } } toSharp() { if (BROWSER_ENV) { throw new Error('toSharp() is only supported in server-side environments.') } return sharp__WEBPACK_IMPORTED_MODULE_3__(this.data, { raw: { width: this.width, height: this.height, channels: this.channels } }); } } /***/ }), /***/ "./src/utils/maths.js": /*!****************************!*\ !*** ./src/utils/maths.js ***! \****************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "FFT": () => (/* binding */ FFT), /* harmony export */ "bankers_round": () => (/* binding */ bankers_round), /* harmony export */ "cos_sim": () => (/* binding */ cos_sim), /* harmony export */ "dot": () => (/* binding */ dot), /* harmony export */ "getTopItems": () => (/* binding */ getTopItems), /* harmony export */ "interpolate_data": () => (/* binding */ interpolate_data), /* harmony export */ "log_softmax": () => (/* binding */ log_softmax), /* harmony export */ "magnitude": () => (/* binding */ magnitude), /* harmony export */ "max": () => (/* binding */ max), /* harmony export */ "medianFilter": () => (/* binding */ medianFilter), /* harmony export */ "min": () => (/* binding */ min), /* harmony export */ "permute_data": () => (/* binding */ permute_data), /* harmony export */ "round": () => (/* binding */ round), /* harmony export */ "softmax": () => (/* binding */ softmax) /* harmony export */ }); /** * @file Helper module for mathematical processing. * * These functions and classes are only used internally, * meaning an end-user shouldn't need to access anything here. * * @module utils/maths */ /** * @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array} TypedArray * @typedef {BigInt64Array | BigUint64Array} BigTypedArray * @typedef {TypedArray | BigTypedArray} AnyTypedArray */ /** * @param {TypedArray} input */ function interpolate_data(input, [in_channels, in_height, in_width], [out_height, out_width], mode = 'bilinear', align_corners = false) { // TODO use mode and align_corners // Output image dimensions const x_scale = out_width / in_width; const y_scale = out_height / in_height; // Output image // @ts-ignore const out_img = new input.constructor(out_height * out_width * in_channels); // Pre-calculate strides const inStride = in_height * in_width; const outStride = out_height * out_width; for (let i = 0; i < out_height; ++i) { for (let j = 0; j < out_width; ++j) { // Calculate output offset const outOffset = i * out_width + j; // Calculate input pixel coordinates const x = (j + 0.5) / x_scale - 0.5; const y = (i + 0.5) / y_scale - 0.5; // Calculate the four nearest input pixels // We also check if the input pixel coordinates are within the image bounds let x1 = Math.floor(x); let y1 = Math.floor(y); const x2 = Math.min(x1 + 1, in_width - 1); const y2 = Math.min(y1 + 1, in_height - 1); x1 = Math.max(x1, 0); y1 = Math.max(y1, 0); // Calculate the fractional distances between the input pixel and the four nearest pixels const s = x - x1; const t = y - y1; // Perform bilinear interpolation const w1 = (1 - s) * (1 - t); const w2 = s * (1 - t); const w3 = (1 - s) * t; const w4 = s * t; // Calculate the four nearest input pixel indices const yStride = y1 * in_width; const xStride = y2 * in_width; const idx1 = yStride + x1; const idx2 = yStride + x2; const idx3 = xStride + x1; const idx4 = xStride + x2; for (let k = 0; k < in_channels; ++k) { // Calculate channel offset const cOffset = k * inStride; out_img[k * outStride + outOffset] = w1 * input[cOffset + idx1] + w2 * input[cOffset + idx2] + w3 * input[cOffset + idx3] + w4 * input[cOffset + idx4]; } } } return out_img; } /** * Helper method to permute a `AnyTypedArray` directly * @template {AnyTypedArray} T * @param {T} array * @param {number[]} dims * @param {number[]} axes * @returns {[T, number[]]} The permuted array and the new shape. */ function permute_data(array, dims, axes) { // Calculate the new shape of the permuted array // and the stride of the original array const shape = new Array(axes.length); const stride = new Array(axes.length); for (let i = axes.length - 1, s = 1; i >= 0; --i) { stride[i] = s; shape[i] = dims[axes[i]]; s *= shape[i]; } // Precompute inverse mapping of stride const invStride = axes.map((_, i) => stride[axes.indexOf(i)]); // Create the permuted array with the new shape // @ts-ignore const permutedData = new array.constructor(array.length); // Permute the original array to the new array for (let i = 0; i < array.length; ++i) { let newIndex = 0; for (let j = dims.length - 1, k = i; j >= 0; --j) { newIndex += (k % dims[j]) * invStride[j]; k = Math.floor(k / dims[j]); } permutedData[newIndex] = array[i]; } return [permutedData, shape]; } /** * Compute the softmax of an array of numbers. * @template {TypedArray|number[]} T * @param {T} arr The array of numbers to compute the softmax of. * @returns {T} The softmax array. */ function softmax(arr) { // Compute the maximum value in the array const maxVal = max(arr)[0]; // Compute the exponentials of the array values const exps = arr.map(x => Math.exp(x - maxVal)); // Compute the sum of the exponentials // @ts-ignore const sumExps = exps.reduce((acc, val) => acc + val, 0); // Compute the softmax values const softmaxArr = exps.map(x => x / sumExps); return /** @type {T} */(softmaxArr); } /** * Calculates the logarithm of the softmax function for the input array. * @template {TypedArray|number[]} T * @param {T} arr The input array to calculate the log_softmax function for. * @returns {T} The resulting log_softmax array. */ function log_softmax(arr) { // Compute the softmax values const softmaxArr = softmax(arr); // Apply log formula to each element const logSoftmaxArr = softmaxArr.map(x => Math.log(x)); return /** @type {T} */(logSoftmaxArr); } /** * Calculates the dot product of two arrays. * @param {number[]} arr1 The first array. * @param {number[]} arr2 The second array. * @returns {number} The dot product of arr1 and arr2. */ function dot(arr1, arr2) { let result = 0; for (let i = 0; i < arr1.length; ++i) { result += arr1[i] * arr2[i]; } return result; } /** * Get the top k items from an iterable, sorted by descending order * @param {any[]|TypedArray} items The items to be sorted * @param {number|null} [top_k=0] The number of top items to return (default: 0 = return all) * @returns {[number, any][]} The top k items, sorted by descending order */ function getTopItems(items, top_k = 0) { // if top == 0, return all items = Array.from(items) .map((x, i) => [i, x]) // Get indices ([index, score]) .sort((a, b) => b[1] - a[1]) // Sort by log probabilities if (top_k !== null && top_k > 0) { items = items.slice(0, top_k); // Get top k items } return items } /** * Computes the cosine similarity between two arrays. * * @param {number[]} arr1 The first array. * @param {number[]} arr2 The second array. * @returns {number} The cosine similarity between the two arrays. */ function cos_sim(arr1, arr2) { // Calculate dot product of the two arrays const dotProduct = dot(arr1, arr2); // Calculate the magnitude of the first array const magnitudeA = magnitude(arr1); // Calculate the magnitude of the second array const magnitudeB = magnitude(arr2); // Calculate the cosine similarity const cosineSimilarity = dotProduct / (magnitudeA * magnitudeB); return cosineSimilarity; } /** * Calculates the magnitude of a given array. * @param {number[]} arr The array to calculate the magnitude of. * @returns {number} The magnitude of the array. */ function magnitude(arr) { return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0)); } /** * Returns the value and index of the minimum element in an array. * @param {number[]|TypedArray} arr array of numbers. * @returns {number[]} the value and index of the minimum element, of the form: [valueOfMin, indexOfMin] * @throws {Error} If array is empty. */ function min(arr) { if (arr.length === 0) throw Error('Array must not be empty'); let min = arr[0]; let indexOfMin = 0; for (let i = 1; i < arr.length; ++i) { if (arr[i] < min) { min = arr[i]; indexOfMin = i; } } return [min, indexOfMin]; } /** * Returns the value and index of the maximum element in an array. * @param {number[]|AnyTypedArray} arr array of numbers. * @returns {[number, number]} the value and index of the maximum element, of the form: [valueOfMax, indexOfMax] * @throws {Error} If array is empty. */ function max(arr) { if (arr.length === 0) throw Error('Array must not be empty'); let max = arr[0]; let indexOfMax = 0; for (let i = 1; i < arr.length; ++i) { if (arr[i] > max) { max = arr[i]; indexOfMax = i; } } return [Number(max), indexOfMax]; } function isPowerOfTwo(number) { // Check if the number is greater than 0 and has only one bit set to 1 return (number > 0) && ((number & (number - 1)) === 0); } /** * Implementation of Radix-4 FFT. * * P2FFT class provides functionality for performing Fast Fourier Transform on arrays * which are a power of two in length. * Code adapted from https://www.npmjs.com/package/fft.js */ class P2FFT { /** * @param {number} size The size of the input array. Must be a power of two larger than 1. * @throws {Error} FFT size must be a power of two larger than 1. */ constructor(size) { this.size = size | 0; // convert to a 32-bit signed integer if (this.size <= 1 || !isPowerOfTwo(this.size)) throw new Error('FFT size must be a power of two larger than 1'); this._csize = size << 1; this.table = new Float64Array(this.size * 2); for (let i = 0; i < this.table.length; i += 2) { const angle = Math.PI * i / this.size; this.table[i] = Math.cos(angle); this.table[i + 1] = -Math.sin(angle); } // Find size's power of two let power = 0; for (let t = 1; this.size > t; t <<= 1) ++power; // Calculate initial step's width: // * If we are full radix-4, it is 2x smaller to give inital len=8 // * Otherwise it is the same as `power` to give len=4 this._width = power % 2 === 0 ? power - 1 : power; // Pre-compute bit-reversal patterns this._bitrev = new Int32Array(1 << this._width); for (let j = 0; j < this._bitrev.length; ++j) { this._bitrev[j] = 0; for (let shift = 0; shift < this._width; shift += 2) { const revShift = this._width - shift - 2; this._bitrev[j] |= ((j >>> shift) & 3) << revShift; } } } /** * Create a complex number array with size `2 * size` * * @returns {Float64Array} A complex number array with size `2 * size` */ createComplexArray() { return new Float64Array(this._csize); } /** * Converts a complex number representation stored in a Float64Array to an array of real numbers. * * @param {Float64Array} complex The complex number representation to be converted. * @param {number[]} [storage] An optional array to store the result in. * @returns {number[]} An array of real numbers representing the input complex number representation. */ fromComplexArray(complex, storage) { const res = storage || new Array(complex.length >>> 1); for (let i = 0; i < complex.length; i += 2) res[i >>> 1] = complex[i]; return res; } /** * Convert a real-valued input array to a complex-valued output array. * @param {Float64Array} input The real-valued input array. * @param {Float64Array} [storage] Optional buffer to store the output array. * @returns {Float64Array} The complex-valued output array. */ toComplexArray(input, storage) { const res = storage || this.createComplexArray(); for (let i = 0; i < res.length; i += 2) { res[i] = input[i >>> 1]; res[i + 1] = 0; } return res; } /** * Completes the spectrum by adding its mirrored negative frequency components. * @param {Float64Array} spectrum The input spectrum. * @returns {void} */ completeSpectrum(spectrum) { const size = this._csize; const half = size >>> 1; for (let i = 2; i < half; i += 2) { spectrum[size - i] = spectrum[i]; spectrum[size - i + 1] = -spectrum[i + 1]; } } /** * Performs a Fast Fourier Transform (FFT) on the given input data and stores the result in the output buffer. * * @param {Float64Array} out The output buffer to store the result. * @param {Float64Array} data The input data to transform. * * @throws {Error} Input and output buffers must be different. * * @returns {void} */ transform(out, data) { if (out === data) throw new Error('Input and output buffers must be different'); this._transform4(out, data, 1 /* DONE */); } /** * Performs a real-valued forward FFT on the given input buffer and stores the result in the given output buffer. * The input buffer must contain real values only, while the output buffer will contain complex values. The input and * output buffers must be different. * * @param {Float64Array} out The output buffer. * @param {Float64Array} data The input buffer containing real values. * * @throws {Error} If the input and output buffers are the same. */ realTransform(out, data) { if (out === data) throw new Error('Input and output buffers must be different'); this._realTransform4(out, data, 1 /* DONE */); } /** * Performs an inverse FFT transformation on the given `data` array, and stores the result in `out`. * The `out` array must be a different buffer than the `data` array. The `out` array will contain the * result of the transformation. The `data` array will not be modified. * * @param {Float64Array} out The output buffer for the transformed data. * @param {Float64Array} data The input data to transform. * @throws {Error} If `out` and `data` refer to the same buffer. * @returns {void} */ inverseTransform(out, data) { if (out === data) throw new Error('Input and output buffers must be different'); this._transform4(out, data, -1 /* DONE */); for (let i = 0; i < out.length; ++i) out[i] /= this.size; } /** * Performs a radix-4 implementation of a discrete Fourier transform on a given set of data. * * @param {Float64Array} out The output buffer for the transformed data. * @param {Float64Array} data The input buffer of data to be transformed. * @param {number} inv A scaling factor to apply to the transform. * @returns {void} */ _transform4(out, data, inv) { // radix-4 implementation const size = this._csize; // Initial step (permute and transform) const width = this._width; let step = 1 << width; let len = (size / step) << 1; let outOff; let t; const bitrev = this._bitrev; if (len === 4) { for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { const off = bitrev[t]; this._singleTransform2(data, out, outOff, off, step); } } else { // len === 8 for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { const off = bitrev[t]; this._singleTransform4(data, out, outOff, off, step, inv); } } // Loop through steps in decreasing order for (step >>= 2; step >= 2; step >>= 2) { len = (size / step) << 1; const quarterLen = len >>> 2; // Loop through offsets in the data for (outOff = 0; outOff < size; outOff += len) { // Full case const limit = outOff + quarterLen - 1; for (let i = outOff, k = 0; i < limit; i += 2, k += step) { const A = i; const B = A + quarterLen; const C = B + quarterLen; const D = C + quarterLen; // Original values const Ar = out[A]; const Ai = out[A + 1]; const Br = out[B]; const Bi = out[B + 1]; const Cr = out[C]; const Ci = out[C + 1]; const Dr = out[D]; const Di = out[D + 1]; const tableBr = this.table[k]; const tableBi = inv * this.table[k + 1]; const MBr = Br * tableBr - Bi * tableBi; const MBi = Br * tableBi + Bi * tableBr; const tableCr = this.table[2 * k]; const tableCi = inv * this.table[2 * k + 1]; const MCr = Cr * tableCr - Ci * tableCi; const MCi = Cr * tableCi + Ci * tableCr; const tableDr = this.table[3 * k]; const tableDi = inv * this.table[3 * k + 1]; const MDr = Dr * tableDr - Di * tableDi; const MDi = Dr * tableDi + Di * tableDr; // Pre-Final values const T0r = Ar + MCr; const T0i = Ai + MCi; const T1r = Ar - MCr; const T1i = Ai - MCi; const T2r = MBr + MDr; const T2i = MBi + MDi; const T3r = inv * (MBr - MDr); const T3i = inv * (MBi - MDi); // Final values out[A] = T0r + T2r; out[A + 1] = T0i + T2i; out[B] = T1r + T3i; out[B + 1] = T1i - T3r; out[C] = T0r - T2r; out[C + 1] = T0i - T2i; out[D] = T1r - T3i; out[D + 1] = T1i + T3r; } } } } /** * Performs a radix-2 implementation of a discrete Fourier transform on a given set of data. * * @param {Float64Array} data The input buffer of data to be transformed. * @param {Float64Array} out The output buffer for the transformed data. * @param {number} outOff The offset at which to write the output data. * @param {number} off The offset at which to begin reading the input data. * @param {number} step The step size for indexing the input data. * @returns {void} */ _singleTransform2(data, out, outOff, off, step) { // radix-2 implementation // NOTE: Only called for len=4 const evenR = data[off]; const evenI = data[off + 1]; const oddR = data[off + step]; const oddI = data[off + step + 1]; out[outOff] = evenR + oddR; out[outOff + 1] = evenI + oddI; out[outOff + 2] = evenR - oddR; out[outOff + 3] = evenI - oddI; } /** * Performs radix-4 transformation on input data of length 8 * * @param {Float64Array} data Input data array of length 8 * @param {Float64Array} out Output data array of length 8 * @param {number} outOff Index of output array to start writing from * @param {number} off Index of input array to start reading from * @param {number} step Step size between elements in input array * @param {number} inv Scaling factor for inverse transform * * @returns {void} */ _singleTransform4(data, out, outOff, off, step, inv) { // radix-4 // NOTE: Only called for len=8 const step2 = step * 2; const step3 = step * 3; // Original values const Ar = data[off]; const Ai = data[off + 1]; const Br = data[off + step]; const Bi = data[off + step + 1]; const Cr = data[off + step2]; const Ci = data[off + step2 + 1]; const Dr = data[off + step3]; const Di = data[off + step3 + 1]; // Pre-Final values const T0r = Ar + Cr; const T0i = Ai + Ci; const T1r = Ar - Cr; const T1i = Ai - Ci; const T2r = Br + Dr; const T2i = Bi + Di; const T3r = inv * (Br - Dr); const T3i = inv * (Bi - Di); // Final values out[outOff] = T0r + T2r; out[outOff + 1] = T0i + T2i; out[outOff + 2] = T1r + T3i; out[outOff + 3] = T1i - T3r; out[outOff + 4] = T0r - T2r; out[outOff + 5] = T0i - T2i; out[outOff + 6] = T1r - T3i; out[outOff + 7] = T1i + T3r; } /** * Real input radix-4 implementation * @param {Float64Array} out Output array for the transformed data * @param {Float64Array} data Input array of real data to be transformed * @param {number} inv The scale factor used to normalize the inverse transform */ _realTransform4(out, data, inv) { // Real input radix-4 implementation const size = this._csize; // Initial step (permute and transform) const width = this._width; let step = 1 << width; let len = (size / step) << 1; let outOff; let t; const bitrev = this._bitrev; if (len === 4) { for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { const off = bitrev[t]; this._singleRealTransform2(data, out, outOff, off >>> 1, step >>> 1); } } else { // len === 8 for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { const off = bitrev[t]; this._singleRealTransform4(data, out, outOff, off >>> 1, step >>> 1, inv); } } // TODO: Optimize once https://github.com/indutny/fft.js/issues/25 is fixed // Loop through steps in decreasing order for (step >>= 2; step >= 2; step >>= 2) { len = (size / step) << 1; const quarterLen = len >>> 2; // Loop through offsets in the data for (outOff = 0; outOff < size; outOff += len) { // Full case const limit = outOff + quarterLen - 1; for (let i = outOff, k = 0; i < limit; i += 2, k += step) { const A = i; const B = A + quarterLen; const C = B + quarterLen; const D = C + quarterLen; // Original values const Ar = out[A]; const Ai = out[A + 1]; const Br = out[B]; const Bi = out[B + 1]; const Cr = out[C]; const Ci = out[C + 1]; const Dr = out[D]; const Di = out[D + 1]; const tableBr = this.table[k]; const tableBi = inv * this.table[k + 1]; const MBr = Br * tableBr - Bi * tableBi; const MBi = Br * tableBi + Bi * tableBr; const tableCr = this.table[2 * k]; const tableCi = inv * this.table[2 * k + 1]; const MCr = Cr * tableCr - Ci * tableCi; const MCi = Cr * tableCi + Ci * tableCr; const tableDr = this.table[3 * k]; const tableDi = inv * this.table[3 * k + 1]; const MDr = Dr * tableDr - Di * tableDi; const MDi = Dr * tableDi + Di * tableDr; // Pre-Final values const T0r = Ar + MCr; const T0i = Ai + MCi; const T1r = Ar - MCr; const T1i = Ai - MCi; const T2r = MBr + MDr; const T2i = MBi + MDi; const T3r = inv * (MBr - MDr); const T3i = inv * (MBi - MDi); // Final values out[A] = T0r + T2r; out[A + 1] = T0i + T2i; out[B] = T1r + T3i; out[B + 1] = T1i - T3r; out[C] = T0r - T2r; out[C + 1] = T0i - T2i; out[D] = T1r - T3i; out[D + 1] = T1i + T3r; } } } } /** * Performs a single real input radix-2 transformation on the provided data * * @param {Float64Array} data The input data array * @param {Float64Array} out The output data array * @param {number} outOff The output offset * @param {number} off The input offset * @param {number} step The step * * @returns {void} */ _singleRealTransform2(data, out, outOff, off, step) { // radix-2 implementation // NOTE: Only called for len=4 const evenR = data[off]; const oddR = data[off + step]; out[outOff] = evenR + oddR; out[outOff + 1] = 0; out[outOff + 2] = evenR - oddR; out[outOff + 3] = 0; } /** * Computes a single real-valued transform using radix-4 algorithm. * This method is only called for len=8. * * @param {Float64Array} data The input data array. * @param {Float64Array} out The output data array. * @param {number} outOff The offset into the output array. * @param {number} off The offset into the input array. * @param {number} step The step size for the input array. * @param {number} inv The value of inverse. */ _singleRealTransform4(data, out, outOff, off, step, inv) { // radix-4 // NOTE: Only called for len=8 const step2 = step * 2; const step3 = step * 3; // Original values const Ar = data[off]; const Br = data[off + step]; const Cr = data[off + step2]; const Dr = data[off + step3]; // Pre-Final values const T0r = Ar + Cr; const T1r = Ar - Cr; const T2r = Br + Dr; const T3r = inv * (Br - Dr); // Final values out[outOff] = T0r + T2r; out[outOff + 1] = 0; out[outOff + 2] = T1r; out[outOff + 3] = -T3r; out[outOff + 4] = T0r - T2r; out[outOff + 5] = 0; out[outOff + 6] = T1r; out[outOff + 7] = T3r; } } /** * NP2FFT class provides functionality for performing Fast Fourier Transform on arrays * which are not a power of two in length. In such cases, the chirp-z transform is used. * * For more information, see: https://math.stackexchange.com/questions/77118/non-power-of-2-ffts/77156#77156 */ class NP2FFT { /** * Constructs a new NP2FFT object. * @param {number} fft_length The length of the FFT */ constructor(fft_length) { // Helper variables const a = 2 * (fft_length - 1); const b = 2 * (2 * fft_length - 1); const nextP2 = 2 ** (Math.ceil(Math.log2(b))) this.bufferSize = nextP2; this._a = a; // Define buffers // Compute chirp for transform const chirp = new Float64Array(b); const ichirp = new Float64Array(nextP2); this._chirpBuffer = new Float64Array(nextP2); this._buffer1 = new Float64Array(nextP2); this._buffer2 = new Float64Array(nextP2); this._outBuffer1 = new Float64Array(nextP2); this._outBuffer2 = new Float64Array(nextP2); // Compute complex exponentiation const theta = -2 * Math.PI / fft_length; const baseR = Math.cos(theta); const baseI = Math.sin(theta); // Precompute helper for chirp-z transform for (let i = 0; i < b >> 1; ++i) { // Compute complex power: const e = (i + 1 - fft_length) ** 2 / 2.0; // Compute the modulus and argument of the result const result_mod = Math.sqrt(baseR ** 2 + baseI ** 2) ** e; const result_arg = e * Math.atan2(baseI, baseR); // Convert the result back to rectangular form // and assign to chirp and ichirp const i2 = 2 * i; chirp[i2] = result_mod * Math.cos(result_arg); chirp[i2 + 1] = result_mod * Math.sin(result_arg); // conjugate ichirp[i2] = chirp[i2]; ichirp[i2 + 1] = - chirp[i2 + 1]; } this._slicedChirpBuffer = chirp.subarray(a, b); // create object to perform Fast Fourier Transforms // with `nextP2` complex numbers this._f = new P2FFT(nextP2 >> 1); this._f.transform(this._chirpBuffer, ichirp); } _transform(output, input, real) { const ib1 = this._buffer1; const ib2 = this._buffer2; const ob2 = this._outBuffer1; const ob3 = this._outBuffer2; const cb = this._chirpBuffer; const sb = this._slicedChirpBuffer; const a = this._a; if (real) { // Real multiplication for (let j = 0; j < sb.length; j += 2) { const j2 = j + 1 const j3 = j >> 1; const a_real = input[j3]; ib1[j] = a_real * sb[j]; ib1[j2] = a_real * sb[j2]; } } else { // Complex multiplication for (let j = 0; j < sb.length; j += 2) { const j2 = j + 1 ib1[j] = input[j] * sb[j] - input[j2] * sb[j2]; ib1[j2] = input[j] * sb[j2] + input[j2] * sb[j]; } } this._f.transform(ob2, ib1); for (let j = 0; j < cb.length; j += 2) { const j2 = j + 1; ib2[j] = ob2[j] * cb[j] - ob2[j2] * cb[j2]; ib2[j2] = ob2[j] * cb[j2] + ob2[j2] * cb[j]; } this._f.inverseTransform(ob3, ib2); for (let j = 0; j < ob3.length; j += 2) { const a_real = ob3[j + a]; const a_imag = ob3[j + a + 1]; const b_real = sb[j]; const b_imag = sb[j + 1]; output[j] = a_real * b_real - a_imag * b_imag; output[j + 1] = a_real * b_imag + a_imag * b_real; } } transform(output, input) { this._transform(output, input, false); } realTransform(output, input) { this._transform(output, input, true); } } class FFT { constructor(fft_length) { this.fft_length = fft_length; this.isPowerOfTwo = isPowerOfTwo(fft_length); if (this.isPowerOfTwo) { this.fft = new P2FFT(fft_length); this.outputBufferSize = 2 * fft_length; } else { this.fft = new NP2FFT(fft_length); this.outputBufferSize = this.fft.bufferSize; } } realTransform(out, input) { this.fft.realTransform(out, input); } transform(out, input) { this.fft.transform(out, input); } } /** * Performs median filter on the provided data. Padding is done by mirroring the data. * @param {AnyTypedArray} data The input array * @param {number} windowSize The window size */ function medianFilter(data, windowSize) { if (windowSize % 2 === 0 || windowSize <= 0) { throw new Error('Window size must be a positive odd number'); } // @ts-ignore const outputArray = new data.constructor(data.length); // @ts-ignore const buffer = new data.constructor(windowSize); // Reusable array for storing values const halfWindowSize = Math.floor(windowSize / 2); for (let i = 0; i < data.length; ++i) { let valuesIndex = 0; for (let j = -halfWindowSize; j <= halfWindowSize; ++j) { let index = i + j; if (index < 0) { index = Math.abs(index); } else if (index >= data.length) { index = 2 * (data.length - 1) - index; } buffer[valuesIndex++] = data[index]; } buffer.sort(); outputArray[i] = buffer[halfWindowSize]; } return outputArray; } /** * Helper function to round a number to a given number of decimals * @param {number} num The number to round * @param {number} decimals The number of decimals * @returns {number} The rounded number */ function round(num, decimals) { const pow = Math.pow(10, decimals); return Math.round(num * pow) / pow; } /** * Helper function to round a number to the nearest integer, with ties rounded to the nearest even number. * Also known as "bankers' rounding". This is the default rounding mode in python. For example: * 1.5 rounds to 2 and 2.5 rounds to 2. * * @param {number} x The number to round * @returns {number} The rounded number */ function bankers_round(x) { const r = Math.round(x); const br = Math.abs(x) % 1 === 0.5 ? (r % 2 === 0 ? r : r - 1) : r; return br; } /***/ }), /***/ "./src/utils/tensor.js": /*!*****************************!*\ !*** ./src/utils/tensor.js ***! \*****************************/ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "Tensor": () => (/* binding */ Tensor), /* harmony export */ "cat": () => (/* binding */ cat), /* harmony export */ "dynamicTimeWarping": () => (/* binding */ dynamicTimeWarping), /* harmony export */ "full": () => (/* binding */ full), /* harmony export */ "full_like": () => (/* binding */ full_like), /* harmony export */ "interpolate": () => (/* binding */ interpolate), /* harmony export */ "interpolate_4d": () => (/* binding */ interpolate_4d), /* harmony export */ "layer_norm": () => (/* binding */ layer_norm), /* harmony export */ "mean": () => (/* binding */ mean), /* harmony export */ "mean_pooling": () => (/* binding */ mean_pooling), /* harmony export */ "ones": () => (/* binding */ ones), /* harmony export */ "ones_like": () => (/* binding */ ones_like), /* harmony export */ "permute": () => (/* binding */ permute), /* harmony export */ "quantize_embeddings": () => (/* binding */ quantize_embeddings), /* harmony export */ "stack": () => (/* binding */ stack), /* harmony export */ "std_mean": () => (/* binding */ std_mean), /* harmony export */ "zeros": () => (/* binding */ zeros), /* harmony export */ "zeros_like": () => (/* binding */ zeros_like) /* harmony export */ }); /* harmony import */ var _maths_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./maths.js */ "./src/utils/maths.js"); /* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../backends/onnx.js */ "./src/backends/onnx.js"); /* harmony import */ var _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../ops/registry.js */ "./src/ops/registry.js"); /** * @file Helper module for `Tensor` processing. * * These functions and classes are only used internally, * meaning an end-user shouldn't need to access anything here. * * @module utils/tensor */ const DataTypeMap = Object.freeze({ float32: Float32Array, float16: Uint16Array, float64: Float64Array, string: Array, // string[] int8: Int8Array, uint8: Uint8Array, int16: Int16Array, uint16: Uint16Array, int32: Int32Array, uint32: Uint32Array, int64: BigInt64Array, uint64: BigUint64Array, bool: Uint8Array, }); /** * @typedef {keyof typeof DataTypeMap} DataType * @typedef {import('./maths.js').AnyTypedArray | any[]} DataArray */ class Tensor { /** @type {number[]} Dimensions of the tensor. */ 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. */ get type() { return this.ort_tensor.type; }; /** @type {DataArray} The data stored in the tensor. */ get data() { return this.ort_tensor.data; } /** @type {number} The number of elements in the tensor. */ get size() { return this.ort_tensor.size; }; ort_tensor; /** * Create a new Tensor or copy an existing Tensor. * @param {[DataType, DataArray, number[]]|[ONNXTensor]} args */ constructor(...args) { if ((0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(args[0])) { this.ort_tensor = /** @type {ONNXTensor} */ (args[0]); } else { // Create new tensor this.ort_tensor = new _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( /** @type {DataType} */(args[0]), /** @type {Exclude} */(args[1]), args[2] ); } return new Proxy(this, { get: (obj, key) => { if (typeof key === 'string') { let index = Number(key); if (Number.isInteger(index)) { // key is an integer (i.e., index) return obj._getitem(index); } } // @ts-ignore return obj[key]; }, set: (obj, key, value) => { // TODO allow setting of data // @ts-ignore return obj[key] = value; } }); } 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. * @returns {Iterator} An iterator object for iterating over the tensor data in row-major order. */ *[Symbol.iterator]() { const [iterLength, ...iterDims] = this.dims; if (iterDims.length > 0) { const iterSize = iterDims.reduce((a, b) => a * b); for (let i = 0; i < iterLength; ++i) { yield this._subarray(i, iterSize, iterDims); } } else { yield* this.data } } /** * Index into a Tensor object. * @param {number} index The index to access. * @returns {Tensor} The data at the specified index. */ _getitem(index) { const [iterLength, ...iterDims] = this.dims; index = safeIndex(index, iterLength); if (iterDims.length > 0) { const iterSize = iterDims.reduce((a, b) => a * b); return this._subarray(index, iterSize, iterDims); } else { return new Tensor(this.type, [this.data[index]], iterDims); } } /** * @param {number|bigint} item The item to search for in the tensor * @returns {number} The index of the first occurrence of item in the tensor data. */ indexOf(item) { const this_data = this.data; for (let index = 0; index < this_data.length; ++index) { // Note: == instead of === so we can match Ints with BigInts if (this_data[index] == item) { return index; } } return -1; } /** * @param {number} index * @param {number} iterSize * @param {any} iterDims * @returns {Tensor} */ _subarray(index, iterSize, iterDims) { const o1 = index * iterSize; const o2 = (index + 1) * iterSize; // We use subarray if available (typed array), otherwise we use slice (normal array) const data = ('subarray' in this.data) ? this.data.subarray(o1, o2) : this.data.slice(o1, o2); return new Tensor(this.type, data, iterDims); } /** * Returns the value of this tensor as a standard JavaScript Number. This only works * for tensors with one element. For other cases, see `Tensor.tolist()`. * @returns {number|bigint} The value of this tensor as a standard JavaScript Number. * @throws {Error} If the tensor has more than one element. */ item() { const this_data = this.data; if (this_data.length !== 1) { throw new Error(`a Tensor with ${this_data.length} elements cannot be converted to Scalar`); } return this_data[0]; } /** * Convert tensor data to a n-dimensional JS list * @returns {Array} */ tolist() { return reshape(this.data, this.dims) } /** * Return a new Tensor with the sigmoid function applied to each element. * @returns {Tensor} The tensor with the sigmoid function applied. */ sigmoid() { return this.clone().sigmoid_(); } /** * Applies the sigmoid function to the tensor in place. * @returns {Tensor} Returns `this`. */ sigmoid_() { const this_data = this.data; for (let i = 0; i < this_data.length; ++i) { this_data[i] = 1 / (1 + Math.exp(-this_data[i])); } return this; } /** * Return a new Tensor with every element multiplied by a constant. * @param {number} val The value to multiply by. * @returns {Tensor} The new tensor. */ mul(val) { return this.clone().mul_(val); } /** * Multiply the tensor by a constant in place. * @param {number} val The value to multiply by. * @returns {Tensor} Returns `this`. */ mul_(val) { const this_data = this.data; for (let i = 0; i < this_data.length; ++i) { this_data[i] *= val; } return this; } /** * Return a new Tensor with every element added by a constant. * @param {number} val The value to add by. * @returns {Tensor} The new tensor. */ add(val) { return this.clone().add_(val); } /** * Add the tensor by a constant in place. * @param {number} val The value to add by. * @returns {Tensor} Returns `this`. */ add_(val) { const this_data = this.data; for (let i = 0; i < this_data.length; ++i) { this_data[i] += val; } return this; } clone() { return new Tensor(this.type, this.data.slice(), this.dims.slice()); } slice(...slices) { // This allows for slicing with ranges and numbers let newTensorDims = []; let newOffsets = []; // slices is an array of numbers or arrays of numbers // e.g., slices = [0, [1, 3], null, [0, 3]] for (let sliceIndex = 0; sliceIndex < this.dims.length; ++sliceIndex) { let slice = slices[sliceIndex]; if (slice === null || slice === undefined) { // null or undefined means take the whole dimension newOffsets.push([0, this.dims[sliceIndex]]); newTensorDims.push(this.dims[sliceIndex]); } else if (typeof slice === 'number') { slice = safeIndex(slice, this.dims[sliceIndex], sliceIndex); // A number means take a single element newOffsets.push([slice, slice + 1]); } else if (Array.isArray(slice) && slice.length === 2) { // An array of length 2 means take a range of elements if (slice[0] > slice[1]) { throw new Error(`Invalid slice: ${slice}`); } let offsets = [ Math.max(slice[0], 0), Math.min(slice[1], this.dims[sliceIndex]) ]; newOffsets.push(offsets); newTensorDims.push(offsets[1] - offsets[0]); } else { throw new Error(`Invalid slice: ${slice}`); } } let newDims = newOffsets.map(([start, end]) => end - start); let newBufferSize = newDims.reduce((a, b) => a * b); const this_data = this.data; // Allocate memory // @ts-ignore let data = new this_data.constructor(newBufferSize); // Precompute strides const stride = this.stride(); for (let i = 0; i < newBufferSize; ++i) { let originalIndex = 0; for (let j = newDims.length - 1, num = i; j >= 0; --j) { const size = newDims[j]; originalIndex += ((num % size) + newOffsets[j][0]) * stride[j]; num = Math.floor(num / size); } data[i] = this_data[originalIndex]; } return new Tensor(this.type, data, newTensorDims); } /** * Return a permuted version of this Tensor, according to the provided dimensions. * @param {...number} dims Dimensions to permute. * @returns {Tensor} The permuted tensor. */ permute(...dims) { return permute(this, dims); } // TODO: implement transpose. For now (backwards compatibility), it's just an alias for permute() transpose(...dims) { return this.permute(...dims); } // TODO add .max() and .min() methods /** * Returns the sum of each row of the input tensor in the given dimension dim. * * @param {number} [dim=null] The dimension or dimensions to reduce. If `null`, all dimensions are reduced. * @param {boolean} keepdim Whether the output tensor has `dim` retained or not. * @returns The summed tensor */ sum(dim = null, keepdim = false) { return this.norm(1, dim, keepdim); } /** * Returns the matrix norm or vector norm of a given tensor. * @param {number|string} [p='fro'] The order of norm * @param {number} [dim=null] Specifies which dimension of the tensor to calculate the norm across. * If dim is None, the norm will be calculated across all dimensions of input. * @param {boolean} [keepdim=false] Whether the output tensors have dim retained or not. * @returns {Tensor} The norm of the tensor. */ norm(p = 'fro', dim = null, keepdim = false) { if (p === 'fro') { // NOTE: Since we only support integer dims, Frobenius norm produces the same result as p=2. p = 2; } else if (typeof p === 'string') { throw Error(`Unsupported norm: ${p}`); } const this_data = this.data; if (dim === null) { // @ts-ignore let val = this_data.reduce((a, b) => a + (b ** p), 0) ** (1 / p); return new Tensor(this.type, [val], []); } // Negative indexing dim = safeIndex(dim, this.dims.length); // Calculate the shape of the resulting array after summation const resultDims = this.dims.slice(); // Copy the original dimensions resultDims[dim] = 1; // Remove the specified axis // Create a new array to store the accumulated values // @ts-ignore const result = new this_data.constructor(this_data.length / this.dims[dim]); // Iterate over the data array for (let i = 0; i < this_data.length; ++i) { // Calculate the index in the resulting array let resultIndex = 0; for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { const size = this.dims[j]; if (j !== dim) { const index = num % size; resultIndex += index * resultMultiplier; resultMultiplier *= resultDims[j]; } num = Math.floor(num / size); } // Accumulate the value at the current index result[resultIndex] += (this_data[i]) ** p; } if (p !== 1) { for (let i = 0; i < result.length; ++i) { result[i] = result[i] ** (1 / p); } } if (!keepdim) { resultDims.splice(dim, 1); } return new Tensor(this.type, result, resultDims); } /** * Performs `L_p` normalization of inputs over specified dimension. Operates in place. * @param {number} [p=2] The exponent value in the norm formulation * @param {number} [dim=1] The dimension to reduce * @returns {Tensor} `this` for operation chaining. */ normalize_(p = 2.0, dim = 1) { dim = safeIndex(dim, this.dims.length); const norm = this.norm(p, dim, true); const this_data = this.data; for (let i = 0; i < this_data.length; ++i) { // Calculate the index in the resulting array let resultIndex = 0; for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { const size = this.dims[j]; if (j !== dim) { const index = num % size; resultIndex += index * resultMultiplier; resultMultiplier *= this.dims[j]; } num = Math.floor(num / size); } // Divide by normalized value this_data[i] /= norm.data[resultIndex]; } return this; } /** * Performs `L_p` normalization of inputs over specified dimension. * @param {number} [p=2] The exponent value in the norm formulation * @param {number} [dim=1] The dimension to reduce * @returns {Tensor} The normalized tensor. */ normalize(p = 2.0, dim = 1) { return this.clone().normalize_(p, dim); } /** * Compute and return the stride of this tensor. * Stride is the jump necessary to go from one element to the next one in the specified dimension dim. * @returns {number[]} The stride of this tensor. */ stride() { return dimsToStride(this.dims); } /** * Returns a tensor with all specified dimensions of input of size 1 removed. * * NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other. * If you would like a copy, use `tensor.clone()` before squeezing. * * @param {number} [dim=null] If given, the input will be squeezed only in the specified dimensions. * @returns {Tensor} The squeezed tensor */ squeeze(dim = null) { return new Tensor( this.type, this.data, calc_squeeze_dims(this.dims, dim) ) } /** * In-place version of @see {@link Tensor.squeeze} */ squeeze_(dim = null) { this.dims = calc_squeeze_dims(this.dims, dim); return this; } /** * Returns a new tensor with a dimension of size one inserted at the specified position. * * NOTE: The returned tensor shares the same underlying data with this tensor. * * @param {number} dim The index at which to insert the singleton dimension * @returns {Tensor} The unsqueezed tensor */ unsqueeze(dim = null) { return new Tensor( this.type, this.data, calc_unsqueeze_dims(this.dims, dim) ); } /** * In-place version of @see {@link Tensor.unsqueeze} */ unsqueeze_(dim = null) { this.dims = calc_unsqueeze_dims(this.dims, dim); return this; } /** * In-place version of @see {@link Tensor.flatten} */ flatten_(start_dim = 0, end_dim = -1) { // TODO validate inputs end_dim = (end_dim + this.dims.length) % this.dims.length; let dimsToKeepBefore = this.dims.slice(0, start_dim); let dimsToFlatten = this.dims.slice(start_dim, end_dim + 1); let dimsToKeepAfter = this.dims.slice(end_dim + 1); this.dims = [...dimsToKeepBefore, dimsToFlatten.reduce((a, b) => a * b, 1), ...dimsToKeepAfter] return this; } /** * Flattens input by reshaping it into a one-dimensional tensor. * If `start_dim` or `end_dim` are passed, only dimensions starting with `start_dim` * and ending with `end_dim` are flattened. The order of elements in input is unchanged. * @param {number} start_dim the first dim to flatten * @param {number} end_dim the last dim to flatten * @returns {Tensor} The flattened tensor. */ flatten(start_dim = 0, end_dim = -1) { return this.clone().flatten_(start_dim, end_dim); } /** * Returns a new tensor with the same data as the `self` tensor but of a different `shape`. * @param {...number} dims the desired size * @returns {Tensor} The tensor with the same data but different shape */ view(...dims) { // TODO: validate dims let inferredIndex = -1; for (let i = 0; i < dims.length; ++i) { if (dims[i] === -1) { if (inferredIndex !== -1) { throw new Error("Only one dimension can be inferred"); } inferredIndex = i; } } if (inferredIndex !== -1) { // Some dimension must be inferred const productOther = dims.reduce((product, curr, index) => { return index !== inferredIndex ? product * curr : product }, 1); dims[inferredIndex] = this.data.length / productOther; } return new Tensor(this.type, this.data, dims); // NOTE: uses same underlying storage } neg_() { const this_data = this.data; for (let i = 0; i < this_data.length; ++i) { this_data[i] = -this_data[i]; } return this; } neg() { return this.clone().neg_(); } /** * In-place version of @see {@link Tensor.clamp} */ clamp_(min, max) { const this_data = this.data; for (let i = 0; i < this_data.length; ++i) { this_data[i] = Math.min(Math.max(this_data[i], min), max); } return this; } /** * Clamps all elements in input into the range [ min, max ] * @param {number} min lower-bound of the range to be clamped to * @param {number} max upper-bound of the range to be clamped to * @returns {Tensor} the output tensor. */ clamp(min, max) { return this.clone().clamp_(min, max); } /** * In-place version of @see {@link Tensor.round} */ round_() { const this_data = this.data; for (let i = 0; i < this_data.length; ++i) { this_data[i] = Math.round(this_data[i]); } return this; } /** * Rounds elements of input to the nearest integer. * @returns {Tensor} the output tensor. */ round() { return this.clone().round_(); } mean(dim = null, keepdim = false) { return mean(this, dim, keepdim); } /** * Performs Tensor dtype conversion. * @param {DataType} type The desired data type. * @returns {Tensor} The converted tensor. */ to(type) { // If the self Tensor already has the correct dtype, then self is returned. if (this.type === type) return this; // Otherwise, the returned tensor is a copy of self with the desired dtype. if (!DataTypeMap.hasOwnProperty(type)) { throw new Error(`Unsupported type: ${type}`); } // @ts-ignore return new Tensor(type, DataTypeMap[type].from(this.data), this.dims); } } /** * This creates a nested array of a given type and depth (see examples). * * @example * NestArray; // string[] * @example * NestArray; // number[][] * @example * NestArray; // string[][][] etc. * @template T * @template {number} Depth * @template {never[]} [Acc=[]] * @typedef {Acc['length'] extends Depth ? T : NestArray} NestArray */ /** * Reshapes a 1-dimensional array into an n-dimensional array, according to the provided dimensions. * * @example * reshape([10 ], [1 ]); // Type: number[] Value: [10] * reshape([1, 2, 3, 4 ], [2, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4]] * reshape([1, 2, 3, 4, 5, 6, 7, 8], [2, 2, 2]); // Type: number[][][] Value: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] * reshape([1, 2, 3, 4, 5, 6, 7, 8], [4, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4], [5, 6], [7, 8]] * @param {T[]|DataArray} data The input array to reshape. * @param {DIM} dimensions The target shape/dimensions. * @template T * @template {[number]|number[]} DIM * @returns {NestArray} The reshaped array. */ function reshape(data, dimensions) { const totalElements = data.length; const dimensionSize = dimensions.reduce((a, b) => a * b); if (totalElements !== dimensionSize) { throw Error(`cannot reshape array of size ${totalElements} into shape (${dimensions})`); } /** @type {any} */ let reshapedArray = data; for (let i = dimensions.length - 1; i >= 0; i--) { reshapedArray = reshapedArray.reduce((acc, val) => { let lastArray = acc[acc.length - 1]; if (lastArray.length < dimensions[i]) { lastArray.push(val); } else { acc.push([val]); } return acc; }, [[]]); } return reshapedArray[0]; } /** * Permutes a tensor according to the provided axes. * @param {any} tensor The input tensor to permute. * @param {Array} axes The axes to permute the tensor along. * @returns {Tensor} The permuted tensor. */ function permute(tensor, axes) { const [permutedData, shape] = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.permute_data)(tensor.data, tensor.dims, axes); return new Tensor(tensor.type, permutedData, shape); } /** * Interpolates an Tensor to the given size. * @param {Tensor} input The input tensor to interpolate. Data must be channel-first (i.e., [c, h, w]) * @param {number[]} size The output size of the image * @param {string} mode The interpolation mode * @param {boolean} align_corners Whether to align corners. * @returns {Tensor} The interpolated tensor. */ function interpolate(input, [out_height, out_width], mode = 'bilinear', align_corners = false) { // Input image dimensions const in_channels = input.dims.at(-3) ?? 1; const in_height = input.dims.at(-2); const in_width = input.dims.at(-1); let output = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.interpolate_data)( /** @type {import('./maths.js').TypedArray}*/(input.data), [in_channels, in_height, in_width], [out_height, out_width], mode, align_corners ); return new Tensor(input.type, output, [in_channels, out_height, out_width]); } /** * Down/up samples the input. * Inspired by https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html. * @param {Tensor} input the input tensor * @param {Object} options the options for the interpolation * @param {[number, number]|[number, number, number]|[number, number, number, number]} [options.size=null] output spatial size. * @param {"bilinear"|"bicubic"} [options.mode='bilinear'] algorithm used for upsampling * @returns {Promise} The interpolated tensor. */ async function interpolate_4d(input, { size = null, mode = 'bilinear', } = {}) { // Error checking if (input.dims.length !== 4) { throw new Error('`interpolate_4d` currently only supports 4D input.'); } if (!size) { // TODO: support scale_factor throw new Error('`interpolate_4d` requires a `size` argument.'); } // Fill in missing dimensions let targetDims; if (size.length === 2) { targetDims = [...input.dims.slice(0, 2), ...size]; } else if (size.length === 3) { targetDims = [input.dims[0], ...size]; } else if (size.length === 4) { targetDims = size; } else { throw new Error('`size` must be of length 2, 3, or 4.'); } let op; if (mode === 'bilinear') { op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bilinear_interpolate_4d; } else if (mode === 'bicubic') { op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bicubic_interpolate_4d; } else { throw new Error(`Unsupported mode: ${mode}`); } const sizeTensor = new Tensor('int64', new BigInt64Array(targetDims.map(BigInt)), [targetDims.length]); return await op({ x: input, s: sizeTensor }); } /** * Perform mean pooling of the last hidden state followed by a normalization step. * @param {Tensor} last_hidden_state Tensor of shape [batchSize, seqLength, embedDim] * @param {Tensor} attention_mask Tensor of shape [batchSize, seqLength] * @returns {Tensor} Returns a new Tensor of shape [batchSize, embedDim]. */ function mean_pooling(last_hidden_state, attention_mask) { // last_hidden_state: [batchSize, seqLength, embedDim] // attention_mask: [batchSize, seqLength] let shape = [last_hidden_state.dims[0], last_hidden_state.dims[2]]; // @ts-ignore let returnedData = new last_hidden_state.data.constructor(shape[0] * shape[1]); let [batchSize, seqLength, embedDim] = last_hidden_state.dims; let outIndex = 0; for (let i = 0; i < batchSize; ++i) { let offset = i * embedDim * seqLength; for (let k = 0; k < embedDim; ++k) { let sum = 0; let count = 0; let attnMaskOffset = i * seqLength; let offset2 = offset + k; // Pool over all words in sequence for (let j = 0; j < seqLength; ++j) { // index into attention mask let attn = Number(attention_mask.data[attnMaskOffset + j]); count += attn; sum += last_hidden_state.data[offset2 + j * embedDim] * attn; } let avg = sum / count; returnedData[outIndex++] = avg; } } return new Tensor( last_hidden_state.type, returnedData, shape ) } /** * Apply Layer Normalization for last certain number of dimensions. * @param {Tensor} input The input tensor * @param {number[]} normalized_shape input shape from an expected input of size * @param {Object} options The options for the layer normalization * @param {number} [options.eps=1e-5] A value added to the denominator for numerical stability. * @returns {Tensor} The normalized tensor. */ function layer_norm(input, normalized_shape, { eps = 1e-5, } = {}) { if (input.dims.length !== 2) { throw new Error('`layer_norm` currently only supports 2D input.'); } const [batchSize, featureDim] = input.dims; if (normalized_shape.length !== 1 && normalized_shape[0] !== featureDim) { throw new Error('`normalized_shape` must be a 1D array with shape `[input.dims[1]]`.'); } const [std, mean] = std_mean(input, 1, 0, true); // @ts-ignore const returnedData = new input.data.constructor(input.data.length); for (let i = 0; i < batchSize; ++i) { const offset = i * featureDim; for (let j = 0; j < featureDim; ++j) { const offset2 = offset + j; returnedData[offset2] = (input.data[offset2] - mean.data[i]) / (std.data[i] + eps); } } return new Tensor(input.type, returnedData, input.dims); } /** * Helper function to calculate new dimensions when performing a squeeze operation. * @param {number[]} dims The dimensions of the tensor. * @param {number|number[]|null} dim The dimension(s) to squeeze. * @returns {number[]} The new dimensions. * @private */ function calc_squeeze_dims(dims, dim) { dims = dims.slice(); if (dim === null) { dims = dims.filter((d) => d !== 1); } else if (typeof dim === 'number') { if (dims[dim] === 1) { dims.splice(dim, 1); } } else if (Array.isArray(dim)) { dims = dims.filter((x, i) => { return x !== 1 || !dim.includes(i); }); } return dims; } /** * Helper function to calculate new dimensions when performing an unsqueeze operation. * @param {number[]} dims The dimensions of the tensor. * @param {number} dim The dimension to unsqueeze. * @returns {number[]} The new dimensions. * @private */ function calc_unsqueeze_dims(dims, dim) { // Dimension out of range (e.g., "expected to be in range of [-4, 3], but got 4") // + 1 since we allow inserting at the end (i.e. dim = -1) dim = safeIndex(dim, dims.length + 1); dims = dims.slice(); // Insert 1 into specified dimension dims.splice(dim, 0, 1); return dims; } /** * Safely calculate the index for an array of a given size, allowing negative indexing. * @param {number} index The index that will be used. * @param {number} size The size of the array. * @param {number} [dimension=null] The dimension that the index is for (optional). * @returns {number} The index, guaranteed to be non-negative and less than `arrayLength`. * * @throws {Error} If the index is out of range. * @private */ function safeIndex(index, size, dimension = null) { if (index < -size || index >= size) { throw new Error(`IndexError: index ${index} is out of bounds for dimension${dimension === null ? '' : ' ' + dimension} with size ${size}`); } if (index < 0) { // Negative indexing, ensuring positive index index = ((index % size) + size) % size; } return index; } /** * Concatenates an array of tensors along a specified dimension. * @param {Tensor[]} tensors The array of tensors to concatenate. * @param {number} dim The dimension to concatenate along. * @returns {Tensor} The concatenated tensor. */ function cat(tensors, dim = 0) { dim = safeIndex(dim, tensors[0].dims.length); // TODO do validation of shapes const resultDims = tensors[0].dims.slice(); resultDims[dim] = tensors.reduce((a, b) => a + b.dims[dim], 0); // Create a new array to store the accumulated values const resultSize = resultDims.reduce((a, b) => a * b, 1); // @ts-ignore const result = new tensors[0].data.constructor(resultSize); // Create output tensor of same type as first const resultType = tensors[0].type; if (dim === 0) { // Handle special case for performance reasons let offset = 0; for (let t of tensors) { result.set(t.data, offset); offset += t.data.length; } } else { let currentDim = 0; for (let t = 0; t < tensors.length; ++t) { let tensor = tensors[t]; // Iterate over the data array for (let i = 0; i < tensor.data.length; ++i) { // Calculate the index in the resulting array let resultIndex = 0; for (let j = tensor.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { const size = tensor.dims[j]; let index = num % size; if (j === dim) { index += currentDim; } resultIndex += index * resultMultiplier; resultMultiplier *= resultDims[j]; num = Math.floor(num / size); } // Accumulate the value at the current index result[resultIndex] = tensor.data[i]; } currentDim += tensor.dims[dim]; } } return new Tensor(resultType, result, resultDims); } /** * Stack an array of tensors along a specified dimension. * @param {Tensor[]} tensors The array of tensors to stack. * @param {number} dim The dimension to stack along. * @returns {Tensor} The stacked tensor. */ function stack(tensors, dim = 0) { // TODO do validation of shapes // NOTE: stack expects each tensor to be equal size return cat(tensors.map(t => t.unsqueeze(dim)), dim); } /** * Calculates the standard deviation and mean over the dimensions specified by dim. dim can be a single dimension or `null` to reduce over all dimensions. * @param {Tensor} input the input tenso * @param {number|null} dim the dimension to reduce. If None, all dimensions are reduced. * @param {number} correction difference between the sample size and sample degrees of freedom. Defaults to Bessel's correction, correction=1. * @param {boolean} keepdim whether the output tensor has dim retained or not. * @returns {Tensor[]} A tuple of (std, mean) tensors. */ function std_mean(input, dim = null, correction = 1, keepdim = false) { if (dim === null) { // None to reduce over all dimensions. // @ts-ignore const sum = input.data.reduce((a, b) => a + b, 0); const mean = sum / input.data.length; // @ts-ignore const std = Math.sqrt(input.data.reduce((a, b) => a + (b - mean) ** 2, 0) / (input.data.length - correction)); const meanTensor = new Tensor(input.type, [mean], [/* scalar */]); const stdTensor = new Tensor(input.type, [std], [/* scalar */]); return [stdTensor, meanTensor]; } // Negative indexing dim = safeIndex(dim, input.dims.length); const meanTensor = mean(input, dim, keepdim); // Calculate the shape of the resulting array after summation const resultDims = input.dims.slice(); // Copy the original dimensions resultDims[dim] = 1; // Remove the specified axis // Create a new array to store the accumulated values // @ts-ignore const result = new input.data.constructor(input.data.length / input.dims[dim]); // Iterate over the data array for (let i = 0; i < input.data.length; ++i) { // Calculate the index in the resulting array let resultIndex = 0; for (let j = input.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { const size = input.dims[j]; if (j !== dim) { const index = num % size; resultIndex += index * resultMultiplier; resultMultiplier *= resultDims[j]; } num = Math.floor(num / size); } // Accumulate the value at the current index result[resultIndex] += (input.data[i] - meanTensor.data[resultIndex]) ** 2; } for (let i = 0; i < result.length; ++i) { result[i] = Math.sqrt(result[i] / (input.dims[dim] - correction)); } if (!keepdim) { resultDims.splice(dim, 1); } const stdTensor = new Tensor(input.type, result, resultDims); return [stdTensor, meanTensor]; } /** * Returns the mean value of each row of the input tensor in the given dimension dim. * @param {Tensor} input the input tensor. * @param {number|null} dim the dimension to reduce. * @param {boolean} keepdim whether the output tensor has dim retained or not. * @returns {Tensor} A new tensor with means taken along the specified dimension. */ function mean(input, dim = null, keepdim = false) { if (dim === null) { // None to reduce over all dimensions. // @ts-ignore let val = input.data.reduce((a, b) => a + b, 0); return new Tensor(input.type, [val / input.data.length], [/* scalar */]); } // Negative indexing dim = safeIndex(dim, input.dims.length); // Calculate the shape of the resulting array after summation const resultDims = input.dims.slice(); // Copy the original dimensions resultDims[dim] = 1; // Remove the specified axis // Create a new array to store the accumulated values // @ts-ignore const result = new input.data.constructor(input.data.length / input.dims[dim]); // Iterate over the data array for (let i = 0; i < input.data.length; ++i) { // Calculate the index in the resulting array let resultIndex = 0; for (let j = input.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { const size = input.dims[j]; if (j !== dim) { const index = num % size; resultIndex += index * resultMultiplier; resultMultiplier *= resultDims[j]; } num = Math.floor(num / size); } // Accumulate the value at the current index result[resultIndex] += input.data[i]; } if (input.dims[dim] !== 1) { for (let i = 0; i < result.length; ++i) { result[i] = result[i] / input.dims[dim]; } } if (!keepdim) { resultDims.splice(dim, 1); } return new Tensor(input.type, result, resultDims); } /** * * Measures similarity between two temporal sequences (e.g., input audio and output tokens * to generate token-level timestamps). * @param {Tensor} matrix * @returns {number[][]} */ function dynamicTimeWarping(matrix) { const [output_length, input_length] = matrix.dims; const outputShape = [output_length + 1, input_length + 1]; const cost = new Tensor( 'float32', new Float32Array(outputShape[0] * outputShape[1]).fill(Infinity), outputShape ); const trace = new Tensor( 'float32', new Float32Array(outputShape[0] * outputShape[1]).fill(-1), outputShape ) // same as `cost[0][0] = 0`; cost[0].data[0] = 0; for (let j = 1; j < input_length + 1; ++j) { for (let i = 1; i < output_length + 1; ++i) { const c0 = cost[i - 1][j - 1].item(); const c1 = cost[i - 1][j].item(); const c2 = cost[i][j - 1].item(); let c, t; if (c0 < c1 && c0 < c2) { c = c0; t = 0; } else if (c1 < c0 && c1 < c2) { c = c1; t = 1; } else { c = c2; t = 2; } cost[i].data[j] = matrix[i - 1][j - 1].item() + c; trace[i].data[j] = t; } } // backtrace let i = output_length; let j = input_length; // @ts-ignore trace.data.fill(2, 0, outputShape[1]) // trace[0, :] = 2 for (let i = 0; i < outputShape[0]; ++i) { // trace[:, 0] = 1 trace[i].data[0] = 1; } let text_indices = []; let time_indices = []; while (i > 0 || j > 0) { text_indices.push(i - 1); time_indices.push(j - 1); const t = trace[i][j].item(); switch (t) { case 0: --i; --j; break; case 1: --i; break; case 2: --j; break; default: throw new Error( `Internal error in dynamic time warping. Unexpected trace[${i}, ${j}]. Please file a bug report.` ) } } text_indices.reverse(); time_indices.reverse(); return [text_indices, time_indices]; } function dimsToStride(dims) { const stride = new Array(dims.length); for (let i = dims.length - 1, s2 = 1; i >= 0; --i) { stride[i] = s2; s2 *= dims[i]; } return stride; } function fullHelper(size, fill_value, dtype, cls) { const numElements = size.reduce((a, b) => a * b, 1); return new Tensor( dtype, new cls(numElements).fill(fill_value), size ) } /** * Creates a tensor of size size filled with fill_value. The tensor's dtype is inferred from fill_value. * @param {number[]} size A sequence of integers defining the shape of the output tensor. * @param {number|bigint} fill_value The value to fill the output tensor with. * @returns {Tensor} The filled tensor. */ function full(size, fill_value) { let dtype; let typedArrayCls; if (typeof fill_value === 'number') { dtype = 'float32'; typedArrayCls = Float32Array; } else if (typeof fill_value === 'bigint') { dtype = 'int64'; typedArrayCls = BigInt64Array; } else { // TODO: support other dtypes throw new Error(`Unsupported data type: ${typeof fill_value}`); } return fullHelper(size, fill_value, dtype, typedArrayCls); } function full_like(tensor, fill_value) { return full(tensor.dims, fill_value); } /** * Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size. * @param {number[]} size A sequence of integers defining the shape of the output tensor. * @returns {Tensor} The ones tensor. */ function ones(size) { return fullHelper(size, 1n, 'int64', BigInt64Array); } /** * Returns a tensor filled with the scalar value 1, with the same size as input. * @param {Tensor} tensor The size of input will determine size of the output tensor. * @returns {Tensor} The ones tensor. */ function ones_like(tensor) { return ones(tensor.dims); } /** * Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size. * @param {number[]} size A sequence of integers defining the shape of the output tensor. * @returns {Tensor} The zeros tensor. */ function zeros(size) { return fullHelper(size, 0n, 'int64', BigInt64Array); } /** * Returns a tensor filled with the scalar value 0, with the same size as input. * @param {Tensor} tensor The size of input will determine size of the output tensor. * @returns {Tensor} The zeros tensor. */ function zeros_like(tensor) { return zeros(tensor.dims); } /** * Quantizes the embeddings tensor to binary or unsigned binary precision. * @param {Tensor} tensor The tensor to quantize. * @param {'binary'|'ubinary'} precision The precision to use for quantization. * @returns {Tensor} The quantized tensor. */ function quantize_embeddings(tensor, precision) { if (tensor.dims.length !== 2) { throw new Error("The tensor must have 2 dimensions"); } if (tensor.dims.at(-1) % 8 !== 0) { throw new Error("The last dimension of the tensor must be a multiple of 8"); } if (!['binary', 'ubinary'].includes(precision)) { throw new Error("The precision must be either 'binary' or 'ubinary'"); } const signed = precision === 'binary'; const dtype = signed ? 'int8' : 'uint8'; // Create a typed array to store the packed bits const cls = signed ? Int8Array : Uint8Array; const inputData = tensor.data; const outputData = new cls(inputData.length / 8); // Iterate over each number in the array for (let i = 0; i < inputData.length; ++i) { // Determine if the number is greater than 0 const bit = inputData[i] > 0 ? 1 : 0; // Calculate the index in the typed array and the position within the byte const arrayIndex = Math.floor(i / 8); const bitPosition = i % 8; // Pack the bit into the typed array outputData[arrayIndex] |= bit << (7 - bitPosition); if (signed && bitPosition === 0) { outputData[arrayIndex] -= 128; } }; return new Tensor(dtype, outputData, [tensor.dims[0], tensor.dims[1] / 8]); } /***/ }) /******/ }); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ var cachedModule = __webpack_module_cache__[moduleId]; /******/ if (cachedModule !== undefined) { /******/ return cachedModule.exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ // no module.id needed /******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ /******/ /* webpack/runtime/create fake namespace object */ /******/ (() => { /******/ var getProto = Object.getPrototypeOf ? (obj) => (Object.getPrototypeOf(obj)) : (obj) => (obj.__proto__); /******/ var leafPrototypes; /******/ // create a fake namespace object /******/ // mode & 1: value is a module id, require it /******/ // mode & 2: merge all properties of value into the ns /******/ // mode & 4: return value when already ns object /******/ // mode & 16: return value when it's Promise-like /******/ // mode & 8|1: behave like require /******/ __webpack_require__.t = function(value, mode) { /******/ if(mode & 1) value = this(value); /******/ if(mode & 8) return value; /******/ if(typeof value === 'object' && value) { /******/ if((mode & 4) && value.__esModule) return value; /******/ if((mode & 16) && typeof value.then === 'function') return value; /******/ } /******/ var ns = Object.create(null); /******/ __webpack_require__.r(ns); /******/ var def = {}; /******/ leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)]; /******/ for(var current = mode & 2 && value; typeof current == 'object' && !~leafPrototypes.indexOf(current); current = getProto(current)) { /******/ Object.getOwnPropertyNames(current).forEach((key) => (def[key] = () => (value[key]))); /******/ } /******/ def['default'] = () => (value); /******/ __webpack_require__.d(ns, def); /******/ return ns; /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. (() => { /*!*****************************!*\ !*** ./src/transformers.js ***! \*****************************/ __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "ASTFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ASTFeatureExtractor), /* harmony export */ "ASTForAudioClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTForAudioClassification), /* harmony export */ "ASTModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTModel), /* harmony export */ "ASTPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTPreTrainedModel), /* harmony export */ "AlbertForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForMaskedLM), /* harmony export */ "AlbertForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForQuestionAnswering), /* harmony export */ "AlbertForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForSequenceClassification), /* harmony export */ "AlbertModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertModel), /* harmony export */ "AlbertPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertPreTrainedModel), /* harmony export */ "AlbertTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AlbertTokenizer), /* harmony export */ "AudioClassificationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AudioClassificationPipeline), /* harmony export */ "AutoConfig": () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_5__.AutoConfig), /* harmony export */ "AutoModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModel), /* harmony export */ "AutoModelForAudioClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioClassification), /* harmony export */ "AutoModelForAudioFrameClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioFrameClassification), /* harmony export */ "AutoModelForCTC": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCTC), /* harmony export */ "AutoModelForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCausalLM), /* harmony export */ "AutoModelForDepthEstimation": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDepthEstimation), /* harmony export */ "AutoModelForDocumentQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDocumentQuestionAnswering), /* harmony export */ "AutoModelForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageClassification), /* harmony export */ "AutoModelForImageFeatureExtraction": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageFeatureExtraction), /* harmony export */ "AutoModelForImageMatting": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageMatting), /* harmony export */ "AutoModelForImageSegmentation": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageSegmentation), /* harmony export */ "AutoModelForImageToImage": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageToImage), /* harmony export */ "AutoModelForMaskGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskGeneration), /* harmony export */ "AutoModelForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskedLM), /* harmony export */ "AutoModelForObjectDetection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForObjectDetection), /* harmony export */ "AutoModelForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForQuestionAnswering), /* harmony export */ "AutoModelForSemanticSegmentation": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSemanticSegmentation), /* harmony export */ "AutoModelForSeq2SeqLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSeq2SeqLM), /* harmony export */ "AutoModelForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSequenceClassification), /* harmony export */ "AutoModelForSpeechSeq2Seq": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSpeechSeq2Seq), /* harmony export */ "AutoModelForTextToSpectrogram": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToSpectrogram), /* harmony export */ "AutoModelForTextToWaveform": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToWaveform), /* harmony export */ "AutoModelForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTokenClassification), /* harmony export */ "AutoModelForVision2Seq": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForVision2Seq), /* harmony export */ "AutoModelForXVector": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForXVector), /* harmony export */ "AutoModelForZeroShotObjectDetection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForZeroShotObjectDetection), /* harmony export */ "AutoProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.AutoProcessor), /* harmony export */ "AutoTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AutoTokenizer), /* harmony export */ "AutomaticSpeechRecognitionPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AutomaticSpeechRecognitionPipeline), /* harmony export */ "BartForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForConditionalGeneration), /* harmony export */ "BartForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForSequenceClassification), /* harmony export */ "BartModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartModel), /* harmony export */ "BartPretrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartPretrainedModel), /* harmony export */ "BartTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BartTokenizer), /* harmony export */ "BaseModelOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BaseModelOutput), /* harmony export */ "BaseStreamer": () => (/* reexport safe */ _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.BaseStreamer), /* harmony export */ "BeitFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.BeitFeatureExtractor), /* harmony export */ "BeitForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitForImageClassification), /* harmony export */ "BeitModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitModel), /* harmony export */ "BeitPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitPreTrainedModel), /* harmony export */ "BertForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForMaskedLM), /* harmony export */ "BertForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForQuestionAnswering), /* harmony export */ "BertForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForSequenceClassification), /* harmony export */ "BertForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForTokenClassification), /* harmony export */ "BertModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertModel), /* harmony export */ "BertPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertPreTrainedModel), /* harmony export */ "BertTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BertTokenizer), /* harmony export */ "BitImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.BitImageProcessor), /* harmony export */ "BlenderbotForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotForConditionalGeneration), /* harmony export */ "BlenderbotModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotModel), /* harmony export */ "BlenderbotPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotPreTrainedModel), /* harmony export */ "BlenderbotSmallForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallForConditionalGeneration), /* harmony export */ "BlenderbotSmallModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallModel), /* harmony export */ "BlenderbotSmallPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallPreTrainedModel), /* harmony export */ "BlenderbotSmallTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotSmallTokenizer), /* harmony export */ "BlenderbotTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotTokenizer), /* harmony export */ "BloomForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomForCausalLM), /* harmony export */ "BloomModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomModel), /* harmony export */ "BloomPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomPreTrainedModel), /* harmony export */ "BloomTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BloomTokenizer), /* harmony export */ "CLIPFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.CLIPFeatureExtractor), /* harmony export */ "CLIPImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.CLIPImageProcessor), /* harmony export */ "CLIPModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPModel), /* harmony export */ "CLIPPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPPreTrainedModel), /* harmony export */ "CLIPSegForImageSegmentation": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegForImageSegmentation), /* harmony export */ "CLIPSegModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegModel), /* harmony export */ "CLIPSegPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegPreTrainedModel), /* harmony export */ "CLIPTextModelWithProjection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModelWithProjection), /* harmony export */ "CLIPTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CLIPTokenizer), /* harmony export */ "CLIPVisionModelWithProjection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModelWithProjection), /* harmony export */ "CamembertForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForMaskedLM), /* harmony export */ "CamembertForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForQuestionAnswering), /* harmony export */ "CamembertForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForSequenceClassification), /* harmony export */ "CamembertForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForTokenClassification), /* harmony export */ "CamembertModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertModel), /* harmony export */ "CamembertPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertPreTrainedModel), /* harmony export */ "CamembertTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CamembertTokenizer), /* harmony export */ "CausalLMOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutput), /* harmony export */ "CausalLMOutputWithPast": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutputWithPast), /* harmony export */ "ChineseCLIPFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ChineseCLIPFeatureExtractor), /* harmony export */ "ChineseCLIPModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPModel), /* harmony export */ "ChineseCLIPPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPPreTrainedModel), /* harmony export */ "ClapAudioModelWithProjection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapAudioModelWithProjection), /* harmony export */ "ClapFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ClapFeatureExtractor), /* harmony export */ "ClapModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapModel), /* harmony export */ "ClapPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapPreTrainedModel), /* harmony export */ "ClapTextModelWithProjection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapTextModelWithProjection), /* harmony export */ "CodeGenForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenForCausalLM), /* harmony export */ "CodeGenModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenModel), /* harmony export */ "CodeGenPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenPreTrainedModel), /* harmony export */ "CodeGenTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeGenTokenizer), /* harmony export */ "CodeLlamaTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeLlamaTokenizer), /* harmony export */ "CohereTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CohereTokenizer), /* harmony export */ "ConvBertForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForMaskedLM), /* harmony export */ "ConvBertForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForQuestionAnswering), /* harmony export */ "ConvBertForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForSequenceClassification), /* harmony export */ "ConvBertForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForTokenClassification), /* harmony export */ "ConvBertModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertModel), /* harmony export */ "ConvBertPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertPreTrainedModel), /* harmony export */ "ConvBertTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ConvBertTokenizer), /* harmony export */ "ConvNextFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextFeatureExtractor), /* harmony export */ "ConvNextForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextForImageClassification), /* harmony export */ "ConvNextImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextImageProcessor), /* harmony export */ "ConvNextModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextModel), /* harmony export */ "ConvNextPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextPreTrainedModel), /* harmony export */ "ConvNextV2ForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2ForImageClassification), /* harmony export */ "ConvNextV2Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2Model), /* harmony export */ "ConvNextV2PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2PreTrainedModel), /* harmony export */ "DPTFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DPTFeatureExtractor), /* harmony export */ "DPTForDepthEstimation": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTForDepthEstimation), /* harmony export */ "DPTImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DPTImageProcessor), /* harmony export */ "DPTModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTModel), /* harmony export */ "DPTPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTPreTrainedModel), /* harmony export */ "DebertaForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForMaskedLM), /* harmony export */ "DebertaForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForQuestionAnswering), /* harmony export */ "DebertaForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForSequenceClassification), /* harmony export */ "DebertaForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForTokenClassification), /* harmony export */ "DebertaModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaModel), /* harmony export */ "DebertaPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaPreTrainedModel), /* harmony export */ "DebertaTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaTokenizer), /* harmony export */ "DebertaV2ForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForMaskedLM), /* harmony export */ "DebertaV2ForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForQuestionAnswering), /* harmony export */ "DebertaV2ForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForSequenceClassification), /* harmony export */ "DebertaV2ForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForTokenClassification), /* harmony export */ "DebertaV2Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2Model), /* harmony export */ "DebertaV2PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2PreTrainedModel), /* harmony export */ "DebertaV2Tokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaV2Tokenizer), /* harmony export */ "DeiTFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DeiTFeatureExtractor), /* harmony export */ "DeiTForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTForImageClassification), /* harmony export */ "DeiTModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTModel), /* harmony export */ "DeiTPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTPreTrainedModel), /* harmony export */ "DepthAnythingForDepthEstimation": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingForDepthEstimation), /* harmony export */ "DepthAnythingPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingPreTrainedModel), /* harmony export */ "DepthEstimationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DepthEstimationPipeline), /* harmony export */ "DetrFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DetrFeatureExtractor), /* harmony export */ "DetrForObjectDetection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForObjectDetection), /* harmony export */ "DetrForSegmentation": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForSegmentation), /* harmony export */ "DetrModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrModel), /* harmony export */ "DetrObjectDetectionOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrObjectDetectionOutput), /* harmony export */ "DetrPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrPreTrainedModel), /* harmony export */ "DetrSegmentationOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrSegmentationOutput), /* harmony export */ "Dinov2ForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2ForImageClassification), /* harmony export */ "Dinov2Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2Model), /* harmony export */ "Dinov2PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2PreTrainedModel), /* harmony export */ "DistilBertForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForMaskedLM), /* harmony export */ "DistilBertForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForQuestionAnswering), /* harmony export */ "DistilBertForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForSequenceClassification), /* harmony export */ "DistilBertForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForTokenClassification), /* harmony export */ "DistilBertModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertModel), /* harmony export */ "DistilBertPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertPreTrainedModel), /* harmony export */ "DistilBertTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DistilBertTokenizer), /* harmony export */ "DocumentQuestionAnsweringPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DocumentQuestionAnsweringPipeline), /* harmony export */ "DonutFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DonutFeatureExtractor), /* harmony export */ "DonutSwinModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinModel), /* harmony export */ "DonutSwinPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinPreTrainedModel), /* harmony export */ "EfficientNetForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetForImageClassification), /* harmony export */ "EfficientNetImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.EfficientNetImageProcessor), /* harmony export */ "EfficientNetModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetModel), /* harmony export */ "EfficientNetPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetPreTrainedModel), /* harmony export */ "ElectraForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForMaskedLM), /* harmony export */ "ElectraForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForQuestionAnswering), /* harmony export */ "ElectraForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForSequenceClassification), /* harmony export */ "ElectraForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForTokenClassification), /* harmony export */ "ElectraModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraModel), /* harmony export */ "ElectraPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraPreTrainedModel), /* harmony export */ "ElectraTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ElectraTokenizer), /* harmony export */ "EsmForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForMaskedLM), /* harmony export */ "EsmForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForSequenceClassification), /* harmony export */ "EsmForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForTokenClassification), /* harmony export */ "EsmModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmModel), /* harmony export */ "EsmPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmPreTrainedModel), /* harmony export */ "EsmTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.EsmTokenizer), /* harmony export */ "FFT": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.FFT), /* harmony export */ "FalconForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconForCausalLM), /* harmony export */ "FalconModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconModel), /* harmony export */ "FalconPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconPreTrainedModel), /* harmony export */ "FalconTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.FalconTokenizer), /* harmony export */ "FeatureExtractionPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FeatureExtractionPipeline), /* harmony export */ "FeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.FeatureExtractor), /* harmony export */ "FillMaskPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FillMaskPipeline), /* harmony export */ "GLPNFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.GLPNFeatureExtractor), /* harmony export */ "GLPNForDepthEstimation": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNForDepthEstimation), /* harmony export */ "GLPNModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNModel), /* harmony export */ "GLPNPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNPreTrainedModel), /* harmony export */ "GPT2LMHeadModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2LMHeadModel), /* harmony export */ "GPT2Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2Model), /* harmony export */ "GPT2PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2PreTrainedModel), /* harmony export */ "GPT2Tokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPT2Tokenizer), /* harmony export */ "GPTBigCodeForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeForCausalLM), /* harmony export */ "GPTBigCodeModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeModel), /* harmony export */ "GPTBigCodePreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodePreTrainedModel), /* harmony export */ "GPTJForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJForCausalLM), /* harmony export */ "GPTJModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJModel), /* harmony export */ "GPTJPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJPreTrainedModel), /* harmony export */ "GPTNeoForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoForCausalLM), /* harmony export */ "GPTNeoModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoModel), /* harmony export */ "GPTNeoPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoPreTrainedModel), /* harmony export */ "GPTNeoXForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXForCausalLM), /* harmony export */ "GPTNeoXModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXModel), /* harmony export */ "GPTNeoXPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXPreTrainedModel), /* harmony export */ "GPTNeoXTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPTNeoXTokenizer), /* harmony export */ "GemmaTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GemmaTokenizer), /* harmony export */ "Grok1Tokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Grok1Tokenizer), /* harmony export */ "HerbertTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.HerbertTokenizer), /* harmony export */ "HubertForCTC": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForCTC), /* harmony export */ "HubertForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForSequenceClassification), /* harmony export */ "HubertModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertModel), /* harmony export */ "HubertPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertPreTrainedModel), /* harmony export */ "ImageClassificationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageClassificationPipeline), /* harmony export */ "ImageFeatureExtractionPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageFeatureExtractionPipeline), /* harmony export */ "ImageFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ImageFeatureExtractor), /* harmony export */ "ImageMattingOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ImageMattingOutput), /* harmony export */ "ImageSegmentationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageSegmentationPipeline), /* harmony export */ "ImageToImagePipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToImagePipeline), /* harmony export */ "ImageToTextPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToTextPipeline), /* harmony export */ "LlamaForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaForCausalLM), /* harmony export */ "LlamaModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaModel), /* harmony export */ "LlamaPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaPreTrainedModel), /* harmony export */ "LlamaTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.LlamaTokenizer), /* harmony export */ "LlavaForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaForConditionalGeneration), /* harmony export */ "LlavaPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaPreTrainedModel), /* harmony export */ "LongT5ForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5ForConditionalGeneration), /* harmony export */ "LongT5Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5Model), /* harmony export */ "LongT5PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5PreTrainedModel), /* harmony export */ "M2M100ForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100ForConditionalGeneration), /* harmony export */ "M2M100Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100Model), /* harmony export */ "M2M100PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100PreTrainedModel), /* harmony export */ "M2M100Tokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.M2M100Tokenizer), /* harmony export */ "MBart50Tokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBart50Tokenizer), /* harmony export */ "MBartForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForCausalLM), /* harmony export */ "MBartForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForConditionalGeneration), /* harmony export */ "MBartForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForSequenceClassification), /* harmony export */ "MBartModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartModel), /* harmony export */ "MBartPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartPreTrainedModel), /* harmony export */ "MBartTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBartTokenizer), /* harmony export */ "MPNetForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForMaskedLM), /* harmony export */ "MPNetForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForQuestionAnswering), /* harmony export */ "MPNetForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForSequenceClassification), /* harmony export */ "MPNetForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForTokenClassification), /* harmony export */ "MPNetModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetModel), /* harmony export */ "MPNetPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetPreTrainedModel), /* harmony export */ "MPNetTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MPNetTokenizer), /* harmony export */ "MT5ForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5ForConditionalGeneration), /* harmony export */ "MT5Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5Model), /* harmony export */ "MT5PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5PreTrainedModel), /* harmony export */ "MarianMTModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianMTModel), /* harmony export */ "MarianModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianModel), /* harmony export */ "MarianPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianPreTrainedModel), /* harmony export */ "MarianTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MarianTokenizer), /* harmony export */ "MaskedLMOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskedLMOutput), /* harmony export */ "MistralForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralForCausalLM), /* harmony export */ "MistralModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralModel), /* harmony export */ "MistralPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralPreTrainedModel), /* harmony export */ "MobileBertForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForMaskedLM), /* harmony export */ "MobileBertForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForQuestionAnswering), /* harmony export */ "MobileBertForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForSequenceClassification), /* harmony export */ "MobileBertModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertModel), /* harmony export */ "MobileBertPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertPreTrainedModel), /* harmony export */ "MobileBertTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MobileBertTokenizer), /* harmony export */ "MobileViTFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileViTFeatureExtractor), /* harmony export */ "MobileViTForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTForImageClassification), /* harmony export */ "MobileViTModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTModel), /* harmony export */ "MobileViTPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTPreTrainedModel), /* harmony export */ "ModelOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ModelOutput), /* harmony export */ "MptForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptForCausalLM), /* harmony export */ "MptModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptModel), /* harmony export */ "MptPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptPreTrainedModel), /* harmony export */ "MusicgenForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForCausalLM), /* harmony export */ "MusicgenForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForConditionalGeneration), /* harmony export */ "MusicgenModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenModel), /* harmony export */ "MusicgenPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenPreTrainedModel), /* harmony export */ "NllbTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NllbTokenizer), /* harmony export */ "NomicBertModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertModel), /* harmony export */ "NomicBertPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertPreTrainedModel), /* harmony export */ "NougatImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.NougatImageProcessor), /* harmony export */ "NougatTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NougatTokenizer), /* harmony export */ "OPTForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTForCausalLM), /* harmony export */ "OPTModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTModel), /* harmony export */ "OPTPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTPreTrainedModel), /* harmony export */ "ObjectDetectionPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ObjectDetectionPipeline), /* harmony export */ "OwlViTFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.OwlViTFeatureExtractor), /* harmony export */ "OwlViTForObjectDetection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTForObjectDetection), /* harmony export */ "OwlViTModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTModel), /* harmony export */ "OwlViTPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTPreTrainedModel), /* harmony export */ "OwlViTProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.OwlViTProcessor), /* harmony export */ "Owlv2ForObjectDetection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2ForObjectDetection), /* harmony export */ "Owlv2ImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Owlv2ImageProcessor), /* harmony export */ "Owlv2Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2Model), /* harmony export */ "Owlv2PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2PreTrainedModel), /* harmony export */ "PhiForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiForCausalLM), /* harmony export */ "PhiModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiModel), /* harmony export */ "PhiPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiPreTrainedModel), /* harmony export */ "Pipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Pipeline), /* harmony export */ "PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PreTrainedModel), /* harmony export */ "PreTrainedTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.PreTrainedTokenizer), /* harmony export */ "PretrainedConfig": () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_5__.PretrainedConfig), /* harmony export */ "PretrainedMixin": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PretrainedMixin), /* harmony export */ "Processor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Processor), /* harmony export */ "QuestionAnsweringModelOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.QuestionAnsweringModelOutput), /* harmony export */ "QuestionAnsweringPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.QuestionAnsweringPipeline), /* harmony export */ "Qwen2ForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2ForCausalLM), /* harmony export */ "Qwen2Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2Model), /* harmony export */ "Qwen2PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2PreTrainedModel), /* harmony export */ "Qwen2Tokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Qwen2Tokenizer), /* harmony export */ "RawImage": () => (/* reexport safe */ _utils_image_js__WEBPACK_IMPORTED_MODULE_7__.RawImage), /* harmony export */ "ResNetForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetForImageClassification), /* harmony export */ "ResNetModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetModel), /* harmony export */ "ResNetPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetPreTrainedModel), /* harmony export */ "RoFormerForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForMaskedLM), /* harmony export */ "RoFormerForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForQuestionAnswering), /* harmony export */ "RoFormerForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForSequenceClassification), /* harmony export */ "RoFormerForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForTokenClassification), /* harmony export */ "RoFormerModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerModel), /* harmony export */ "RoFormerPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerPreTrainedModel), /* harmony export */ "RoFormerTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RoFormerTokenizer), /* harmony export */ "RobertaForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForMaskedLM), /* harmony export */ "RobertaForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForQuestionAnswering), /* harmony export */ "RobertaForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForSequenceClassification), /* harmony export */ "RobertaForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForTokenClassification), /* harmony export */ "RobertaModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaModel), /* harmony export */ "RobertaPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaPreTrainedModel), /* harmony export */ "RobertaTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RobertaTokenizer), /* harmony export */ "SamImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SamImageProcessor), /* harmony export */ "SamImageSegmentationOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamImageSegmentationOutput), /* harmony export */ "SamModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamModel), /* harmony export */ "SamPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamPreTrainedModel), /* harmony export */ "SamProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SamProcessor), /* harmony export */ "SeamlessM4TFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SeamlessM4TFeatureExtractor), /* harmony export */ "SegformerFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SegformerFeatureExtractor), /* harmony export */ "SegformerForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForImageClassification), /* harmony export */ "SegformerForSemanticSegmentation": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForSemanticSegmentation), /* harmony export */ "SegformerModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerModel), /* harmony export */ "SegformerPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerPreTrainedModel), /* harmony export */ "Seq2SeqLMOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Seq2SeqLMOutput), /* harmony export */ "SequenceClassifierOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SequenceClassifierOutput), /* harmony export */ "SiglipImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SiglipImageProcessor), /* harmony export */ "SiglipModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipModel), /* harmony export */ "SiglipPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipPreTrainedModel), /* harmony export */ "SiglipTextModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipTextModel), /* harmony export */ "SiglipTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SiglipTokenizer), /* harmony export */ "SiglipVisionModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipVisionModel), /* harmony export */ "SpeechT5FeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SpeechT5FeatureExtractor), /* harmony export */ "SpeechT5ForSpeechToText": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForSpeechToText), /* harmony export */ "SpeechT5ForTextToSpeech": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForTextToSpeech), /* harmony export */ "SpeechT5HifiGan": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5HifiGan), /* harmony export */ "SpeechT5Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5Model), /* harmony export */ "SpeechT5PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5PreTrainedModel), /* harmony export */ "SpeechT5Processor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SpeechT5Processor), /* harmony export */ "SpeechT5Tokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SpeechT5Tokenizer), /* harmony export */ "SqueezeBertForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForMaskedLM), /* harmony export */ "SqueezeBertForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForQuestionAnswering), /* harmony export */ "SqueezeBertForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForSequenceClassification), /* harmony export */ "SqueezeBertModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertModel), /* harmony export */ "SqueezeBertPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertPreTrainedModel), /* harmony export */ "SqueezeBertTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SqueezeBertTokenizer), /* harmony export */ "StableLmForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmForCausalLM), /* harmony export */ "StableLmModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmModel), /* harmony export */ "StableLmPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmPreTrainedModel), /* harmony export */ "Starcoder2ForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2ForCausalLM), /* harmony export */ "Starcoder2Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2Model), /* harmony export */ "Starcoder2PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2PreTrainedModel), /* harmony export */ "SummarizationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.SummarizationPipeline), /* harmony export */ "Swin2SRForImageSuperResolution": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRForImageSuperResolution), /* harmony export */ "Swin2SRImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Swin2SRImageProcessor), /* harmony export */ "Swin2SRModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRModel), /* harmony export */ "Swin2SRPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRPreTrainedModel), /* harmony export */ "SwinForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinForImageClassification), /* harmony export */ "SwinModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinModel), /* harmony export */ "SwinPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinPreTrainedModel), /* harmony export */ "T5ForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5ForConditionalGeneration), /* harmony export */ "T5Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5Model), /* harmony export */ "T5PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5PreTrainedModel), /* harmony export */ "T5Tokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.T5Tokenizer), /* harmony export */ "TableTransformerForObjectDetection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerForObjectDetection), /* harmony export */ "TableTransformerModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerModel), /* harmony export */ "TableTransformerObjectDetectionOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerObjectDetectionOutput), /* harmony export */ "TableTransformerPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerPreTrainedModel), /* harmony export */ "Tensor": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor), /* harmony export */ "Text2TextGenerationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Text2TextGenerationPipeline), /* harmony export */ "TextClassificationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextClassificationPipeline), /* harmony export */ "TextGenerationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextGenerationPipeline), /* harmony export */ "TextToAudioPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextToAudioPipeline), /* harmony export */ "TokenClassificationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TokenClassificationPipeline), /* harmony export */ "TokenClassifierOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TokenClassifierOutput), /* harmony export */ "TokenizerModel": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.TokenizerModel), /* harmony export */ "TrOCRForCausalLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRForCausalLM), /* harmony export */ "TrOCRPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRPreTrainedModel), /* harmony export */ "TranslationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TranslationPipeline), /* harmony export */ "UniSpeechForCTC": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForCTC), /* harmony export */ "UniSpeechForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForSequenceClassification), /* harmony export */ "UniSpeechModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechModel), /* harmony export */ "UniSpeechPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechPreTrainedModel), /* harmony export */ "UniSpeechSatForAudioFrameClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForAudioFrameClassification), /* harmony export */ "UniSpeechSatForCTC": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForCTC), /* harmony export */ "UniSpeechSatForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForSequenceClassification), /* harmony export */ "UniSpeechSatModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatModel), /* harmony export */ "UniSpeechSatPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatPreTrainedModel), /* harmony export */ "ViTFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ViTFeatureExtractor), /* harmony export */ "ViTForImageClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTForImageClassification), /* harmony export */ "ViTImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ViTImageProcessor), /* harmony export */ "ViTModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTModel), /* harmony export */ "ViTPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTPreTrainedModel), /* harmony export */ "VisionEncoderDecoderModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VisionEncoderDecoderModel), /* harmony export */ "VitMatteForImageMatting": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMatteForImageMatting), /* harmony export */ "VitMatteImageProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.VitMatteImageProcessor), /* harmony export */ "VitMattePreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMattePreTrainedModel), /* harmony export */ "VitsModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModel), /* harmony export */ "VitsModelOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModelOutput), /* harmony export */ "VitsPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsPreTrainedModel), /* harmony export */ "VitsTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.VitsTokenizer), /* harmony export */ "Wav2Vec2BertForCTC": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForCTC), /* harmony export */ "Wav2Vec2BertForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForSequenceClassification), /* harmony export */ "Wav2Vec2BertModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertModel), /* harmony export */ "Wav2Vec2BertPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertPreTrainedModel), /* harmony export */ "Wav2Vec2CTCTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Wav2Vec2CTCTokenizer), /* harmony export */ "Wav2Vec2FeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Wav2Vec2FeatureExtractor), /* harmony export */ "Wav2Vec2ForAudioFrameClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForAudioFrameClassification), /* harmony export */ "Wav2Vec2ForCTC": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForCTC), /* harmony export */ "Wav2Vec2ForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForSequenceClassification), /* harmony export */ "Wav2Vec2Model": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2Model), /* harmony export */ "Wav2Vec2PreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2PreTrainedModel), /* harmony export */ "Wav2Vec2ProcessorWithLM": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Wav2Vec2ProcessorWithLM), /* harmony export */ "WavLMForAudioFrameClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForAudioFrameClassification), /* harmony export */ "WavLMForCTC": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForCTC), /* harmony export */ "WavLMForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForSequenceClassification), /* harmony export */ "WavLMForXVector": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForXVector), /* harmony export */ "WavLMModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMModel), /* harmony export */ "WavLMPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMPreTrainedModel), /* harmony export */ "WhisperFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.WhisperFeatureExtractor), /* harmony export */ "WhisperForConditionalGeneration": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperForConditionalGeneration), /* harmony export */ "WhisperModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperModel), /* harmony export */ "WhisperPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperPreTrainedModel), /* harmony export */ "WhisperProcessor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.WhisperProcessor), /* harmony export */ "WhisperTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.WhisperTokenizer), /* harmony export */ "XLMForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForQuestionAnswering), /* harmony export */ "XLMForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForSequenceClassification), /* harmony export */ "XLMForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForTokenClassification), /* harmony export */ "XLMModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMModel), /* harmony export */ "XLMPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMPreTrainedModel), /* harmony export */ "XLMRobertaForMaskedLM": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForMaskedLM), /* harmony export */ "XLMRobertaForQuestionAnswering": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForQuestionAnswering), /* harmony export */ "XLMRobertaForSequenceClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForSequenceClassification), /* harmony export */ "XLMRobertaForTokenClassification": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForTokenClassification), /* harmony export */ "XLMRobertaModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaModel), /* harmony export */ "XLMRobertaPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaPreTrainedModel), /* harmony export */ "XLMRobertaTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMRobertaTokenizer), /* harmony export */ "XLMTokenizer": () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMTokenizer), /* harmony export */ "XLMWithLMHeadModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMWithLMHeadModel), /* harmony export */ "XVectorOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XVectorOutput), /* harmony export */ "YolosFeatureExtractor": () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.YolosFeatureExtractor), /* harmony export */ "YolosForObjectDetection": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosForObjectDetection), /* harmony export */ "YolosModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosModel), /* harmony export */ "YolosObjectDetectionOutput": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosObjectDetectionOutput), /* harmony export */ "YolosPreTrainedModel": () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosPreTrainedModel), /* harmony export */ "ZeroShotAudioClassificationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotAudioClassificationPipeline), /* harmony export */ "ZeroShotClassificationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotClassificationPipeline), /* harmony export */ "ZeroShotImageClassificationPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotImageClassificationPipeline), /* harmony export */ "ZeroShotObjectDetectionPipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotObjectDetectionPipeline), /* harmony export */ "bankers_round": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.bankers_round), /* harmony export */ "cat": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat), /* harmony export */ "cos_sim": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.cos_sim), /* harmony export */ "dot": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dot), /* harmony export */ "dynamicTimeWarping": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.dynamicTimeWarping), /* harmony export */ "env": () => (/* reexport safe */ _env_js__WEBPACK_IMPORTED_MODULE_0__.env), /* harmony export */ "full": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full), /* harmony export */ "full_like": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full_like), /* harmony export */ "getTopItems": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.getTopItems), /* harmony export */ "hanning": () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.hanning), /* harmony export */ "interpolate": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate), /* harmony export */ "interpolate_4d": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate_4d), /* harmony export */ "interpolate_data": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.interpolate_data), /* harmony export */ "layer_norm": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.layer_norm), /* harmony export */ "log_softmax": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.log_softmax), /* harmony export */ "magnitude": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.magnitude), /* harmony export */ "max": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.max), /* harmony export */ "mean": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean), /* harmony export */ "mean_pooling": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean_pooling), /* harmony export */ "medianFilter": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.medianFilter), /* harmony export */ "mel_filter_bank": () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank), /* harmony export */ "min": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.min), /* harmony export */ "ones": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones), /* harmony export */ "ones_like": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones_like), /* harmony export */ "permute": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.permute), /* harmony export */ "permute_data": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.permute_data), /* harmony export */ "pipeline": () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.pipeline), /* harmony export */ "quantize_embeddings": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.quantize_embeddings), /* harmony export */ "read_audio": () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.read_audio), /* harmony export */ "round": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.round), /* harmony export */ "softmax": () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.softmax), /* harmony export */ "spectrogram": () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram), /* harmony export */ "stack": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.stack), /* harmony export */ "std_mean": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.std_mean), /* harmony export */ "window_function": () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function), /* harmony export */ "zeros": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros), /* harmony export */ "zeros_like": () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros_like) /* harmony export */ }); /* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env.js */ "./src/env.js"); /* harmony import */ var _pipelines_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./pipelines.js */ "./src/pipelines.js"); /* harmony import */ var _models_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./models.js */ "./src/models.js"); /* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tokenizers.js */ "./src/tokenizers.js"); /* harmony import */ var _processors_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./processors.js */ "./src/processors.js"); /* harmony import */ var _configs_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./configs.js */ "./src/configs.js"); /* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); /* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); /* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); /* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); /* harmony import */ var _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./generation/streamers.js */ "./src/generation/streamers.js"); /** * @file Entry point for the Transformers.js library. Only the exports from this file * are available to the end user, and are grouped as follows: * * 1. [Pipelines](./pipelines) * 2. [Environment variables](./env) * 3. [Models](./models) * 4. [Tokenizers](./tokenizers) * 5. [Processors](./processors) * * @module transformers */ })(); var __webpack_exports__ASTFeatureExtractor = __webpack_exports__.ASTFeatureExtractor; var __webpack_exports__ASTForAudioClassification = __webpack_exports__.ASTForAudioClassification; var __webpack_exports__ASTModel = __webpack_exports__.ASTModel; var __webpack_exports__ASTPreTrainedModel = __webpack_exports__.ASTPreTrainedModel; var __webpack_exports__AlbertForMaskedLM = __webpack_exports__.AlbertForMaskedLM; var __webpack_exports__AlbertForQuestionAnswering = __webpack_exports__.AlbertForQuestionAnswering; var __webpack_exports__AlbertForSequenceClassification = __webpack_exports__.AlbertForSequenceClassification; var __webpack_exports__AlbertModel = __webpack_exports__.AlbertModel; var __webpack_exports__AlbertPreTrainedModel = __webpack_exports__.AlbertPreTrainedModel; var __webpack_exports__AlbertTokenizer = __webpack_exports__.AlbertTokenizer; var __webpack_exports__AudioClassificationPipeline = __webpack_exports__.AudioClassificationPipeline; var __webpack_exports__AutoConfig = __webpack_exports__.AutoConfig; var __webpack_exports__AutoModel = __webpack_exports__.AutoModel; var __webpack_exports__AutoModelForAudioClassification = __webpack_exports__.AutoModelForAudioClassification; var __webpack_exports__AutoModelForAudioFrameClassification = __webpack_exports__.AutoModelForAudioFrameClassification; var __webpack_exports__AutoModelForCTC = __webpack_exports__.AutoModelForCTC; var __webpack_exports__AutoModelForCausalLM = __webpack_exports__.AutoModelForCausalLM; var __webpack_exports__AutoModelForDepthEstimation = __webpack_exports__.AutoModelForDepthEstimation; var __webpack_exports__AutoModelForDocumentQuestionAnswering = __webpack_exports__.AutoModelForDocumentQuestionAnswering; var __webpack_exports__AutoModelForImageClassification = __webpack_exports__.AutoModelForImageClassification; var __webpack_exports__AutoModelForImageFeatureExtraction = __webpack_exports__.AutoModelForImageFeatureExtraction; var __webpack_exports__AutoModelForImageMatting = __webpack_exports__.AutoModelForImageMatting; var __webpack_exports__AutoModelForImageSegmentation = __webpack_exports__.AutoModelForImageSegmentation; var __webpack_exports__AutoModelForImageToImage = __webpack_exports__.AutoModelForImageToImage; var __webpack_exports__AutoModelForMaskGeneration = __webpack_exports__.AutoModelForMaskGeneration; var __webpack_exports__AutoModelForMaskedLM = __webpack_exports__.AutoModelForMaskedLM; var __webpack_exports__AutoModelForObjectDetection = __webpack_exports__.AutoModelForObjectDetection; var __webpack_exports__AutoModelForQuestionAnswering = __webpack_exports__.AutoModelForQuestionAnswering; var __webpack_exports__AutoModelForSemanticSegmentation = __webpack_exports__.AutoModelForSemanticSegmentation; var __webpack_exports__AutoModelForSeq2SeqLM = __webpack_exports__.AutoModelForSeq2SeqLM; var __webpack_exports__AutoModelForSequenceClassification = __webpack_exports__.AutoModelForSequenceClassification; var __webpack_exports__AutoModelForSpeechSeq2Seq = __webpack_exports__.AutoModelForSpeechSeq2Seq; var __webpack_exports__AutoModelForTextToSpectrogram = __webpack_exports__.AutoModelForTextToSpectrogram; var __webpack_exports__AutoModelForTextToWaveform = __webpack_exports__.AutoModelForTextToWaveform; var __webpack_exports__AutoModelForTokenClassification = __webpack_exports__.AutoModelForTokenClassification; var __webpack_exports__AutoModelForVision2Seq = __webpack_exports__.AutoModelForVision2Seq; var __webpack_exports__AutoModelForXVector = __webpack_exports__.AutoModelForXVector; var __webpack_exports__AutoModelForZeroShotObjectDetection = __webpack_exports__.AutoModelForZeroShotObjectDetection; var __webpack_exports__AutoProcessor = __webpack_exports__.AutoProcessor; var __webpack_exports__AutoTokenizer = __webpack_exports__.AutoTokenizer; var __webpack_exports__AutomaticSpeechRecognitionPipeline = __webpack_exports__.AutomaticSpeechRecognitionPipeline; var __webpack_exports__BartForConditionalGeneration = __webpack_exports__.BartForConditionalGeneration; var __webpack_exports__BartForSequenceClassification = __webpack_exports__.BartForSequenceClassification; var __webpack_exports__BartModel = __webpack_exports__.BartModel; var __webpack_exports__BartPretrainedModel = __webpack_exports__.BartPretrainedModel; var __webpack_exports__BartTokenizer = __webpack_exports__.BartTokenizer; var __webpack_exports__BaseModelOutput = __webpack_exports__.BaseModelOutput; var __webpack_exports__BaseStreamer = __webpack_exports__.BaseStreamer; var __webpack_exports__BeitFeatureExtractor = __webpack_exports__.BeitFeatureExtractor; var __webpack_exports__BeitForImageClassification = __webpack_exports__.BeitForImageClassification; var __webpack_exports__BeitModel = __webpack_exports__.BeitModel; var __webpack_exports__BeitPreTrainedModel = __webpack_exports__.BeitPreTrainedModel; var __webpack_exports__BertForMaskedLM = __webpack_exports__.BertForMaskedLM; var __webpack_exports__BertForQuestionAnswering = __webpack_exports__.BertForQuestionAnswering; var __webpack_exports__BertForSequenceClassification = __webpack_exports__.BertForSequenceClassification; var __webpack_exports__BertForTokenClassification = __webpack_exports__.BertForTokenClassification; var __webpack_exports__BertModel = __webpack_exports__.BertModel; var __webpack_exports__BertPreTrainedModel = __webpack_exports__.BertPreTrainedModel; var __webpack_exports__BertTokenizer = __webpack_exports__.BertTokenizer; var __webpack_exports__BitImageProcessor = __webpack_exports__.BitImageProcessor; var __webpack_exports__BlenderbotForConditionalGeneration = __webpack_exports__.BlenderbotForConditionalGeneration; var __webpack_exports__BlenderbotModel = __webpack_exports__.BlenderbotModel; var __webpack_exports__BlenderbotPreTrainedModel = __webpack_exports__.BlenderbotPreTrainedModel; var __webpack_exports__BlenderbotSmallForConditionalGeneration = __webpack_exports__.BlenderbotSmallForConditionalGeneration; var __webpack_exports__BlenderbotSmallModel = __webpack_exports__.BlenderbotSmallModel; var __webpack_exports__BlenderbotSmallPreTrainedModel = __webpack_exports__.BlenderbotSmallPreTrainedModel; var __webpack_exports__BlenderbotSmallTokenizer = __webpack_exports__.BlenderbotSmallTokenizer; var __webpack_exports__BlenderbotTokenizer = __webpack_exports__.BlenderbotTokenizer; var __webpack_exports__BloomForCausalLM = __webpack_exports__.BloomForCausalLM; var __webpack_exports__BloomModel = __webpack_exports__.BloomModel; var __webpack_exports__BloomPreTrainedModel = __webpack_exports__.BloomPreTrainedModel; var __webpack_exports__BloomTokenizer = __webpack_exports__.BloomTokenizer; var __webpack_exports__CLIPFeatureExtractor = __webpack_exports__.CLIPFeatureExtractor; var __webpack_exports__CLIPImageProcessor = __webpack_exports__.CLIPImageProcessor; var __webpack_exports__CLIPModel = __webpack_exports__.CLIPModel; var __webpack_exports__CLIPPreTrainedModel = __webpack_exports__.CLIPPreTrainedModel; var __webpack_exports__CLIPSegForImageSegmentation = __webpack_exports__.CLIPSegForImageSegmentation; var __webpack_exports__CLIPSegModel = __webpack_exports__.CLIPSegModel; var __webpack_exports__CLIPSegPreTrainedModel = __webpack_exports__.CLIPSegPreTrainedModel; var __webpack_exports__CLIPTextModelWithProjection = __webpack_exports__.CLIPTextModelWithProjection; var __webpack_exports__CLIPTokenizer = __webpack_exports__.CLIPTokenizer; var __webpack_exports__CLIPVisionModelWithProjection = __webpack_exports__.CLIPVisionModelWithProjection; var __webpack_exports__CamembertForMaskedLM = __webpack_exports__.CamembertForMaskedLM; var __webpack_exports__CamembertForQuestionAnswering = __webpack_exports__.CamembertForQuestionAnswering; var __webpack_exports__CamembertForSequenceClassification = __webpack_exports__.CamembertForSequenceClassification; var __webpack_exports__CamembertForTokenClassification = __webpack_exports__.CamembertForTokenClassification; var __webpack_exports__CamembertModel = __webpack_exports__.CamembertModel; var __webpack_exports__CamembertPreTrainedModel = __webpack_exports__.CamembertPreTrainedModel; var __webpack_exports__CamembertTokenizer = __webpack_exports__.CamembertTokenizer; var __webpack_exports__CausalLMOutput = __webpack_exports__.CausalLMOutput; var __webpack_exports__CausalLMOutputWithPast = __webpack_exports__.CausalLMOutputWithPast; var __webpack_exports__ChineseCLIPFeatureExtractor = __webpack_exports__.ChineseCLIPFeatureExtractor; var __webpack_exports__ChineseCLIPModel = __webpack_exports__.ChineseCLIPModel; var __webpack_exports__ChineseCLIPPreTrainedModel = __webpack_exports__.ChineseCLIPPreTrainedModel; var __webpack_exports__ClapAudioModelWithProjection = __webpack_exports__.ClapAudioModelWithProjection; var __webpack_exports__ClapFeatureExtractor = __webpack_exports__.ClapFeatureExtractor; var __webpack_exports__ClapModel = __webpack_exports__.ClapModel; var __webpack_exports__ClapPreTrainedModel = __webpack_exports__.ClapPreTrainedModel; var __webpack_exports__ClapTextModelWithProjection = __webpack_exports__.ClapTextModelWithProjection; var __webpack_exports__CodeGenForCausalLM = __webpack_exports__.CodeGenForCausalLM; var __webpack_exports__CodeGenModel = __webpack_exports__.CodeGenModel; var __webpack_exports__CodeGenPreTrainedModel = __webpack_exports__.CodeGenPreTrainedModel; var __webpack_exports__CodeGenTokenizer = __webpack_exports__.CodeGenTokenizer; var __webpack_exports__CodeLlamaTokenizer = __webpack_exports__.CodeLlamaTokenizer; var __webpack_exports__CohereTokenizer = __webpack_exports__.CohereTokenizer; var __webpack_exports__ConvBertForMaskedLM = __webpack_exports__.ConvBertForMaskedLM; var __webpack_exports__ConvBertForQuestionAnswering = __webpack_exports__.ConvBertForQuestionAnswering; var __webpack_exports__ConvBertForSequenceClassification = __webpack_exports__.ConvBertForSequenceClassification; var __webpack_exports__ConvBertForTokenClassification = __webpack_exports__.ConvBertForTokenClassification; var __webpack_exports__ConvBertModel = __webpack_exports__.ConvBertModel; var __webpack_exports__ConvBertPreTrainedModel = __webpack_exports__.ConvBertPreTrainedModel; var __webpack_exports__ConvBertTokenizer = __webpack_exports__.ConvBertTokenizer; var __webpack_exports__ConvNextFeatureExtractor = __webpack_exports__.ConvNextFeatureExtractor; var __webpack_exports__ConvNextForImageClassification = __webpack_exports__.ConvNextForImageClassification; var __webpack_exports__ConvNextImageProcessor = __webpack_exports__.ConvNextImageProcessor; var __webpack_exports__ConvNextModel = __webpack_exports__.ConvNextModel; var __webpack_exports__ConvNextPreTrainedModel = __webpack_exports__.ConvNextPreTrainedModel; var __webpack_exports__ConvNextV2ForImageClassification = __webpack_exports__.ConvNextV2ForImageClassification; var __webpack_exports__ConvNextV2Model = __webpack_exports__.ConvNextV2Model; var __webpack_exports__ConvNextV2PreTrainedModel = __webpack_exports__.ConvNextV2PreTrainedModel; var __webpack_exports__DPTFeatureExtractor = __webpack_exports__.DPTFeatureExtractor; var __webpack_exports__DPTForDepthEstimation = __webpack_exports__.DPTForDepthEstimation; var __webpack_exports__DPTImageProcessor = __webpack_exports__.DPTImageProcessor; var __webpack_exports__DPTModel = __webpack_exports__.DPTModel; var __webpack_exports__DPTPreTrainedModel = __webpack_exports__.DPTPreTrainedModel; var __webpack_exports__DebertaForMaskedLM = __webpack_exports__.DebertaForMaskedLM; var __webpack_exports__DebertaForQuestionAnswering = __webpack_exports__.DebertaForQuestionAnswering; var __webpack_exports__DebertaForSequenceClassification = __webpack_exports__.DebertaForSequenceClassification; var __webpack_exports__DebertaForTokenClassification = __webpack_exports__.DebertaForTokenClassification; var __webpack_exports__DebertaModel = __webpack_exports__.DebertaModel; var __webpack_exports__DebertaPreTrainedModel = __webpack_exports__.DebertaPreTrainedModel; var __webpack_exports__DebertaTokenizer = __webpack_exports__.DebertaTokenizer; var __webpack_exports__DebertaV2ForMaskedLM = __webpack_exports__.DebertaV2ForMaskedLM; var __webpack_exports__DebertaV2ForQuestionAnswering = __webpack_exports__.DebertaV2ForQuestionAnswering; var __webpack_exports__DebertaV2ForSequenceClassification = __webpack_exports__.DebertaV2ForSequenceClassification; var __webpack_exports__DebertaV2ForTokenClassification = __webpack_exports__.DebertaV2ForTokenClassification; var __webpack_exports__DebertaV2Model = __webpack_exports__.DebertaV2Model; var __webpack_exports__DebertaV2PreTrainedModel = __webpack_exports__.DebertaV2PreTrainedModel; var __webpack_exports__DebertaV2Tokenizer = __webpack_exports__.DebertaV2Tokenizer; var __webpack_exports__DeiTFeatureExtractor = __webpack_exports__.DeiTFeatureExtractor; var __webpack_exports__DeiTForImageClassification = __webpack_exports__.DeiTForImageClassification; var __webpack_exports__DeiTModel = __webpack_exports__.DeiTModel; var __webpack_exports__DeiTPreTrainedModel = __webpack_exports__.DeiTPreTrainedModel; var __webpack_exports__DepthAnythingForDepthEstimation = __webpack_exports__.DepthAnythingForDepthEstimation; var __webpack_exports__DepthAnythingPreTrainedModel = __webpack_exports__.DepthAnythingPreTrainedModel; var __webpack_exports__DepthEstimationPipeline = __webpack_exports__.DepthEstimationPipeline; var __webpack_exports__DetrFeatureExtractor = __webpack_exports__.DetrFeatureExtractor; var __webpack_exports__DetrForObjectDetection = __webpack_exports__.DetrForObjectDetection; var __webpack_exports__DetrForSegmentation = __webpack_exports__.DetrForSegmentation; var __webpack_exports__DetrModel = __webpack_exports__.DetrModel; var __webpack_exports__DetrObjectDetectionOutput = __webpack_exports__.DetrObjectDetectionOutput; var __webpack_exports__DetrPreTrainedModel = __webpack_exports__.DetrPreTrainedModel; var __webpack_exports__DetrSegmentationOutput = __webpack_exports__.DetrSegmentationOutput; var __webpack_exports__Dinov2ForImageClassification = __webpack_exports__.Dinov2ForImageClassification; var __webpack_exports__Dinov2Model = __webpack_exports__.Dinov2Model; var __webpack_exports__Dinov2PreTrainedModel = __webpack_exports__.Dinov2PreTrainedModel; var __webpack_exports__DistilBertForMaskedLM = __webpack_exports__.DistilBertForMaskedLM; var __webpack_exports__DistilBertForQuestionAnswering = __webpack_exports__.DistilBertForQuestionAnswering; var __webpack_exports__DistilBertForSequenceClassification = __webpack_exports__.DistilBertForSequenceClassification; var __webpack_exports__DistilBertForTokenClassification = __webpack_exports__.DistilBertForTokenClassification; var __webpack_exports__DistilBertModel = __webpack_exports__.DistilBertModel; var __webpack_exports__DistilBertPreTrainedModel = __webpack_exports__.DistilBertPreTrainedModel; var __webpack_exports__DistilBertTokenizer = __webpack_exports__.DistilBertTokenizer; var __webpack_exports__DocumentQuestionAnsweringPipeline = __webpack_exports__.DocumentQuestionAnsweringPipeline; var __webpack_exports__DonutFeatureExtractor = __webpack_exports__.DonutFeatureExtractor; var __webpack_exports__DonutSwinModel = __webpack_exports__.DonutSwinModel; var __webpack_exports__DonutSwinPreTrainedModel = __webpack_exports__.DonutSwinPreTrainedModel; var __webpack_exports__EfficientNetForImageClassification = __webpack_exports__.EfficientNetForImageClassification; var __webpack_exports__EfficientNetImageProcessor = __webpack_exports__.EfficientNetImageProcessor; var __webpack_exports__EfficientNetModel = __webpack_exports__.EfficientNetModel; var __webpack_exports__EfficientNetPreTrainedModel = __webpack_exports__.EfficientNetPreTrainedModel; var __webpack_exports__ElectraForMaskedLM = __webpack_exports__.ElectraForMaskedLM; var __webpack_exports__ElectraForQuestionAnswering = __webpack_exports__.ElectraForQuestionAnswering; var __webpack_exports__ElectraForSequenceClassification = __webpack_exports__.ElectraForSequenceClassification; var __webpack_exports__ElectraForTokenClassification = __webpack_exports__.ElectraForTokenClassification; var __webpack_exports__ElectraModel = __webpack_exports__.ElectraModel; var __webpack_exports__ElectraPreTrainedModel = __webpack_exports__.ElectraPreTrainedModel; var __webpack_exports__ElectraTokenizer = __webpack_exports__.ElectraTokenizer; var __webpack_exports__EsmForMaskedLM = __webpack_exports__.EsmForMaskedLM; var __webpack_exports__EsmForSequenceClassification = __webpack_exports__.EsmForSequenceClassification; var __webpack_exports__EsmForTokenClassification = __webpack_exports__.EsmForTokenClassification; var __webpack_exports__EsmModel = __webpack_exports__.EsmModel; var __webpack_exports__EsmPreTrainedModel = __webpack_exports__.EsmPreTrainedModel; var __webpack_exports__EsmTokenizer = __webpack_exports__.EsmTokenizer; var __webpack_exports__FFT = __webpack_exports__.FFT; var __webpack_exports__FalconForCausalLM = __webpack_exports__.FalconForCausalLM; var __webpack_exports__FalconModel = __webpack_exports__.FalconModel; var __webpack_exports__FalconPreTrainedModel = __webpack_exports__.FalconPreTrainedModel; var __webpack_exports__FalconTokenizer = __webpack_exports__.FalconTokenizer; var __webpack_exports__FeatureExtractionPipeline = __webpack_exports__.FeatureExtractionPipeline; var __webpack_exports__FeatureExtractor = __webpack_exports__.FeatureExtractor; var __webpack_exports__FillMaskPipeline = __webpack_exports__.FillMaskPipeline; var __webpack_exports__GLPNFeatureExtractor = __webpack_exports__.GLPNFeatureExtractor; var __webpack_exports__GLPNForDepthEstimation = __webpack_exports__.GLPNForDepthEstimation; var __webpack_exports__GLPNModel = __webpack_exports__.GLPNModel; var __webpack_exports__GLPNPreTrainedModel = __webpack_exports__.GLPNPreTrainedModel; var __webpack_exports__GPT2LMHeadModel = __webpack_exports__.GPT2LMHeadModel; var __webpack_exports__GPT2Model = __webpack_exports__.GPT2Model; var __webpack_exports__GPT2PreTrainedModel = __webpack_exports__.GPT2PreTrainedModel; var __webpack_exports__GPT2Tokenizer = __webpack_exports__.GPT2Tokenizer; var __webpack_exports__GPTBigCodeForCausalLM = __webpack_exports__.GPTBigCodeForCausalLM; var __webpack_exports__GPTBigCodeModel = __webpack_exports__.GPTBigCodeModel; var __webpack_exports__GPTBigCodePreTrainedModel = __webpack_exports__.GPTBigCodePreTrainedModel; var __webpack_exports__GPTJForCausalLM = __webpack_exports__.GPTJForCausalLM; var __webpack_exports__GPTJModel = __webpack_exports__.GPTJModel; var __webpack_exports__GPTJPreTrainedModel = __webpack_exports__.GPTJPreTrainedModel; var __webpack_exports__GPTNeoForCausalLM = __webpack_exports__.GPTNeoForCausalLM; var __webpack_exports__GPTNeoModel = __webpack_exports__.GPTNeoModel; var __webpack_exports__GPTNeoPreTrainedModel = __webpack_exports__.GPTNeoPreTrainedModel; var __webpack_exports__GPTNeoXForCausalLM = __webpack_exports__.GPTNeoXForCausalLM; var __webpack_exports__GPTNeoXModel = __webpack_exports__.GPTNeoXModel; var __webpack_exports__GPTNeoXPreTrainedModel = __webpack_exports__.GPTNeoXPreTrainedModel; var __webpack_exports__GPTNeoXTokenizer = __webpack_exports__.GPTNeoXTokenizer; var __webpack_exports__GemmaTokenizer = __webpack_exports__.GemmaTokenizer; var __webpack_exports__Grok1Tokenizer = __webpack_exports__.Grok1Tokenizer; var __webpack_exports__HerbertTokenizer = __webpack_exports__.HerbertTokenizer; var __webpack_exports__HubertForCTC = __webpack_exports__.HubertForCTC; var __webpack_exports__HubertForSequenceClassification = __webpack_exports__.HubertForSequenceClassification; var __webpack_exports__HubertModel = __webpack_exports__.HubertModel; var __webpack_exports__HubertPreTrainedModel = __webpack_exports__.HubertPreTrainedModel; var __webpack_exports__ImageClassificationPipeline = __webpack_exports__.ImageClassificationPipeline; var __webpack_exports__ImageFeatureExtractionPipeline = __webpack_exports__.ImageFeatureExtractionPipeline; var __webpack_exports__ImageFeatureExtractor = __webpack_exports__.ImageFeatureExtractor; var __webpack_exports__ImageMattingOutput = __webpack_exports__.ImageMattingOutput; var __webpack_exports__ImageSegmentationPipeline = __webpack_exports__.ImageSegmentationPipeline; var __webpack_exports__ImageToImagePipeline = __webpack_exports__.ImageToImagePipeline; var __webpack_exports__ImageToTextPipeline = __webpack_exports__.ImageToTextPipeline; var __webpack_exports__LlamaForCausalLM = __webpack_exports__.LlamaForCausalLM; var __webpack_exports__LlamaModel = __webpack_exports__.LlamaModel; var __webpack_exports__LlamaPreTrainedModel = __webpack_exports__.LlamaPreTrainedModel; var __webpack_exports__LlamaTokenizer = __webpack_exports__.LlamaTokenizer; var __webpack_exports__LlavaForConditionalGeneration = __webpack_exports__.LlavaForConditionalGeneration; var __webpack_exports__LlavaPreTrainedModel = __webpack_exports__.LlavaPreTrainedModel; var __webpack_exports__LongT5ForConditionalGeneration = __webpack_exports__.LongT5ForConditionalGeneration; var __webpack_exports__LongT5Model = __webpack_exports__.LongT5Model; var __webpack_exports__LongT5PreTrainedModel = __webpack_exports__.LongT5PreTrainedModel; var __webpack_exports__M2M100ForConditionalGeneration = __webpack_exports__.M2M100ForConditionalGeneration; var __webpack_exports__M2M100Model = __webpack_exports__.M2M100Model; var __webpack_exports__M2M100PreTrainedModel = __webpack_exports__.M2M100PreTrainedModel; var __webpack_exports__M2M100Tokenizer = __webpack_exports__.M2M100Tokenizer; var __webpack_exports__MBart50Tokenizer = __webpack_exports__.MBart50Tokenizer; var __webpack_exports__MBartForCausalLM = __webpack_exports__.MBartForCausalLM; var __webpack_exports__MBartForConditionalGeneration = __webpack_exports__.MBartForConditionalGeneration; var __webpack_exports__MBartForSequenceClassification = __webpack_exports__.MBartForSequenceClassification; var __webpack_exports__MBartModel = __webpack_exports__.MBartModel; var __webpack_exports__MBartPreTrainedModel = __webpack_exports__.MBartPreTrainedModel; var __webpack_exports__MBartTokenizer = __webpack_exports__.MBartTokenizer; var __webpack_exports__MPNetForMaskedLM = __webpack_exports__.MPNetForMaskedLM; var __webpack_exports__MPNetForQuestionAnswering = __webpack_exports__.MPNetForQuestionAnswering; var __webpack_exports__MPNetForSequenceClassification = __webpack_exports__.MPNetForSequenceClassification; var __webpack_exports__MPNetForTokenClassification = __webpack_exports__.MPNetForTokenClassification; var __webpack_exports__MPNetModel = __webpack_exports__.MPNetModel; var __webpack_exports__MPNetPreTrainedModel = __webpack_exports__.MPNetPreTrainedModel; var __webpack_exports__MPNetTokenizer = __webpack_exports__.MPNetTokenizer; var __webpack_exports__MT5ForConditionalGeneration = __webpack_exports__.MT5ForConditionalGeneration; var __webpack_exports__MT5Model = __webpack_exports__.MT5Model; var __webpack_exports__MT5PreTrainedModel = __webpack_exports__.MT5PreTrainedModel; var __webpack_exports__MarianMTModel = __webpack_exports__.MarianMTModel; var __webpack_exports__MarianModel = __webpack_exports__.MarianModel; var __webpack_exports__MarianPreTrainedModel = __webpack_exports__.MarianPreTrainedModel; var __webpack_exports__MarianTokenizer = __webpack_exports__.MarianTokenizer; var __webpack_exports__MaskedLMOutput = __webpack_exports__.MaskedLMOutput; var __webpack_exports__MistralForCausalLM = __webpack_exports__.MistralForCausalLM; var __webpack_exports__MistralModel = __webpack_exports__.MistralModel; var __webpack_exports__MistralPreTrainedModel = __webpack_exports__.MistralPreTrainedModel; var __webpack_exports__MobileBertForMaskedLM = __webpack_exports__.MobileBertForMaskedLM; var __webpack_exports__MobileBertForQuestionAnswering = __webpack_exports__.MobileBertForQuestionAnswering; var __webpack_exports__MobileBertForSequenceClassification = __webpack_exports__.MobileBertForSequenceClassification; var __webpack_exports__MobileBertModel = __webpack_exports__.MobileBertModel; var __webpack_exports__MobileBertPreTrainedModel = __webpack_exports__.MobileBertPreTrainedModel; var __webpack_exports__MobileBertTokenizer = __webpack_exports__.MobileBertTokenizer; var __webpack_exports__MobileViTFeatureExtractor = __webpack_exports__.MobileViTFeatureExtractor; var __webpack_exports__MobileViTForImageClassification = __webpack_exports__.MobileViTForImageClassification; var __webpack_exports__MobileViTModel = __webpack_exports__.MobileViTModel; var __webpack_exports__MobileViTPreTrainedModel = __webpack_exports__.MobileViTPreTrainedModel; var __webpack_exports__ModelOutput = __webpack_exports__.ModelOutput; var __webpack_exports__MptForCausalLM = __webpack_exports__.MptForCausalLM; var __webpack_exports__MptModel = __webpack_exports__.MptModel; var __webpack_exports__MptPreTrainedModel = __webpack_exports__.MptPreTrainedModel; var __webpack_exports__MusicgenForCausalLM = __webpack_exports__.MusicgenForCausalLM; var __webpack_exports__MusicgenForConditionalGeneration = __webpack_exports__.MusicgenForConditionalGeneration; var __webpack_exports__MusicgenModel = __webpack_exports__.MusicgenModel; var __webpack_exports__MusicgenPreTrainedModel = __webpack_exports__.MusicgenPreTrainedModel; var __webpack_exports__NllbTokenizer = __webpack_exports__.NllbTokenizer; var __webpack_exports__NomicBertModel = __webpack_exports__.NomicBertModel; var __webpack_exports__NomicBertPreTrainedModel = __webpack_exports__.NomicBertPreTrainedModel; var __webpack_exports__NougatImageProcessor = __webpack_exports__.NougatImageProcessor; var __webpack_exports__NougatTokenizer = __webpack_exports__.NougatTokenizer; var __webpack_exports__OPTForCausalLM = __webpack_exports__.OPTForCausalLM; var __webpack_exports__OPTModel = __webpack_exports__.OPTModel; var __webpack_exports__OPTPreTrainedModel = __webpack_exports__.OPTPreTrainedModel; var __webpack_exports__ObjectDetectionPipeline = __webpack_exports__.ObjectDetectionPipeline; var __webpack_exports__OwlViTFeatureExtractor = __webpack_exports__.OwlViTFeatureExtractor; var __webpack_exports__OwlViTForObjectDetection = __webpack_exports__.OwlViTForObjectDetection; var __webpack_exports__OwlViTModel = __webpack_exports__.OwlViTModel; var __webpack_exports__OwlViTPreTrainedModel = __webpack_exports__.OwlViTPreTrainedModel; var __webpack_exports__OwlViTProcessor = __webpack_exports__.OwlViTProcessor; var __webpack_exports__Owlv2ForObjectDetection = __webpack_exports__.Owlv2ForObjectDetection; var __webpack_exports__Owlv2ImageProcessor = __webpack_exports__.Owlv2ImageProcessor; var __webpack_exports__Owlv2Model = __webpack_exports__.Owlv2Model; var __webpack_exports__Owlv2PreTrainedModel = __webpack_exports__.Owlv2PreTrainedModel; var __webpack_exports__PhiForCausalLM = __webpack_exports__.PhiForCausalLM; var __webpack_exports__PhiModel = __webpack_exports__.PhiModel; var __webpack_exports__PhiPreTrainedModel = __webpack_exports__.PhiPreTrainedModel; var __webpack_exports__Pipeline = __webpack_exports__.Pipeline; var __webpack_exports__PreTrainedModel = __webpack_exports__.PreTrainedModel; var __webpack_exports__PreTrainedTokenizer = __webpack_exports__.PreTrainedTokenizer; var __webpack_exports__PretrainedConfig = __webpack_exports__.PretrainedConfig; var __webpack_exports__PretrainedMixin = __webpack_exports__.PretrainedMixin; var __webpack_exports__Processor = __webpack_exports__.Processor; var __webpack_exports__QuestionAnsweringModelOutput = __webpack_exports__.QuestionAnsweringModelOutput; var __webpack_exports__QuestionAnsweringPipeline = __webpack_exports__.QuestionAnsweringPipeline; var __webpack_exports__Qwen2ForCausalLM = __webpack_exports__.Qwen2ForCausalLM; var __webpack_exports__Qwen2Model = __webpack_exports__.Qwen2Model; var __webpack_exports__Qwen2PreTrainedModel = __webpack_exports__.Qwen2PreTrainedModel; var __webpack_exports__Qwen2Tokenizer = __webpack_exports__.Qwen2Tokenizer; var __webpack_exports__RawImage = __webpack_exports__.RawImage; var __webpack_exports__ResNetForImageClassification = __webpack_exports__.ResNetForImageClassification; var __webpack_exports__ResNetModel = __webpack_exports__.ResNetModel; var __webpack_exports__ResNetPreTrainedModel = __webpack_exports__.ResNetPreTrainedModel; var __webpack_exports__RoFormerForMaskedLM = __webpack_exports__.RoFormerForMaskedLM; var __webpack_exports__RoFormerForQuestionAnswering = __webpack_exports__.RoFormerForQuestionAnswering; var __webpack_exports__RoFormerForSequenceClassification = __webpack_exports__.RoFormerForSequenceClassification; var __webpack_exports__RoFormerForTokenClassification = __webpack_exports__.RoFormerForTokenClassification; var __webpack_exports__RoFormerModel = __webpack_exports__.RoFormerModel; var __webpack_exports__RoFormerPreTrainedModel = __webpack_exports__.RoFormerPreTrainedModel; var __webpack_exports__RoFormerTokenizer = __webpack_exports__.RoFormerTokenizer; var __webpack_exports__RobertaForMaskedLM = __webpack_exports__.RobertaForMaskedLM; var __webpack_exports__RobertaForQuestionAnswering = __webpack_exports__.RobertaForQuestionAnswering; var __webpack_exports__RobertaForSequenceClassification = __webpack_exports__.RobertaForSequenceClassification; var __webpack_exports__RobertaForTokenClassification = __webpack_exports__.RobertaForTokenClassification; var __webpack_exports__RobertaModel = __webpack_exports__.RobertaModel; var __webpack_exports__RobertaPreTrainedModel = __webpack_exports__.RobertaPreTrainedModel; var __webpack_exports__RobertaTokenizer = __webpack_exports__.RobertaTokenizer; var __webpack_exports__SamImageProcessor = __webpack_exports__.SamImageProcessor; var __webpack_exports__SamImageSegmentationOutput = __webpack_exports__.SamImageSegmentationOutput; var __webpack_exports__SamModel = __webpack_exports__.SamModel; var __webpack_exports__SamPreTrainedModel = __webpack_exports__.SamPreTrainedModel; var __webpack_exports__SamProcessor = __webpack_exports__.SamProcessor; var __webpack_exports__SeamlessM4TFeatureExtractor = __webpack_exports__.SeamlessM4TFeatureExtractor; var __webpack_exports__SegformerFeatureExtractor = __webpack_exports__.SegformerFeatureExtractor; var __webpack_exports__SegformerForImageClassification = __webpack_exports__.SegformerForImageClassification; var __webpack_exports__SegformerForSemanticSegmentation = __webpack_exports__.SegformerForSemanticSegmentation; var __webpack_exports__SegformerModel = __webpack_exports__.SegformerModel; var __webpack_exports__SegformerPreTrainedModel = __webpack_exports__.SegformerPreTrainedModel; var __webpack_exports__Seq2SeqLMOutput = __webpack_exports__.Seq2SeqLMOutput; var __webpack_exports__SequenceClassifierOutput = __webpack_exports__.SequenceClassifierOutput; var __webpack_exports__SiglipImageProcessor = __webpack_exports__.SiglipImageProcessor; var __webpack_exports__SiglipModel = __webpack_exports__.SiglipModel; var __webpack_exports__SiglipPreTrainedModel = __webpack_exports__.SiglipPreTrainedModel; var __webpack_exports__SiglipTextModel = __webpack_exports__.SiglipTextModel; var __webpack_exports__SiglipTokenizer = __webpack_exports__.SiglipTokenizer; var __webpack_exports__SiglipVisionModel = __webpack_exports__.SiglipVisionModel; var __webpack_exports__SpeechT5FeatureExtractor = __webpack_exports__.SpeechT5FeatureExtractor; var __webpack_exports__SpeechT5ForSpeechToText = __webpack_exports__.SpeechT5ForSpeechToText; var __webpack_exports__SpeechT5ForTextToSpeech = __webpack_exports__.SpeechT5ForTextToSpeech; var __webpack_exports__SpeechT5HifiGan = __webpack_exports__.SpeechT5HifiGan; var __webpack_exports__SpeechT5Model = __webpack_exports__.SpeechT5Model; var __webpack_exports__SpeechT5PreTrainedModel = __webpack_exports__.SpeechT5PreTrainedModel; var __webpack_exports__SpeechT5Processor = __webpack_exports__.SpeechT5Processor; var __webpack_exports__SpeechT5Tokenizer = __webpack_exports__.SpeechT5Tokenizer; var __webpack_exports__SqueezeBertForMaskedLM = __webpack_exports__.SqueezeBertForMaskedLM; var __webpack_exports__SqueezeBertForQuestionAnswering = __webpack_exports__.SqueezeBertForQuestionAnswering; var __webpack_exports__SqueezeBertForSequenceClassification = __webpack_exports__.SqueezeBertForSequenceClassification; var __webpack_exports__SqueezeBertModel = __webpack_exports__.SqueezeBertModel; var __webpack_exports__SqueezeBertPreTrainedModel = __webpack_exports__.SqueezeBertPreTrainedModel; var __webpack_exports__SqueezeBertTokenizer = __webpack_exports__.SqueezeBertTokenizer; var __webpack_exports__StableLmForCausalLM = __webpack_exports__.StableLmForCausalLM; var __webpack_exports__StableLmModel = __webpack_exports__.StableLmModel; var __webpack_exports__StableLmPreTrainedModel = __webpack_exports__.StableLmPreTrainedModel; var __webpack_exports__Starcoder2ForCausalLM = __webpack_exports__.Starcoder2ForCausalLM; var __webpack_exports__Starcoder2Model = __webpack_exports__.Starcoder2Model; var __webpack_exports__Starcoder2PreTrainedModel = __webpack_exports__.Starcoder2PreTrainedModel; var __webpack_exports__SummarizationPipeline = __webpack_exports__.SummarizationPipeline; var __webpack_exports__Swin2SRForImageSuperResolution = __webpack_exports__.Swin2SRForImageSuperResolution; var __webpack_exports__Swin2SRImageProcessor = __webpack_exports__.Swin2SRImageProcessor; var __webpack_exports__Swin2SRModel = __webpack_exports__.Swin2SRModel; var __webpack_exports__Swin2SRPreTrainedModel = __webpack_exports__.Swin2SRPreTrainedModel; var __webpack_exports__SwinForImageClassification = __webpack_exports__.SwinForImageClassification; var __webpack_exports__SwinModel = __webpack_exports__.SwinModel; var __webpack_exports__SwinPreTrainedModel = __webpack_exports__.SwinPreTrainedModel; var __webpack_exports__T5ForConditionalGeneration = __webpack_exports__.T5ForConditionalGeneration; var __webpack_exports__T5Model = __webpack_exports__.T5Model; var __webpack_exports__T5PreTrainedModel = __webpack_exports__.T5PreTrainedModel; var __webpack_exports__T5Tokenizer = __webpack_exports__.T5Tokenizer; var __webpack_exports__TableTransformerForObjectDetection = __webpack_exports__.TableTransformerForObjectDetection; var __webpack_exports__TableTransformerModel = __webpack_exports__.TableTransformerModel; var __webpack_exports__TableTransformerObjectDetectionOutput = __webpack_exports__.TableTransformerObjectDetectionOutput; var __webpack_exports__TableTransformerPreTrainedModel = __webpack_exports__.TableTransformerPreTrainedModel; var __webpack_exports__Tensor = __webpack_exports__.Tensor; var __webpack_exports__Text2TextGenerationPipeline = __webpack_exports__.Text2TextGenerationPipeline; var __webpack_exports__TextClassificationPipeline = __webpack_exports__.TextClassificationPipeline; var __webpack_exports__TextGenerationPipeline = __webpack_exports__.TextGenerationPipeline; var __webpack_exports__TextToAudioPipeline = __webpack_exports__.TextToAudioPipeline; var __webpack_exports__TokenClassificationPipeline = __webpack_exports__.TokenClassificationPipeline; var __webpack_exports__TokenClassifierOutput = __webpack_exports__.TokenClassifierOutput; var __webpack_exports__TokenizerModel = __webpack_exports__.TokenizerModel; var __webpack_exports__TrOCRForCausalLM = __webpack_exports__.TrOCRForCausalLM; var __webpack_exports__TrOCRPreTrainedModel = __webpack_exports__.TrOCRPreTrainedModel; var __webpack_exports__TranslationPipeline = __webpack_exports__.TranslationPipeline; var __webpack_exports__UniSpeechForCTC = __webpack_exports__.UniSpeechForCTC; var __webpack_exports__UniSpeechForSequenceClassification = __webpack_exports__.UniSpeechForSequenceClassification; var __webpack_exports__UniSpeechModel = __webpack_exports__.UniSpeechModel; var __webpack_exports__UniSpeechPreTrainedModel = __webpack_exports__.UniSpeechPreTrainedModel; var __webpack_exports__UniSpeechSatForAudioFrameClassification = __webpack_exports__.UniSpeechSatForAudioFrameClassification; var __webpack_exports__UniSpeechSatForCTC = __webpack_exports__.UniSpeechSatForCTC; var __webpack_exports__UniSpeechSatForSequenceClassification = __webpack_exports__.UniSpeechSatForSequenceClassification; var __webpack_exports__UniSpeechSatModel = __webpack_exports__.UniSpeechSatModel; var __webpack_exports__UniSpeechSatPreTrainedModel = __webpack_exports__.UniSpeechSatPreTrainedModel; var __webpack_exports__ViTFeatureExtractor = __webpack_exports__.ViTFeatureExtractor; var __webpack_exports__ViTForImageClassification = __webpack_exports__.ViTForImageClassification; var __webpack_exports__ViTImageProcessor = __webpack_exports__.ViTImageProcessor; var __webpack_exports__ViTModel = __webpack_exports__.ViTModel; var __webpack_exports__ViTPreTrainedModel = __webpack_exports__.ViTPreTrainedModel; var __webpack_exports__VisionEncoderDecoderModel = __webpack_exports__.VisionEncoderDecoderModel; var __webpack_exports__VitMatteForImageMatting = __webpack_exports__.VitMatteForImageMatting; var __webpack_exports__VitMatteImageProcessor = __webpack_exports__.VitMatteImageProcessor; var __webpack_exports__VitMattePreTrainedModel = __webpack_exports__.VitMattePreTrainedModel; var __webpack_exports__VitsModel = __webpack_exports__.VitsModel; var __webpack_exports__VitsModelOutput = __webpack_exports__.VitsModelOutput; var __webpack_exports__VitsPreTrainedModel = __webpack_exports__.VitsPreTrainedModel; var __webpack_exports__VitsTokenizer = __webpack_exports__.VitsTokenizer; var __webpack_exports__Wav2Vec2BertForCTC = __webpack_exports__.Wav2Vec2BertForCTC; var __webpack_exports__Wav2Vec2BertForSequenceClassification = __webpack_exports__.Wav2Vec2BertForSequenceClassification; var __webpack_exports__Wav2Vec2BertModel = __webpack_exports__.Wav2Vec2BertModel; var __webpack_exports__Wav2Vec2BertPreTrainedModel = __webpack_exports__.Wav2Vec2BertPreTrainedModel; var __webpack_exports__Wav2Vec2CTCTokenizer = __webpack_exports__.Wav2Vec2CTCTokenizer; var __webpack_exports__Wav2Vec2FeatureExtractor = __webpack_exports__.Wav2Vec2FeatureExtractor; var __webpack_exports__Wav2Vec2ForAudioFrameClassification = __webpack_exports__.Wav2Vec2ForAudioFrameClassification; var __webpack_exports__Wav2Vec2ForCTC = __webpack_exports__.Wav2Vec2ForCTC; var __webpack_exports__Wav2Vec2ForSequenceClassification = __webpack_exports__.Wav2Vec2ForSequenceClassification; var __webpack_exports__Wav2Vec2Model = __webpack_exports__.Wav2Vec2Model; var __webpack_exports__Wav2Vec2PreTrainedModel = __webpack_exports__.Wav2Vec2PreTrainedModel; var __webpack_exports__Wav2Vec2ProcessorWithLM = __webpack_exports__.Wav2Vec2ProcessorWithLM; var __webpack_exports__WavLMForAudioFrameClassification = __webpack_exports__.WavLMForAudioFrameClassification; var __webpack_exports__WavLMForCTC = __webpack_exports__.WavLMForCTC; var __webpack_exports__WavLMForSequenceClassification = __webpack_exports__.WavLMForSequenceClassification; var __webpack_exports__WavLMForXVector = __webpack_exports__.WavLMForXVector; var __webpack_exports__WavLMModel = __webpack_exports__.WavLMModel; var __webpack_exports__WavLMPreTrainedModel = __webpack_exports__.WavLMPreTrainedModel; var __webpack_exports__WhisperFeatureExtractor = __webpack_exports__.WhisperFeatureExtractor; var __webpack_exports__WhisperForConditionalGeneration = __webpack_exports__.WhisperForConditionalGeneration; var __webpack_exports__WhisperModel = __webpack_exports__.WhisperModel; var __webpack_exports__WhisperPreTrainedModel = __webpack_exports__.WhisperPreTrainedModel; var __webpack_exports__WhisperProcessor = __webpack_exports__.WhisperProcessor; var __webpack_exports__WhisperTokenizer = __webpack_exports__.WhisperTokenizer; var __webpack_exports__XLMForQuestionAnswering = __webpack_exports__.XLMForQuestionAnswering; var __webpack_exports__XLMForSequenceClassification = __webpack_exports__.XLMForSequenceClassification; var __webpack_exports__XLMForTokenClassification = __webpack_exports__.XLMForTokenClassification; var __webpack_exports__XLMModel = __webpack_exports__.XLMModel; var __webpack_exports__XLMPreTrainedModel = __webpack_exports__.XLMPreTrainedModel; var __webpack_exports__XLMRobertaForMaskedLM = __webpack_exports__.XLMRobertaForMaskedLM; var __webpack_exports__XLMRobertaForQuestionAnswering = __webpack_exports__.XLMRobertaForQuestionAnswering; var __webpack_exports__XLMRobertaForSequenceClassification = __webpack_exports__.XLMRobertaForSequenceClassification; var __webpack_exports__XLMRobertaForTokenClassification = __webpack_exports__.XLMRobertaForTokenClassification; var __webpack_exports__XLMRobertaModel = __webpack_exports__.XLMRobertaModel; var __webpack_exports__XLMRobertaPreTrainedModel = __webpack_exports__.XLMRobertaPreTrainedModel; var __webpack_exports__XLMRobertaTokenizer = __webpack_exports__.XLMRobertaTokenizer; var __webpack_exports__XLMTokenizer = __webpack_exports__.XLMTokenizer; var __webpack_exports__XLMWithLMHeadModel = __webpack_exports__.XLMWithLMHeadModel; var __webpack_exports__XVectorOutput = __webpack_exports__.XVectorOutput; var __webpack_exports__YolosFeatureExtractor = __webpack_exports__.YolosFeatureExtractor; var __webpack_exports__YolosForObjectDetection = __webpack_exports__.YolosForObjectDetection; var __webpack_exports__YolosModel = __webpack_exports__.YolosModel; var __webpack_exports__YolosObjectDetectionOutput = __webpack_exports__.YolosObjectDetectionOutput; var __webpack_exports__YolosPreTrainedModel = __webpack_exports__.YolosPreTrainedModel; var __webpack_exports__ZeroShotAudioClassificationPipeline = __webpack_exports__.ZeroShotAudioClassificationPipeline; var __webpack_exports__ZeroShotClassificationPipeline = __webpack_exports__.ZeroShotClassificationPipeline; var __webpack_exports__ZeroShotImageClassificationPipeline = __webpack_exports__.ZeroShotImageClassificationPipeline; var __webpack_exports__ZeroShotObjectDetectionPipeline = __webpack_exports__.ZeroShotObjectDetectionPipeline; var __webpack_exports__bankers_round = __webpack_exports__.bankers_round; var __webpack_exports__cat = __webpack_exports__.cat; var __webpack_exports__cos_sim = __webpack_exports__.cos_sim; var __webpack_exports__dot = __webpack_exports__.dot; var __webpack_exports__dynamicTimeWarping = __webpack_exports__.dynamicTimeWarping; var __webpack_exports__env = __webpack_exports__.env; var __webpack_exports__full = __webpack_exports__.full; var __webpack_exports__full_like = __webpack_exports__.full_like; var __webpack_exports__getTopItems = __webpack_exports__.getTopItems; var __webpack_exports__hanning = __webpack_exports__.hanning; var __webpack_exports__interpolate = __webpack_exports__.interpolate; var __webpack_exports__interpolate_4d = __webpack_exports__.interpolate_4d; var __webpack_exports__interpolate_data = __webpack_exports__.interpolate_data; var __webpack_exports__layer_norm = __webpack_exports__.layer_norm; var __webpack_exports__log_softmax = __webpack_exports__.log_softmax; var __webpack_exports__magnitude = __webpack_exports__.magnitude; var __webpack_exports__max = __webpack_exports__.max; var __webpack_exports__mean = __webpack_exports__.mean; var __webpack_exports__mean_pooling = __webpack_exports__.mean_pooling; var __webpack_exports__medianFilter = __webpack_exports__.medianFilter; var __webpack_exports__mel_filter_bank = __webpack_exports__.mel_filter_bank; var __webpack_exports__min = __webpack_exports__.min; var __webpack_exports__ones = __webpack_exports__.ones; var __webpack_exports__ones_like = __webpack_exports__.ones_like; var __webpack_exports__permute = __webpack_exports__.permute; var __webpack_exports__permute_data = __webpack_exports__.permute_data; var __webpack_exports__pipeline = __webpack_exports__.pipeline; var __webpack_exports__quantize_embeddings = __webpack_exports__.quantize_embeddings; var __webpack_exports__read_audio = __webpack_exports__.read_audio; var __webpack_exports__round = __webpack_exports__.round; var __webpack_exports__softmax = __webpack_exports__.softmax; var __webpack_exports__spectrogram = __webpack_exports__.spectrogram; var __webpack_exports__stack = __webpack_exports__.stack; var __webpack_exports__std_mean = __webpack_exports__.std_mean; var __webpack_exports__window_function = __webpack_exports__.window_function; var __webpack_exports__zeros = __webpack_exports__.zeros; var __webpack_exports__zeros_like = __webpack_exports__.zeros_like; export { __webpack_exports__ASTFeatureExtractor as ASTFeatureExtractor, __webpack_exports__ASTForAudioClassification as ASTForAudioClassification, __webpack_exports__ASTModel as ASTModel, __webpack_exports__ASTPreTrainedModel as ASTPreTrainedModel, __webpack_exports__AlbertForMaskedLM as AlbertForMaskedLM, __webpack_exports__AlbertForQuestionAnswering as AlbertForQuestionAnswering, __webpack_exports__AlbertForSequenceClassification as AlbertForSequenceClassification, __webpack_exports__AlbertModel as AlbertModel, __webpack_exports__AlbertPreTrainedModel as AlbertPreTrainedModel, __webpack_exports__AlbertTokenizer as AlbertTokenizer, __webpack_exports__AudioClassificationPipeline as AudioClassificationPipeline, __webpack_exports__AutoConfig as AutoConfig, __webpack_exports__AutoModel as AutoModel, __webpack_exports__AutoModelForAudioClassification as AutoModelForAudioClassification, __webpack_exports__AutoModelForAudioFrameClassification as AutoModelForAudioFrameClassification, __webpack_exports__AutoModelForCTC as AutoModelForCTC, __webpack_exports__AutoModelForCausalLM as AutoModelForCausalLM, __webpack_exports__AutoModelForDepthEstimation as AutoModelForDepthEstimation, __webpack_exports__AutoModelForDocumentQuestionAnswering as AutoModelForDocumentQuestionAnswering, __webpack_exports__AutoModelForImageClassification as AutoModelForImageClassification, __webpack_exports__AutoModelForImageFeatureExtraction as AutoModelForImageFeatureExtraction, __webpack_exports__AutoModelForImageMatting as AutoModelForImageMatting, __webpack_exports__AutoModelForImageSegmentation as AutoModelForImageSegmentation, __webpack_exports__AutoModelForImageToImage as AutoModelForImageToImage, __webpack_exports__AutoModelForMaskGeneration as AutoModelForMaskGeneration, __webpack_exports__AutoModelForMaskedLM as AutoModelForMaskedLM, __webpack_exports__AutoModelForObjectDetection as AutoModelForObjectDetection, __webpack_exports__AutoModelForQuestionAnswering as AutoModelForQuestionAnswering, __webpack_exports__AutoModelForSemanticSegmentation as AutoModelForSemanticSegmentation, __webpack_exports__AutoModelForSeq2SeqLM as AutoModelForSeq2SeqLM, __webpack_exports__AutoModelForSequenceClassification as AutoModelForSequenceClassification, __webpack_exports__AutoModelForSpeechSeq2Seq as AutoModelForSpeechSeq2Seq, __webpack_exports__AutoModelForTextToSpectrogram as AutoModelForTextToSpectrogram, __webpack_exports__AutoModelForTextToWaveform as AutoModelForTextToWaveform, __webpack_exports__AutoModelForTokenClassification as AutoModelForTokenClassification, __webpack_exports__AutoModelForVision2Seq as AutoModelForVision2Seq, __webpack_exports__AutoModelForXVector as AutoModelForXVector, __webpack_exports__AutoModelForZeroShotObjectDetection as AutoModelForZeroShotObjectDetection, __webpack_exports__AutoProcessor as AutoProcessor, __webpack_exports__AutoTokenizer as AutoTokenizer, __webpack_exports__AutomaticSpeechRecognitionPipeline as AutomaticSpeechRecognitionPipeline, __webpack_exports__BartForConditionalGeneration as BartForConditionalGeneration, __webpack_exports__BartForSequenceClassification as BartForSequenceClassification, __webpack_exports__BartModel as BartModel, __webpack_exports__BartPretrainedModel as BartPretrainedModel, __webpack_exports__BartTokenizer as BartTokenizer, __webpack_exports__BaseModelOutput as BaseModelOutput, __webpack_exports__BaseStreamer as BaseStreamer, __webpack_exports__BeitFeatureExtractor as BeitFeatureExtractor, __webpack_exports__BeitForImageClassification as BeitForImageClassification, __webpack_exports__BeitModel as BeitModel, __webpack_exports__BeitPreTrainedModel as BeitPreTrainedModel, __webpack_exports__BertForMaskedLM as BertForMaskedLM, __webpack_exports__BertForQuestionAnswering as BertForQuestionAnswering, __webpack_exports__BertForSequenceClassification as BertForSequenceClassification, __webpack_exports__BertForTokenClassification as BertForTokenClassification, __webpack_exports__BertModel as BertModel, __webpack_exports__BertPreTrainedModel as BertPreTrainedModel, __webpack_exports__BertTokenizer as BertTokenizer, __webpack_exports__BitImageProcessor as BitImageProcessor, __webpack_exports__BlenderbotForConditionalGeneration as BlenderbotForConditionalGeneration, __webpack_exports__BlenderbotModel as BlenderbotModel, __webpack_exports__BlenderbotPreTrainedModel as BlenderbotPreTrainedModel, __webpack_exports__BlenderbotSmallForConditionalGeneration as BlenderbotSmallForConditionalGeneration, __webpack_exports__BlenderbotSmallModel as BlenderbotSmallModel, __webpack_exports__BlenderbotSmallPreTrainedModel as BlenderbotSmallPreTrainedModel, __webpack_exports__BlenderbotSmallTokenizer as BlenderbotSmallTokenizer, __webpack_exports__BlenderbotTokenizer as BlenderbotTokenizer, __webpack_exports__BloomForCausalLM as BloomForCausalLM, __webpack_exports__BloomModel as BloomModel, __webpack_exports__BloomPreTrainedModel as BloomPreTrainedModel, __webpack_exports__BloomTokenizer as BloomTokenizer, __webpack_exports__CLIPFeatureExtractor as CLIPFeatureExtractor, __webpack_exports__CLIPImageProcessor as CLIPImageProcessor, __webpack_exports__CLIPModel as CLIPModel, __webpack_exports__CLIPPreTrainedModel as CLIPPreTrainedModel, __webpack_exports__CLIPSegForImageSegmentation as CLIPSegForImageSegmentation, __webpack_exports__CLIPSegModel as CLIPSegModel, __webpack_exports__CLIPSegPreTrainedModel as CLIPSegPreTrainedModel, __webpack_exports__CLIPTextModelWithProjection as CLIPTextModelWithProjection, __webpack_exports__CLIPTokenizer as CLIPTokenizer, __webpack_exports__CLIPVisionModelWithProjection as CLIPVisionModelWithProjection, __webpack_exports__CamembertForMaskedLM as CamembertForMaskedLM, __webpack_exports__CamembertForQuestionAnswering as CamembertForQuestionAnswering, __webpack_exports__CamembertForSequenceClassification as CamembertForSequenceClassification, __webpack_exports__CamembertForTokenClassification as CamembertForTokenClassification, __webpack_exports__CamembertModel as CamembertModel, __webpack_exports__CamembertPreTrainedModel as CamembertPreTrainedModel, __webpack_exports__CamembertTokenizer as CamembertTokenizer, __webpack_exports__CausalLMOutput as CausalLMOutput, __webpack_exports__CausalLMOutputWithPast as CausalLMOutputWithPast, __webpack_exports__ChineseCLIPFeatureExtractor as ChineseCLIPFeatureExtractor, __webpack_exports__ChineseCLIPModel as ChineseCLIPModel, __webpack_exports__ChineseCLIPPreTrainedModel as ChineseCLIPPreTrainedModel, __webpack_exports__ClapAudioModelWithProjection as ClapAudioModelWithProjection, __webpack_exports__ClapFeatureExtractor as ClapFeatureExtractor, __webpack_exports__ClapModel as ClapModel, __webpack_exports__ClapPreTrainedModel as ClapPreTrainedModel, __webpack_exports__ClapTextModelWithProjection as ClapTextModelWithProjection, __webpack_exports__CodeGenForCausalLM as CodeGenForCausalLM, __webpack_exports__CodeGenModel as CodeGenModel, __webpack_exports__CodeGenPreTrainedModel as CodeGenPreTrainedModel, __webpack_exports__CodeGenTokenizer as CodeGenTokenizer, __webpack_exports__CodeLlamaTokenizer as CodeLlamaTokenizer, __webpack_exports__CohereTokenizer as CohereTokenizer, __webpack_exports__ConvBertForMaskedLM as ConvBertForMaskedLM, __webpack_exports__ConvBertForQuestionAnswering as ConvBertForQuestionAnswering, __webpack_exports__ConvBertForSequenceClassification as ConvBertForSequenceClassification, __webpack_exports__ConvBertForTokenClassification as ConvBertForTokenClassification, __webpack_exports__ConvBertModel as ConvBertModel, __webpack_exports__ConvBertPreTrainedModel as ConvBertPreTrainedModel, __webpack_exports__ConvBertTokenizer as ConvBertTokenizer, __webpack_exports__ConvNextFeatureExtractor as ConvNextFeatureExtractor, __webpack_exports__ConvNextForImageClassification as ConvNextForImageClassification, __webpack_exports__ConvNextImageProcessor as ConvNextImageProcessor, __webpack_exports__ConvNextModel as ConvNextModel, __webpack_exports__ConvNextPreTrainedModel as ConvNextPreTrainedModel, __webpack_exports__ConvNextV2ForImageClassification as ConvNextV2ForImageClassification, __webpack_exports__ConvNextV2Model as ConvNextV2Model, __webpack_exports__ConvNextV2PreTrainedModel as ConvNextV2PreTrainedModel, __webpack_exports__DPTFeatureExtractor as DPTFeatureExtractor, __webpack_exports__DPTForDepthEstimation as DPTForDepthEstimation, __webpack_exports__DPTImageProcessor as DPTImageProcessor, __webpack_exports__DPTModel as DPTModel, __webpack_exports__DPTPreTrainedModel as DPTPreTrainedModel, __webpack_exports__DebertaForMaskedLM as DebertaForMaskedLM, __webpack_exports__DebertaForQuestionAnswering as DebertaForQuestionAnswering, __webpack_exports__DebertaForSequenceClassification as DebertaForSequenceClassification, __webpack_exports__DebertaForTokenClassification as DebertaForTokenClassification, __webpack_exports__DebertaModel as DebertaModel, __webpack_exports__DebertaPreTrainedModel as DebertaPreTrainedModel, __webpack_exports__DebertaTokenizer as DebertaTokenizer, __webpack_exports__DebertaV2ForMaskedLM as DebertaV2ForMaskedLM, __webpack_exports__DebertaV2ForQuestionAnswering as DebertaV2ForQuestionAnswering, __webpack_exports__DebertaV2ForSequenceClassification as DebertaV2ForSequenceClassification, __webpack_exports__DebertaV2ForTokenClassification as DebertaV2ForTokenClassification, __webpack_exports__DebertaV2Model as DebertaV2Model, __webpack_exports__DebertaV2PreTrainedModel as DebertaV2PreTrainedModel, __webpack_exports__DebertaV2Tokenizer as DebertaV2Tokenizer, __webpack_exports__DeiTFeatureExtractor as DeiTFeatureExtractor, __webpack_exports__DeiTForImageClassification as DeiTForImageClassification, __webpack_exports__DeiTModel as DeiTModel, __webpack_exports__DeiTPreTrainedModel as DeiTPreTrainedModel, __webpack_exports__DepthAnythingForDepthEstimation as DepthAnythingForDepthEstimation, __webpack_exports__DepthAnythingPreTrainedModel as DepthAnythingPreTrainedModel, __webpack_exports__DepthEstimationPipeline as DepthEstimationPipeline, __webpack_exports__DetrFeatureExtractor as DetrFeatureExtractor, __webpack_exports__DetrForObjectDetection as DetrForObjectDetection, __webpack_exports__DetrForSegmentation as DetrForSegmentation, __webpack_exports__DetrModel as DetrModel, __webpack_exports__DetrObjectDetectionOutput as DetrObjectDetectionOutput, __webpack_exports__DetrPreTrainedModel as DetrPreTrainedModel, __webpack_exports__DetrSegmentationOutput as DetrSegmentationOutput, __webpack_exports__Dinov2ForImageClassification as Dinov2ForImageClassification, __webpack_exports__Dinov2Model as Dinov2Model, __webpack_exports__Dinov2PreTrainedModel as Dinov2PreTrainedModel, __webpack_exports__DistilBertForMaskedLM as DistilBertForMaskedLM, __webpack_exports__DistilBertForQuestionAnswering as DistilBertForQuestionAnswering, __webpack_exports__DistilBertForSequenceClassification as DistilBertForSequenceClassification, __webpack_exports__DistilBertForTokenClassification as DistilBertForTokenClassification, __webpack_exports__DistilBertModel as DistilBertModel, __webpack_exports__DistilBertPreTrainedModel as DistilBertPreTrainedModel, __webpack_exports__DistilBertTokenizer as DistilBertTokenizer, __webpack_exports__DocumentQuestionAnsweringPipeline as DocumentQuestionAnsweringPipeline, __webpack_exports__DonutFeatureExtractor as DonutFeatureExtractor, __webpack_exports__DonutSwinModel as DonutSwinModel, __webpack_exports__DonutSwinPreTrainedModel as DonutSwinPreTrainedModel, __webpack_exports__EfficientNetForImageClassification as EfficientNetForImageClassification, __webpack_exports__EfficientNetImageProcessor as EfficientNetImageProcessor, __webpack_exports__EfficientNetModel as EfficientNetModel, __webpack_exports__EfficientNetPreTrainedModel as EfficientNetPreTrainedModel, __webpack_exports__ElectraForMaskedLM as ElectraForMaskedLM, __webpack_exports__ElectraForQuestionAnswering as ElectraForQuestionAnswering, __webpack_exports__ElectraForSequenceClassification as ElectraForSequenceClassification, __webpack_exports__ElectraForTokenClassification as ElectraForTokenClassification, __webpack_exports__ElectraModel as ElectraModel, __webpack_exports__ElectraPreTrainedModel as ElectraPreTrainedModel, __webpack_exports__ElectraTokenizer as ElectraTokenizer, __webpack_exports__EsmForMaskedLM as EsmForMaskedLM, __webpack_exports__EsmForSequenceClassification as EsmForSequenceClassification, __webpack_exports__EsmForTokenClassification as EsmForTokenClassification, __webpack_exports__EsmModel as EsmModel, __webpack_exports__EsmPreTrainedModel as EsmPreTrainedModel, __webpack_exports__EsmTokenizer as EsmTokenizer, __webpack_exports__FFT as FFT, __webpack_exports__FalconForCausalLM as FalconForCausalLM, __webpack_exports__FalconModel as FalconModel, __webpack_exports__FalconPreTrainedModel as FalconPreTrainedModel, __webpack_exports__FalconTokenizer as FalconTokenizer, __webpack_exports__FeatureExtractionPipeline as FeatureExtractionPipeline, __webpack_exports__FeatureExtractor as FeatureExtractor, __webpack_exports__FillMaskPipeline as FillMaskPipeline, __webpack_exports__GLPNFeatureExtractor as GLPNFeatureExtractor, __webpack_exports__GLPNForDepthEstimation as GLPNForDepthEstimation, __webpack_exports__GLPNModel as GLPNModel, __webpack_exports__GLPNPreTrainedModel as GLPNPreTrainedModel, __webpack_exports__GPT2LMHeadModel as GPT2LMHeadModel, __webpack_exports__GPT2Model as GPT2Model, __webpack_exports__GPT2PreTrainedModel as GPT2PreTrainedModel, __webpack_exports__GPT2Tokenizer as GPT2Tokenizer, __webpack_exports__GPTBigCodeForCausalLM as GPTBigCodeForCausalLM, __webpack_exports__GPTBigCodeModel as GPTBigCodeModel, __webpack_exports__GPTBigCodePreTrainedModel as GPTBigCodePreTrainedModel, __webpack_exports__GPTJForCausalLM as GPTJForCausalLM, __webpack_exports__GPTJModel as GPTJModel, __webpack_exports__GPTJPreTrainedModel as GPTJPreTrainedModel, __webpack_exports__GPTNeoForCausalLM as GPTNeoForCausalLM, __webpack_exports__GPTNeoModel as GPTNeoModel, __webpack_exports__GPTNeoPreTrainedModel as GPTNeoPreTrainedModel, __webpack_exports__GPTNeoXForCausalLM as GPTNeoXForCausalLM, __webpack_exports__GPTNeoXModel as GPTNeoXModel, __webpack_exports__GPTNeoXPreTrainedModel as GPTNeoXPreTrainedModel, __webpack_exports__GPTNeoXTokenizer as GPTNeoXTokenizer, __webpack_exports__GemmaTokenizer as GemmaTokenizer, __webpack_exports__Grok1Tokenizer as Grok1Tokenizer, __webpack_exports__HerbertTokenizer as HerbertTokenizer, __webpack_exports__HubertForCTC as HubertForCTC, __webpack_exports__HubertForSequenceClassification as HubertForSequenceClassification, __webpack_exports__HubertModel as HubertModel, __webpack_exports__HubertPreTrainedModel as HubertPreTrainedModel, __webpack_exports__ImageClassificationPipeline as ImageClassificationPipeline, __webpack_exports__ImageFeatureExtractionPipeline as ImageFeatureExtractionPipeline, __webpack_exports__ImageFeatureExtractor as ImageFeatureExtractor, __webpack_exports__ImageMattingOutput as ImageMattingOutput, __webpack_exports__ImageSegmentationPipeline as ImageSegmentationPipeline, __webpack_exports__ImageToImagePipeline as ImageToImagePipeline, __webpack_exports__ImageToTextPipeline as ImageToTextPipeline, __webpack_exports__LlamaForCausalLM as LlamaForCausalLM, __webpack_exports__LlamaModel as LlamaModel, __webpack_exports__LlamaPreTrainedModel as LlamaPreTrainedModel, __webpack_exports__LlamaTokenizer as LlamaTokenizer, __webpack_exports__LlavaForConditionalGeneration as LlavaForConditionalGeneration, __webpack_exports__LlavaPreTrainedModel as LlavaPreTrainedModel, __webpack_exports__LongT5ForConditionalGeneration as LongT5ForConditionalGeneration, __webpack_exports__LongT5Model as LongT5Model, __webpack_exports__LongT5PreTrainedModel as LongT5PreTrainedModel, __webpack_exports__M2M100ForConditionalGeneration as M2M100ForConditionalGeneration, __webpack_exports__M2M100Model as M2M100Model, __webpack_exports__M2M100PreTrainedModel as M2M100PreTrainedModel, __webpack_exports__M2M100Tokenizer as M2M100Tokenizer, __webpack_exports__MBart50Tokenizer as MBart50Tokenizer, __webpack_exports__MBartForCausalLM as MBartForCausalLM, __webpack_exports__MBartForConditionalGeneration as MBartForConditionalGeneration, __webpack_exports__MBartForSequenceClassification as MBartForSequenceClassification, __webpack_exports__MBartModel as MBartModel, __webpack_exports__MBartPreTrainedModel as MBartPreTrainedModel, __webpack_exports__MBartTokenizer as MBartTokenizer, __webpack_exports__MPNetForMaskedLM as MPNetForMaskedLM, __webpack_exports__MPNetForQuestionAnswering as MPNetForQuestionAnswering, __webpack_exports__MPNetForSequenceClassification as MPNetForSequenceClassification, __webpack_exports__MPNetForTokenClassification as MPNetForTokenClassification, __webpack_exports__MPNetModel as MPNetModel, __webpack_exports__MPNetPreTrainedModel as MPNetPreTrainedModel, __webpack_exports__MPNetTokenizer as MPNetTokenizer, __webpack_exports__MT5ForConditionalGeneration as MT5ForConditionalGeneration, __webpack_exports__MT5Model as MT5Model, __webpack_exports__MT5PreTrainedModel as MT5PreTrainedModel, __webpack_exports__MarianMTModel as MarianMTModel, __webpack_exports__MarianModel as MarianModel, __webpack_exports__MarianPreTrainedModel as MarianPreTrainedModel, __webpack_exports__MarianTokenizer as MarianTokenizer, __webpack_exports__MaskedLMOutput as MaskedLMOutput, __webpack_exports__MistralForCausalLM as MistralForCausalLM, __webpack_exports__MistralModel as MistralModel, __webpack_exports__MistralPreTrainedModel as MistralPreTrainedModel, __webpack_exports__MobileBertForMaskedLM as MobileBertForMaskedLM, __webpack_exports__MobileBertForQuestionAnswering as MobileBertForQuestionAnswering, __webpack_exports__MobileBertForSequenceClassification as MobileBertForSequenceClassification, __webpack_exports__MobileBertModel as MobileBertModel, __webpack_exports__MobileBertPreTrainedModel as MobileBertPreTrainedModel, __webpack_exports__MobileBertTokenizer as MobileBertTokenizer, __webpack_exports__MobileViTFeatureExtractor as MobileViTFeatureExtractor, __webpack_exports__MobileViTForImageClassification as MobileViTForImageClassification, __webpack_exports__MobileViTModel as MobileViTModel, __webpack_exports__MobileViTPreTrainedModel as MobileViTPreTrainedModel, __webpack_exports__ModelOutput as ModelOutput, __webpack_exports__MptForCausalLM as MptForCausalLM, __webpack_exports__MptModel as MptModel, __webpack_exports__MptPreTrainedModel as MptPreTrainedModel, __webpack_exports__MusicgenForCausalLM as MusicgenForCausalLM, __webpack_exports__MusicgenForConditionalGeneration as MusicgenForConditionalGeneration, __webpack_exports__MusicgenModel as MusicgenModel, __webpack_exports__MusicgenPreTrainedModel as MusicgenPreTrainedModel, __webpack_exports__NllbTokenizer as NllbTokenizer, __webpack_exports__NomicBertModel as NomicBertModel, __webpack_exports__NomicBertPreTrainedModel as NomicBertPreTrainedModel, __webpack_exports__NougatImageProcessor as NougatImageProcessor, __webpack_exports__NougatTokenizer as NougatTokenizer, __webpack_exports__OPTForCausalLM as OPTForCausalLM, __webpack_exports__OPTModel as OPTModel, __webpack_exports__OPTPreTrainedModel as OPTPreTrainedModel, __webpack_exports__ObjectDetectionPipeline as ObjectDetectionPipeline, __webpack_exports__OwlViTFeatureExtractor as OwlViTFeatureExtractor, __webpack_exports__OwlViTForObjectDetection as OwlViTForObjectDetection, __webpack_exports__OwlViTModel as OwlViTModel, __webpack_exports__OwlViTPreTrainedModel as OwlViTPreTrainedModel, __webpack_exports__OwlViTProcessor as OwlViTProcessor, __webpack_exports__Owlv2ForObjectDetection as Owlv2ForObjectDetection, __webpack_exports__Owlv2ImageProcessor as Owlv2ImageProcessor, __webpack_exports__Owlv2Model as Owlv2Model, __webpack_exports__Owlv2PreTrainedModel as Owlv2PreTrainedModel, __webpack_exports__PhiForCausalLM as PhiForCausalLM, __webpack_exports__PhiModel as PhiModel, __webpack_exports__PhiPreTrainedModel as PhiPreTrainedModel, __webpack_exports__Pipeline as Pipeline, __webpack_exports__PreTrainedModel as PreTrainedModel, __webpack_exports__PreTrainedTokenizer as PreTrainedTokenizer, __webpack_exports__PretrainedConfig as PretrainedConfig, __webpack_exports__PretrainedMixin as PretrainedMixin, __webpack_exports__Processor as Processor, __webpack_exports__QuestionAnsweringModelOutput as QuestionAnsweringModelOutput, __webpack_exports__QuestionAnsweringPipeline as QuestionAnsweringPipeline, __webpack_exports__Qwen2ForCausalLM as Qwen2ForCausalLM, __webpack_exports__Qwen2Model as Qwen2Model, __webpack_exports__Qwen2PreTrainedModel as Qwen2PreTrainedModel, __webpack_exports__Qwen2Tokenizer as Qwen2Tokenizer, __webpack_exports__RawImage as RawImage, __webpack_exports__ResNetForImageClassification as ResNetForImageClassification, __webpack_exports__ResNetModel as ResNetModel, __webpack_exports__ResNetPreTrainedModel as ResNetPreTrainedModel, __webpack_exports__RoFormerForMaskedLM as RoFormerForMaskedLM, __webpack_exports__RoFormerForQuestionAnswering as RoFormerForQuestionAnswering, __webpack_exports__RoFormerForSequenceClassification as RoFormerForSequenceClassification, __webpack_exports__RoFormerForTokenClassification as RoFormerForTokenClassification, __webpack_exports__RoFormerModel as RoFormerModel, __webpack_exports__RoFormerPreTrainedModel as RoFormerPreTrainedModel, __webpack_exports__RoFormerTokenizer as RoFormerTokenizer, __webpack_exports__RobertaForMaskedLM as RobertaForMaskedLM, __webpack_exports__RobertaForQuestionAnswering as RobertaForQuestionAnswering, __webpack_exports__RobertaForSequenceClassification as RobertaForSequenceClassification, __webpack_exports__RobertaForTokenClassification as RobertaForTokenClassification, __webpack_exports__RobertaModel as RobertaModel, __webpack_exports__RobertaPreTrainedModel as RobertaPreTrainedModel, __webpack_exports__RobertaTokenizer as RobertaTokenizer, __webpack_exports__SamImageProcessor as SamImageProcessor, __webpack_exports__SamImageSegmentationOutput as SamImageSegmentationOutput, __webpack_exports__SamModel as SamModel, __webpack_exports__SamPreTrainedModel as SamPreTrainedModel, __webpack_exports__SamProcessor as SamProcessor, __webpack_exports__SeamlessM4TFeatureExtractor as SeamlessM4TFeatureExtractor, __webpack_exports__SegformerFeatureExtractor as SegformerFeatureExtractor, __webpack_exports__SegformerForImageClassification as SegformerForImageClassification, __webpack_exports__SegformerForSemanticSegmentation as SegformerForSemanticSegmentation, __webpack_exports__SegformerModel as SegformerModel, __webpack_exports__SegformerPreTrainedModel as SegformerPreTrainedModel, __webpack_exports__Seq2SeqLMOutput as Seq2SeqLMOutput, __webpack_exports__SequenceClassifierOutput as SequenceClassifierOutput, __webpack_exports__SiglipImageProcessor as SiglipImageProcessor, __webpack_exports__SiglipModel as SiglipModel, __webpack_exports__SiglipPreTrainedModel as SiglipPreTrainedModel, __webpack_exports__SiglipTextModel as SiglipTextModel, __webpack_exports__SiglipTokenizer as SiglipTokenizer, __webpack_exports__SiglipVisionModel as SiglipVisionModel, __webpack_exports__SpeechT5FeatureExtractor as SpeechT5FeatureExtractor, __webpack_exports__SpeechT5ForSpeechToText as SpeechT5ForSpeechToText, __webpack_exports__SpeechT5ForTextToSpeech as SpeechT5ForTextToSpeech, __webpack_exports__SpeechT5HifiGan as SpeechT5HifiGan, __webpack_exports__SpeechT5Model as SpeechT5Model, __webpack_exports__SpeechT5PreTrainedModel as SpeechT5PreTrainedModel, __webpack_exports__SpeechT5Processor as SpeechT5Processor, __webpack_exports__SpeechT5Tokenizer as SpeechT5Tokenizer, __webpack_exports__SqueezeBertForMaskedLM as SqueezeBertForMaskedLM, __webpack_exports__SqueezeBertForQuestionAnswering as SqueezeBertForQuestionAnswering, __webpack_exports__SqueezeBertForSequenceClassification as SqueezeBertForSequenceClassification, __webpack_exports__SqueezeBertModel as SqueezeBertModel, __webpack_exports__SqueezeBertPreTrainedModel as SqueezeBertPreTrainedModel, __webpack_exports__SqueezeBertTokenizer as SqueezeBertTokenizer, __webpack_exports__StableLmForCausalLM as StableLmForCausalLM, __webpack_exports__StableLmModel as StableLmModel, __webpack_exports__StableLmPreTrainedModel as StableLmPreTrainedModel, __webpack_exports__Starcoder2ForCausalLM as Starcoder2ForCausalLM, __webpack_exports__Starcoder2Model as Starcoder2Model, __webpack_exports__Starcoder2PreTrainedModel as Starcoder2PreTrainedModel, __webpack_exports__SummarizationPipeline as SummarizationPipeline, __webpack_exports__Swin2SRForImageSuperResolution as Swin2SRForImageSuperResolution, __webpack_exports__Swin2SRImageProcessor as Swin2SRImageProcessor, __webpack_exports__Swin2SRModel as Swin2SRModel, __webpack_exports__Swin2SRPreTrainedModel as Swin2SRPreTrainedModel, __webpack_exports__SwinForImageClassification as SwinForImageClassification, __webpack_exports__SwinModel as SwinModel, __webpack_exports__SwinPreTrainedModel as SwinPreTrainedModel, __webpack_exports__T5ForConditionalGeneration as T5ForConditionalGeneration, __webpack_exports__T5Model as T5Model, __webpack_exports__T5PreTrainedModel as T5PreTrainedModel, __webpack_exports__T5Tokenizer as T5Tokenizer, __webpack_exports__TableTransformerForObjectDetection as TableTransformerForObjectDetection, __webpack_exports__TableTransformerModel as TableTransformerModel, __webpack_exports__TableTransformerObjectDetectionOutput as TableTransformerObjectDetectionOutput, __webpack_exports__TableTransformerPreTrainedModel as TableTransformerPreTrainedModel, __webpack_exports__Tensor as Tensor, __webpack_exports__Text2TextGenerationPipeline as Text2TextGenerationPipeline, __webpack_exports__TextClassificationPipeline as TextClassificationPipeline, __webpack_exports__TextGenerationPipeline as TextGenerationPipeline, __webpack_exports__TextToAudioPipeline as TextToAudioPipeline, __webpack_exports__TokenClassificationPipeline as TokenClassificationPipeline, __webpack_exports__TokenClassifierOutput as TokenClassifierOutput, __webpack_exports__TokenizerModel as TokenizerModel, __webpack_exports__TrOCRForCausalLM as TrOCRForCausalLM, __webpack_exports__TrOCRPreTrainedModel as TrOCRPreTrainedModel, __webpack_exports__TranslationPipeline as TranslationPipeline, __webpack_exports__UniSpeechForCTC as UniSpeechForCTC, __webpack_exports__UniSpeechForSequenceClassification as UniSpeechForSequenceClassification, __webpack_exports__UniSpeechModel as UniSpeechModel, __webpack_exports__UniSpeechPreTrainedModel as UniSpeechPreTrainedModel, __webpack_exports__UniSpeechSatForAudioFrameClassification as UniSpeechSatForAudioFrameClassification, __webpack_exports__UniSpeechSatForCTC as UniSpeechSatForCTC, __webpack_exports__UniSpeechSatForSequenceClassification as UniSpeechSatForSequenceClassification, __webpack_exports__UniSpeechSatModel as UniSpeechSatModel, __webpack_exports__UniSpeechSatPreTrainedModel as UniSpeechSatPreTrainedModel, __webpack_exports__ViTFeatureExtractor as ViTFeatureExtractor, __webpack_exports__ViTForImageClassification as ViTForImageClassification, __webpack_exports__ViTImageProcessor as ViTImageProcessor, __webpack_exports__ViTModel as ViTModel, __webpack_exports__ViTPreTrainedModel as ViTPreTrainedModel, __webpack_exports__VisionEncoderDecoderModel as VisionEncoderDecoderModel, __webpack_exports__VitMatteForImageMatting as VitMatteForImageMatting, __webpack_exports__VitMatteImageProcessor as VitMatteImageProcessor, __webpack_exports__VitMattePreTrainedModel as VitMattePreTrainedModel, __webpack_exports__VitsModel as VitsModel, __webpack_exports__VitsModelOutput as VitsModelOutput, __webpack_exports__VitsPreTrainedModel as VitsPreTrainedModel, __webpack_exports__VitsTokenizer as VitsTokenizer, __webpack_exports__Wav2Vec2BertForCTC as Wav2Vec2BertForCTC, __webpack_exports__Wav2Vec2BertForSequenceClassification as Wav2Vec2BertForSequenceClassification, __webpack_exports__Wav2Vec2BertModel as Wav2Vec2BertModel, __webpack_exports__Wav2Vec2BertPreTrainedModel as Wav2Vec2BertPreTrainedModel, __webpack_exports__Wav2Vec2CTCTokenizer as Wav2Vec2CTCTokenizer, __webpack_exports__Wav2Vec2FeatureExtractor as Wav2Vec2FeatureExtractor, __webpack_exports__Wav2Vec2ForAudioFrameClassification as Wav2Vec2ForAudioFrameClassification, __webpack_exports__Wav2Vec2ForCTC as Wav2Vec2ForCTC, __webpack_exports__Wav2Vec2ForSequenceClassification as Wav2Vec2ForSequenceClassification, __webpack_exports__Wav2Vec2Model as Wav2Vec2Model, __webpack_exports__Wav2Vec2PreTrainedModel as Wav2Vec2PreTrainedModel, __webpack_exports__Wav2Vec2ProcessorWithLM as Wav2Vec2ProcessorWithLM, __webpack_exports__WavLMForAudioFrameClassification as WavLMForAudioFrameClassification, __webpack_exports__WavLMForCTC as WavLMForCTC, __webpack_exports__WavLMForSequenceClassification as WavLMForSequenceClassification, __webpack_exports__WavLMForXVector as WavLMForXVector, __webpack_exports__WavLMModel as WavLMModel, __webpack_exports__WavLMPreTrainedModel as WavLMPreTrainedModel, __webpack_exports__WhisperFeatureExtractor as WhisperFeatureExtractor, __webpack_exports__WhisperForConditionalGeneration as WhisperForConditionalGeneration, __webpack_exports__WhisperModel as WhisperModel, __webpack_exports__WhisperPreTrainedModel as WhisperPreTrainedModel, __webpack_exports__WhisperProcessor as WhisperProcessor, __webpack_exports__WhisperTokenizer as WhisperTokenizer, __webpack_exports__XLMForQuestionAnswering as XLMForQuestionAnswering, __webpack_exports__XLMForSequenceClassification as XLMForSequenceClassification, __webpack_exports__XLMForTokenClassification as XLMForTokenClassification, __webpack_exports__XLMModel as XLMModel, __webpack_exports__XLMPreTrainedModel as XLMPreTrainedModel, __webpack_exports__XLMRobertaForMaskedLM as XLMRobertaForMaskedLM, __webpack_exports__XLMRobertaForQuestionAnswering as XLMRobertaForQuestionAnswering, __webpack_exports__XLMRobertaForSequenceClassification as XLMRobertaForSequenceClassification, __webpack_exports__XLMRobertaForTokenClassification as XLMRobertaForTokenClassification, __webpack_exports__XLMRobertaModel as XLMRobertaModel, __webpack_exports__XLMRobertaPreTrainedModel as XLMRobertaPreTrainedModel, __webpack_exports__XLMRobertaTokenizer as XLMRobertaTokenizer, __webpack_exports__XLMTokenizer as XLMTokenizer, __webpack_exports__XLMWithLMHeadModel as XLMWithLMHeadModel, __webpack_exports__XVectorOutput as XVectorOutput, __webpack_exports__YolosFeatureExtractor as YolosFeatureExtractor, __webpack_exports__YolosForObjectDetection as YolosForObjectDetection, __webpack_exports__YolosModel as YolosModel, __webpack_exports__YolosObjectDetectionOutput as YolosObjectDetectionOutput, __webpack_exports__YolosPreTrainedModel as YolosPreTrainedModel, __webpack_exports__ZeroShotAudioClassificationPipeline as ZeroShotAudioClassificationPipeline, __webpack_exports__ZeroShotClassificationPipeline as ZeroShotClassificationPipeline, __webpack_exports__ZeroShotImageClassificationPipeline as ZeroShotImageClassificationPipeline, __webpack_exports__ZeroShotObjectDetectionPipeline as ZeroShotObjectDetectionPipeline, __webpack_exports__bankers_round as bankers_round, __webpack_exports__cat as cat, __webpack_exports__cos_sim as cos_sim, __webpack_exports__dot as dot, __webpack_exports__dynamicTimeWarping as dynamicTimeWarping, __webpack_exports__env as env, __webpack_exports__full as full, __webpack_exports__full_like as full_like, __webpack_exports__getTopItems as getTopItems, __webpack_exports__hanning as hanning, __webpack_exports__interpolate as interpolate, __webpack_exports__interpolate_4d as interpolate_4d, __webpack_exports__interpolate_data as interpolate_data, __webpack_exports__layer_norm as layer_norm, __webpack_exports__log_softmax as log_softmax, __webpack_exports__magnitude as magnitude, __webpack_exports__max as max, __webpack_exports__mean as mean, __webpack_exports__mean_pooling as mean_pooling, __webpack_exports__medianFilter as medianFilter, __webpack_exports__mel_filter_bank as mel_filter_bank, __webpack_exports__min as min, __webpack_exports__ones as ones, __webpack_exports__ones_like as ones_like, __webpack_exports__permute as permute, __webpack_exports__permute_data as permute_data, __webpack_exports__pipeline as pipeline, __webpack_exports__quantize_embeddings as quantize_embeddings, __webpack_exports__read_audio as read_audio, __webpack_exports__round as round, __webpack_exports__softmax as softmax, __webpack_exports__spectrogram as spectrogram, __webpack_exports__stack as stack, __webpack_exports__std_mean as std_mean, __webpack_exports__window_function as window_function, __webpack_exports__zeros as zeros, __webpack_exports__zeros_like as zeros_like }; //# sourceMappingURL=transformers.js.map