fc_mkldnn_op.cc 20.7 KB
Newer Older
M
mozga-intel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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. */

15 16
#include <mkldnn/include/mkldnn_types.h>
#include <memory>
M
mozga-intel 已提交
17
#include "paddle/fluid/framework/tensor.h"
18
#include "paddle/fluid/operators/fc_op.h"
M
mozga-intel 已提交
19 20
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/mkldnn_helper.h"
21
#include "paddle/fluid/platform/variant.h"
M
mozga-intel 已提交
22 23 24 25

namespace paddle {
namespace operators {

26 27 28 29 30 31 32 33 34 35 36 37 38
using framework::DataLayout;
using framework::Tensor;
using framework::LoDTensor;
using framework::DDim;
using framework::ExecutionContext;
using platform::MKLDNNDeviceContext;
using platform::to_void_cast;
using platform::GetMKLDNNFormat;
using mkldnn::memory;
using mkldnn::inner_product_forward;
using mkldnn::primitive;
using mkldnn::stream;
using mkldnn::prop_kind;
M
mozga-intel 已提交
39

M
Michał Gallus 已提交
40
template <typename T_in, typename T_w, typename T_out>
41
class FCPrimitiveFactory {
M
mozga-intel 已提交
42
 public:
43 44 45 46 47 48 49
  explicit FCPrimitiveFactory(const mkldnn::engine& engine) : engine_(engine) {}

  inner_product_forward CreateFcPrimitive(const LoDTensor* input,
                                          const Tensor* weights,
                                          const Tensor* bias, LoDTensor* output,
                                          const ExecutionContext& ctx) {
    RecomputeOutputDims(ctx, input, weights, output);
M
Michał Gallus 已提交
50 51
    // If primitive has already been created and cached, don't create new one,
    // but update input and output data pointers and return it.
52 53 54 55
    if (fc_) {
      UpdateDataPointers(ctx, output, input);
      return *fc_;
    }
M
Michał Gallus 已提交
56 57
    auto src_desc = CreateMemDescriptor<T_in>(input, input->format());
    input_ = CreateMemory<T_in>(src_desc, input);
M
mozga-intel 已提交
58

M
Michał Gallus 已提交
59 60 61
    // Since MKL-DNN doesn't support 4D column-major data formats in
    // inner_product
    // primitive, transpose the weights to be in row-major format
62 63 64 65
    weights_ = TransposeWeights(weights);
    if (src_desc.data.ndims == 4) {
      weights_ = CreateFourDimWeightsMemory(input, weights);
    }
M
Michał Gallus 已提交
66 67
    // If int8 data type is desired, weights are quantized to signed int8
    QuantizeWeights(ctx);
68

M
Michał Gallus 已提交
69 70 71 72
    // Choose MKLDNNMemoryFormat::any so that MKL-DNN can determine itself what
    // is the best format for output during the creation of inner product
    // primitive descriptor
    auto dst_desc = CreateMemDescriptor<T_out>(output, MKLDNNMemoryFormat::any);
M
mozga-intel 已提交
73

74 75
    fc_ = CreateFcPrimitive(*input_, *weights_, dst_desc, bias, output, ctx);
    return *fc_;
M
mozga-intel 已提交
76 77
  }

78 79 80
 private:
  void UpdateDataPointers(const ExecutionContext& ctx, Tensor* out,
                          const Tensor* in) {
M
Michał Gallus 已提交
81 82 83 84 85
    input_->set_data_handle(to_void_cast(in->data<T_in>()));
    output_->set_data_handle(out->mutable_data<T_out>(ctx.GetPlace()));
    // If the primitive exists, but the output tensor has changed its
    // variable, update its format to what has been determined in first
    // call to CreateFcPrimitive method.
86
    if (out->format() == MKLDNNMemoryFormat::format_undef) {
A
Adam 已提交
87
      auto output_format = platform::GetMKLDNNFormat(*output_);
88
      out->set_format((MKLDNNMemoryFormat)output_format);
89
    }
M
mozga-intel 已提交
90 91
  }

M
Michał Gallus 已提交
92
  // Choose weight memory format based on input memory format
93 94
  MKLDNNMemoryFormat MatchWeightFormat(MKLDNNMemoryFormat fmt) {
    using format = MKLDNNMemoryFormat;
95 96 97 98 99 100 101
    switch (fmt) {
      case format::nChw16c:
        return format::oIhw16i;
      case format::nChw8c:
        return format::oIhw8i;
      case format::nchw:
        return format::oihw;
M
Michał Gallus 已提交
102 103
      case format::nhwc:
        return format::hwio;
104 105 106
      default:
        return format::format_undef;
    }
M
mozga-intel 已提交
107 108
  }

M
Michał Gallus 已提交
109
  // Convert data from one data format to another
110 111 112 113
  mkldnn::memory Reorder(const memory::desc& src_desc,
                         const memory::desc& dst_desc, const void* src_data) {
    auto src_mem = memory({src_desc, engine_}, const_cast<void*>(src_data));
    auto dst_mem = memory({dst_desc, engine_});
M
mozga-intel 已提交
114

115 116
    auto reorder = mkldnn::reorder(src_mem, dst_mem);
    stream(stream::kind::eager).submit({reorder}).wait();
M
mozga-intel 已提交
117

118
    return dst_mem;
M
mozga-intel 已提交
119 120
  }

M
Michał Gallus 已提交
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
  // Convert data from one data format to another and rescale it.
  // If the desired data type is (un)signed int8, quantization occurs here.
  mkldnn::memory Reorder(const memory& src_mem,
                         const memory::primitive_desc& dst_pd,
                         const std::vector<float>& scale_data) {
    mkldnn::memory dst_mem = mkldnn::memory(dst_pd);
    mkldnn::primitive_attr attributes;
    // According to MKL-DNN's documentation mask determines along which
    // dimensions should the scale be applied.
    // 0 - Single scale applied to whole tensor
    // 1 - Apply Scale along a slice of each dimension which index is 1.
    //     In case of weights quantization, that dimension is output,
    //     becuase we perform per-output-channel quantization
    int mask = CreateMask(0, scale_data.size() > 1);
    attributes.set_output_scales(mask, scale_data);
    auto reorder =
        mkldnn::reorder(mkldnn::reorder::primitive_desc(
                            src_mem.get_primitive_desc(), dst_pd, attributes),
                        src_mem, dst_mem);

    stream(stream::kind::eager).submit({reorder}).wait();

    return dst_mem;
  }

  template <typename T>
147
  static mkldnn::memory::desc CreateMemDescriptor(const std::vector<int>& dims,
148
                                                  MKLDNNMemoryFormat format) {
149 150
    return platform::MKLDNNMemDesc(dims, platform::MKLDNNGetDataType<T>(),
                                   format);
M
mozga-intel 已提交
151 152
  }

M
Michał Gallus 已提交
153
  template <typename T>
154
  static mkldnn::memory::desc CreateMemDescriptor(const Tensor* tensor,
155
                                                  MKLDNNMemoryFormat format) {
156
    auto dims = framework::vectorize<int>(tensor->dims());
M
Michał Gallus 已提交
157
    return CreateMemDescriptor<T>(dims, format);
M
mozga-intel 已提交
158 159
  }

M
Michał Gallus 已提交
160
  template <typename T>
161 162 163
  mkldnn::memory CreateMemory(const mkldnn::memory::desc& desc,
                              const Tensor* tensor) {
    return CreateMemory(desc, tensor->data<T>());
M
mozga-intel 已提交
164 165
  }

166 167 168
  mkldnn::memory CreateMemory(const mkldnn::memory::desc& desc,
                              const void* data) {
    return memory({desc, engine_}, const_cast<void*>(data));
M
mozga-intel 已提交
169 170
  }

M
Michał Gallus 已提交
171
  // Transpose weights through MKL-DNN's reorder from io to oi format.
172
  mkldnn::memory TransposeWeights(const Tensor* weights) {
173
    auto dims = framework::vectorize<int>(weights->dims());
174
    std::swap(dims[0], dims[1]);  // Correct output dimensions
M
Michał Gallus 已提交
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
    auto src_desc = CreateMemDescriptor<float>(dims, MKLDNNMemoryFormat::io);
    auto dst_desc = CreateMemDescriptor<float>(dims, MKLDNNMemoryFormat::oi);
    return Reorder(src_desc, dst_desc, weights->data<float>());
  }

  // Compute the bias scales so that its values correspond to the
  // scale of data being an output of weights and input multiplication
  std::vector<float> ComputeBiasScales(const ExecutionContext& ctx) {
    auto scale_in_data = ctx.Attr<float>("Scale_in");
    auto scale_weights_data = ctx.Attr<std::vector<float>>("Scale_weights");
    const size_t weight_scales_num = scale_weights_data.size();
    std::vector<float> bias_scales(weight_scales_num);

#pragma omp parallel for
    for (size_t i = 0; i < weight_scales_num; i++) {
      if (scale_weights_data[i] == 0.0)
        bias_scales[i] = 1.0f;
      else
        bias_scales[i] = scale_in_data * scale_weights_data[i];
    }

    return bias_scales;
  }

  // Correct output scale, to take into account scaling of input and weights
  // Since the data that comes out of input and weight multiplication is
  // scaled with its own scales, this data needs to be divided by
  // those scales to normalise them back to what their floating-point range
  // was. Then we multiply them by desired output scale we want on the output.
  std::vector<float> ComputeOutputShiftScale(const ExecutionContext& ctx) {
    auto scale_in_data = ctx.Attr<float>("Scale_in");
    auto scale_weights_data = ctx.Attr<std::vector<float>>("Scale_weights");
    // If the output will be in floats, we don't multiply by scale_out.
    auto scale_out_data = ctx.Attr<bool>("force_fp32_output")
                              ? 1.0f
                              : ctx.Attr<float>("Scale_out");
    const size_t weight_scales_num = scale_weights_data.size();
    std::vector<float> output_shift_scale(weight_scales_num);

#pragma omp parallel for
    for (size_t i = 0; i < weight_scales_num; i++) {
      if (scale_weights_data[i] == 0.0)
        output_shift_scale[i] = scale_out_data;
      else
        output_shift_scale[i] =
            scale_out_data / (scale_in_data * scale_weights_data[i]);
    }

    return output_shift_scale;
  }

  // Computing MKL-DNN's scaling mask which determines along which dimension
  // slice should the scaling be applied. For more data plase refer to:
  // https://intel.github.io/mkl-dnn/group__c__api__attributes.html
  // Section dnnl_status_t DNNL_API dnnl_primitive_attr_set_output_scales
  int CreateMask(int slice_dimension, bool is_multi_channel_quantizied) {
    return is_multi_channel_quantizied ? 1 << slice_dimension : 0;
  }

  void QuantizeWeights(const ExecutionContext& ctx) {
    auto quantized_desc = weights_->get_primitive_desc().desc();
    quantized_desc.data.data_type =
        (mkldnn_data_type_t)platform::MKLDNNGetDataType<T_w>();
    weights_ = Reorder(*weights_, {quantized_desc, engine_},
                       ctx.Attr<std::vector<float>>("Scale_weights"));
  }

  void QuantizeBias(const inner_product_forward::primitive_desc& fc_prim_desc,
                    const ExecutionContext& ctx) {
    auto bias_scales = ComputeBiasScales(ctx);
    bias_ = Reorder(*bias_, fc_prim_desc.bias_primitive_desc(), bias_scales);
  }

  // Fuse relu into FC with activation type attribute has been set to 'relu'
  mkldnn::primitive_attr CreatePostOps(const ExecutionContext& ctx) {
    mkldnn::primitive_attr attributes;
    mkldnn::post_ops post_operations;

    auto output_shift_scale = ComputeOutputShiftScale(ctx);
    int mask = CreateMask(1, output_shift_scale.size() > 1);
    attributes.set_output_scales(mask, output_shift_scale);

    if (ctx.Attr<std::string>("activation_type") == "relu") {
      constexpr float scale = 1.0f;
      constexpr float negative_slope = 0.0f;
      constexpr float placeholder = 1.0f;  // beta
      post_operations.append_eltwise(scale, mkldnn::algorithm::eltwise_relu,
                                     negative_slope, placeholder);
    }

    attributes.set_post_ops(post_operations);
    return attributes;
267
  }
M
mozga-intel 已提交
268

269 270 271 272 273
  inner_product_forward CreateFcPrimitive(const memory& src_memory,
                                          const memory& weights_memory,
                                          const memory::desc& dst_desc,
                                          const Tensor* bias, Tensor* output,
                                          const ExecutionContext& ctx) {
M
Michał Gallus 已提交
274 275
    // Acquire descriptors needed for creation of inner_product primitive
    // descriptor
276 277
    const auto weights_desc = weights_memory.get_primitive_desc().desc();
    const auto src_desc = src_memory.get_primitive_desc().desc();
M
Michał Gallus 已提交
278 279 280 281
    // Based on provided attributes, create attributes used by MKL-DNN to
    // enable fused post-op activations such as 'relu'
    const auto attrs = CreatePostOps(ctx);
    // If bias exists, create inner_product primitive with or without bias
282
    if (bias) {
M
Michał Gallus 已提交
283 284 285 286
      auto bias_desc = CreateMemDescriptor<float>(bias, bias->format());
      bias_ = CreateMemory<float>(bias_desc, bias);
      // Create inner_product descriptor. At this point the format of output
      // is determined.
287
      auto fc_prim_desc =
M
Michał Gallus 已提交
288 289 290
          CreateFcPrimDesc(src_desc, weights_desc, bias_desc, dst_desc, attrs);
      // If int8 is desired, quantize bias into 32-bit signed int
      QuantizeBias(fc_prim_desc, ctx);
291

M
Michał Gallus 已提交
292 293
      // Based on format determined by inner_product, create output in desired
      // memory format
294 295
      output_ = CreateDstMemory(fc_prim_desc, ctx, output);

M
Michał Gallus 已提交
296
      // Return MKL-DNN primitive ready to be fed into pipeline and executed
297 298 299
      return inner_product_forward(fc_prim_desc, src_memory, weights_memory,
                                   *bias_, *output_);
    } else {
M
Michał Gallus 已提交
300 301
      auto fc_prim_desc =
          CreateFcPrimDesc(src_desc, weights_desc, dst_desc, attrs);
302 303 304 305 306 307
      output_ = CreateDstMemory(fc_prim_desc, ctx, output);

      return inner_product_forward(fc_prim_desc, src_memory, weights_memory,
                                   *output_);
    }
  }
M
mozga-intel 已提交
308

309 310 311 312
  mkldnn::inner_product_forward::primitive_desc CreateFcPrimDesc(
      const mkldnn::memory::desc& input_desc,
      const mkldnn::memory::desc& weights_desc,
      const mkldnn::memory::desc& bias_desc,
M
Michał Gallus 已提交
313 314
      const mkldnn::memory::desc& dst_desc,
      const mkldnn::primitive_attr& attrs) {
315 316 317
    auto fc_desc =
        inner_product_forward::desc(prop_kind::forward_scoring, input_desc,
                                    weights_desc, bias_desc, dst_desc);
M
mozga-intel 已提交
318

M
Michał Gallus 已提交
319
    return inner_product_forward::primitive_desc(fc_desc, attrs, engine_);
320
  }
M
mozga-intel 已提交
321

322 323 324
  mkldnn::inner_product_forward::primitive_desc CreateFcPrimDesc(
      const mkldnn::memory::desc& input_desc,
      const mkldnn::memory::desc& weights_desc,
M
Michał Gallus 已提交
325 326
      const mkldnn::memory::desc& dst_desc,
      const mkldnn::primitive_attr& attrs) {
327 328
    auto fc_desc = inner_product_forward::desc(prop_kind::forward, input_desc,
                                               weights_desc, dst_desc);
M
mozga-intel 已提交
329

M
Michał Gallus 已提交
330
    return inner_product_forward::primitive_desc(fc_desc, attrs, engine_);
331
  }
M
mozga-intel 已提交
332

M
Michał Gallus 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345
  // Since MKL-DNN requires the number of input dimensions to be
  // equal to the number of weight dimensions, we have to convert
  // weights to 4D memory if input is 4D. It also requires that
  // all dimensions of weights and inputs agree, with an exception
  // for the batch size and number of output channels (the first dim).
  // In order to perform that we have to prepare the memory descriptor
  // by hand, as MKL-DNN's reorder does not support conversion
  // from one dimensionality to another. Hence, we set
  // the first dimension of weights to resemble number of outputs
  // and then we use the sizes of number of input channels as well
  // as image width and height for latter dimensions. Then we create
  // memories, find a format corresponding with input format and
  // perform a converion.
346 347
  mkldnn::memory CreateFourDimWeightsMemory(const Tensor* input,
                                            const Tensor* weights) {
348 349
    auto input_dims = framework::vectorize<int>(input->dims());
    auto weight_dims = framework::vectorize<int>(weights->dims());
350
    auto dims = {weight_dims[1], input_dims[1], input_dims[2], input_dims[3]};
M
mozga-intel 已提交
351

352
    auto dst_format = MatchWeightFormat(input->format());
M
Michał Gallus 已提交
353 354
    auto src_desc = CreateMemDescriptor<float>(dims, MKLDNNMemoryFormat::oihw);
    auto dst_desc = CreateMemDescriptor<float>(dims, dst_format);
M
mozga-intel 已提交
355

356 357
    return Reorder(src_desc, dst_desc, weights_->get_data_handle());
  }
M
mozga-intel 已提交
358

M
Michał Gallus 已提交
359 360
  // Create output memory based on output tensor and inner_product
  // primitive descriptor format chosen for output
361 362 363 364 365
  mkldnn::memory CreateDstMemory(
      const mkldnn::inner_product_forward::primitive_desc& fc_prim_desc,
      const ExecutionContext& ctx, Tensor* output) {
    auto dst_prim_desc = fc_prim_desc.dst_primitive_desc();
    auto buffer_size = dst_prim_desc.get_size();
M
Michał Gallus 已提交
366 367 368
    T_out* output_data =
        output->mutable_data<T_out>(ctx.GetPlace(), buffer_size);
    memory dst_mem(dst_prim_desc, to_void_cast<T_out>(output_data));
A
Adam 已提交
369 370
    output->set_format(platform::GetMKLDNNFormat(dst_mem));
    return dst_mem;
371
  }
M
mozga-intel 已提交
372

373 374
  void RecomputeOutputDims(const ExecutionContext& ctx, const LoDTensor* input,
                           const Tensor* w, LoDTensor* output) {
L
luotao1 已提交
375
    int in_num_col_dims = ctx.Attr<int>("in_num_col_dims");
376 377 378 379
    bool padding_weights = ctx.Attr<bool>("padding_weights");
    PADDLE_ENFORCE_EQ(padding_weights, false,
                      platform::errors::PermissionDenied(
                          "Weight padding in fc can not be used in MKLDNN."));
L
luotao1 已提交
380
    std::vector<int64_t> output_dims;
381 382
    FCOutputSize(input->dims(), w->dims(), output_dims, in_num_col_dims,
                 padding_weights);
L
luotao1 已提交
383 384
    output->Resize(framework::make_ddim(output_dims));
    output->set_lod(input->lod());
385
  }
L
luotao1 已提交
386

387 388 389 390 391 392 393 394
 private:
  const mkldnn::engine& engine_;
  boost::optional<memory> bias_;
  boost::optional<memory> input_;
  boost::optional<memory> output_;
  boost::optional<memory> weights_;
  boost::optional<inner_product_forward> fc_;
};
M
mozga-intel 已提交
395

M
Michał Gallus 已提交
396 397 398 399 400 401 402 403 404
// Attempt to fetch cached primitive factory based on provided parameters
// of input format, weight dimensions and output name.
// If not cached, create a new one.
template <typename T_in, typename T_w, typename T_out>
static std::shared_ptr<FCPrimitiveFactory<T_in, T_w, T_out>>
GetPrimitiveFactory(const MKLDNNDeviceContext& dev_ctx,
                    const ExecutionContext& ctx, const Tensor* input,
                    const Tensor* weights,
                    const mkldnn::engine& mkldnn_engine) {
405
  const std::string key = platform::CreateKey(
M
Michał Gallus 已提交
406
      platform::ThreadIDasStr(), input->format(),
H
hong 已提交
407
      framework::vectorize<int>(weights->dims()), ctx.OutputName("Out"));
408 409

  auto prim_creator =
M
Michał Gallus 已提交
410 411
      std::static_pointer_cast<FCPrimitiveFactory<T_in, T_w, T_out>>(
          dev_ctx.GetBlob(key));
412
  if (prim_creator == nullptr) {
M
Michał Gallus 已提交
413 414
    prim_creator =
        std::make_shared<FCPrimitiveFactory<T_in, T_w, T_out>>(mkldnn_engine);
415
    dev_ctx.SetBlob(key, prim_creator);
M
mozga-intel 已提交
416 417
  }

418 419
  return prim_creator;
}
M
mozga-intel 已提交
420

M
Michał Gallus 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
// Choose appropriate primitive factory implementation based on inferred
// output type (uint8, int8 or float).
template <typename T_in, typename T_w>
static inner_product_forward GetFcPrimitive(
    const MKLDNNDeviceContext& dev_ctx, const ExecutionContext& ctx,
    const LoDTensor* input, const Tensor* w, const Tensor* bias,
    LoDTensor* output, const mkldnn::engine& mkldnn_engine, bool fuse_relu,
    bool force_fp32_output) {
  constexpr bool is_int8 =
      std::is_same<T_in, int8_t>::value || std::is_same<T_in, uint8_t>::value;
  if (!is_int8 || force_fp32_output) {
    return GetPrimitiveFactory<T_in, T_w, float>(dev_ctx, ctx, input, w,
                                                 mkldnn_engine)
        ->CreateFcPrimitive(input, w, bias, output, ctx);
  } else if (fuse_relu) {
    return GetPrimitiveFactory<T_in, T_w, uint8_t>(dev_ctx, ctx, input, w,
                                                   mkldnn_engine)
        ->CreateFcPrimitive(input, w, bias, output, ctx);
  } else {
    return GetPrimitiveFactory<T_in, T_w, int8_t>(dev_ctx, ctx, input, w,
                                                  mkldnn_engine)
        ->CreateFcPrimitive(input, w, bias, output, ctx);
  }
}

template <typename T_in, typename T_w>
class FCMKLDNNOpKernel : public framework::OpKernel<T_in> {
M
mozga-intel 已提交
448 449
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const override {
M
Michał Gallus 已提交
450 451 452
    PADDLE_ENFORCE_EQ(
        platform::is_cpu_place(ctx.GetPlace()), true,
        platform::errors::PreconditionNotMet("FC MKL-DNN must use CPUPlace."));
M
mozga-intel 已提交
453 454 455
    auto& dev_ctx = ctx.template device_context<MKLDNNDeviceContext>();
    const auto& mkldnn_engine = dev_ctx.GetEngine();

456 457
    auto input = ctx.Input<LoDTensor>("Input");
    auto w = ctx.Input<Tensor>("W");
T
tensor-tang 已提交
458
    auto bias = ctx.Input<Tensor>("Bias");
459
    auto output = ctx.Output<LoDTensor>("Out");
M
mozga-intel 已提交
460

M
Michał Gallus 已提交
461 462 463 464 465 466
    bool fuse_relu = ctx.Attr<std::string>("activation_type") == "relu";
    bool force_fp32_output = ctx.Attr<bool>("force_fp32_output");

    auto fc =
        GetFcPrimitive<T_in, T_w>(dev_ctx, ctx, input, w, bias, output,
                                  mkldnn_engine, fuse_relu, force_fp32_output);
467
    stream(stream::kind::eager).submit({fc}).wait();
M
mozga-intel 已提交
468

469
    output->set_layout(DataLayout::kMKLDNN);
M
mozga-intel 已提交
470 471 472 473 474
  }
};
}  // namespace operators
}  // namespace paddle

M
Michał Gallus 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
// Weights of FC are by default stored using fp32, template argument of weight
// data type implies their destination data type. (What's eventually going to
// be used during computations of kernel).
namespace ops = paddle::operators;
REGISTER_OP_KERNEL_WITH_CUSTOM_TYPE(fc, MKLDNN, ::paddle::platform::CPUPlace,
                                    FP32, ops::kFCMKLDNNFP32,
                                    ops::FCMKLDNNOpKernel<float, float>);

REGISTER_OP_KERNEL_WITH_CUSTOM_TYPE(fc, MKLDNN, ::paddle::platform::CPUPlace,
                                    U8, ops::kFCMKLDNNINT8,
                                    ops::FCMKLDNNOpKernel<uint8_t, int8_t>);

REGISTER_OP_KERNEL_WITH_CUSTOM_TYPE(fc, MKLDNN, ::paddle::platform::CPUPlace,
                                    S8, ops::kFCMKLDNNINT8,
                                    ops::FCMKLDNNOpKernel<int8_t, int8_t>);