-
Notifications
You must be signed in to change notification settings - Fork 656
/
function_library.h
114 lines (95 loc) · 4.31 KB
/
function_library.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
/*
Copyright 2020 The OneFlow Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef ONEFLOW_CORE_FUNCTIONAL_FUNCTION_LIBRARY_H_
#define ONEFLOW_CORE_FUNCTIONAL_FUNCTION_LIBRARY_H_
#include "oneflow/core/common/util.h"
#include "oneflow/core/common/wrap_dim_utils.h"
#include "oneflow/core/functional/packed_functor.h"
#include "oneflow/core/common/stride.h"
#include "oneflow/core/framework/tensor_methods.h"
#include "oneflow/core/common/throw.h"
namespace oneflow {
namespace one {
namespace functional {
class FunctionLibrary {
public:
virtual ~FunctionLibrary() = default;
template<typename T>
struct PackedFuncCreatorMap;
template<typename R, typename... Args>
struct PackedFuncCreatorMap<R(Args...)> {
using FunctorCreator = typename std::function<PackedFunctor<R(Args...)>()>;
static HashMap<std::string, FunctorCreator>* Get() {
static HashMap<std::string, FunctorCreator> functors;
return &functors;
}
};
template<typename Func>
void add_functor(const std::string& func_name, const Func& func) {
using func_type = typename function_traits<Func>::func_type;
add_functor_creator<Func>(
func_name, [=]() { return PackedFunctorMaker<func_type>::make(func_name, func); });
}
template<typename Func>
void add_one_functor(const std::string& func_name) {
using func_type = typename function_traits<Func>::func_type;
add_functor_creator<Func>(func_name, [=]() {
// Lazily construct functor since ops maybe have not been registered.
Func func;
return PackedFunctorMaker<func_type>::make(func_name, func);
});
}
template<typename... Fs>
void add_functor(const std::string& func_name) {
static_assert(sizeof...(Fs) > 0, "at least one functor is expected");
(add_one_functor<Fs>(func_name), ...);
}
template<typename R, typename... Args>
auto find(const std::string& func_name)
-> Maybe<PackedFunctor<typename PackedFunctorMaker<R(Args...)>::FType>> {
auto* functors = PackedFuncCreatorMap<typename PackedFunctorMaker<R(Args...)>::FType>::Get();
const auto& it = functors->find(func_name);
CHECK_OR_RETURN(it != functors->end())
<< Error::RuntimeError() << "Functor was not found for \"" << func_name
<< "\", please check whether the functor has been registered correctly or not.";
return it->second();
}
static FunctionLibrary* Global() {
static FunctionLibrary global_function_library;
return &global_function_library;
}
private:
FunctionLibrary() = default;
template<typename Func, typename Creator>
void add_functor_creator(const std::string& func_name, Creator creator) {
using func_type = typename function_traits<Func>::func_type;
auto* functors = PackedFuncCreatorMap<typename PackedFunctorMaker<func_type>::FType>::Get();
CHECK_OR_THROW(functors->count(func_name) == 0)
<< Error::RuntimeError() << "The functor with name " << func_name
<< " has been registered more than once.";
functors->emplace(func_name, creator);
}
};
#define ONEFLOW_FUNCTION_LIBRARY(m) ONEFLOW_FUNCTION_LIBRARY_IMPL(m, __COUNTER__)
#define ONEFLOW_FUNCTION_LIBRARY_IMPL(m, uuid) \
static void OF_PP_CAT(_oneflow_function_library_, uuid)(FunctionLibrary & m); \
static int OF_PP_CAT(_oneflow_function_library_dummy_, uuid) = []() { \
FunctionLibrary* library = FunctionLibrary::Global(); \
OF_PP_CAT(_oneflow_function_library_, uuid)(*library); \
return 0; \
}(); \
void OF_PP_CAT(_oneflow_function_library_, uuid)(FunctionLibrary & m)
} // namespace functional
} // namespace one
} // namespace oneflow
#endif // ONEFLOW_CORE_FUNCTIONAL_FUNCTION_LIBRARY_H_