From e1104a752c9675473394ed2cb4bb508f1ab56393 Mon Sep 17 00:00:00 2001 From: Giuseppe Franco Date: Mon, 13 Nov 2023 10:40:25 +0000 Subject: [PATCH] Fix (notebook): netron skip-exeuction and fixed seed --- notebooks/quantized_recurrent.ipynb | 192 ++++++++++++++++++++-------- 1 file changed, 140 insertions(+), 52 deletions(-) diff --git a/notebooks/quantized_recurrent.ipynb b/notebooks/quantized_recurrent.ipynb index 5e0b04cba..f032c442c 100644 --- a/notebooks/quantized_recurrent.ipynb +++ b/notebooks/quantized_recurrent.ipynb @@ -76,6 +76,8 @@ "import inspect\n", "from brevitas.nn import QuantRNN\n", "from IPython.display import Markdown, display\n", + "import torch\n", + "torch.manual_seed(0)\n", "\n", "def pretty_print_source(source):\n", " display(Markdown('```python\\n' + source + '\\n```'))\n", @@ -893,6 +895,25 @@ "cell_type": "code", "execution_count": 18, "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "from brevitas.nn import QuantLSTM\n", + "from brevitas.export import export_onnx_qcdq\n", + "\n", + "quant_lstm_weight_only = QuantLSTM(input_size=10, hidden_size=20, weight_bit_width=4, io_quant=None, bias_quant=None, gate_acc_quant=None, sigmoid_quant=None, tanh_quant=None, cell_state_quant=None)\n", + "export_path = 'quant_lstm_weight_only_4b.onnx'\n", + "exported_model = export_onnx_qcdq(quant_lstm_weight_only, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "tags": [ + "skip-execution" + ] + }, "outputs": [ { "name": "stdout", @@ -916,22 +937,15 @@ " " ], "text/plain": [ - "" + "" ] }, - "execution_count": 18, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "import torch\n", - "from brevitas.nn import QuantLSTM\n", - "from brevitas.export import export_onnx_qcdq\n", - "\n", - "quant_lstm_weight_only = QuantLSTM(input_size=10, hidden_size=20, weight_bit_width=4, io_quant=None, bias_quant=None, gate_acc_quant=None, sigmoid_quant=None, tanh_quant=None, cell_state_quant=None)\n", - "export_path = 'quant_lstm_weight_only_4b.onnx'\n", - "export_onnx_qcdq(quant_lstm_weight_only, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)\n", "show_netron(export_path, 8080)" ] }, @@ -968,8 +982,29 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "from brevitas.nn import QuantLSTM\n", + "from brevitas.export import export_onnx_qcdq\n", + "\n", + "quant_lstm_weight_only_cifg = QuantLSTM(\n", + " input_size=10, hidden_size=20, coupled_input_forget_gates=True, weight_bit_width=4,\n", + " io_quant=None, bias_quant=None, gate_acc_quant=None, sigmoid_quant=None, tanh_quant=None, cell_state_quant=None)\n", + "export_path = 'quant_lstm_weight_only_cifg_4b.onnx'\n", + "exported_model = export_onnx_qcdq(quant_lstm_weight_only_cifg, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "tags": [ + "skip-execution" + ] + }, "outputs": [ { "name": "stdout", @@ -993,24 +1028,15 @@ " " ], "text/plain": [ - "" + "" ] }, - "execution_count": 20, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "import torch\n", - "from brevitas.nn import QuantLSTM\n", - "from brevitas.export import export_onnx_qcdq\n", - "\n", - "quant_lstm_weight_only_cifg = QuantLSTM(\n", - " input_size=10, hidden_size=20, coupled_input_forget_gates=True, weight_bit_width=4,\n", - " io_quant=None, bias_quant=None, gate_acc_quant=None, sigmoid_quant=None, tanh_quant=None, cell_state_quant=None)\n", - "export_path = 'quant_lstm_weight_only_cifg_4b.onnx'\n", - "export_onnx_qcdq(quant_lstm_weight_only_cifg, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)\n", "show_netron(export_path, 8082)" ] }, @@ -1047,17 +1073,39 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "c:\\users\\alessand\\documents\\brevitas\\src\\brevitas\\nn\\mixin\\base.py:112: UserWarning: Keyword arguments are being passed but they not being used.\n", + "/home/giuseppe/Documents/git/brevitas/src/brevitas/nn/mixin/base.py:77: UserWarning: Keyword arguments are being passed but they not being used.\n", " warn('Keyword arguments are being passed but they not being used.')\n" ] - }, + } + ], + "source": [ + "import torch\n", + "from brevitas.nn import QuantLSTM\n", + "from brevitas.export import export_onnx_qcdq\n", + "\n", + "quant_lstm_weight_only_bidirectional_2_layers = QuantLSTM(\n", + " input_size=10, hidden_size=20, bidirectional=True, num_layers=2, weight_bit_width=4,\n", + " io_quant=None, bias_quant=None, gate_acc_quant=None, sigmoid_quant=None, tanh_quant=None, cell_state_quant=None)\n", + "export_path = 'quant_lstm_weight_only_bidirectional_2_layers.onnx'\n", + "exported_model = export_onnx_qcdq(quant_lstm_weight_only_bidirectional_2_layers, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "tags": [ + "skip-execution" + ] + }, + "outputs": [ { "name": "stdout", "output_type": "stream", @@ -1080,24 +1128,15 @@ " " ], "text/plain": [ - "" + "" ] }, - "execution_count": 22, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "import torch\n", - "from brevitas.nn import QuantLSTM\n", - "from brevitas.export import export_onnx_qcdq\n", - "\n", - "quant_lstm_weight_only_bidirectional_2_layers = QuantLSTM(\n", - " input_size=10, hidden_size=20, bidirectional=True, num_layers=2, weight_bit_width=4,\n", - " io_quant=None, bias_quant=None, gate_acc_quant=None, sigmoid_quant=None, tanh_quant=None, cell_state_quant=None)\n", - "export_path = 'quant_lstm_weight_only_bidirectional_2_layers.onnx'\n", - "export_onnx_qcdq(quant_lstm_weight_only_bidirectional_2_layers, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)\n", "show_netron(export_path, 8083)" ] }, @@ -1110,17 +1149,39 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "c:\\users\\alessand\\documents\\brevitas\\src\\brevitas\\nn\\mixin\\base.py:112: UserWarning: Keyword arguments are being passed but they not being used.\n", + "/home/giuseppe/Documents/git/brevitas/src/brevitas/nn/mixin/base.py:77: UserWarning: Keyword arguments are being passed but they not being used.\n", " warn('Keyword arguments are being passed but they not being used.')\n" ] - }, + } + ], + "source": [ + "import torch\n", + "from brevitas.nn import QuantLSTM\n", + "from brevitas.export import export_onnx_qcdq\n", + "\n", + "quant_lstm_weight_only_bidirectional_2_layers_shared = QuantLSTM(\n", + " input_size=10, hidden_size=20, bidirectional=True, shared_input_hidden_weights=True, weight_bit_width=4,\n", + " io_quant=None, bias_quant=None, gate_acc_quant=None, sigmoid_quant=None, tanh_quant=None, cell_state_quant=None)\n", + "export_path = 'quant_lstm_weight_only_bidirectional_2_layers_shared_ih.onnx'\n", + "exported_model = export_onnx_qcdq(quant_lstm_weight_only_bidirectional_2_layers_shared, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "tags": [ + "skip-execution" + ] + }, + "outputs": [ { "name": "stdout", "output_type": "stream", @@ -1143,24 +1204,15 @@ " " ], "text/plain": [ - "" + "" ] }, - "execution_count": 23, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "import torch\n", - "from brevitas.nn import QuantLSTM\n", - "from brevitas.export import export_onnx_qcdq\n", - "\n", - "quant_lstm_weight_only_bidirectional_2_layers_shared = QuantLSTM(\n", - " input_size=10, hidden_size=20, bidirectional=True, shared_input_hidden_weights=True, weight_bit_width=4,\n", - " io_quant=None, bias_quant=None, gate_acc_quant=None, sigmoid_quant=None, tanh_quant=None, cell_state_quant=None)\n", - "export_path = 'quant_lstm_weight_only_bidirectional_2_layers_shared_ih.onnx'\n", - "export_onnx_qcdq(quant_lstm_weight_only_bidirectional_2_layers_shared, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)\n", "show_netron(export_path, 8085)" ] }, @@ -1224,7 +1276,19 @@ " input_size=10, hidden_size=20, bidirectional=True, num_layers=2, weight_bit_width=4, shared_intra_layer_weight_quant=True,\n", " io_quant=None, bias_quant=None, gate_acc_quant=None, sigmoid_quant=None, tanh_quant=None, cell_state_quant=None)\n", "export_path = 'quant_lstm_weight_only_bidirectional_2_layers_shared_q.onnx'\n", - "export_onnx_qcdq(quant_lstm_weight_only_bidirectional_2_layers, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)\n", + "exported_model = export_onnx_qcdq(quant_lstm_weight_only_bidirectional_2_layers, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "skip-execution" + ] + }, + "outputs": [], + "source": [ "show_netron(export_path, 8086)" ] }, @@ -1289,7 +1353,19 @@ " shared_input_hidden_weights=True, shared_intra_layer_weight_quant=True,\n", " io_quant=None, bias_quant=None, gate_acc_quant=None, sigmoid_quant=None, tanh_quant=None, cell_state_quant=None)\n", "export_path = 'quant_lstm_weight_only_bidirectional_2_layers_shared_q_ih.onnx'\n", - "export_onnx_qcdq(quant_lstm_weight_only_bidirectional_2_layers, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)\n", + "exported_model = export_onnx_qcdq(quant_lstm_weight_only_bidirectional_2_layers, (torch.randn(5, 1, 10)), opset_version=14, export_path=export_path)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "skip-execution" + ] + }, + "outputs": [], + "source": [ "show_netron(export_path, 8087)" ] }, @@ -1345,7 +1421,19 @@ "\n", "quant_lstm = QuantLSTM(input_size=10, hidden_size=20)\n", "export_path = 'quant_lstm.onnx'\n", - "export_qonnx(quant_lstm, (torch.randn(5, 1, 10)), export_path=export_path)\n", + "exported_model = export_qonnx(quant_lstm, (torch.randn(5, 1, 10)), export_path=export_path)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "skip-execution" + ] + }, + "outputs": [], + "source": [ "show_netron(export_path, 8088)" ] },