メインコンテンツまでスキップ
バージョン: v2512

変換失敗原因の詳細分析

概要

このステップではAcuiRTの出力情報から変換に失敗しているモジュールの情報をどのように抽出・分析する方法について解説します。

環境依存性について

本チュートリアルには実行環境 で述べた環境における結果を記載しています。TensorRTやPyTorchのバージョン・使用しているGPUの世代・型によって異なる変換結果となる場合があります。

出力レポートの確認

前ステップで出力されたレポートを確認し、変換失敗の原因を分析します。test.pyを実行すると、--trt-engine-dirに与えたパスにレポートとTensorRTエンジンファイルが出力されています。サンプルの場合はexps/baselineに出力されています。出力内容は下記の通りです。

workflow_report.json

Accuracy/Latencyや変換率を含みます。AcuiRT適用後の性能比較や全体俯瞰に有用です。

  • conversion_rate: AcuiRTによってTensorRTのエンジンに変換されたnn.Moduleの数と 全体のnn.Moduleの数のタプルです。これは、AcuiRTがどの程度のnn.ModuleをTensorRTエンジンに変換できたかを表します。理想的には 変換されたnn.Moduleの数が全体のnn.Moduleの数と一致します。

  • num_modules: AcuiRTが変換時に生成したTensorRTのエンジンの総数です。理想的には全てのnn.Moduleが単一のTensorRTエンジンに変換されるため、この値は「1」になります

  • performance: AcuiRTを用いた際のモデル全体のAccuracy/Latencyのデータが格納されています。

  • non_converted_performance: PyTorchモデルを使用した場合のAccuracy/Latencyが格納されています。

    workflow_report.json
    {
    "conversion_rate": [
    73,
    447
    ],
    "num_modules": 63,
    "performance": {
    "accuracy": {
    "AP": 0.41445725757137836
    },
    "latency": "66.11 ms"
    },
    "non_converted_performance": {
    "accuracy": {
    "AP": 0.5310819473995319
    },
    "latency": "60.92 ms"
    }
    }

DETRの例では、このレポートから下記のことがわかります。

  • num_modulesを確認すると、理想的な1ではなく63となっています。これは、DETRのモデルが複数のTensorRTエンジンに分割されていることを意味しています。

  • また、conversion_rateに着目するとTensorRT上で計算が可能なモジュールは全モジュールの16%程度であることが分かります。この状態では演算の約80%程度がPyTorch上で実行されている状態です。そのため最適化の余地があることが分かります。

statistics.csv

クラス・変換失敗/成功状況・エラーごとに集約された統計データです。エラー傾向の把握やボトルネックの特定に役立ちます。

  • statistics.csvの例:

    statuserrornum_errorsmodule_class_namemodule_code_filenamemodule_code_linedevice_time_totalcpu_time_totaldevice_events_totalcpu_events_totalused_named_modules
    'failed'"Failed to export the model with torch.export. \x1...2'DETR''detr/models/detr.py'213.74413338933"['']"
    'failed'"Failed to export the model with torch.export. \x1...1'Joiner''detr/models/backbone.py'96011721803"['backbone']"
    'failed'"Failed to export the model with torch.export. \x1...2'Backbone''detr/models/backbone.py'8353.82311707733"['backbone.0']"
    'failed''Dynamic shape loading is not implemented yet.'1'IntermediateLayerGetter''detr/.venv/lib/python3.12/...13011702703"['backbone.0.body']"
    'failed''Dynamic shape loading is not implemented yet.'105'Conv2d''detr/.venv/lib/python3.12/...374059274.40315"['input_proj', 'backbone.0.body.conv1', 'backbone...
    'failed''Dynamic shape loading is not implemented yet.'208'FrozenBatchNorm2d''detr/models/backbone.py'1932142.630941.7312312"['backbone.0.body.bn1', 'backbone.0.body.layer1.0...
    'failed''Dynamic shape loading is not implemented yet.'68'ReLU''detr/.venv/lib/python3.12/...10510230.910230.9300300"['backbone.0.body.relu', 'backbone.0.body.layer1....
    'failed''Dynamic shape loading is not implemented yet.'2'MaxPool2d''detr/.venv/lib/python3.12/...1571187.731187.7333"['backbone.0.body.maxpool']"

trace.json

PyTorch Profilerのトレース結果です。perfetto を用いて各モジュールの推論時間や割合を視覚化できます。

  • perfettoで視覚化した例

    perfettoでの可視化例

conversion_report.json

変換の成功・失敗、エラー内容(traceback)、設定オプションなどを出力します。現状の確認に便利です。

conversion_report.json
{
"": {
"rt_mode": null,
"children": null,
"input_shapes": null,
"input_args": null,
"class_name": "DETR",
"module_name": "models.detr",
"status": "failed",
"error": "Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted, but their usage is not recommended. Here, received an input of unsupported type: NestedTensor",
"traceback": "Traceback (most recent call last):\n File \".venv/lib/python3.12/site-packagesaibooster/intelligence/acuirt/convert/converter/convert_auto.py\", line 70, in auto_convert2trt\n summary = CONVERSION_REGISTRY[conversion_mode](\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \".venv/lib/python3.12/site-packagesaibooster/intelligence/acuirt/convert/converter/convert_onnx.py\", line 117, in convert_trt_with_onnx\n torch.onnx.export(**arguments)\n File \".venv/lib/python3.12/site-packages/torch/onnx/__init__.py\", line 375, in export\n export(\n File \".venv/lib/python3.12/site-packages/torch/onnx/utils.py\", line 502, in export\n _export(\n File \".venv/lib/python3.12/site-packages/torch/onnx/utils.py\", line 1564, in _export\n graph, params_dict, torch_out = _model_to_graph(\n ^^^^^^^^^^^^^^^^\n File \".venv/lib/python3.12/site-packages/torch/onnx/utils.py\", line 1113, in _model_to_graph\n graph, params, torch_out, module = _create_jit_graph(model, args)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \".venv/lib/python3.12/site-packages/torch/onnx/utils.py\", line 997, in _create_jit_graph\n graph, torch_out = _trace_and_get_graph_from_model(model, args)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \".venv/lib/python3.12/site-packages/torch/onnx/utils.py\", line 904, in _trace_and_get_graph_from_model\n trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph(\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \".venv/lib/python3.12/site-packages/torch/jit/_trace.py\", line 1500, in _get_trace_graph\n outs = ONNXTracedModule(\n ^^^^^^^^^^^^^^^^^\n File \".venv/lib/python3.12/site-packages/torch/nn/modules/module.py\", line 1736, in _wrapped_call_impl\n return self._call_impl(*args, **kwargs)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \".venv/lib/python3.12/site-packages/torch/nn/modules/module.py\", line 1747, in _call_impl\n return forward_call(*args, **kwargs)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \".venv/lib/python3.12/site-packages/torch/jit/_trace.py\", line 106, in forward\n in_vars, in_desc = _flatten(args)\n ^^^^^^^^^^^^^^\nRuntimeError: Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted, but their usage is not recommended. Here, received an input of unsupported type: NestedTensor\n"
},
"transformer": {
"rt_mode": null,
"children": null,
"input_shapes": null,
"input_args": null,
"class_name": "Transformer",
"module_name": "models.transformer",
"status": "failed",
"error": "Dynamic shape loading is not implemented yet.",
"traceback": "Traceback (most recent call last):\n File \".venv/lib/python3.12/site-packagesaibooster/intelligence/acuirt/convert/converter/convert_auto.py\", line 70, in auto_convert2trt\n summary = CONVERSION_REGISTRY[conversion_mode](\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \".venv/lib/python3.12/site-packagesaibooster/intelligence/acuirt/convert/converter/convert_onnx.py\", line 68, in convert_trt_with_onnx\n args, keywords = input_args[0]\n ~~~~~~~~~~^^^\n File \".venv/lib/python3.12/site-packagesaibooster/intelligence/acuirt/convert/variable_cache.py\", line 128, in __getitem__\n raise NotImplementedError(\"Dynamic shape loading is not implemented yet.\")\nNotImplementedError: Dynamic shape loading is not implemented yet.\n"
},
"transformer.encoder": {
"rt_mode": null,
"children": null,
"input_shapes": null,
"input_args": null,
"class_name": "TransformerEncoder",
"module_name": "models.transformer",
"status": "failed",
"error": "Dynamic shape loading is not implemented yet.",
"traceback": "Traceback (most recent call last):\n File \".venv/lib/python3.12/site-packagesaibooster/intelligence/acuirt/convert/converter/convert_auto.py\", line 70, in auto_convert2trt\n summary = CONVERSION_REGISTRY[conversion_mode](\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \".venv/lib/python3.12/site-packagesaibooster/intelligence/acuirt/convert/converter/convert_onnx.py\", line 68, in convert_trt_with_onnx\n args, keywords = input_args[0]\n ~~~~~~~~~~^^^\n File \".venv/lib/python3.12/site-packagesaibooster/intelligence/acuirt/convert/variable_cache.py\", line 128, in __getitem__\n raise NotImplementedError(\"Dynamic shape loading is not implemented yet.\")\nNotImplementedError: Dynamic shape loading is not implemented yet.\n"
},
}

変換レポートから変換失敗原因を特定する

レポート情報を確認すると 変換エラーは下記の3パターンに分類できます。

1. Dynamic Shape の検出

Error: Dynamic shape loading is not implemented yet.

  • 原因: AcuiRTがモジュールへの入力を解析した際、shapeが動的に変化している(Dynamic Shape)ことを検知しました。
  • 背景: DETRは任意の画像サイズを受け入れますが、最適化(TensorRT化)の観点からは固定サイズが推奨されます。

2. 未サポートの入力型 (NestedTensor)

Error: <class 'ValueError'>: Unsupported input type <class 'util.misc.NestedTensor'>

  • 原因: ONNX形式への出力時に、NestedTensor という独自クラスが入力されているためエラーとなっています。
  • 背景: ONNX変換において、入力は基本的に torch.Tensor(またはそのリスト・辞書)である必要があります。独自のオブジェクトは解釈できません。

3. 不適切な出力形式 (辞書型など)

Error: 'str' object has no attribute 'detach'

  • 原因: AcuiRTの内部処理において、予期しないデータ構造(辞書型など)が返却されたことに起因します。
  • 背景: TensorRTエンジンの出力は、元のPyTorch実装に関わらず torch.Tensor のリストとなります。辞書型などのリッチな構造は仕様と整合しません。

次のステップでは、これらのエラーを修正し、DETRをAcuiRTで正常にコンパイル可能な状態にするための具体的なリファクタリング手順を解説します。