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.1 kB

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

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