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

#include "test/common/deduce_layout_proxy.h"
#include "test/common/exec_proxy.h"
#include "test/common/inspect_type.h"
#include "test/common/opr_trait.h"
#include "test/common/timer.h"
#include "test/common/workspace_wrapper.h"

#include <algorithm>
22 23
#include <memory>

24 25 26 27 28 29 30 31 32 33 34 35 36
namespace megdnn {
namespace test {

template <typename Opr, size_t arity = OprTrait<Opr>::arity,
          bool has_workspace = OprTrait<Opr>::has_workspace,
          bool can_deduce_layout = OprTrait<Opr>::can_deduce_layout>
struct OprProxyDefaultImpl
        : public DeduceLayoutProxy<Opr, arity, can_deduce_layout>,
          public ExecProxy<Opr, arity, has_workspace> {};

template <typename Opr>
struct OprProxy : public OprProxyDefaultImpl<Opr> {};

37 38 39
template <typename Opr>
struct OprWeightPreprocessProxy : public OprProxyDefaultImpl<Opr> {};

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
template <typename Opr>
struct OprProxyVectorToSingle {};

template <>
struct OprProxy<ElemwiseForward> {
    static void deduce_layout(ElemwiseForward* opr,
                              TensorLayoutArray& layouts) {
        megdnn_assert(layouts.size() >= 2);
        auto inp = layouts;
        inp.pop_back();
        opr->deduce_layout(inp, layouts.back());
    }

    static void exec(ElemwiseForward* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() >= 2);
        auto inp = tensors;
        inp.pop_back();
        opr->exec(inp, tensors.back());
    }
};

template <>
struct OprProxy<ElemwiseMultiType> {
    static void deduce_layout(ElemwiseMultiType* opr,
                              TensorLayoutArray& layouts) {
        megdnn_assert(layouts.size() >= 2);
        auto inp = layouts;
        inp.pop_back();
        opr->deduce_layout(inp, layouts.back());
    }

    static void exec(ElemwiseMultiType* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() >= 2);
        auto inp = tensors;
        inp.pop_back();
        opr->exec(inp, tensors.back());
    }
};

template <>
struct OprProxy<ConcatForward> {
    static void deduce_layout(ConcatForward* opr, TensorLayoutArray& layouts) {
        megdnn_assert(layouts.size() >= 2);
        auto inp = layouts;
        inp.pop_back();
        opr->deduce_layout(inp, layouts.back());
    }

    static void exec(ConcatForward* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() >= 2);
        auto inp = tensors;
        inp.pop_back();

        TensorLayoutArray layouts(tensors.size());
        std::transform(tensors.begin(), tensors.end(), layouts.begin(),
                       [](const TensorND& tensor) { return tensor.layout; });
        auto inp_layouts = layouts;
        inp_layouts.pop_back();

        WorkspaceWrapper W(opr->handle(), opr->get_workspace_in_bytes(
                                                  inp_layouts, layouts.back()));

        auto inp_tensors = tensors;
        inp_tensors.pop_back();
        opr->exec(inp_tensors, tensors.back(), W.workspace());
    }
};

template <>
struct OprProxy<SplitForward> : DeduceLayoutProxy<SplitForward, 0, false> {
    static void exec(SplitForward* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() >= 2);
        auto out = tensors;
        out.erase(out.begin());

        TensorLayoutArray layouts(tensors.size());
        std::transform(tensors.begin(), tensors.end(), layouts.begin(),
                       [](const TensorND& tensor) { return tensor.layout; });
        auto out_layouts = layouts;
        out_layouts.erase(out_layouts.begin());

        WorkspaceWrapper W(
                opr->handle(),
                opr->get_workspace_in_bytes(layouts.front(), out_layouts));

        auto out_tensors = tensors;
        out_tensors.erase(out_tensors.begin());
        opr->exec(tensors.front(), out_tensors, W.workspace());
    }
};

//! OprProxy impl for tenary oprs with profiling support
template <class Opr, int arity>
struct OprProxyProfilingBase
        : public DeduceLayoutProxy<Opr, arity,
                                   OprTrait<Opr>::can_deduce_layout> {
    size_t warmup_times = 10, exec_times = 100;

    //! whether to enable profiling
    bool m_profiling;
    WorkspaceWrapper W;

    //! target algo setup by profiler; it can also be directly specified by the
    //! caller
144
    typename Opr::AlgorithmInfo target_algo_info;
145 146

    OprProxyProfilingBase(bool profile = false) { m_profiling = profile; }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

    //! used for alloc tensor for weight preprocess
    static std::shared_ptr<TensorNDArray> alloc_tensors(
            Handle* handle, const TensorLayoutArray& layouts) {
        auto deleter = [handle](TensorNDArray* ptr) {
            for (auto&& i : *ptr) {
                auto pdata = static_cast<dt_byte*>(i.raw_ptr) +
                             i.layout.span().low_byte;
                megdnn_free(handle, pdata);
            }
            delete ptr;
        };
        std::shared_ptr<TensorNDArray> ret{new TensorNDArray, deleter};
        for (size_t i = 0; i < layouts.size(); ++i) {
            auto span = layouts[i].span();
            ret->emplace_back(static_cast<dt_byte*>(
                                      megdnn_malloc(handle, span.dist_byte())) -
                                      span.low_byte,
                              layouts[i]);
        }
        return ret;
    }
169 170 171 172 173 174 175 176 177 178 179
};

template <class Opr>
struct OprProxyProfilingTernary : public OprProxyProfilingBase<Opr, 3> {
    using Base = OprProxyProfilingBase<Opr, 3>;
    using OprProxyProfilingBase<Opr, 3>::OprProxyProfilingBase;
    void exec(Opr* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() == 3);
        if (!Base::W.valid()) {
            Base::W = WorkspaceWrapper(opr->handle(), 0);
        }
180
        if (Base::m_profiling && !Base::target_algo_info.valid()) {
181
            size_t min_time = std::numeric_limits<size_t>::max();
182 183 184 185
            for (auto algo : opr->get_all_algorithms_info(tensors[0].layout,
                                                          tensors[1].layout,
                                                          tensors[2].layout)) {
                opr->execution_policy().algo = algo;
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
                auto workspace_size = opr->get_workspace_in_bytes(
                        tensors[0].layout, tensors[1].layout,
                        tensors[2].layout);
                Base::W.update(workspace_size);

                for (size_t times = 0; times < Base::warmup_times; ++times)
                    opr->exec(tensors[0], tensors[1], tensors[2],
                              Base::W.workspace());
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                Timer timer;
                timer.start();
                for (size_t times = 0; times < Base::exec_times; ++times) {
                    opr->exec(tensors[0], tensors[1], tensors[2],
                              Base::W.workspace());
                }
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                timer.stop();
                printf("%.3fms %s\n", timer.get_time_in_us() / 1e3,
204
                       algo.name.c_str());
205 206
                if (min_time > timer.get_time_in_us()) {
                    min_time = timer.get_time_in_us();
207
                    Base::target_algo_info = algo;
208 209
                }
            }
210
            opr->execution_policy().algo = Base::target_algo_info;
211 212 213 214
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout);
            Base::W.update(workspace_size);
        }
215
        if (!Base::target_algo_info.valid()) {
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout);
            Base::W.update(workspace_size);
        }
        opr->exec(tensors[0], tensors[1], tensors[2], Base::W.workspace());
    }
};

#define DEF_PROF3(c)                                                 \
    template <>                                                      \
    struct OprProxy<c> : public OprProxyProfilingTernary<c> {        \
        using OprProxyProfilingTernary<c>::OprProxyProfilingTernary; \
    }

DEF_PROF3(ConvolutionBackwardData);
DEF_PROF3(ConvolutionBackwardFilter);
DEF_PROF3(LocalShareForward);
DEF_PROF3(LocalShareBackwardData);
DEF_PROF3(LocalShareBackwardFilter);
#undef DEF_PROF3

237 238 239
template <>
struct OprProxy<ConvolutionForward>
        : public OprProxyProfilingTernary<ConvolutionForward> {
240 241
    using OprProxyProfilingTernary<
            ConvolutionForward>::OprProxyProfilingTernary;
242 243 244 245 246
    void exec(ConvolutionForward* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() == 3);
        if (!Base::W.valid()) {
            Base::W = WorkspaceWrapper(opr->handle(), 0);
        }
247
        if (Base::m_profiling && !Base::target_algo_info.desc.valid()) {
248
            size_t min_time = std::numeric_limits<size_t>::max();
249 250 251 252
            for (auto algo : opr->get_all_algorithms_info(tensors[0].layout,
                                                          tensors[1].layout,
                                                          tensors[2].layout)) {
                opr->execution_policy().algo = algo;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
                auto workspace_size = opr->get_workspace_in_bytes(
                        tensors[0].layout, tensors[1].layout, tensors[2].layout,
                        nullptr);
                Base::W.update(workspace_size);

                for (size_t times = 0; times < Base::warmup_times; ++times)
                    opr->exec(tensors[0], tensors[1], tensors[2], nullptr,
                              Base::W.workspace());
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                Timer timer;
                timer.start();
                for (size_t times = 0; times < Base::exec_times; ++times) {
                    opr->exec(tensors[0], tensors[1], tensors[2], nullptr,
                              Base::W.workspace());
                }
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                timer.stop();
                printf("%.3fms %s\n", timer.get_time_in_us() / 1e3,
271
                       algo.name.c_str());
272 273
                if (min_time > timer.get_time_in_us()) {
                    min_time = timer.get_time_in_us();
274
                    Base::target_algo_info = algo;
275 276
                }
            }
277
            opr->execution_policy().algo = Base::target_algo_info;
278
            auto workspace_size = opr->get_workspace_in_bytes(
279 280
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    nullptr);
281 282
            Base::W.update(workspace_size);
        }
283
        if (!Base::target_algo_info.desc.valid()) {
284 285 286 287 288 289 290 291 292 293
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    nullptr);
            Base::W.update(workspace_size);
        }
        opr->exec(tensors[0], tensors[1], tensors[2], nullptr,
                  Base::W.workspace());
    }
};

294 295 296
template <>
struct OprWeightPreprocessProxy<ConvolutionForward>
        : public OprProxyProfilingTernary<ConvolutionForward> {
297 298
    using OprProxyProfilingTernary<
            ConvolutionForward>::OprProxyProfilingTernary;
299 300 301 302 303
    void exec(ConvolutionForward* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() == 3);
        if (!Base::W.valid()) {
            Base::W = WorkspaceWrapper(opr->handle(), 0);
        }
304
        if (Base::m_profiling && !Base::target_algo_info.desc.valid()) {
305
            size_t min_time = std::numeric_limits<size_t>::max();
306 307 308 309
            for (auto algo : opr->get_all_algorithms_info(tensors[0].layout,
                                                          tensors[1].layout,
                                                          tensors[2].layout)) {
                opr->execution_policy().algo = algo;
310

311 312
                auto preprocess_tensors =
                        weight_prerocess(opr, tensors, algo.desc);
313 314
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                ConvolutionForward::PreprocessedFilter preprocessed_filter{
315
                        nullptr, *preprocess_tensors};
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

                auto workspace_size = opr->get_workspace_in_bytes(
                        tensors[0].layout, tensors[1].layout, tensors[2].layout,
                        &preprocessed_filter);
                Base::W.update(workspace_size);

                for (size_t times = 0; times < Base::warmup_times; ++times)
                    opr->exec(tensors[0], tensors[1], tensors[2],
                              &preprocessed_filter, Base::W.workspace());
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                Timer timer;
                timer.start();
                for (size_t times = 0; times < Base::exec_times; ++times) {
                    opr->exec(tensors[0], tensors[1], tensors[2],
                              &preprocessed_filter, Base::W.workspace());
                }
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                timer.stop();
                printf("%.3fms %s\n", timer.get_time_in_us() / 1e3,
335
                       algo.name.c_str());
336 337
                if (min_time > timer.get_time_in_us()) {
                    min_time = timer.get_time_in_us();
338
                    Base::target_algo_info = algo;
339 340
                }
            }
341
            opr->execution_policy().algo = Base::target_algo_info;
342
            auto preprocess_tensors =
343
                    weight_prerocess(opr, tensors, Base::target_algo_info.desc);
344 345
            megcoreSynchronize(opr->handle()->megcore_computing_handle());
            ConvolutionForward::PreprocessedFilter preprocessed_filter{
346
                    nullptr, *preprocess_tensors};
347 348 349 350 351 352
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    &preprocessed_filter);
            Base::W.update(workspace_size);
        }
        auto preprocess_tensors =
353
                weight_prerocess(opr, tensors, Base::target_algo_info.desc);
354 355
        megcoreSynchronize(opr->handle()->megcore_computing_handle());
        ConvolutionForward::PreprocessedFilter preprocessed_filter{
356 357
                nullptr, *preprocess_tensors};
        if (!Base::target_algo_info.valid()) {
358 359 360 361 362 363 364 365 366 367 368 369
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    &preprocessed_filter);
            Base::W.update(workspace_size);
        }
        opr->exec(tensors[0], tensors[1], tensors[2], &preprocessed_filter,
                  Base::W.workspace());
    }

    //! handle weight preprocess
    std::shared_ptr<TensorNDArray> weight_prerocess(
            ConvolutionForward* opr, const TensorNDArray& tensors,
370
            const ConvolutionForward::AlgorithmDesc&) {
371 372 373 374 375
        auto weight_perprocess_layouts = opr->deduce_preprocessed_filter_layout(
                tensors[0].layout, tensors[1].layout, tensors[2].layout);
        auto preprocessed_filter_tensors_ptr =
                alloc_tensors(opr->handle(), weight_perprocess_layouts);
        ConvolutionForward::PreprocessedFilter preprocessed_filter{
376
                nullptr, *preprocessed_filter_tensors_ptr};
377 378 379 380 381 382 383 384 385 386 387 388 389
        size_t preprocess_workspace_size =
                opr->get_preprocess_workspace_in_bytes(tensors[0].layout,
                                                       tensors[1].layout,
                                                       tensors[2].layout);
        WorkspaceWrapper preprocess_workspace(opr->handle(),
                                              preprocess_workspace_size);
        opr->exec_preprocess(tensors[0].layout, tensors[1], tensors[2].layout,
                             &preprocessed_filter,
                             preprocess_workspace.workspace());
        return preprocessed_filter_tensors_ptr;
    }
};

390 391 392 393 394 395 396 397 398
template <class Opr>
struct OprProxyProfiling5 : public OprProxyProfilingBase<Opr, 5> {
    using Base = OprProxyProfilingBase<Opr, 5>;
    using OprProxyProfilingBase<Opr, 5>::OprProxyProfilingBase;
    void exec(Opr* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() == 5);
        if (!Base::W.valid()) {
            Base::W = WorkspaceWrapper(opr->handle(), 0);
        }
399
        if (Base::m_profiling && !Base::target_algo_info.valid()) {
400
            size_t min_time = std::numeric_limits<size_t>::max();
401 402 403 404 405
            for (auto algo : opr->get_all_algorithms_info(
                         tensors[0].layout, tensors[1].layout,
                         tensors[2].layout, tensors[3].layout,
                         tensors[4].layout)) {
                opr->execution_policy().algo = algo;
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
                auto workspace_size = opr->get_workspace_in_bytes(
                        tensors[0].layout, tensors[1].layout, tensors[2].layout,
                        tensors[3].layout, tensors[4].layout);
                Base::W.update(workspace_size);

                for (size_t times = 0; times < Base::warmup_times; ++times)
                    opr->exec(tensors[0], tensors[1], tensors[2], tensors[3],
                              tensors[4], Base::W.workspace());
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                Timer timer;
                timer.start();
                for (size_t times = 0; times < Base::exec_times; ++times) {
                    opr->exec(tensors[0], tensors[1], tensors[2], tensors[3],
                              tensors[4], Base::W.workspace());
                }
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                timer.stop();
                printf("%.3fms %s\n", timer.get_time_in_us() / 1e3,
424
                       algo.name.c_str());
425 426
                if (min_time > timer.get_time_in_us()) {
                    min_time = timer.get_time_in_us();
427
                    Base::target_algo_info = algo;
428 429
                }
            }
430
            opr->execution_policy().algo = Base::target_algo_info;
431 432 433 434 435
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    tensors[3].layout, tensors[4].layout);
            Base::W.update(workspace_size);
        }
436
        if (!Base::target_algo_info.valid()) {
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    tensors[3].layout, tensors[4].layout);
            Base::W.update(workspace_size);
        }
        opr->exec(tensors[0], tensors[1], tensors[2], tensors[3], tensors[4],
                  Base::W.workspace());
    }
};

#define DEF_PROF5(c)                                     \
    template <>                                          \
    struct OprProxy<c> : public OprProxyProfiling5<c> {  \
        using OprProxyProfiling5<c>::OprProxyProfiling5; \
    }

DEF_PROF5(DeformableConvForward);
DEF_PROF5(DeformableConvBackwardFilter);
DEF_PROF5(BatchConvBiasForward);
#undef DEF_PROF5

458 459 460 461 462 463 464 465
template <>
struct OprProxy<ConvBiasForward> : public OprProxyProfiling5<ConvBiasForward> {
    using OprProxyProfiling5<ConvBiasForward>::OprProxyProfiling5;
    void exec(ConvBiasForward* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() == 5);
        if (!Base::W.valid()) {
            Base::W = WorkspaceWrapper(opr->handle(), 0);
        }
466
        if (Base::m_profiling && !Base::target_algo_info.desc.valid()) {
467
            size_t min_time = std::numeric_limits<size_t>::max();
468 469 470 471 472
            for (auto algo : opr->get_all_algorithms_info(
                         tensors[0].layout, tensors[1].layout,
                         tensors[2].layout, tensors[3].layout,
                         tensors[4].layout)) {
                opr->execution_policy().algo = algo;
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
                auto workspace_size = opr->get_workspace_in_bytes(
                        tensors[0].layout, tensors[1].layout, tensors[2].layout,
                        tensors[3].layout, tensors[4].layout, nullptr);
                Base::W.update(workspace_size);

                for (size_t times = 0; times < Base::warmup_times; ++times)
                    opr->exec(tensors[0], tensors[1], tensors[2], tensors[3],
                              tensors[4], nullptr, Base::W.workspace());
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                Timer timer;
                timer.start();
                for (size_t times = 0; times < Base::exec_times; ++times) {
                    opr->exec(tensors[0], tensors[1], tensors[2], tensors[3],
                              tensors[4], nullptr, Base::W.workspace());
                }
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                timer.stop();
                printf("%.3fms %s\n", timer.get_time_in_us() / 1e3,
491
                       algo.name.c_str());
492 493
                if (min_time > timer.get_time_in_us()) {
                    min_time = timer.get_time_in_us();
494
                    Base::target_algo_info = algo;
495 496
                }
            }
497
            opr->execution_policy().algo = Base::target_algo_info;
498 499 500 501 502
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    tensors[3].layout, tensors[4].layout, nullptr);
            Base::W.update(workspace_size);
        }
503
        if (!Base::target_algo_info.valid()) {
504 505 506 507 508 509 510 511 512 513
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    tensors[3].layout, tensors[4].layout, nullptr);
            Base::W.update(workspace_size);
        }
        opr->exec(tensors[0], tensors[1], tensors[2], tensors[3], tensors[4],
                  nullptr, Base::W.workspace());
    }
};

514 515 516 517 518 519 520 521 522
template <>
struct OprWeightPreprocessProxy<ConvBiasForward>
        : public OprProxyProfiling5<ConvBiasForward> {
    using OprProxyProfiling5<ConvBiasForward>::OprProxyProfiling5;
    void exec(ConvBiasForward* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() == 5);
        if (!Base::W.valid()) {
            Base::W = WorkspaceWrapper(opr->handle(), 0);
        }
523
        if (Base::m_profiling && !Base::target_algo_info.valid()) {
524
            size_t min_time = std::numeric_limits<size_t>::max();
525 526 527 528 529
            for (auto algo : opr->get_all_algorithms_info(
                         tensors[0].layout, tensors[1].layout,
                         tensors[2].layout, tensors[3].layout,
                         tensors[4].layout)) {
                opr->execution_policy().algo = algo;
530

531 532
                auto preprocess_tensors =
                        weight_prerocess(opr, tensors, algo.desc);
533 534
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                ConvBiasForward::PreprocessedFilter preprocessed_filter{
535
                        nullptr, *preprocess_tensors};
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557

                auto workspace_size = opr->get_workspace_in_bytes(
                        tensors[0].layout, tensors[1].layout, tensors[2].layout,
                        tensors[3].layout, tensors[4].layout,
                        &preprocessed_filter);
                Base::W.update(workspace_size);

                for (size_t times = 0; times < Base::warmup_times; ++times)
                    opr->exec(tensors[0], tensors[1], tensors[2], tensors[3],
                              tensors[4], &preprocessed_filter,
                              Base::W.workspace());
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                Timer timer;
                timer.start();
                for (size_t times = 0; times < Base::exec_times; ++times) {
                    opr->exec(tensors[0], tensors[1], tensors[2], tensors[3],
                              tensors[4], &preprocessed_filter,
                              Base::W.workspace());
                }
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                timer.stop();
                printf("%.3fms %s\n", timer.get_time_in_us() / 1e3,
558
                       algo.name.c_str());
559 560
                if (min_time > timer.get_time_in_us()) {
                    min_time = timer.get_time_in_us();
561
                    Base::target_algo_info = algo;
562 563
                }
            }
564
            opr->execution_policy().algo = Base::target_algo_info;
565
            auto preprocess_tensors =
566
                    weight_prerocess(opr, tensors, Base::target_algo_info.desc);
567 568
            megcoreSynchronize(opr->handle()->megcore_computing_handle());
            ConvBiasForward::PreprocessedFilter preprocessed_filter{
569
                    nullptr, *preprocess_tensors};
570 571 572 573 574 575
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    tensors[3].layout, tensors[4].layout, &preprocessed_filter);
            Base::W.update(workspace_size);
        }
        auto preprocess_tensors =
576
                weight_prerocess(opr, tensors, Base::target_algo_info.desc);
577 578
        megcoreSynchronize(opr->handle()->megcore_computing_handle());
        ConvBiasForward::PreprocessedFilter preprocessed_filter{
579 580
                nullptr, *preprocess_tensors};
        if (!Base::target_algo_info.valid()) {
581 582 583 584 585 586 587 588 589 590 591 592
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    tensors[3].layout, tensors[4].layout, &preprocessed_filter);
            Base::W.update(workspace_size);
        }
        opr->exec(tensors[0], tensors[1], tensors[2], tensors[3], tensors[4],
                  &preprocessed_filter, Base::W.workspace());
    }

    //! handle weight preprocess
    std::shared_ptr<TensorNDArray> weight_prerocess(
            ConvBiasForward* opr, const TensorNDArray& tensors,
593
            const ConvBiasForward::AlgorithmDesc&) {
594 595 596 597 598 599
        auto weight_perprocess_layouts = opr->deduce_preprocessed_filter_layout(
                tensors[0].layout, tensors[1].layout, tensors[2].layout,
                tensors[3].layout, tensors[4].layout);
        auto preprocessed_filter_tensors_ptr =
                alloc_tensors(opr->handle(), weight_perprocess_layouts);
        ConvBiasForward::PreprocessedFilter preprocessed_filter{
600
                nullptr, *preprocessed_filter_tensors_ptr};
601 602 603 604 605 606
        size_t preprocess_workspace_size =
                opr->get_preprocess_workspace_in_bytes(
                        tensors[0].layout, tensors[1].layout, tensors[2].layout,
                        tensors[3].layout, tensors[4].layout);
        WorkspaceWrapper preprocess_workspace(opr->handle(),
                                              preprocess_workspace_size);
607
        opr->exec_preprocess(tensors[0].layout, tensors[1], tensors[2],
608 609 610 611 612 613 614
                             tensors[3].layout, tensors[4].layout,
                             &preprocessed_filter,
                             preprocess_workspace.workspace());
        return preprocessed_filter_tensors_ptr;
    }
};

615 616 617 618 619 620 621 622 623
template <class Opr>
struct OprProxyProfiling8 : public OprProxyProfilingBase<Opr, 8> {
    using Base = OprProxyProfilingBase<Opr, 8>;
    using OprProxyProfilingBase<Opr, 8>::OprProxyProfilingBase;
    void exec(Opr* opr, const TensorNDArray& tensors) {
        megdnn_assert(tensors.size() == 8);
        if (!Base::W.valid()) {
            Base::W = WorkspaceWrapper(opr->handle(), 0);
        }
624
        if (Base::m_profiling && !Base::target_algo_info.valid()) {
625
            size_t min_time = std::numeric_limits<size_t>::max();
626
            for (auto algo : opr->get_all_algorithms_info(
627 628 629 630
                         tensors[0].layout, tensors[1].layout,
                         tensors[2].layout, tensors[3].layout,
                         tensors[4].layout, tensors[5].layout,
                         tensors[6].layout, tensors[7].layout)) {
631
                opr->execution_policy().algo = algo;
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
                auto workspace_size = opr->get_workspace_in_bytes(
                        tensors[0].layout, tensors[1].layout, tensors[2].layout,
                        tensors[3].layout, tensors[4].layout, tensors[5].layout,
                        tensors[6].layout, tensors[7].layout);
                Base::W.update(workspace_size);

                for (size_t times = 0; times < Base::warmup_times; ++times)
                    opr->exec(tensors[0], tensors[1], tensors[2], tensors[3],
                              tensors[4], tensors[5], tensors[6], tensors[7],
                              Base::W.workspace());
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                Timer timer;
                timer.start();
                for (size_t times = 0; times < Base::exec_times; ++times) {
                    opr->exec(tensors[0], tensors[1], tensors[2], tensors[3],
                              tensors[4], tensors[5], tensors[6], tensors[7],
                              Base::W.workspace());
                }
                megcoreSynchronize(opr->handle()->megcore_computing_handle());
                timer.stop();
                printf("%.3fms %s\n", timer.get_time_in_us() / 1e3,
653
                       algo.name.c_str());
654 655
                if (min_time > timer.get_time_in_us()) {
                    min_time = timer.get_time_in_us();
656
                    Base::target_algo_info = algo;
657 658
                }
            }
659
            opr->execution_policy().algo = Base::target_algo_info;
660 661 662 663 664 665
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    tensors[3].layout, tensors[4].layout, tensors[5].layout,
                    tensors[6].layout, tensors[7].layout);
            Base::W.update(workspace_size);
        }
666
        if (!Base::target_algo_info.valid()) {
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
            auto workspace_size = opr->get_workspace_in_bytes(
                    tensors[0].layout, tensors[1].layout, tensors[2].layout,
                    tensors[3].layout, tensors[4].layout, tensors[5].layout,
                    tensors[6].layout, tensors[7].layout);
            Base::W.update(workspace_size);
        }
        opr->exec(tensors[0], tensors[1], tensors[2], tensors[3], tensors[4],
                  tensors[5], tensors[6], tensors[7], Base::W.workspace());
    }
};

#define DEF_PROF8(c)                                     \
    template <>                                          \
    struct OprProxy<c> : public OprProxyProfiling8<c> {  \
        using OprProxyProfiling8<c>::OprProxyProfiling8; \
    }

DEF_PROF8(DeformableConvBackwardData);

#undef DEF_PROF8
}  // namespace test
}  // namespace megdnn

// vim: syntax=cpp.doxygen