forked from serizba/cppflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops.h
More file actions
103 lines (77 loc) · 2.42 KB
/
Copy pathops.h
File metadata and controls
103 lines (77 loc) · 2.42 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
//
// Created by serizba on 31/7/20.
//
#ifndef CPPFLOW2_OPS_H
#define CPPFLOW2_OPS_H
#include "tensor.h"
#include "raw_ops.h"
namespace cppflow {
/**
* @name Operators
*/
//@{
/**
* @returns x + y elementwise
*/
tensor operator+(const tensor& x, const tensor& y);
/**
* @returns x - y elementwise
*/
tensor operator-(const tensor& x, const tensor& y);
/**
* @returns x * y elementwise
*/
tensor operator*(const tensor& x, const tensor& y);
/**
* @return x / y elementwise
*/
tensor operator/(const tensor& x, const tensor& y);
std::ostream& operator<<(std::ostream& os, const cppflow::tensor& t);
//@}
/**
* @return A string representing t in the form:
* (tensor: shape=?, data=
* ?)
*/
std::string to_string(const tensor& t);
}
/******************************
* IMPLEMENTATION DETAILS *
******************************/
namespace cppflow {
// Operators
tensor operator+(const tensor& x, const tensor& y) {
return add(x, y);
}
tensor operator-(const tensor& x, const tensor& y) {
return sub(x, y);
}
tensor operator*(const tensor& x, const tensor& y) {
return mul(x, y);
}
tensor operator/(const tensor& x, const tensor& y) {
return div(x, y);
}
std::ostream& operator<<(std::ostream& os, const cppflow::tensor& t) {
std::string res = to_string(t);
return os << res;
}
std::string to_string(const tensor &t) {
auto res_tensor = string_format({t.shape(), t}, "(tensor: shape=%s, data=\n%s)");
auto res_tensor_h = res_tensor.get_tensor();
#ifdef TENSORFLOW_C_TF_TSTRING_H_
// For future version TensorFlow 2.4
//auto *t_str = reinterpret_cast<TF_TString *>(TF_TensorData(res_tensor_h.get()));
auto *t_str = (TF_TString *)(TF_TensorData(res_tensor_h.get()));
auto result = std::string(TF_TString_GetDataPointer(t_str), TF_TString_GetSize(t_str));
#else
const char* dst[1] = {nullptr};
size_t dst_len[1] = {3};
TF_StringDecode(static_cast<char*>(TF_TensorData(res_tensor_h.get())) + 8, TF_TensorByteSize(res_tensor_h.get()), dst, dst_len, context::get_status());
status_check(context::get_status());
auto result = std::string(dst[0], *dst_len);
#endif // TENSORFLOW_C_TF_TSTRING_H_
return result;
}
}
#endif //CPPFLOW2_OPS_H