axpy_handler.cc 4.1 KB
Newer Older
L
lidanqing 已提交
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
/* Copyright (c) 2021 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. */

#include <cinttypes>
#include <memory>
#include <string>
#include <vector>

#include "mkldnn.hpp"
#include "paddle/fluid/operators/mkldnn/axpy_handler.h"
#include "paddle/fluid/platform/bfloat16.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/mkldnn_helper.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/platform/profiler.h"

namespace paddle {
namespace operators {

namespace plat = paddle::platform;

namespace {

template <typename T>
36
class AXPYHandler {
L
lidanqing 已提交
37
 public:
38 39 40 41 42 43 44 45 46 47 48
  AXPYHandler(const dnnl::engine mkldnn_engine, int n, float alpha) {
    platform::MKLDNNDeviceContext::tls().log_lib_version();
    auto md = dnnl::memory::desc({n}, plat::MKLDNNGetDataType<T>(),
                                 dnnl::memory::format_tag::x);
    src_mem_ = dnnl::memory(md, mkldnn_engine, DNNL_MEMORY_NONE);
    dst_mem_ = dnnl::memory(md, mkldnn_engine, DNNL_MEMORY_NONE);
    dnnl::primitive_attr reorder_attr;
    dnnl::post_ops post_operations;
    if (alpha != 1.f) {
      std::vector<float> scales(1, alpha);
      reorder_attr.set_output_scales(0, scales);
L
lidanqing 已提交
49
    }
50
    post_operations.append_sum(1.0f);
L
lidanqing 已提交
51

52 53
    reorder_attr.set_post_ops(post_operations);
    reorder_p_ = dnnl::reorder(src_mem_, dst_mem_, reorder_attr);
L
lidanqing 已提交
54 55
  }

56 57 58
  dnnl::memory &AcquireSrcMemory(const T *x) {
    src_mem_.set_data_handle(plat::to_void_cast<T>(x));
    return src_mem_;
L
lidanqing 已提交
59 60
  }

61 62 63
  dnnl::memory &AcquireDstMemory(T *y) {
    dst_mem_.set_data_handle(y);
    return dst_mem_;
L
lidanqing 已提交
64 65
  }

66 67
  const dnnl::reorder &AcquireReorder() { return reorder_p_; }

L
lidanqing 已提交
68
 private:
69 70 71
  dnnl::memory src_mem_;
  dnnl::memory dst_mem_;
  dnnl::reorder reorder_p_;
L
lidanqing 已提交
72 73
};

74 75
template class AXPYHandler<float>;
template class AXPYHandler<plat::bfloat16>;
L
lidanqing 已提交
76 77 78 79 80 81 82 83 84 85

template <typename T>
static void naive_axpy(int n, T alpha, const T *x, T *y) {
  while (n-- > 0) {
    *y += alpha * *x;
    ++y;
    ++x;
  }
}

86 87
}  // anonnymouse namespace

L
lidanqing 已提交
88
template <typename T>
89 90 91 92 93 94 95 96 97 98
class OneDNNAXPYHandler<T>::Impl {
 public:
  Impl(int64_t n, T alpha);
  void operator()(const T *x, T *y);

 private:
  std::unique_ptr<AXPYHandler<T>> handler_;
  int64_t n_;
  T alpha_;
};
L
lidanqing 已提交
99

100 101
template <typename T>
OneDNNAXPYHandler<T>::Impl::Impl(int64_t n, T alpha) : n_{n}, alpha_{alpha} {
L
lidanqing 已提交
102 103 104 105 106
  auto &pool = plat::DeviceContextPool::Instance();
  auto cpu_place = plat::CPUPlace();
  auto *dev_ctx =
      dynamic_cast<plat::MKLDNNDeviceContext *>(pool.Get(cpu_place));
  auto &cpu_engine = dev_ctx->GetEngine();
107 108 109
  handler_ = std::make_unique<AXPYHandler<T>>(cpu_engine, n,
                                              static_cast<float>(alpha));
}
L
lidanqing 已提交
110

111 112 113 114 115 116
template <typename T>
void OneDNNAXPYHandler<T>::Impl::operator()(const T *x, T *y) {
  if (this->n_ < 100) {
    naive_axpy(this->n_, this->alpha_, x, y);
    return;
  }
L
lidanqing 已提交
117

118 119 120
  auto &reorder_src_mem_p = handler_->AcquireSrcMemory(x);
  auto &reorder_dst_mem_p = handler_->AcquireDstMemory(y);
  auto reorder_p = handler_->AcquireReorder();
L
lidanqing 已提交
121
  auto &astream = plat::MKLDNNDeviceContext::tls().get_stream();
122
  reorder_p.execute(astream, reorder_src_mem_p, reorder_dst_mem_p);
L
lidanqing 已提交
123 124 125
  astream.wait();
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139
template <typename T>
OneDNNAXPYHandler<T>::OneDNNAXPYHandler(int64_t n, T alpha)
    : pimpl_{new Impl{n, alpha}, [](Impl *impl) { delete impl; }} {
  VLOG(4) << "[OneDNN] OneDNNAXPYHandler<" << typeid(T).name() << ">, "
          << "n: " << n << ", alpha: " << alpha;
}

template <typename T>
void OneDNNAXPYHandler<T>::operator()(const T *x, T *y) {
  pimpl_->operator()(x, y);
}

template class OneDNNAXPYHandler<float>;
template class OneDNNAXPYHandler<plat::bfloat16>;
L
lidanqing 已提交
140 141 142

}  // namespace operators
}  // namespace paddle