1#ifndef ATEN_CORE_TENSOR_UTILS_H_
2#define ATEN_CORE_TENSOR_UTILS_H_
21 int i =
static_cast<int>(str.length()) - 1;
22 while (i >= 0 && str[i] ==
'0') {
28 return str.substr(0, i + 1);
58 for (
int i = 0; i < size; i++) {
61 digits = log10(-arr[i]) + 1;
62 if (digits + 1 > integer_count) {
63 integer_count = digits + 1;
67 digits = log10(arr[i]) + 1;
68 if (digits > integer_count) {
69 integer_count = digits;
73 T fraction = arr[i] - std::floor(arr[i]);
78 digits = str.length() - str.find(
'.');
79 if (digits > fraction_count) {
80 fraction_count = digits;
84 return integer_count + fraction_count;
105 const std::complex<T>* arr,
110 return _get_digit_places<T>(
reinterpret_cast<const T*
>(arr), size * 2, integer_count, fraction_count);
129 const int& digit_width,
130 const int& fraction_count)
132 os << std::setw(digit_width) \
133 << std::setprecision(fraction_count) << std::fixed << data;
151 const std::complex<T> data,
152 const int& digit_width,
153 const int& fraction_count)
158 os << std::setw(digit_width) \
159 << std::setprecision(fraction_count) << std::fixed
162 os << std::setw(digit_width) \
163 << std::setprecision(fraction_count) << std::fixed
182 const int& digit_width,
183 const int& fraction_count)
185 os << std::setw(digit_width - 1) \
186 << std::setprecision(fraction_count) << std::fixed << data;
209 const int64_t& num_elements)
211 int integer_count = 0, fraction_count = 0;
212 int digit_width =
_get_digit_places(data, num_elements, integer_count, fraction_count) + 1;
213 if (shape.
ndim() == 1) {
215 for (
int i = 0; i < num_elements; ++i) {
217 if (i != num_elements - 1) {
223 else if (shape.
ndim() == 2) {
225 for (
int i = 0; i < shape.
dim_size(0); ++i) {
226 if (i != 0) os <<
" ";
228 for (
int j = 0; j < shape.
dim_size(1); ++j) {
235 if (i != shape.
dim_size(0) - 1) os <<
",\n";
239 else if (shape.
ndim() == 3) {
241 for (
int i = 0; i < shape.
dim_size(0); ++i) {
242 if (i != 0) os <<
" ";
244 for (
int j = 0; j < shape.
dim_size(1); ++j) {
245 if (j != 0) os <<
" ";
247 for (
int k = 0; k < shape.
dim_size(2); ++k) {
254 if (j != shape.
dim_size(1) - 1) os <<
",\n";
257 if (i != shape.
dim_size(0) - 1) os <<
",\n\n";
262 for (int64_t i = 0; i < num_elements; ++i) {
264 if (i < num_elements - 1) {
274 return reinterpret_cast<T*
>(tensor.
data())[0];
280 &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:214
T extract(const container::Tensor &tensor)
Definition tensor_utils.h:272
@ CpuDevice
Memory type is CPU.
__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:205
__inline__ std::string removeTrailingZeros(std::string str)
Removes trailing zeros from a string.
Definition tensor_utils.h:20
__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:126
__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:49
Synchronizes memory between devices.
Definition memory.h:58