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.h 6.7 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.h
  47. *
  48. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  49. *
  50. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  51. *
  52. * Unless required by applicable law or agreed to in writing,
  53. * software distributed under the License is distributed on an
  54. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  55. *
  56. * This file has been modified by Megvii ("Megvii Modifications").
  57. * All Megvii Modifications are Copyright (C) 2014-2021 Megvii Inc. All rights reserved.
  58. *
  59. * ---------------------------------------------------------------------------
  60. */
  61. #pragma once
  62. #include "src/common/cv/aligned_allocator.h"
  63. #include "src/common/utils.h"
  64. #include "./helper.h"
  65. #include "megdnn/opr_param_defs.h"
  66. #include <cstdint>
  67. #include <memory>
  68. #include <mutex>
  69. namespace megdnn {
  70. namespace megcv {
  71. using InterpolationMode = megdnn::param::WarpPerspective::InterpolationMode;
  72. using BorderMode = megdnn::param::WarpPerspective::BorderMode;
  73. /*!
  74. * \brief helper for generating interpolation tables for different interpolation
  75. * modes
  76. */
  77. template <int INTER_BITS_ = 5, int INTER_MAX_ = 7,
  78. int INTER_REMAP_COEF_BITS_ = 15>
  79. class InterpolationTable {
  80. public:
  81. using IMode = InterpolationMode;
  82. static constexpr int INTER_BITS = INTER_BITS_;
  83. static constexpr int INTER_MAX = INTER_MAX_;
  84. static constexpr int INTER_REMAP_COEF_BITS = INTER_REMAP_COEF_BITS_;
  85. static constexpr int INTER_TAB_SIZE = (1 << INTER_BITS);
  86. static constexpr int INTER_TAB_SIZE2 = INTER_TAB_SIZE * INTER_TAB_SIZE;
  87. static constexpr int INTER_REMAP_COEF_SCALE = 1 << INTER_REMAP_COEF_BITS;
  88. /*!
  89. * \brief get interpolation table
  90. *
  91. * The table dimension is [INTER_TAB_SIZE][INTER_TAB_SIZE][ksize][ksize]
  92. *
  93. * \param imode interpolation mode
  94. * \param fixpt if this is true, return a table for int16_t; else return a
  95. * table for float
  96. * \return table for int16 or float according to fixpt
  97. */
  98. static const void* get_table(InterpolationMode imode, bool fixpt);
  99. #if MEGDNN_X86
  100. /**
  101. * \brief get interpolation table for linear mode.
  102. *
  103. * This current only avaiable in \warning X86.
  104. *
  105. * \return bilineartab_ic4_buf
  106. */
  107. static const int16_t* get_linear_ic4_table();
  108. #endif
  109. private:
  110. template <int ksize>
  111. struct Table {
  112. float ftab[INTER_TAB_SIZE2 * ksize * ksize];
  113. int16_t itab[INTER_TAB_SIZE2 * ksize * ksize];
  114. #if MEGDNN_X86
  115. alignas(128) int16_t bilineartab_ic4_buf[INTER_TAB_SIZE2 * 2 * 8];
  116. static void* operator new(std::size_t sz) {
  117. return ah::aligned_allocator<Table, 128>().allocate(sz /
  118. sizeof(Table));
  119. }
  120. void operator delete(void* ptr) noexcept {
  121. ah::aligned_allocator<Table, 128>().deallocate(
  122. reinterpret_cast<Table*>(ptr), 0);
  123. }
  124. #endif
  125. };
  126. struct TableHolderBase {
  127. DNN_MUTEX mtx;
  128. //! get table pointer; return whether already init
  129. virtual bool get(float**, int16_t**) = 0;
  130. protected:
  131. ~TableHolderBase() = default;
  132. };
  133. template <int ksize>
  134. struct TableHolder final : public TableHolderBase {
  135. std::unique_ptr<Table<ksize>> table;
  136. bool get(float** ftab, int16_t** itab) override {
  137. bool ret = true;
  138. if (!table) {
  139. ret = false;
  140. table.reset(new Table<ksize>);
  141. }
  142. *ftab = table->ftab;
  143. *itab = table->itab;
  144. return ret;
  145. }
  146. };
  147. static void init_inter_tab_1d(InterpolationMode imode, float* tab,
  148. int tabsz);
  149. static inline void interpolate_linear(float x, float* coeffs);
  150. static inline void interpolate_cubic(float x, float* coeffs);
  151. static inline void interpolate_lanczos4(float x, float* coeffs);
  152. static TableHolder<2> sm_tab_linear;
  153. static TableHolder<4> sm_tab_cubic;
  154. static TableHolder<8> sm_tab_lanczos4;
  155. };
  156. } // namespace megcv
  157. } // namespace megdnn
  158. // vim: syntax=cpp.doxygen

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