forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops.h
190 lines (163 loc) · 5.41 KB
/
ops.h
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
#include <ATen/Utils.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/runtime/static/impl.h>
namespace at {
namespace native {
at::Tensor& reshape_copy_out(
at::Tensor& out,
const at::Tensor& self,
const at::DimVector& proposed_shape,
bool infer_size = true);
at::Tensor& to_copy_out(
Tensor& out,
const Tensor& self,
bool non_blocking,
bool copy_strides,
c10::optional<MemoryFormat> memory_format);
} // namespace native
} // namespace at
namespace torch {
namespace jit {
using SROpFunctor = SROperator (*)(Node* n);
struct SROperatorFunctor {
virtual SROperator Generate(Node*) {
SROperator out;
return out;
}
virtual ~SROperatorFunctor() = default;
};
C10_DECLARE_REGISTRY(SROperatorRegistry, SROperatorFunctor);
#define REGISTER_OPERATOR_FUNCTOR(name, id, ...) \
struct SROperatorFunctor_##id : public SROperatorFunctor { \
const SROpFunctor fn = __VA_ARGS__; \
SROperator Generate(Node* n) override { \
return fn(n); \
} \
}; \
C10_REGISTER_CLASS(SROperatorRegistry, name, SROperatorFunctor_##id);
C10_DECLARE_REGISTRY(SRNativeOperatorRegistry, SROperatorFunctor);
#define REGISTER_NATIVE_OPERATOR_FUNCTOR(name, id, ...) \
struct SRNativeOperatorFunctor_##id : public SROperatorFunctor { \
const SROpFunctor fn = __VA_ARGS__; \
SROperator Generate(Node* n) override { \
return fn(n); \
} \
}; \
C10_REGISTER_CLASS( \
SRNativeOperatorRegistry, name, SRNativeOperatorFunctor_##id);
inline at::Tensor create_empty_from(const at::Tensor& t) {
return at::detail::empty_cpu(
{0},
c10::typeMetaToScalarType(t.dtype()),
t.layout(),
t.device(),
c10::nullopt,
c10::nullopt);
}
inline at::Tensor create_empty_from(
at::IntArrayRef sizes,
const at::Tensor& t) {
return at::detail::empty_cpu(
sizes,
c10::typeMetaToScalarType(t.dtype()),
t.layout(),
t.device(),
c10::nullopt,
c10::nullopt);
}
inline at::Tensor create_empty(c10::ScalarType dtype) {
return at::detail::empty_cpu(
{0}, dtype, c10::nullopt, c10::nullopt, c10::nullopt, c10::nullopt);
}
inline at::Tensor create_empty_from(
const at::Tensor& t,
c10::ScalarType dtype) {
return at::detail::empty_cpu(
{0}, dtype, t.layout(), t.device(), c10::nullopt, c10::nullopt);
}
inline at::Tensor create_empty_from(const at::Tensor& t, c10::Layout layout) {
return at::detail::empty_cpu(
{0},
c10::typeMetaToScalarType(t.dtype()),
layout,
t.device(),
c10::nullopt,
c10::nullopt);
}
inline at::Tensor create_empty_from(const at::Tensor& t, c10::Device device) {
return at::detail::empty_cpu(
{0},
c10::typeMetaToScalarType(t.dtype()),
t.layout(),
device,
c10::nullopt,
c10::nullopt);
}
inline at::Tensor create_empty_from(
const at::Tensor& t,
c10::MemoryFormat memory_format) {
return at::detail::empty_cpu(
{0},
c10::typeMetaToScalarType(t.dtype()),
t.layout(),
t.device(),
c10::nullopt,
memory_format);
}
inline at::Tensor create_empty_from(
const at::Tensor& t,
c10::ScalarType dtype,
c10::MemoryFormat memory_format) {
return at::detail::empty_cpu(
{0}, dtype, t.layout(), t.device(), c10::nullopt, memory_format);
}
inline bool checkResizedDataPtr(at::Tensor& t) {
auto const prev_data_ptr = t.data_ptr();
t.resize_({0});
return prev_data_ptr == t.data_ptr();
}
inline void fastResizeToZero(at::Tensor& t) {
t.unsafeGetTensorImpl()->set_sizes_contiguous({0});
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(checkResizedDataPtr(t));
}
// check if an op has an out variant registered in Static Runtime
bool opIsRegistered(const c10::Symbol& op_name);
// check if Static Runtime can run an op natively.
// prim ops that are implemented directly in the jit interpreter are implemented
// as native ops in Static Runtime
bool nativeOpIsRegistered(const c10::Symbol& op_name);
bool canReuseInputsOutputs(
Node* n,
const FastMap<Node*, bool>& node_has_out_variant);
bool isOptimizableContainerType(
Node* n,
const FastMap<Node*, bool>& node_has_out_variant);
SROperator getOutOfPlaceOperation(Node* n);
SROperator getNativeOperation(Node* n);
bool hasVarArgs(Node* n);
inline std::string PrintNode(const Node* node) {
std::ostringstream ss;
node->print(ss, 0, nullptr, false);
return ss.str();
}
inline void LogAndDumpSchema(const Node* node) {
VLOG(1) << "Found schema mismatch for: " << node->schema();
}
inline bool sr_schema_check(torch::jit::Node*) {
return true;
}
template <typename Schema, typename... Schemas>
bool sr_schema_check(
torch::jit::Node* node,
Schema&& first,
Schemas&&... rest) {
auto is_match = node->matches(first) || sr_schema_check(node, rest...);
if (!is_match) {
torch::jit::LogAndDumpSchema(node);
}
return is_match;
}
bool sr_schema_check_kind(torch::jit::Node* node, c10::Symbol node_kind);
} // namespace jit
} // namespace torch