ONNX
security
malware-detection
Vigil / source /pkg /compactonnx /types.go
turentomer's picture
Make published source self-contained
a2a3348 verified
Raw
History Blame Contribute Delete
2.89 kB
// Package compactonnx loads and scores explicitly supplied compact ONNX
// candidates. It has no embedded model and no legacy scoring fallback.
package compactonnx
import "huggingface.co/turenlabs/Vigil/source/pkg/compactmodel"
const (
MetadataSchema = "vigil.compact-onnx-metadata.v1"
InputName = "features"
OutputName = "probability"
MaxModelBytes = 10 * 1024 * 1024
)
// Metadata is the complete adjacent JSON contract for one compact candidate.
// The model digest and byte size bind the JSON to the exact ONNX bytes.
type Metadata struct {
SchemaVersion string `json:"schema_version"`
Model ModelBinding `json:"model"`
Preprocessing PreprocessingBinding `json:"preprocessing"`
Threshold float64 `json:"threshold"`
}
type ModelBinding struct {
Family string `json:"family"`
SHA256 string `json:"sha256"`
SizeBytes int64 `json:"size_bytes"`
InputName string `json:"input_name"`
InputFeatures int `json:"input_features"`
OutputName string `json:"output_name"`
OutputElements int `json:"output_elements"`
}
type PreprocessingBinding struct {
SchemaVersion string `json:"schema_version"`
ContractSHA256 string `json:"contract_sha256"`
WordFeatures int `json:"word_features"`
CharFeatures int `json:"char_features"`
StructuredFeatures int `json:"structured_features"`
TotalFeatures int `json:"total_features"`
}
// Config identifies an external development candidate. MetadataPath may be
// empty, in which case ModelPath+".json" is required. RuntimeLibrary is an
// optional explicit ONNX Runtime shared library path.
type Config struct {
ModelPath string
MetadataPath string
RuntimeLibrary string
}
// Markers are stable, aggregate-safe proof of the exact scoring runtime.
type Markers struct {
ModelLoaded bool `json:"model_loaded"`
ModelType string `json:"model_type"`
ModelSHA256 string `json:"model_sha256"`
ModelSizeBytes int64 `json:"model_size_bytes"`
MetadataSHA256 string `json:"metadata_sha256"`
PreprocessingContractSHA256 string `json:"preprocessing_contract_sha256"`
Runtime string `json:"runtime"`
RuntimeVersion string `json:"runtime_version"`
FallbackUsed bool `json:"fallback_used"`
}
func (metadata Metadata) preprocessingMetadata() compactmodel.Metadata {
return compactmodel.Metadata{
SchemaVersion: metadata.Preprocessing.SchemaVersion,
ContractSHA256: metadata.Preprocessing.ContractSHA256,
WordFeatures: metadata.Preprocessing.WordFeatures,
CharFeatures: metadata.Preprocessing.CharFeatures,
StructuredFeatures: metadata.Preprocessing.StructuredFeatures,
TotalFeatures: metadata.Preprocessing.TotalFeatures,
}
}