mul_compute.h 2.3 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2019 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.

#pragma once
16 17
#include <memory>
#include "lite/backends/cuda/math/gemm.h"
Y
Yan Chunwei 已提交
18 19 20 21 22 23 24 25
#include "lite/core/kernel.h"
#include "lite/operators/op_params.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace cuda {

W
Wilber 已提交
26 27
template <typename T, PrecisionType PType>
class MulCompute : public KernelLite<TARGET(kCUDA), PType> {
Y
Yan Chunwei 已提交
28 29 30
 public:
  using param_t = operators::MulParam;

31
  void PrepareForRun() override {
W
Wilber 已提交
32
    gemm_impl_.reset(new lite::cuda::math::Gemm<T, T>);
33 34
  }

Y
Yan Chunwei 已提交
35
  void Run() override {
36
    auto& context = this->ctx_->template As<CUDAContext>();
W
Wilber 已提交
37 38 39 40
    auto& param = this->template Param<param_t>();
    const auto* x_data = param.x->template data<T>();
    const auto* y_data = param.y->template data<T>();
    auto* out_data = param.output->template mutable_data<T>(TARGET(kCUDA));
Y
Yan Chunwei 已提交
41

42 43 44 45 46 47 48 49 50 51 52 53 54
    int x_h = static_cast<int>(
        param.x->dims().Slice(0, param.x_num_col_dims).production());
    int x_w = static_cast<int>(
        param.x->dims()
            .Slice(param.x_num_col_dims, param.x->dims().size())
            .production());
    int y_h = static_cast<int>(
        param.y->dims().Slice(0, param.y_num_col_dims).production());
    int y_w = static_cast<int>(
        param.y->dims()
            .Slice(param.y_num_col_dims, param.y->dims().size())
            .production());
    CHECK_EQ(x_w, y_h) << "x_w must be equal with y_h";
Y
Yan Chunwei 已提交
55

56 57
    CHECK(gemm_impl_->init(false, false, x_h, y_w, x_w, &context));
    gemm_impl_->run(1.0f, 0.0f, x_data, y_data, out_data, &context);
Y
Yan Chunwei 已提交
58 59 60
  }

  virtual ~MulCompute() = default;
61 62

 private:
W
Wilber 已提交
63
  std::unique_ptr<lite::cuda::math::Gemm<T, T>> gemm_impl_{nullptr};
Y
Yan Chunwei 已提交
64 65 66 67 68 69
};

}  // namespace cuda
}  // namespace kernels
}  // namespace lite
}  // namespace paddle