checksum.cpp 2.7 KB
Newer Older
1 2 3 4
/**
 * \file dnn/test/cambricon/checksum.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#include "megdnn/oprs.h"
#include "test/cambricon/fixture.h"
#include "test/common/checker.h"

using namespace megdnn;
using namespace test;

TEST_F(CAMBRICON, CHECKSUM_FORWARD) {
M
Megvii Engine Team 已提交
20
    auto cambricon_opr = handle_cambricon()->create_operator<megdnn::Checksum>(),
21 22
         naive_opr = handle_naive()->create_operator<megdnn::Checksum>();
    std::mt19937 rng(std::random_device{}());
M
Megvii Engine Team 已提交
23
    for (size_t size : {3, 8, 4 * 4 * 1024, 12345, 1024 * 1024, 1024 * 1024 * 10}) {
24 25 26
        auto aligned_size = size + ((512 - size % 512) % 512);
        auto run = [&](megdnn::Checksum* opr, void* ptr, bool log_size) {
            TensorND tensor;
27
            tensor.reset_ptr(ptr);
28 29 30
            tensor.layout.init_contiguous_stride({size});
            tensor.layout.dtype = dtype::Byte();
            WorkspaceWrapper workspace(
M
Megvii Engine Team 已提交
31
                    handle_cambricon(), opr->get_workspace_in_bytes(tensor.layout));
32 33 34 35 36 37 38 39 40 41
            if (log_size) {
                printf("checksum(%zu): workspace=%zu\n", size,
                       workspace.workspace().size);
            }
            return opr->exec(tensor, workspace.workspace());
        };
        std::vector<uint8_t> buf(aligned_size);
        for (size_t i = 0; i < size; ++i)
            buf[i] = 1;
        auto run_offsset = [&](size_t offset) {
M
Megvii Engine Team 已提交
42
            void* dev_ptr = megdnn_malloc(handle_cambricon(), buf.size() + offset);
43 44 45 46 47 48 49 50
            void* dev_buf = static_cast<char*>(dev_ptr) + offset;

            Checksum::Result res_cambricon[2], res_naive[2];

            for (int change_last = 0; change_last < 2; ++change_last) {
                if (change_last)
                    ++buf[size - 1];

M
Megvii Engine Team 已提交
51
                megdnn_memcpy_H2D(handle_cambricon(), dev_buf, buf.data(), size);
52 53
                res_cambricon[change_last] =
                        run(cambricon_opr.get(), dev_buf, !change_last);
M
Megvii Engine Team 已提交
54
                res_naive[change_last] = run(naive_opr.get(), buf.data(), false);
55 56 57 58
            }

            megdnn_free(handle_cambricon(), dev_ptr);

M
Megvii Engine Team 已提交
59
            ASSERT_EQ(res_naive[0], res_cambricon[0]) << "failed for size " << size;
60 61 62 63 64 65 66 67 68 69 70
            ASSERT_EQ(res_naive[1], res_cambricon[1]);
            ASSERT_NE(res_cambricon[0], res_cambricon[1]);
        };

        for (size_t i = 0; i < 8; ++i) {
            run_offsset(i);
        }
    }
}

// vim: syntax=cpp.doxygen