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.

checker.h 22 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /**
  2. * \file dnn/test/common/checker.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied.
  11. */
  12. #pragma once
  13. #include "megdnn/basic_types.h"
  14. #include "megdnn/tensor_iter.h"
  15. #include "test/common/opr_algo_proxy.h"
  16. #include "test/common/opr_proxy.h"
  17. #include "test/common/rng.h"
  18. #include <gtest/gtest.h>
  19. #include <memory>
  20. #include <regex>
  21. #include <unordered_map>
  22. // clang-format off
  23. #if defined(__has_feature)
  24. #if __has_feature(address_sanitizer)
  25. #define MEGDNN_TEST_ASAN 1
  26. #else
  27. #define MEGDNN_TEST_ASAN 0
  28. #endif
  29. #elif defined(__SANITIZE_ADDRESS__)
  30. #define MEGDNN_TEST_ASAN 1
  31. #else
  32. #define MEGDNN_TEST_ASAN 0
  33. #endif
  34. // clang-format on
  35. namespace megdnn {
  36. namespace test {
  37. class CheckerHelper {
  38. // TensorLayoutArray and TensorValueArray should be protected in theory;
  39. // but g++-4.9 bugs handle access privilege wrongfully, so we change it
  40. // to public.
  41. public:
  42. using TensorValueArray = TensorNDArray;
  43. using TensorsConstriant = std::function<void(TensorValueArray& tensors)>;
  44. using ExtraOprImpl = std::function<void(const TensorNDArray&)>;
  45. using OutputCanonizer = std::function<void(const TensorValueArray&)>;
  46. static std::shared_ptr<TensorValueArray> alloc_tensors(
  47. Handle* handle, const TensorLayoutArray& layouts, size_t offset);
  48. Handle* handle() const { return m_handle_cur; }
  49. protected:
  50. //! whether to use physically contiguous (i.e. default layout) for naive
  51. //! impl
  52. bool m_enable_contig_naive = false;
  53. bool m_prev_succ = true;
  54. const char* m_input_tensors_fpath = nullptr;
  55. thin_function<void()> m_expect_exec_fail;
  56. std::unique_ptr<Handle> m_handle_naive;
  57. Handle* m_handle_cur;
  58. std::unique_ptr<RNG> m_default_rng;
  59. std::unordered_map<size_t, RNG*> m_rng;
  60. std::unordered_map<size_t, DType> m_dtype;
  61. std::unordered_map<size_t, TensorFormat> m_fmt;
  62. float_t m_epsilon = 1e-3, m_max_avg_error = 1e-3,
  63. m_max_avg_biased_error = 1e-3;
  64. float_t m_perf_check_threshold = -1;
  65. bool m_perf_check = false;
  66. ExtraOprImpl m_extra_opr_impl;
  67. OutputCanonizer m_output_canonizer;
  68. TensorsConstriant m_tensor_constraint;
  69. bool m_no_naive_and_check = false;
  70. bool m_stable_check = false;
  71. /**
  72. * the offset from the start of malloc memory
  73. *
  74. * \note alloc \p m_offset more memory when alloc memory for a tensor,
  75. * the start of tensor just begin at \p m_offset.
  76. * \warning current only used for opencl
  77. */
  78. size_t m_offset = 0;
  79. CheckerHelper(Handle* handle, bool check_dispatch = true);
  80. ~CheckerHelper() noexcept;
  81. using OprExec = std::function<void(const TensorValueArray&)>;
  82. void do_exec_with_testcases(const TensorValueArray& testcase_in,
  83. const TensorValueArray& testcase_out,
  84. const OprExec& exec_opr);
  85. void do_exec(const TensorLayoutArray& user_layouts,
  86. const TensorLayoutArray& deduced_layouts,
  87. const OprExec& exec_naive, const OprExec& exec_opr);
  88. void enable_contig_naive() { m_enable_contig_naive = true; }
  89. void copy_tensors_to_device(const TensorValueArray& dest,
  90. const TensorValueArray& src);
  91. void copy_tensors_from_device(const TensorValueArray& dest,
  92. const TensorValueArray& src);
  93. private:
  94. std::shared_ptr<TensorValueArray> m_tensors_naive;
  95. void init_naive_values();
  96. void check_tensors(const TensorValueArray& expected,
  97. const TensorValueArray& computed);
  98. };
  99. template <typename Opr, typename Proxy = OprProxy<Opr>>
  100. class Checker : public CheckerHelper {
  101. public:
  102. using Param = typename Opr::Param;
  103. using BeforeExecCallback =
  104. std::function<void(Opr*, const TensorValueArray&)>;
  105. Checker(Handle* handle, bool check_dispatch = true)
  106. : CheckerHelper(handle, check_dispatch), m_param(Param()) {}
  107. TensorLayoutArray make_layouts(const TensorShapeArray& shapes) {
  108. TensorLayoutArray layouts(shapes.size());
  109. for (size_t i = 0; i < shapes.size(); ++i) {
  110. DType dt = (m_dtype.find(i) != m_dtype.end() ? m_dtype[i]
  111. : dtype::Float32());
  112. if (m_fmt.find(i) == m_fmt.end()) {
  113. layouts[i] = TensorLayout(shapes[i], dt);
  114. layouts[i].init_contiguous_stride();
  115. } else
  116. layouts[i] = TensorLayout(shapes[i], dt, m_fmt[i]);
  117. }
  118. return layouts;
  119. }
  120. /*!
  121. * \brief execute opr on current param/dtype/rng config
  122. * \param shapes input/output shapes, which would be passed as
  123. * arguments to Opr::deduce_layout
  124. *
  125. * Checker would construct TensorLayout vectors from shapes and dtypes,
  126. * and call exec(TensorLayoutArray &).
  127. */
  128. Checker& exec(const TensorShapeArray& shapes) {
  129. exec(make_layouts(shapes));
  130. return *this;
  131. }
  132. void exec(TensorLayoutArray layouts);
  133. //! explicitly require argument to be TensorShape
  134. Checker& execs(const TensorShapeArray& shapes) { return exec(shapes); }
  135. //! explicitly require argument to be TensorLayout
  136. Checker& execl(const TensorLayoutArray& layouts) {
  137. exec(layouts);
  138. return *this;
  139. }
  140. Checker& exect(const TensorValueArray& testcase_in,
  141. const TensorValueArray& testcase_out);
  142. Checker& set_param(Param param) {
  143. m_param = param;
  144. opr()->param() = param;
  145. return *this;
  146. }
  147. Checker& set_dtype(size_t idx, DType dtype) {
  148. m_dtype[idx] = dtype;
  149. return *this;
  150. }
  151. Checker& set_fmt(size_t idx, TensorFormat fmt) {
  152. m_fmt[idx] = fmt;
  153. return *this;
  154. }
  155. Checker& set_rng(size_t idx, RNG* rng) {
  156. m_rng[idx] = rng;
  157. return *this;
  158. }
  159. //! max error of a single element
  160. Checker& set_epsilon(dt_float32 epsilon) {
  161. m_epsilon = epsilon;
  162. m_max_avg_error = epsilon;
  163. m_max_avg_biased_error = epsilon;
  164. return *this;
  165. }
  166. //! max average error; defaults to epsilon
  167. Checker& set_max_avg_error(dt_float32 error) {
  168. m_max_avg_error = error;
  169. return *this;
  170. }
  171. //! max average biased error; defaults to epsilon
  172. Checker& set_max_avg_biased_error(dt_float32 error) {
  173. m_max_avg_biased_error = error;
  174. return *this;
  175. }
  176. Checker& set_offset(size_t offset) {
  177. m_offset = offset;
  178. return *this;
  179. }
  180. Checker& set_proxy(const Proxy& proxy) {
  181. m_naive_proxy = proxy;
  182. m_cur_proxy = proxy;
  183. return *this;
  184. }
  185. //! set_perf_check and set_perf_check_threshold control the
  186. //! performance checking behavior.
  187. //!
  188. //! If perf_check is on (default to off), the running time of the
  189. //! current operator and the naive operator would be measured and
  190. //! checked when calling exec.
  191. //! The accelerating ratio should be larger than perf_check_threshold,
  192. //! otherwise errors would be reported.
  193. //! perf_check_threshold must be set in advance since the default value
  194. //! (which is negative) is invalid.
  195. Checker& set_perf_check(bool perf_check) {
  196. m_perf_check = perf_check;
  197. return *this;
  198. }
  199. Checker& set_perf_check_threshold(float perf_check_threshold) {
  200. m_perf_check_threshold = perf_check_threshold;
  201. return *this;
  202. }
  203. //! stable check will run many iter and compare result with first iter
  204. Checker& set_stable_check(bool stable_check) {
  205. m_stable_check = stable_check;
  206. return *this;
  207. }
  208. Checker& set_no_naive_check(bool no_naive_and_check) {
  209. m_no_naive_and_check = no_naive_and_check;
  210. return *this;
  211. }
  212. //! load input tensors from file for next run
  213. Checker& load_input_tensors(const char* fpath) {
  214. m_input_tensors_fpath = fpath;
  215. return *this;
  216. }
  217. //! add another checker to ensure naive implementation is correct
  218. Checker& set_extra_opr_impl(const ExtraOprImpl& chk) {
  219. m_extra_opr_impl = chk;
  220. return *this;
  221. }
  222. //! set a callback to be invoked before executing the operator
  223. Checker& set_before_exec_callback(const BeforeExecCallback& cb) {
  224. m_before_exec_callback = cb;
  225. return *this;
  226. }
  227. Checker& reset_before_exec_callback() {
  228. m_before_exec_callback = nullptr;
  229. return *this;
  230. }
  231. //! set a tensors constraints function, for the purpose of manipulating
  232. //! tensors when testing.
  233. Checker& set_tensors_constraint(
  234. const TensorsConstriant& tensor_constraint) {
  235. m_tensor_constraint = tensor_constraint;
  236. return *this;
  237. }
  238. /*!
  239. * \brief set that exec() on opr should fail, so naive is not called and
  240. * exec() returns directly after opr is called.
  241. *
  242. * This is only valid for next exec() call. It is usually used for
  243. * testing megcore::AsyncErrorInfo.
  244. *
  245. * \param cb callback to be invoked after opr exec (so error would not
  246. * be passed to destructor)
  247. */
  248. Checker& set_expect_exec_fail(const thin_function<void()>& cb) {
  249. m_expect_exec_fail = cb;
  250. return *this;
  251. }
  252. /*!
  253. * \brief set a function to canonize the outputs
  254. *
  255. * For some oprs maybe multiple outputs can be accepted; we can use a
  256. * function to transform them into a canonized form before comparing.
  257. *
  258. * The arguments are tensors on CPU and should be modified in-place.
  259. */
  260. Checker& set_output_canonizer(OutputCanonizer canonizer) {
  261. m_output_canonizer = std::move(canonizer);
  262. return *this;
  263. }
  264. //! get the opr impl so setting other than param() can be modified
  265. Opr* opr() {
  266. if (!m_opr_cur) {
  267. m_opr_cur = m_handle_cur->create_operator<Opr>();
  268. }
  269. return m_opr_cur.get();
  270. }
  271. //! whether previous exec succeeds
  272. bool prev_succ() const { return m_prev_succ; }
  273. private:
  274. BeforeExecCallback m_before_exec_callback;
  275. Param m_param;
  276. Proxy m_naive_proxy, m_cur_proxy;
  277. std::unique_ptr<Opr> m_opr_cur;
  278. };
  279. ::testing::AssertionResult __assert_tensor_eq(
  280. const char* expr0, const char* expr1, const char* expr_maxerr,
  281. const char* expr_maxerr_avg, const char* expr_maxerr_avg_biased,
  282. const TensorND& v0, const TensorND& v1, float maxerr, float maxerr_avg,
  283. float maxerr_avg_biased);
  284. #define MEGDNN_ASSERT_TENSOR_EQ_EPS_AVG(v0, v1, maxerr, maxerr_avg, \
  285. maxerr_avg_biased) \
  286. ASSERT_PRED_FORMAT5(::megdnn::test::__assert_tensor_eq, v0, v1, maxerr, \
  287. maxerr_avg, maxerr_avg_biased)
  288. #define MEGDNN_ASSERT_TENSOR_EQ_EPS(v0, v1, maxerr) \
  289. MEGDNN_ASSERT_TENSOR_EQ_EPS_AVG(v0, v1, maxerr, maxerr, maxerr)
  290. #define MEGDNN_ASSERT_TENSOR_EQ(v0, v1) \
  291. MEGDNN_ASSERT_TENSOR_EQ_EPS(v0, v1, 1e-3)
  292. template <typename Opr, typename Proxy>
  293. void Checker<Opr, Proxy>::exec(TensorLayoutArray layouts) {
  294. auto opr_naive = m_handle_naive->create_operator<Opr>();
  295. auto opr_relayout = m_handle_naive->create_operator<RelayoutForward>();
  296. auto opr_cur = this->opr();
  297. opr_naive->param() = m_param;
  298. opr_cur->param() = m_param;
  299. m_naive_proxy.deduce_layout(opr_naive.get(), layouts);
  300. auto exec_naive = [this, &opr_naive, &layouts,
  301. &opr_relayout](const TensorValueArray& values) {
  302. TensorValueArray contig_values = values;
  303. TensorValueArray real_values = values;
  304. std::shared_ptr<TensorValueArray> tensors_naive_contig_storage;
  305. if (m_enable_contig_naive) {
  306. TensorLayoutArray contig_layouts;
  307. for (auto&& layout : layouts) {
  308. contig_layouts.emplace_back(TensorLayout{
  309. static_cast<const TensorShape&>(layout), layout.dtype});
  310. }
  311. m_naive_proxy.deduce_layout(opr_naive.get(), contig_layouts);
  312. tensors_naive_contig_storage = alloc_tensors(
  313. m_handle_naive.get(), contig_layouts, m_offset);
  314. contig_values = *tensors_naive_contig_storage;
  315. //! relayout value to the contig_values
  316. for (size_t i = 0; i < contig_values.size(); ++i) {
  317. if (real_values[i].layout.ndim == 0)
  318. continue;
  319. real_values[i].layout.format = {};
  320. opr_relayout->exec(real_values[i], contig_values[i],
  321. m_handle_naive.get());
  322. }
  323. }
  324. m_naive_proxy.exec(opr_naive.get(), contig_values);
  325. if (m_enable_contig_naive) {
  326. //! relayout to the values
  327. for (size_t i = 0; i < contig_values.size(); ++i) {
  328. if (real_values[i].layout.ndim == 0)
  329. continue;
  330. opr_relayout->exec(contig_values[i], real_values[i],
  331. m_handle_naive.get());
  332. }
  333. }
  334. };
  335. auto exec_opr = [this, opr_cur](const TensorValueArray& values) {
  336. if (m_before_exec_callback) {
  337. m_before_exec_callback(opr_cur, values);
  338. }
  339. m_cur_proxy.exec(opr_cur, values);
  340. };
  341. auto user_layouts = layouts;
  342. do_exec(user_layouts, layouts, exec_naive, exec_opr);
  343. }
  344. template <typename Opr, typename Proxy>
  345. Checker<Opr, Proxy>& Checker<Opr, Proxy>::exect(
  346. const TensorValueArray& testcase_in,
  347. const TensorValueArray& testcase_out) {
  348. auto opr_cur = this->opr();
  349. opr_cur->param() = m_param;
  350. auto exec_opr = [this, opr_cur](const TensorValueArray& values) {
  351. if (m_before_exec_callback) {
  352. m_before_exec_callback(opr_cur, values);
  353. }
  354. m_cur_proxy.exec(opr_cur, values);
  355. };
  356. do_exec_with_testcases(testcase_in, testcase_out, exec_opr);
  357. return *this;
  358. }
  359. template <typename T, typename U>
  360. TensorND TensorValue(const TensorShape& shape, T dtype,
  361. std::initializer_list<U> values) {
  362. TensorND tensor;
  363. tensor.layout = {shape, dtype};
  364. tensor.raw_ptr =
  365. static_cast<dt_byte*>(malloc(tensor.layout.span().dist_byte()));
  366. megdnn_assert(values.size() == tensor.layout.total_nr_elems(), "%zu == %zu",
  367. values.size(), tensor.layout.total_nr_elems());
  368. auto ptr = tensor.ptr<typename DTypeTrait<T>::ctype>();
  369. for (const auto& v : values) {
  370. *ptr++ = typename DTypeTrait<T>::ctype(v);
  371. }
  372. return tensor;
  373. }
  374. template <typename T, typename U>
  375. TensorND TensorValueLowbit4(const TensorShape& shape, T dtype,
  376. std::vector<U> values) {
  377. TensorND tensor;
  378. tensor.layout = {shape, dtype};
  379. tensor.raw_ptr =
  380. static_cast<dt_byte*>(malloc(tensor.layout.span().dist_byte()));
  381. megdnn_assert(values.size() == tensor.layout.total_nr_elems());
  382. auto ptr = tensor.ptr<typename DTypeTrait<T>::ctype>();
  383. auto layout = tensor.layout;
  384. auto dim_in = shape[layout.ndim - 1];
  385. auto elems = tensor.layout.total_nr_elems();
  386. auto dim_out = elems / dim_in;
  387. auto stride_out = div_ceil(dim_in, 2_z);
  388. size_t in_offset = 0;
  389. for (size_t i = 0; i < dim_out; ++i) {
  390. for (size_t j = 0; j < dim_in; j += 2) {
  391. U a = values[in_offset + j];
  392. U b = 0;
  393. if (j + 1 < dim_in)
  394. b = values[in_offset + j + 1];
  395. megdnn_assert(a >= DTypeTrait<T>::min());
  396. megdnn_assert(a <= DTypeTrait<T>::max());
  397. megdnn_assert(b >= DTypeTrait<T>::min());
  398. megdnn_assert(b <= DTypeTrait<T>::max());
  399. ptr[j / 2] = (a & 0xF) | (b << 4);
  400. }
  401. in_offset += dim_in;
  402. ptr += stride_out;
  403. }
  404. return tensor;
  405. }
  406. class Testcase : public SmallVector<TensorND> {
  407. public:
  408. using SmallVector<TensorND>::SmallVector;
  409. ~Testcase() {
  410. // Suicide
  411. for (const auto& tensor : *this) {
  412. if (tensor.raw_ptr) {
  413. free(tensor.raw_ptr);
  414. }
  415. }
  416. }
  417. Testcase(const Testcase&) = delete;
  418. Testcase operator=(const Testcase&) = delete;
  419. };
  420. struct ExecutionPolicyAlgoName {
  421. std::string name;
  422. std::vector<ExecutionPolicyAlgoName> sub_policy_names;
  423. ExecutionPolicyAlgoName(const char* name) : name{name} {}
  424. ExecutionPolicyAlgoName(
  425. const char* name,
  426. const std::vector<ExecutionPolicyAlgoName>& sub_policy)
  427. : name{name}, sub_policy_names{sub_policy} {}
  428. };
  429. /*!
  430. * \brief a callable to check that given algorithm is used for heuristic
  431. * \param require_algo if its value is true, then requires
  432. * get_algorithm_heuristic() to return the expected algo; otherwise the
  433. * expected algo must exist in get_all_algorithms() and it would be set to
  434. * be used
  435. */
  436. template <class Opr, typename OprAlgoProxy = OprAlgoProxy<Opr>>
  437. class AlgoChecker {
  438. public:
  439. AlgoChecker(ExecutionPolicyAlgoName name, bool* require_algo = nullptr)
  440. : m_policy_name{name}, m_require_algo{require_algo} {}
  441. AlgoChecker(ExecutionPolicy policy, bool* require_algo = nullptr)
  442. : m_policy{policy}, m_require_algo{require_algo} {}
  443. static ExecutionPolicy construct_execution_policy_from_name(
  444. const ExecutionPolicyAlgoName& policy_name,
  445. const TensorLayoutArray& layouts, const std::string& param,
  446. Handle* handle) {
  447. ExecutionPolicy ret;
  448. megdnn_assert(layouts.size() == OprTrait<Opr>::arity);
  449. auto opr = handle->create_operator<Opr>();
  450. opr->param() =
  451. Algorithm::deserialize_read_pod<typename Opr::Param>(param);
  452. for (auto algo_info :
  453. AlgoProxy<Opr, OprTrait<Opr>::arity>::get_all_algorithms_info(
  454. opr.get(), layouts)) {
  455. if (std::regex_match(
  456. algo_info.desc.name,
  457. std::regex("(" + policy_name.name + ")(.*)"))) {
  458. ret.algo = algo_info.desc;
  459. } else {
  460. continue;
  461. }
  462. Algorithm* algo = opr->get_algorithm_from_desc(algo_info.desc);
  463. std::vector<Algorithm::SearchItem>&& sub_items =
  464. algo->get_subopr_list(layouts, opr.get());
  465. if (sub_items.size() != policy_name.sub_policy_names.size()) {
  466. printf("Invalid sub_policy_names in %s, expected %zu but got "
  467. "%zu\n",
  468. algo_info.desc.name.c_str(), sub_items.size(),
  469. policy_name.sub_policy_names.size());
  470. return {};
  471. }
  472. FOREACH_OPR_TYPE_DISPATCH(sub_items, {
  473. ExecutionPolicy policy =
  474. AlgoChecker<_Opr>::construct_execution_policy_from_name(
  475. policy_name.sub_policy_names[_item_idx],
  476. _item.layouts, _item.param, handle);
  477. ret.sub_policy.push_back(policy);
  478. });
  479. return ret;
  480. }
  481. return ret;
  482. }
  483. void operator()(Opr* opr, const CheckerHelper::TensorValueArray& arr) {
  484. TensorLayoutArray layouts;
  485. for (auto&& val : arr) {
  486. layouts.push_back(val.layout);
  487. }
  488. if (!m_policy_name.name.empty()) {
  489. std::string param_str;
  490. Algorithm::serialize_write_pod(opr->param(), param_str);
  491. m_policy = construct_execution_policy_from_name(
  492. m_policy_name, layouts, param_str, opr->handle());
  493. ASSERT_TRUE(m_policy.algo.valid())
  494. << "algorithm " << m_policy_name.name << " not found";
  495. }
  496. if (m_require_algo && *m_require_algo) {
  497. auto algo =
  498. OprAlgoProxy::get_algorithm_info_heuristic(opr, layouts);
  499. ASSERT_STREQ(opr->get_algorithm_from_desc(m_policy.algo)->name(),
  500. algo.desc.name.c_str());
  501. } else {
  502. opr->execution_policy() = m_policy;
  503. }
  504. }
  505. private:
  506. ExecutionPolicyAlgoName m_policy_name;
  507. ExecutionPolicy m_policy;
  508. bool* m_require_algo;
  509. };
  510. template <typename Opr>
  511. void construct_sub_execution_policy_heuristic(ExecutionPolicy& policy,
  512. const TensorLayoutArray& layouts,
  513. const std::string& param,
  514. Handle* handle) {
  515. megdnn_assert(layouts.size() == OprTrait<Opr>::arity);
  516. auto opr = handle->create_operator<Opr>();
  517. opr->param() = Algorithm::deserialize_read_pod<typename Opr::Param>(param);
  518. if (!policy.algo.valid()) {
  519. policy.algo = AlgoProxy<Opr, OprTrait<Opr>::arity>::
  520. get_algorithm_info_heuristic(opr.get(), layouts)
  521. .desc;
  522. }
  523. Algorithm* algo = opr->get_algorithm_from_desc(policy.algo);
  524. std::vector<Algorithm::SearchItem>&& sub_items =
  525. algo->get_subopr_list(layouts, opr.get());
  526. FOREACH_OPR_TYPE_DISPATCH(sub_items, {
  527. policy.sub_policy.push_back(ExecutionPolicy{});
  528. construct_sub_execution_policy_heuristic<_Opr>(
  529. policy.sub_policy.back(), _item.layouts, _item.param, handle);
  530. });
  531. }
  532. } // namespace test
  533. } // namespace megdnn
  534. // vim: syntax=cpp.doxygen

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