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.

checksum.cpp 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * \file dnn/test/cambricon/checksum.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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 implied.
  10. */
  11. #include "megdnn/oprs.h"
  12. #include "test/cambricon/fixture.h"
  13. #include "test/common/checker.h"
  14. using namespace megdnn;
  15. using namespace test;
  16. TEST_F(CAMBRICON, CHECKSUM_FORWARD) {
  17. auto cambricon_opr =
  18. handle_cambricon()->create_operator<megdnn::Checksum>(),
  19. naive_opr = handle_naive()->create_operator<megdnn::Checksum>();
  20. std::mt19937 rng(std::random_device{}());
  21. for (size_t size :
  22. {3, 8, 4 * 4 * 1024, 12345, 1024 * 1024, 1024 * 1024 * 10}) {
  23. auto aligned_size = size + ((512 - size % 512) % 512);
  24. auto run = [&](megdnn::Checksum* opr, void* ptr, bool log_size) {
  25. TensorND tensor;
  26. tensor.raw_ptr = ptr;
  27. tensor.layout.init_contiguous_stride({size});
  28. tensor.layout.dtype = dtype::Byte();
  29. WorkspaceWrapper workspace(
  30. handle_cambricon(),
  31. opr->get_workspace_in_bytes(tensor.layout));
  32. if (log_size) {
  33. printf("checksum(%zu): workspace=%zu\n", size,
  34. workspace.workspace().size);
  35. }
  36. return opr->exec(tensor, workspace.workspace());
  37. };
  38. std::vector<uint8_t> buf(aligned_size);
  39. for (size_t i = 0; i < size; ++i)
  40. buf[i] = 1;
  41. auto run_offsset = [&](size_t offset) {
  42. void* dev_ptr =
  43. megdnn_malloc(handle_cambricon(), buf.size() + offset);
  44. void* dev_buf = static_cast<char*>(dev_ptr) + offset;
  45. Checksum::Result res_cambricon[2], res_naive[2];
  46. for (int change_last = 0; change_last < 2; ++change_last) {
  47. if (change_last)
  48. ++buf[size - 1];
  49. megdnn_memcpy_H2D(handle_cambricon(), dev_buf, buf.data(),
  50. size);
  51. res_cambricon[change_last] =
  52. run(cambricon_opr.get(), dev_buf, !change_last);
  53. res_naive[change_last] =
  54. run(naive_opr.get(), buf.data(), false);
  55. }
  56. megdnn_free(handle_cambricon(), dev_ptr);
  57. ASSERT_EQ(res_naive[0], res_cambricon[0])
  58. << "failed for size " << size;
  59. ASSERT_EQ(res_naive[1], res_cambricon[1]);
  60. ASSERT_NE(res_cambricon[0], res_cambricon[1]);
  61. };
  62. for (size_t i = 0; i < 8; ++i) {
  63. run_offsset(i);
  64. }
  65. }
  66. }
  67. // vim: syntax=cpp.doxygen

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