Skip to content

Commit

Permalink
Fix (notebook): netron skip-exeuction and fixed seed
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe5 committed Nov 13, 2023
1 parent 7e26cbf commit e1104a7
Showing 1 changed file with 140 additions and 52 deletions.
192 changes: 140 additions & 52 deletions notebooks/quantized_recurrent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -916,22 +937,15 @@
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1f104c37df0>"
"<IPython.lib.display.IFrame at 0x7f5401082890>"
]
},
"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)"
]
},
Expand Down Expand Up @@ -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",
Expand All @@ -993,24 +1028,15 @@
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1f105f160d0>"
"<IPython.lib.display.IFrame at 0x7f5400fef4d0>"
]
},
"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)"
]
},
Expand Down Expand Up @@ -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",
Expand All @@ -1080,24 +1128,15 @@
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1f1061d6e50>"
"<IPython.lib.display.IFrame at 0x7f53fe880cd0>"
]
},
"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)"
]
},
Expand All @@ -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",
Expand All @@ -1143,24 +1204,15 @@
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1f1062b0760>"
"<IPython.lib.display.IFrame at 0x7f5430180890>"
]
},
"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)"
]
},
Expand Down Expand Up @@ -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)"
]
},
Expand Down Expand Up @@ -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)"
]
},
Expand Down Expand Up @@ -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)"
]
},
Expand Down

0 comments on commit e1104a7

Please sign in to comment.