sgd_op_xpu.cc 3.8 KB
Newer Older
C
Chengmo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2020 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. */
#ifdef PADDLE_WITH_XPU
#include "paddle/fluid/operators/optimizers/sgd_op.h"
#include <string>
17 18
#include "paddle/fluid/platform/device/device_wrapper.h"

C
Chengmo 已提交
19 20 21 22 23
namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class SGDOpXPUKernel : public framework::OpKernel<T> {
24 25
  using XPUType = typename XPUTypeTrait<T>::Type;

C
Chengmo 已提交
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
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
    const auto *learning_rate = ctx.Input<framework::Tensor>("LearningRate");

    const auto *param_var = ctx.InputVar("Param");
    const auto *grad_var = ctx.InputVar("Grad");

    if (param_var->IsType<framework::LoDTensor>() &&
        grad_var->IsType<framework::LoDTensor>()) {
      const auto *param = ctx.Input<framework::Tensor>("Param");
      auto *param_out = ctx.Output<framework::Tensor>("ParamOut");
      // Actually, all tensors are LoDTensor except SelectedRows.
      const auto *grad = ctx.Input<framework::Tensor>("Grad");
      auto sz = param_out->numel();
      PADDLE_ENFORCE_EQ(param->numel(), sz,
                        platform::errors::InvalidArgument(
                            "The input tensor Param's numel of SgdOp "
                            "should be equal with ParamOut's numel. "
                            "But received Param's "
                            "numel = [%s], ParamOut's numel = [%s]",
                            param->numel(), sz));
      PADDLE_ENFORCE_EQ(grad->numel(), sz,
                        platform::errors::InvalidArgument(
                            "The input tensor Grad's numel of SgdOp "
                            "should be equal with ParamOut's numel. "
                            "But received Grad's "
                            "numel = [%s], ParamOut's numel = [%s]",
                            grad->numel(), sz));

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      const T *lr_t = learning_rate->data<T>();
      auto &dev_ctx = ctx.template device_context<DeviceContext>();
      xpu::ctx_guard RAII_GUARD(dev_ctx.x_context());
      const float *lr = nullptr;
      if (std::is_same<T, paddle::platform::float16>::value) {
        float *lr_float =
            RAII_GUARD.alloc_l3_or_gm<float>(learning_rate->numel());
        int r = xpu::cast_v2<XPUType, float>(
            dev_ctx.x_context(), reinterpret_cast<const XPUType *>(lr_t),
            lr_float, learning_rate->numel());
        PADDLE_ENFORCE_XDNN_SUCCESS(r, "clip_v2");
        lr = lr_float;
      } else {
        lr = reinterpret_cast<const float *>(lr_t);
      }

C
Chengmo 已提交
71 72 73 74
      const T *param_data = param->data<T>();
      const T *grad_data = grad->data<T>();
      T *out_data = param_out->mutable_data<T>(ctx.GetPlace());

75 76 77 78 79
      int r = xpu::sgd(dev_ctx.x_context(),
                       reinterpret_cast<const XPUType *>(grad_data),
                       reinterpret_cast<const XPUType *>(param_data), lr,
                       reinterpret_cast<XPUType *>(out_data), sz);
      PADDLE_ENFORCE_XDNN_SUCCESS(r, "sgd");
C
Chengmo 已提交
80 81 82 83 84 85 86 87
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
88
namespace plat = paddle::platform;
C
Chengmo 已提交
89
REGISTER_OP_XPU_KERNEL(
90 91
    sgd, ops::SGDOpXPUKernel<paddle::platform::XPUDeviceContext, float>,
    ops::SGDOpXPUKernel<paddle::platform::XPUDeviceContext, plat::float16>);
C
Chengmo 已提交
92
#endif