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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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-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 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 = handle_cambricon()->create_operator<megdnn::Checksum>(),
  18. naive_opr = handle_naive()->create_operator<megdnn::Checksum>();
  19. std::mt19937 rng(std::random_device{}());
  20. for (size_t size : {3, 8, 4 * 4 * 1024, 12345, 1024 * 1024, 1024 * 1024 * 10}) {
  21. auto aligned_size = size + ((512 - size % 512) % 512);
  22. auto run = [&](megdnn::Checksum* opr, void* ptr, bool log_size) {
  23. TensorND tensor;
  24. tensor.reset_ptr(ptr);
  25. tensor.layout.init_contiguous_stride({size});
  26. tensor.layout.dtype = dtype::Byte();
  27. WorkspaceWrapper workspace(
  28. handle_cambricon(), opr->get_workspace_in_bytes(tensor.layout));
  29. if (log_size) {
  30. printf("checksum(%zu): workspace=%zu\n", size,
  31. workspace.workspace().size);
  32. }
  33. return opr->exec(tensor, workspace.workspace());
  34. };
  35. std::vector<uint8_t> buf(aligned_size);
  36. for (size_t i = 0; i < size; ++i)
  37. buf[i] = 1;
  38. auto run_offsset = [&](size_t offset) {
  39. void* dev_ptr = megdnn_malloc(handle_cambricon(), buf.size() + offset);
  40. void* dev_buf = static_cast<char*>(dev_ptr) + offset;
  41. Checksum::Result res_cambricon[2], res_naive[2];
  42. for (int change_last = 0; change_last < 2; ++change_last) {
  43. if (change_last)
  44. ++buf[size - 1];
  45. megdnn_memcpy_H2D(handle_cambricon(), dev_buf, buf.data(), size);
  46. res_cambricon[change_last] =
  47. run(cambricon_opr.get(), dev_buf, !change_last);
  48. res_naive[change_last] = run(naive_opr.get(), buf.data(), false);
  49. }
  50. megdnn_free(handle_cambricon(), dev_ptr);
  51. ASSERT_EQ(res_naive[0], res_cambricon[0]) << "failed for size " << size;
  52. ASSERT_EQ(res_naive[1], res_cambricon[1]);
  53. ASSERT_NE(res_cambricon[0], res_cambricon[1]);
  54. };
  55. for (size_t i = 0; i < 8; ++i) {
  56. run_offsset(i);
  57. }
  58. }
  59. }
  60. // vim: syntax=cpp.doxygen

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