conv_common.hip.hpp 4.68 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
#pragma once
2
#include "ConstantTensorDescriptor.hip.hpp"
Chao Liu's avatar
Chao Liu committed
3
4

// this is ugly, only for 4d
Jing Zhang's avatar
Jing Zhang committed
5
6
7
template <class InDesc, class WeiDesc, class Strides, class Dilations>
constexpr auto
    get_convolution_output_default_4d_tensor_descriptor(InDesc, WeiDesc, Strides, Dilations)
Chao Liu's avatar
Chao Liu committed
8
9
10
11
12
13
14
15
16
{
    constexpr auto in_desc  = InDesc{};
    constexpr auto wei_desc = WeiDesc{};

    constexpr auto I0 = Number<0>{};
    constexpr auto I1 = Number<1>{};
    constexpr auto I2 = Number<2>{};
    constexpr auto I3 = Number<3>{};

Chao Liu's avatar
Chao Liu committed
17
18
    static_assert(in_desc.GetNumOfDimension() == 4, "input nDim is not 4");
    static_assert(wei_desc.GetNumOfDimension() == 4, "weight nDim is not 4");
Chao Liu's avatar
Chao Liu committed
19
20
21
22
23
24
25
26
    static_assert(in_desc.GetLength(I1) == wei_desc.GetLength(I1),
                  "input & weight dimension not consistent");

    constexpr auto N  = in_desc.GetLength(I0);
    constexpr auto HI = in_desc.GetLength(I2);
    constexpr auto WI = in_desc.GetLength(I3);

    constexpr auto K = wei_desc.GetLength(I0);
Chao Liu's avatar
Chao Liu committed
27
28
    constexpr auto Y = wei_desc.GetLength(I2);
    constexpr auto X = wei_desc.GetLength(I3);
Chao Liu's avatar
Chao Liu committed
29

Jing Zhang's avatar
Jing Zhang committed
30
31
    constexpr index_t stride_h = Strides{}.Get(I0);
    constexpr index_t stride_w = Strides{}.Get(I1);
Jing Zhang's avatar
Jing Zhang committed
32

Jing Zhang's avatar
Jing Zhang committed
33
34
35
36
37
    constexpr index_t dilation_h = Dilations{}.Get(I0);
    constexpr index_t dilation_w = Dilations{}.Get(I1);

    constexpr auto HO = (HI - Y - (Y - 1) * (dilation_h - 1)) / stride_h + 1;
    constexpr auto WO = (WI - X - (X - 1) * (dilation_w - 1)) / stride_w + 1;
38

Chao Liu's avatar
Chao Liu committed
39
    return make_ConstantTensorDescriptor_packed(Sequence<N, K, HO, WO>{});
40
41
42
}

template <class InDesc, class WeiDesc, class LowerPads, class UpperPads>
43
44
45
46
constexpr auto get_convolution_with_padding_output_default_4d_tensor_descriptor(InDesc,
                                                                                WeiDesc,
                                                                                LowerPads,
                                                                                UpperPads)
47
48
49
50
51
52
53
54
55
{
    constexpr auto in_desc  = InDesc{};
    constexpr auto wei_desc = WeiDesc{};

    constexpr auto I0 = Number<0>{};
    constexpr auto I1 = Number<1>{};
    constexpr auto I2 = Number<2>{};
    constexpr auto I3 = Number<3>{};

Chao Liu's avatar
Chao Liu committed
56
57
    static_assert(in_desc.GetNumOfDimension() == 4, "input nDim is not 4");
    static_assert(wei_desc.GetNumOfDimension() == 4, "weight nDim is not 4");
58
59
60
61
62
63
64
65
    static_assert(in_desc.GetLength(I1) == wei_desc.GetLength(I1),
                  "input & weight dimension not consistent");

    constexpr auto N  = in_desc.GetLength(I0);
    constexpr auto HI = in_desc.GetLength(I2);
    constexpr auto WI = in_desc.GetLength(I3);

    constexpr auto K = wei_desc.GetLength(I0);
Chao Liu's avatar
Chao Liu committed
66
67
    constexpr auto Y = wei_desc.GetLength(I2);
    constexpr auto X = wei_desc.GetLength(I3);
68
69
70
71
72
73
74

    constexpr auto HPadLow = LowerPads{}.Get(I0);
    constexpr auto WPadLow = LowerPads{}.Get(I1);

    constexpr auto HPadUp = UpperPads{}.Get(I0);
    constexpr auto WPadUp = UpperPads{}.Get(I1);

Chao Liu's avatar
Chao Liu committed
75
76
    constexpr auto HO = HI + HPadLow + HPadUp + 1 - Y;
    constexpr auto WO = WI + WPadLow + WPadUp + 1 - X;
Chao Liu's avatar
Chao Liu committed
77

Chao Liu's avatar
Chao Liu committed
78
    return make_ConstantTensorDescriptor_packed(Sequence<N, K, HO, WO>{});
Chao Liu's avatar
Chao Liu committed
79
}
Chao Liu's avatar
Chao Liu committed
80
81

template <class InDesc, class WeiDesc, class OutDesc>
Chao Liu's avatar
Chao Liu committed
82
constexpr std::size_t calculate_convolution_flops(InDesc, WeiDesc, OutDesc)
Chao Liu's avatar
Chao Liu committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{
    constexpr auto wei_desc = WeiDesc{};
    constexpr auto out_desc = OutDesc{};

    constexpr auto I0 = Number<0>{};
    constexpr auto I1 = Number<1>{};
    constexpr auto I2 = Number<2>{};
    constexpr auto I3 = Number<3>{};

    constexpr index_t N  = out_desc.GetLength(I0);
    constexpr index_t K  = out_desc.GetLength(I1);
    constexpr index_t Ho = out_desc.GetLength(I2);
    constexpr index_t Wo = out_desc.GetLength(I3);

    constexpr index_t C = wei_desc.GetLength(I1);
    constexpr index_t Y = wei_desc.GetLength(I2);
    constexpr index_t X = wei_desc.GetLength(I3);

    return std::size_t(2) * N * K * Ho * Wo * C * Y * X;
}
Chao Liu's avatar
Chao Liu committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

template <class Float, class InDesc, class WeiDesc, class OutDesc>
constexpr std::size_t calculate_convolution_memory_size(Float, InDesc, WeiDesc, OutDesc)
{
    constexpr auto wei_desc = WeiDesc{};
    constexpr auto out_desc = OutDesc{};

    constexpr auto I0 = Number<0>{};
    constexpr auto I1 = Number<1>{};
    constexpr auto I2 = Number<2>{};
    constexpr auto I3 = Number<3>{};

    constexpr index_t N  = out_desc.GetLength(I0);
    constexpr index_t K  = out_desc.GetLength(I1);
    constexpr index_t Ho = out_desc.GetLength(I2);
    constexpr index_t Wo = out_desc.GetLength(I3);

    constexpr index_t C = wei_desc.GetLength(I1);
    constexpr index_t Y = wei_desc.GetLength(I2);
    constexpr index_t X = wei_desc.GetLength(I3);

124
125
    return sizeof(Float) *
           (InDesc::GetElementSpace() + WeiDesc::GetElementSpace() + OutDesc::GetElementSpace());
Chao Liu's avatar
Chao Liu committed
126
}