WeightMapper

Unified weight mapping utility for applying loaded weights to SKaiNET modules.

This object provides format-agnostic weight mapping logic that can be used by any model loader (ONNX, GGUF, SafeTensors, etc.). It supports:

  • Path-based name matching with automatic normalization

  • Shape-based fallback matching when names don't match

  • Debug mode for troubleshooting mapping issues

Example usage:

val tensors: List<WeightTensor<FP32, Float>> = loader.loadWeights(modelFile)
val result = WeightMapper.applyWeights(module, tensors)
WeightMapper.validateAllParametersMapped(result)

Types

Link copied to clipboard
data class MappingResult(val mapped: Int, val total: Int, val missingParams: List<String>, val unusedTensors: List<String>)

Result of mapping weights to module parameters.

Functions

Link copied to clipboard
fun <T : DType, V> applyWeights(module: Module<T, V>, tensors: List<WeightTensor<T, V>>, config: MappingConfig = MappingConfig()): WeightMapper.MappingResult

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

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
fun matchesLayerName(tensorName: String, layerName: String?): Boolean

Check if a weight tensor name matches a DSL layer name. E.g., "model.22.cv2.0.0.conv.weight" matches DSL: "model.22.cv2.0.0"

Link copied to clipboard

Normalize a DSL layer name to ONNX-style 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 weight tensors. Throws IllegalArgumentException if mapping is incomplete.