ruy

参考:实践解析|如何通过WebAssembly在Web进行实时视频人像分割

  • ruy、XNNPACK都是tensorflow lite的向量、矩阵运算框架。ruy是必须模块。XNNPACK则是可选的,通过delegate模块融入tensorflow lite。

TensorFlow有三套向量、矩阵运算框架:XNNPACK、Eigen、ruy。实际上它们在不同平台上的性能都有差异。对此进行测试,在x86_64 window10环境下的测试结果如下。可以明显看到此时场景下XNNPACK的表现是最好的,因为它是专门针对浮点运算进行了优化的运算框架。

这里只是展示了x86下的运算测试结果,并不能代表所有平台上的最终结果。因为ruy这个框架是TensorFlow在移动平台上的默认运算框架,它对ARM架构优化更好。所以要是在别的不同平台上进行测试,可能不一样结果。

在旧版本的tensorflow lite中,支持了不同的GEMM(General Matrix to Matrix Multiplication:通用矩阵乘法)库来加速计算。比如Eigen和GEMMLOWP,其中对于float32的模型调用的是Eigen库,对于8bit量化的模型会走GEMMLOWP库。这些库在Arm平台上性能表现较差,因此在tensorflow 2.3的版本引入了Ruy矩阵乘法库。

在Arm平台上Ruy表现得更好,因此在对于Arm平台编译的时候默认的选项就是Ruy,当然也可以加上编译选项使用Eigen和Gemmlowp库。对于一些其它平台,默认使用的可能还是GEMMLOWP,同样可以在编译的时候加上Ruy的选项。

谷歌开源小型独立低精度通用矩阵乘法库gemmlowp。所谓低精度(LOWP),是指输入8位、相乘后输出8位,但中间运算过程会按32位计算。然后输出结果取32位的高8位,否则中间运算若直接按8位算则会溢出而损失精度。gemmlowp足够的灵活来支持多种低精度的案例,并且可以通过多种方式将含义附加到8bit值上,以便计算可以依赖gemmlowp提供的8bit gemm。

在进行模型推理的时候,也有一些第三方底层计算库,如XNNPACK,它的性能甚至比Ruy更好,但它只专注于浮点操作,可以通过delegate来使用。

 

ruy是必须模块。

9>   Creating library C:\ddksample\apps-src\apps\projectfiles\vc\Release\studio.lib and object C:\ddksample\apps-src\apps\projectfiles\vc\Release\studio.exp
9>4libtensorflowlite.lib(interpreter.obj) : error LNK2019: unresolved external symbol "public: __thiscall ruy::ScopedSuppressDenormals::ScopedSuppressDenormals(void)" (??0ScopedSuppressDenormals@ruy@@QAE@XZ) referenced in function "public: enum TfLiteStatus __thiscall tflite::impl::Interpreter::Invoke(void)" (?Invoke@Interpreter@impl@tflite@@QAE?AW4TfLiteStatus@@XZ)
9>4libtensorflowlite.lib(interpreter.obj) : error LNK2019: unresolved external symbol "public: __thiscall ruy::ScopedSuppressDenormals::~ScopedSuppressDenormals(void)" (??1ScopedSuppressDenormals@ruy@@QAE@XZ) referenced in function "public: enum TfLiteStatus __thiscall tflite::impl::Interpreter::Invoke(void)" (?Invoke@Interpreter@impl@tflite@@QAE?AW4TfLiteStatus@@XZ)
9>4libtensorflowlite.lib(fully_connected.obj) : error LNK2001: unresolved external symbol "class ruy::Ctx * __cdecl ruy::get_ctx(class ruy::Context *)" (?get_ctx@ruy@@YAPAVCtx@1@PAVContext@1@@Z)
9>4libtensorflowlite.lib(lstm.obj) : error LNK2001: unresolved external symbol "class ruy::Ctx * __cdecl ruy::get_ctx(class ruy::Context *)" (?get_ctx@ruy@@YAPAVCtx@1@PAVContext@1@@Z)
9>4libtensorflowlite.lib(transpose_conv.obj) : error LNK2001: unresolved external symbol "class ruy::Ctx * __cdecl ruy::get_ctx(class ruy::Context *)" (?get_ctx@ruy@@YAPAVCtx@1@PAVContext@1@@Z)
9>4libtensorflowlite.lib(lstm_eval.obj) : error LNK2001: unresolved external symbol "class ruy::Ctx * __cdecl ruy::get_ctx(class ruy::Context *)" (?get_ctx@ruy@@YAPAVCtx@1@PAVContext@1@@Z)
9>4libtensorflowlite.lib(batch_matmul.obj) : error LNK2001: unresolved external symbol "class ruy::Ctx * __cdecl ruy::get_ctx(class ruy::Context *)" (?get_ctx@ruy@@YAPAVCtx@1@PAVContext@1@@Z)
9>4libtensorflowlite.lib(conv.obj) : error LNK2001: unresolved external symbol "class ruy::Ctx * __cdecl ruy::get_ctx(class ruy::Context *)" (?get_ctx@ruy@@YAPAVCtx@1@PAVContext@1@@Z)
9>4libtensorflowlite.lib(conv3d.obj) : error LNK2001: unresolved external symbol "class ruy::Ctx * __cdecl ruy::get_ctx(class ruy::Context *)" (?get_ctx@ruy@@YAPAVCtx@1@PAVContext@1@@Z)
9>4libtensorflowlite.lib(conv3d_transpose.obj) : error LNK2001: unresolved external symbol "class ruy::Ctx * __cdecl ruy::get_ctx(class ruy::Context *)" (?get_ctx@ruy@@YAPAVCtx@1@PAVContext@1@@Z)
9>4libtensorflowlite.lib(fully_connected.obj) : error LNK2001: unresolved external symbol "public: void __thiscall ruy::Ctx::clear_performance_advisories(void)" (?clear_performance_advisories@Ctx@ruy@@QAEXXZ)
9>4libtensorflowlite.lib(lstm.obj) : error LNK2001: unresolved external symbol "public: void __thiscall ruy::Ctx::clear_performance_advisories(void)" (?clear_performance_advisories@Ctx@ruy@@QAEXXZ)
9>4libtensorflowlite.lib(transpose_conv.obj) : error LNK2001: unresolved external symbol "public: void __thiscall ruy::Ctx::clear_performance_advisories(void)" (?clear_performance_advisories@Ctx@ruy@@QAEXXZ)
9>4libtensorflowlite.lib(lstm_eval.obj) : error LNK2001: unresolved external symbol "public: void __thiscall ruy::Ctx::clear_performance_advisories(void)" (?clear_performance_advisories@Ctx@ruy@@QAEXXZ)
9>4libtensorflowlite.lib(batch_matmul.obj) : error LNK2001: unresolved external symbol "public: void __thiscall ruy::Ctx::clear_performance_advisories(void)" (?clear_performance_advisories@Ctx@ruy@@QAEXXZ)
9>4libtensorflowlite.lib(conv.obj) : error LNK2001: unresolved external symbol "public: void __thiscall ruy::Ctx::clear_performance_advisories(void)" (?clear_performance_advisories@Ctx@ruy@@QAEXXZ)
9>4libtensorflowlite.lib(conv3d.obj) : error LNK2001: unresolved external symbol "public: void __thiscall ruy::Ctx::clear_performance_advisories(void)" (?clear_performance_advisories@Ctx@ruy@@QAEXXZ)
9>4libtensorflowlite.lib(conv3d_transpose.obj) : error LNK2001: unresolved external symbol "public: void __thiscall ruy::Ctx::clear_performance_advisories(void)" (?clear_performance_advisories@Ctx@ruy@@QAEXXZ)
.......

要是没有编译ruy源码,在tensorflow lite链接阶段会报以上错误,完整错误见“tfl-without-ruy.cpp”。

全部评论: 0

    写评论: