megenginelite-sys 1.8.2

A safe megenginelite wrapper in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**
 * \file dnn/src/cuda/relayout/kern.cuh
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
 *
 * 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.
 */

#pragma once

#include "megdnn/basic_types.h"
#include "src/cuda/elemwise_helper.cuh"
#include "src/cuda/int_fastdiv.cuh"
#include "src/cuda/relayout/param_visitor.cuh"
#include "src/cuda/utils.cuh"

namespace megdnn {
namespace cuda {

void copy_noncontig_general(
        const TensorND& dst, const TensorND& src, cudaStream_t stream);
void get_launch_spec_unroll16(
        const void* kern, size_t size, int* grid_size, int* block_size);
void get_launch_spec_unroll4(
        const void* kern, size_t size, int* grid_size, int* block_size);

//! internals for general
namespace noncontig_general_intl {

#define devfunc __device__ __forceinline__
/*!
 * \brief contiguous type
 * If the layout is contiguous, then the type is CONTIG_FULL, CONTIG_OTHER
 * otherwise.
 */

template <class PVis0, class PVis1>
struct OpCallerBinaryNoContiguous {
    PVis0 par0;
    PVis1 par1;
};

/* f{{{ cuda kern */

#if MEGDNN_CC_CUDA
/*!
 * \brief cuda kern for general case replacing elemwise_helper
 */
template <typename OpCaller>
__global__ void cuda_kern_general(OpCaller op_caller, uint32_t size) {
    uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x,
             delta = blockDim.x * gridDim.x;
    if (idx < size) {
        int offset0 = op_caller.par0.offset(idx);
        int offset1 = op_caller.par1.offset(idx);
        op_caller.par0.ptr()[offset0] = op_caller.par1.ptr()[offset1];
        idx += delta;
        if (idx < size) {
            offset0 = op_caller.par0.offset(idx);
            offset1 = op_caller.par1.offset(idx);
            op_caller.par0.ptr()[offset0] = op_caller.par1.ptr()[offset1];
            idx += delta;
            if (idx < size) {
                offset0 = op_caller.par0.offset(idx);
                offset1 = op_caller.par1.offset(idx);
                op_caller.par0.ptr()[offset0] = op_caller.par1.ptr()[offset1];
            }
        }
    }
}

/*!
 * \brief cuda kern for last two shape transpose
 *        type is byte, dst is last contig and %4 = 0
 */
template <typename OpCaller>
__global__ void dst_pack_kern(OpCaller op_caller, uint32_t size) {
    uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x,
             delta = blockDim.x * gridDim.x;
    //! each thread fetch 4 elements
    uint32_t num = size / 4;
    if (idx < num) {
        uint32_t offset = idx * 4;
        uchar1 val1 = *reinterpret_cast<uchar1*>(&op_caller.par1.at(offset));
        uchar1 val2 = *reinterpret_cast<uchar1*>(&op_caller.par1.at(offset + 1));
        uchar1 val3 = *reinterpret_cast<uchar1*>(&op_caller.par1.at(offset + 2));
        uchar1 val4 = *reinterpret_cast<uchar1*>(&op_caller.par1.at(offset + 3));

        *reinterpret_cast<uchar4*>(&op_caller.par0.at(offset)) =
                uchar4{val1.x, val2.x, val3.x, val4.x};

        idx += delta;
    }
}

/*!
 * \brief cuda kern for last two shape transpose
 *        type is byte, src is last contig and %4 = 0
 */
template <typename OpCaller>
__global__ void src_pack_kern(OpCaller op_caller, uint32_t size) {
    uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x,
             delta = blockDim.x * gridDim.x;
    //! each thread fetch 4 elements
    uint32_t num = size / 4;
    if (idx < num) {
        uint32_t offset = idx * 4;
        uchar4 val = *reinterpret_cast<uchar4*>(&op_caller.par1.at(offset));
        *reinterpret_cast<uchar1*>(&op_caller.par0.at(offset)) = uchar1{val.x};
        *reinterpret_cast<uchar1*>(&op_caller.par0.at(offset + 1)) = uchar1{val.y};
        *reinterpret_cast<uchar1*>(&op_caller.par0.at(offset + 2)) = uchar1{val.z};
        *reinterpret_cast<uchar1*>(&op_caller.par0.at(offset + 3)) = uchar1{val.w};

        idx += delta;
    }
}
/* f}}} */

#define DEFINE_CONTIG_RECEIVER(_ndim, _cb_header, _cb_dispatch, _layout) \
    _cb_header(_ndim) {                                                  \
        if (_layout.is_contiguous()) {                                   \
            return _cb_dispatch(_ndim, CONTIG_FULL);                     \
        }                                                                \
        return _cb_dispatch(_ndim, CONTIG_OTHER);                        \
    }

//! invoke a user Op passed to run_elemwise
template <typename ctype, int arity>
class UserOpInvoker;

/* f{{{ UserOpInvoker specializations */

//! specialization for binary opr
template <typename ctype>
class UserOpInvoker<ctype, 2> {
    bool m_invoked;
    const ElemwiseOpParamN<2>& m_param;
    cudaStream_t m_stream;
    size_t m_rw_size;

    void dispatch0() {
        switch (m_param[0].layout.ndim) {
#define cb(ndim) \
    case ndim:   \
        return dispatch1_##ndim();
            MEGDNN_FOREACH_TENSOR_NDIM(cb)
#undef cb
        }
    }

#define cb_header(ndim) void dispatch1_##ndim()
#define cb_dispatch(ndim, contig_mask) \
    dispatch2<ParamElemVisitor<ndim, ctype, contig_mask>>()
    DEFINE_CONTIG_RECEIVER(1, cb_header, cb_dispatch, m_param[0].layout)
    DEFINE_CONTIG_RECEIVER(2, cb_header, cb_dispatch, m_param[0].layout)
    DEFINE_CONTIG_RECEIVER(3, cb_header, cb_dispatch, m_param[0].layout)
    DEFINE_CONTIG_RECEIVER(4, cb_header, cb_dispatch, m_param[0].layout)
    DEFINE_CONTIG_RECEIVER(5, cb_header, cb_dispatch, m_param[0].layout)
    DEFINE_CONTIG_RECEIVER(6, cb_header, cb_dispatch, m_param[0].layout)
    DEFINE_CONTIG_RECEIVER(7, cb_header, cb_dispatch, m_param[0].layout)
#undef cb_header
#undef cb_dispatch

    template <class PVis0>
    void dispatch2() {
        switch (m_param[1].layout.ndim) {
#define cb(ndim) \
    case ndim:   \
        return dispatch3_##ndim<PVis0>();
            MEGDNN_FOREACH_TENSOR_NDIM(cb)
#undef cb
        }
    }

#define cb_header(ndim)    \
    template <class PVis0> \
    void dispatch3_##ndim()
#define cb_dispatch(ndim, contig_mask) \
    do_run<PVis0, ParamElemVisitor<ndim, ctype, contig_mask>>()
    DEFINE_CONTIG_RECEIVER(1, cb_header, cb_dispatch, m_param[1].layout)
    DEFINE_CONTIG_RECEIVER(2, cb_header, cb_dispatch, m_param[1].layout)
    DEFINE_CONTIG_RECEIVER(3, cb_header, cb_dispatch, m_param[1].layout)
    DEFINE_CONTIG_RECEIVER(4, cb_header, cb_dispatch, m_param[1].layout)
    DEFINE_CONTIG_RECEIVER(5, cb_header, cb_dispatch, m_param[1].layout)
    DEFINE_CONTIG_RECEIVER(6, cb_header, cb_dispatch, m_param[1].layout)
    DEFINE_CONTIG_RECEIVER(7, cb_header, cb_dispatch, m_param[1].layout)
#undef cb_header
#undef cb_dispatch

    int try_int8_pack() {
        //! return-type -1: general kernel
        //!              0: src pack int8
        //!              1: dst pack int8
        auto src = m_param[1].layout;
        auto dst = m_param[0].layout;
        bool dst_contig = dst.stride[dst.ndim - 1] == 1;
        bool src_contig = src.stride[src.ndim - 1] == 1;
        if (!src_contig && !dst_contig) {
            return -1;
        }
        if (std::is_same<ctype, dt_float16>::value)
            return -1;
        else if (std::is_same<ctype, dt_int32>::value)
            return -1;
        else if (std::is_same<ctype, dt_byte>::value) {
            //! check if src or dst is one dim and contig
            auto check_one_dim = [&]() {
                if (dst.ndim == 1 && dst_contig)
                    return true;
                else if (src.ndim == 1 && src_contig)
                    return true;
                else
                    return false;
            };
            if (check_one_dim()) {
                bool src_pack = src.shape[src.ndim - 1] % 4 == 0;
                bool dst_pack = dst.shape[dst.ndim - 1] % 4 == 0;
                if (src_pack && src_contig)
                    return 1;
                else if (dst_pack && dst_contig)
                    return 0;
            }
        }
        return -1;
    }
    int count = 0;
    template <class PVis0, class PVis1>
    void do_run() {
        megdnn_assert(!m_invoked);
        m_invoked = true;
        typedef OpCallerBinaryNoContiguous<PVis0, PVis1> Caller;
        size_t size = m_param.size;
        int grid_size, block_size;

        Caller caller;
        auto param_host_init = [&]() {
            caller.par0.host_init(m_param[0], grid_size, block_size);
            caller.par1.host_init(m_param[1], grid_size, block_size);
        };
        int kernel_type = try_int8_pack();
        if (kernel_type == 1) {
            //! src pack: read 1 uchar4, write 4 uchar
            auto fptr = src_pack_kern<Caller>;
            get_launch_spec_unroll4(
                    reinterpret_cast<const void*>(fptr), size, &grid_size, &block_size);
            param_host_init();
            (*fptr)<<<grid_size, block_size, 0, m_stream>>>(caller, size);

        } else if (kernel_type == 0) {
            //! dst pack: read 4 uchar, write 1 uchar4
            auto fptr = dst_pack_kern<Caller>;
            get_launch_spec_unroll4(
                    reinterpret_cast<const void*>(fptr), size, &grid_size, &block_size);
            param_host_init();
            (*fptr)<<<grid_size, block_size, 0, m_stream>>>(caller, size);

        } else {
            //! general
            auto fptr = cuda_kern_general<Caller>;
            elemwise_intl::get_launch_spec(
                    reinterpret_cast<const void*>(fptr), size, &grid_size, &block_size);
            param_host_init();
            (*fptr)<<<grid_size, block_size, 0, m_stream>>>(caller, size);
        }
        after_kernel_launch();
    }

public:
    UserOpInvoker(const ElemwiseOpParamN<2>& param, cudaStream_t stream)
            : m_rw_size(param.size), m_param(param), m_stream(stream) {
        m_invoked = false;
        dispatch0();
        megdnn_assert(m_invoked);
    }
};

#undef DEFINE_CONTIG_RECEIVER

#define ON(access_idx, par0, par1)                                     \
    {                                                                  \
        int32_t idx0 = par0.idx(access_idx * 2);                       \
        int32_t idx1 = par0.idx(access_idx * 2 + 1);                   \
        Storage x = (idx0 >= 0) ? par1.at(idx0) : (Storage)0;          \
        Storage y = (idx1 >= 0) ? par1.at(idx1) : (Storage)0;          \
        Storage dst = par0.make_vector(x, y).x;                        \
        Storage* ptr = par0.ptr();                                     \
        int32_t offset = par0.offset_from_access(access_idx * 2) >> 1; \
        ptr[offset] = dst;                                             \
    }

template <typename OpCaller>
__global__ void cuda_kern_general_q4(OpCaller op_caller, uint32_t size) {
    uint32_t access_idx = blockIdx.x * blockDim.x + threadIdx.x,
             delta = blockDim.x * gridDim.x;
    using Storage = uint8_t;
    if (access_idx < size) {
        ON(access_idx, op_caller.par0, op_caller.par1);
        access_idx += delta;
        if (access_idx < size) {
            ON(access_idx, op_caller.par0, op_caller.par1);
            access_idx += delta;
            if (access_idx < size) {
                ON(access_idx, op_caller.par0, op_caller.par1);
            }
        }
    }
}

#undef ON

#define DEFINE_CONTIG_RECEIVER(_ndim, _cb_header, _cb_dispatch) \
    _cb_header(_ndim) { return _cb_dispatch(_ndim, CONTIG_OTHER); }

template <>
class UserOpInvoker<dt_quint4, 2> {
    bool m_invoked;
    const ElemwiseOpParamN<2>& m_param;
    cudaStream_t m_stream;
    size_t m_rw_size;

    void dispatch0() {
        switch (m_param[0].layout.ndim) {
#define cb(ndim) \
    case ndim:   \
        return dispatch1_##ndim();
            MEGDNN_FOREACH_TENSOR_NDIM(cb)
#undef cb
        }
    }

#define cb_header(ndim) void dispatch1_##ndim()
#define cb_dispatch(ndim, contig_mask) \
    dispatch2<ParamElemVisitor<ndim, dt_quint4, contig_mask>>()
    DEFINE_CONTIG_RECEIVER(1, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(2, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(3, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(4, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(5, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(6, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(7, cb_header, cb_dispatch)
#undef cb_header
#undef cb_dispatch

    template <class PVis0>
    void dispatch2() {
        switch (m_param[1].layout.ndim) {
#define cb(ndim) \
    case ndim:   \
        return dispatch3_##ndim<PVis0>();
            MEGDNN_FOREACH_TENSOR_NDIM(cb)
#undef cb
        }
    }

#define cb_header(ndim)    \
    template <class PVis0> \
    void dispatch3_##ndim()
#define cb_dispatch(ndim, contig_mask) \
    do_run<PVis0, ParamElemVisitor<ndim, dt_quint4, contig_mask>>()
    DEFINE_CONTIG_RECEIVER(1, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(2, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(3, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(4, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(5, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(6, cb_header, cb_dispatch)
    DEFINE_CONTIG_RECEIVER(7, cb_header, cb_dispatch)
#undef cb_header
#undef cb_dispatch

    int count = 0;
    template <class PVis0, class PVis1>
    void do_run() {
        megdnn_assert(!m_invoked);
        m_invoked = true;
        typedef OpCallerBinaryNoContiguous<PVis0, PVis1> Caller;
        size_t size = m_param[0].layout.access_bytes();
        int grid_size, block_size;

        Caller caller;
        auto param_host_init = [&]() {
            caller.par0.host_init(m_param[0], grid_size, block_size);
            caller.par1.host_init(m_param[1], grid_size, block_size);
        };
        //! general
        auto fptr = cuda_kern_general_q4<Caller>;
        elemwise_intl::get_launch_spec(
                reinterpret_cast<const void*>(fptr), size, &grid_size, &block_size);
        param_host_init();
        (*fptr)<<<grid_size, block_size, 0, m_stream>>>(caller, size);
        after_kernel_launch();
    }

public:
    UserOpInvoker(const ElemwiseOpParamN<2>& param, cudaStream_t stream)
            : m_rw_size(param.size), m_param(param), m_stream(stream) {
        m_invoked = false;
        dispatch0();
        megdnn_assert(m_invoked);
    }
};

/* f}}} */

#endif

#undef devfunc

}  // namespace noncontig_general_intl
}  // namespace cuda
}  // namespace megdnn

// vim: ft=cpp syntax=cpp.doxygen