• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Revision History
16// Version 0: Initial version.
17// Version 1: Add subgraphs to schema.
18// Version 2: Rename operators to conform to NN API.
19// Version 3: Move buffer data from Model.Subgraph.Tensors to Model.Buffers.
20
21namespace tflite;
22
23// This corresponds to the version.
24file_identifier "TFL3";
25// File extension of any written files.
26file_extension "tflite";
27
28// IMPORTANT: All new members of tables, enums and unions must be added at the
29// end to ensure backwards compatibility.
30
31// The type of data stored in a tensor.
32enum TensorType : byte {
33  FLOAT32 = 0,
34  FLOAT16 = 1,
35  INT32 = 2,
36  UINT8 = 3,
37  INT64 = 4,
38  STRING = 5,
39  BOOL = 6,
40  INT16 = 7,
41  COMPLEX64 = 8,
42  INT8 = 9,
43  FLOAT64 = 10,
44}
45
46// Custom quantization parameters for experimenting with new quantization
47// techniques.
48table CustomQuantization {
49  custom:[ubyte] (force_align: 16);
50}
51
52// Represents a specific quantization technique's parameters.
53union QuantizationDetails {
54  CustomQuantization,
55}
56
57// Parameters for converting a quantized tensor back to float.
58table QuantizationParameters {
59  // These four parameters are the asymmetric linear quantization parameters.
60  // Given a quantized value q, the corresponding float value f should be:
61  //   f = scale * (q - zero_point)
62  // For other quantization types, the QuantizationDetails below is used.
63  min:[float];  // For importing back into tensorflow.
64  max:[float];  // For importing back into tensorflow.
65  scale:[float];  // For dequantizing the tensor's values.
66  zero_point:[long];
67
68  // If this is not none, the other quantization parameters (i.e. min, max,
69  // scale, zero_point fields above) are ignored and the value of the
70  // QuantizationDetails union should be used.
71  details:QuantizationDetails;
72
73  // Specifies the dimension of the Tensor's shape that the scales and
74  // zero_points correspond to. For example, a tensor t, with dims=[4, 3, 2, 1]
75  // with quantization params:
76  //   scale=[1.0, 2.0, 3.0], zero_point=[1, 2, 3], quantization_dimension=1
77  // will be quantized across the second dimension of t.
78  //   t[:, 0, :, :] will have scale[0]=1.0, zero_point[0]=1
79  //   t[:, 1, :, :] will have scale[1]=2.0, zero_point[0]=2
80  //   t[:, 2, :, :] will have scale[2]=3.0, zero_point[0]=3
81  quantized_dimension:int;
82}
83
84// Sparse tensors.
85// We use a modification of the TACO format.
86// Reference: http://tensor-compiler.org/kjolstad-oopsla17-tensor-compiler.pdf
87//
88// To encode a conceptual n-dimensional dense tensor with dims (d0, ..., dn-1),
89// potentially with a k-dimensional block (0 <= k <= n) with dims
90// (dn, ..., dn+k-1), the format needs to specify:
91//   1. In what order to traverse these dimensions. For example, to store a 2-D
92//      matrix in row major order, the traversal order would be (d0, d1),
93//      whereas to store it in column major order, the traversal order would be
94//      (d1, d0). If the 2-D matrix has a 2-D inner block, the traversal order
95//      could be (d0, d1, d2, d3).
96//   2. How each block dimension in (dn, ..., dn+k-1) maps to the original
97//      tensor dimension in (d0, ..., dn-1).
98//   3. In the traversal order defined above, the format (dense vs. sparse) and
99//      index metadata for each dimension. For a dense dimension, this is just
100//      the size of that dimension. For a sparse dimension, it's the same as
101//      the compressed index defined in the Compressed Sparse Row (CSR) format.
102//      (http://scipy-lectures.org/advanced/scipy_sparse/csr_matrix.html)
103
104// The storage type for a dimension. Currently we support:
105//   1. DENSE: each coordinate in this dimension is stored implicitly.
106//   2. SPARSE_CSR: only the coordinates with non-zero elements are stored. The
107//      compression technique is the same what CSR uses.
108// More types like a sparse dimension with a different compression technique
109// could be added to the list in the future.
110enum DimensionType : byte {
111  DENSE = 0,
112  SPARSE_CSR = 1,
113}
114
115table Int32Vector {
116  values:[int];
117}
118
119table Uint16Vector {
120  values:[ushort] (force_align: 4);
121}
122
123table Uint8Vector {
124  values:[ubyte] (force_align: 4);
125}
126
127// Variable-typed buffer to store the index metadata for a sparse dimension.
128// The widest type is Int32 instead of UInt32 because tensor's shape is a int32
129// vector. We don't want the per-dimensional index to overflow that range.
130union SparseIndexVector {
131  Int32Vector,
132  Uint16Vector,
133  Uint8Vector
134}
135
136table DimensionMetadata {
137  // Whether a dimension is dense or sparse.
138  format:DimensionType;
139  // Index metadata used for a dimension.
140  //   - If format is DimensionType.DENSE then we use the dense_size field to
141  //     store the size of that dimension. Each index in that dimension is
142  //     stored implicitly.
143  //   - If format is DimensionType.SPARSE_CSR then we use array_segments and
144  //     array_indices to encode that dimension. array_segments represents how
145  //     to segment the indices array, each segment corresponds to one element
146  //     in the previous dimension. array_indices represents the index of the
147  //     non-zero elements within this dimension (as those in the CSR matrix
148  //     format, where the first array is row pointers and the second array is
149  //     column indices).
150  dense_size:int;
151  array_segments:SparseIndexVector;
152  array_indices:SparseIndexVector;
153}
154
155// Parameters to encode a sparse TfLite tensor.
156table SparsityParameters {
157  // The traversal order of the dimensions defined in the `shape` field of the
158  // conceptual dense tensor. For a n-dimensional tensors with dims (d0, d1,
159  // ..., dn-1),
160  //   - if not block sparse, the traversal_order is just a permutation of (d0,
161  //     ..., dn-1). For example, a 2-D matrix stored in row-major order would
162  //     have traversal_order = (d0, d1).
163  //   - if block sparse with a k-dimensional block (0 <= k <= n), the
164  //     traversal_order has n + k elements. The first n elements are still a
165  //     permutation of (d0, ..., dn-1). The lask k elements are a permutation
166  //     of (dn, ..., dn+k-1), defining how to traverse a block internally. For
167  //     example, a 2-D matrix with 2-D blocks, both stored in row-major order
168  //     would have traversal_order = (d0, d1, d2, d3).
169  traversal_order:[int];
170  // For an n-dimensional tensor with a k-dimensional block (0 <= k <= n),
171  // stores how a block dimension in (dn, ..., dn+k-1) maps to the original
172  // tensor dimension in (d0, ..., dn).
173  // It's stored in the order of (dn, ..., dn+k-1).
174  // If not block-sparse, this field is NULL.
175  block_map:[int];
176  // In the traversal order defined above, the metadata needed for
177  // each dimension to locate the non-zero values in the original dense tensor.
178  // The size of the dim_metadata array = the size of the traversal_order array
179  // = n + k.
180  dim_metadata:[DimensionMetadata];
181}
182
183table Tensor {
184  // The tensor shape. The meaning of each entry is operator-specific but
185  // builtin ops use: [batch size, height, width, number of channels] (That's
186  // Tensorflow's NHWC).
187  shape:[int];
188  type:TensorType;
189  // An index that refers to the buffers table at the root of the model. Or,
190  // if there is no data buffer associated (i.e. intermediate results), then
191  // this is 0 (which refers to an always existent empty buffer).
192  //
193  // The data_buffer itself is an opaque container, with the assumption that the
194  // target device is little-endian. In addition, all builtin operators assume
195  // the memory is ordered such that if `shape` is [4, 3, 2], then index
196  // [i, j, k] maps to data_buffer[i*3*2 + j*2 + k].
197  buffer:uint;
198  name:string;  // For debugging and importing back into tensorflow.
199  quantization:QuantizationParameters;  // Optional.
200
201  is_variable:bool = false;
202
203  // Parameters to encode a sparse tensor. See the example in
204  // tensorflow/lite/testdata/sparse_tensor.json.
205  sparsity:SparsityParameters;  // Optional.
206
207  // Encodes `shape` with unknown dimensions. Unknown dimensions are
208  // represented with -1.
209  shape_signature:[int]; // Optional.
210}
211
212// A list of builtin operators. Builtin operators are slightly faster than custom
213// ones, but not by much. Moreover, while custom operators accept an opaque
214// object containing configuration parameters, builtins have a predetermined
215// set of acceptable options.
216
217enum BuiltinOperator : byte {
218  ADD = 0,
219  AVERAGE_POOL_2D = 1,
220  CONCATENATION = 2,
221  CONV_2D = 3,
222  DEPTHWISE_CONV_2D = 4,
223  DEPTH_TO_SPACE = 5,
224  DEQUANTIZE = 6,
225  EMBEDDING_LOOKUP = 7,
226  FLOOR = 8,
227  FULLY_CONNECTED = 9,
228  HASHTABLE_LOOKUP = 10,
229  L2_NORMALIZATION = 11,
230  L2_POOL_2D = 12,
231  LOCAL_RESPONSE_NORMALIZATION = 13,
232  LOGISTIC = 14,
233  LSH_PROJECTION = 15,
234  LSTM = 16,
235  MAX_POOL_2D = 17,
236  MUL = 18,
237  RELU = 19,
238  // NOTE(aselle): RELU_N1_TO_1 used to be called RELU1, but it was renamed
239  // since different model developers use RELU1 in different ways. Never
240  // create another op called RELU1.
241  RELU_N1_TO_1 = 20,
242  RELU6 = 21,
243  RESHAPE = 22,
244  RESIZE_BILINEAR = 23,
245  RNN = 24,
246  SOFTMAX = 25,
247  SPACE_TO_DEPTH = 26,
248  SVDF = 27,
249  TANH = 28,
250  // Consider rename to CONCATENATE_EMBEDDINGS
251  CONCAT_EMBEDDINGS = 29,
252  SKIP_GRAM = 30,
253  CALL = 31,
254  CUSTOM = 32,
255  EMBEDDING_LOOKUP_SPARSE = 33,
256  PAD = 34,
257  UNIDIRECTIONAL_SEQUENCE_RNN = 35,
258  GATHER = 36,
259  BATCH_TO_SPACE_ND = 37,
260  SPACE_TO_BATCH_ND = 38,
261  TRANSPOSE = 39,
262  MEAN = 40,
263  SUB = 41,
264  DIV = 42,
265  SQUEEZE = 43,
266  UNIDIRECTIONAL_SEQUENCE_LSTM = 44,
267  STRIDED_SLICE = 45,
268  BIDIRECTIONAL_SEQUENCE_RNN = 46,
269  EXP = 47,
270  TOPK_V2 = 48,
271  SPLIT = 49,
272  LOG_SOFTMAX = 50,
273  // DELEGATE is a special op type for the operations which are delegated to
274  // other backends.
275  // WARNING: Experimental interface, subject to change
276  DELEGATE = 51,
277  BIDIRECTIONAL_SEQUENCE_LSTM = 52,
278  CAST = 53,
279  PRELU = 54,
280  MAXIMUM = 55,
281  ARG_MAX = 56,
282  MINIMUM = 57,
283  LESS = 58,
284  NEG = 59,
285  PADV2 = 60,
286  GREATER = 61,
287  GREATER_EQUAL = 62,
288  LESS_EQUAL = 63,
289  SELECT = 64,
290  SLICE = 65,
291  SIN = 66,
292  TRANSPOSE_CONV = 67,
293  SPARSE_TO_DENSE = 68,
294  TILE = 69,
295  EXPAND_DIMS = 70,
296  EQUAL = 71,
297  NOT_EQUAL = 72,
298  LOG = 73,
299  SUM = 74,
300  SQRT = 75,
301  RSQRT = 76,
302  SHAPE = 77,
303  POW = 78,
304  ARG_MIN = 79,
305  FAKE_QUANT = 80,
306  REDUCE_PROD = 81,
307  REDUCE_MAX = 82,
308  PACK = 83,
309  LOGICAL_OR = 84,
310  ONE_HOT = 85,
311  LOGICAL_AND = 86,
312  LOGICAL_NOT = 87,
313  UNPACK = 88,
314  REDUCE_MIN = 89,
315  FLOOR_DIV = 90,
316  REDUCE_ANY = 91,
317  SQUARE = 92,
318  ZEROS_LIKE = 93,
319  FILL = 94,
320  FLOOR_MOD = 95,
321  RANGE = 96,
322  RESIZE_NEAREST_NEIGHBOR = 97,
323  LEAKY_RELU = 98,
324  SQUARED_DIFFERENCE = 99,
325  MIRROR_PAD = 100,
326  ABS = 101,
327  SPLIT_V = 102,
328  UNIQUE = 103,
329  CEIL = 104,
330  REVERSE_V2 = 105,
331  ADD_N = 106,
332  GATHER_ND = 107,
333  COS = 108,
334  WHERE = 109,
335  RANK = 110,
336  ELU = 111,
337  REVERSE_SEQUENCE = 112,
338  MATRIX_DIAG = 113,
339  QUANTIZE = 114,
340  MATRIX_SET_DIAG = 115,
341  ROUND = 116,
342  HARD_SWISH = 117,
343  IF = 118,
344  WHILE = 119,
345  NON_MAX_SUPPRESSION_V4 = 120,
346  NON_MAX_SUPPRESSION_V5 = 121,
347  SCATTER_ND = 122,
348  SELECT_V2 = 123,
349  DENSIFY = 124,
350  SEGMENT_SUM = 125,
351  BATCH_MATMUL = 126
352}
353
354
355// Options for the builtin operators.
356union BuiltinOptions {
357  Conv2DOptions,
358  DepthwiseConv2DOptions,
359  ConcatEmbeddingsOptions,
360  LSHProjectionOptions,
361  Pool2DOptions,
362  SVDFOptions,
363  RNNOptions,
364  FullyConnectedOptions,
365  SoftmaxOptions,
366  ConcatenationOptions,
367  AddOptions,
368  L2NormOptions,
369  LocalResponseNormalizationOptions,
370  LSTMOptions,
371  ResizeBilinearOptions,
372  CallOptions,
373  ReshapeOptions,
374  SkipGramOptions,
375  SpaceToDepthOptions,
376  EmbeddingLookupSparseOptions,
377  MulOptions,
378  PadOptions,
379  GatherOptions,
380  BatchToSpaceNDOptions,
381  SpaceToBatchNDOptions,
382  TransposeOptions,
383  ReducerOptions,
384  SubOptions,
385  DivOptions,
386  SqueezeOptions,
387  SequenceRNNOptions,
388  StridedSliceOptions,
389  ExpOptions,
390  TopKV2Options,
391  SplitOptions,
392  LogSoftmaxOptions,
393  CastOptions,
394  DequantizeOptions,
395  MaximumMinimumOptions,
396  ArgMaxOptions,
397  LessOptions,
398  NegOptions,
399  PadV2Options,
400  GreaterOptions,
401  GreaterEqualOptions,
402  LessEqualOptions,
403  SelectOptions,
404  SliceOptions,
405  TransposeConvOptions,
406  SparseToDenseOptions,
407  TileOptions,
408  ExpandDimsOptions,
409  EqualOptions,
410  NotEqualOptions,
411  ShapeOptions,
412  PowOptions,
413  ArgMinOptions,
414  FakeQuantOptions,
415  PackOptions,
416  LogicalOrOptions,
417  OneHotOptions,
418  LogicalAndOptions,
419  LogicalNotOptions,
420  UnpackOptions,
421  FloorDivOptions,
422  SquareOptions,
423  ZerosLikeOptions,
424  FillOptions,
425  BidirectionalSequenceLSTMOptions,
426  BidirectionalSequenceRNNOptions,
427  UnidirectionalSequenceLSTMOptions,
428  FloorModOptions,
429  RangeOptions,
430  ResizeNearestNeighborOptions,
431  LeakyReluOptions,
432  SquaredDifferenceOptions,
433  MirrorPadOptions,
434  AbsOptions,
435  SplitVOptions,
436  UniqueOptions,
437  ReverseV2Options,
438  AddNOptions,
439  GatherNdOptions,
440  CosOptions,
441  WhereOptions,
442  RankOptions,
443  ReverseSequenceOptions,
444  MatrixDiagOptions,
445  QuantizeOptions,
446  MatrixSetDiagOptions,
447  HardSwishOptions,
448  IfOptions,
449  WhileOptions,
450  DepthToSpaceOptions,
451  NonMaxSuppressionV4Options,
452  NonMaxSuppressionV5Options,
453  ScatterNdOptions,
454  SelectV2Options,
455  DensifyOptions,
456  SegmentSumOptions,
457  BatchMatMulOptions
458}
459
460enum Padding : byte { SAME, VALID }
461
462enum ActivationFunctionType : byte {
463  NONE = 0,
464  RELU = 1,
465  RELU_N1_TO_1 = 2,
466  RELU6 = 3,
467  TANH = 4,
468  SIGN_BIT = 5,
469}
470
471table Conv2DOptions {
472  padding:Padding;
473  stride_w:int;
474  stride_h:int;
475  fused_activation_function:ActivationFunctionType;
476  dilation_w_factor:int = 1;
477  dilation_h_factor:int = 1;
478}
479
480table Pool2DOptions {
481  padding:Padding;
482  stride_w:int;
483  stride_h:int;
484  filter_width:int;
485  filter_height:int;
486  fused_activation_function:ActivationFunctionType;
487}
488
489table DepthwiseConv2DOptions {
490  // Parameters for DepthwiseConv version 1 or above.
491  padding:Padding;
492  stride_w:int;
493  stride_h:int;
494  // `depth_multiplier` is redundant. It's used by CPU kernels in
495  // TensorFlow 2.0 or below, but ignored in versions above.
496  // See comments in lite/c/builtin_op_data.h for more details.
497  depth_multiplier:int;
498  fused_activation_function:ActivationFunctionType;
499  // Parameters for DepthwiseConv version 2 or above.
500  dilation_w_factor:int = 1;
501  dilation_h_factor:int = 1;
502}
503
504table ConcatEmbeddingsOptions {
505  num_channels:int;
506  num_columns_per_channel:[int];
507  embedding_dim_per_channel:[int]; // This could be inferred from parameters.
508}
509
510enum LSHProjectionType: byte {
511  UNKNOWN = 0,
512  SPARSE = 1,
513  DENSE = 2,
514}
515
516table LSHProjectionOptions {
517  type: LSHProjectionType;
518}
519
520table SVDFOptions {
521  rank:int;
522  fused_activation_function:ActivationFunctionType;
523  // For weights-only quantization, use asymmetric quantization for non
524  // constant inputs at evaluation time.
525  asymmetric_quantize_inputs:bool;
526}
527
528// An implementation of TensorFlow RNNCell.
529table RNNOptions {
530  fused_activation_function:ActivationFunctionType;
531  asymmetric_quantize_inputs:bool;
532}
533
534// An implementation of TensorFlow dynamic_rnn with RNNCell.
535table SequenceRNNOptions {
536  time_major:bool;
537  fused_activation_function:ActivationFunctionType;
538  asymmetric_quantize_inputs:bool;
539}
540
541// An implementation of TensorFlow bidrectional_dynamic_rnn with RNNCell.
542table BidirectionalSequenceRNNOptions {
543  time_major:bool;
544  fused_activation_function:ActivationFunctionType;
545  merge_outputs: bool;
546  asymmetric_quantize_inputs:bool;
547}
548
549enum FullyConnectedOptionsWeightsFormat: byte {
550  DEFAULT = 0,
551  SHUFFLED4x16INT8 = 1,
552}
553
554// An implementation of TensorFlow fully_connected (a.k.a Dense) layer.
555table FullyConnectedOptions {
556  // Parameters for FullyConnected version 1 or above.
557  fused_activation_function:ActivationFunctionType;
558
559  // Parameters for FullyConnected version 2 or above.
560  weights_format:FullyConnectedOptionsWeightsFormat = DEFAULT;
561
562  // Parameters for FullyConnected version 5 or above.
563  // If set to true, then the number of dimension is preserved. Furthermore,
564  // all but the last dimension of the input and output shapes will be equal.
565  keep_num_dims: bool;
566
567  // Parameters for FullyConnected version 7 or above.
568  // If set to true, then weights-only op will use asymmetric quantization for
569  // inputs.
570  asymmetric_quantize_inputs: bool;
571}
572
573table SoftmaxOptions {
574  beta: float;
575}
576
577// An implementation of TensorFlow concat.
578table ConcatenationOptions {
579  axis:int;
580  fused_activation_function:ActivationFunctionType;
581}
582
583table AddOptions {
584  fused_activation_function:ActivationFunctionType;
585}
586
587table MulOptions {
588  fused_activation_function:ActivationFunctionType;
589}
590
591table L2NormOptions {
592  fused_activation_function:ActivationFunctionType;
593}
594
595table LocalResponseNormalizationOptions {
596  radius:int;
597  bias:float;
598  alpha:float;
599  beta:float;
600}
601
602enum LSTMKernelType : byte {
603  // Full LSTM kernel which supports peephole and projection.
604  FULL = 0,
605  // Basic LSTM kernels. Equivalent to TensorFlow BasicLSTMCell.
606  BASIC = 1,
607}
608
609// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell
610table LSTMOptions {
611  // Parameters for LSTM version 1 or above.
612  fused_activation_function:ActivationFunctionType;
613  cell_clip: float; // Optional, 0.0 means no clipping
614  proj_clip: float; // Optional, 0.0 means no clipping
615
616  // Parameters for LSTM version 2 or above.
617  // Basic kernel is only supported in version 2 or above.
618  kernel_type: LSTMKernelType = FULL;
619
620  // Parameters for LSTM version 4 or above.
621  asymmetric_quantize_inputs: bool;
622}
623
624// An implementation of TensorFlow dynamic_rnn with LSTMCell.
625table UnidirectionalSequenceLSTMOptions {
626  fused_activation_function:ActivationFunctionType;
627  cell_clip: float; // Optional, 0.0 means no clipping
628  proj_clip: float; // Optional, 0.0 means no clipping
629
630  // If true then first dimension is sequence, otherwise batch.
631  time_major:bool;
632
633  // Parameter for Unidirectional Sequence LSTM version 4.
634  asymmetric_quantize_inputs:bool;
635}
636
637table BidirectionalSequenceLSTMOptions {
638  // Parameters supported by version 1:
639  fused_activation_function:ActivationFunctionType;
640  cell_clip: float; // Optional, 0.0 means no clipping
641  proj_clip: float; // Optional, 0.0 means no clipping
642
643  // If true, store the outputs of both directions into the first output.
644  merge_outputs: bool;
645
646  // Parameters supported by version 2:
647  // If true then first dimension is sequence, otherwise batch.
648  // Version 1 implementations assumed time_major to be true, so this default
649  // value should never change.
650  time_major: bool = true;
651
652  // Parameters for version 3 or above.
653  asymmetric_quantize_inputs:bool;
654}
655
656table ResizeBilinearOptions {
657  new_height: int (deprecated);
658  new_width: int (deprecated);
659  align_corners: bool;
660  half_pixel_centers: bool;
661}
662
663table ResizeNearestNeighborOptions {
664  align_corners: bool;
665  half_pixel_centers: bool;
666}
667
668// A call operation options
669table CallOptions {
670  // The subgraph index that needs to be called.
671  subgraph:uint;
672}
673
674table PadOptions {
675}
676
677table PadV2Options {
678}
679
680table ReshapeOptions {
681  new_shape:[int];
682}
683
684table SpaceToBatchNDOptions {
685}
686
687table BatchToSpaceNDOptions {
688}
689
690table SkipGramOptions {
691  ngram_size: int;
692  max_skip_size: int;
693  include_all_ngrams: bool;
694}
695
696table SpaceToDepthOptions {
697  block_size: int;
698}
699
700table DepthToSpaceOptions {
701  block_size: int;
702}
703
704table SubOptions {
705  fused_activation_function:ActivationFunctionType;
706}
707
708table DivOptions {
709  fused_activation_function:ActivationFunctionType;
710}
711
712table TopKV2Options {
713}
714
715enum CombinerType : byte {
716  SUM = 0,
717  MEAN = 1,
718  SQRTN = 2,
719}
720
721table EmbeddingLookupSparseOptions {
722  combiner:CombinerType;
723}
724
725table GatherOptions {
726  axis: int;
727}
728
729table TransposeOptions {
730}
731
732table ExpOptions {
733}
734
735table CosOptions {
736}
737
738table ReducerOptions {
739  keep_dims: bool;
740}
741
742table SqueezeOptions {
743  squeeze_dims:[int];
744}
745
746table SplitOptions {
747  num_splits: int;
748}
749
750table SplitVOptions {
751  num_splits: int;
752}
753
754table StridedSliceOptions {
755  begin_mask: int;
756  end_mask: int;
757  ellipsis_mask: int;
758  new_axis_mask: int;
759  shrink_axis_mask: int;
760}
761
762table LogSoftmaxOptions {
763}
764
765table CastOptions {
766  in_data_type: TensorType;
767  out_data_type: TensorType;
768}
769
770table DequantizeOptions {
771}
772
773table MaximumMinimumOptions {
774}
775
776table TileOptions {
777}
778
779table ArgMaxOptions {
780  output_type : TensorType;
781}
782
783table ArgMinOptions {
784  output_type : TensorType;
785}
786
787table GreaterOptions {
788}
789
790table GreaterEqualOptions {
791}
792
793table LessOptions {
794}
795
796table LessEqualOptions {
797}
798
799table NegOptions {
800}
801
802table SelectOptions {
803}
804
805table SliceOptions {
806}
807
808table TransposeConvOptions {
809  padding:Padding;
810  stride_w:int;
811  stride_h:int;
812}
813
814table ExpandDimsOptions {
815}
816
817table SparseToDenseOptions {
818  validate_indices:bool;
819}
820
821table EqualOptions {
822}
823
824table NotEqualOptions {
825}
826
827table ShapeOptions {
828  // Optional output type of the operation (int32 or int64). Defaults to int32.
829  out_type : TensorType;
830}
831
832table RankOptions {
833}
834
835table PowOptions {
836}
837
838table FakeQuantOptions {
839  // Parameters supported by version 1:
840  min:float;
841  max:float;
842  num_bits:int;
843
844  // Parameters supported by version 2:
845  narrow_range:bool;
846}
847
848table PackOptions {
849  values_count:int;
850  axis:int;
851}
852
853table LogicalOrOptions {
854}
855
856table OneHotOptions {
857  axis:int;
858}
859
860table AbsOptions {
861}
862
863
864table HardSwishOptions {
865}
866
867table LogicalAndOptions {
868}
869
870table LogicalNotOptions {
871}
872
873table UnpackOptions {
874  num:int;
875  axis:int;
876}
877
878table FloorDivOptions {
879}
880
881table SquareOptions {
882}
883
884table ZerosLikeOptions {
885}
886
887table FillOptions {
888}
889
890table FloorModOptions {
891}
892
893table RangeOptions {
894}
895
896table LeakyReluOptions {
897  alpha:float;
898}
899
900table SquaredDifferenceOptions {
901}
902
903enum MirrorPadMode : byte {
904  // Doesn't include borders.
905  REFLECT = 0,
906  // Includes borders.
907  SYMMETRIC = 1,
908}
909
910table MirrorPadOptions {
911  mode:MirrorPadMode;
912}
913
914table UniqueOptions {
915  idx_out_type:TensorType = INT32;
916}
917
918table ReverseV2Options {
919}
920
921table AddNOptions {
922}
923
924table GatherNdOptions {
925}
926
927table WhereOptions {
928}
929
930table ReverseSequenceOptions {
931  seq_dim:int;
932  batch_dim:int = 0;
933}
934
935table MatrixDiagOptions {
936}
937
938table QuantizeOptions {
939}
940
941table MatrixSetDiagOptions {
942}
943
944table IfOptions {
945  then_subgraph_index:int;
946  else_subgraph_index:int;
947}
948
949table WhileOptions {
950  cond_subgraph_index:int;
951  body_subgraph_index:int;
952}
953
954table NonMaxSuppressionV4Options {
955}
956
957table NonMaxSuppressionV5Options {
958}
959
960table ScatterNdOptions {
961}
962
963table SelectV2Options {
964}
965
966table DensifyOptions {
967}
968
969table SegmentSumOptions {
970}
971
972table BatchMatMulOptions {
973  adj_x:bool;
974  adj_y:bool;
975}
976
977// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a
978// builtin, or a string if the operator is custom.
979table OperatorCode {
980  builtin_code:BuiltinOperator;
981  custom_code:string;
982
983  // The version of the operator. The version need to be bumped whenever new
984  // parameters are introduced into an op.
985  version:int = 1;
986}
987
988enum CustomOptionsFormat : byte {
989  FLEXBUFFERS = 0,
990}
991
992// An operator takes tensors as inputs and outputs. The type of operation being
993// performed is determined by an index into the list of valid OperatorCodes,
994// while the specifics of each operations is configured using builtin_options
995// or custom_options.
996table Operator {
997  // Index into the operator_codes array. Using an integer here avoids
998  // complicate map lookups.
999  opcode_index:uint;
1000
1001  // Optional input are indicated by -1.
1002  inputs:[int];
1003  outputs:[int];
1004
1005  builtin_options:BuiltinOptions;
1006  custom_options:[ubyte];
1007  custom_options_format:CustomOptionsFormat;
1008
1009  // A list of booleans indicating the input tensors which are being mutated by
1010  // this operator.(e.g. used by RNN and LSTM).
1011  // For example, if the "inputs" array refers to 5 tensors and the second and
1012  // fifth are mutable variables, then this list will contain
1013  // [false, true, false, false, true].
1014  //
1015  // If the list is empty, no variable is mutated in this operator.
1016  // The list either has the same length as `inputs`, or is empty.
1017  mutating_variable_inputs:[bool];
1018
1019  // A list of indices to the subgraph's "tensors" that are internal to an Op.
1020  // Internal tensors are those that do not flow in or out of the operation,
1021  // but instead are part of internal computation. As such, the operation's
1022  // implementation may manage its memory more efficiently. They are needed
1023  // however (i.e. not just an implementation detail) since they are part of the
1024  // computation, which may require relevant metadata such as quantization
1025  // parameters.
1026  intermediates:[int];
1027}
1028
1029// The root type, defining a subgraph, which typically represents an entire
1030// model.
1031table SubGraph {
1032  // A list of all tensors used in this subgraph.
1033  tensors:[Tensor];
1034
1035  // Indices of the tensors that are inputs into this subgraph. Note this is
1036  // the list of non-static tensors that feed into the subgraph for inference.
1037  inputs:[int];
1038
1039  // Indices of the tensors that are outputs out of this subgraph. Note this is
1040  // the list of output tensors that are considered the product of the
1041  // subgraph's inference.
1042  outputs:[int];
1043
1044  // All operators, in execution order.
1045  operators:[Operator];
1046
1047  // Name of this subgraph (used for debugging).
1048  name:string;
1049}
1050
1051// Table of raw data buffers (used for constant tensors). Referenced by tensors
1052// by index. The generous alignment accommodates mmap-friendly data structures.
1053table Buffer {
1054  data:[ubyte] (force_align: 16);
1055}
1056
1057table Metadata {
1058  // A human readable string to uniquely identify a Metadata.
1059  name:string;
1060  // An index to the buffers table.
1061  buffer:uint;
1062}
1063
1064table Model {
1065  // Version of the schema.
1066  version:uint;
1067
1068  // A list of all operator codes used in this model. This is
1069  // kept in order because operators carry an index into this
1070  // vector.
1071  operator_codes:[OperatorCode];
1072
1073  // All the subgraphs of the model. The 0th is assumed to be the main
1074  // model.
1075  subgraphs:[SubGraph];
1076
1077  // A description of the model.
1078  description:string;
1079
1080  // Buffers of the model.
1081  // Note the 0th entry of this array must be an empty buffer (sentinel).
1082  // This is a convention so that tensors without a buffer can provide 0 as
1083  // their buffer.
1084  buffers:[Buffer];
1085
1086  // Metadata about the model. Indirects into the existings buffers list.
1087  // Deprecated, prefer to use metadata field.
1088  metadata_buffer:[int];
1089
1090  // Metadata about the model.
1091  metadata:[Metadata];
1092}
1093
1094root_type Model;
1095