-
Notifications
You must be signed in to change notification settings - Fork 5
feat: sync wrapper for ONNX embeddings function #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vkozyura
wants to merge
20
commits into
main
Choose a base branch
from
sqlite-embeddings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0996b2f
Sync wrapper for Sqlite for using ONNX embeddings function
vkozyura 6675fdf
fix imple and add tests
vkozyura ac48114
add semantic tests
vkozyura fe729e7
use LOG
vkozyura 750b2d4
test 4 params
vkozyura 36b7fd8
small fixes
vkozyura eeaeb31
fix: address PR bot comments - add division by zero guard and fix linβ¦
vkozyura b07f5d9
chore: run prettier formatting
vkozyura 0f0d584
fix tests
vkozyura fb7684d
dix duplicated function registration
vkozyura 3994931
more frixes
vkozyura 264b37a
export vector_embedding directly
vkozyura ad1269a
rem unused
vkozyura 02f3857
refactor
vkozyura f353e6d
refactor
vkozyura 086f64f
linter
vkozyura 41e9e58
remove comment
vkozyura f12bd88
Update CHANGELOG.md
vkozyura 38d8109
Update README.md
vkozyura bcf9016
export embeddings
vkozyura File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| // Copy from onnxruntime-common/dist/cjs/inference-session-impl.js and referenced files by it | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| // Adjusted to meet the needs of SQLite by making the run functions synchronous to avoid WorkerThreads | ||
| import { createRequire } from 'module'; | ||
| const require = createRequire(import.meta.url); | ||
| const ort = require('onnxruntime-common'); | ||
| const binding = require('onnxruntime-node/dist/binding.js'); | ||
|
|
||
| class InferenceSession { | ||
| constructor(handler) { | ||
| this.handler = handler; | ||
| } | ||
|
|
||
| run(feeds) { | ||
| const fetches = {}; | ||
| let options = {}; | ||
| // check inputs | ||
| if ( | ||
| typeof feeds !== 'object' || | ||
| feeds === null || | ||
| feeds instanceof ort.Tensor || | ||
| Array.isArray(feeds) | ||
| ) { | ||
| throw new TypeError( | ||
| "'feeds' must be an object that use input names as keys and OnnxValue as corresponding values." | ||
| ); | ||
| } | ||
| // check if all inputs are in feed | ||
| for (const name of this.handler.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 | ||
| for (const name of this.handler.outputNames) { | ||
| fetches[name] = null; | ||
| } | ||
| // feeds, fetches and options are prepared | ||
| const results = 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 ort.Tensor) returnValue[key] = result; | ||
| else returnValue[key] = new ort.Tensor(result.type, result.data, result.dims); | ||
| } | ||
| } | ||
| return returnValue; | ||
| } | ||
|
|
||
| static async create(arg0) { | ||
| let filePathOrUint8Array; | ||
| if (arg0 instanceof Uint8Array) filePathOrUint8Array = arg0; | ||
| else | ||
| throw Error( | ||
| 'Argument is not supported. Check original InferenceSession implementation if this adjustment needs to be adopted' | ||
| ); | ||
|
|
||
| // resolve backend, update session options with validated EPs, and create session handler | ||
| const [backend, optionsWithValidatedEPs] = await resolveBackendAndExecutionProviders(); | ||
| const handler = await backend.createInferenceSessionHandler( | ||
| filePathOrUint8Array, | ||
| optionsWithValidatedEPs | ||
| ); | ||
| return new InferenceSession(handler); | ||
| } | ||
| } | ||
|
|
||
| // Copy from onnxruntime-common/dist/cjs/backend-impl.js | ||
| async function resolveBackendAndExecutionProviders() { | ||
| const backends = new Map(); | ||
| const backendsList = listSupportedBackends(); | ||
| for (const backend of backendsList) { | ||
| backends.set(backend.name, { backend: onnxruntimeBackend }); | ||
| } | ||
| const backendNames = [...backends.keys()]; | ||
| // try to resolve and initialize all requested backends | ||
| let backend; | ||
| const errors = []; | ||
| const availableBackendNames = new Set(); | ||
| for (const backendName of backendNames) { | ||
| // eslint-disable-next-line no-await-in-loop | ||
| const resolveResult = await tryResolveAndInitializeBackend(backendName, backends); | ||
| if (typeof resolveResult === 'string') { | ||
| errors.push({ name: backendName, err: resolveResult }); | ||
| } else { | ||
| if (!backend) { | ||
| backend = resolveResult; | ||
| } | ||
| if (backend === resolveResult) { | ||
| availableBackendNames.add(backendName); | ||
| } | ||
| } | ||
| } | ||
| // if no backend is available, throw error. | ||
| if (!backend) { | ||
| throw new Error( | ||
| `no available backend found. ERR: ${errors.map((e) => `[${e.name}] ${e.err}`).join(', ')}` | ||
| ); | ||
| } | ||
| return [ | ||
| backend, | ||
| new Proxy( | ||
| {}, | ||
| { | ||
| get: (target, prop) => { | ||
| if (prop === 'executionProviders') { | ||
| return []; | ||
| } | ||
| return Reflect.get(target, prop); | ||
| } | ||
| } | ||
| ) | ||
| ]; | ||
| } | ||
|
|
||
| async function tryResolveAndInitializeBackend(backendName, backends) { | ||
| const backendInfo = backends.get(backendName); | ||
| if (!backendInfo) { | ||
| return 'backend not found.'; | ||
| } | ||
| if (backendInfo.initialized) { | ||
| return backendInfo.backend; | ||
| } else if (backendInfo.aborted) { | ||
| return backendInfo.error; | ||
| } else { | ||
| 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) { | ||
| backendInfo.error = `${e}`; | ||
| backendInfo.aborted = true; | ||
| } | ||
| return backendInfo.error; | ||
| } finally { | ||
| delete backendInfo.initPromise; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Copy from test/bookshop/node_modules/onnxruntime-node/dist/backend.js | ||
| const dataTypeStrings = [ | ||
| undefined, | ||
| 'float32', | ||
| 'uint8', | ||
| 'int8', | ||
| 'uint16', | ||
| 'int16', | ||
| 'int32', | ||
| 'int64', | ||
| 'string', | ||
| 'bool', | ||
| 'float16', | ||
| 'float64', | ||
| 'uint32', | ||
| 'uint64', | ||
| undefined, | ||
| undefined, | ||
| undefined, | ||
| undefined, | ||
| undefined, | ||
| undefined, | ||
| undefined, | ||
| 'uint4', | ||
| 'int4' | ||
| ]; | ||
| class OnnxruntimeSessionHandler { | ||
| static inferenceSession = new WeakMap(); | ||
| constructor(pathOrBuffer, options) { | ||
| binding.initOrt(); | ||
| OnnxruntimeSessionHandler.inferenceSession.set(this, new binding.binding.InferenceSession()); | ||
| if (typeof pathOrBuffer === 'string') { | ||
| OnnxruntimeSessionHandler.inferenceSession.get(this).loadModel(pathOrBuffer, options); | ||
| } else { | ||
| OnnxruntimeSessionHandler.inferenceSession | ||
| .get(this) | ||
| .loadModel(pathOrBuffer.buffer, pathOrBuffer.byteOffset, pathOrBuffer.byteLength, options); | ||
| } | ||
| // prepare input/output names and metadata | ||
| this.inputNames = []; | ||
| this.outputNames = []; | ||
| this.inputMetadata = []; | ||
| this.outputMetadata = []; | ||
| // this function takes raw metadata from binding and returns a tuple of the following 2 items: | ||
| // - an array of string representing names | ||
| // - an array of converted InferenceSession.ValueMetadata | ||
| const fillNamesAndMetadata = (rawMetadata) => { | ||
| const names = []; | ||
| const metadata = []; | ||
| for (const m of rawMetadata) { | ||
| names.push(m.name); | ||
| if (!m.isTensor) { | ||
| metadata.push({ name: m.name, isTensor: false }); | ||
| } else { | ||
| const type = dataTypeStrings[m.type]; | ||
| if (type === undefined) { | ||
| throw new Error(`Unsupported data type: ${m.type}`); | ||
| } | ||
| const shape = []; | ||
| for (let i = 0; i < m.shape.length; ++i) { | ||
| const dim = m.shape[i]; | ||
| if (dim === -1) { | ||
| shape.push(m.symbolicDimensions[i]); | ||
| } else if (dim >= 0) { | ||
| shape.push(dim); | ||
| } else { | ||
| throw new Error(`Invalid dimension: ${dim}`); | ||
| } | ||
| } | ||
| metadata.push({ | ||
| name: m.name, | ||
| isTensor: m.isTensor, | ||
| type, | ||
| shape | ||
| }); | ||
| } | ||
| } | ||
| return [names, metadata]; | ||
| }; | ||
| [this.inputNames, this.inputMetadata] = fillNamesAndMetadata( | ||
| OnnxruntimeSessionHandler.inferenceSession.get(this).inputMetadata | ||
| ); | ||
| [this.outputNames, this.outputMetadata] = fillNamesAndMetadata( | ||
| OnnxruntimeSessionHandler.inferenceSession.get(this).outputMetadata | ||
| ); | ||
| } | ||
| async dispose() { | ||
| OnnxruntimeSessionHandler.inferenceSession.get(this).dispose(); | ||
| } | ||
| run(feeds, fetches, options) { | ||
| return OnnxruntimeSessionHandler.inferenceSession.get(this).run(feeds, fetches, options); | ||
| } | ||
| } | ||
| class OnnxruntimeBackend { | ||
| init() {} | ||
| createInferenceSessionHandler(pathOrBuffer, options) { | ||
| return new OnnxruntimeSessionHandler(pathOrBuffer, options || {}); | ||
| } | ||
| } | ||
|
vkozyura marked this conversation as resolved.
|
||
| const onnxruntimeBackend = new OnnxruntimeBackend(); | ||
| const listSupportedBackends = binding.binding.listSupportedBackends; | ||
|
|
||
| export { InferenceSession }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.