“d8f95214df1f7c2994a196f9f7777a0b2d8df8eb”上不存在“projects/Core00077/imports.yml”
l1_norm.cc 2.0 KB
Newer Older
1
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13

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. */
14
#include "paddle/pten/kernels/funcs/eigen/eigen_function.h"
15

16 17
namespace pten {
namespace funcs {
18 19 20 21 22

template <typename T>
struct EigenL1Norm<Eigen::DefaultDevice, T> {
  using InType = Eigen::TensorMap<
      Eigen::Tensor<const T, 1, Eigen::RowMajor, Eigen::DenseIndex>>;
23 24 25 26 27 28
  using OutType = Eigen::TensorMap<Eigen::TensorFixedSize<T,
                                                          Eigen::Sizes<>,
                                                          Eigen::RowMajor,
                                                          Eigen::DenseIndex>>;
  static void Eval(const Eigen::DefaultDevice& dev,
                   OutType out,
29 30 31 32 33 34 35 36 37 38 39 40
                   const InType& in) {
    out.device(dev) = in.abs().sum();
  }
};

template <typename T>
struct EigenL1NormGrad<Eigen::DefaultDevice, T> {
  using Array = Eigen::DSizes<Eigen::DenseIndex, 1>;
  using InType = Eigen::TensorMap<
      Eigen::Tensor<const T, 1, Eigen::RowMajor, Eigen::DenseIndex>>;
  using OutType =
      Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>>;
41 42 43 44 45
  static void Eval(const Eigen::DefaultDevice& dev,
                   OutType din,
                   const InType& dout,
                   const InType& in,
                   const Array& bcast) {
46 47 48 49 50 51 52
    din.device(dev) = dout.broadcast(bcast) * in.sign();
  }
};

template struct EigenL1Norm<Eigen::DefaultDevice, float>;
template struct EigenL1NormGrad<Eigen::DefaultDevice, float>;

53 54
}  // namespace funcs
}  // namespace pten