algo.cpp 4.2 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/cuda/convolution/backward_data/algo.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
 *
 * 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 22 23
 */

#include "./algo.h"
#include "src/cuda/utils.h"

using namespace megdnn;
using namespace cuda;

ConvolutionBackwardDataImpl::AlgoPack::AlgoPack() {
    non_cudnn_algos.push_back(&chanwise);
    non_cudnn_algos.push_back(&chanwise_small);
    non_cudnn_algos.push_back(&matmul);

24 25
    all_algos.push_back(&chanwise);        // prefer chanwise
    all_algos.push_back(&chanwise_small);  // prefer small chanwise
26 27

    fill_cudnn_algos();
28
    for (auto&& i : cudnn) {
29 30 31 32
        all_algos.push_back(&i);
    }
    all_algos.push_back(&matmul);

33 34 35 36 37 38
    fill_int8_dp4a_algos();
    for (auto&& algo : int8_nchw4_dotprod) {
        all_algos.push_back(&algo);
        int8_algos.push_back(&algo);
    }

39 40 41 42
    all_algos.reserve(all_algos.size() * 2);

    // add gconv algos by AlgoGroupConvGeneral
    auto all_algos_data = all_algos.data();
43
    size_t group_algo_start = 2;
44
    for (size_t i = group_algo_start; i < all_algos.size(); ++i) {
45 46
        gconv.push_back({all_algos[i]});
    }
47
    for (size_t i = group_algo_start; i < all_algos.size(); ++i) {
48
        algo2gconv[all_algos[i]] = &gconv[i - group_algo_start];
49
    }
50
    for (auto&& i : gconv) {
51 52 53 54
        all_algos.push_back(&i);
    }
    megdnn_assert(all_algos_data == all_algos.data());

55
    non_cudnn_algos.push_back(all_algos.rbegin()[0]);  // group matmul
56 57
    all_algos.push_back(&bfloat16);
    bfloat16_algos.push_back(&bfloat16);
58 59 60 61

    for (auto&& algo : all_algos) {
        m_all_algos_map.emplace(algo->info().desc, algo);
    }
62 63
}

64 65
MEGDNN_DEF_GET_ALGO_FROM_DESC(ConvolutionBackwardDataImpl)

66 67 68
ConvolutionBackwardDataImpl::AlgoCUDNN*
ConvolutionBackwardDataImpl::AlgoPack::cudnn_from_enum(
        cudnnConvolutionBwdDataAlgo_t algo) {
69
    for (auto&& i : cudnn) {
70 71 72
        if (i.cudnn_enum() == algo)
            return &i;
    }
73 74 75
    megdnn_throw(
            megdnn_mangle(ssprintf("can not find cudnn bwd_data algorithm %d",
                                   static_cast<int>(algo))));
76 77 78 79 80
}

ConvolutionBackwardDataImpl::AlgoPack ConvolutionBackwardDataImpl::sm_algo_pack;

ConvolutionBackwardDataImpl::AlgoBase::SizeArgs::SizeArgs(
81 82 83 84
        ConvolutionBackwardDataImpl* o, const TensorLayout& filter,
        const TensorLayout& diff, const TensorLayout& grad)
        : SizeArgs(o, filter, o->check_layout_fwd(grad, filter, diff), diff,
                   grad) {}
85 86

ConvolutionBackwardDataImpl::AlgoBase::SizeArgs::SizeArgs(
87 88 89 90 91 92 93 94 95
        ConvolutionBackwardDataImpl* o, const TensorLayout& filter,
        const CanonizedFilterMeta& filter_meta, const TensorLayout& diff,
        const TensorLayout& grad)
        : handle{concrete_handle(o->handle())},
          filter_meta{filter_meta},
          diff_layout{&diff},
          grad_layout{&grad},
          filter_layout{&filter},
          opr{o} {}
96 97

ConvolutionBackwardDataImpl::AlgoBase::ExecArgs::ExecArgs(
98 99 100 101 102 103 104 105
        ConvolutionBackwardDataImpl* opr, _megdnn_tensor_in filter,
        _megdnn_tensor_in diff, _megdnn_tensor_out grad,
        _megdnn_workspace workspace)
        : SizeArgs(opr, filter.layout, diff.layout, grad.layout),
          filter_tensor{&filter},
          diff_tensor{&diff},
          grad_tensor{&grad},
          workspace{workspace} {}
106 107

std::string ConvolutionBackwardDataImpl::AlgoBase::SizeArgs::to_string() const {
108
    auto&& fm = filter_meta;
109 110
    MEGDNN_MARK_USED_VAR(fm);
    return megdnn_mangle(ssprintf(
111 112 113 114 115 116 117
            "filter=%u{%u,%u,%u,%u}, diff=%s, grad=%s, "
            "pad=%ux%u, stride=%ux%u, dilate=%ux%u, xcorr=%d, dtype=%s,%s",
            fm.group, fm.ocpg, fm.icpg, fm.spatial[0], fm.spatial[1],
            diff_layout->to_string().c_str(), grad_layout->to_string().c_str(),
            fm.padding[0], fm.padding[1], fm.stride[0], fm.stride[1],
            fm.dilation[0], fm.dilation[1], !fm.should_flip,
            diff_layout->dtype.name(), grad_layout->dtype.name()));
118 119 120
}

// vim: syntax=cpp.doxygen