-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoperator.h
More file actions
167 lines (128 loc) · 4.31 KB
/
operator.h
File metadata and controls
167 lines (128 loc) · 4.31 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
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
#ifndef INFINI_OPS_OPERATOR_H_
#define INFINI_OPS_OPERATOR_H_
#include <cassert>
#include <memory>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include "dispatcher.h"
#include "handle.h"
#include "tensor.h"
namespace infini::ops::detail {
struct CacheKey {
std::size_t hash;
std::vector<Tensor> tensors;
std::size_t scalar_hash;
template <typename... Args>
static CacheKey Build(const Args&... args) {
CacheKey key;
key.hash = 0;
key.scalar_hash = 0;
(key.Absorb(args), ...);
return key;
}
private:
void Absorb(const Tensor& t) {
hash_combine(hash, t);
tensors.push_back(t);
}
template <typename T>
void Absorb(const T& v) {
hash_combine(hash, v);
hash_combine(scalar_hash, v);
}
};
} // namespace infini::ops::detail
template <>
struct std::hash<infini::ops::detail::CacheKey> {
std::size_t operator()(const infini::ops::detail::CacheKey& key) const {
return key.hash;
}
};
template <>
struct std::equal_to<infini::ops::detail::CacheKey> {
bool operator()(const infini::ops::detail::CacheKey& a,
const infini::ops::detail::CacheKey& b) const {
if (a.scalar_hash != b.scalar_hash) return false;
if (a.tensors.size() != b.tensors.size()) return false;
std::equal_to<infini::ops::Tensor> eq;
for (std::size_t i = 0; i < a.tensors.size(); ++i) {
if (!eq(a.tensors[i], b.tensors[i])) return false;
}
return true;
}
};
namespace infini::ops {
class OperatorBase {
public:
virtual ~OperatorBase() = default;
virtual std::size_t workspace_size_in_bytes() const { return 0; }
void set_handle(const Handle& handle) { handle_ = handle; }
void set_stream(void* stream) { stream_ = stream; }
void set_workspace(void* workspace) { workspace_ = workspace; }
void set_workspace_size_in_bytes(std::size_t workspace_size_in_bytes) {
workspace_size_in_bytes_ = workspace_size_in_bytes;
}
protected:
Handle handle_;
void* stream_{nullptr};
void* workspace_{nullptr};
std::size_t workspace_size_in_bytes_{0};
};
template <typename Key, Device::Type device_type = Device::Type::kCount>
class Operator : public OperatorBase {
public:
template <typename... Args>
static auto make(const Tensor tensor, Args&&... args) {
std::unique_ptr<Operator> op_ptr;
DispatchFunc<ActiveDevices>(
tensor.device().type(),
[&](auto tag) {
constexpr Device::Type kDev = decltype(tag)::value;
if constexpr (std::is_constructible_v<Operator<Key, kDev>,
const Tensor&, Args...>) {
op_ptr = std::make_unique<Operator<Key, kDev>>(
tensor, std::forward<Args>(args)...);
} else {
assert(false && "operator is not implemented for this device");
}
},
"Operator::make");
return op_ptr;
}
template <typename... Args>
static auto call(const Handle& handle, void* stream, void* workspace,
std::size_t workspace_size_in_bytes, Args&&... args) {
static std::unordered_map<detail::CacheKey, std::unique_ptr<Operator>>
cache;
auto key = detail::CacheKey::Build(args...);
auto it{cache.find(key)};
if (it == cache.end()) {
it = cache.emplace(std::move(key), make(std::forward<Args>(args)...))
.first;
}
auto& op{it->second};
auto resolved_stream{stream ? stream : handle.stream()};
auto resolved_workspace{workspace ? workspace : handle.workspace()};
auto resolved_workspace_size{workspace_size_in_bytes
? workspace_size_in_bytes
: handle.workspace_size_in_bytes()};
op->set_handle(handle);
op->set_stream(resolved_stream);
op->set_workspace(resolved_workspace);
op->set_workspace_size_in_bytes(resolved_workspace_size);
return (*op)(std::forward<Args>(args)...);
}
template <typename... Args>
static auto call(const Tensor tensor, Args&&... args) {
return call({}, nullptr, nullptr, 0, tensor, std::forward<Args>(args)...);
}
template <typename... Args>
auto operator()(Args&&... args) const {
return (*static_cast<const Key*>(this))(std::forward<Args>(args)...);
}
protected:
static constexpr Device::Type device_type_{device_type};
};
} // namespace infini::ops
#endif