{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-03-06T20:16:09.220355Z",
     "iopub.status.busy": "2023-03-06T20:16:09.219355Z",
     "iopub.status.idle": "2023-03-06T20:16:09.481504Z",
     "shell.execute_reply": "2023-03-06T20:16:09.480694Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The producer_name in model: onnx-example\n",
      "\n",
      "The graph in model:\n",
      "node {\n",
      "  input: \"X\"\n",
      "  input: \"Pads\"\n",
      "  output: \"Y\"\n",
      "  op_type: \"Pad\"\n",
      "  attribute {\n",
      "    name: \"mode\"\n",
      "    s: \"constant\"\n",
      "    type: STRING\n",
      "  }\n",
      "}\n",
      "name: \"test-model\"\n",
      "initializer {\n",
      "  dims: 4\n",
      "  data_type: 7\n",
      "  int64_data: 0\n",
      "  int64_data: 0\n",
      "  int64_data: 1\n",
      "  int64_data: 1\n",
      "  name: \"Pads\"\n",
      "}\n",
      "input {\n",
      "  name: \"X\"\n",
      "  type {\n",
      "    tensor_type {\n",
      "      elem_type: 1\n",
      "      shape {\n",
      "        dim {\n",
      "          dim_value: 1\n",
      "        }\n",
      "        dim {\n",
      "          dim_value: 2\n",
      "        }\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}\n",
      "input {\n",
      "  name: \"Pads\"\n",
      "  type {\n",
      "    tensor_type {\n",
      "      elem_type: 7\n",
      "      shape {\n",
      "        dim {\n",
      "          dim_value: 4\n",
      "        }\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}\n",
      "output {\n",
      "  name: \"Y\"\n",
      "  type {\n",
      "    tensor_type {\n",
      "      elem_type: 1\n",
      "      shape {\n",
      "        dim {\n",
      "          dim_value: 1\n",
      "        }\n",
      "        dim {\n",
      "          dim_value: 4\n",
      "        }\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}\n",
      "\n",
      "The model is checked!\n"
     ]
    }
   ],
   "source": [
    "# NBVAL_SKIP\n",
    "# Protobuf 4 and Protobuf 3 might output different order of protobuf fields\n",
    "\n",
    "import onnx\n",
    "from onnx import helper\n",
    "from onnx import AttributeProto, TensorProto, GraphProto\n",
    "\n",
    "\n",
    "# The protobuf definition can be found here:\n",
    "# https://github.com/onnx/onnx/blob/main/onnx/onnx.proto\n",
    "\n",
    "\n",
    "# Create one input (ValueInfoProto)\n",
    "X = helper.make_tensor_value_info(\"X\", TensorProto.FLOAT, [1, 2])\n",
    "\n",
    "# Create second input (ValueInfoProto)\n",
    "Pads = helper.make_tensor_value_info(\"Pads\", TensorProto.INT64, [4])\n",
    "\n",
    "# Create one output (ValueInfoProto)\n",
    "Y = helper.make_tensor_value_info(\"Y\", TensorProto.FLOAT, [1, 4])\n",
    "\n",
    "# Create a node (NodeProto)\n",
    "node_def = helper.make_node(\n",
    "    \"Pad\", # node name\n",
    "    [\"X\", \"Pads\"], # inputs\n",
    "    [\"Y\"], # outputs\n",
    "    mode=\"constant\", # Attributes\n",
    ")\n",
    "\n",
    "# Create the graph (GraphProto)\n",
    "graph_def = helper.make_graph(\n",
    "    [node_def],\n",
    "    \"test-model\",\n",
    "    [X, Pads],\n",
    "    [Y],\n",
    "    [helper.make_tensor(\"Pads\", TensorProto.INT64, [4,], [0, 0, 1, 1,])],\n",
    ")\n",
    "\n",
    "# Create the model (ModelProto)\n",
    "model_def = helper.make_model(graph_def,\n",
    "                              producer_name=\"onnx-example\")\n",
    "\n",
    "print(\"The producer_name in model: {}\\n\".format(model_def.producer_name))\n",
    "print(\"The graph in model:\\n{}\".format(model_def.graph))\n",
    "onnx.checker.check_model(model_def)\n",
    "print(\"The model is checked!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.11 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.11"
  },
  "vscode": {
   "interpreter": {
    "hash": "f9fa6017a53cd3e89c2ae5d3938d7461048c25b2aa8e520267fca421440325a1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
