Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
TensorIO.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_IO_H
12#define EIGEN_TENSOR_TENSOR_IO_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19struct TensorIOFormat;
20
21namespace internal {
22template <typename Tensor, std::size_t rank, typename Format, typename EnableIf = void>
23struct TensorPrinter;
24}
25
26template <typename Derived_>
27struct TensorIOFormatBase {
28 using Derived = Derived_;
29 TensorIOFormatBase(const std::vector<std::string>& separator, const std::vector<std::string>& prefix,
30 const std::vector<std::string>& suffix, int precision = StreamPrecision, int flags = 0,
31 const std::string& tenPrefix = "", const std::string& tenSuffix = "", const char fill = ' ')
32 : tenPrefix(tenPrefix),
33 tenSuffix(tenSuffix),
34 prefix(prefix),
35 suffix(suffix),
36 separator(separator),
37 fill(fill),
38 precision(precision),
39 flags(flags) {
40 init_spacer();
41 }
42
43 void init_spacer() {
44 if (flags & DontAlignCols) return;
45 spacer.resize(prefix.size());
46 spacer[0] = "";
47 int i = int(tenPrefix.length()) - 1;
48 while (i >= 0 && tenPrefix[i] != '\n') {
49 spacer[0] += ' ';
50 i--;
51 }
52
53 for (std::size_t k = 1; k < prefix.size(); k++) {
54 int j = int(prefix[k].length()) - 1;
55 while (j >= 0 && prefix[k][j] != '\n') {
56 spacer[k] += ' ';
57 j--;
58 }
59 }
60 }
61
62 std::string tenPrefix;
63 std::string tenSuffix;
64 std::vector<std::string> prefix;
65 std::vector<std::string> suffix;
66 std::vector<std::string> separator;
67 char fill;
68 int precision;
69 int flags;
70 std::vector<std::string> spacer{};
71};
72
73struct TensorIOFormatNumpy : public TensorIOFormatBase<TensorIOFormatNumpy> {
74 using Base = TensorIOFormatBase<TensorIOFormatNumpy>;
75 TensorIOFormatNumpy()
76 : Base(/*separator=*/{" ", "\n"}, /*prefix=*/{"", "["}, /*suffix=*/{"", "]"}, /*precision=*/StreamPrecision,
77 /*flags=*/0, /*tenPrefix=*/"[", /*tenSuffix=*/"]") {}
78};
79
80struct TensorIOFormatNative : public TensorIOFormatBase<TensorIOFormatNative> {
81 using Base = TensorIOFormatBase<TensorIOFormatNative>;
82 TensorIOFormatNative()
83 : Base(/*separator=*/{", ", ",\n", "\n"}, /*prefix=*/{"", "{"}, /*suffix=*/{"", "}"},
84 /*precision=*/StreamPrecision, /*flags=*/0, /*tenPrefix=*/"{", /*tenSuffix=*/"}") {}
85};
86
87struct TensorIOFormatPlain : public TensorIOFormatBase<TensorIOFormatPlain> {
88 using Base = TensorIOFormatBase<TensorIOFormatPlain>;
89 TensorIOFormatPlain()
90 : Base(/*separator=*/{" ", "\n", "\n", ""}, /*prefix=*/{""}, /*suffix=*/{""}, /*precision=*/StreamPrecision,
91 /*flags=*/0, /*tenPrefix=*/"", /*tenSuffix=*/"") {}
92};
93
94struct TensorIOFormatLegacy : public TensorIOFormatBase<TensorIOFormatLegacy> {
95 using Base = TensorIOFormatBase<TensorIOFormatLegacy>;
96 TensorIOFormatLegacy()
97 : Base(/*separator=*/{", ", "\n"}, /*prefix=*/{"", "["}, /*suffix=*/{"", "]"}, /*precision=*/StreamPrecision,
98 /*flags=*/0, /*tenPrefix=*/"", /*tenSuffix=*/"") {}
99};
100
101struct TensorIOFormat : public TensorIOFormatBase<TensorIOFormat> {
102 using Base = TensorIOFormatBase<TensorIOFormat>;
103 TensorIOFormat(const std::vector<std::string>& separator, const std::vector<std::string>& prefix,
104 const std::vector<std::string>& suffix, int precision = StreamPrecision, int flags = 0,
105 const std::string& tenPrefix = "", const std::string& tenSuffix = "", const char fill = ' ')
106 : Base(separator, prefix, suffix, precision, flags, tenPrefix, tenSuffix, fill) {}
107
108 static inline const TensorIOFormatNumpy Numpy() { return TensorIOFormatNumpy{}; }
109
110 static inline const TensorIOFormatPlain Plain() { return TensorIOFormatPlain{}; }
111
112 static inline const TensorIOFormatNative Native() { return TensorIOFormatNative{}; }
113
114 static inline const TensorIOFormatLegacy Legacy() { return TensorIOFormatLegacy{}; }
115};
116
117template <typename T, int Layout, int rank, typename Format>
118class TensorWithFormat;
119// specialize for Layout=ColMajor, Layout=RowMajor and rank=0.
120template <typename T, int rank, typename Format>
121class TensorWithFormat<T, RowMajor, rank, Format> {
122 public:
123 TensorWithFormat(const T& tensor, const Format& format) : t_tensor(tensor), t_format(format) {}
124
125 friend std::ostream& operator<<(std::ostream& os, const TensorWithFormat<T, RowMajor, rank, Format>& wf) {
126 // Evaluate the expression if needed
127 using Evaluator = TensorEvaluator<const TensorForcedEvalOp<const T>, DefaultDevice>;
128 TensorForcedEvalOp<const T> eval = wf.t_tensor.eval();
129 Evaluator tensor(eval, DefaultDevice());
130 tensor.evalSubExprsIfNeeded(nullptr);
131 internal::TensorPrinter<Evaluator, rank, Format>::run(os, tensor, wf.t_format);
132 // Cleanup.
133 tensor.cleanup();
134 return os;
135 }
136
137 protected:
138 T t_tensor;
139 Format t_format;
140};
141
142template <typename T, int rank, typename Format>
143class TensorWithFormat<T, ColMajor, rank, Format> {
144 public:
145 TensorWithFormat(const T& tensor, const Format& format) : t_tensor(tensor), t_format(format) {}
146
147 friend std::ostream& operator<<(std::ostream& os, const TensorWithFormat<T, ColMajor, rank, Format>& wf) {
148 // Switch to RowMajor storage and print afterwards
149 using IndexType = typename T::Index;
150 std::array<IndexType, rank> shuffle;
151 std::array<IndexType, rank> id;
152 std::iota(id.begin(), id.end(), IndexType(0));
153 std::copy(id.begin(), id.end(), shuffle.rbegin());
154 auto tensor_row_major = wf.t_tensor.swap_layout().shuffle(shuffle);
155
156 // Evaluate the expression if needed
157 using Evaluator = TensorEvaluator<const TensorForcedEvalOp<const decltype(tensor_row_major)>, DefaultDevice>;
158 TensorForcedEvalOp<const decltype(tensor_row_major)> eval = tensor_row_major.eval();
159 Evaluator tensor(eval, DefaultDevice());
160 tensor.evalSubExprsIfNeeded(nullptr);
161 internal::TensorPrinter<Evaluator, rank, Format>::run(os, tensor, wf.t_format);
162 // Cleanup.
163 tensor.cleanup();
164 return os;
165 }
166
167 protected:
168 T t_tensor;
169 Format t_format;
170};
171
172template <typename T, typename Format>
173class TensorWithFormat<T, ColMajor, 0, Format> {
174 public:
175 TensorWithFormat(const T& tensor, const Format& format) : t_tensor(tensor), t_format(format) {}
176
177 friend std::ostream& operator<<(std::ostream& os, const TensorWithFormat<T, ColMajor, 0, Format>& wf) {
178 // Evaluate the expression if needed
179 using Evaluator = TensorEvaluator<const TensorForcedEvalOp<const T>, DefaultDevice>;
180 TensorForcedEvalOp<const T> eval = wf.t_tensor.eval();
181 Evaluator tensor(eval, DefaultDevice());
182 tensor.evalSubExprsIfNeeded(nullptr);
183 internal::TensorPrinter<Evaluator, 0, Format>::run(os, tensor, wf.t_format);
184 // Cleanup.
185 tensor.cleanup();
186 return os;
187 }
188
189 protected:
190 T t_tensor;
191 Format t_format;
192};
193
194namespace internal {
195
196// Default scalar printer.
197template <typename Scalar, typename Format, typename EnableIf = void>
198struct ScalarPrinter {
199 static void run(std::ostream& stream, const Scalar& scalar, const Format&) { stream << scalar; }
200};
201
202template <typename Scalar>
203struct ScalarPrinter<Scalar, TensorIOFormatNumpy, std::enable_if_t<NumTraits<Scalar>::IsComplex>> {
204 static void run(std::ostream& stream, const Scalar& scalar, const TensorIOFormatNumpy&) {
205 stream << numext::real(scalar) << "+" << numext::imag(scalar) << "j";
206 }
207};
208
209template <typename Scalar>
210struct ScalarPrinter<Scalar, TensorIOFormatNative, std::enable_if_t<NumTraits<Scalar>::IsComplex>> {
211 static void run(std::ostream& stream, const Scalar& scalar, const TensorIOFormatNative&) {
212 stream << "{" << numext::real(scalar) << ", " << numext::imag(scalar) << "}";
213 }
214};
215
216template <typename Tensor, std::size_t rank, typename Format, typename EnableIf>
217struct TensorPrinter {
218 using Scalar = std::remove_const_t<typename Tensor::Scalar>;
219
220 static void run(std::ostream& s, const Tensor& tensor, const Format& fmt) {
221 using IndexType = typename Tensor::Index;
222
223 eigen_assert(Tensor::Layout == RowMajor);
224 using PrintType =
225 std::conditional_t<std::is_same<Scalar, char>::value || std::is_same<Scalar, unsigned char>::value ||
226 std::is_same<Scalar, numext::int8_t>::value ||
227 std::is_same<Scalar, numext::uint8_t>::value,
228 int,
229 std::conditional_t<std::is_same<Scalar, std::complex<char>>::value ||
230 std::is_same<Scalar, std::complex<unsigned char>>::value ||
231 std::is_same<Scalar, std::complex<numext::int8_t>>::value ||
232 std::is_same<Scalar, std::complex<numext::uint8_t>>::value,
233 std::complex<int>, const Scalar&>>;
234 using PrintScalar = std::remove_const_t<std::remove_reference_t<PrintType>>;
235
236 const IndexType total_size = array_prod(tensor.dimensions());
237
238 std::streamsize explicit_precision;
239 if (fmt.precision == StreamPrecision) {
240 explicit_precision = 0;
241 } else if (fmt.precision == FullPrecision) {
242 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsInteger) {
243 explicit_precision = 0;
244 } else {
245 explicit_precision = significant_decimals_impl<Scalar>::run();
246 }
247 } else {
248 explicit_precision = fmt.precision;
249 }
250
251 std::streamsize old_precision = 0;
252 if (explicit_precision) old_precision = s.precision(explicit_precision);
253
254 IndexType width = 0;
255 bool align_cols = !(fmt.flags & DontAlignCols);
256 if (align_cols) {
257 // compute the largest width
258 for (IndexType i = 0; i < total_size; i++) {
259 std::stringstream sstr;
260 sstr.copyfmt(s);
261 ScalarPrinter<PrintScalar, Format>::run(sstr, static_cast<PrintType>(tensor.data()[i]), fmt);
262 width = std::max<IndexType>(width, IndexType(sstr.str().length()));
263 }
264 }
265 s << fmt.tenPrefix;
266 for (IndexType i = 0; i < total_size; i++) {
267 std::array<bool, rank> is_at_end{};
268 std::array<bool, rank> is_at_begin{};
269
270 // is the ith element the end of a coeff (always true), of a row, of a matrix, ...?
271 for (std::size_t k = 0; k < rank; k++) {
272 if ((i + 1) % (std::accumulate(tensor.dimensions().rbegin(), tensor.dimensions().rbegin() + k, 1,
273 std::multiplies<IndexType>())) ==
274 0) {
275 is_at_end[k] = true;
276 }
277 }
278
279 // is the ith element the begin of a coeff (always true), of a row, of a matrix, ...?
280 for (std::size_t k = 0; k < rank; k++) {
281 if (i % (std::accumulate(tensor.dimensions().rbegin(), tensor.dimensions().rbegin() + k, 1,
282 std::multiplies<IndexType>())) ==
283 0) {
284 is_at_begin[k] = true;
285 }
286 }
287
288 // do we have a line break?
289 bool is_at_begin_after_newline = false;
290 for (std::size_t k = 0; k < rank; k++) {
291 if (is_at_begin[k]) {
292 std::size_t separator_index = (k < fmt.separator.size()) ? k : fmt.separator.size() - 1;
293 if (fmt.separator[separator_index].find('\n') != std::string::npos) {
294 is_at_begin_after_newline = true;
295 }
296 }
297 }
298
299 bool is_at_end_before_newline = false;
300 for (std::size_t k = 0; k < rank; k++) {
301 if (is_at_end[k]) {
302 std::size_t separator_index = (k < fmt.separator.size()) ? k : fmt.separator.size() - 1;
303 if (fmt.separator[separator_index].find('\n') != std::string::npos) {
304 is_at_end_before_newline = true;
305 }
306 }
307 }
308
309 std::stringstream suffix, prefix, separator;
310 for (std::size_t k = 0; k < rank; k++) {
311 std::size_t suffix_index = (k < fmt.suffix.size()) ? k : fmt.suffix.size() - 1;
312 if (is_at_end[k]) {
313 suffix << fmt.suffix[suffix_index];
314 }
315 }
316 for (std::size_t k = 0; k < rank; k++) {
317 std::size_t separator_index = (k < fmt.separator.size()) ? k : fmt.separator.size() - 1;
318 if (is_at_end[k] &&
319 (!is_at_end_before_newline || fmt.separator[separator_index].find('\n') != std::string::npos)) {
320 separator << fmt.separator[separator_index];
321 }
322 }
323 for (std::size_t k = 0; k < rank; k++) {
324 std::size_t spacer_index = (k < fmt.spacer.size()) ? k : fmt.spacer.size() - 1;
325 if (i != 0 && is_at_begin_after_newline && (!is_at_begin[k] || k == 0)) {
326 prefix << fmt.spacer[spacer_index];
327 }
328 }
329 for (int k = rank - 1; k >= 0; k--) {
330 std::size_t prefix_index = (static_cast<std::size_t>(k) < fmt.prefix.size()) ? k : fmt.prefix.size() - 1;
331 if (is_at_begin[k]) {
332 prefix << fmt.prefix[prefix_index];
333 }
334 }
335
336 s << prefix.str();
337 // So we don't mess around with formatting, output scalar to a string stream, and adjust the width/fill manually.
338 std::stringstream sstr;
339 sstr.copyfmt(s);
340 ScalarPrinter<PrintScalar, Format>::run(sstr, static_cast<PrintType>(tensor.data()[i]), fmt);
341 std::string scalar_str = sstr.str();
342 IndexType scalar_width = scalar_str.length();
343 if (width && scalar_width < width) {
344 std::string filler;
345 for (IndexType j = scalar_width; j < width; ++j) {
346 filler.push_back(fmt.fill);
347 }
348 s << filler;
349 }
350 s << scalar_str;
351 s << suffix.str();
352 if (i < total_size - 1) {
353 s << separator.str();
354 }
355 }
356 s << fmt.tenSuffix;
357 if (explicit_precision) s.precision(old_precision);
358 }
359};
360
361template <typename Tensor, std::size_t rank>
362struct TensorPrinter<Tensor, rank, TensorIOFormatLegacy, std::enable_if_t<rank != 0>> {
363 using Format = TensorIOFormatLegacy;
364 using Scalar = std::remove_const_t<typename Tensor::Scalar>;
365
366 static void run(std::ostream& s, const Tensor& tensor, const Format&) {
367 using IndexType = typename Tensor::Index;
368 // backwards compatibility case: print tensor after reshaping to matrix of size dim(0) x
369 // (dim(1)*dim(2)*...*dim(rank-1)).
370 const IndexType total_size = internal::array_prod(tensor.dimensions());
371 if (total_size > 0) {
372 const IndexType first_dim = Eigen::internal::array_get<0>(tensor.dimensions());
373 Map<const Array<Scalar, Dynamic, Dynamic, Tensor::Layout>> matrix(tensor.data(), first_dim,
374 total_size / first_dim);
375 s << matrix;
376 return;
377 }
378 }
379};
380
381template <typename Tensor, typename Format>
382struct TensorPrinter<Tensor, 0, Format> {
383 static void run(std::ostream& s, const Tensor& tensor, const Format& fmt) {
384 using Scalar = std::remove_const_t<typename Tensor::Scalar>;
385
386 std::streamsize explicit_precision;
387 if (fmt.precision == StreamPrecision) {
388 explicit_precision = 0;
389 } else if (fmt.precision == FullPrecision) {
390 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsInteger) {
391 explicit_precision = 0;
392 } else {
393 explicit_precision = significant_decimals_impl<Scalar>::run();
394 }
395 } else {
396 explicit_precision = fmt.precision;
397 }
398
399 std::streamsize old_precision = 0;
400 if (explicit_precision) old_precision = s.precision(explicit_precision);
401 s << fmt.tenPrefix;
402 ScalarPrinter<Scalar, Format>::run(s, tensor.coeff(0), fmt);
403 s << fmt.tenSuffix;
404 if (explicit_precision) s.precision(old_precision);
405 }
406};
407
408} // end namespace internal
409template <typename T>
410std::ostream& operator<<(std::ostream& s, const TensorBase<T, ReadOnlyAccessors>& t) {
411 s << t.format(TensorIOFormat::Plain());
412 return s;
413}
414} // end namespace Eigen
415
416#endif // EIGEN_TENSOR_TENSOR_IO_H
The tensor base class.
Definition TensorForwardDeclarations.h:69
Namespace containing all symbols from the Eigen library.