You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

API_quant.md 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # OneFlow 量化推理
  2. ## OneFlow 中的 XRT
  3. XRT 是一个同时支持多个计算引擎的运行时加速库,目前已经集成了 TensorFlow XLA 和 NVIDIA
  4. TensorRT 两个后端引擎。其中 XLA 全面支持训练和预测,TensorRT 支持预测以及部分算子支持训练。对于同一个计算图,XRT 允许多个计算引擎联合使用,以获得更好的加速效果,其中 TensorRT 具有 Int8 量化功能。
  5. 由于 TensorRT 中官方支持的 op 并没有那么全面,其余自定义 op 有可能受接口限制,因此 OneFlow 后续会采用 plug-in 形式添加,支持更多算子。
  6. ## 在 OneFlow 中使用 TensorRT
  7. * 前期准备
  8. * 数据集:以测试 ResNet50 为例,需要提前准备 ImageNet 的 OFRecord 格式数据集。
  9. * 下载 TensorRT:编译时需要链接 TensorRT 的头文件和动态库,因此用户需要根据自己系统和已安装的 CUDA 版本选择相应版本的 TensorRT,同时满足 TensorRT 的其他依赖。
  10. * 下载 OneFlow-Benchmark:OneFlow-Benchmark 是 OneFlow 的模型基准仓库,提供了一系列完备实现的网络模型,本次测试选择的是其中的ResNet50。
  11. * 编译:编译时开启 -DWITH_TENSORRT 选项,并指定 TensorRT 源码解压后的所在路径
  12. ```
  13. cmake .. -DWITH_TENSORRT=ON -DTENSORRT_ROOT=/home/${user}/TensorRT-6.0.1.8 && make -j 24
  14. ```
  15. 或者可以在 cmake 前使用环境变量指定
  16. ```
  17. export TENSORRT_ROOT=/home/${user}/TensorRT-6.0.1.8
  18. ```
  19. 编译成功后即可安装支持 TensoRT 的 OneFlow。
  20. * 运行
  21. 目前 OneFlow 中的 TensorRT 仅支持单卡推理。编译成功后切换到 dev_trt_infer 分支,在 config.py 中
  22. * 添加 --use\_tensorrt,可使用 TenosrRT 推理。
  23. * 添加 --use\_tensorrt 和 use\_int8,可开启 TenosrRT 的 int8 量化。
  24. ## 环境
  25. 硬件环境
  26. * CPU:Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz x 6
  27. * GPU:[GeForce GTX 1080] x 4
  28. 软件环境
  29. * 系统:Ubuntu 18.04.4 LTS
  30. * NVIDIA Driver Version:440.44
  31. * CUDA:10.2
  32. * GCC:7.5
  33. * Cmake:3.14.4
  34. * Make:4.1
  35. 测试结果
  36. 测试模型为 ResNet 50(以下称 rn50),使用在线量化,分别进行单机单卡和单机多卡推理,batch_size 取 64 和可运行的最大 batch_size。
  37. 若正常运行,log 打印如下:
  38. ```
  39. ==================================================================
  40. Running resnet50: num_gpu_per_node = 1, num_nodes = 1.
  41. ==================================================================
  42. dtype = float32
  43. gpu_num_per_node = 1
  44. num_nodes = 1
  45. node_ips = ['127.0.0.1']
  46. ctrl_port = 50051
  47. model = resnet50
  48. use_fp16 = None
  49. use_xla = None
  50. channel_last = None
  51. pad_output = None
  52. num_epochs = 1
  53. model_load_dir = resnet_v15_of_best_model_val_top1_77318
  54. batch_size_per_device = 64
  55. val_batch_size_per_device = 256
  56. nccl_fusion_threshold_mb = 0
  57. nccl_fusion_max_ops = 0
  58. fuse_bn_relu = False
  59. fuse_bn_add_relu = False
  60. gpu_image_decoder = False
  61. image_path = test_img/tiger.jpg
  62. num_classes = 1000
  63. num_examples = 1281167
  64. num_val_examples = 50000
  65. rgb_mean = [123.68, 116.779, 103.939]
  66. rgb_std = [58.393, 57.12, 57.375]
  67. image_shape = [3, 224, 224]
  68. label_smoothing = 0.1
  69. model_save_dir = ./output/snapshots/model_save-20201123172206
  70. log_dir = ./output
  71. loss_print_every_n_iter = 1
  72. image_size = 224
  73. resize_shorter = 256
  74. train_data_dir = None
  75. train_data_part_num = 256
  76. val_data_dir = /dataset/ImageNet/ofrecord/validation
  77. val_data_part_num = 256
  78. optimizer = sgd
  79. learning_rate = 0.256
  80. wd = 3.0517578125e-05
  81. momentum = 0.875
  82. lr_decay = cosine
  83. lr_decay_rate = 0.94
  84. lr_decay_epochs = 2
  85. warmup_epochs = 5
  86. decay_rate = 0.9
  87. epsilon = 1.0
  88. gradient_clipping = 0.0
  89. ------------------------------------------------------------------
  90. Time stamp: 2020-11-23-17:22:06
  91. Restoring model from resnet_v15_of_best_model_val_top1_77318.
  92. Loading data from /dataset/ImageNet/ofrecord/validation
  93. W1123 17:23:41.120939 31217 trt_executable.cpp:146] Rebuild engine since the maximum batch size 1 is less than the input batch size 256
  94. W1123 17:24:25.756124 33076 trt_logger.cpp:35] TensorRT Logging: Explicit batch network detected and batch size specified, use execute without batch size instead.
  95. W1123 17:24:31.005220 33076 trt_logger.cpp:35] TensorRT Logging: Explicit batch network detected and batch size specified, use execute without batch size instead.
  96. W1123 17:24:36.085610 33076 trt_logger.cpp:35] TensorRT Logging: Explicit batch network detected and batch size specified, use execute without batch size instead.
  97. W1123 17:24:41.073289 33076 trt_logger.cpp:35] TensorRT Logging: Explicit batch network detected and batch size specified, use execute without batch size instead.
  98. W1123 17:24:45.920917 33076 trt_logger.cpp:35] TensorRT Logging: Explicit batch network detected and batch size specified, use execute without batch size instead.
  99. W1123 17:24:50.633805 33076 trt_logger.cpp:35] TensorRT Logging: Explicit batch network detected and batch size specified, use execute without batch size instead.
  100. W1123 17:24:55.354147 33076 trt_logger.cpp:35] TensorRT Logging: Explicit batch network detected and batch size specified, use execute without batch size instead.
  101. W1123 17:24:59.904863 33076 trt_logger.cpp:35] TensorRT Logging: Explicit batch network detected and batch size specified, use execute without batch size instead.
  102. validation: epoch 0, iter 195, top_1: 0.772155, top_k: 0.934856, samples/s: 181.038 1606123666.3968866
  103. ```
  104. ### 单机单卡

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能