• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//
2// WARNING: This file is automatically generated!  Please edit onnx.in.proto.
3//
4
5
6// Copyright (c) ONNX Project Contributors.
7// Licensed under the MIT license.
8
9syntax = "proto2";
10
11package onnx;
12
13// Overview
14//
15// ONNX is an open specification that is comprised of the following components:
16//
17// 1)  A definition of an extensible computation graph model.
18// 2)  Definitions of standard data types.
19// 3)  Definitions of built-in operators.
20//
21// This document describes the syntax of models and their computation graphs,
22// as well as the standard data types. Together, they are referred to as the ONNX
23// Intermediate Representation, or 'IR' for short.
24//
25// The normative semantic specification of the ONNX IR is found in docs/IR.md.
26// Definitions of the built-in neural network operators may be found in docs/Operators.md.
27
28// Notes
29//
30// Release
31//
32// We are still in the very early stage of defining ONNX. The current
33// version of ONNX is a starting point. While we are actively working
34// towards a complete spec, we would like to get the community involved
35// by sharing our working version of ONNX.
36//
37// Protobuf compatibility
38//
39// To simplify framework compatibility, ONNX is defined using the subset of protobuf
40// that is compatible with both protobuf v2 and v3. This means that we do not use any
41// protobuf features that are only available in one of the two versions.
42//
43// Here are the most notable contortions we have to carry out to work around
44// these limitations:
45//
46//   - No 'map' (added protobuf 3.0). We instead represent mappings as lists
47//     of key-value pairs, where order does not matter and duplicates
48//     are not allowed.
49
50
51// Versioning
52//
53// ONNX versioning is specified in docs/IR.md and elaborated on in docs/Versioning.md
54//
55// To be compatible with both proto2 and proto3, we will use a version number
56// that is not defined by the default value but an explicit enum number.
57enum Version {
58  // proto3 requires the first enum value to be zero.
59  // We add this just to appease the compiler.
60  _START_VERSION = 0;
61  // The version field is always serialized and we will use it to store the
62  // version that the  graph is generated from. This helps us set up version
63  // control.
64  // For the IR, we are using simple numbers starting with with 0x00000001,
65  // which was the version we published on Oct 10, 2017.
66  IR_VERSION_2017_10_10 = 0x0000000000000001;
67
68  // IR_VERSION 2 published on Oct 30, 2017
69  // - Added type discriminator to AttributeProto to support proto3 users
70  IR_VERSION_2017_10_30 = 0x0000000000000002;
71
72  // IR VERSION 3 published on Nov 3, 2017
73  // - For operator versioning:
74  //    - Added new message OperatorSetIdProto
75  //    - Added opset_import in ModelProto
76  // - For vendor extensions, added domain in NodeProto
77  IR_VERSION_2017_11_3 = 0x0000000000000003;
78
79  // IR VERSION 4 published on Jan 22, 2019
80  // - Relax constraint that initializers should be a subset of graph inputs
81  // - Add type BFLOAT16
82  IR_VERSION_2019_1_22 = 0x0000000000000004;
83
84  // IR VERSION 5 published on March 18, 2019
85  // - Add message TensorAnnotation.
86  // - Add quantization annotation in GraphProto to map tensor with its scale and zero point quantization parameters.
87  IR_VERSION_2019_3_18 = 0x0000000000000005;
88
89  // IR VERSION 6 published on Sep 19, 2019
90  // - Add support for sparse tensor constants stored in model.
91  //   - Add message SparseTensorProto
92  //   - Add sparse initializers
93  IR_VERSION = 0x0000000000000006;
94}
95
96// Attributes
97//
98// A named attribute containing either singular float, integer, string, graph,
99// and tensor values, or repeated float, integer, string, graph, and tensor values.
100// An AttributeProto MUST contain the name field, and *only one* of the
101// following content fields, effectively enforcing a C/C++ union equivalent.
102message AttributeProto {
103
104  // Note: this enum is structurally identical to the OpSchema::AttrType
105  // enum defined in schema.h.  If you rev one, you likely need to rev the other.
106  enum AttributeType {
107    UNDEFINED = 0;
108    FLOAT = 1;
109    INT = 2;
110    STRING = 3;
111    TENSOR = 4;
112    GRAPH = 5;
113    SPARSE_TENSOR = 11;
114
115    FLOATS = 6;
116    INTS = 7;
117    STRINGS = 8;
118    TENSORS = 9;
119    GRAPHS = 10;
120    SPARSE_TENSORS = 12;
121  }
122
123  // The name field MUST be present for this version of the IR.
124  optional string name = 1;           // namespace Attribute
125
126  // if ref_attr_name is not empty, ref_attr_name is the attribute name in parent function.
127  // In this case, this AttributeProto does not contain data, and it's a reference of attribute
128  // in parent scope.
129  // NOTE: This should ONLY be used in function (sub-graph). It's invalid to be used in main graph.
130  optional string ref_attr_name = 21;
131
132  // A human-readable documentation for this attribute. Markdown is allowed.
133  optional string doc_string = 13;
134
135  // The type field MUST be present for this version of the IR.
136  // For 0.0.1 versions of the IR, this field was not defined, and
137  // implementations needed to use has_field hueristics to determine
138  // which value field was in use.  For IR_VERSION 0.0.2 or later, this
139  // field MUST be set and match the f|i|s|t|... field in use.  This
140  // change was made to accomodate proto3 implementations.
141  optional AttributeType type = 20;   // discriminator that indicates which field below is in use
142
143  // Exactly ONE of the following fields must be present for this version of the IR
144  optional float f = 2;               // float
145  optional int64 i = 3;               // int
146  optional bytes s = 4;               // UTF-8 string
147  optional TensorProto t = 5;         // tensor value
148  optional GraphProto g = 6;          // graph
149  optional SparseTensorProto sparse_tensor = 22;  // sparse tensor value
150  // Do not use field below, it's deprecated.
151  // optional ValueProto v = 12;         // value - subsumes everything but graph
152
153  repeated float floats = 7;          // list of floats
154  repeated int64 ints = 8;            // list of ints
155  repeated bytes strings = 9;         // list of UTF-8 strings
156  repeated TensorProto tensors = 10;  // list of tensors
157  repeated GraphProto graphs = 11;    // list of graph
158  repeated SparseTensorProto sparse_tensors = 23; // list of sparse tensors
159}
160
161// Defines information on value, including the name, the type, and
162// the shape of the value.
163message ValueInfoProto {
164  // This field MUST be present in this version of the IR.
165  optional string name = 1;     // namespace Value
166  // This field MUST be present in this version of the IR for
167  // inputs and outputs of the top-level graph.
168  optional TypeProto type = 2;
169  // A human-readable documentation for this value. Markdown is allowed.
170  optional string doc_string = 3;
171}
172
173// Nodes
174//
175// Computation graphs are made up of a DAG of nodes, which represent what is
176// commonly called a "layer" or "pipeline stage" in machine learning frameworks.
177//
178// For example, it can be a node of type "Conv" that takes in an image, a filter
179// tensor and a bias tensor, and produces the convolved output.
180message NodeProto {
181  repeated string input = 1;    // namespace Value
182  repeated string output = 2;   // namespace Value
183
184  // An optional identifier for this node in a graph.
185  // This field MAY be absent in ths version of the IR.
186  optional string name = 3;     // namespace Node
187
188  // The symbolic identifier of the Operator to execute.
189  optional string op_type = 4;  // namespace Operator
190  // The domain of the OperatorSet that specifies the operator named by op_type.
191  optional string domain = 7;   // namespace Domain
192
193  // Additional named attributes.
194  repeated AttributeProto attribute = 5;
195
196  // A human-readable documentation for this node. Markdown is allowed.
197  optional string doc_string = 6;
198}
199
200// Models
201//
202// ModelProto is a top-level file/container format for bundling a ML model and
203// associating its computation graph with metadata.
204//
205// The semantics of the model are described by the associated GraphProto.
206message ModelProto {
207  // The version of the IR this model targets. See Version enum above.
208  // This field MUST be present.
209  optional int64 ir_version = 1;
210
211  // The OperatorSets this model relies on.
212  // All ModelProtos MUST have at least one entry that
213  // specifies which version of the ONNX OperatorSet is
214  // being imported.
215  //
216  // All nodes in the ModelProto's graph will bind against the operator
217  // with the same-domain/same-op_type operator with the HIGHEST version
218  // in the referenced operator sets.
219  repeated OperatorSetIdProto opset_import = 8;
220
221  // The name of the framework or tool used to generate this model.
222  // This field SHOULD be present to indicate which implementation/tool/framework
223  // emitted the model.
224  optional string producer_name = 2;
225
226  // The version of the framework or tool used to generate this model.
227  // This field SHOULD be present to indicate which implementation/tool/framework
228  // emitted the model.
229  optional string producer_version = 3;
230
231  // Domain name of the model.
232  // We use reverse domain names as name space indicators. For example:
233  // `com.facebook.fair` or `com.microsoft.cognitiveservices`
234  //
235  // Together with `model_version` and GraphProto.name, this forms the unique identity of
236  // the graph.
237  optional string domain = 4;
238
239  // The version of the graph encoded. See Version enum below.
240  optional int64 model_version = 5;
241
242  // A human-readable documentation for this model. Markdown is allowed.
243  optional string doc_string = 6;
244
245  // The parameterized graph that is evaluated to execute the model.
246  optional GraphProto graph = 7;
247
248  // Named metadata values; keys should be distinct.
249  repeated StringStringEntryProto metadata_props = 14;
250};
251
252// StringStringEntryProto follows the pattern for cross-proto-version maps.
253// See https://developers.google.com/protocol-buffers/docs/proto3#maps
254message StringStringEntryProto {
255  optional string key = 1;
256  optional string value= 2;
257};
258
259message TensorAnnotation {
260  optional string tensor_name = 1;
261  // <key, value> pairs to annotate tensor specified by <tensor_name> above.
262  // The keys used in the mapping below must be pre-defined in ONNX spec.
263  // For example, for 8-bit linear quantization case, 'SCALE_TENSOR', 'ZERO_POINT_TENSOR' will be pre-defined as
264  // quantization parameter keys.
265  repeated StringStringEntryProto quant_parameter_tensor_names = 2;
266}
267
268
269
270// Graphs
271//
272// A graph defines the computational logic of a model and is comprised of a parameterized
273// list of nodes that form a directed acyclic graph based on their inputs and outputs.
274// This is the equivalent of the "network" or "graph" in many deep learning
275// frameworks.
276message GraphProto {
277  // The nodes in the graph, sorted topologically.
278  repeated NodeProto node = 1;
279
280  // The name of the graph.
281  optional string name = 2;   // namespace Graph
282
283  // A list of named tensor values, used to specify constant inputs of the graph.
284  // Each TensorProto entry must have a distinct name (within the list) that
285  // MAY also appear in the input list.
286  repeated TensorProto initializer = 5;
287
288  // Initializers (see above) stored in sparse format.
289  repeated SparseTensorProto sparse_initializer = 15;
290
291  // A human-readable documentation for this graph. Markdown is allowed.
292  optional string doc_string = 10;
293
294  // The inputs and outputs of the graph.
295  repeated ValueInfoProto input = 11;
296  repeated ValueInfoProto output = 12;
297
298  // Information for the values in the graph. The ValueInfoProto.name's
299  // must be distinct. It is optional for a value to appear in value_info list.
300  repeated ValueInfoProto value_info = 13;
301
302  // This field carries information to indicate the mapping among a tensor and its
303  // quantization parameter tensors. For example:
304  // For tensor 'a', it may have {'SCALE_TENSOR', 'a_scale'} and {'ZERO_POINT_TENSOR', 'a_zero_point'} annotated,
305  // which means, tensor 'a_scale' and tensor 'a_zero_point' are scale and zero point of tensor 'a' in the model.
306  repeated TensorAnnotation quantization_annotation = 14;
307
308  // DO NOT USE the following fields, they were deprecated from earlier versions.
309  // repeated string input = 3;
310  // repeated string output = 4;
311  // optional int64 ir_version = 6;
312  // optional int64 producer_version = 7;
313  // optional string producer_tag = 8;
314  // optional string domain = 9;
315}
316
317// Tensors
318//
319// A serialized tensor value.
320message TensorProto {
321  enum DataType {
322    UNDEFINED = 0;
323    // Basic types.
324    FLOAT = 1;   // float
325    UINT8 = 2;   // uint8_t
326    INT8 = 3;    // int8_t
327    UINT16 = 4;  // uint16_t
328    INT16 = 5;   // int16_t
329    INT32 = 6;   // int32_t
330    INT64 = 7;   // int64_t
331    STRING = 8;  // string
332    BOOL = 9;    // bool
333
334    // IEEE754 half-precision floating-point format (16 bits wide).
335    // This format has 1 sign bit, 5 exponent bits, and 10 mantissa bits.
336    FLOAT16 = 10;
337
338    DOUBLE = 11;
339    UINT32 = 12;
340    UINT64 = 13;
341    COMPLEX64 = 14;     // complex with float32 real and imaginary components
342    COMPLEX128 = 15;    // complex with float64 real and imaginary components
343
344    // Non-IEEE floating-point format based on IEEE754 single-precision
345    // floating-point number truncated to 16 bits.
346    // This format has 1 sign bit, 8 exponent bits, and 7 mantissa bits.
347    BFLOAT16 = 16;
348
349    // Future extensions go here.
350  }
351
352  // The shape of the tensor.
353  repeated int64 dims = 1;
354
355  // The data type of the tensor.
356  // This field MUST have a valid TensorProto.DataType value
357  optional int32 data_type = 2;
358
359  // For very large tensors, we may want to store them in chunks, in which
360  // case the following fields will specify the segment that is stored in
361  // the current TensorProto.
362  message Segment {
363    optional int64 begin = 1;
364    optional int64 end = 2;
365  }
366  optional Segment segment = 3;
367
368  // Tensor content must be organized in row-major order.
369  //
370  // Depending on the data_type field, exactly one of the fields below with
371  // name ending in _data is used to store the elements of the tensor.
372
373  // For float and complex64 values
374  // Complex64 tensors are encoded as a single array of floats,
375  // with the real components appearing in odd numbered positions,
376  // and the corresponding imaginary component apparing in the
377  // subsequent even numbered position. (e.g., [1.0 + 2.0i, 3.0 + 4.0i]
378  // is encoded as [1.0, 2.0 ,3.0 ,4.0]
379  // When this field is present, the data_type field MUST be FLOAT or COMPLEX64.
380  repeated float float_data = 4 [packed = true];
381
382  // For int32, uint8, int8, uint16, int16, bool, and float16 values
383  // float16 values must be bit-wise converted to an uint16_t prior
384  // to writing to the buffer.
385  // When this field is present, the data_type field MUST be
386  // INT32, INT16, INT8, UINT16, UINT8, BOOL, or FLOAT16
387  repeated int32 int32_data = 5 [packed = true];
388
389  // For strings.
390  // Each element of string_data is a UTF-8 encoded Unicode
391  // string. No trailing null, no leading BOM. The protobuf "string"
392  // scalar type is not used to match ML community conventions.
393  // When this field is present, the data_type field MUST be STRING
394  repeated bytes string_data = 6;
395
396  // For int64.
397  // When this field is present, the data_type field MUST be INT64
398  repeated int64 int64_data = 7 [packed = true];
399
400  // Optionally, a name for the tensor.
401  optional string name = 8; // namespace Value
402
403  // A human-readable documentation for this tensor. Markdown is allowed.
404  optional string doc_string = 12;
405
406  // Serializations can either use one of the fields above, or use this
407  // raw bytes field. The only exception is the string case, where one is
408  // required to store the content in the repeated bytes string_data field.
409  //
410  // When this raw_data field is used to store tensor value, elements MUST
411  // be stored in as fixed-width, little-endian order.
412  // Floating-point data types MUST be stored in IEEE 754 format.
413  // Complex64 elements must be written as two consecutive FLOAT values, real component first.
414  // Complex128 elements must be written as two consecutive DOUBLE values, real component first.
415  // Boolean type MUST be written one byte per tensor element (00000001 for true, 00000000 for false).
416  //
417  // Note: the advantage of specific field rather than the raw_data field is
418  // that in some cases (e.g. int data), protobuf does a better packing via
419  // variable length storage, and may lead to smaller binary footprint.
420  // When this field is present, the data_type field MUST NOT be STRING or UNDEFINED
421  optional bytes raw_data = 9;
422
423  // Data can be stored inside the protobuf file using type-specific fields or raw_data.
424  // Alternatively, raw bytes data can be stored in an external file, using the external_data field.
425  // external_data stores key-value pairs describing data location. Recognized keys are:
426  // - "location" (required) - POSIX filesystem path relative to the directory where the ONNX
427  //                           protobuf model was stored
428  // - "offset" (optional) - position of byte at which stored data begins. Integer stored as string.
429  //                         Offset values SHOULD be multiples 4096 (page size) to enable mmap support.
430  // - "length" (optional) - number of bytes containing data. Integer stored as string.
431  // - "checksum" (optional) - SHA1 digest of file specified in under 'location' key.
432  repeated StringStringEntryProto external_data = 13;
433
434  // Location of the data for this tensor. MUST be one of:
435  // - DEFAULT - data stored inside the protobuf message. Data is stored in raw_data (if set) otherwise in type-specified field.
436  // - EXTERNAL - data stored in an external location as described by external_data field.
437  enum DataLocation {
438    DEFAULT = 0;
439    EXTERNAL = 1;
440  }
441
442  // If value not set, data is stored in raw_data (if set) otherwise in type-specified field.
443  optional DataLocation data_location = 14;
444
445  // For double
446  // Complex128 tensors are encoded as a single array of doubles,
447  // with the real components appearing in odd numbered positions,
448  // and the corresponding imaginary component apparing in the
449  // subsequent even numbered position. (e.g., [1.0 + 2.0i, 3.0 + 4.0i]
450  // is encoded as [1.0, 2.0 ,3.0 ,4.0]
451  // When this field is present, the data_type field MUST be DOUBLE or COMPLEX128
452  repeated double double_data = 10 [packed = true];
453
454  // For uint64 and uint32 values
455  // When this field is present, the data_type field MUST be
456  // UINT32 or UINT64
457  repeated uint64 uint64_data = 11 [packed = true];
458}
459
460// A serialized sparse-tensor value
461message SparseTensorProto {
462  // The sequence of non-default values are encoded as a tensor of shape [NNZ].
463  // The default-value is zero for numeric tensors, and empty-string for string tensors.
464  optional TensorProto values = 1;
465
466  // The indices of the non-default values, which may be stored in one of two formats.
467  // (a) Indices can be a tensor of shape [NNZ, rank] with the [i,j]-th value
468  // corresponding to the j-th index of the i-th value (in the values tensor).
469  // (b) Indices can be a tensor of shape [NNZ], in which case the i-th value
470  // must be the linearized-index of the i-th value (in the values tensor).
471  // The linearized-index can be converted into an index tuple (k_1,...,k_rank)
472  // using the shape provided below.
473  // The indices must appear in ascending order without duplication.
474  // In the first format, the ordering is lexicographic-ordering:
475  // e.g., index-value [1,4] must appear before [2,1]
476  optional TensorProto indices = 2;
477
478  // The shape of the underlying dense-tensor: [dim_1, dim_2, ... dim_rank]
479  repeated int64 dims = 3;
480}
481
482// Defines a tensor shape. A dimension can be either an integer value
483// or a symbolic variable. A symbolic variable represents an unknown
484// dimension.
485message TensorShapeProto {
486  message Dimension {
487    oneof value {
488      int64 dim_value = 1;
489      string dim_param = 2;   // namespace Shape
490    };
491    // Standard denotation can optionally be used to denote tensor
492    // dimensions with standard semantic descriptions to ensure
493    // that operations are applied to the correct axis of a tensor.
494    // Refer to https://github.com/onnx/onnx/blob/master/docs/DimensionDenotation.md#denotation-definition
495    // for pre-defined dimension denotations.
496    optional string denotation = 3;
497  };
498  repeated Dimension dim = 1;
499}
500
501// Types
502//
503// The standard ONNX data types.
504message TypeProto {
505
506  message Tensor {
507    // This field MUST NOT have the value of UNDEFINED
508    // This field MUST have a valid TensorProto.DataType value
509    // This field MUST be present for this version of the IR.
510    optional int32 elem_type = 1;
511    optional TensorShapeProto shape = 2;
512  }
513
514  // repeated T
515  message Sequence {
516    // The type and optional shape of each element of the sequence.
517    // This field MUST be present for this version of the IR.
518    optional TypeProto elem_type = 1;
519  };
520
521  // map<K,V>
522  message Map {
523    // This field MUST have a valid TensorProto.DataType value
524    // This field MUST be present for this version of the IR.
525    // This field MUST refer to an integral type ([U]INT{8|16|32|64}) or STRING
526    optional int32 key_type = 1;
527    // This field MUST be present for this version of the IR.
528    optional TypeProto value_type = 2;
529  };
530
531
532  oneof value {
533    // The type of a tensor.
534    Tensor tensor_type = 1;
535
536    // NOTE:  DNN-only implementations of ONNX MAY elect to not support non-tensor values
537    //        as input and output to graphs and nodes. These types are needed to naturally
538    //        support classical ML operators.  DNN operators SHOULD restrict their input
539    //        and output types to tensors.
540
541    // The type of a sequence.
542    Sequence sequence_type = 4;
543
544    // The type of a map.
545    Map map_type = 5;
546
547  }
548
549  // An optional denotation can be used to denote the whole
550  // type with a standard semantic description as to what is
551  // stored inside. Refer to https://github.com/onnx/onnx/blob/master/docs/TypeDenotation.md#type-denotation-definition
552  // for pre-defined type denotations.
553  optional string denotation = 6;
554}
555
556// Operator Sets
557//
558// OperatorSets are uniquely identified by a (domain, opset_version) pair.
559message OperatorSetIdProto {
560  // The domain of the operator set being identified.
561  // The empty string ("") or absence of this field implies the operator
562  // set that is defined as part of the ONNX specification.
563  // This field MUST be present in this version of the IR when referring to any other operator set.
564  optional string domain = 1;
565
566  // The version of the operator set being identified.
567  // This field MUST be present in this version of the IR.
568  optional int64 version = 2;
569}