cpu_reorder.cpp 12 KB
Newer Older
openvino-pushbot's avatar
openvino-pushbot committed
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
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/

#include <assert.h>

#include "cpu_engine.hpp"
#include "cpu_memory.hpp"
#include "type_helpers.hpp"

#include "cpu/jit_uni_reorder.hpp"
#include "cpu/simple_reorder.hpp"
#include "cpu/wino_reorder.hpp"
26
#include "cpu/rnn/rnn_reorders.hpp"
openvino-pushbot's avatar
openvino-pushbot committed
27 28 29 30 31 32 33 34 35 36 37

namespace mkldnn {
namespace impl {
namespace cpu {

using rpd_create_f = mkldnn::impl::engine_t::reorder_primitive_desc_create_f;

namespace {
using namespace mkldnn::impl::data_type;
using namespace mkldnn::impl::memory_format;

Alexey Suhov's avatar
Alexey Suhov committed
38 39 40 41 42 43 44 45 46 47 48
#define REG_SR(idt, ifmt, odt, ofmt, ...) \
    simple_reorder_t<idt, ifmt, odt, ofmt, __VA_ARGS__>::pd_t::create

#define REG_SR_BIDIR(idt, ifmt, odt, ofmt) \
    REG_SR(idt, ifmt, odt, ofmt, fmt_order::keep), \
    REG_SR(idt, ifmt, odt, ofmt, fmt_order::reverse)

#define REG_SR_DIRECT_COPY(idt, odt) \
    REG_SR(idt, any, odt, any, fmt_order::any, spec::direct_copy), \
    REG_SR(idt, any, odt, any, fmt_order::any, spec::direct_copy_except_dim_0)

openvino-pushbot's avatar
openvino-pushbot committed
49
static const rpd_create_f cpu_reorder_impl_list[] = {
Alexey Suhov's avatar
Alexey Suhov committed
50 51 52 53
    /* winograd */
    wino_reorder_t<f32, f32>::pd_t::create,
    wino_reorder_t<f32, s8>::pd_t::create,

54 55 56 57 58 59 60 61 62 63 64 65 66 67
    /* rnn reorders */
    rnn_data_reorder_t<f32, u8>::pd_t::create,
    rnn_weights_reorder_t<f32, f32>::pd_t::create,
    rnn_weights_reorder_t<f32, s8>::pd_t::create,

#if defined(__INTEL_COMPILER) || (defined(__GNUC__) && !defined(__clang__))
    /* Direct copy for icc which is faster than jitted code;
     * Direct copy for gcc which might or might not be faster than jitted
     * code, but still worth it because doesn't require jitting, i.e. much
     * faster creation time. This is tentative solution and should be removed
     * later (when we will cache jitted code?...). */
    REG_SR_DIRECT_COPY(f32, f32),
#endif

Alexey Suhov's avatar
Alexey Suhov committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#ifdef __INTEL_COMPILER
    /* direct copy for icc, which is faster than jitted code */
    REG_SR_DIRECT_COPY(f32, s32),
    REG_SR_DIRECT_COPY(f32, s8),
//    REG_SR_DIRECT_COPY(f32, u8), FIXME: Disabled due to accuracy failure on int8 network
    REG_SR_DIRECT_COPY(s32, f32),
    REG_SR_DIRECT_COPY(s32, s32),
    REG_SR_DIRECT_COPY(s32, s8),
    REG_SR_DIRECT_COPY(s32, u8),
    REG_SR_DIRECT_COPY(s8, f32),
    REG_SR_DIRECT_COPY(s8, s32),
    REG_SR_DIRECT_COPY(s8, s8),
    REG_SR_DIRECT_COPY(s8, u8),
    REG_SR_DIRECT_COPY(u8, f32),
    REG_SR_DIRECT_COPY(u8, s32),
    REG_SR_DIRECT_COPY(u8, s8),
    REG_SR_DIRECT_COPY(u8, u8),
#endif

    /* jit */
openvino-pushbot's avatar
openvino-pushbot committed
88
    jit_uni_reorder_create,
Alexey Suhov's avatar
Alexey Suhov committed
89

90 91 92 93 94 95 96
    /* fp32: flat <-> blocked with tail */
    REG_SR_BIDIR(f32, any, f32, nCw4c),

    REG_SR_BIDIR(f32, nchw, bin, nhwc),
    REG_SR_BIDIR(f32, nhwc, bin, nhwc),
    REG_SR_DIRECT_COPY(bin, bin),

97
    REG_SR_BIDIR(f32, any, f32, nCw8c),
98
    REG_SR_BIDIR(f32, any, f32, OIw4i4o),
99 100
    REG_SR_BIDIR(f32, any, f32, OIw8i8o),
    REG_SR_BIDIR(f32, any, f32, OIw8o8i),
101
    REG_SR_BIDIR(f32, any, f32, gOIw4i4o),
102 103 104 105 106 107 108 109 110 111 112
    REG_SR_BIDIR(f32, any, f32, gOIw8i8o),
    REG_SR_BIDIR(f32, any, f32, gOIw8o8i),

    REG_SR_BIDIR(f32, any, f32, nCw16c),
    REG_SR_BIDIR(f32, any, f32, OIw16o16i),
    REG_SR_BIDIR(f32, any, f32, OIw16i16o),
    REG_SR_BIDIR(f32, any, f32, IOw16o16i),
    REG_SR_BIDIR(f32, any, f32, gOIw16o16i),
    REG_SR_BIDIR(f32, any, f32, gOIw16i16o),
    REG_SR_BIDIR(f32, any, f32, gIOw16o16i),

113
    REG_SR_BIDIR(f32, any, f32, nChw4c),
Alexey Suhov's avatar
Alexey Suhov committed
114
    REG_SR_BIDIR(f32, any, f32, nChw8c),
115
    REG_SR_BIDIR(f32, any, f32, OIhw4i4o),
116 117 118
    REG_SR_BIDIR(f32, any, f32, Ohwi8o),
    REG_SR_BIDIR(f32, any, f32, OIhw8i8o),
    REG_SR_BIDIR(f32, any, f32, OIhw8o8i),
119 120
    REG_SR_BIDIR(f32, any, f32, gOIhw4i4o),
    REG_SR_BIDIR(f32, any, f32, gOIhw4o4i),
121 122 123
    REG_SR_BIDIR(f32, any, f32, gOhwi8o),
    REG_SR_BIDIR(f32, any, f32, gOIhw8i8o),
    REG_SR_BIDIR(f32, any, f32, gOIhw8o8i),
Alexey Suhov's avatar
Alexey Suhov committed
124

125
    REG_SR_BIDIR(f32, any, f32, nChw16c),
126
    REG_SR_BIDIR(f32, any, f32, Oihw4o),
Alexey Suhov's avatar
Alexey Suhov committed
127
    REG_SR_BIDIR(f32, any, f32, Oihw16o),
128
    REG_SR_BIDIR(f32, any, f32, Ohwi4o),
Alexey Suhov's avatar
Alexey Suhov committed
129 130 131 132
    REG_SR_BIDIR(f32, any, f32, Ohwi16o),
    REG_SR_BIDIR(f32, any, f32, OIhw16o16i),
    REG_SR_BIDIR(f32, any, f32, OIhw16i16o),
    REG_SR_BIDIR(f32, any, f32, IOhw16o16i),
133
    REG_SR_BIDIR(f32, any, f32, gOihw4o),
Alexey Suhov's avatar
Alexey Suhov committed
134
    REG_SR_BIDIR(f32, any, f32, gOihw16o),
135
    REG_SR_BIDIR(f32, any, f32, gOhwi4o),
Alexey Suhov's avatar
Alexey Suhov committed
136 137 138
    REG_SR_BIDIR(f32, any, f32, gOhwi16o),
    REG_SR_BIDIR(f32, any, f32, gOIhw16o16i),
    REG_SR_BIDIR(f32, any, f32, gOIhw16i16o),
139 140
    REG_SR_BIDIR(f32, any, f32, gIOhw16o16i),

141
    REG_SR_BIDIR(f32, any, f32, nCdhw4c),
142
    REG_SR_BIDIR(f32, any, f32, nCdhw8c),
143
    REG_SR_BIDIR(f32, any, f32, OIdhw4i4o),
144 145 146
    REG_SR_BIDIR(f32, any, f32, Odhwi8o),
    REG_SR_BIDIR(f32, any, f32, OIdhw8i8o),
    REG_SR_BIDIR(f32, any, f32, OIdhw8o8i),
147
    REG_SR_BIDIR(f32, any, f32, gOIdhw4i4o),
148 149 150 151 152
    REG_SR_BIDIR(f32, any, f32, gOdhwi8o),
    REG_SR_BIDIR(f32, any, f32, gOIdhw8i8o),
    REG_SR_BIDIR(f32, any, f32, gOIdhw8o8i),

    REG_SR_BIDIR(f32, any, f32, nCdhw16c),
153
    REG_SR_BIDIR(f32, any, f32, Oidhw4o),
154 155 156 157
    REG_SR_BIDIR(f32, any, f32, Oidhw16o),
    REG_SR_BIDIR(f32, any, f32, Odhwi16o),
    REG_SR_BIDIR(f32, any, f32, OIdhw16o16i),
    REG_SR_BIDIR(f32, any, f32, OIdhw16i16o),
158
    REG_SR_BIDIR(f32, any, f32, gOidhw4o),
159 160
    REG_SR_BIDIR(f32, any, f32, gOidhw16o),
    REG_SR_BIDIR(f32, any, f32, gOdhwi16o),
Alexey Suhov's avatar
Alexey Suhov committed
161 162
    REG_SR_BIDIR(f32, any, f32, gOIdhw16o16i),
    REG_SR_BIDIR(f32, any, f32, gOIdhw16i16o),
163 164 165 166 167 168 169

    /* WA to prevent fallback on reference implementations */
    REG_SR_DIRECT_COPY(u8, f32),
    REG_SR_DIRECT_COPY(u8, s8),
    REG_SR_DIRECT_COPY(s8, u8),
    REG_SR_DIRECT_COPY(u8, u8),
    REG_SR_DIRECT_COPY(s8, s8),
Alexey Suhov's avatar
Alexey Suhov committed
170

171 172 173 174 175
 /* fp32: blocked <-> blocked with tail */
    REG_SR_BIDIR(f32, nCw8c, f32, nCw16c),
    REG_SR_BIDIR(f32, nChw8c, f32, nChw16c),
    REG_SR_BIDIR(f32, nCdhw8c, f32, nCdhw16c),

Alexey Suhov's avatar
Alexey Suhov committed
176
    /* int: flat <-> blocked with tail */
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
    REG_SR(f32, nChw8c, u8, nhwc, fmt_order::keep),
    REG_SR(f32, nChw8c, s8, nhwc, fmt_order::keep),
    REG_SR(u8, nhwc, f32, nChw8c, fmt_order::keep),
    REG_SR(s8, nhwc, f32, nChw8c, fmt_order::keep),
    REG_SR(f32, nhwc, u8, nhwc, fmt_order::keep),
    REG_SR(f32, nhwc, s8, nhwc, fmt_order::keep),
    REG_SR(u8, nhwc, f32, nhwc, fmt_order::keep),
    REG_SR(s8, nhwc, f32, nhwc, fmt_order::keep),
    REG_SR(s8, nhwc, u8, nhwc, fmt_order::keep),
    REG_SR(u8, nhwc, s8, nhwc, fmt_order::keep),
    REG_SR(u8, nhwc, s8, nhwc, fmt_order::keep),
    REG_SR(f32, nchw, u8, nhwc, fmt_order::keep),
    REG_SR(f32, nchw, s8, nhwc, fmt_order::keep),
    REG_SR(u8, nchw, u8, nhwc, fmt_order::keep),
    REG_SR(s8, nchw, s8, nhwc, fmt_order::keep),
    REG_SR(u8, nhwc, f32, nchw, fmt_order::keep),

    REG_SR_BIDIR(f32, any, s32, nChw8c),
    REG_SR_BIDIR(f32, any, s8, nChw8c),
    REG_SR_BIDIR(f32, any, u8, nChw8c),
    REG_SR_BIDIR(s32, any, f32, nChw8c),
    REG_SR_BIDIR(s32, any, s32, nChw8c),
    REG_SR_BIDIR(s32, any, s8, nChw8c),
    REG_SR_BIDIR(s32, any, u8, nChw8c),
    REG_SR_BIDIR(s8, any, f32, nChw8c),
    REG_SR_BIDIR(s8, any, s32, nChw8c),
    REG_SR_BIDIR(s8, any, s8, nChw8c),
    REG_SR_BIDIR(s8, any, u8, nChw8c),
    REG_SR_BIDIR(u8, any, f32, nChw8c),
    REG_SR_BIDIR(u8, any, s32, nChw8c),
    REG_SR_BIDIR(u8, any, s8, nChw8c),
    REG_SR_BIDIR(u8, any, u8, nChw8c),

    REG_SR_BIDIR(f32, any, s32, nChw16c),
    REG_SR_BIDIR(f32, any, s8, nChw16c),
    REG_SR_BIDIR(f32, any, u8, nChw16c),
    REG_SR_BIDIR(s32, any, f32, nChw16c),
    REG_SR_BIDIR(s32, any, s32, nChw16c),
    REG_SR_BIDIR(s32, any, s8, nChw16c),
    REG_SR_BIDIR(s32, any, u8, nChw16c),
    REG_SR_BIDIR(s8, any, f32, nChw16c),
    REG_SR_BIDIR(s8, any, s32, nChw16c),
    REG_SR_BIDIR(s8, any, s8, nChw16c),
    REG_SR_BIDIR(s8, any, u8, nChw16c),
    REG_SR_BIDIR(u8, any, f32, nChw16c),
    REG_SR_BIDIR(u8, any, s32, nChw16c),
    REG_SR_BIDIR(u8, any, s8, nChw16c),
    REG_SR_BIDIR(u8, any, u8, nChw16c),

    REG_SR_BIDIR(f32, any, f32, OIhw4i16o4i),
    REG_SR_BIDIR(f32, any, s8, OIhw4i16o4i),
    REG_SR_BIDIR(s8, any, f32, OIhw4i16o4i),
    REG_SR_BIDIR(s8, any, s8, OIhw4i16o4i),
    REG_SR_BIDIR(f32, any, s8, gOIhw4i16o4i),
    REG_SR_BIDIR(s8, any, f32, gOIhw4i16o4i),
    REG_SR_BIDIR(f32, any, f32, gOIhw4i16o4i),
    REG_SR_BIDIR(s8, any, s8, gOIhw4i16o4i),

    REG_SR(f32, any, f32, OhIw8o4i, fmt_order::keep),
    REG_SR(f32, any, s8, OhIw8o4i, fmt_order::keep),
    REG_SR(s8, any, f32, OhIw8o4i, fmt_order::keep),
    REG_SR(s8, any, s8, OhIw8o4i, fmt_order::keep),
    REG_SR(f32, any, s8, gOhIw8o4i, fmt_order::keep),
    REG_SR(s8, any, f32, gOhIw8o4i, fmt_order::keep),
    REG_SR(f32, any, f32, gOhIw8o4i, fmt_order::keep),
    REG_SR(s8, any, s8, gOhIw8o4i, fmt_order::keep),
    REG_SR(f32, oihw, s8, OhIw8o4i_s8s8, fmt_order::keep),
    REG_SR(s8, oihw, s8, OhIw8o4i_s8s8, fmt_order::keep),
    REG_SR(f32, goihw, s8, gOhIw8o4i_s8s8, fmt_order::keep),
    REG_SR(s8, goihw, s8, gOhIw8o4i_s8s8, fmt_order::keep),

248 249 250
    REG_SR(bin, any, bin, OhIw8o32i, fmt_order::keep),
    REG_SR(bin, any, bin, OhIw16o32i, fmt_order::keep),

251 252
    REG_SR(f32, any, s8, hwio_s8s8, fmt_order::keep),
    REG_SR(f32, any, s8, hwigo_s8s8, fmt_order::keep),
253
    REG_SR(s8, any, s8, hwio_s8s8, fmt_order::keep),
254
    REG_SR(s8, any, s8, hwigo_s8s8, fmt_order::keep),
255 256 257 258

    REG_SR(f32, goihw, s8, gOIhw4o4i_s8s8, fmt_order::keep),
    REG_SR(s8, goihw, s8, gOIhw4o4i_s8s8, fmt_order::keep),

259 260
    REG_SR(f32, oihw, s8, OIhw4i16o4i_s8s8, fmt_order::keep),
    REG_SR(f32, goihw, s8, gOIhw4i16o4i_s8s8, fmt_order::keep),
261
    REG_SR(s8, oihw, s8, OIhw4i16o4i_s8s8, fmt_order::keep),
262
    REG_SR(s8, goihw, s8, gOIhw4i16o4i_s8s8, fmt_order::keep),
Alexey Suhov's avatar
Alexey Suhov committed
263

264 265 266 267 268
    REG_SR(f32, goihw, s8, gOIhw2i8o4i_s8s8, fmt_order::keep),
    REG_SR(s8, goihw, s8, gOIhw2i8o4i_s8s8, fmt_order::keep),

    REG_SR(f32, goihw, s8, Goihw16g_s8s8, fmt_order::keep),
    REG_SR(s8, goihw, s8, Goihw16g_s8s8, fmt_order::keep),
openvino-pushbot's avatar
openvino-pushbot committed
269
    /* s16 <-> s16 */
Alexey Suhov's avatar
Alexey Suhov committed
270
    REG_SR_DIRECT_COPY(s16, s16),
271 272 273

    REG_SR_BIDIR(s16, any, s16, OIhw8i16o2i),
    REG_SR_BIDIR(s16, any, s16, gOIhw8i16o2i),
Alexey Suhov's avatar
Alexey Suhov committed
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
    REG_SR_BIDIR(s16, OIhw8i16o2i, s16, OIhw8o16i2o),
    REG_SR_BIDIR(s16, gOIhw8i16o2i, s16, gOIhw8o16i2o),

    /* reference: the last line of defence */
    REG_SR(f32, any, f32, any, fmt_order::any, spec::reference),
    REG_SR(f32, any, s32, any, fmt_order::any, spec::reference),
    REG_SR(f32, any, s16, any, fmt_order::any, spec::reference),
    REG_SR(f32, any, s8, any, fmt_order::any, spec::reference),
    REG_SR(f32, any, u8, any, fmt_order::any, spec::reference),

    REG_SR(s32, any, f32, any, fmt_order::any, spec::reference),
    REG_SR(s32, any, s32, any, fmt_order::any, spec::reference),
    REG_SR(s32, any, s16, any, fmt_order::any, spec::reference),
    REG_SR(s32, any, s8, any, fmt_order::any, spec::reference),
    REG_SR(s32, any, u8, any, fmt_order::any, spec::reference),

    REG_SR(s16, any, f32, any, fmt_order::any, spec::reference),
    REG_SR(s16, any, s32, any, fmt_order::any, spec::reference),
    REG_SR(s16, any, s16, any, fmt_order::any, spec::reference),

    REG_SR(s8, any, f32, any, fmt_order::any, spec::reference),
    REG_SR(s8, any, s32, any, fmt_order::any, spec::reference),
    REG_SR(s8, any, s8, any, fmt_order::any, spec::reference),
    REG_SR(s8, any, u8, any, fmt_order::any, spec::reference),

    REG_SR(u8, any, f32, any, fmt_order::any, spec::reference),
    REG_SR(u8, any, s32, any, fmt_order::any, spec::reference),
    REG_SR(u8, any, u8, any, fmt_order::any, spec::reference),
    REG_SR(u8, any, s8, any, fmt_order::any, spec::reference),

openvino-pushbot's avatar
openvino-pushbot committed
304 305 306 307 308 309 310 311 312 313 314 315
    /* eol */
    nullptr,
};
}

const rpd_create_f *cpu_engine_t::get_reorder_implementation_list() const {
    return cpu_reorder_impl_list;
}

}
}
}