From a8954b5bf5fdcc7b5039da3311b0e7377aba3f0d Mon Sep 17 00:00:00 2001 From: auphelia <jakobapk@web.de> Date: Wed, 4 Dec 2019 15:03:36 +0000 Subject: [PATCH] [notebook - analysis passes] Added information about tensors --- notebooks/FINN-HowToTransformationPass.ipynb | 43 +++-- ...INN-ModelWrapperAndHowToAnalysisPass.ipynb | 168 +++++++++++++++++- 2 files changed, 189 insertions(+), 22 deletions(-) diff --git a/notebooks/FINN-HowToTransformationPass.ipynb b/notebooks/FINN-HowToTransformationPass.ipynb index ecbc36366..2ca150533 100644 --- a/notebooks/FINN-HowToTransformationPass.ipynb +++ b/notebooks/FINN-HowToTransformationPass.ipynb @@ -6,6 +6,27 @@ "source": [ "# FINN - Transformation passes\n", "--------------------------------------\n", + "<font size=\"3\">In this notebook the idea behind transformation passes in FINN will be explained and with the help of an example the procedure of a transformation will be shown.\n", + "\n", + "Following showSrc function is used to print the source code of function calls in the Jupyter notebook:</font>" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import inspect\n", + "\n", + "def showSrc(what):\n", + " print(\"\".join(inspect.getsourcelines(what)[0]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "* <font size=\"3\">changes (transforms) the given graph</font>\n", "* <font size=\"3\">input: ModelWrapper</font>\n", "* <font size=\"3\">returns the changed model (ModelWrapper) and flag `model_was_changed`</font>" @@ -29,19 +50,7 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "import inspect\n", - "\n", - "def showSrc(what):\n", - " print(\"\".join(inspect.getsourcelines(what)[0]))" - ] - }, - { - "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -97,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -139,7 +148,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -151,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -187,7 +196,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ diff --git a/notebooks/FINN-ModelWrapperAndHowToAnalysisPass.ipynb b/notebooks/FINN-ModelWrapperAndHowToAnalysisPass.ipynb index b399c5658..c2e2b2f9e 100644 --- a/notebooks/FINN-ModelWrapperAndHowToAnalysisPass.ipynb +++ b/notebooks/FINN-ModelWrapperAndHowToAnalysisPass.ipynb @@ -5,7 +5,22 @@ "metadata": {}, "source": [ "# FINN - ModelWrapper and Analysis passes\n", - "--------------------------------------" + "--------------------------------------\n", + "<font size=\"3\"> This notebook is about the ModelWrapper class and analysis passes within FINN. \n", + "\n", + "Following showSrc function is used to print the source code of function calls in the Jupyter notebook:</font>" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "import inspect\n", + "\n", + "def showSrc(what):\n", + " print(\"\".join(inspect.getsourcelines(what)[0]))" ] }, { @@ -30,7 +45,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ @@ -48,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ @@ -66,19 +81,162 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Tensors\n", + "#### Tensors\n", "<font size=\"3\"> Every input and output of every node in the onnx model is represented as tensor with several properties (i.e. name, shape, data type). ModelWrapper provides some utility functions to work with the tensors </font>" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Tensor names" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['0', 'features.3.weight', 'features.3.bias', 'features.3.running_mean', 'features.3.running_var', 'features.7.weight', 'features.7.bias', 'features.7.running_mean', 'features.7.running_var', 'features.11.weight', 'features.11.bias', 'features.11.running_mean', 'features.11.running_var', '20', '23', '28', '30', '33', '34', '41', '42', '49', '50', '57', '58', '60']\n" + ] + } + ], + "source": [ + "# get all tensor names\n", + "tensor_list = onnx_model.get_all_tensor_names()\n", + "print(tensor_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Producer and consumer of a tensor" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "input: \"59\"\n", + "input: \"58\"\n", + "output: \"60\"\n", + "op_type: \"Mul\"\n", + "\n", + "input: \"0\"\n", + "output: \"21\"\n", + "op_type: \"Shape\"\n", + "\n" + ] + } + ], + "source": [ + "# get random tensor and find producer and consumer (returns node)\n", + "\n", + "tensor_name = tensor_list[25]\n", + "print(onnx_model.find_producer(tensor_name))\n", + "\n", + "tensor_name = tensor_list[0]\n", + "print(onnx_model.find_consumer(tensor_name))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Tensor shape" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 28, 28]\n" + ] + } + ], + "source": [ + "# get tensor_shape\n", + "\n", + "print(onnx_model.get_tensor_shape(tensor_name))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<font size=\"3\"> It is also possible to set the tensor shape with a helper function. The syntax would be the following:\n", + " \n", + "`onnx_model.set_tensor_shape(tensor_name, tensor_shape)`\n", + "\n", + "Optionally, the dtype of the tensor can also be specified as third argument. By default it is set to TensorProto.FLOAT. \n", + " \n", + "**Important:** dtype should not be confused with FINN data type, which specifies the quantization annotation.\n", + "</font>" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Tensor (FINN) data type" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# get all tenso" + "**Write about difference between dtype and FINN data type**" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "DataType.FLOAT32\n" + ] + } + ], + "source": [ + "# get tensor data type (FINN data type)\n", + "print(onnx_model.get_tensor_datatype(tensor_name))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**set function**" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, -- GitLab