ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
tensor_utils.h
Go to the documentation of this file.
1#ifndef ATEN_CORE_TENSOR_UTILS_H_
2#define ATEN_CORE_TENSOR_UTILS_H_
3
4#include <iomanip>
5
6#include <ATen/core/tensor.h>
8
9namespace container {
10
21__inline__
22std::string removeTrailingZeros(std::string str) {
23 int i = static_cast<int>(str.length()) - 1;
24 while (i >= 0 && str[i] == '0') {
25 i--;
26 }
27 if (i == -1) {
28 return "0";
29 }
30 return str.substr(0, i + 1);
31}
32
48
49template<typename T>
50__inline__
52 const T* arr,
53 int size,
54 int& integer_count,
55 int& fraction_count)
56{
57 integer_count = 0;
58 fraction_count = 0;
59
60 for (int i = 0; i < size; i++) {
61 int digits = 0;
62 if (arr[i] < 0) {
63 digits = log10(-arr[i]) + 1;
64 if (digits + 1 > integer_count) {
65 integer_count = digits + 1;
66 }
67 }
68 else {
69 digits = log10(arr[i]) + 1;
70 if (digits > integer_count) {
71 integer_count = digits;
72 }
73 }
74
75 T fraction = arr[i] - std::floor(arr[i]);
76 if (fraction == 0) {
77 continue;
78 }
79 std::string str = removeTrailingZeros(std::to_string(fraction));
80 digits = str.length() - str.find('.');
81 if (digits > fraction_count) {
82 fraction_count = digits;
83 }
84 }
85
86 return integer_count + fraction_count;
87}
88
104template<typename T>
105__inline__
107 const std::complex<T>* arr,
108 int size,
109 int& integer_count,
110 int& fraction_count)
111{
112 return _get_digit_places<T>(reinterpret_cast<const T*>(arr), size * 2, integer_count, fraction_count);
113}
114
126template <typename T>
127__inline__
129 std::ostream& os,
130 const T data,
131 const int& digit_width,
132 const int& fraction_count)
133{
134 os << std::setw(digit_width) \
135 << std::setprecision(fraction_count) << std::fixed << data;
136}
137
149template <typename T>
150__inline__
152 std::ostream& os,
153 const std::complex<T> data,
154 const int& digit_width,
155 const int& fraction_count)
156{
157 // Write the real and imaginary parts of the complex value to the output stream
158 // with the specified formatting.
159 os << "{";
160 os << std::setw(digit_width) \
161 << std::setprecision(fraction_count) << std::fixed
162 << data.real();
163 os << ", ";
164 os << std::setw(digit_width) \
165 << std::setprecision(fraction_count) << std::fixed
166 << data.imag();
167 os << "}";
168}
169
179template <>
180__inline__
182 std::ostream& os,
183 const int data,
184 const int& digit_width,
185 const int& fraction_count)
186{
187 os << std::setw(digit_width - 1) \
188 << std::setprecision(fraction_count) << std::fixed << data;
189}
190
205template <typename T>
206__inline__
208 std::ostream& os,
209 const T * data,
210 const TensorShape& shape,
211 const int64_t& num_elements)
212{
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) {
216 os << "[";
217 for (int i = 0; i < num_elements; ++i) {
218 _output_wrapper(os, data[i], digit_width, fraction_count);
219 if (i != num_elements - 1) {
220 os << ",";
221 }
222 }
223 os << "]";
224 }
225 else if (shape.ndim() == 2) {
226 os << "[";
227 for (int i = 0; i < shape.dim_size(0); ++i) {
228 if (i != 0) os << " ";
229 os << "[";
230 for (int j = 0; j < shape.dim_size(1); ++j) {
231 _output_wrapper(os, data[i * shape.dim_size(1) + j], digit_width, fraction_count);
232 if (j != shape.dim_size(1) - 1) {
233 os << ", ";
234 }
235 }
236 os << "]";
237 if (i != shape.dim_size(0) - 1) os << ",\n";
238 }
239 os << "]";
240 }
241 else if (shape.ndim() == 3) {
242 os << "[";
243 for (int i = 0; i < shape.dim_size(0); ++i) {
244 if (i != 0) os << " ";
245 os << "[";
246 for (int j = 0; j < shape.dim_size(1); ++j) {
247 if (j != 0) os << " ";
248 os << "[";
249 for (int k = 0; k < shape.dim_size(2); ++k) {
250 _output_wrapper(os, data[i * shape.dim_size(1) * shape.dim_size(2) + j * shape.dim_size(2) + k], digit_width, fraction_count);
251 if (k != shape.dim_size(2) - 1) {
252 os << ", ";
253 }
254 }
255 os << "]";
256 if (j != shape.dim_size(1) - 1) os << ",\n";
257 }
258 os << "]";
259 if (i != shape.dim_size(0) - 1) os << ",\n\n";
260 }
261 os << "]";
262 }
263 else {
264 for (int64_t i = 0; i < num_elements; ++i) {
265 _output_wrapper(os, data[i], 0, 0);
266 if (i < num_elements - 1) {
267 os << ", ";
268 }
269 }
270 }
271}
272
273template <typename T>
275 if (tensor.device_type() == DeviceType::CpuDevice) {
276 return reinterpret_cast<T*>(tensor.data())[0];
277 }
278 else {
279 T result = 0;
280 TEMPLATE_ALL_2(tensor.data_type(), tensor.device_type(),
282 &result, reinterpret_cast<T*>(tensor.data()), 1))
283 return result;
284 }
285}
286
287} // namespace container
288
289#endif // ATEN_CORE_TENSOR_UTILS_H_
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
Definition tensor.cpp:8
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