From 57ee7ab4674bd9e2e31aac58a8bb856c7a7a5972 Mon Sep 17 00:00:00 2001
From: auphelia <jakobapk@web.de>
Date: Tue, 4 Feb 2020 17:18:21 +0000
Subject: [PATCH] [Notebook] Finished section about network preparation

---
 notebooks/9-FINN-EndToEndFlow.ipynb | 74 ++++++++++++++++++++++++++++-
 1 file changed, 72 insertions(+), 2 deletions(-)

diff --git a/notebooks/9-FINN-EndToEndFlow.ipynb b/notebooks/9-FINN-EndToEndFlow.ipynb
index 48e78453e..83ff14252 100644
--- a/notebooks/9-FINN-EndToEndFlow.ipynb
+++ b/notebooks/9-FINN-EndToEndFlow.ipynb
@@ -409,7 +409,10 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "Our example network is a quantized network with 1 bit precision. For this reason, after streamlining, the resulting bipolar matrix multiplications are converted into xnorpopcount operations. This transformation produces operations that are again collapsed and converted into thresholds. This procedure is shown below. After these transformations, the nodes can be converted to HLS layers for further processing."
+    "Our example network is a quantized network with 1 bit precision. For this reason, after streamlining, the resulting bipolar matrix multiplications are converted into xnorpopcount operations. This transformation produces operations that are again collapsed and converted into thresholds. This procedure is shown below. \n",
+    "In this state the model can still be simulated with Python, even if it no longer contains only standard ONNX nodes. For details, see section [Simulation using Python](#simpy).\n",
+    "\n",
+    "After these finishing transformations, the nodes can be converted to HLS layers for further processing."
    ]
   },
   {
@@ -498,7 +501,74 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "### Folding <a id='folding'></a>"
+    "### Folding <a id='folding'></a>\n",
+    "Since the folding parameters are node attributes, they can be easily accessed and changed using a helper function of the `ModelWrapper`. But first we have to extract the nodes which are StreamingFCLayer_Batch operations. This is where netron helps us, in the above diagram we can see that the third to sixth nodes are StreamingFCLayer_Batch. Through the `print`s we can check if the extracted nodes all have the op_type \"StreamingFCLayer_Batch\". For more details on how to use ONNX model, see Jupyter notebook [1-FINN-HowToWorkWithONNX](1-FINN-HowToWorkWithONNX.ipynb)."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 22,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "fc0 has the op_type: StreamingFCLayer_Batch\n",
+      "fc1 has the op_type: StreamingFCLayer_Batch\n",
+      "fc2 has the op_type: StreamingFCLayer_Batch\n",
+      "fc3 has the op_type: StreamingFCLayer_Batch\n"
+     ]
+    }
+   ],
+   "source": [
+    "fc0 = model.graph.node[2]\n",
+    "fc1 = model.graph.node[3]\n",
+    "fc2 = model.graph.node[4]\n",
+    "fc3 = model.graph.node[5]\n",
+    "print(\"fc0 has the op_type: \" + str(fc0.op_type))\n",
+    "print(\"fc1 has the op_type: \" + str(fc1.op_type))\n",
+    "print(\"fc2 has the op_type: \" + str(fc2.op_type))\n",
+    "print(\"fc3 has the op_type: \" + str(fc3.op_type))"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Now we can use the [HLSCustomOp](https://github.com/Xilinx/finn/blob/dev/src/finn/custom_op/fpgadataflow/__init__.py) class to create a [StreamingFCLayer_Batch](https://github.com/Xilinx/finn/blob/dev/src/finn/custom_op/fpgadataflow/streamingfclayer_batch.py) object for each node to set PE and SIMD. This procedure is identical for each node. For more details about custom ops, see Jupyter notebook [7-FINN-CustomOps](7-FINN-CustomOps.ipynb)."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 25,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "from finn.custom_op.fpgadataflow.streamingfclayer_batch import StreamingFCLayer_Batch\n",
+    "\n",
+    "fc0w = StreamingFCLayer_Batch(fc0)\n",
+    "fc0w.set_nodeattr(\"SIMD\", 784)\n",
+    "fc0w.set_nodeattr(\"PE\", 32)\n",
+    "\n",
+    "fc1w = StreamingFCLayer_Batch(fc1)\n",
+    "fc1w.set_nodeattr(\"SIMD\", 1024)\n",
+    "fc1w.set_nodeattr(\"PE\", 32)\n",
+    "\n",
+    "fc2w = StreamingFCLayer_Batch(fc2)\n",
+    "fc2w.set_nodeattr(\"SIMD\", 1024)\n",
+    "fc2w.set_nodeattr(\"PE\", 32)\n",
+    "\n",
+    "fc3w = StreamingFCLayer_Batch(fc3)\n",
+    "fc3w.set_nodeattr(\"SIMD\", 1024)\n",
+    "fc3w.set_nodeattr(\"PE\", 10)\n"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "This completes the network preparation and the network can be passed on to the next block *Vivado HLS and Vivado synthesis*. Which is described below."
    ]
   },
   {
-- 
GitLab