@wetron/onnx
v0.1.0
Published
ONNX model parser - extracts graph structure (nodes, edges, shapes) from .onnx files
Downloads
418
Maintainers
Readme
@wetron/onnx
ONNX model parser for wetron. Reads .onnx files and returns a ModelGraph IR. Inline initializer bytes use ModelGraph.weights.kind === "available"; initializers with data_location = EXTERNAL use kind === "external" and must be fetched separately.
Install
pnpm add @wetron/onnxIncluded automatically when you install @wetron/core or @wetron/react.
API
import { parseOnnx } from '@wetron/onnx';
const bytes = new Uint8Array(await file.arrayBuffer());
const graph = parseOnnx(bytes);Throws ParseError from @wetron/common/ir on malformed input.
External data loading
For models where initializers use data_location = EXTERNAL, fetch the external files and build a WeightSource:
import { loadOnnxExternalWeightsFromUrl } from '@wetron/onnx';
const weights = await loadOnnxExternalWeightsFromUrl(modelBytes, 'https://.../model-dir');
// weights.get("init_name") -> Uint8Array | undefined
const graphWithWeights = { ...graph, weights: { kind: 'available' as const, source: weights } };Each unique location filename is fetched once from ${baseUrl}/${location} and shared across initializers that slice it. If any initializer is external, parseOnnx does not expose a partial source for inline initializers. The loader returns an empty WeightSource when the model has no EXTERNAL initializers and throws ParseError on non-ok responses.
What gets parsed
- All nodes with op type, inputs, outputs, and attributes
- Initializer shapes and dtypes (raw weight data is not read)
- Intermediate tensor shapes from
value_info - Opset versions per domain
dtype mapping
| ONNX enum | dtype |
| --------- | ---------- |
| 1 | float32 |
| 2 | uint8 |
| 3 | int8 |
| 6 | int32 |
| 7 | int64 |
| 10 | float16 |
| 11 | float64 |
| 16 | bfloat16 |
