Jizhi AI|TensorRT定数演算子について説明する

  一緒に書く習慣をつけましょう!「ナゲッツデイリーニュープラン・6月アップデートチャレンジ」に参加して17日目です。クリックしてイベントの詳細をご覧ください

欢迎关注我的公众号 [极智视界],获取我的更多笔记分享

  みなさん、こんにちは。JizhiVisionです。この記事では、TensorRTConstant演算子について説明します。

  定数演算子は定数層を指します。この演算子が一般的に使用されるのはいつですか。通常、次の演算子は2行列の乗算、2行列の内積、2行列のスプライシング、およびその他の2入力演算子であり、オフラインで読み取る場合は行列が必要です。 、Constant演算子は、オフライン読み取り用のテンソルを作成するために必要です。上記はConstant演算子の使用シナリオを説明し、次にTensorRTにConstant演算子を追加する方法を説明します。

  TensorRTで定数演算子を作成する方法を見てみましょう。

# 通过 add_constant 添加 constant 算子
constantLayer = network.add_constant([1], np.array([1], dtype=np.float32))

# 重设常量数据
constantLayer.weights = data 

# 重设常量形状
constantLayer.shape = data.shape

  実際の例を見てみましょう。

import numpy as np
from cuda import cudart
import tensorrt as trt

# 输入张量 NCHW
nIn, cIn, hIn, wIn = 1, 3, 4, 5  # 输入张量 NCHW

# 输入数据
data = np.arange(nIn * cIn * hIn * wIn, dtype=np.float32).reshape(nIn, cIn, hIn, wIn) 

np.set_printoptions(precision=8, linewidth=200, suppress=True)
cudart.cudaDeviceSynchronize()

logger = trt.Logger(trt.Logger.ERROR)
builder = trt.Builder(logger)
network = builder.create_network(1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH))
config = builder.create_builder_config()
#---------------------------------------------------------- --------------------# 替换部分
# 添加 constant 算子
constantLayer = network.add_constant(data.shape, data)
#---------------------------------------------------------- --------------------# 替换部分
network.mark_output(constantLayer.get_output(0))

engineString = builder.build_serialized_network(network, config)
engine = trt.Runtime(logger).deserialize_cuda_engine(engineString)
context = engine.create_execution_context()
_, stream = cudart.cudaStreamCreate()

outputH0 = np.empty(context.get_binding_shape(0), dtype=trt.nptype(engine.get_binding_dtype(0)))
_, outputD0 = cudart.cudaMallocAsync(outputH0.nbytes, stream)

context.execute_async_v2([int(outputD0)], stream)
cudart.cudaMemcpyAsync(outputH0.ctypes.data, outputD0, outputH0.nbytes, cudart.cudaMemcpyKind.cudaMemcpyDeviceToHost, stream)
cudart.cudaStreamSynchronize(stream)

print("outputH0:", outputH0.shape)
print(outputH0)

cudart.cudaStreamDestroy(stream)
cudart.cudaFree(outputD0)
  • 出力テンソル形状(1,3,4,5)
[ [ [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. ] [ 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. ] [ 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. ] ] ] \left[\begin{matrix} \left[\begin{matrix} \left[\begin{matrix} 0. & 1. & 2. & 3. & 4. \\ 5. & 6. & 7. & 8. & 9. \\ 10. & 11. & 12. & 13. & 14. \\ 15. & 16. & 17. & 18. & 19. \end{matrix}\right] \left[\begin{matrix} 20. & 21. & 22. & 23. & 24. \\ 25. & 26. & 27. & 28. & 29. \\ 30. & 31. & 32. & 33. & 34. \\ 35. & 36. & 37. & 38. & 39. \end{matrix}\right] \left[\begin{matrix} 40. & 41. & 42. & 43. & 44. \\ 45. & 46. & 47. & 48. & 49. \\ 50. & 51. & 52. & 53. & 54. \\ 55. & 56. & 57. & 58. & 59. \end{matrix}\right] \end{matrix}\right] \end{matrix}\right]

  さて、私は上記のTensorRT Constant演算子を共有して説明しました。私の共有が、あなたの学習に少し役立つことを願っています。


 【公開番号送信】

「非常にインテリジェントなAI|TensorRT定数演算子の説明」


logo_show.gif

おすすめ

転載: juejin.im/post/7113173172215611406