forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor_formatter.cc
More file actions
187 lines (159 loc) · 6.72 KB
/
tensor_formatter.cc
File metadata and controls
187 lines (159 loc) · 6.72 KB
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
26
27
28
29
30
31
32
33
34
35
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* 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. */
#include "paddle/phi/kernels/funcs/tensor_formatter.h"
#include <string>
#include "paddle/phi/backends/context_pool.h"
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/tensor_utils.h"
namespace phi::funcs {
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;
}
void TensorFormatter::Print(const phi::DenseTensor& print_tensor,
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);
}
std::string TensorFormatter::Format(const phi::DenseTensor& print_tensor,
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: {";
const phi::LegacyLoD& lod = print_tensor.lod();
for (auto const& 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_) {
log_stream << " - layout: " << print_tensor.layout() << std::endl;
}
auto dtype = print_tensor.dtype();
if (print_tensor_type_) {
log_stream << " - dtype: " << dtype << std::endl;
}
if (dtype == phi::DataType::FLOAT32) {
FormatData<float>(print_tensor, log_stream);
} else if (dtype == phi::DataType::FLOAT64) {
FormatData<double>(print_tensor, log_stream);
} else if (dtype == phi::DataType::INT32) {
FormatData<int>(print_tensor, log_stream);
} else if (dtype == phi::DataType::INT64) {
FormatData<int64_t>(print_tensor, log_stream);
} else if (dtype == phi::DataType::BOOL) {
FormatData<bool>(print_tensor, log_stream);
} else if (dtype == phi::DataType::FLOAT16) {
FormatData<phi::dtype::float16>(print_tensor, log_stream);
} else if (dtype == phi::DataType::BFLOAT16) {
FormatData<phi::dtype::bfloat16>(print_tensor, log_stream);
} else if (dtype == phi::DataType::FLOAT8_E4M3FN) {
FormatData<phi::dtype::float8_e4m3fn>(print_tensor, log_stream);
} else if (dtype == phi::DataType::FLOAT8_E5M2) {
FormatData<phi::dtype::float8_e5m2>(print_tensor, log_stream);
} else if (dtype == phi::DataType::COMPLEX64) {
FormatData<phi::dtype::complex<float>>(print_tensor, log_stream);
} else if (dtype == phi::DataType::COMPLEX128) {
FormatData<phi::dtype::complex<double>>(print_tensor, log_stream);
} else {
log_stream << " - data: unprintable type: " << dtype << std::endl;
}
return log_stream.str();
}
template <typename T>
void TensorFormatter::FormatData(const phi::DenseTensor& print_tensor,
std::stringstream& log_stream) {
int64_t print_size = summarize_ == -1
? print_tensor.numel()
: std::min(summarize_, print_tensor.numel());
const T* data = nullptr;
phi::DenseTensor cpu_tensor;
if (print_tensor.place().GetType() == phi::AllocationType::CPU) {
data = print_tensor.data<T>();
} else {
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);
data = cpu_tensor.data<T>();
}
log_stream << " - data: [";
if (print_size > 0) {
auto print_element = [&log_stream](const auto& elem) {
if constexpr (std::is_same_v<T, phi::dtype::complex<float>> ||
std::is_same_v<T, phi::dtype::complex<double>>) {
log_stream << static_cast<float>(elem.real) << "+"
<< static_cast<float>(elem.imag) << "j";
} else {
log_stream << static_cast<float>(elem);
}
};
print_element(data[0]);
for (int64_t i = 1; i < print_size; ++i) {
log_stream << " ";
print_element(data[i]);
}
}
log_stream << "]" << std::endl;
}
template void TensorFormatter::FormatData<bool>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<float>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<double>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<int>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<int64_t>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<phi::dtype::float16>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<phi::dtype::bfloat16>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<phi::dtype::complex<float>>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<phi::dtype::complex<double>>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
} // namespace phi::funcs