activation_op_mlu.cc 17.9 KB
Newer Older
F
fwenguang 已提交
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 (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 Licnse. */

#include <memory>
#include <string>

#include "paddle/fluid/operators/activation_op.h"
#include "paddle/fluid/operators/mlu/mlu_baseop.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

26
template <cnnlActivationMode_t act_mode, typename T>
F
fwenguang 已提交
27 28 29 30 31
class ActivationMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<Tensor>("X");
    auto* output = ctx.Output<Tensor>("Out");
32
    float alpha = ctx.HasAttr("alpha") ? ctx.Attr<float>("alpha") : 1.0f;
F
fwenguang 已提交
33 34 35

    output->mutable_data<T>(ctx.GetPlace());

36
    MLUCnnlActivationDesc act_desc(act_mode, alpha);
37 38 39
    MLUCnnlTensorDesc input_desc(*input);
    MLUCnnlTensorDesc output_desc(*output);

40 41 42 43 44 45
    MLUCnnl::Active(ctx,
                    act_desc.get(),
                    input_desc.get(),
                    GetBasePtr(input),
                    output_desc.get(),
                    GetBasePtr(output));
F
fwenguang 已提交
46 47 48
  }
};

49
// For gelu, leaky_relu
50
template <cnnlActivationMode_t act_mode, typename T>
51 52 53 54 55 56 57 58 59 60 61 62 63 64
class ActivationGradMLUKernelV1 : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    float alpha = ctx.HasAttr("alpha") ? ctx.Attr<float>("alpha") : 1.0f;

    dx->mutable_data<T>(ctx.GetPlace());

    MLUCnnlTensorDesc x_desc(*x);
    MLUCnnlTensorDesc dout_desc(*dout);
    MLUCnnlTensorDesc dx_desc(*dx);
    MLUCnnlActivationDesc act_desc(act_mode, alpha);
65 66 67 68 69 70 71 72 73 74 75 76
    MLUCnnl::ActiveGrad(ctx,
                        act_desc.get(),
                        nullptr,
                        nullptr,
                        nullptr,
                        nullptr,
                        dout_desc.get(),
                        GetBasePtr(dout),
                        x_desc.get(),
                        GetBasePtr(x),
                        dx_desc.get(),
                        GetBasePtr(dx));
77 78 79 80 81 82
  }
};

// For tanh, sigmoid
template <cnnlActivationMode_t act_mode, typename T>
class ActivationGradMLUKernelV2 : public framework::OpKernel<T> {
F
fwenguang 已提交
83 84 85 86 87
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* out = ctx.Input<Tensor>("Out");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
88
    float alpha = ctx.HasAttr("alpha") ? ctx.Attr<float>("alpha") : 1.0f;
F
fwenguang 已提交
89 90 91

    dx->mutable_data<T>(ctx.GetPlace());

92 93 94
    MLUCnnlTensorDesc out_desc(*out);
    MLUCnnlTensorDesc dout_desc(*dout);
    MLUCnnlTensorDesc dx_desc(*dx);
95
    MLUCnnlActivationDesc act_desc(act_mode, alpha);
96 97 98 99 100 101 102 103 104 105 106 107
    MLUCnnl::ActiveGrad(ctx,
                        act_desc.get(),
                        nullptr,
                        nullptr,
                        out_desc.get(),
                        GetBasePtr(out),
                        dout_desc.get(),
                        GetBasePtr(dout),
                        nullptr,
                        nullptr,
                        dx_desc.get(),
                        GetBasePtr(dx));
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  }
};

// For relu, relu6
template <cnnlActivationMode_t act_mode, typename T>
class ActivationGradMLUKernelV3 : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* out = ctx.Input<Tensor>("Out");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    float alpha = ctx.HasAttr("alpha") ? ctx.Attr<float>("alpha") : 1.0f;

    dx->mutable_data<T>(ctx.GetPlace());

    MLUCnnlTensorDesc out_desc(*out);
    MLUCnnlTensorDesc dout_desc(*dout);
    MLUCnnlTensorDesc dx_desc(*dx);
    MLUCnnlActivationDesc act_desc(act_mode, alpha);
127 128 129 130 131 132 133 134 135 136 137 138
    MLUCnnl::ActiveGrad(ctx,
                        act_desc.get(),
                        nullptr,
                        nullptr,
                        nullptr,
                        nullptr,
                        dout_desc.get(),
                        GetBasePtr(dout),
                        out_desc.get(),
                        GetBasePtr(out),
                        dx_desc.get(),
                        GetBasePtr(dx));
F
fwenguang 已提交
139 140 141
  }
};

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
// For sqrt
template <typename T>
class SqrtMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");
    auto place = ctx.GetPlace();

    out->mutable_data<T>(place);

    MLUCnnlTensorDesc input_desc(*x);
    MLUCnnlTensorDesc output_desc(*out);

    cnnlComputationPreference_t prefer = CNNL_COMPUTATION_FAST;
157 158 159 160 161 162
    MLUCnnl::Sqrt(ctx,
                  prefer,
                  input_desc.get(),
                  GetBasePtr(x),
                  output_desc.get(),
                  GetBasePtr(out));
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  }
};

template <typename T>
class SqrtGradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* out = ctx.Input<Tensor>("Out");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto place = ctx.GetPlace();

    dx->mutable_data<T>(place);

    MLUCnnlTensorDesc data_desc(*out);
178 179 180 181
    MLUCnnl::SqrtGrad(ctx,
                      data_desc.get(),
                      GetBasePtr(out),
                      GetBasePtr(dout),
182 183 184 185
                      GetBasePtr(dx));
  }
};

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
// CNNL_LOG_E = 0,
// CNNL_LOG_2 = 1,
// CNNL_LOG_10 = 2,
template <cnnlLogBase_t Log_base, typename T>
class LogMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<Tensor>("X");
    auto* output = ctx.Output<Tensor>("Out");
    output->mutable_data<T>(ctx.GetPlace());

    MLUCnnlTensorDesc input_desc(*input);
    MLUCnnlTensorDesc output_desc(*output);
    cnnlComputationPreference_t prefer = CNNL_COMPUTATION_HIGH_PRECISION;

201 202 203 204 205 206 207
    MLUCnnl::Log(ctx,
                 prefer,
                 Log_base,
                 input_desc.get(),
                 GetBasePtr(input),
                 output_desc.get(),
                 GetBasePtr(output));
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
template <typename T>
class ExpMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<Tensor>("X");
    auto* output = ctx.Output<Tensor>("Out");
    output->mutable_data<T>(ctx.GetPlace());

    MLUCnnlTensorDesc input_desc(*input);
    MLUCnnlTensorDesc output_desc(*output);
    cnnlComputationPreference_t prefer = CNNL_COMPUTATION_HIGH_PRECISION;

    MLUCnnl::Exp(ctx,
                 prefer,
                 input_desc.get(),
                 GetBasePtr(input),
                 output_desc.get(),
                 GetBasePtr(output));
  }
};

template <typename T>
class ExpGradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* out = ctx.Input<Tensor>("Out");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    dx->mutable_data<T>(ctx.GetPlace());
    MLUCnnlTensorDesc dout_desc(*dout);
    MLUCnnlTensorDesc dx_desc(*dx);
    MLUCnnlTensorDesc out_desc(*out);

    MLUCnnlOpTensorDesc op_tensor_desc(
        CNNL_OP_TENSOR_MUL, ToCnnlDataType<T>(), CNNL_NOT_PROPAGATE_NAN);

    MLUCnnl::OpTensor(ctx,
                      op_tensor_desc.get(),
                      dout_desc.get(),
                      GetBasePtr(dout),
                      out_desc.get(),
                      GetBasePtr(out),
                      dx_desc.get(),
                      GetBasePtr(dx),
                      ToCnnlDataType<T>());
  }
};

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
template <typename T>
class HardSwishMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<Tensor>("X");
    auto* output = ctx.Output<Tensor>("Out");
    output->mutable_data<T>(ctx.GetPlace());
    float threshold = ctx.Attr<float>("threshold");
    float scale = ctx.Attr<float>("scale");
    float offset = ctx.Attr<float>("offset");
    PADDLE_ENFORCE_EQ(threshold,
                      6.0f,
                      platform::errors::External(
                          "Not support threshold [%f] in MLU", threshold));
    PADDLE_ENFORCE_EQ(
        scale,
        6.0f,
        platform::errors::External("Not support scale [%f] in MLU", scale));
    PADDLE_ENFORCE_EQ(
        offset,
        3.0f,
        platform::errors::External("Not support offset [%f] in MLU", offset));

    MLUCnnlActivationDesc act_desc(CNNL_ACTIVATION_HARDSWISH,
                                   1.0f /*ceof useless*/);
    MLUCnnlTensorDesc input_desc(*input);
    MLUCnnlTensorDesc output_desc(*output);

    MLUCnnl::Active(ctx,
                    act_desc.get(),
                    input_desc.get(),
                    GetBasePtr(input),
                    output_desc.get(),
                    GetBasePtr(output));
  }
};

template <typename T>
class HardSwishGradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    float threshold = ctx.Attr<float>("threshold");
    float scale = ctx.Attr<float>("scale");
    float offset = ctx.Attr<float>("offset");
    PADDLE_ENFORCE_EQ(threshold,
                      6.0f,
                      platform::errors::External(
                          "Not support threshold [%f] in MLU", threshold));
    PADDLE_ENFORCE_EQ(
        scale,
        6.0f,
        platform::errors::External("Not support scale [%f] in MLU", scale));
    PADDLE_ENFORCE_EQ(
        offset,
        3.0f,
        platform::errors::External("Not support offset [%f] in MLU", offset));
    auto* out = ctx.Input<Tensor>("X");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

    dx->mutable_data<T>(ctx.GetPlace());

    MLUCnnlTensorDesc out_desc(*out);
    MLUCnnlTensorDesc dout_desc(*dout);
    MLUCnnlTensorDesc dx_desc(*dx);
    MLUCnnlActivationDesc act_desc(CNNL_ACTIVATION_HARDSWISH,
                                   1.0f /*ceof useless*/);
    MLUCnnl::ActiveGrad(ctx,
                        act_desc.get(),
                        nullptr,
                        nullptr,
                        nullptr,
                        nullptr,
                        dout_desc.get(),
                        GetBasePtr(dout),
                        out_desc.get(),
                        GetBasePtr(out),
                        dx_desc.get(),
                        GetBasePtr(dx));
  }
};

template <typename T>
class HardSigmoidMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<Tensor>("X");
    auto* output = ctx.Output<Tensor>("Out");
    float slope = ctx.Attr<float>("slope");
    float offset = ctx.Attr<float>("offset");
    output->mutable_data<T>(ctx.GetPlace());

    MLUCnnlActivationDesc act_desc(CNNL_ACTIVATION_HARDSIGMOID,
                                   1.0f /*ceof useless*/,
                                   1.0f /*sliced_dim useless*/,
                                   slope,
                                   offset);
    MLUCnnlTensorDesc input_desc(*input);
    MLUCnnlTensorDesc output_desc(*output);

    MLUCnnl::Active(ctx,
                    act_desc.get(),
                    input_desc.get(),
                    GetBasePtr(input),
                    output_desc.get(),
                    GetBasePtr(output));
  }
};

template <typename T>
class HardSigmoidGradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* out = ctx.Input<Tensor>("Out");
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    float slope = ctx.Attr<float>("slope");
    float offset = ctx.Attr<float>("offset");
    dx->mutable_data<T>(ctx.GetPlace());

    MLUCnnlActivationDesc act_desc(CNNL_ACTIVATION_HARDSIGMOID,
                                   1.0f /*ceof useless*/,
                                   1.0f /*sliced_dim useless*/,
                                   slope,
                                   offset);
    MLUCnnlTensorDesc out_desc(*out);
    MLUCnnlTensorDesc dout_desc(*dout);
    MLUCnnlTensorDesc dx_desc(*dx);
    MLUCnnl::ActiveGrad(ctx,
                        act_desc.get(),
                        nullptr,
                        nullptr,
                        nullptr,
                        nullptr,
                        dout_desc.get(),
                        GetBasePtr(dout),
                        out_desc.get(),
                        GetBasePtr(out),
                        dx_desc.get(),
                        GetBasePtr(dx));
  }
};

F
fwenguang 已提交
402 403 404 405 406
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

407
// relu
F
fwenguang 已提交
408
REGISTER_OP_MLU_KERNEL(
409 410
    relu,
    ops::ActivationMLUKernel<CNNL_ACTIVATION_RELU, float>,
411
    ops::ActivationMLUKernel<CNNL_ACTIVATION_RELU, paddle::platform::float16>);
F
fwenguang 已提交
412
REGISTER_OP_MLU_KERNEL(
413 414
    relu_grad,
    ops::ActivationGradMLUKernelV3<CNNL_ACTIVATION_RELU, float>,
415 416 417 418 419
    ops::ActivationGradMLUKernelV3<CNNL_ACTIVATION_RELU,
                                   paddle::platform::float16>);

// relu6
REGISTER_OP_MLU_KERNEL(
420 421
    relu6,
    ops::ActivationMLUKernel<CNNL_ACTIVATION_RELU6, float>,
422 423
    ops::ActivationMLUKernel<CNNL_ACTIVATION_RELU6, paddle::platform::float16>);
REGISTER_OP_MLU_KERNEL(
424 425
    relu6_grad,
    ops::ActivationGradMLUKernelV3<CNNL_ACTIVATION_RELU6, float>,
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    ops::ActivationGradMLUKernelV3<CNNL_ACTIVATION_RELU6,
                                   paddle::platform::float16>);

// sigmoid
REGISTER_OP_MLU_KERNEL(sigmoid,
                       ops::ActivationMLUKernel<CNNL_ACTIVATION_SIGMOID, float>,
                       ops::ActivationMLUKernel<CNNL_ACTIVATION_SIGMOID,
                                                paddle::platform::float16>);
REGISTER_OP_MLU_KERNEL(
    sigmoid_grad,
    ops::ActivationGradMLUKernelV2<CNNL_ACTIVATION_SIGMOID, float>,
    ops::ActivationGradMLUKernelV2<CNNL_ACTIVATION_SIGMOID,
                                   paddle::platform::float16>);

// tanh
REGISTER_OP_MLU_KERNEL(
442 443
    tanh,
    ops::ActivationMLUKernel<CNNL_ACTIVATION_TANH, float>,
444 445
    ops::ActivationMLUKernel<CNNL_ACTIVATION_TANH, paddle::platform::float16>);
REGISTER_OP_MLU_KERNEL(
446 447
    tanh_grad,
    ops::ActivationGradMLUKernelV2<CNNL_ACTIVATION_TANH, float>,
448 449 450 451 452
    ops::ActivationGradMLUKernelV2<CNNL_ACTIVATION_TANH,
                                   paddle::platform::float16>);

// gelu
REGISTER_OP_MLU_KERNEL(
453 454
    gelu,
    ops::ActivationMLUKernel<CNNL_ACTIVATION_GELU, float>,
455 456
    ops::ActivationMLUKernel<CNNL_ACTIVATION_GELU, paddle::platform::float16>);
REGISTER_OP_MLU_KERNEL(
457 458
    gelu_grad,
    ops::ActivationGradMLUKernelV1<CNNL_ACTIVATION_GELU, float>,
459 460 461 462 463
    ops::ActivationGradMLUKernelV1<CNNL_ACTIVATION_GELU,
                                   paddle::platform::float16>);

// leaky_relu
REGISTER_OP_MLU_KERNEL(
464 465
    leaky_relu,
    ops::ActivationMLUKernel<CNNL_ACTIVATION_LEAKYRELU, float>,
466 467 468 469 470 471 472
    ops::ActivationMLUKernel<CNNL_ACTIVATION_LEAKYRELU,
                             paddle::platform::float16>);
REGISTER_OP_MLU_KERNEL(
    leaky_relu_grad,
    ops::ActivationGradMLUKernelV1<CNNL_ACTIVATION_LEAKYRELU, float>,
    ops::ActivationGradMLUKernelV1<CNNL_ACTIVATION_LEAKYRELU,
                                   paddle::platform::float16>);
473 474

// sqrt
475 476
REGISTER_OP_MLU_KERNEL(sqrt,
                       ops::SqrtMLUKernel<float>,
477
                       ops::SqrtMLUKernel<paddle::platform::float16>);
478 479
REGISTER_OP_MLU_KERNEL(sqrt_grad,
                       ops::SqrtGradMLUKernel<float>,
480
                       ops::SqrtGradMLUKernel<paddle::platform::float16>);
481 482 483

// log log2 log10
REGISTER_OP_MLU_KERNEL(
484 485
    log,
    ops::LogMLUKernel<CNNL_LOG_E, float>,
486 487 488
    ops::LogMLUKernel<CNNL_LOG_E, paddle::platform::float16>);

REGISTER_OP_MLU_KERNEL(
489 490
    log2,
    ops::LogMLUKernel<CNNL_LOG_2, float>,
491 492 493
    ops::LogMLUKernel<CNNL_LOG_2, paddle::platform::float16>);

REGISTER_OP_MLU_KERNEL(
494 495
    log10,
    ops::LogMLUKernel<CNNL_LOG_10, float>,
496
    ops::LogMLUKernel<CNNL_LOG_10, paddle::platform::float16>);
497 498 499 500 501 502 503 504

REGISTER_OP_MLU_KERNEL(exp,
                       ops::ExpMLUKernel<float>,
                       ops::ExpMLUKernel<paddle::platform::float16>);

REGISTER_OP_MLU_KERNEL(exp_grad,
                       ops::ExpGradMLUKernel<float>,
                       ops::ExpGradMLUKernel<paddle::platform::float16>);
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

REGISTER_OP_MLU_KERNEL(hard_swish,
                       ops::HardSwishMLUKernel<float>,
                       ops::HardSwishMLUKernel<paddle::platform::float16>);

REGISTER_OP_MLU_KERNEL(hard_swish_grad,
                       ops::HardSwishGradMLUKernel<float>,
                       ops::HardSwishGradMLUKernel<paddle::platform::float16>);

REGISTER_OP_MLU_KERNEL(hard_sigmoid,
                       ops::HardSigmoidMLUKernel<float>,
                       ops::HardSigmoidMLUKernel<paddle::platform::float16>);

REGISTER_OP_MLU_KERNEL(
    hard_sigmoid_grad,
    ops::HardSigmoidGradMLUKernel<float>,
    ops::HardSigmoidGradMLUKernel<paddle::platform::float16>);