or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

backend-integration.mdindex.mdmodel-composition.mdmodel-construction.mdmodel-hub.mdmodel-io.mdmodel-validation.mdnumpy-integration.mdoperator-definitions.mdreference-implementation.mdshape-inference.mdtext-processing.mdversion-conversion.md

model-validation.mddocs/

0

# Model Validation

1

2

Comprehensive validation functions to verify ONNX model correctness, including graph structure, node compatibility, type consistency, and operator definitions. These functions ensure models conform to the ONNX specification and can be executed correctly by runtimes.

3

4

## Capabilities

5

6

### Model Validation

7

8

Validate complete ONNX models with comprehensive checks for structure, types, and operator compatibility.

9

10

```python { .api }

11

def check_model(

12

model: ModelProto | str | bytes | os.PathLike,

13

full_check: bool = False,

14

skip_opset_compatibility_check: bool = False,

15

) -> None:

16

"""

17

Check the consistency of a model. An exception is raised if the test fails.

18

19

Parameters:

20

- model: Model to check.

21

- full_check: If True, the function also checks for shapes that can be inferred.

22

- skip_opset_compatibility_check: If True, the function skips the check for

23

opset compatibility.

24

"""

25

```

26

27

### Graph Validation

28

29

Validate computation graphs for structural correctness and data flow consistency.

30

31

```python { .api }

32

def check_graph(graph, ctx=DEFAULT_CONTEXT):

33

"""

34

Validate a GraphProto.

35

36

Parameters:

37

- graph: GraphProto to validate

38

- ctx: Validation context for configuration

39

40

Returns:

41

None (raises ValidationError if invalid)

42

43

Raises:

44

ValidationError: If graph structure is invalid

45

"""

46

```

47

48

### Node Validation

49

50

Validate individual nodes for operator compatibility and attribute correctness.

51

52

```python { .api }

53

def check_node(node, ctx=DEFAULT_CONTEXT):

54

"""

55

Validate a NodeProto.

56

57

Parameters:

58

- node: NodeProto to validate

59

- ctx: Validation context for configuration

60

61

Returns:

62

None (raises ValidationError if invalid)

63

64

Raises:

65

ValidationError: If node is invalid (unknown operator, wrong attributes, etc.)

66

"""

67

```

68

69

### Function Validation

70

71

Validate user-defined functions for correctness and compatibility.

72

73

```python { .api }

74

def check_function(function, ctx=DEFAULT_CONTEXT):

75

"""

76

Validate a FunctionProto.

77

78

Parameters:

79

- function: FunctionProto to validate

80

- ctx: Validation context for configuration

81

82

Returns:

83

None (raises ValidationError if invalid)

84

85

Raises:

86

ValidationError: If function definition is invalid

87

"""

88

```

89

90

### Tensor Validation

91

92

Validate tensor data structures for type consistency and data integrity.

93

94

```python { .api }

95

def check_tensor(tensor: TensorProto, ctx: C.CheckerContext = DEFAULT_CONTEXT) -> None:

96

"""

97

Validate a TensorProto.

98

99

Parameters:

100

- tensor: TensorProto to validate

101

- ctx: Validation context for configuration

102

103

Returns:

104

None (raises ValidationError if invalid)

105

106

Raises:

107

ValidationError: If tensor data is inconsistent or malformed

108

"""

109

110

def check_sparse_tensor(

111

sparse: SparseTensorProto, ctx: C.CheckerContext = DEFAULT_CONTEXT

112

) -> None:

113

"""

114

Validate a SparseTensorProto.

115

116

Parameters:

117

- sparse: SparseTensorProto to validate

118

- ctx: Validation context for configuration

119

120

Returns:

121

None (raises ValidationError if invalid)

122

123

Raises:

124

ValidationError: If sparse tensor structure is invalid

125

"""

126

```

127

128

### Attribute Validation

129

130

Validate node attributes for type correctness and operator compatibility.

131

132

```python { .api }

133

def check_attribute(attribute, ctx=DEFAULT_CONTEXT):

134

"""

135

Validate an AttributeProto.

136

137

Parameters:

138

- attribute: AttributeProto to validate

139

- ctx: Validation context for configuration

140

141

Returns:

142

None (raises ValidationError if invalid)

143

144

Raises:

145

ValidationError: If attribute type or value is invalid

146

"""

147

148

def check_value_info(value_info, ctx=DEFAULT_CONTEXT):

149

"""

150

Validate a ValueInfoProto.

151

152

Parameters:

153

- value_info: ValueInfoProto to validate

154

- ctx: Validation context for configuration

155

156

Returns:

157

None (raises ValidationError if invalid)

158

159

Raises:

160

ValidationError: If value info type or shape is invalid

161

"""

162

```

163

164

### Validation Constants and Context

165

166

Important constants and context objects for validation configuration:

167

168

```python { .api }

169

DEFAULT_CONTEXT # Default validation context with standard settings

170

MAXIMUM_PROTOBUF # Maximum protobuf file size limit (2GB)

171

172

class ValidationError(Exception):

173

"""

174

Exception raised when ONNX validation fails.

175

176

Contains detailed information about validation failures

177

including location and reason for the error.

178

"""

179

180

# Checker context from C++ implementation

181

C # Module containing C++ checker implementation

182

```

183

184

185

## Usage Examples

186

187

### Basic Model Validation

188

189

```python

190

import onnx

191

from onnx.checker import check_model, ValidationError

192

193

try:

194

# Load and validate a model

195

model = onnx.load_model("path/to/model.onnx")

196

check_model(model)

197

print("Model is valid!")

198

199

except ValidationError as e:

200

print(f"Model validation failed: {e}")

201

except Exception as e:

202

print(f"Error loading model: {e}")

203

```

204

205

### Comprehensive Model Checking

206

207

```python

208

import onnx

209

from onnx.checker import check_model, ValidationError

210

211

# Load model

212

model = onnx.load_model("complex_model.onnx")

213

214

try:

215

# Perform full validation (slower but comprehensive)

216

check_model(model, full_check=True)

217

print("Model passed comprehensive validation!")

218

219

except ValidationError as e:

220

print(f"Validation failed: {e}")

221

# Print specific validation errors

222

print("This could indicate:")

223

print("- Unknown or unsupported operators")

224

print("- Type mismatches between connected nodes")

225

print("- Missing required attributes")

226

print("- Invalid tensor shapes or data types")

227

```

228

229

### Validating Individual Components

230

231

```python

232

import onnx

233

from onnx import helper, checker, TensorProto

234

235

# Create a simple node

236

node = helper.make_node('Relu', ['input'], ['output'])

237

238

try:

239

# Validate the node

240

checker.check_node(node)

241

print("Node is valid!")

242

except checker.ValidationError as e:

243

print(f"Node validation failed: {e}")

244

245

# Create and validate a tensor

246

import numpy as np

247

data = np.array([1.0, 2.0, 3.0], dtype=np.float32)

248

tensor = helper.make_tensor('test_tensor', TensorProto.FLOAT, [3], data)

249

250

try:

251

checker.check_tensor(tensor)

252

print("Tensor is valid!")

253

except checker.ValidationError as e:

254

print(f"Tensor validation failed: {e}")

255

```

256

257

### Validation During Model Construction

258

259

```python

260

import onnx

261

from onnx import helper, checker, TensorProto

262

263

def build_and_validate_model():

264

# Define inputs and outputs

265

X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [1, 3, 224, 224])

266

Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [1, 1000])

267

268

# Create nodes

269

conv_node = helper.make_node(

270

'Conv', ['X', 'W'], ['conv_out'],

271

kernel_shape=[3, 3], pads=[1, 1, 1, 1]

272

)

273

274

# Validate node immediately

275

try:

276

checker.check_node(conv_node)

277

except checker.ValidationError as e:

278

print(f"Invalid node: {e}")

279

return None

280

281

relu_node = helper.make_node('Relu', ['conv_out'], ['Y'])

282

283

# Create weight tensor

284

weight_data = np.random.randn(1000, 3, 3, 3).astype(np.float32)

285

weight = helper.make_tensor('W', TensorProto.FLOAT,

286

weight_data.shape, weight_data)

287

288

# Create graph

289

graph = helper.make_graph(

290

[conv_node, relu_node],

291

'test_model',

292

[X], [Y], [weight]

293

)

294

295

# Validate graph

296

try:

297

checker.check_graph(graph)

298

except checker.ValidationError as e:

299

print(f"Invalid graph: {e}")

300

return None

301

302

# Create and validate model

303

model = helper.make_model(graph)

304

305

try:

306

checker.check_model(model)

307

print("Model construction and validation successful!")

308

return model

309

except checker.ValidationError as e:

310

print(f"Invalid model: {e}")

311

return None

312

313

# Build and validate

314

model = build_and_validate_model()

315

```

316

317

### Custom Validation Context

318

319

```python

320

import onnx

321

from onnx import checker

322

323

# For advanced use cases, custom validation contexts can be used

324

# to configure specific validation behavior

325

326

try:

327

model = onnx.load_model("model.onnx")

328

329

# Use default context for standard validation

330

checker.check_model(model, full_check=True)

331

332

# Skip operator set compatibility for experimental models

333

checker.check_model(model, skip_opset_compatibility_check=True)

334

335

except checker.ValidationError as e:

336

# Handle specific validation errors

337

error_msg = str(e)

338

if "operator" in error_msg.lower():

339

print("Operator-related validation error")

340

elif "type" in error_msg.lower():

341

print("Type-related validation error")

342

elif "shape" in error_msg.lower():

343

print("Shape-related validation error")

344

else:

345

print("General validation error")

346

print(f"Details: {e}")

347

```