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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "hcc_detail/hcc_defs_prologue.h"
  2. #include "megdnn/oprs.h"
  3. #include "test/common/checker.h"
  4. #include "test/rocm/fixture.h"
  5. using namespace megdnn;
  6. using namespace test;
  7. TEST_F(ROCM, CHECKSUM_FORWARD) {
  8. auto rocm_opr = handle_rocm()->create_operator<megdnn::Checksum>(),
  9. naive_opr = handle_naive()->create_operator<megdnn::Checksum>();
  10. std::mt19937 rng(std::random_device{}());
  11. for (size_t size : {3, 8, 12345, 1024 * 1024, 1024 * 1024 * 10}) {
  12. auto aligned_size = size + ((512 - size % 512) % 512);
  13. auto run = [&](megdnn::Checksum* opr, void* ptr, bool log_size) {
  14. TensorND tensor;
  15. tensor.reset_ptr(ptr);
  16. tensor.layout.init_contiguous_stride({size});
  17. tensor.layout.dtype = dtype::Byte();
  18. WorkspaceWrapper workspace(
  19. handle_rocm(), opr->get_workspace_in_bytes(tensor.layout));
  20. if (log_size) {
  21. printf("checksum(%zu): workspace=%zu\n", size,
  22. workspace.workspace().size);
  23. }
  24. return opr->exec(tensor, workspace.workspace());
  25. };
  26. std::vector<uint8_t> buf(aligned_size);
  27. for (size_t i = 0; i < size; ++i)
  28. buf[i] = rng();
  29. auto run_offsset = [&](size_t offset) {
  30. void* dev_ptr = megdnn_malloc(handle_rocm(), buf.size() + offset);
  31. void* dev_buf = static_cast<char*>(dev_ptr) + offset;
  32. Checksum::Result res_rocm[2], res_naive[2];
  33. for (int change_last = 0; change_last < 2; ++change_last) {
  34. if (change_last)
  35. ++buf[size - 1];
  36. megdnn_memcpy_H2D(handle_rocm(), dev_buf, buf.data(), size);
  37. res_rocm[change_last] = run(rocm_opr.get(), dev_buf, !change_last);
  38. res_naive[change_last] = run(naive_opr.get(), buf.data(), false);
  39. }
  40. megdnn_free(handle_rocm(), dev_ptr);
  41. ASSERT_EQ(res_naive[0], res_rocm[0]) << "failed for size " << size;
  42. ASSERT_EQ(res_naive[1], res_rocm[1]);
  43. ASSERT_NE(res_rocm[0], res_rocm[1]);
  44. };
  45. for (size_t i = 0; i < 8; ++i) {
  46. run_offsset(i);
  47. }
  48. }
  49. }
  50. // vim: syntax=cpp.doxygen