check_tensorrt_usage.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. """
  3. 检查程序运行时是否使用了 TensorRT
  4. """
  5. import sys
  6. import re
  7. def check_log_for_tensorrt(log_file=None):
  8. """检查日志文件中是否有 TensorRT 相关信息"""
  9. if log_file:
  10. try:
  11. with open(log_file, 'r', encoding='utf-8') as f:
  12. content = f.read()
  13. except:
  14. print(f"无法读取日志文件: {log_file}")
  15. return False
  16. else:
  17. print("请提供日志文件路径,或直接查看程序输出")
  18. return None
  19. # 检查 TensorRT 相关关键词
  20. tensorrt_indicators = [
  21. r"Compiling models with TensorRT",
  22. r"TensorRT optimization completed",
  23. r"SuperPoint compiled with TensorRT",
  24. r"Precision: (fp16|fp32|int8)", # TensorRT 精度设置
  25. ]
  26. found = False
  27. for pattern in tensorrt_indicators:
  28. if re.search(pattern, content, re.IGNORECASE):
  29. print(f"✓ 找到 TensorRT 使用证据: {pattern}")
  30. found = True
  31. if not found:
  32. # 检查是否有警告信息
  33. if "Warning: TensorRT" in content or "Failed to compile with TensorRT" in content:
  34. print("✗ TensorRT 编译失败或未启用")
  35. else:
  36. print("✗ 未找到 TensorRT 使用证据")
  37. print(" 可能原因:")
  38. print(" 1. 运行时未使用 --use_tensorrt 参数")
  39. print(" 2. TensorRT 编译失败,回退到 PyTorch")
  40. print(" 3. 程序在 CPU 上运行(TensorRT 需要 CUDA)")
  41. return found
  42. def check_command_line():
  43. """检查命令行参数"""
  44. print("="*60)
  45. print("如何判断是否使用了 TensorRT:")
  46. print("="*60)
  47. print()
  48. print("1. 查看程序启动时的输出:")
  49. print(" 如果使用了 TensorRT,会看到:")
  50. print(" ============================================================")
  51. print(" Compiling models with TensorRT...")
  52. print(" Precision: fp16")
  53. print(" This may take several minutes on first run...")
  54. print(" ============================================================")
  55. print()
  56. print("2. 编译成功后会看到:")
  57. print(" ✓ SuperPoint compiled with TensorRT")
  58. print(" ✓ TensorRT optimization completed")
  59. print()
  60. print("3. 检查运行命令:")
  61. print(" 必须包含 --use_tensorrt 参数")
  62. print()
  63. print("4. 性能对比:")
  64. print(" - PyTorch FP16: ~22 FPS")
  65. print(" - TensorRT FP16: ~35-45 FPS")
  66. print(" 如果 FPS 明显提升,说明使用了 TensorRT")
  67. print()
  68. print("="*60)
  69. print("正确的运行命令应该是:")
  70. print("="*60)
  71. print()
  72. print('python demo_lightglue_camera_position_async.py ^')
  73. print(' --input "udp://0.0.0.0:12346" ^')
  74. print(' --max_keypoints 128 ^')
  75. print(' --use_fp16 ^')
  76. print(' --use_tensorrt ^')
  77. print(' --tensorrt_precision fp16')
  78. print()
  79. if __name__ == "__main__":
  80. if len(sys.argv) > 1:
  81. log_file = sys.argv[1]
  82. check_log_for_tensorrt(log_file)
  83. else:
  84. check_command_line()