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.

accuracy_shake_checker.cpp 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * \file dnn/test/common/accuracy_shake_checker.cpp
  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. #include "test/common/accuracy_shake_checker.h"
  13. using namespace megdnn;
  14. using namespace test;
  15. namespace {
  16. template <typename ctype>
  17. ::testing::AssertionResult assert_tensor_binary_eq(
  18. const char* expr0, const char* expr1, const char* /*expr2*/, const TensorND& v0,
  19. const TensorND& v1, const std::string& algo_name) {
  20. ctype* it0_orig = v0.ptr<ctype>();
  21. ctype* it1 = v1.ptr<ctype>();
  22. ctype* it0 = it0_orig;
  23. auto nr_elem = v1.layout.total_nr_elems();
  24. auto nr_elem_single_batch = v0.layout.total_nr_elems();
  25. for (size_t i = 0; i < nr_elem; ++i) {
  26. if (i % nr_elem_single_batch == 0) {
  27. it0 = it0_orig;
  28. }
  29. ctype iv0 = *it0, iv1 = *it1;
  30. if (!good_float(iv0) || !good_float(iv1) || memcmp(it0, it1, sizeof(ctype))) {
  31. Index index(v1.layout, i);
  32. return ::testing::AssertionFailure()
  33. << "Unequal value\n"
  34. << "Value of: " << expr1 << "\n"
  35. << " Actual: " << (iv1 + 0) << "\n"
  36. << "Expected: " << expr0 << "\n"
  37. << "Which is: " << (iv0 + 0) << "\n"
  38. << "At index: " << index.to_string() << "/"
  39. << v1.layout.TensorShape::to_string() << "\n"
  40. << " DType: " << v1.layout.dtype.name() << "\n"
  41. << "algo: " << algo_name;
  42. }
  43. ++it0;
  44. ++it1;
  45. }
  46. return ::testing::AssertionSuccess();
  47. }
  48. } // namespace
  49. ::testing::AssertionResult test::__assert_tensor_binary_eq(
  50. const char* expr0, const char* expr1, const char* expr2, const TensorND& v0,
  51. const TensorND& v1, const Algorithm::Info::Desc& algo) {
  52. bool shape_match = v0.layout[0] == 1;
  53. for (size_t i = 1; i < v0.layout.ndim; ++i) {
  54. shape_match &= v0.layout[i] == v1.layout[i];
  55. }
  56. if (!shape_match) {
  57. return ::testing::AssertionFailure()
  58. << "Shape mismatch\n"
  59. << "Value of: " << expr1 << "\n"
  60. << " Actual: " << v1.layout.TensorShape::to_string() << "\n"
  61. << "Expected: " << expr0 << "\n"
  62. << "Which is: " << v0.layout.TensorShape::to_string() << "\n"
  63. << "algo: " << algo.name << "\n";
  64. }
  65. if (!v0.layout.is_physical_contiguous() || !v1.layout.is_physical_contiguous()) {
  66. return ::testing::AssertionFailure()
  67. << "layout should be physical contiguous\n"
  68. << "Value of: " << expr1 << "\n"
  69. << " Actual: " << v1.layout.is_physical_contiguous() << "\n"
  70. << "Expected: " << expr0 << "\n"
  71. << "Which is: " << v0.layout.is_physical_contiguous() << "\n"
  72. << "algo: " << algo.name << "\n";
  73. }
  74. auto dtype = v0.layout.dtype;
  75. if (dtype != v1.layout.dtype) {
  76. return ::testing::AssertionFailure()
  77. << "Data type should match\n"
  78. << "Value of: " << expr1 << "\n"
  79. << " Actual: " << v1.layout.dtype.name() << "\n"
  80. << "Expected: " << expr0 << "\n"
  81. << "Which is: " << v0.layout.dtype.name() << "\n"
  82. << "algo: " << algo.name << "\n";
  83. }
  84. switch (dtype.enumv()) {
  85. #define cb(_dt) \
  86. case DTypeTrait<_dt>::enumv: \
  87. return assert_tensor_binary_eq<DTypeTrait<_dt>::ctype>( \
  88. expr0, expr1, expr2, v0, v1, algo.name);
  89. MEGDNN_FOREACH_COMPUTING_DTYPE(cb)
  90. MEGDNN_FOREACH_QUANTIZED_DTYPE(cb)
  91. #undef cb
  92. default:
  93. megdnn_trap();
  94. }
  95. }
  96. // vim: syntax=cpp.doxygen

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