ONNX 文本语法

概述

本文档描述了 ONNX 模型的文本语法,目前它是一个实验性功能。该语法能够以紧凑且易读的方式表示 ONNX 模型。它源于几个用例。一个是能够以紧凑的方式描述测试用例,并在 CI 中使用它(在 ONNX 仓库中以及其他相关仓库中,如 ONNX-MLIR)。第二个是帮助简化 ONNX 函数的定义。现有的许多函数定义都很冗长,使用此语法将使函数定义更加紧凑、易读和易于维护。高效表示和高效解析非常大的张量常量**不是**目标。应使用替代方法来实现这一点。

API

关键解析器方法是 OnnxParser::Parse 方法,如下所示使用。

  const char* code = R"ONNX(
<
  ir_version: 7,
  opset_import: [ "" : 10 ]
>
agraph (float[N, 128] X, float[128, 10] W, float[10] B) => (float[N, 10] C)
{
    T = MatMul(X, W)
    S = Add(T, B)
    C = Softmax(S)
}
)ONNX";

  ModelProto model;
  OnnxParser::Parse(model, code);

  checker::check_model(model);

请参阅测试用例以获取更多示例,这些示例说明了 API 和语法。

语法

下面的语法描述了语法

   id-list ::= id (',' id)*
   quotable-id-list ::= quotable-id (',' quotable-id)*
   tensor-dim ::= '?' | id | int-constant
   tensor-dims ::= tensor-dim (',' tensor-dim)*
   tensor-type ::= prim-type | prim-type '[' ']' | prim-type '[' tensor-dims ']'
   type ::= tensor-type | 'seq' '(' type ')' | 'map' '(' prim-type ',' type ')'
            | 'optional' '(' type ')' | 'sparse_tensor' '(' tensor-type ')'
   value-info ::= type quotable-id
   value-infos ::= value-info (',' value-info)*
   value-info-list ::= '(' value-infos? ')
   id-or-value-info ::= type? quotable-id
   id-or-value-infos ::= id-or-value-info (',' id-or-value-info)*
   quoted-str :== '"' ([^"])* '"'
   quotable-id :== id | quoted-str
   str-str :== quoted-str ':' quoted-str
   str-str-list :== '[' str-str (',' str-str)* ']'
   internal-data ::= '{' prim-constants '}'
   external-data ::= str-str-list
   constant-data ::= internal-data | external-data
   value-info-or-initializer ::= type quotable-id [ '=' constant-data ]
   value-info-or-initializers ::= value-info-or-initializer (',' value-info-or-initializer)*
   input-list ::= '(' value-info-or-initializers? ')'
   output-list ::= '(' value-infos? ')'
   initializer-list ::= '<' value-info-or-initializers? '>'
   prim-constants ::= prim-constant (',' prim-constant)*
   tensor-constant ::= tensor-type (quotable-id)? ('=')? '{' prim-constants '}'
   attr-ref ::= '@' id
   single-attr-value ::= tensor-constant | graph | prim-constant | attr-ref
   attr-value-list ::= '[' single-attr-value (',' single-attr-value)* ']'
   attr-value ::= single-attr-value | attr-value-list
   attr-type ::= ':' id
   attr ::= id attr-type? '=' attr-value
   attr-list ::= '<' attr (',' attr)* '>'
   node-label ::= '[' quotable-id ']'
   node ::= node-label? quotable-id-list? '=' qualified-id attr-list? '(' quotable-id-list? ')'
         |  node-label? quotable-id-list? '=' qualified-id '(' quotable-id-list? ')' attr-list
   node-list ::= '{' node* '}'
   graph ::= quotable-id input-list '=>' output-list initializer-list node-list
   other-data ::= id ':' value
   other-data-list ::= '<' other-data (',' other-data)* '>'
   fun-attr-list ::= '<' id | attr (',' id | attr)* '>'
   fun-input-list ::= '(' id-or-value-infos ')'
   fun-output-list ::= '(' id-or-value-infos ')'
   fun-value-infos ::= ( '<' value-infos '>' )?
   function ::= other-data-list? id fun-attr-list? quotable-id fun-input-list '=>' fun-output-list fun-value-infos node-list
   model ::= other-data-list? graph function*