1#ifndef ATEN_CORE_TENSOR_UTILS_H_
2#define ATEN_CORE_TENSOR_UTILS_H_
23 int i =
static_cast<int>(str.length()) - 1;
24 while (i >= 0 && str[i] ==
'0') {
30 return str.substr(0, i + 1);
60 for (
int i = 0; i < size; i++) {
63 digits = log10(-arr[i]) + 1;
64 if (digits + 1 > integer_count) {
65 integer_count = digits + 1;
69 digits = log10(arr[i]) + 1;
70 if (digits > integer_count) {
71 integer_count = digits;
75 T fraction = arr[i] - std::floor(arr[i]);
80 digits = str.length() - str.find(
'.');
81 if (digits > fraction_count) {
82 fraction_count = digits;
86 return integer_count + fraction_count;
107 const std::complex<T>* arr,
112 return _get_digit_places<T>(
reinterpret_cast<const T*
>(arr), size * 2, integer_count, fraction_count);
131 const int& digit_width,
132 const int& fraction_count)
134 os << std::setw(digit_width) \
135 << std::setprecision(fraction_count) << std::fixed << data;
153 const std::complex<T> data,
154 const int& digit_width,
155 const int& fraction_count)
160 os << std::setw(digit_width) \
161 << std::setprecision(fraction_count) << std::fixed
164 os << std::setw(digit_width) \
165 << std::setprecision(fraction_count) << std::fixed
184 const int& digit_width,
185 const int& fraction_count)
187 os << std::setw(digit_width - 1) \
188 << std::setprecision(fraction_count) << std::fixed << data;
211 const int64_t& num_elements)
213 int integer_count = 0, fraction_count = 0;
214 int digit_width =
_get_digit_places(data, num_elements, integer_count, fraction_count) + 1;
215 if (shape.
ndim() == 1) {
217 for (
int i = 0; i < num_elements; ++i) {
219 if (i != num_elements - 1) {
225 else if (shape.
ndim() == 2) {
227 for (
int i = 0; i < shape.
dim_size(0); ++i) {
228 if (i != 0) os <<
" ";
230 for (
int j = 0; j < shape.
dim_size(1); ++j) {
237 if (i != shape.
dim_size(0) - 1) os <<
",\n";
241 else if (shape.
ndim() == 3) {
243 for (
int i = 0; i < shape.
dim_size(0); ++i) {
244 if (i != 0) os <<
" ";
246 for (
int j = 0; j < shape.
dim_size(1); ++j) {
247 if (j != 0) os <<
" ";
249 for (
int k = 0; k < shape.
dim_size(2); ++k) {
256 if (j != shape.
dim_size(1) - 1) os <<
",\n";
259 if (i != shape.
dim_size(0) - 1) os <<
",\n\n";
264 for (int64_t i = 0; i < num_elements; ++i) {
266 if (i < num_elements - 1) {
276 return reinterpret_cast<T*
>(tensor.
data())[0];
282 &result,
reinterpret_cast<T*
>(tensor.
data()), 1))
A class for representing the shape of a tensor.
Definition tensor_shape.h:13
int64_t dim_size(int dim) const
Get the size of a dimension in the tensor.
Definition tensor_shape.cpp:31
unsigned int ndim() const
Get the ndim of the tensor.
Definition tensor_shape.cpp:46
A multi-dimensional array of elements of a single data type.
Definition tensor.h:32
void * data() const
Get a pointer to the data buffer of the tensor.
Definition tensor.cpp:73
DeviceType device_type() const
Get the data type of the tensor.
Definition tensor.cpp:64
DataType data_type() const
Get the data type of the tensor.
Definition tensor.cpp:61
#define T
Definition exp.cpp:237
#define TEMPLATE_ALL_2(TYPE_ENUM, DEVICE_ENUM,...)
Definition macros.h:221
T extract(const container::Tensor &tensor)
Definition tensor_utils.h:274
@ CpuDevice
Definition tensor_enums.h:30
__inline__ void _internal_output(std::ostream &os, const T *data, const TensorShape &shape, const int64_t &num_elements)
Outputs tensor data to a given output stream.
Definition tensor_utils.h:207
__inline__ std::string removeTrailingZeros(std::string str)
Removes trailing zeros from a string.
Definition tensor_utils.h:22
__inline__ void _output_wrapper(std::ostream &os, const T data, const int &digit_width, const int &fraction_count)
Output wrapper for a data value with given formatting parameters.
Definition tensor_utils.h:128
__inline__ int _get_digit_places(const T *arr, int size, int &integer_count, int &fraction_count)
Calculates the length of the longest integer and fractional part of an array of numbers.
Definition tensor_utils.h:51
Synchronizes memory between devices.
Definition memory.h:58