【ONNX】onnx.in.proto说明

onnx.in.protoONNX 官方 提供的原始文件,用于生成 onnx-ml.protoonnx-operators-ml.proto 文件。它是 ONNX 项目的一部分,包含了 ONNX 模型格式的核心定义。如果需要扩展 ONNX 模型格式(例如添加新的操作符或修改现有定义),只需修改 onnx.in.proto,然后重新生成相关文件。


1. onnx.in.proto 的作用

  • onnx.in.proto 是 ONNX 模型定义的 源文件,包含了 ONNX 模型的核心数据结构和操作符的定义。
  • 它使用 Protocol Buffers(protobuf) 语法编写,定义了神经网络模型的结构(如节点、张量、图等)和操作符(如卷积、池化、激活函数等)。
  • 通过工具链(如脚本或构建系统),onnx.in.proto 会被处理并生成 onnx-ml.protoonnx-operators-ml.proto 文件。

2. 为什么需要 onnx.in.proto

  • 模块化和可维护性
    • onnx.in.proto 是 ONNX 模型定义的单一来源(Single Source of Truth),所有与模型格式相关的定义都集中在这个文件中。
    • 通过工具链生成 onnx-ml.protoonnx-operators-ml.proto,可以确保这些文件的一致性,并减少手动维护的工作量。
  • 扩展性
    • 如果需要扩展 ONNX 模型格式(例如添加新的操作符或修改现有定义),只需修改 onnx.in.proto,然后重新生成相关文件。
  • 自动化生成
    • 使用工具链生成文件可以避免手动编写和修改 onnx-ml.protoonnx-operators-ml.proto,减少错误。

3. onnx.in.proto 在哪里?

  • onnx.in.proto 是 ONNX 官方仓库的一部分,位于 ONNX 项目的源代码中。
  • 你可以在 ONNX 的 GitHub 仓库中找到它:
    • 仓库地址:https://github.com/onnx/onnx
    • 文件路径:onnx/onnx.in.proto

4. onnx.in.proto 的生成流程

  • ONNX 项目使用 Python 脚本构建工具 来处理 onnx.in.proto,并生成 onnx-ml.protoonnx-operators-ml.proto
  • 生成流程大致如下:
    1. 读取 onnx.in.proto 文件。
    2. 根据文件内容生成 onnx-ml.protoonnx-operators-ml.proto
    3. 将生成的文件输出到指定目录。
  • 这个过程通常由 ONNX 的构建系统(如 CMake 或 Makefile)自动完成。

5. 为什么 onnx-ml.protoonnx-operators-ml.proto 中有警告?

  • onnx-ml.protoonnx-operators-ml.proto 文件中,通常会包含以下注释:
    WARNING: This file is automatically generated! Please edit onnx.in.proto.
    
  • 这个警告的目的是提醒开发者:
    • 不要直接修改 onnx-ml.protoonnx-operators-ml.proto,因为它们是自动生成的。
    • 如果需要修改 ONNX 模型的定义,应该修改 onnx.in.proto,然后重新生成这些文件。

6. 如何获取 onnx.in.proto

  • 如果你需要获取 onnx.in.proto,可以通过以下方式:
    1. 从 ONNX 官方仓库下载
      • 访问 ONNX 的 GitHub 仓库:https://github.com/onnx/onnx
      • 找到 onnx.in.proto 文件并下载。
    2. 克隆 ONNX 仓库
      git clone https://github.com/onnx/onnx.git
      
      克隆后,onnx.in.proto 文件位于 onnx/ 目录下。

7. onnx.in.proto 的内容示例

onnx.in.proto 文件的内容通常包括以下部分:

  • 语法声明
    syntax = "proto3";
    
  • 包声明
    package onnx;
    
  • 消息定义
    message TensorProto {
      // 张量的数据类型
      enum DataType {
        UNDEFINED = 0;
        FLOAT = 1;
        INT32 = 2;
        // 其他数据类型
      }
      DataType data_type = 1;
      // 张量的形状
      repeated int64 dims = 2;
      // 张量的数据
      bytes raw_data = 3;
    }
    
  • 操作符定义
    message NodeProto {
      // 操作符的名称
      string op_type = 1;
      // 输入和输出
      repeated string input = 2;
      repeated string output = 3;
      // 属性
      repeated AttributeProto attribute = 4;
    }
    

总结

onnx.in.proto 是 ONNX 官方提供的原始文件,用于定义 ONNX 模型的核心数据结构和操作符。它是 onnx-ml.protoonnx-operators-ml.proto 的源文件,通过工具链自动生成。开发者不应直接修改生成的 .proto 文件,而是应该修改 onnx.in.proto 并重新生成。你可以在 ONNX 的 GitHub 仓库中找到 onnx.in.proto 文件。

/home/re/dev/sox/sox.py:182: DeprecationWarning: You are using the legacy TorchScript-based ONNX export. Starting in PyTorch 2.9, the new torch.export-based ONNX exporter will be the default. To switch now, set dynamo=True in torch.onnx.export. This new exporter supports features like exporting LLMs with DynamicCache. We encourage you to try it and share feedback to help improve the experience. Learn more about the new export logic: https://pytorch.org/docs/stable/onnx_dynamo.html. For exporting control flow: https://pytorch.org/tutorials/beginner/onnx/export_control_flow_model_to_onnx_tutorial.html. torch.onnx.export(model, input_sample, "soc_model_weights.onnx", input_names=["input"], output_names=["output"], verbose=True) Traceback (most recent call last): File "/home/re/.local/lib/python3.10/site-packages/torch/onnx/_internal/onnx_proto_utils.py", line 182, in _add_onnxscript_fn import onnx ModuleNotFoundError: No module named 'onnx' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/re/dev/sox/sox.py", line 463, in <module> main() File "/home/re/dev/sox/sox.py", line 428, in main model, scaler, X_train, X_test, y_train, y_test, train_losses, test_losses = train_soc_model(X, y) File "/home/re/dev/sox/sox.py", line 182, in train_soc_model torch.onnx.export(model, input_sample, "soc_model_weights.onnx", input_names=["input"], output_names=["output"], verbose=True) File "/home/re/.local/lib/python3.10/site-packages/torch/onnx/__init__.py", line 424, in export export( File "/home/re/.local/lib/python3.10/site-packages/torch/onnx/utils.py", line 522, in export _export( File "/home/re/.local/lib/python3.10/site-packages/torch/onnx/utils.py", line 1530, in _export proto = onnx_proto_utils._add_onnxscript_fn( File "/home/re/.local/lib/python3.10/site-packages/torch/onnx/_internal/onnx_proto_utils.py", line 184, in _add_onnxscript_fn raise errors.OnnxExporterError("Module onnx is not installed!") from e torch.onnx.OnnxExporterError: Module onnx is not installed!
09-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值