"git@developer.sourcefind.cn:orangecat/ollama.git" did not exist on "f8464785a66461496fa1291cd68630d8540171d5"
conv.cu 10.3 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
#include <iostream>
Chao Liu's avatar
Chao Liu committed
2
3
#include <numeric>
#include <initializer_list>
Chao Liu's avatar
Chao Liu committed
4
5
#include "nvToolsExt.h"
#include "tensor.hpp"
Chao Liu's avatar
Chao Liu committed
6
7
8
9
#include "constant_tensor_descriptor.cuh"
#include "device_tensor_descriptor.cuh"

#if 0
Chao Liu's avatar
Chao Liu committed
10
#include "direct_convolution.cuh"
Chao Liu's avatar
Chao Liu committed
11
12
13
#else
#include "constant_direct_convolution.cuh"
#endif
Chao Liu's avatar
Chao Liu committed
14

Chao Liu's avatar
Chao Liu committed
15
template <class T>
Chao Liu's avatar
Chao Liu committed
16
struct GeneratorConstant
Chao Liu's avatar
Chao Liu committed
17
18
19
20
21
22
23
{
    T value = 0;

    template <class... Is>
    T operator()(Is... is)
    {
        return value;
Chao Liu's avatar
Chao Liu committed
24
25
26
27
28
29
30
31
32
33
    }
};

template <class T>
struct GeneratorTensor
{
    template <class... Is>
    T operator()(Is... is)
    {
#if 0
Chao Liu's avatar
Chao Liu committed
34
35
        std::initializer_list<std::size_t> ls = {static_cast<std::size_t>(is)...};
        return std::accumulate(ls.begin(), ls.end(), std::size_t(0));
Chao Liu's avatar
Chao Liu committed
36
37
38
39
40
41
42
#else
        assert(sizeof...(Is) > 0);
        std::initializer_list<std::size_t> ids = {static_cast<std::size_t>(is)...};
        std::vector<std::size_t> lens(sizeof...(Is), 100);
        std::vector<std::size_t> strides(sizeof...(Is), 1);
        std::partial_sum(lens.rbegin(), lens.rbegin() + (sizeof...(Is) - 1), strides.rbegin() + 1);
        return std::inner_product(ids.begin(), ids.end(), strides.begin(), std::size_t(0)) + 1;
Chao Liu's avatar
Chao Liu committed
43
44
45
46
#endif
    }
};

Chao Liu's avatar
Chao Liu committed
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
// this is ugly, only for 4d
template <class TConstTensorDesc>
void ostream_ConstantTensorDescriptor(TConstTensorDesc, std::ostream& os = std::cout)
{
    static_assert(TConstTensorDesc::nDim == 4, "nDim is not 4");

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

    os << "Lengths: {" << desc.GetLength(I0) << ", " << desc.GetLength(I1) << ", "
       << desc.GetLength(I2) << ", " << desc.GetLength(I3) << "}, "
       << "Strides: {" << desc.GetStride(I0) << ", " << desc.GetStride(I1) << ", "
       << desc.GetStride(I2) << ", " << desc.GetStride(I3) << "}" << std::endl;
}

// this is ugly, only for 4d
template <class TConstTensorDesc>
auto make_TensorDescriptor(TConstTensorDesc)
{
    static_assert(TConstTensorDesc::nDim == 4, "nDim is not 4");

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

    std::initializer_list<unsigned> lengths = {
        desc.GetLength(I0), desc.GetLength(I1), desc.GetLength(I2), desc.GetLength(I3)};
    std::initializer_list<unsigned> strides = {
        desc.GetStride(I0), desc.GetStride(I1), desc.GetStride(I2), desc.GetStride(I3)};

    return TensorDescriptor(lengths, strides);
}

template <class T>
void host_convolution(const Tensor<T>& in, const Tensor<T>& wei, Tensor<T>& out)
Chao Liu's avatar
Chao Liu committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{
    auto f = [&](auto n, auto k, auto ho, auto wo) {
        double v = 0;
        for(int c = 0; c < wei.mDesc.GetLengths()[1]; ++c)
        {
            for(int y = 0; y < wei.mDesc.GetLengths()[2]; ++y)
            {
                int hi = ho + y;
                for(int x = 0; x < wei.mDesc.GetLengths()[3]; ++x)
                {
                    int wi = wo + x;
                    v += in(n, c, hi, wi) * wei(k, c, y, x);
                }
            }
        }
        out(n, k, ho, wo) = v;
    };

    auto f_par = make_ParallelTensorFunctor(f,
                                            out.mDesc.GetLengths()[0],
                                            out.mDesc.GetLengths()[1],
                                            out.mDesc.GetLengths()[2],
                                            out.mDesc.GetLengths()[3]);

Chao Liu's avatar
Chao Liu committed
111
    f_par(std::thread::hardware_concurrency());
Chao Liu's avatar
Chao Liu committed
112
113
}

Chao Liu's avatar
Chao Liu committed
114
115
#if 0
template <class T>
Chao Liu's avatar
Chao Liu committed
116
void device_convolution(
Chao Liu's avatar
Chao Liu committed
117
    const Tensor<T>& in, const Tensor<T>& wei, Tensor<T>& out)
Chao Liu's avatar
Chao Liu committed
118
{
Chao Liu's avatar
Chao Liu committed
119
120
121
122
    DeviceTensorDescriptor<4> in_desc_device(in.mDesc);
    DeviceTensorDescriptor<4> wei_desc_device(wei.mDesc);
    DeviceTensorDescriptor<4> out_desc_device(out.mDesc);

Chao Liu's avatar
Chao Liu committed
123
    printf("__func__: in_desc_device: {%u %u %u %u}, {%u %u %u %u}\n",
Chao Liu's avatar
Chao Liu committed
124
125
126
           in_desc_device.GetLength(0),
           in_desc_device.GetLength(1),
           in_desc_device.GetLength(2),
Chao Liu's avatar
Chao Liu committed
127
128
129
130
131
           in_desc_device.GetLength(3),
           in_desc_device.GetStride(0),
           in_desc_device.GetStride(1),
           in_desc_device.GetStride(2),
           in_desc_device.GetStride(3));
Chao Liu's avatar
Chao Liu committed
132
133
134
135
136
137

    std::size_t data_sz = sizeof(T);
    DeviceMem in_device_buf(data_sz * in.mDesc.GetElementSpace());
    DeviceMem wei_device_buf(data_sz * wei.mDesc.GetElementSpace());
    DeviceMem out_device_buf(data_sz * out.mDesc.GetElementSpace());

Chao Liu's avatar
Chao Liu committed
138
139
    int num_thread = std::thread::hardware_concurrency();

Chao Liu's avatar
Chao Liu committed
140
    out.GenerateTensorValue(GeneratorConstant<float>{0}, num_thread);
Chao Liu's avatar
Chao Liu committed
141

Chao Liu's avatar
Chao Liu committed
142
143
    in_device_buf.ToDevice(in.mData.data());
    wei_device_buf.ToDevice(wei.mData.data());
Chao Liu's avatar
Chao Liu committed
144
    out_device_buf.ToDevice(out.mData.data());
Chao Liu's avatar
Chao Liu committed
145

Chao Liu's avatar
Chao Liu committed
146
    dim3 block_dim(64, 1, 1);
Chao Liu's avatar
Chao Liu committed
147
    dim3 grid_dim(1, 1, 1);
Chao Liu's avatar
Chao Liu committed
148

Chao Liu's avatar
Chao Liu committed
149
    gridwise_convolution<T, 3, 3, 4, 4, 2, 2, 1, 1, 8, 8, 1>
Chao Liu's avatar
Chao Liu committed
150
151
152
153
154
155
        <<<grid_dim, block_dim>>>(in_desc_device,
                                  static_cast<T*>(in_device_buf.GetDeviceBuffer()),
                                  wei_desc_device,
                                  static_cast<T*>(wei_device_buf.GetDeviceBuffer()),
                                  out_desc_device,
                                  static_cast<T*>(out_device_buf.GetDeviceBuffer()));
Chao Liu's avatar
Chao Liu committed
156
157
158
159

    checkCudaErrors(cudaGetLastError());
    out_device_buf.FromDevice(out.mData.data());
}
Chao Liu's avatar
Chao Liu committed
160
#else
Chao Liu's avatar
Chao Liu committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
template <class T, class InDesc, class WeiDesc, class OutDesc>
void const_device_convolution(
    InDesc, const Tensor<T>& in, WeiDesc, const Tensor<T>& wei, OutDesc, Tensor<T>& out)
{
    std::size_t data_sz = sizeof(T);
    DeviceMem in_device_buf(data_sz * in.mDesc.GetElementSpace());
    DeviceMem wei_device_buf(data_sz * wei.mDesc.GetElementSpace());
    DeviceMem out_device_buf(data_sz * out.mDesc.GetElementSpace());

    int num_thread = std::thread::hardware_concurrency();

    out.GenerateTensorValue(GeneratorConstant<float>{0}, num_thread);

    in_device_buf.ToDevice(in.mData.data());
    wei_device_buf.ToDevice(wei.mData.data());
    out_device_buf.ToDevice(out.mData.data());

    dim3 block_dim(64, 1, 1);
    dim3 grid_dim(1, 1, 1);

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

    constexpr auto in_desc = InDesc{};
    constexpr auto wei_desc = WeiDesc{};
    constexpr auto out_desc = OutDesc{};
    constexpr unsigned NPerBlock = 1;
    constexpr unsigned KPerBlock = 1;
    constexpr unsigned CPerBlockLoop = 1;
    constexpr unsigned OutTileSizeH = 2;
    constexpr unsigned OutTileSizeW = 2;
    constexpr unsigned YPerBlock = (out_desc.GetLength(I2) + OutTileSizeH - 1) / OutTileSizeH;
    constexpr unsigned XPerBlock = (out_desc.GetLength(I3) + OutTileSizeW - 1) / OutTileSizeW;

    constexpr unsigned NBlockCopyLen0 = 1;
    constexpr unsigned NBlockCopyLen1 = 1;
    constexpr unsigned NBlockCopyLen2 = 1;
    constexpr unsigned NBlockCopyLen3 = 64;

    gridwise_convolution<T,
                         InDesc,
                         WeiDesc,
                         OutDesc,
                         NPerBlock,
                         KPerBlock,
                         CPerBlockLoop,
                         OutTileSizeH,
                         OutTileSizeW,
                         YPerBlock,
                         XPerBlock,
                         NBlockCopyLen0,
                         NBlockCopyLen1,
                         NBlockCopyLen2,
                         NBlockCopyLen3>
Chao Liu's avatar
Chao Liu committed
217
218
219
220
221
222
        <<<grid_dim, block_dim>>>(InDesc{},
                                  static_cast<T*>(in_device_buf.GetDeviceBuffer()),
                                  WeiDesc{},
                                  static_cast<T*>(wei_device_buf.GetDeviceBuffer()),
                                  OutDesc{},
                                  static_cast<T*>(out_device_buf.GetDeviceBuffer()));
Chao Liu's avatar
Chao Liu committed
223

Chao Liu's avatar
Chao Liu committed
224
    checkCudaErrors(cudaGetLastError());
Chao Liu's avatar
Chao Liu committed
225
226
    out_device_buf.FromDevice(out.mData.data());
}
Chao Liu's avatar
Chao Liu committed
227
#endif
Chao Liu's avatar
Chao Liu committed
228
229
230

int main()
{
Chao Liu's avatar
Chao Liu committed
231
232
233
234
235
236
237
238
#if 1
    constexpr unsigned N  = 1;
    constexpr unsigned C  = 1;
    constexpr unsigned HI = 18;
    constexpr unsigned WI = 18;
    constexpr unsigned K  = 1;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;
Chao Liu's avatar
Chao Liu committed
239
#elif 1
Chao Liu's avatar
Chao Liu committed
240
241
    constexpr unsigned N = 1;
    constexpr unsigned C = 1;
Chao Liu's avatar
Chao Liu committed
242
243
    constexpr unsigned HI = 36;
    constexpr unsigned WI = 36;
Chao Liu's avatar
Chao Liu committed
244
245
246
    constexpr unsigned K = 1;
    constexpr unsigned S = 3;
    constexpr unsigned R = 3;
Chao Liu's avatar
Chao Liu committed
247
248
249
250
251
252
253
254
#elif 0
    constexpr unsigned N  = 1;
    constexpr unsigned C  = 1;
    constexpr unsigned HI = 130;
    constexpr unsigned WI = 130;
    constexpr unsigned K  = 1;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;
Chao Liu's avatar
Chao Liu committed
255
256
257
258
259
260
261
262
#elif 0
    constexpr unsigned N  = 3;
    constexpr unsigned C  = 16;
    constexpr unsigned HI = 130;
    constexpr unsigned WI = 130;
    constexpr unsigned K  = 4;
    constexpr unsigned S  = 3;
    constexpr unsigned R  = 3;
Chao Liu's avatar
Chao Liu committed
263
#endif
Chao Liu's avatar
Chao Liu committed
264
265
266
267
268
269
270
271
272
273
274
275
276

    auto in_desc  = make_ConstantTensorDescriptor(Sequence<N, C, HI, WI>{});
    auto wei_desc = make_ConstantTensorDescriptor(Sequence<K, C, S, R>{});
    auto out_desc = get_output_4d_tensor_descriptor(in_desc, wei_desc);

    ostream_ConstantTensorDescriptor(in_desc, std::cout << "in_desc: ");
    ostream_ConstantTensorDescriptor(wei_desc, std::cout << "wei_desc: ");
    ostream_ConstantTensorDescriptor(out_desc, std::cout << "out_desc: ");

    Tensor<float> in(make_TensorDescriptor(in_desc));
    Tensor<float> wei(make_TensorDescriptor(wei_desc));
    Tensor<float> out_host(make_TensorDescriptor(out_desc));

Chao Liu's avatar
Chao Liu committed
277
278
279
280
    Tensor<float> out_device = out_host;

    int num_thread = std::thread::hardware_concurrency();

Chao Liu's avatar
Chao Liu committed
281
282
    in.GenerateTensorValue(GeneratorTensor<float>{}, num_thread);
    wei.GenerateTensorValue(GeneratorTensor<float>{}, num_thread);
Chao Liu's avatar
Chao Liu committed
283

Chao Liu's avatar
Chao Liu committed
284
    host_convolution(in, wei, out_host);
Chao Liu's avatar
Chao Liu committed
285
286
287
288
289
290

#if 0
    device_convolution(in, wei, out_device);
#else
    const_device_convolution(in_desc, in, wei_desc, wei, out_desc, out_device);
#endif
Chao Liu's avatar
Chao Liu committed
291
292
293

    std::cout << __func__ << ": done" << std::endl;

Chao Liu's avatar
Chao Liu committed
294
295
296
    LogRange(std::cout << __func__ << "in : ", in.mData, ",") << std::endl;
    LogRange(std::cout << __func__ << "wei: ", wei.mData, ",") << std::endl;
    LogRange(std::cout, out_host.mData, ",") << std::endl;
Chao Liu's avatar
Chao Liu committed
297
    LogRange(std::cout, out_device.mData, ",") << std::endl;
Chao Liu's avatar
Chao Liu committed
298
299
300
301
302
303
304

    float error = 0;
    for(int i = 0; i < out_host.mData.size(); ++i)
    {
        error += std::abs(out_host.mData[i] - out_device.mData[i]);
    }
    std::cout << "error: " << error << std::endl;
Chao Liu's avatar
Chao Liu committed
305
}