‘tensorrt.tensorrt.Builder‘ object has no attribute ‘build_cuda_engine‘

问题简述:

AttributeError: 'tensorrt.tensorrt.Builder' object has no attribute 'build_cuda_engine'

原因:

tensorrt版本大于等于7时,不再使用build_cuda_engine。

解决办法:

print('[info] Building an engine...')
if TRT_VERSION_MAJOR >= 7:
  # Create an optimization profile (see Section 7.2 of https://docs.nvidia.com/deeplearning/sdk/pdf/TensorRT-Developer-Guide.pdf).
  profile = builder.create_optimization_profile()
  # FIXME: Hardcoded for ImageNet. The minimum/optimum/maximum dimensions of a dynamic input tensor are the same.
  # profile.set_shape(input_tensor_name, (1, 3, 224, 224), (max_batch_size, 3, 224, 224), (max_batch_size, 3, 224, 224))

  config = builder.create_builder_config()
  config.add_optimization_profile(profile)

  trt_model_engine  = builder.build_engine(network, config)
  trt_model_context = trt_model_engine.create_execution_context()

else:
  # if trt version is below 7 then use the build_cuda_engine
  trt_model_engine  = builder.build_cuda_engine(network)
  trt_model_context = trt_model_engine.create_execution_context()

# 来源:
# https://www.codegrepper.com/code-examples/python/AttributeError%3A+%27tensorrt.tensorrt.Builder%27+object+has+no+attribute+%27build_cuda_engine%27

### 解决 `ICudaEngine` 对象无 `num_bindings` 属性错误 遇到 `AttributeError: 'NoneType' object has no attribute 'num_bindings'` 的情况通常意味着创建的引擎对象为空,即 `ICudaEngine` 实际上是一个 `NoneType` 而不是预期的对象[^1]。 为了确保能够成功获取到有效的 `ICudaEngine` 并访问其属性如 `num_bindings`,建议按照如下方法排查并解决问题: #### 1. 验证 TensorRT 安装环境 确认安装了适用于当前系统的 PyCUDATensorRT 版本。如果存在兼容性问题或未正确安装这些依赖库,则可能导致无法正常加载所需模块或类实例化失败。对于某些特定版本组合下的已知问题,可以通过更新至最新稳定版来尝试修复[^2]。 ```bash pip install pycuda tensorrt==8.x.x # 使用与操作系统匹配的具体版本号替换 x.x.x ``` #### 2. 构建序列化模型文件 (UFF/ONNX) 当从其他框架(例如 PyTorch)转换而来的网络结构被用于构建 TensorRT 引擎时,应先将其导出为 UFF 或 ONNX 格式的中间表示形式。这一步骤有助于简化后续处理流程,并减少因跨平台差异引起的问题发生几率[^3]。 #### 3. 创建 Builder 及配置参数设置 在初始化 TensorRT builder 后,需合理设定各项参数以适应目标硬件特性及性能需求。注意不同版本间 API 接口可能存在变动;针对较新版本可能需要调整相应选项名称或使用方式。 ```python import tensorrt as trt TRT_LOGGER = trt.Logger(trt.Logger.WARNING) with trt.Builder(TRT_LOGGER) as builder, \ builder.create_network() as network, \ trt.OnnxParser(network, TRT_LOGGER) as parser: with open('model.onnx', 'rb') as model_file: parsed_successfully = parser.parse(model_file.read()) if not parsed_successfully: for error in range(parser.num_errors): print(parser.get_error(error)) config = builder.create_builder_config() profile = builder.create_optimization_profile() engine = builder.build_serialized_network(network, config) ``` #### 4. 序列化保存和反序列化读取 Engine 文件 通过上述过程得到的优化后引擎可以直接序列化存储以便重复利用,从而加快部署速度。而在实际应用环境中则只需负责加载预编译好的二进制数据即可快速恢复完整的推理能力。 ```python with open("engine.trt", "wb") as f: f.write(engine.serialize()) # 加载部分... with open("engine.trt", "rb") as f, trt.Runtime(TRT_LOGGER) as runtime: engine = runtime.deserialize_cuda_engine(f.read()) if engine is None: raise RuntimeError("Failed to deserialize the CUDA engine.") print(f"Number of bindings: {engine.num_bindings}") # 正常情况下这里应该能打印出绑定数量而非抛异常 ```
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值