no_memory_copy.cpp 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * \file src/gopt/test/no_memory_copy.cpp
 *
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
 *
 * 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 <memory>
#include "./network.h"
#include "megbrain/comp_node_env.h"
#include "megbrain/opr/basic_arith.h"
16
#include "megbrain/opr/tensor_manip.h"
17 18 19 20 21 22 23
#include "megbrain/test/helper.h"

using namespace mgb;

struct TestGraph {
    CompNode m_cn;
    HostTensorGenerator<> m_gen;
24
    HostTensorGenerator<dtype::Int32> m_gen_int;
25 26 27
    std::unique_ptr<Network> m_network;
    SymbolVar m_out_var;
    std::shared_ptr<HostTensorND> input_tensor;
28
    std::shared_ptr<HostTensorND> input_tensor2;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

    TestGraph() {
        m_cn = CompNode::load("cpu0");
        m_network = std::make_unique<Network>(m_cn);
    }

    void create_graph() {
        input_tensor = m_gen({1, 3, 32, 32}, m_cn);
        auto input = opr::Host2DeviceCopy::make(*m_network->graph, input_tensor, m_cn)
                             .rename("input");
        auto f = m_network->add_conv(
                input, 4, {3, 3}, dtype::Float32(), true, {2, 2}, {0, 0});
        f = m_network->add_elemwise(
                {f}, dtype::Float32(), opr::Elemwise::Param::Mode::EXP);
        f = m_network->add_conv(f, 8, {3, 3}, dtype::Float32(), true, {1, 1}, {1, 1});
44 45
        f = m_network->add_pooling(f, {2, 2}, {2, 2});
        m_out_var = m_network->add_concat(f, -f);
46 47
    }

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    void create_graph_with_subtensor_forward() {
        input_tensor = m_gen({2, 3, 32, 32}, m_cn);
        auto input = opr::Host2DeviceCopy::make(*m_network->graph, input_tensor, m_cn)
                             .rename("input");

        auto cv = [&](int v) {
            auto rst = input.make_scalar(v);
            return rst;
        };

        using Ad = opr::Subtensor::AxisIndexer;
        auto sub =
                opr::Subtensor::make(input, {Ad::make_interval(0, cv(1), cv(2), None)});

        auto f = m_network->add_conv(
                sub, 4, {3, 3}, dtype::Float32(), true, {2, 2}, {0, 0});
        f = m_network->add_elemwise(
                {f}, dtype::Float32(), opr::Elemwise::Param::Mode::EXP);
        f = m_network->add_conv(f, 8, {3, 3}, dtype::Float32(), true, {1, 1}, {1, 1});
67 68
        f = m_network->add_pooling(f, {2, 2}, {2, 2});
        m_out_var = m_network->add_concat(f, -f);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    }

    void create_graph_with_subtensor_relayout() {
        input_tensor = m_gen({2, 3, 32, 40}, m_cn);
        auto input = opr::Host2DeviceCopy::make(*m_network->graph, input_tensor, m_cn)
                             .rename("input");

        auto cv = [&](int v) {
            auto rst = input.make_scalar(v);
            return rst;
        };

        using Ad = opr::Subtensor::AxisIndexer;
        auto sub = opr::Subtensor::make(
                input, {Ad::make_interval(0, cv(1), cv(2), None),
                        Ad::make_interval(3, cv(0), cv(32), None)});

        auto f = m_network->add_conv(
                sub, 4, {3, 3}, dtype::Float32(), true, {2, 2}, {0, 0});
        f = m_network->add_elemwise(
                {f}, dtype::Float32(), opr::Elemwise::Param::Mode::EXP);
        f = m_network->add_conv(f, 8, {3, 3}, dtype::Float32(), true, {1, 1}, {1, 1});
91 92
        f = m_network->add_pooling(f, {2, 2}, {2, 2});
        m_out_var = m_network->add_concat(f, -f);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    }

    void create_graph_with_setsubtensor() {
        input_tensor = m_gen({1, 3, 32, 32}, m_cn);
        input_tensor2 = m_gen({1, 1, 32, 32}, m_cn);
        auto input = opr::Host2DeviceCopy::make(*m_network->graph, input_tensor, m_cn)
                             .rename("input");

        auto input_sub =
                opr::Host2DeviceCopy::make(*m_network->graph, input_tensor2, m_cn)
                        .rename("input2");

        auto cv = [&](int v) {
            auto rst = input.make_scalar(v);
            return rst;
        };

        using Ad = opr::Subtensor::AxisIndexer;
        input = opr::SetSubtensor::make(
                input, input_sub, {Ad::make_interval(1, cv(1), cv(2), None)});

        auto f = m_network->add_conv(
                input, 4, {3, 3}, dtype::Float32(), true, {2, 2}, {0, 0});
        f = m_network->add_elemwise(
                {f}, dtype::Float32(), opr::Elemwise::Param::Mode::EXP);
        f = m_network->add_conv(f, 8, {3, 3}, dtype::Float32(), true, {1, 1}, {1, 1});
119 120
        f = m_network->add_pooling(f, {2, 2}, {2, 2});
        m_out_var = m_network->add_concat(f, -f);
121 122
    }

123 124 125 126 127 128 129 130 131 132
    std::unique_ptr<cg::AsyncExecutable> compile_without_copy() {
        return m_network->graph->compile({{m_out_var, nullptr}});
    }

    std::unique_ptr<cg::AsyncExecutable> compile_with_copy(HostTensorND& host) {
        auto cb = [&host](const DeviceTensorND& dv) mutable { host.copy_from(dv); };
        return m_network->graph->compile({{m_out_var, std::move(cb)}});
    }
};

133 134
namespace {
void test_basic_input_no_copy(bool record) {
135
    auto test_graph = TestGraph();
136 137
    auto compute_graph = test_graph.m_network->graph;
    compute_graph->options().comp_node_seq_record_level = record;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    test_graph.create_graph();
    HostTensorND out, out_pre;
    auto func = test_graph.compile_with_copy(out);
    size_t times = 10;
    for (size_t i = 0; i < times; i++) {
        if (i % 2 == 0) {
            auto input_tensor = test_graph.input_tensor;
            auto layout = input_tensor->layout();
            size_t length = layout.total_nr_elems();
            auto storage = TensorStorage<HostTensorStorageTrait>(test_graph.m_cn);
            storage.ensure_size(length * sizeof(float));
            float* ptr = storage.ptr()->as<float>();
            for (size_t d = 0; d < length; d++) {
                ptr[d] = i;
            }
153 154 155 156 157
            if (record) {
                input_tensor->only_reset_raw_storage(storage);
            } else {
                input_tensor->reset(storage, layout);
            }
158 159 160 161 162 163 164 165 166
        }
        func->execute();
        func->wait();
        if (i % 2 != 0) {
            MGB_ASSERT_TENSOR_EQ(out, out_pre);
        }
        out_pre.copy_from(out).sync();
    }
}
167 168 169 170 171
}  // namespace

TEST(TestNoCopy, InputNoCopyPtrEQ) {
    test_basic_input_no_copy(0);
}
172 173 174 175

TEST(TestNoCopy, IONoCopyPtrEQ) {
    auto test_graph = TestGraph();
    auto compute_graph = test_graph.m_network->graph;
176
    compute_graph->options().force_output_use_user_specified_memory = true;
177 178 179
    test_graph.create_graph();
    auto func = test_graph.compile_without_copy();
    auto&& outvar = func->get_output_vars()[0];
180 181
    DeviceTensorND dv0(test_graph.m_cn, {2, 8, 7, 7});
    DeviceTensorND dv1(test_graph.m_cn, {2, 8, 7, 7});
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    size_t times = 10;
    for (size_t i = 0; i < times; i++) {
        auto input_tensor = test_graph.input_tensor;
        auto layout = input_tensor->layout();
        size_t length = layout.total_nr_elems();
        auto storage = TensorStorage<HostTensorStorageTrait>(test_graph.m_cn);
        storage.ensure_size(length * sizeof(float));
        float* ptr = storage.ptr()->as<float>();
        for (size_t d = 0; d < length; d++) {
            ptr[d] = i;
        }
        input_tensor->reset(storage, layout);
        if (i % 2 == 0) {
            outvar->init_mem_plan(&dv0);
            outvar->reset_dev_tensor_from_tensor(dv0);
        } else {
            outvar->init_mem_plan(&dv1);
            outvar->reset_dev_tensor_from_tensor(dv1);
        }

        func->execute();
        func->wait();
        auto out = func->get_output_vars()[0]->dev_tensor().ptr<float>();

        if (i % 2 == 0) {
            ASSERT_EQ(dv0.ptr<float>(), out);
        } else {
            ASSERT_EQ(dv1.ptr<float>(), out);
        }
    }
}

TEST(TestNoCopy, IONoCopyCorrect) {
    auto test_graph = TestGraph();
    auto compute_graph = test_graph.m_network->graph;
217
    compute_graph->options().force_output_use_user_specified_memory = true;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    test_graph.create_graph();
    HostTensorND truth;
    auto func = test_graph.compile_without_copy();
    //! because the output tensor not assign user memory, so it will wrong
    ASSERT_THROW(func->execute(), MegBrainError);
    auto&& outvar = func->get_output_vars()[0];
    size_t times = 10;
    for (size_t i = 0; i < times; i++) {
        auto input_tensor = test_graph.input_tensor;
        auto layout = input_tensor->layout();
        size_t length = layout.total_nr_elems();
        auto storage = TensorStorage<HostTensorStorageTrait>(test_graph.m_cn);
        storage.ensure_size(length * sizeof(float));
        float* ptr = storage.ptr()->as<float>();
        for (size_t d = 0; d < length; d++) {
            ptr[d] = i / 5 + 3;
        }
        input_tensor->reset(storage, layout);
236
        DeviceTensorND dv(test_graph.m_cn, {2, 8, 7, 7});
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        outvar->init_mem_plan(&dv);
        outvar->reset_dev_tensor_from_tensor(dv);

        func->execute();
        func->wait();
        if (i % 5 == 0) {
            truth.copy_from(func->get_output_vars()[0]->dev_tensor()).sync();
            continue;
        }
        HostTensorND to_check;
        to_check.copy_from(func->get_output_vars()[0]->dev_tensor()).sync();
        MGB_ASSERT_TENSOR_EQ(to_check, truth);
    }
}

252 253 254 255 256 257 258 259 260 261 262 263 264
TEST(TestNoCopy, InputNoCopyRecord) {
    test_basic_input_no_copy(1);
}

TEST(TestNoCopy, IONoCopyRecord) {
    auto test_graph = TestGraph();
    auto compute_graph = test_graph.m_network->graph;
    compute_graph->options().force_output_use_user_specified_memory = true;
    compute_graph->options().comp_node_seq_record_level = 1;
    test_graph.create_graph();
    HostTensorND truth;
    auto func = test_graph.compile_without_copy();
    auto&& outvar = func->get_output_vars()[0];
265
    DeviceTensorND tmp(test_graph.m_cn, {2, 8, 7, 7});
266 267 268 269 270 271 272 273 274 275 276 277 278
    outvar->init_mem_plan(&tmp);
    size_t times = 10;
    for (size_t i = 0; i < times; i++) {
        auto input_tensor = test_graph.input_tensor;
        auto layout = input_tensor->layout();
        size_t length = layout.total_nr_elems();
        auto storage = TensorStorage<HostTensorStorageTrait>(test_graph.m_cn);
        storage.ensure_size(length * sizeof(float));
        float* ptr = storage.ptr()->as<float>();
        for (size_t d = 0; d < length; d++) {
            ptr[d] = i / 5 + 3;
        }
        input_tensor->only_reset_raw_storage(storage);
279
        DeviceTensorND dv(test_graph.m_cn, {2, 8, 7, 7});
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        dv.raw_ptr();

        auto& dev_tensor = outvar->mutable_dev_tensor();
        dev_tensor.only_reset_raw_storage(dv.storage());

        func->execute();
        func->wait();
        if (i % 5 == 0) {
            truth.copy_from(dv).sync();
            continue;
        }
        HostTensorND to_check;
        to_check.copy_from(dv).sync();
        MGB_ASSERT_TENSOR_EQ(to_check, truth);
    }
}

namespace {
void test_subtensor_record(int level) {
    auto test_graph = TestGraph();
    auto compute_graph = test_graph.m_network->graph;
    compute_graph->options().force_output_use_user_specified_memory = true;
    compute_graph->options().comp_node_seq_record_level = 1;
    if (level == 2) {
        test_graph.create_graph_with_setsubtensor();
    } else if (level == 1) {
        test_graph.create_graph_with_subtensor_forward();
    } else {
        test_graph.create_graph_with_subtensor_relayout();
    }
    HostTensorND truth;
    auto func = test_graph.compile_without_copy();
    auto&& outvar = func->get_output_vars()[0];
313
    DeviceTensorND tmp(test_graph.m_cn, {2, 8, 7, 7});
314 315 316 317 318 319 320 321 322 323 324 325 326
    outvar->init_mem_plan(&tmp);
    size_t times = 10;
    for (size_t i = 0; i < times; i++) {
        auto input_tensor = test_graph.input_tensor;
        auto layout = input_tensor->layout();
        size_t length = layout.total_nr_elems();
        auto storage = TensorStorage<HostTensorStorageTrait>(test_graph.m_cn);
        storage.ensure_size(length * sizeof(float));
        float* ptr = storage.ptr()->as<float>();
        for (size_t d = 0; d < length; d++) {
            ptr[d] = i / 5 + 3;
        }
        input_tensor->only_reset_raw_storage(storage);
327
        DeviceTensorND dv(test_graph.m_cn, {2, 8, 7, 7});
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
        dv.raw_ptr();

        auto& dev_tensor = outvar->mutable_dev_tensor();
        dev_tensor.only_reset_raw_storage(dv.storage());

        func->execute();
        func->wait();
        if (i % 5 == 0) {
            truth.copy_from(dv).sync();
            continue;
        }
        HostTensorND to_check;
        to_check.copy_from(dv).sync();
        MGB_ASSERT_TENSOR_EQ(to_check, truth);
    }
}
}  // namespace

TEST(TestNoCopy, IONoCopyRecordSubTensor) {
    test_subtensor_record(0);
}

TEST(TestNoCopy, IONoCopyRecordSubTensorRelayout) {
    test_subtensor_record(1);
}
353

354 355 356 357 358
//! TODO: the test should fix compnode memory copy, which now not record reference
//! ptr, when support it, the test will pass
/*TEST(TestNoCopy, IONoCopyRecordSetSubTensor) {
    test_subtensor_record(2);
}*/
359 360

// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}