OnnxWeightLoader

Utility for loading ONNX model initializers (weights) into SKaiNET Module parameters.

Features:

  • ONNX-specific tensor decoding (FLOAT, INT32, INT64)

  • Format-agnostic weight mapping via WeightMapper

  • Support for multiple tensor data types (all converted to Float)

  • Debug mode for troubleshooting weight mapping issues

Example usage:

val model = ModelProto.decodeFromByteArray(bytes)
val ctx = DirectCpuExecutionContext(phase = Phase.INFERENCE)
val loadResult = OnnxWeightLoader.loadInitializers(model, ctx)
val mapping = OnnxWeightLoader.applyWeights(module, loadResult.tensors)

Types

Link copied to clipboard
data class InitializerLoadResult(val tensors: List<OnnxWeightLoader.InitTensor>, val skipped: List<String>)

Result of loading initializers from an ONNX model.

Link copied to clipboard
data class InitTensor(val name: String, val isBias: Boolean, val shape: List<Int>, val tensor: Tensor<FP32, Float>)

Holds a decoded ONNX tensor ready for weight assignment.

Functions

Link copied to clipboard

Apply ONNX weights to a module using name-based matching with shape-based fallback.

Link copied to clipboard

Apply weights from legacy InitTensor list.

Link copied to clipboard

Apply weights with optional debug output.

Link copied to clipboard

Decode a single ONNX TensorProto into a WeightTensor.

Link copied to clipboard

Extract the most specific "model.X.Y.Z" pattern from a DSL module path. E.g., "Yolo8/model.22.cv2.0/model.22.cv2.0.0/Conv2d-0" -> "model.22.cv2.0.0"

Link copied to clipboard

Load all initializers (weights) from an ONNX ModelProto.

Link copied to clipboard

Load initializers returning the legacy InitializerLoadResult type.

Link copied to clipboard
fun matchesOnnxLayer(onnxName: String, dslLayerName: String?): Boolean

Check if an ONNX tensor name matches a DSL layer name.

Link copied to clipboard

Normalize a DSL layer name to ONNX naming format. DSL uses underscores in some places while ONNX uses dots.

Link copied to clipboard
fun validateAllParametersMapped(mapping: WeightMapper.MappingResult, skipped: List<String> = emptyList())

Validate that all module parameters were mapped from ONNX initializers. Throws IllegalArgumentException if mapping is incomplete.