快捷方式

torch_tensorrt.dynamo

函式

torch_tensorrt.dynamo.compile(exported_program: ExportedProgram, inputs: Optional[Sequence[Sequence[Any]]] = None, *, arg_inputs: Optional[Sequence[Sequence[Any]]] = None, kwarg_inputs: Optional[dict[Any, Any]] = None, device: Optional[Union[Device, device, str]] = None, disable_tf32: bool = False, assume_dynamic_shape_support: bool = False, sparse_weights: bool = False, enabled_precisions: Union[Set[Union[dtype, dtype]], Tuple[Union[dtype, dtype]]] = {dtype.f32}, engine_capability: EngineCapability = EngineCapability.STANDARD, debug: bool = False, num_avg_timing_iters: int = 1, workspace_size: int = 0, dla_sram_size: int = 1048576, dla_local_dram_size: int = 1073741824, dla_global_dram_size: int = 536870912, truncate_double: bool = False, require_full_compilation: bool = False, min_block_size: int = 5, torch_executed_ops: Optional[Collection[Union[Callable[[...], Any], str]]] = None, torch_executed_modules: Optional[List[str]] = None, pass_through_build_failures: bool = False, max_aux_streams: Optional[int] = None, version_compatible: bool = False, optimization_level: Optional[int] = None, use_python_runtime: bool = False, use_fast_partitioner: bool = True, enable_experimental_decompositions: bool = False, dryrun: bool = False, hardware_compatible: bool = False, timing_cache_path: str = '/tmp/torch_tensorrt_engine_cache/timing_cache.bin', lazy_engine_init: bool = False, cache_built_engines: bool = False, reuse_cached_engines: bool = False, engine_cache_dir: str = '/tmp/torch_tensorrt_engine_cache', engine_cache_size: int = 5368709120, custom_engine_cache: Optional[BaseEngineCache] = None, use_explicit_typing: bool = False, use_fp32_acc: bool = False, refit_identical_engine_weights: bool = False, strip_engine_weights: bool = False, immutable_weights: bool = True, enable_weight_streaming: bool = False, tiling_optimization_level: str = 'none', l2_limit_for_tiling: int = - 1, **kwargs: Any) GraphModule[source]

使用 TensorRT 為 NVIDIA GPU 編譯 ExportedProgram 模組

接受一個現有的 TorchScript 模組和一組配置編譯器的設定,並將方法轉換為呼叫等效 TensorRT 引擎的 JIT Graphs

專門轉換 TorchScript 模組的 forward 方法

引數
  • exported_program (torch.export.ExportedProgram) – 源模組,對 torch.nn.Module 執行 torch.export 得到

  • inputs (Tuple[Any, ...]) –

    模組輸入的輸入形狀、dtype 和記憶體佈局規格列表。此引數為必需。輸入大小可以指定為 torch sizes、tuples 或 lists。dtype 可以使用 torch 資料型別或 torch_tensorrt 資料型別指定,您可以使用 torch 裝置或 torch_tensorrt 裝置型別列舉來選擇裝置型別。

    inputs=[
        torch_tensorrt.Input((1, 3, 224, 224)), # Static NCHW input shape for input #1
        torch_tensorrt.Input(
            min_shape=(1, 224, 224, 3),
            opt_shape=(1, 512, 512, 3),
            max_shape=(1, 1024, 1024, 3),
            dtype=torch.int32
            format=torch.channel_last
        ), # Dynamic input shape for input #2
        torch.randn((1, 3, 224, 244)) # Use an example tensor and let torch_tensorrt infer settings
    ]
    

關鍵字引數
  • arg_inputs (Tuple[Any, ...]) – 同 inputs。與 kwarg_inputs 一起使用以便更好地理解的別名。

  • kwarg_inputs (dict[Any, ...]) – 可選的模組 forward 函式關鍵字輸入。

  • device (Union(Device, torch.device, dict)) –

    TensorRT 引擎的目標執行裝置

    device=torch_tensorrt.Device("dla:1", allow_gpu_fallback=True)
    

  • disable_tf32 (布林值) – 強制 FP32 層使用傳統的 FP32 格式,而不是預設的行為(在乘法前將輸入舍入到 10 位尾數,但使用 23 位尾數累積求和)

  • assume_dynamic_shape_support (布林值) – 將此設定為 true 可使轉換器支援動態和靜態形狀。預設值:False

  • sparse_weights (布林值) – 為卷積層和全連線層啟用稀疏性。

  • enabled_precision (Set(Union(torch.dpython:type, torch_tensorrt.dpython:type))) – TensorRT 在選擇核心時可以使用的資料型別集合

  • debug (布林值) – 啟用可除錯引擎

  • capability (EngineCapability) – 將核心選擇限制為安全的 GPU 核心或安全的 DLA 核心

  • num_avg_timing_iters (整型) – 用於選擇核心的平均計時迭代次數

  • workspace_size (整型) – 提供給 TensorRT 的最大工作空間大小

  • dla_sram_size (整型) – DLA 用於層內通訊的快速軟體管理 RAM。

  • dla_local_dram_size (整型) – DLA 用於跨操作共享中間張量資料的本地 DRAM(主機 RAM)

  • dla_global_dram_size (整型) – DLA 用於儲存權重和元資料以進行執行的全域性 DRAM(主機 RAM)

  • truncate_double (布林值) – 將以 double (float64) 提供的權重截斷為 float32

  • calibrator (Union(torch_tensorrt._C.IInt8Calibrator, tensorrt.IInt8Calibrator)) – 校準器物件,將向 PTQ 系統提供資料用於 INT8 校準

  • require_full_compilation (布林值) – 要求模組進行端到端編譯,否則返回錯誤,而不是返回一個混合圖(其中無法在 TensorRT 中執行的操作在 PyTorch 中執行)

  • min_block_size (整型) – 為了在 TensorRT 中執行一組操作,連續的可轉換為 TensorRT 的操作的最小數量

  • torch_executed_ops (Collection[Target]) – 必須在 PyTorch 中執行的 aten 運算子集。如果此集合非空但 require_full_compilation 為 True,則會丟擲錯誤

  • torch_executed_modules (列表[字串]) – 必須在 PyTorch 中執行的模組列表。如果此列表非空但 require_full_compilation 為 True,則會丟擲錯誤

  • pass_through_build_failures (布林值) – 如果編譯期間出現問題則報錯(僅適用於 torch.compile 工作流程)

  • max_aux_stream (可選的[整型]) – 引擎中的最大輔助流數量

  • version_compatible (布林值) – 構建與未來版本的 TensorRT 相容的 TensorRT 引擎(限制為精簡執行時運算子,以便為引擎提供版本向前相容性)

  • optimization_level – (可選的[整型]):設定更高的最佳化級別允許 TensorRT 花費更長的引擎構建時間來尋找更多最佳化選項。與較低最佳化級別構建的引擎相比,由此生成的引擎可能具有更好的效能。預設最佳化級別為 3。有效值包括從 0 到當前最大最佳化級別 5 的整數。將其設定為大於最大級別將導致與最大級別相同的行為。

  • use_python_runtime – (布林值):使用純 Python 執行時返回圖,減少了序列化選項

  • use_fast_partitioner – (布林值):使用基於鄰接的劃分方案而不是全域性劃分器。鄰接劃分速度更快,但可能不是最優的。如果尋求最佳效能,請使用全域性劃分器(False

  • enable_experimental_decompositions (布林值) – 使用完整的運算子分解集。這些分解可能未經測試,但有助於使圖更容易轉換為 TensorRT,潛在地增加在 TensorRT 中執行的圖的數量。

  • dryrun (布林值) – “空執行”模式開關,執行除轉換為 TRT 和日誌輸出之外的所有內容

  • hardware_compatible (布林值) – 構建與構建引擎的 GPU 架構之外的其他 GPU 架構相容的 TensorRT 引擎(目前適用於 NVIDIA Ampere 及更新的架構)

  • timing_cache_path (字串) – 如果存在則為計時快取的路徑,或編譯後將儲存計時快取的路徑

  • lazy_engine_init (布林值) – 推遲引擎設定直到所有引擎編譯完成。這可以允許具有多個圖中斷的大型模型進行編譯,但在執行時可能導致 GPU 記憶體過載。

  • cache_built_engines (布林值) – 是否將編譯好的 TRT 引擎儲存到儲存中

  • reuse_cached_engines (布林值) – 是否從儲存載入快取的 TRT 引擎

  • engine_cache_dir (可選的[字串]) – 儲存快取的 TRT 引擎的目錄

  • engine_cache_size (可選的[整型]) – 用於引擎快取的最大硬碟空間(位元組),預設值為 1GB。如果快取超出此大小,則預設會移除最舊的引擎

  • custom_engine_cache (Optional[BaseEngineCache]) – 用於儲存和載入引擎的引擎快取例項。使用者可以透過繼承 BaseEngineCache 提供自己的引擎快取。如果使用此引數,則 engine_cache_dir 和 engine_cache_size 將被忽略。

  • use_explicit_typing (布林值) – 此標誌在 TensorRT 編譯中啟用強型別,該型別會遵守 PyTorch 模型中設定的精度。當用戶具有混合精度圖時,這很有用。

  • use_fp32_acc (布林值) – 此選項在 matmul 層周圍插入轉換為 FP32 的節點,TensorRT 確保 matmul 的累積在 FP32 中進行。僅當在 enabled_precisions 中配置了 FP16 精度時使用此選項。

  • refit_identical_engine_weights (布林值) – 使用相同權重對引擎進行重新適配。當使用不同輸入多次編譯同一個模型且權重相同時,這很有用。這將透過對不同輸入重用相同的引擎來節省時間。

  • strip_engine_weights (布林值) – 從序列化的引擎中剝離引擎權重。當引擎部署在不需要權重的環境中時,這很有用。

  • immutable_weights (布林值) – 構建不可重新適配的引擎。這對於某些不可重新適配的層很有用。如果此引數設定為 true,則 strip_engine_weightsrefit_identical_engine_weights 將被忽略。

  • enable_weight_streaming (布林值) – 啟用權重流式傳輸。

  • tiling_optimization_level (字串) – 平鋪策略的最佳化級別。更高的級別允許 TensorRT 花費更多時間搜尋更好的平鋪策略。我們當前支援 [“none”, “fast”, “moderate”, “full”]。

  • l2_limit_for_tiling (整型) – 平鋪最佳化的目標 L2 快取使用限制(位元組)(預設值為 -1,表示無限制)。

  • **kwargs – 任意型別,

返回值

編譯後的 FX 模組,執行時將透過 TensorRT 執行

返回型別

torch.fx.GraphModule

torch_tensorrt.dynamo.trace(mod: torch.nn.modules.module.Module | torch.fx.graph_module.GraphModule, inputs: Optional[Tuple[Any, ...]] = None, *, arg_inputs: Optional[Tuple[Any, ...]] = None, kwarg_inputs: Optional[dict[Any, Any]] = None, **kwargs: Any) ExportedProgram[source]

torch.nn.Moduletorch.fx.GraphModule 匯出 torch.export.ExportedProgram,專門針對使用 Torch-TensorRT 進行編譯。

torch.nn.Moduletorch.fx.GraphModule 中匯出 torch.export.ExportedProgram。執行特定於 Torch-TensorRT 的 dynamo 前端編譯的運算元分解。

引數
  • mod (torch.nn.Module | torch.fx.GraphModule) – 源模組,稍後將由 Torch-TensorRT 的 dynamo 前端編譯。

  • inputs (Tuple[Any, ...]) –

    模組輸入的輸入形狀、dtype 和記憶體佈局規格列表。此引數為必需。輸入大小可以指定為 torch sizes、tuples 或 lists。dtype 可以使用 torch 資料型別或 torch_tensorrt 資料型別指定,您可以使用 torch 裝置或 torch_tensorrt 裝置型別列舉來選擇裝置型別。

    input=[
        torch_tensorrt.Input((1, 3, 224, 224)), # Static NCHW input shape for input #1
        torch_tensorrt.Input(
            min_shape=(1, 224, 224, 3),
            opt_shape=(1, 512, 512, 3),
            max_shape=(1, 1024, 1024, 3),
            dtype=torch.int32
            format=torch.channel_last
        ), # Dynamic input shape for input #2
        torch.randn((1, 3, 224, 244)) # Use an example tensor and let torch_tensorrt infer settings
    ]
    

關鍵字引數
  • arg_inputs (Tuple[Any, ...]) – 同 inputs。與 kwarg_inputs 一起使用以便更好地理解的別名。

  • kwarg_inputs (dict[Any, ...]) – 可選的模組 forward 函式關鍵字輸入。

  • device (Union(torch.device, dict)) –

    TensorRT 引擎的目標執行裝置

    device=torch.device("cuda:0")
    

  • debug (布林值) – 啟用可除錯引擎

  • enable_experimental_decompositions (布林值) – 使用完整的運算子分解集。這些分解可能未經測試,但有助於使圖更容易轉換為 TensorRT,潛在地增加在 TensorRT 中執行的圖的數量。

  • **kwargs – 任意型別,

返回值

編譯後的 FX 模組,執行時將透過 TensorRT 執行

返回型別

torch.fx.GraphModule

torch_tensorrt.dynamo.export(gm: GraphModule, cross_compile_flag: Optional[bool] = False) ExportedProgram[source]

將 TensorRT 編譯的結果匯出為所需的輸出格式。

引數
  • gm (torch.fx.GraphModule) – 編譯後的 Torch-TensorRT 模組,由 torch_tensorrt.dynamo.compile 生成。

  • inputs (torch.Tensor) – PyTorch 輸入張量。

  • cross_compile_flag (bool) – 指示是否啟用交叉編譯的標誌。

torch_tensorrt.dynamo.convert_exported_program_to_serialized_trt_engine(exported_program: ExportedProgram, inputs: Optional[Sequence[Sequence[Any]]] = None, *, arg_inputs: Optional[Sequence[Sequence[Any]]] = None, kwarg_inputs: Optional[dict[Any, Any]] = None, enabled_precisions: Union[Set[Union[dtype, dtype]], Tuple[Union[dtype, dtype]]] = {dtype.f32}, debug: bool = False, assume_dynamic_shape_support: bool = False, workspace_size: int = 0, min_block_size: int = 5, torch_executed_ops: Optional[Set[str]] = None, pass_through_build_failures: bool = False, max_aux_streams: Optional[int] = None, version_compatible: bool = False, optimization_level: Optional[int] = None, use_python_runtime: Optional[bool] = False, truncate_double: bool = False, use_fast_partitioner: bool = True, enable_experimental_decompositions: bool = False, device: Device = Device(type=DeviceType.GPU, gpu_id=0), require_full_compilation: bool = False, disable_tf32: bool = False, sparse_weights: bool = False, engine_capability: EngineCapability = EngineCapability.STANDARD, num_avg_timing_iters: int = 1, dla_sram_size: int = 1048576, dla_local_dram_size: int = 1073741824, dla_global_dram_size: int = 536870912, calibrator: object = None, allow_shape_tensors: bool = False, timing_cache_path: str = '/tmp/torch_tensorrt_engine_cache/timing_cache.bin', use_explicit_typing: bool = False, use_fp32_acc: bool = False, refit_identical_engine_weights: bool = False, strip_engine_weights: bool = False, immutable_weights: bool = True, enable_weight_streaming: bool = False, tiling_optimization_level: str = 'none', l2_limit_for_tiling: int = - 1, **kwargs: Any) bytes[source]

將 ExportedProgram 轉換為序列化的 TensorRT 引擎。

根據轉換設定字典,將 ExportedProgram 轉換為序列化的 TensorRT 引擎。

引數

exported_program (torch.export.ExportedProgram) – 源模組。

關鍵字引數
  • inputs (Optional[Sequence[Input | torch.Tensor]]) –

    必需 模組輸入形狀、資料型別和記憶體佈局規範列表。此引數是必需的。輸入大小可以指定為 PyTorch 大小、元組或列表。資料型別可以使用 PyTorch 資料型別或 torch_tensorrt 資料型別指定,您可以使用 PyTorch 裝置或 torch_tensorrt 裝置型別列舉來選擇裝置型別。

    inputs=[
          torch_tensorrt.Input((1, 3, 224, 224)), # Static NCHW input shape for input #1
          torch_tensorrt.Input(
              min_shape=(1, 224, 224, 3),
              opt_shape=(1, 512, 512, 3),
              max_shape=(1, 1024, 1024, 3),
              dtype=torch.int32
              format=torch.channel_last
          ), # Dynamic input shape for input #2
          torch.randn((1, 3, 224, 244)) # Use an example tensor and let torch_tensorrt infer settings
      ]
    

  • enabled_precisions (Optional[Set[torch.dpython:type | _enums.dpython:type]]) – TensorRT 可以使用的資料型別集合。

  • debug (bool) – 是否列印詳細的除錯資訊。

  • workspace_size (python:int) – TensorRT 允許為模組使用的工作空間大小(預設為 0)。

  • min_block_size (python:int) – 每個 TensorRT 引擎塊的最小運算元數量。

  • torch_executed_ops (Set[str]) – 一組無論轉換器是否覆蓋都將在 PyTorch 中執行的運算元。

  • pass_through_build_failures (bool) – 是否在 TensorRT 引擎構建錯誤時失敗 (True) 或不失敗 (False)。

  • max_aux_streams (Optional[python:int]) – 每個引擎允許的最大輔助 TensorRT 流數量。

  • version_compatible (bool) – 為引擎計劃檔案提供版本前向相容性。

  • optimization_level (Optional[python:int]) – 構建器最佳化級別 0-5,級別越高意味著構建時間越長,會搜尋更多的最佳化選項。TensorRT 預設為 3。

  • use_python_runtime (Optional[bool]) – 是否嚴格使用 Python 執行時或 C++ 執行時。要根據 C++ 依賴的存在性自動選擇執行時(優先選擇 C++ 執行時,如果可用),請將此引數留空 None。

  • truncate_double (bool) – 是否將 float64 TensorRT 引擎輸入或權重截斷為 float32。

  • use_fast_partitioner (bool) – 是否使用快速或全域性圖分割槽系統。

  • enable_experimental_decompositions (bool) – 是否啟用所有核心 aten 分解,還是隻啟用選定的子集。

  • device (Device) – 用於編譯模型的 GPU。

  • require_full_compilation (bool) – 是否要求在 TensorRT 中完全編譯圖。僅適用於 ir=”dynamo”;對 torch.compile 路徑無效。

  • disable_tf32 (bool) – 是否停用 TensorRT 層的 TF32 計算。

  • sparse_weights (bool) – 是否允許構建器使用稀疏權重。

  • engine_capability (trt.EngineCapability) – 將核心選擇限制為安全的 GPU 核心或安全的 DLA 核心。

  • num_avg_timing_iters (整型) – 用於選擇核心的平均計時迭代次數

  • dla_sram_size (整型) – DLA 用於層內通訊的快速軟體管理 RAM。

  • dla_local_dram_size (整型) – DLA 用於跨操作共享中間張量資料的本地 DRAM(主機 RAM)

  • dla_global_dram_size (整型) – DLA 用於儲存權重和元資料以進行執行的全域性 DRAM(主機 RAM)

  • calibrator (Union(torch_tensorrt._C.IInt8Calibrator, tensorrt.IInt8Calibrator)) – 校準器物件,將向 PTQ 系統提供資料用於 INT8 校準

  • allow_shape_tensors – (實驗性)允許 aten::size 使用 TensorRT 中的 IShapeLayer 輸出形狀張量。

  • timing_cache_path (字串) – 如果存在則為計時快取的路徑,或編譯後將儲存計時快取的路徑

  • use_explicit_typing (布林值) – 此標誌在 TensorRT 編譯中啟用強型別,該型別會遵守 PyTorch 模型中設定的精度。當用戶具有混合精度圖時,這很有用。

  • use_fp32_acc (布林值) – 此選項在 matmul 層周圍插入轉換為 FP32 的節點,TensorRT 確保 matmul 的累積在 FP32 中進行。僅當在 enabled_precisions 中配置了 FP16 精度時使用此選項。

  • refit_identical_engine_weights (布林值) – 使用相同權重對引擎進行重新適配。當使用不同輸入多次編譯同一個模型且權重相同時,這很有用。這將透過對不同輸入重用相同的引擎來節省時間。

  • strip_engine_weights (布林值) – 從序列化的引擎中剝離引擎權重。當引擎部署在不需要權重的環境中時,這很有用。

  • immutable_weights (布林值) – 構建不可重新適配的引擎。這對於某些不可重新適配的層很有用。如果此引數設定為 true,則 strip_engine_weightsrefit_identical_engine_weights 將被忽略。

  • enable_weight_streaming (布林值) – 啟用權重流式傳輸。

  • tiling_optimization_level (字串) – 平鋪策略的最佳化級別。更高的級別允許 TensorRT 花費更多時間搜尋更好的平鋪策略。我們當前支援 [“none”, “fast”, “moderate”, “full”]。

  • l2_limit_for_tiling (整型) – 平鋪最佳化的目標 L2 快取使用限制(位元組)(預設值為 -1,表示無限制)。

返回值

序列化的 TensorRT 引擎,可以儲存到檔案或透過 TensorRT API 反序列化。

返回型別

位元組

torch_tensorrt.dynamo.cross_compile_for_windows(exported_program: ExportedProgram, inputs: Optional[Sequence[Sequence[Any]]] = None, *, arg_inputs: Optional[Sequence[Sequence[Any]]] = None, kwarg_inputs: Optional[dict[Any, Any]] = None, device: Optional[Union[Device, device, str]] = None, disable_tf32: bool = False, assume_dynamic_shape_support: bool = False, sparse_weights: bool = False, enabled_precisions: Union[Set[Union[dtype, dtype]], Tuple[Union[dtype, dtype]]] = {dtype.f32}, engine_capability: EngineCapability = EngineCapability.STANDARD, debug: bool = False, num_avg_timing_iters: int = 1, workspace_size: int = 0, dla_sram_size: int = 1048576, dla_local_dram_size: int = 1073741824, dla_global_dram_size: int = 536870912, truncate_double: bool = False, require_full_compilation: bool = False, min_block_size: int = 5, torch_executed_ops: Optional[Collection[Union[Callable[[...], Any], str]]] = None, torch_executed_modules: Optional[List[str]] = None, pass_through_build_failures: bool = False, max_aux_streams: Optional[int] = None, version_compatible: bool = False, optimization_level: Optional[int] = None, use_python_runtime: bool = False, use_fast_partitioner: bool = True, enable_experimental_decompositions: bool = False, dryrun: bool = False, hardware_compatible: bool = False, timing_cache_path: str = '/tmp/torch_tensorrt_engine_cache/timing_cache.bin', lazy_engine_init: bool = False, cache_built_engines: bool = False, reuse_cached_engines: bool = False, engine_cache_dir: str = '/tmp/torch_tensorrt_engine_cache', engine_cache_size: int = 5368709120, custom_engine_cache: Optional[BaseEngineCache] = None, use_explicit_typing: bool = False, use_fp32_acc: bool = False, refit_identical_engine_weights: bool = False, strip_engine_weights: bool = False, immutable_weights: bool = True, enable_weight_streaming: bool = False, tiling_optimization_level: str = 'none', l2_limit_for_tiling: int = - 1, **kwargs: Any) GraphModule[source]

在 Linux 中使用 TensorRT 編譯 ExportedProgram 模組,以便在 Windows 中進行推理

接受一個匯出的程式和一組用於配置編譯器的設定,它將把方法轉換為呼叫等效 TensorRT 引擎的 AOT 圖

引數
  • exported_program (torch.export.ExportedProgram) – 源模組,對 torch.nn.Module 執行 torch.export 得到

  • inputs (Tuple[Any, ...]) –

    模組輸入的輸入形狀、dtype 和記憶體佈局規格列表。此引數為必需。輸入大小可以指定為 torch sizes、tuples 或 lists。dtype 可以使用 torch 資料型別或 torch_tensorrt 資料型別指定,您可以使用 torch 裝置或 torch_tensorrt 裝置型別列舉來選擇裝置型別。

    inputs=[
        torch_tensorrt.Input((1, 3, 224, 224)), # Static NCHW input shape for input #1
        torch_tensorrt.Input(
            min_shape=(1, 224, 224, 3),
            opt_shape=(1, 512, 512, 3),
            max_shape=(1, 1024, 1024, 3),
            dtype=torch.int32
            format=torch.channel_last
        ), # Dynamic input shape for input #2
        torch.randn((1, 3, 224, 244)) # Use an example tensor and let torch_tensorrt infer settings
    ]
    

關鍵字引數
  • arg_inputs (Tuple[Any, ...]) – 同 inputs。與 kwarg_inputs 一起使用以便更好地理解的別名。

  • kwarg_inputs (dict[Any, ...]) – 可選的模組 forward 函式關鍵字輸入。

  • device (Union(Device, torch.device, dict)) –

    TensorRT 引擎的目標執行裝置

    device=torch_tensorrt.Device("dla:1", allow_gpu_fallback=True)
    

  • disable_tf32 (布林值) – 強制 FP32 層使用傳統的 FP32 格式,而不是預設的行為(在乘法前將輸入舍入到 10 位尾數,但使用 23 位尾數累積求和)

  • assume_dynamic_shape_support (布林值) – 將此設定為 true 可使轉換器支援動態和靜態形狀。預設值:False

  • sparse_weights (布林值) – 為卷積層和全連線層啟用稀疏性。

  • enabled_precision (Set(Union(torch.dpython:type, torch_tensorrt.dpython:type))) – TensorRT 在選擇核心時可以使用的資料型別集合

  • debug (布林值) – 啟用可除錯引擎

  • capability (EngineCapability) – 將核心選擇限制為安全的 GPU 核心或安全的 DLA 核心

  • num_avg_timing_iters (整型) – 用於選擇核心的平均計時迭代次數

  • workspace_size (整型) – 提供給 TensorRT 的最大工作空間大小

  • dla_sram_size (整型) – DLA 用於層內通訊的快速軟體管理 RAM。

  • dla_local_dram_size (整型) – DLA 用於跨操作共享中間張量資料的本地 DRAM(主機 RAM)

  • dla_global_dram_size (整型) – DLA 用於儲存權重和元資料以進行執行的全域性 DRAM(主機 RAM)

  • truncate_double (布林值) – 將以 double (float64) 提供的權重截斷為 float32

  • calibrator (Union(torch_tensorrt._C.IInt8Calibrator, tensorrt.IInt8Calibrator)) – 校準器物件,將向 PTQ 系統提供資料用於 INT8 校準

  • require_full_compilation (布林值) – 要求模組進行端到端編譯,否則返回錯誤,而不是返回一個混合圖(其中無法在 TensorRT 中執行的操作在 PyTorch 中執行)

  • min_block_size (整型) – 為了在 TensorRT 中執行一組操作,連續的可轉換為 TensorRT 的操作的最小數量

  • torch_executed_ops (Collection[Target]) – 必須在 PyTorch 中執行的 aten 運算子集。如果此集合非空但 require_full_compilation 為 True,則會丟擲錯誤

  • torch_executed_modules (列表[字串]) – 必須在 PyTorch 中執行的模組列表。如果此列表非空但 require_full_compilation 為 True,則會丟擲錯誤

  • pass_through_build_failures (布林值) – 如果編譯期間出現問題則報錯(僅適用於 torch.compile 工作流程)

  • max_aux_stream (可選的[整型]) – 引擎中的最大輔助流數量

  • version_compatible (布林值) – 構建與未來版本的 TensorRT 相容的 TensorRT 引擎(限制為精簡執行時運算子,以便為引擎提供版本向前相容性)

  • optimization_level – (可選的[整型]):設定更高的最佳化級別允許 TensorRT 花費更長的引擎構建時間來尋找更多最佳化選項。與較低最佳化級別構建的引擎相比,由此生成的引擎可能具有更好的效能。預設最佳化級別為 3。有效值包括從 0 到當前最大最佳化級別 5 的整數。將其設定為大於最大級別將導致與最大級別相同的行為。

  • use_python_runtime – (布林值):使用純 Python 執行時返回圖,減少了序列化選項

  • use_fast_partitioner – (布林值):使用基於鄰接的劃分方案而不是全域性劃分器。鄰接劃分速度更快,但可能不是最優的。如果尋求最佳效能,請使用全域性劃分器(False

  • enable_experimental_decompositions (布林值) – 使用完整的運算子分解集。這些分解可能未經測試,但有助於使圖更容易轉換為 TensorRT,潛在地增加在 TensorRT 中執行的圖的數量。

  • dryrun (布林值) – “空執行”模式開關,執行除轉換為 TRT 和日誌輸出之外的所有內容

  • hardware_compatible (布林值) – 構建與構建引擎的 GPU 架構之外的其他 GPU 架構相容的 TensorRT 引擎(目前適用於 NVIDIA Ampere 及更新的架構)

  • timing_cache_path (字串) – 如果存在則為計時快取的路徑,或編譯後將儲存計時快取的路徑

  • lazy_engine_init (布林值) – 推遲引擎設定直到所有引擎編譯完成。這可以允許具有多個圖中斷的大型模型進行編譯,但在執行時可能導致 GPU 記憶體過載。

  • cache_built_engines (布林值) – 是否將編譯好的 TRT 引擎儲存到儲存中

  • reuse_cached_engines (布林值) – 是否從儲存載入快取的 TRT 引擎

  • engine_cache_dir (可選的[字串]) – 儲存快取的 TRT 引擎的目錄

  • engine_cache_size (可選的[整型]) – 用於引擎快取的最大硬碟空間(位元組),預設值為 1GB。如果快取超出此大小,則預設會移除最舊的引擎

  • custom_engine_cache (Optional[BaseEngineCache]) – 用於儲存和載入引擎的引擎快取例項。使用者可以透過繼承 BaseEngineCache 提供自己的引擎快取。如果使用此引數,則 engine_cache_dir 和 engine_cache_size 將被忽略。

  • use_explicit_typing (布林值) – 此標誌在 TensorRT 編譯中啟用強型別,該型別會遵守 PyTorch 模型中設定的精度。當用戶具有混合精度圖時,這很有用。

  • use_fp32_acc (布林值) – 此選項在 matmul 層周圍插入轉換為 FP32 的節點,TensorRT 確保 matmul 的累積在 FP32 中進行。僅當在 enabled_precisions 中配置了 FP16 精度時使用此選項。

  • refit_identical_engine_weights (布林值) – 使用相同權重對引擎進行重新適配。當使用不同輸入多次編譯同一個模型且權重相同時,這很有用。這將透過對不同輸入重用相同的引擎來節省時間。

  • strip_engine_weights (布林值) – 從序列化的引擎中剝離引擎權重。當引擎部署在不需要權重的環境中時,這很有用。

  • immutable_weights (布林值) – 構建不可重新適配的引擎。這對於某些不可重新適配的層很有用。如果此引數設定為 true,則 strip_engine_weightsrefit_identical_engine_weights 將被忽略。

  • enable_weight_streaming (布林值) – 啟用權重流式傳輸。

  • tiling_optimization_level (字串) – 平鋪策略的最佳化級別。更高的級別允許 TensorRT 花費更多時間搜尋更好的平鋪策略。我們當前支援 [“none”, “fast”, “moderate”, “full”]。

  • l2_limit_for_tiling (整型) – 平鋪最佳化的目標 L2 快取使用限制(位元組)(預設值為 -1,表示無限制)。

  • **kwargs – 任意型別,

返回值

編譯後的 FX 模組,執行時將透過 TensorRT 執行

返回型別

torch.fx.GraphModule

torch_tensorrt.dynamo.save_cross_compiled_exported_program(gm: GraphModule, file_path: str) None[source]

將交叉編譯的匯出程式儲存到磁碟。

引數
  • module (torch.fx.GraphModule) – 交叉編譯的 Torch-TensorRT 模組

  • file_path (str) – 匯出程式將儲存到磁碟的檔案路徑

torch_tensorrt.dynamo.load_cross_compiled_exported_program(file_path: str = '') Any[source]

在 Windows 中載入先前在 Linux 中交叉編譯的 ExportedProgram 檔案

引數

file_path (str) – 磁碟上的檔案路徑

丟擲

ValueError – 如果 API 不是在 Windows 中呼叫,或者沒有檔案,或者檔案不是有效的 ExportedProgram 檔案

torch_tensorrt.dynamo.refit_module_weights(*args: List[Any], **kwargs: Dict[str, Any]) Any

class torch_tensorrt.dynamo.CompilationSettings(enabled_precisions: ~typing.Set[~torch_tensorrt._enums.dtype] = <factory>, debug: bool = False, workspace_size: int = 0, min_block_size: int = 5, torch_executed_ops: ~typing.Collection[~typing.Union[~typing.Callable[[...], ~typing.Any], str]] = <factory>, pass_through_build_failures: bool = False, max_aux_streams: ~typing.Optional[int] = None, version_compatible: bool = False, optimization_level: ~typing.Optional[int] = None, use_python_runtime: ~typing.Optional[bool] = False, truncate_double: bool = False, use_fast_partitioner: bool = True, enable_experimental_decompositions: bool = False, device: ~torch_tensorrt._Device.Device = <factory>, require_full_compilation: bool = False, disable_tf32: bool = False, assume_dynamic_shape_support: bool = False, sparse_weights: bool = False, engine_capability: ~torch_tensorrt._enums.EngineCapability = <factory>, num_avg_timing_iters: int = 1, dla_sram_size: int = 1048576, dla_local_dram_size: int = 1073741824, dla_global_dram_size: int = 536870912, dryrun: ~typing.Union[bool, str] = False, hardware_compatible: bool = False, timing_cache_path: str = '/tmp/torch_tensorrt_engine_cache/timing_cache.bin', lazy_engine_init: bool = False, cache_built_engines: bool = False, reuse_cached_engines: bool = False, use_explicit_typing: bool = False, use_fp32_acc: bool = False, refit_identical_engine_weights: bool = False, strip_engine_weights: bool = False, immutable_weights: bool = True, enable_weight_streaming: bool = False, enable_cross_compile_for_windows: bool = False, tiling_optimization_level: str = 'none', l2_limit_for_tiling: int = -1, use_distributed_mode_trace: bool = False)[source]

Torch-TensorRT Dynamo 路徑的編譯設定

引數
  • enabled_precisions (Set[dpython:type]) – 可用的核心 dtype 精度

  • debug (bool) – 是否列印詳細的除錯資訊。

  • workspace_size (python:int) – TensorRT 允許為模組使用的工作空間大小(預設為 0)。

  • min_block_size (python:int) – 每個 TensorRT 引擎塊的最小運算元數量。

  • torch_executed_ops (Collection[Target]) – 要在 Torch 中執行的操作集合,無論轉換器覆蓋率如何

  • pass_through_build_failures (bool) – 是否在 TensorRT 引擎構建錯誤時失敗 (True) 或不失敗 (False)。

  • max_aux_streams (Optional[python:int]) – 每個引擎允許的最大輔助 TensorRT 流數量。

  • version_compatible (bool) – 為引擎計劃檔案提供版本前向相容性。

  • optimization_level (Optional[python:int]) – 構建器最佳化級別 0-5,級別越高意味著構建時間越長,會搜尋更多的最佳化選項。TensorRT 預設為 3。

  • use_python_runtime (Optional[bool]) – 是否嚴格使用 Python 執行時或 C++ 執行時。要根據 C++ 依賴的存在性自動選擇執行時(優先選擇 C++ 執行時,如果可用),請將此引數留空 None。

  • truncate_double (bool) – 是否將 float64 TensorRT 引擎輸入或權重截斷為 float32。

  • use_fast_partitioner (bool) – 是否使用快速或全域性圖分割槽系統。

  • enable_experimental_decompositions (bool) – 是否啟用所有核心 aten 分解,還是隻啟用選定的子集。

  • device (Device) – 用於編譯模型的 GPU。

  • require_full_compilation (bool) – 是否要求在 TensorRT 中完全編譯圖。僅適用於 ir=”dynamo”;對 torch.compile 路徑無效。

  • assume_dynamic_shape_support (布林值) – 將此設定為 true 可使轉換器支援動態和靜態形狀。預設值:False

  • disable_tf32 (bool) – 是否停用 TensorRT 層的 TF32 計算。

  • sparse_weights (bool) – 是否允許構建器使用稀疏權重。

  • engine_capability (trt.EngineCapability) – 將核心選擇限制為安全的 GPU 核心或安全的 DLA 核心。

  • num_avg_timing_iters (整型) – 用於選擇核心的平均計時迭代次數

  • dla_sram_size (整型) – DLA 用於層內通訊的快速軟體管理 RAM。

  • dla_local_dram_size (整型) – DLA 用於跨操作共享中間張量資料的本地 DRAM(主機 RAM)

  • dla_global_dram_size (整型) – DLA 用於儲存權重和元資料以進行執行的全域性 DRAM(主機 RAM)

  • dryrun (Union[bool, str]) – 切換“試執行”模式,該模式會執行所有分割槽操作,但不進行 TRT 引擎轉換。列印圖結構和分割槽性質的詳細日誌。如果指定了字串路徑,則可選擇將輸出儲存到檔案。

  • hardware_compatible (布林值) – 構建與構建引擎的 GPU 架構之外的其他 GPU 架構相容的 TensorRT 引擎(目前適用於 NVIDIA Ampere 及更新的架構)

  • timing_cache_path (字串) – 如果存在則為計時快取的路徑,或編譯後將儲存計時快取的路徑

  • cache_built_engines (布林值) – 是否將編譯好的 TRT 引擎儲存到儲存中

  • reuse_cached_engines (布林值) – 是否從儲存載入快取的 TRT 引擎

  • use_strong_typing (bool) – 此標誌在 TensorRT 編譯中啟用強型別檢查,它會遵循 Pytorch 模型中設定的精度。這對於使用者擁有混合精度圖的場景很有用。

  • use_fp32_acc (布林值) – 此選項在 matmul 層周圍插入轉換為 FP32 的節點,TensorRT 確保 matmul 的累積在 FP32 中進行。僅當在 enabled_precisions 中配置了 FP16 精度時使用此選項。

  • refit_identical_engine_weights (bool) – 是否使用相同的權重重新擬合引擎

  • strip_engine_weights (bool) – 是否剝離引擎權重

  • immutable_weights (bool) – 構建不可重新擬合的引擎。這對於一些不可重新擬合的層很有用。如果此引數設定為 True,則 strip_engine_weightsrefit_identical_engine_weights 將被忽略。

  • enable_weight_streaming (布林值) – 啟用權重流式傳輸。

  • enable_cross_compile_for_windows (bool) – 預設情況下,此引數為 False,表示 TensorRT 引擎只能在其構建的同一平臺上執行。設定為 True 將啟用跨平臺相容性,允許引擎在 Linux 上構建並在 Windows 上執行。

  • tiling_optimization_level (字串) – 平鋪策略的最佳化級別。更高的級別允許 TensorRT 花費更多時間搜尋更好的平鋪策略。我們當前支援 [“none”, “fast”, “moderate”, “full”]。

  • l2_limit_for_tiling (整型) – 平鋪最佳化的目標 L2 快取使用限制(位元組)(預設值為 -1,表示無限制)。

  • use_distributed_mode_trace (bool) – 使用 aot_autograd 來跟蹤圖。當分散式模型中存在 DTensors 或分散式張量時啟用此功能。

文件

查閱 PyTorch 的綜合開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源