diag_functor.h 3.9 KB
Newer Older
L
Linjie Chen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2022 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

17 18 19 20 21 22 23 24
#include "paddle/phi/common/type_traits.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/hostdevice.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/for_range.h"

// TODO(paddle-dev): Remove this file when we can call related Kernel directly

L
Linjie Chen 已提交
25 26 27 28 29 30 31 32 33 34 35
namespace phi {
namespace funcs {

inline int ComputeStride(int axis, phi::DDim dims) {
  int size = 1;
  for (int i = axis + 1; i < dims.size(); i++) {
    size *= dims[i];
  }
  return size;
}

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
template <typename T, typename ValueType>
struct DiagAndFillFunctor {
  DiagAndFillFunctor(const int m,
                     const int n,
                     const int num_lower_diags,
                     const int num_upper_diags,
                     const ValueType* scale,
                     const T* input,
                     T* output)
      : m_(m),
        n_(n),
        num_lower_diags_(num_lower_diags),
        num_upper_diags_(num_upper_diags),
        scale_(scale),
        input_(input),
        output_(output) {}

  HOSTDEVICE void operator()(size_t index) const {
    const int col = index % n_;
    const int row = (index / n_) % m_;
    const int band_start = (num_lower_diags_ < 0 ? 0 : row - num_lower_diags_);
    const int band_end =
        (num_upper_diags_ < 0 ? n_ : row + num_upper_diags_ + 1);
    if (col < band_start || col >= band_end) {
      output_[index] = input_[index];
    } else if (col == band_end - 1) {
      output_[index] = static_cast<T>(scale_[index % m_]);
    } else {
      output_[index] = input_[index];
    }
  }

 private:
  const int m_, n_, num_lower_diags_, num_upper_diags_;
  const ValueType* scale_;
  const T* input_;
  T* output_;
};

template <typename T, typename ValueType, typename Context>
DenseTensor DiagFill(const Context& dev_ctx,
                     const int m,
                     const int n,
                     const int num_lower_diags,
                     const int num_upper_diags,
                     const DenseTensor& scale,
                     const DenseTensor& input) {
  DenseTensor out;
  out.Resize(input.dims());
  dev_ctx.template Alloc<T>(&out);
  funcs::ForRange<Context> for_range(dev_ctx, input.numel());
  DiagAndFillFunctor<T, ValueType> diag_and_copy_functor(
      m,
      n,
      num_lower_diags,
      num_upper_diags,
      scale.data<ValueType>(),
      input.data<T>(),
      out.data<T>());
  for_range(diag_and_copy_functor);
  return out;
}

template <typename T, typename Context>
DenseTensor BatchDiag(const Context& dev_ctx, const DenseTensor& x, int batch) {
  DenseTensor out;
  auto* x_data = x.data<phi::dtype::Real<T>>();
  auto numel = x.numel();
  out.Resize(x.dims());
  auto* out_data = dev_ctx.template HostAlloc<phi::dtype::Real<T>>(
      &out, static_cast<size_t>(numel * sizeof(phi::dtype::Real<T>)));

  auto x_dims = x.dims();
  int num_dims = x_dims.size();
  std::vector<int> out_shape;

  for (int i = 0; i < num_dims - 1; ++i) {
    out_shape.push_back(x.dims()[i]);
  }
  out.Resize(phi::make_ddim(out_shape));
  int order = x.dims()[num_dims - 1];
  int stride_out = order * order;
  int stride_in = order + 1;
  for (int i = 0; i < batch; ++i) {
    for (int j = 0; j < order; ++j) {
      out_data[i * order + j] = x_data[stride_out * i + stride_in * j];
    }
  }
  return out;
}

L
Linjie Chen 已提交
127 128
}  // namespace funcs
}  // namespace phi