opr_impl.cpp 2.6 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/cuda/images2neibs/opr_impl.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 20 21 22 23 24 25 26 27 28 29
 *
 * 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 "src/cuda/images2neibs/opr_impl.h"

#include "src/cuda/utils.h"
#include "src/cuda/images2neibs/kernel.cuh"

namespace megdnn {
namespace cuda {

void Images2NeibsForwardImpl::exec(_megdnn_tensor_in src,
        _megdnn_tensor_out dst,
        _megdnn_workspace workspace)
{
    check_exec(src.layout, dst.layout, workspace.size);
    auto stream = cuda_stream(handle());
    int N = src.layout[0], C = src.layout[1],
        IH = src.layout[2], IW = src.layout[3];
    int OH = dst.layout[2], OW = dst.layout[3];
    int ph = param().pad_h, pw = param().pad_w;
    int sh = param().stride_h, sw = param().stride_w;
30
    int dh = param().dilate_h, dw = param().dilate_w;
31 32 33 34 35 36
    int wh = param().window_h, ww = param().window_w;
#define cb(DType) \
    if (src.layout.dtype.enumv() == DTypeTrait<DType>::enumv) { \
        using T = DTypeTrait<DType>::ctype; \
        images2neibs::forward(src.ptr<T>(), dst.ptr<T>(), \
                N, C, IH, IW, OH, OW, \
37
                ph, pw, sh, sw, dh, dw, wh, ww, \
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
                stream); \
        return; \
    }
    MEGDNN_FOREACH_COMPUTING_DTYPE(cb);
#undef cb
    megdnn_assert_internal(0);
}

void Images2NeibsBackwardImpl::exec(_megdnn_tensor_in diff,
        _megdnn_tensor_out grad,
        _megdnn_workspace workspace)
{
    check_exec(diff.layout, grad.layout, workspace.size);
    auto stream = cuda_stream(handle());
    int N = grad.layout[0], C = grad.layout[1],
        IH = grad.layout[2], IW = grad.layout[3];
    int OH = diff.layout[2], OW = diff.layout[3];
    int ph = param().pad_h, pw = param().pad_w;
    int sh = param().stride_h, sw = param().stride_w;
57
    int dh = param().dilate_h, dw = param().dilate_w;
58 59 60 61 62 63
    int wh = param().window_h, ww = param().window_w;
#define cb(DType) \
    if (diff.layout.dtype == DType()) { \
        using T = DTypeTrait<DType>::ctype; \
        images2neibs::backward(diff.ptr<T>(), grad.ptr<T>(), \
                N, C, IH, IW, OH, OW, \
64
                ph, pw, sh, sw, dh, dw, wh, ww, \
65 66 67 68 69 70 71 72 73 74 75 76
                stream); \
        return; \
    }
    MEGDNN_FOREACH_COMPUTING_DTYPE(cb);
#undef cb
    megdnn_assert_internal(0);
}

} // namespace cuda
} // namespace megdnn

// vim: syntax=cpp.doxygen