{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Debugging Torch-TensorRT Compilation\n\nTensorRT conversion can perform many graph transformations and backend specific\noptimizations that are sometimes hard to inspect. Torch-TensorRT provides a\nDebugger utility to help visualize FX graphs around lowering passes, monitor\nengine building, and capture profiling or TensorRT API traces.\n\nIn this example, we demonstrate how to:\n\n 1. Enable the Torch-TensorRT Debugger context\n 2. Capture and visualize FX graphs before and/or after specific lowering passes\n 3. Configure logging directory and verbosity\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\nimport tempfile\n\nimport numpy as np\nimport torch\nimport torch_tensorrt as torch_trt\nimport torchvision.models as models\n\ntemp_dir = os.path.join(tempfile.gettempdir(), \"torch_tensorrt_debugger_example\")\n\nnp.random.seed(0)\ntorch.manual_seed(0)\ninputs = [torch.rand((1, 3, 224, 224)).to(\"cuda\")]\n\n\nmodel = models.resnet18(pretrained=False).to(\"cuda\").eval()\nexp_program = torch.export.export(model, tuple(inputs))\nenabled_precisions = {torch.float}\nworkspace_size = 20 << 30\nmin_block_size = 0\nuse_python_runtime = False\ntorch_executed_ops = {}\n\nwith torch_trt.dynamo.Debugger(\n log_level=\"debug\",\n logging_dir=temp_dir,\n engine_builder_monitor=False, # whether to monitor the engine building process\n capture_fx_graph_after=[\n \"complex_graph_detection\"\n ], # fx graph visualization after certain lowering pass\n capture_fx_graph_before=[\n \"remove_detach\"\n ], # fx graph visualization before certain lowering pass\n):\n\n trt_gm = torch_trt.dynamo.compile(\n exp_program,\n tuple(inputs),\n use_python_runtime=use_python_runtime,\n enabled_precisions=enabled_precisions,\n min_block_size=min_block_size,\n torch_executed_ops=torch_executed_ops,\n immutable_weights=False,\n reuse_cached_engines=False,\n )\n\n trt_output = trt_gm(*inputs)\n\n\n\"\"\"\nThe logging directory will contain the following files:\n- /tmp/torch_tensorrt_debugger_example/\n torch_tensorrt_logging.log\n - /lowering_passes_visualization/\n after_complex_graph_detection.svg\n before_remove_detach.svg\n\"\"\"" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.11.14" } }, "nbformat": 4, "nbformat_minor": 0 }