Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Onnxruntime Session Run Nodejs

From Leeroopedia
Revision as of 15:47, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Microsoft_Onnxruntime_Session_Run_Nodejs.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Field Value
Implementation Name Session_Run_Nodejs
Overview Asynchronous execution of ONNX model inference using the session.run() method with named input tensors in Node.js.
Type API Doc
Language JavaScript
Domains ML_Inference, JavaScript_Integration
Source Repository microsoft/onnxruntime
Last Updated 2026-02-10

Overview

Asynchronous execution of ONNX model inference using the session.run() method with named input tensors in Node.js. This method accepts a feeds dictionary mapping input names to ort.Tensor objects and returns a Promise resolving to a results dictionary mapping output names to output tensors.

API

session.run(feeds: Record<string, ort.Tensor>) -> Promise<Record<string, ort.Tensor>>

Source Code Reference

The sample model in the basic usage example contains a single MatMul node with two inputs a (float32, 3x4) and b (float32, 4x3) and one output c (float32, 3x3).

Key Parameters

Parameter Type Required Description
feeds Record<string, ort.Tensor> Yes Object mapping model input names to ort.Tensor objects containing input data

I/O Contract

Direction Name Type Description
Input feeds Record<string, ort.Tensor> Dictionary of named input tensors; keys must match model input names
Output results Promise<Record<string, ort.Tensor>> Promise resolving to a dictionary of named output tensors

Usage Examples

Basic MatMul Inference

const ort = require('onnxruntime-node');

async function main() {
    // Create session from model
    const session = await ort.InferenceSession.create('./model.onnx');

    // Prepare input tensors
    const dataA = Float32Array.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
    const dataB = Float32Array.from([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]);
    const tensorA = new ort.Tensor('float32', dataA, [3, 4]);
    const tensorB = new ort.Tensor('float32', dataB, [4, 3]);

    // Build feeds object with model input names as keys
    const feeds = { a: tensorA, b: tensorB };

    // Run inference
    const results = await session.run(feeds);

    // Access output tensor
    const dataC = results.c.data;
    console.log(`data of result tensor 'c': ${dataC}`);
}
main();

Inspecting Output Metadata

const results = await session.run(feeds);

// Each output is an ort.Tensor with type, dims, data, and size
for (const [name, tensor] of Object.entries(results)) {
    console.log(`Output '${name}': type=${tensor.type}, dims=${tensor.dims}, size=${tensor.size}`);
}

Key Details

  • The feeds object keys must exactly match the input names defined in the ONNX model graph.
  • The method is asynchronous; inference computation happens on native background threads via N-API.
  • Input tensor shapes and types must match the model's expected input specifications.
  • The returned results object maps output names to ort.Tensor instances containing the computed values.
  • Each call to run() executes one complete forward pass through the model graph.
  • The Node.js event loop remains responsive during inference computation.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment