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.

interp_helper.cpp 10 kB

feat(bazel/windows/xp/sp2/inference): implement inference on windows xp (os vesion >= sp2) build with bazel * bazel build support(define __DEPLOY_ON_XP_SP2__ when deploy on xp sp2): (dbg)./bazel build //brain/megbrain:load_and_run --cpu='x86_windows_xp' --compiler='clang_cl' -c dbg --copt "-D__DEPLOY_ON_XP_SP2__=1" (opt)./bazel build //brain/megbrain:load_and_run --cpu='x86_windows_xp' --compiler='clang_cl' -c opt --copt "-D__DEPLOY_ON_XP_SP2__=1" * internal behavior: will define MGB_HAVE_THREAD=0 when enable __DEPLOY_ON_XP_SP2__ * refer to https://docs.microsoft.com/en-us/cpp/build/configuring-programs-for-windows-xp?view=msvc-160 xp sp2(x86) do not support vc runtime fully, casused by KERNEL32.dll do not implement some base apis for c++ std function, for example, std::mutex/std::thread/std::condition_variable as a workround, we will disable some MegEngine features on xp sp2 env, for exampe, multi-thread etc! * about DNN_MUTEX/MGB_MUTEX, if your code will build in inference code (even CPU backends), please replace std::mutex to DNN_MUTEX/MGB_MUTEX, * about multi-thread, if you code need multi-thread support, please enable it when MGB_HAVE_THREAD=1 * about test build env status 1: Visual Studio 2019(MSVC version <= 14.26.28801)---- pass 2: Visual Studio 2019(MSVC version > 14.26.28801) ---- failed caused by this 'new' version will put VCR depends on win7 KERNEL32.DLL, this may be fixed at Visual Studio 2019 later version but we do not test at this MR merge point 3: Visual Studio 2017 ---------- pass 4: Visual Studio 2014 ---------- pass GitOrigin-RevId: 65ac48b95e99f2c510fe5db449cc8182d682e113
3 years ago
feat(bazel/windows/xp/sp2/inference): implement inference on windows xp (os vesion >= sp2) build with bazel * bazel build support(define __DEPLOY_ON_XP_SP2__ when deploy on xp sp2): (dbg)./bazel build //brain/megbrain:load_and_run --cpu='x86_windows_xp' --compiler='clang_cl' -c dbg --copt "-D__DEPLOY_ON_XP_SP2__=1" (opt)./bazel build //brain/megbrain:load_and_run --cpu='x86_windows_xp' --compiler='clang_cl' -c opt --copt "-D__DEPLOY_ON_XP_SP2__=1" * internal behavior: will define MGB_HAVE_THREAD=0 when enable __DEPLOY_ON_XP_SP2__ * refer to https://docs.microsoft.com/en-us/cpp/build/configuring-programs-for-windows-xp?view=msvc-160 xp sp2(x86) do not support vc runtime fully, casused by KERNEL32.dll do not implement some base apis for c++ std function, for example, std::mutex/std::thread/std::condition_variable as a workround, we will disable some MegEngine features on xp sp2 env, for exampe, multi-thread etc! * about DNN_MUTEX/MGB_MUTEX, if your code will build in inference code (even CPU backends), please replace std::mutex to DNN_MUTEX/MGB_MUTEX, * about multi-thread, if you code need multi-thread support, please enable it when MGB_HAVE_THREAD=1 * about test build env status 1: Visual Studio 2019(MSVC version <= 14.26.28801)---- pass 2: Visual Studio 2019(MSVC version > 14.26.28801) ---- failed caused by this 'new' version will put VCR depends on win7 KERNEL32.DLL, this may be fixed at Visual Studio 2019 later version but we do not test at this MR merge point 3: Visual Studio 2017 ---------- pass 4: Visual Studio 2014 ---------- pass GitOrigin-RevId: 65ac48b95e99f2c510fe5db449cc8182d682e113
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * By downloading, copying, installing or using the software you agree to this license.
  3. * If you do not agree to this license, do not download, install,
  4. * copy or use the software.
  5. *
  6. *
  7. * License Agreement
  8. * For Open Source Computer Vision Library
  9. * (3-clause BSD License)
  10. *
  11. * Copyright (C) 2000-2020, Intel Corporation, all rights reserved.
  12. * Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
  13. * Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved.
  14. * Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
  15. * Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved.
  16. * Copyright (C) 2015-2016, Itseez Inc., all rights reserved.
  17. * Copyright (C) 2019-2020, Xperience AI, all rights reserved.
  18. * Third party copyrights are property of their respective owners.
  19. *
  20. * Redistribution and use in source and binary forms, with or without modification,
  21. * are permitted provided that the following conditions are met:
  22. *
  23. * * Redistributions of source code must retain the above copyright notice,
  24. * this list of conditions and the following disclaimer.
  25. *
  26. * * Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials provided with the distribution.
  29. *
  30. * * Neither the names of the copyright holders nor the names of the contributors
  31. * may be used to endorse or promote products derived from this software
  32. * without specific prior written permission.
  33. *
  34. * This software is provided by the copyright holders and contributors "as is" and
  35. * any express or implied warranties, including, but not limited to, the implied
  36. * warranties of merchantability and fitness for a particular purpose are disclaimed.
  37. * In no event shall copyright holders or contributors be liable for any direct,
  38. * indirect, incidental, special, exemplary, or consequential damages
  39. * (including, but not limited to, procurement of substitute goods or services;
  40. * loss of use, data, or profits; or business interruption) however caused
  41. * and on any theory of liability, whether in contract, strict liability,
  42. * or tort (including negligence or otherwise) arising in any way out of
  43. * the use of this software, even if advised of the possibility of such damage.
  44. *
  45. * ---------------------------------------------------------------------------
  46. * \file dnn/src/common/cv/interp_helper.cpp
  47. *
  48. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  49. *
  50. * Unless required by applicable law or agreed to in writing,
  51. * software distributed under the License is distributed on an
  52. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  53. *
  54. * This file has been modified by Megvii ("Megvii Modifications").
  55. * All Megvii Modifications are Copyright (C) 2014-2021 Megvii Inc. All rights reserved.
  56. *
  57. * ---------------------------------------------------------------------------
  58. */
  59. #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  60. // TableHolderBase has no problem; ignore the warning for old clang versions
  61. #include "./interp_helper.h"
  62. using namespace megdnn;
  63. using namespace megdnn::megcv;
  64. static constexpr double MEGCV_PI_4 = 0.78539816339744830962; /* pi/4 */
  65. #define DEF_FUN(_ret) \
  66. template <int INTER_BITS_, int INTER_MAX_, int INTER_REMAP_COEF_BITS_> \
  67. _ret InterpolationTable<INTER_BITS_, INTER_MAX_, INTER_REMAP_COEF_BITS_>::
  68. #define DEF_TABLE_HOLDER(_name, _ksize) \
  69. template <int INTER_BITS_, int INTER_MAX_, int INTER_REMAP_COEF_BITS_> \
  70. typename InterpolationTable< \
  71. INTER_BITS_, INTER_MAX_, \
  72. INTER_REMAP_COEF_BITS_>::template TableHolder<_ksize> \
  73. InterpolationTable<INTER_BITS_, INTER_MAX_, \
  74. INTER_REMAP_COEF_BITS_>::_name
  75. DEF_TABLE_HOLDER(sm_tab_linear, 2);
  76. DEF_TABLE_HOLDER(sm_tab_cubic, 4);
  77. DEF_TABLE_HOLDER(sm_tab_lanczos4, 8);
  78. DEF_FUN(void) interpolate_linear(float x, float* coeffs) {
  79. coeffs[0] = 1.f - x;
  80. coeffs[1] = x;
  81. }
  82. DEF_FUN(void) interpolate_cubic(float x, float* coeffs) {
  83. const float A = -0.75f;
  84. coeffs[0] = ((A * (x + 1) - 5 * A) * (x + 1) + 8 * A) * (x + 1) - 4 * A;
  85. coeffs[1] = ((A + 2) * x - (A + 3)) * x * x + 1;
  86. coeffs[2] = ((A + 2) * (1 - x) - (A + 3)) * (1 - x) * (1 - x) + 1;
  87. coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2];
  88. }
  89. DEF_FUN(void) interpolate_lanczos4(float x, float* coeffs) {
  90. static const double s45 = 0.70710678118654752440084436210485;
  91. static const double cs[][2] = {{1, 0}, {-s45, -s45}, {0, 1}, {s45, -s45},
  92. {-1, 0}, {s45, s45}, {0, -1}, {-s45, s45}};
  93. if (x < FLT_EPSILON) {
  94. for (int i = 0; i < 8; i++)
  95. coeffs[i] = 0;
  96. coeffs[3] = 1;
  97. return;
  98. }
  99. float sum = 0;
  100. double y0 = -(x + 3) * MEGCV_PI_4, s0 = sin(y0), c0 = cos(y0);
  101. for (int i = 0; i < 8; i++) {
  102. double y = -(x + 3 - i) * MEGCV_PI_4;
  103. coeffs[i] = (float)((cs[i][0] * s0 + cs[i][1] * c0) / (y * y));
  104. sum += coeffs[i];
  105. }
  106. sum = 1.f / sum;
  107. for (int i = 0; i < 8; i++)
  108. coeffs[i] *= sum;
  109. }
  110. DEF_FUN(void)
  111. init_inter_tab_1d(InterpolationMode imode, float* tab, int tabsz) {
  112. float scale = 1.f / tabsz;
  113. switch (imode) {
  114. case IMode::INTER_LINEAR:
  115. for (int i = 0; i < tabsz; ++i, tab += 2)
  116. interpolate_linear(i * scale, tab);
  117. break;
  118. case IMode::INTER_CUBIC:
  119. for (int i = 0; i < tabsz; ++i, tab += 4)
  120. interpolate_cubic(i * scale, tab);
  121. break;
  122. case IMode::INTER_LANCZOS4:
  123. for (int i = 0; i < tabsz; ++i, tab += 8)
  124. interpolate_lanczos4(i * scale, tab);
  125. break;
  126. default:
  127. megdnn_throw("unsupported interpolation mode");
  128. }
  129. }
  130. #if MEGDNN_X86
  131. DEF_FUN(const int16_t*) get_linear_ic4_table() {
  132. auto table_holder = &sm_tab_linear;
  133. MEGDNN_LOCK_GUARD(table_holder->mtx);
  134. float* tab = nullptr;
  135. short* itab = nullptr;
  136. MEGDNN_MARK_USED_VAR(tab);
  137. MEGDNN_MARK_USED_VAR(itab);
  138. megdnn_assert(table_holder->get(&tab, &itab),
  139. "invoke get_table before get_linear_ic4_table");
  140. return table_holder->table->bilineartab_ic4_buf;
  141. }
  142. #endif
  143. DEF_FUN(const void*) get_table(InterpolationMode imode, bool fixpt) {
  144. TableHolderBase* table_holder = nullptr;
  145. int ksize = 0;
  146. switch (imode) {
  147. case IMode::INTER_LINEAR:
  148. table_holder = &sm_tab_linear;
  149. ksize = 2;
  150. break;
  151. case IMode::INTER_CUBIC:
  152. table_holder = &sm_tab_cubic;
  153. ksize = 4;
  154. break;
  155. case IMode::INTER_LANCZOS4:
  156. table_holder = &sm_tab_lanczos4;
  157. ksize = 8;
  158. break;
  159. default:
  160. megdnn_throw(("unsupported interpolation mode"));
  161. }
  162. MEGDNN_LOCK_GUARD(table_holder->mtx);
  163. float* tab = nullptr;
  164. short* itab = nullptr;
  165. if (!table_holder->get(&tab, &itab)) {
  166. float _tab[8 * INTER_TAB_SIZE];
  167. int i, j, k1, k2;
  168. init_inter_tab_1d(imode, _tab, INTER_TAB_SIZE);
  169. for (i = 0; i < INTER_TAB_SIZE; ++i) {
  170. for (j = 0; j < INTER_TAB_SIZE;
  171. ++j, tab += ksize * ksize, itab += ksize * ksize) {
  172. int isum = 0;
  173. for (k1 = 0; k1 < ksize; ++k1) {
  174. float vy = _tab[i * ksize + k1];
  175. for (k2 = 0; k2 < ksize; ++k2) {
  176. float v = vy * _tab[j * ksize + k2];
  177. tab[k1 * ksize + k2] = v;
  178. isum += itab[k1 * ksize + k2] = saturate_cast<short>(
  179. v * INTER_REMAP_COEF_SCALE);
  180. }
  181. }
  182. if (isum != INTER_REMAP_COEF_SCALE) {
  183. int diff = isum - INTER_REMAP_COEF_SCALE;
  184. int ksize2 = ksize / 2, Mk1 = ksize2, Mk2 = ksize2;
  185. int mk1 = ksize2, mk2 = ksize2;
  186. for (k1 = ksize2; k1 < ksize2 + 2; ++k1)
  187. for (k2 = ksize2; k2 < ksize2 + 2; ++k2) {
  188. if (itab[k1 * ksize + k2] <
  189. itab[mk1 * ksize + mk2]) {
  190. mk1 = k1;
  191. mk2 = k2;
  192. } else if (itab[k1 * ksize + k2] >
  193. itab[Mk1 * ksize + Mk2]) {
  194. Mk1 = k1;
  195. Mk2 = k2;
  196. }
  197. }
  198. if (diff < 0)
  199. itab[Mk1 * ksize + Mk2] =
  200. (short)(itab[Mk1 * ksize + Mk2] - diff);
  201. else
  202. itab[mk1 * ksize + mk2] =
  203. (short)(itab[mk1 * ksize + mk2] - diff);
  204. }
  205. }
  206. }
  207. tab -= INTER_TAB_SIZE2 * ksize * ksize;
  208. itab -= INTER_TAB_SIZE2 * ksize * ksize;
  209. #if MEGDNN_X86
  210. if (imode == IMode::INTER_LINEAR) {
  211. int16_t* bilineartab_ic4_buf =
  212. sm_tab_linear.table->bilineartab_ic4_buf;
  213. for (i = 0; i < INTER_TAB_SIZE2; i++)
  214. for (j = 0; j < 4; j++) {
  215. bilineartab_ic4_buf[i * 2 * 8 + 0 * 8 + j * 2] =
  216. itab[i * ksize * ksize + 0 * ksize + 0];
  217. bilineartab_ic4_buf[i * 2 * 8 + 0 * 8 + j * 2 + 1] =
  218. itab[i * ksize * ksize + 0 * ksize + 1];
  219. bilineartab_ic4_buf[i * 2 * 8 + 1 * 8 + j * 2] =
  220. itab[i * ksize * ksize + 1 * ksize + 0];
  221. bilineartab_ic4_buf[i * 2 * 8 + 1 * 8 + j * 2 + 1] =
  222. itab[i * ksize * ksize + 1 * ksize + 1];
  223. }
  224. }
  225. #endif
  226. }
  227. return fixpt ? static_cast<void*>(itab) : static_cast<void*>(tab);
  228. }
  229. namespace megdnn {
  230. namespace megcv {
  231. // explicit inst
  232. template class InterpolationTable<5, 7, 15>;
  233. } // namespace megcv
  234. } // namespace megdnn
  235. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台