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

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