ONNX
security
malware-detection
File size: 2,887 Bytes
d2507b5
 
 
 
a2a3348
d2507b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 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,
	}
}