{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "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": [
    "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",
    "    [\n",
    "        helper.make_tensor(\n",
    "            \"Pads\",\n",
    "            TensorProto.INT64,\n",
    "            [\n",
    "                4,\n",
    "            ],\n",
    "            [\n",
    "                0,\n",
    "                0,\n",
    "                1,\n",
    "                1,\n",
    "            ],\n",
    "        )\n",
    "    ],\n",
    ")\n",
    "\n",
    "# Create the model (ModelProto)\n",
    "model_def = helper.make_model(graph_def, 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!\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
