Skip to content

Instantly share code, notes, and snippets.

@geoffreyangus
Last active November 8, 2022 19:29
Show Gist options
  • Save geoffreyangus/6996c1cd7b0311385673d876c94740c8 to your computer and use it in GitHub Desktop.
Save geoffreyangus/6996c1cd7b0311385673d876c94740c8 to your computer and use it in GitHub Desktop.
Shows MNIST download, training, and TorchScript export, as well as errors from ONNX export.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "b3927fc7-c6d3-4214-910c-5d9214f84214",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/geoffreyangus/repositories/predibase/ludwig/venv_issue2292/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import os\n",
"import pandas as pd\n",
"import torch\n",
"from ludwig.models.inference import InferenceModule\n",
"from ludwig.utils.data_utils import load_json\n",
"from ludwig.utils.inference_utils import to_inference_module_input_from_dataframe"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c17e02c2-b817-42bc-af32-06ccf703122c",
"metadata": {},
"outputs": [],
"source": [
"# # 1. Download MNIST\n",
"# !ludwig datasets download mnist -o /Users/geoffreyangus/Downloads/issue2292/mnist\n",
"\n",
"# # 2. Run train using the provided config.yaml file\n",
"# !ludwig experiment --config=/Users/geoffreyangus/Downloads/issue2292/mnist/config.yaml --dataset=/Users/geoffreyangus/Downloads/issue2292/mnist/mnist.parquet --output_directory=/Users/geoffreyangus/Downloads/issue2292/\n",
"\n",
"# # 3. Export TorchScript (unspecified --output_path flag places the artifacts into the --model_path dir)\n",
"# !ludwig export_torchscript --model_path=/Users/geoffreyangus/Downloads/issue2292/experiment_run/model"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "16c64673-4298-437e-9059-0d99bb42248c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>image_path</th>\n",
" <th>label</th>\n",
" <th>split</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>13311</th>\n",
" <td>/Users/geoffreyangus/Downloads/issue2292/mnist...</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>43758</th>\n",
" <td>/Users/geoffreyangus/Downloads/issue2292/mnist...</td>\n",
" <td>7</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3635</th>\n",
" <td>/Users/geoffreyangus/Downloads/issue2292/mnist...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>65963</th>\n",
" <td>/Users/geoffreyangus/Downloads/issue2292/mnist...</td>\n",
" <td>5</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2059</th>\n",
" <td>/Users/geoffreyangus/Downloads/issue2292/mnist...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" image_path label split\n",
"13311 /Users/geoffreyangus/Downloads/issue2292/mnist... 2 0\n",
"43758 /Users/geoffreyangus/Downloads/issue2292/mnist... 7 0\n",
"3635 /Users/geoffreyangus/Downloads/issue2292/mnist... 0 0\n",
"65963 /Users/geoffreyangus/Downloads/issue2292/mnist... 5 2\n",
"2059 /Users/geoffreyangus/Downloads/issue2292/mnist... 0 0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_parquet('/Users/geoffreyangus/Downloads/issue2292/mnist/mnist.parquet')\n",
"config = load_json('/Users/geoffreyangus/Downloads/issue2292/experiment_run/model/model_hyperparameters.json')\n",
"\n",
"# Update the image_path values from relative paths to absolute paths\n",
"sample_df = df.sample(5)\n",
"sample_df['image_path'] = sample_df['image_path'].map(lambda x: os.path.join('/Users/geoffreyangus/Downloads/issue2292/mnist', x))\n",
"sample_df"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "283dc6e8-274b-47b7-adaf-dd4e05fc359c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['image_path'])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use the `load_paths` keyword argument to ensure that the image path columns are actually loaded as image tensors\n",
"sample_input = to_inference_module_input_from_dataframe(sample_df, config, load_paths=True)\n",
"sample_input.keys()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "dfbdafef-0178-4ef2-aee4-7fac6feeb66b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No device specified. Loading using device \"cpu\".\n",
"Loaded torchscript module for preprocessor from inference_preprocessor.pt.\n",
"Loaded torchscript module for predictor from inference_predictor-cpu.pt.\n",
"Loaded torchscript module for postprocessor from inference_postprocessor.pt.\n",
"Loading metadata from: /Users/geoffreyangus/Downloads/issue2292/experiment_run/model/training_set_metadata.json\n"
]
}
],
"source": [
"# Recommend using the ludwig.models.inference.InferenceModule class to load the preprocessor, predictor, and postprocessor artifacts\n",
"# as one torch.nn.Module for easy TorchScript export.\n",
"inference_pipeline = torch.jit.script(InferenceModule.from_directory('/Users/geoffreyangus/Downloads/issue2292/experiment_run/model'))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "bc22cb4e-c313-4f56-ab89-0d851c2e7bbe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'label': {'predictions': ['2', '7', '0', '5', '0'],\n",
" 'probabilities': tensor([[2.0062e-03, 1.7255e-03, 4.9359e-02, 9.4350e-01, 7.1058e-06, 8.3150e-05,\n",
" 5.4053e-05, 3.1687e-03, 1.8448e-05, 7.3448e-05],\n",
" [2.6325e-05, 9.9947e-01, 1.9280e-04, 1.6618e-04, 1.1855e-04, 5.0083e-06,\n",
" 2.1291e-08, 8.0450e-06, 1.7813e-06, 1.1995e-05],\n",
" [2.8192e-04, 1.1060e-04, 2.9322e-05, 7.0533e-04, 6.3088e-04, 9.8941e-01,\n",
" 6.2594e-03, 1.0901e-03, 1.4481e-03, 3.8569e-05],\n",
" [1.0531e-03, 1.1224e-03, 7.0515e-02, 4.5050e-03, 9.6964e-04, 1.8820e-02,\n",
" 2.3580e-02, 1.0226e-02, 1.4057e-04, 8.6907e-01],\n",
" [2.3324e-06, 7.1718e-05, 9.1266e-07, 2.9334e-04, 1.6444e-04, 9.9923e-01,\n",
" 1.8420e-04, 2.5986e-05, 2.3444e-05, 2.2788e-06]])}}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inference_pipeline(sample_input)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8a70f484-e205-4b32-a4e5-7d07ca5d5120",
"metadata": {},
"outputs": [
{
"ename": "SymbolicValueError",
"evalue": "Unsupported prim::Constant kind: 'ival'. Please send a bug report at https://github.com/pytorch/pytorch/issues. [Caused by the value 'self.postprocessor.postproc_modules.label__ludwig.idx2str defined in (%self.postprocessor.postproc_modules.label__ludwig.idx2str : Dict(int, str) = prim::Constant[value={0: \"1\", 1: \"7\", 2: \"3\", 3: \"2\", 4: \"9\", 5: \"0\", 6: \"6\", 7: \"8\", 8: \"4\", 9: \"5\"}]()\n)' (type 'Dict[int, str]') in the TorchScript graph. The containing node has kind 'prim::Constant'.] \n\n Inputs:\n Empty\n Outputs:\n #0: self.postprocessor.postproc_modules.label__ludwig.idx2str defined in (%self.postprocessor.postproc_modules.label__ludwig.idx2str : Dict(int, str) = prim::Constant[value={0: \"1\", 1: \"7\", 2: \"3\", 3: \"2\", 4: \"9\", 5: \"0\", 6: \"6\", 7: \"8\", 8: \"4\", 9: \"5\"}]()\n ) (type 'Dict[int, str]')",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mSymbolicValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn [7], line 5\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01monnx\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# ERROR: Ludwig uses Dict objects behind the scenes to do pre- and post- processing. However, it seems that ONNX does not support exporting these.\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# Open GitHub issue here: https://github.com/pytorch/pytorch/issues/87785\u001b[39;00m\n\u001b[0;32m----> 5\u001b[0m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43monnx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexport\u001b[49m\u001b[43m(\u001b[49m\u001b[43minference_pipeline\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43msample_input\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mimage_path\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m/Users/geoffreyangus/Downloads/issue2292/experiment_run/model/model.onnx\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/repositories/predibase/ludwig/venv_issue2292/lib/python3.8/site-packages/torch/onnx/utils.py:504\u001b[0m, in \u001b[0;36mexport\u001b[0;34m(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, opset_version, do_constant_folding, dynamic_axes, keep_initializers_as_inputs, custom_opsets, export_modules_as_functions)\u001b[0m\n\u001b[1;32m 186\u001b[0m \u001b[38;5;129m@_beartype\u001b[39m\u001b[38;5;241m.\u001b[39mbeartype\n\u001b[1;32m 187\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mexport\u001b[39m(\n\u001b[1;32m 188\u001b[0m model: Union[torch\u001b[38;5;241m.\u001b[39mnn\u001b[38;5;241m.\u001b[39mModule, torch\u001b[38;5;241m.\u001b[39mjit\u001b[38;5;241m.\u001b[39mScriptModule, torch\u001b[38;5;241m.\u001b[39mjit\u001b[38;5;241m.\u001b[39mScriptFunction],\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 204\u001b[0m export_modules_as_functions: Union[\u001b[38;5;28mbool\u001b[39m, Collection[Type[torch\u001b[38;5;241m.\u001b[39mnn\u001b[38;5;241m.\u001b[39mModule]]] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m 205\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 206\u001b[0m \u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"Exports a model into ONNX format.\u001b[39;00m\n\u001b[1;32m 207\u001b[0m \n\u001b[1;32m 208\u001b[0m \u001b[38;5;124;03m If ``model`` is not a :class:`torch.jit.ScriptModule` nor a\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 501\u001b[0m \u001b[38;5;124;03m All errors are subclasses of :class:`errors.OnnxExporterError`.\u001b[39;00m\n\u001b[1;32m 502\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 504\u001b[0m \u001b[43m_export\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 505\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 506\u001b[0m \u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 507\u001b[0m \u001b[43m \u001b[49m\u001b[43mf\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 508\u001b[0m \u001b[43m \u001b[49m\u001b[43mexport_params\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 509\u001b[0m \u001b[43m \u001b[49m\u001b[43mverbose\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 510\u001b[0m \u001b[43m \u001b[49m\u001b[43mtraining\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 511\u001b[0m \u001b[43m \u001b[49m\u001b[43minput_names\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 512\u001b[0m \u001b[43m \u001b[49m\u001b[43moutput_names\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 513\u001b[0m \u001b[43m \u001b[49m\u001b[43moperator_export_type\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moperator_export_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 514\u001b[0m \u001b[43m \u001b[49m\u001b[43mopset_version\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mopset_version\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 515\u001b[0m \u001b[43m \u001b[49m\u001b[43mdo_constant_folding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdo_constant_folding\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 516\u001b[0m \u001b[43m \u001b[49m\u001b[43mdynamic_axes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdynamic_axes\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 517\u001b[0m \u001b[43m \u001b[49m\u001b[43mkeep_initializers_as_inputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkeep_initializers_as_inputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 518\u001b[0m \u001b[43m \u001b[49m\u001b[43mcustom_opsets\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcustom_opsets\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 519\u001b[0m \u001b[43m \u001b[49m\u001b[43mexport_modules_as_functions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mexport_modules_as_functions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 520\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/repositories/predibase/ludwig/venv_issue2292/lib/python3.8/site-packages/torch/onnx/utils.py:1529\u001b[0m, in \u001b[0;36m_export\u001b[0;34m(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, export_type, opset_version, do_constant_folding, dynamic_axes, keep_initializers_as_inputs, fixed_batch_size, custom_opsets, add_node_names, onnx_shape_inference, export_modules_as_functions)\u001b[0m\n\u001b[1;32m 1526\u001b[0m dynamic_axes \u001b[38;5;241m=\u001b[39m {}\n\u001b[1;32m 1527\u001b[0m _validate_dynamic_axes(dynamic_axes, model, input_names, output_names)\n\u001b[0;32m-> 1529\u001b[0m graph, params_dict, torch_out \u001b[38;5;241m=\u001b[39m \u001b[43m_model_to_graph\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1530\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1531\u001b[0m \u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1532\u001b[0m \u001b[43m \u001b[49m\u001b[43mverbose\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1533\u001b[0m \u001b[43m \u001b[49m\u001b[43minput_names\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1534\u001b[0m \u001b[43m \u001b[49m\u001b[43moutput_names\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1535\u001b[0m \u001b[43m \u001b[49m\u001b[43moperator_export_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1536\u001b[0m \u001b[43m \u001b[49m\u001b[43mval_do_constant_folding\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1537\u001b[0m \u001b[43m \u001b[49m\u001b[43mfixed_batch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfixed_batch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1538\u001b[0m \u001b[43m \u001b[49m\u001b[43mtraining\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtraining\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1539\u001b[0m \u001b[43m \u001b[49m\u001b[43mdynamic_axes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdynamic_axes\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1540\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1542\u001b[0m \u001b[38;5;66;03m# TODO: Don't allocate a in-memory string for the protobuf\u001b[39;00m\n\u001b[1;32m 1543\u001b[0m defer_weight_export \u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m 1544\u001b[0m export_type \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _exporter_states\u001b[38;5;241m.\u001b[39mExportTypes\u001b[38;5;241m.\u001b[39mPROTOBUF_FILE\n\u001b[1;32m 1545\u001b[0m )\n",
"File \u001b[0;32m~/repositories/predibase/ludwig/venv_issue2292/lib/python3.8/site-packages/torch/onnx/utils.py:1115\u001b[0m, in \u001b[0;36m_model_to_graph\u001b[0;34m(model, args, verbose, input_names, output_names, operator_export_type, do_constant_folding, _disable_torch_constant_prop, fixed_batch_size, training, dynamic_axes)\u001b[0m\n\u001b[1;32m 1112\u001b[0m params_dict \u001b[38;5;241m=\u001b[39m _get_named_param_dict(graph, params)\n\u001b[1;32m 1114\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1115\u001b[0m graph \u001b[38;5;241m=\u001b[39m \u001b[43m_optimize_graph\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1116\u001b[0m \u001b[43m \u001b[49m\u001b[43mgraph\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1117\u001b[0m \u001b[43m \u001b[49m\u001b[43moperator_export_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1118\u001b[0m \u001b[43m \u001b[49m\u001b[43m_disable_torch_constant_prop\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m_disable_torch_constant_prop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1119\u001b[0m \u001b[43m \u001b[49m\u001b[43mfixed_batch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfixed_batch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1120\u001b[0m \u001b[43m \u001b[49m\u001b[43mparams_dict\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mparams_dict\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1121\u001b[0m \u001b[43m \u001b[49m\u001b[43mdynamic_axes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdynamic_axes\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1122\u001b[0m \u001b[43m \u001b[49m\u001b[43minput_names\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minput_names\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1123\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodule\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmodule\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1124\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1125\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 1126\u001b[0m torch\u001b[38;5;241m.\u001b[39monnx\u001b[38;5;241m.\u001b[39mlog(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTorch IR graph at exception: \u001b[39m\u001b[38;5;124m\"\u001b[39m, graph)\n",
"File \u001b[0;32m~/repositories/predibase/ludwig/venv_issue2292/lib/python3.8/site-packages/torch/onnx/utils.py:663\u001b[0m, in \u001b[0;36m_optimize_graph\u001b[0;34m(graph, operator_export_type, _disable_torch_constant_prop, fixed_batch_size, params_dict, dynamic_axes, input_names, module)\u001b[0m\n\u001b[1;32m 660\u001b[0m _C\u001b[38;5;241m.\u001b[39m_jit_pass_onnx_set_dynamic_input_shape(graph, dynamic_axes, input_names)\n\u001b[1;32m 661\u001b[0m _C\u001b[38;5;241m.\u001b[39m_jit_pass_onnx_lint(graph)\n\u001b[0;32m--> 663\u001b[0m graph \u001b[38;5;241m=\u001b[39m \u001b[43m_C\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_jit_pass_onnx\u001b[49m\u001b[43m(\u001b[49m\u001b[43mgraph\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moperator_export_type\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 664\u001b[0m _C\u001b[38;5;241m.\u001b[39m_jit_pass_onnx_lint(graph)\n\u001b[1;32m 665\u001b[0m _C\u001b[38;5;241m.\u001b[39m_jit_pass_lint(graph)\n",
"File \u001b[0;32m~/repositories/predibase/ludwig/venv_issue2292/lib/python3.8/site-packages/torch/onnx/utils.py:1867\u001b[0m, in \u001b[0;36m_run_symbolic_function\u001b[0;34m(graph, block, node, inputs, env, operator_export_type)\u001b[0m\n\u001b[1;32m 1863\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m symbolic_fn \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 1864\u001b[0m attrs \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 1865\u001b[0m k: symbolic_helper\u001b[38;5;241m.\u001b[39m_node_get(node, k) \u001b[38;5;28;01mfor\u001b[39;00m k \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39mattributeNames()\n\u001b[1;32m 1866\u001b[0m }\n\u001b[0;32m-> 1867\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43msymbolic_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[43mgraph_context\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mattrs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1869\u001b[0m attrs \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 1870\u001b[0m k \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m node\u001b[38;5;241m.\u001b[39mkindOf(k)[\u001b[38;5;241m0\u001b[39m]: symbolic_helper\u001b[38;5;241m.\u001b[39m_node_get(node, k)\n\u001b[1;32m 1871\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39mattributeNames()\n\u001b[1;32m 1872\u001b[0m }\n\u001b[1;32m 1873\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m namespace \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124monnx\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 1874\u001b[0m \u001b[38;5;66;03m# Clone node to trigger ONNX shape inference\u001b[39;00m\n",
"File \u001b[0;32m~/repositories/predibase/ludwig/venv_issue2292/lib/python3.8/site-packages/torch/onnx/symbolic_opset9.py:6635\u001b[0m, in \u001b[0;36mprim_constant\u001b[0;34m(g, *inputs, **attrs)\u001b[0m\n\u001b[1;32m 6628\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m node\u001b[38;5;241m.\u001b[39moutput()\u001b[38;5;241m.\u001b[39mtype()\u001b[38;5;241m.\u001b[39misSubtypeOf(\n\u001b[1;32m 6629\u001b[0m _C\u001b[38;5;241m.\u001b[39mListType\u001b[38;5;241m.\u001b[39mofInts()\n\u001b[1;32m 6630\u001b[0m ) \u001b[38;5;129;01mor\u001b[39;00m node\u001b[38;5;241m.\u001b[39moutput()\u001b[38;5;241m.\u001b[39mtype()\u001b[38;5;241m.\u001b[39misSubtypeOf(_C\u001b[38;5;241m.\u001b[39mListType\u001b[38;5;241m.\u001b[39mofFloats()):\n\u001b[1;32m 6631\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m g\u001b[38;5;241m.\u001b[39mop(\n\u001b[1;32m 6632\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mConstant\u001b[39m\u001b[38;5;124m\"\u001b[39m, value_t\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39mtensor(symbolic_helper\u001b[38;5;241m.\u001b[39m_node_get(node, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[1;32m 6633\u001b[0m )\n\u001b[0;32m-> 6635\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m errors\u001b[38;5;241m.\u001b[39mSymbolicValueError(\n\u001b[1;32m 6636\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnsupported prim::Constant kind: \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mnode\u001b[38;5;241m.\u001b[39mkindOf(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 6637\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease send a bug report at \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m_constants\u001b[38;5;241m.\u001b[39mPYTORCH_GITHUB_ISSUES_URL\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 6638\u001b[0m node\u001b[38;5;241m.\u001b[39moutput(),\n\u001b[1;32m 6639\u001b[0m )\n",
"\u001b[0;31mSymbolicValueError\u001b[0m: Unsupported prim::Constant kind: 'ival'. Please send a bug report at https://github.com/pytorch/pytorch/issues. [Caused by the value 'self.postprocessor.postproc_modules.label__ludwig.idx2str defined in (%self.postprocessor.postproc_modules.label__ludwig.idx2str : Dict(int, str) = prim::Constant[value={0: \"1\", 1: \"7\", 2: \"3\", 3: \"2\", 4: \"9\", 5: \"0\", 6: \"6\", 7: \"8\", 8: \"4\", 9: \"5\"}]()\n)' (type 'Dict[int, str]') in the TorchScript graph. The containing node has kind 'prim::Constant'.] \n\n Inputs:\n Empty\n Outputs:\n #0: self.postprocessor.postproc_modules.label__ludwig.idx2str defined in (%self.postprocessor.postproc_modules.label__ludwig.idx2str : Dict(int, str) = prim::Constant[value={0: \"1\", 1: \"7\", 2: \"3\", 3: \"2\", 4: \"9\", 5: \"0\", 6: \"6\", 7: \"8\", 8: \"4\", 9: \"5\"}]()\n ) (type 'Dict[int, str]')"
]
}
],
"source": [
"import torch.onnx\n",
"\n",
"# ERROR: Ludwig uses Dict objects behind the scenes to do pre- and post- processing. However, it seems that ONNX does not support exporting these.\n",
"# Open GitHub issue here: https://github.com/pytorch/pytorch/issues/87785\n",
"torch.onnx.export(inference_pipeline, (sample_input['image_path'],), '/Users/geoffreyangus/Downloads/issue2292/experiment_run/model/model.onnx')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c8c2150-1172-4d1c-9831-0105819bfc90",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv_issue2292",
"language": "python",
"name": "venv_issue2292"
},
"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.8.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment