blas.cc 2.0 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//   Copyright (c) 2018 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.

#include "paddle/fluid/operators/math/blas.h"
Y
Yu Yang 已提交
16 17

#include <utility>
Y
Yu Yang 已提交
18 19 20
namespace paddle {
namespace operators {
namespace math {
Y
Yu Yang 已提交
21 22
MatDescriptor CreateMatrixDescriptor(const framework::DDim &tensor_dim,
                                     int num_flatten_cols, bool trans) {
23 24 25 26 27
  PADDLE_ENFORCE_GT(
      tensor_dim.size(), 1,
      platform::errors::InvalidArgument("The tensor dim size should be greater "
                                        "than 1, but reveived dim size is %d",
                                        tensor_dim.size()));
Y
Yu Yang 已提交
28
  MatDescriptor retv;
Y
Yu Yang 已提交
29
  if (num_flatten_cols > 1) {
Y
Yu Yang 已提交
30
    auto flatten_dim = framework::flatten_to_2d(tensor_dim, num_flatten_cols);
Y
Yu Yang 已提交
31 32 33
    retv.height_ = flatten_dim[0];
    retv.width_ = flatten_dim[1];
  } else {
Y
Yu Yang 已提交
34 35 36
    if (tensor_dim.size() == 2) {
      retv.height_ = tensor_dim[0];
      retv.width_ = tensor_dim[1];
Y
Yu Yang 已提交
37
    } else {
Y
Yu Yang 已提交
38 39 40 41
      auto dim_vec = framework::vectorize(tensor_dim);
      retv.batch_size_ = 1;
      for (size_t i = 0; i < dim_vec.size() - 2; ++i) {
        retv.batch_size_ *= dim_vec[i];
Y
Yu Yang 已提交
42
      }
Y
Yu Yang 已提交
43 44
      retv.height_ = dim_vec[dim_vec.size() - 2];
      retv.width_ = dim_vec[dim_vec.size() - 1];
Y
Yu Yang 已提交
45 46 47 48 49 50 51 52 53
      retv.stride_ = retv.height_ * retv.width_;
    }
  }
  if (trans) {
    std::swap(retv.width_, retv.height_);
  }
  retv.trans_ = trans;
  return retv;
}
Y
Yu Yang 已提交
54 55 56
}  // namespace math
}  // namespace operators
}  // namespace paddle