tensor_formatter.cc 5.1 KB
Newer Older
H
Huihuang Zheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

H
hong 已提交
15
#include "paddle/phi/kernels/funcs/tensor_formatter.h"
16

H
Huihuang Zheng 已提交
17
#include <string>
18

H
hong 已提交
19 20 21
#include "paddle/phi/backends/context_pool.h"
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/tensor_utils.h"
H
Huihuang Zheng 已提交
22 23

namespace paddle {
H
hong 已提交
24
namespace funcs {
H
Huihuang Zheng 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

void TensorFormatter::SetPrintTensorType(bool print_tensor_type) {
  print_tensor_type_ = print_tensor_type;
}

void TensorFormatter::SetPrintTensorShape(bool print_tensor_shape) {
  print_tensor_shape_ = print_tensor_shape;
}

void TensorFormatter::SetPrintTensorLod(bool print_tensor_lod) {
  print_tensor_lod_ = print_tensor_lod;
}

void TensorFormatter::SetPrintTensorLayout(bool print_tensor_layout) {
  print_tensor_layout_ = print_tensor_layout;
}

void TensorFormatter::SetSummarize(int64_t summarize) {
  summarize_ = summarize;
}

46
void TensorFormatter::Print(const phi::DenseTensor& print_tensor,
H
Huihuang Zheng 已提交
47 48 49 50 51 52 53
                            const std::string& tensor_name,
                            const std::string& message) {
  static std::mutex mutex;
  std::lock_guard<std::mutex> lock(mutex);
  std::cout << Format(print_tensor, tensor_name, message);
}

54
std::string TensorFormatter::Format(const phi::DenseTensor& print_tensor,
H
Huihuang Zheng 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67
                                    const std::string& tensor_name,
                                    const std::string& message) {
  std::stringstream log_stream;
  if (!tensor_name.empty()) {
    log_stream << "Variable: " << tensor_name << std::endl;
  }

  if (!message.empty()) {
    log_stream << "  - message: " << message << std::endl;
  }

  if (print_tensor_lod_) {
    log_stream << "  - lod: {";
H
hong 已提交
68
    const phi::LoD& lod = print_tensor.lod();
H
Huihuang Zheng 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    for (auto level : lod) {
      log_stream << "{";
      bool is_first = true;
      for (auto i : level) {
        if (is_first) {
          log_stream << i;
          is_first = false;
        } else {
          log_stream << ", " << i;
        }
      }
      log_stream << "}";
    }
    log_stream << "}" << std::endl;
  }

  log_stream << "  - place: " << print_tensor.place() << std::endl;

  if (print_tensor_shape_) {
    log_stream << "  - shape: " << print_tensor.dims().to_str() << std::endl;
  }

  if (print_tensor_layout_) {
H
hong 已提交
92
    log_stream << "  - layout: " << print_tensor.layout() << std::endl;
H
Huihuang Zheng 已提交
93 94
  }

H
hong 已提交
95
  auto dtype = print_tensor.dtype();
H
Huihuang Zheng 已提交
96
  if (print_tensor_type_) {
H
hong 已提交
97
    log_stream << "  - dtype: " << dtype << std::endl;
H
Huihuang Zheng 已提交
98 99
  }

H
hong 已提交
100
  if (dtype == phi::DataType::FLOAT32) {
H
Huihuang Zheng 已提交
101
    FormatData<float>(print_tensor, log_stream);
H
hong 已提交
102
  } else if (dtype == phi::DataType::FLOAT64) {
H
Huihuang Zheng 已提交
103
    FormatData<double>(print_tensor, log_stream);
H
hong 已提交
104
  } else if (dtype == phi::DataType::INT32) {
H
Huihuang Zheng 已提交
105
    FormatData<int>(print_tensor, log_stream);
H
hong 已提交
106
  } else if (dtype == phi::DataType::INT64) {
H
Huihuang Zheng 已提交
107
    FormatData<int64_t>(print_tensor, log_stream);
H
hong 已提交
108
  } else if (dtype == phi::DataType::BOOL) {
H
Huihuang Zheng 已提交
109 110
    FormatData<bool>(print_tensor, log_stream);
  } else {
H
hong 已提交
111
    log_stream << "  - data: unprintable type: " << dtype << std::endl;
H
Huihuang Zheng 已提交
112 113 114 115 116
  }
  return log_stream.str();
}

template <typename T>
117
void TensorFormatter::FormatData(const phi::DenseTensor& print_tensor,
H
Huihuang Zheng 已提交
118 119 120 121 122
                                 std::stringstream& log_stream) {
  int64_t print_size = summarize_ == -1
                           ? print_tensor.numel()
                           : std::min(summarize_, print_tensor.numel());
  const T* data = nullptr;
123
  phi::DenseTensor cpu_tensor;
H
hong 已提交
124
  if (print_tensor.place().GetType() == phi::AllocationType::CPU) {
H
Huihuang Zheng 已提交
125 126
    data = print_tensor.data<T>();
  } else {
H
hong 已提交
127 128 129 130 131 132
    phi::CPUPlace cpu_place;

    phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
    auto dev_ctx = pool.Get(print_tensor.place());

    phi::Copy(*dev_ctx, print_tensor, cpu_place, true, &cpu_tensor);
H
Huihuang Zheng 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146
    data = cpu_tensor.data<T>();
  }

  log_stream << "  - data: [";
  if (print_size > 0) {
    log_stream << data[0];
    for (int64_t i = 1; i < print_size; ++i) {
      log_stream << " " << data[i];
    }
  }
  log_stream << "]" << std::endl;
}

template void TensorFormatter::FormatData<bool>(
147
    const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
H
Huihuang Zheng 已提交
148
template void TensorFormatter::FormatData<float>(
149
    const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
H
Huihuang Zheng 已提交
150
template void TensorFormatter::FormatData<double>(
151
    const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
H
Huihuang Zheng 已提交
152
template void TensorFormatter::FormatData<int>(
153
    const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
H
Huihuang Zheng 已提交
154
template void TensorFormatter::FormatData<int64_t>(
155
    const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
H
Huihuang Zheng 已提交
156

H
hong 已提交
157
}  // namespace funcs
H
Huihuang Zheng 已提交
158
}  // namespace paddle