-
Notifications
You must be signed in to change notification settings - Fork 0
/
np_functional.py
240 lines (184 loc) · 7.91 KB
/
np_functional.py
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from ast import Lambda
from ctypes import c_int
import ctypes
import ctree
from ctree.c.nodes import FunctionCall, SymbolRef, FunctionDecl, For, Assign, \
Constant, Lt, PreInc, ArrayRef, Return, CFile
from ctree.cpp.nodes import CppDefine
from ctree.jit import LazySpecializedFunction, ConcreteSpecializedFunction
from ctree.nodes import Project
from ctree.transformations import PyBasicConversions
from ctree.visitors import NodeTransformer
import numpy as np
import logging
logging.basicConfig(level=20)
def np_map(function, array):
vec_func = np.frompyfunc(function, 1, 1)
array[:] = vec_func(array)
return array
def np_reduce(function, array):
return reduce(function, array.flat)
def np_elementwise(function, array1, array2):
array1[:] = function(array1, array2)
return array1
class LambdaLifter(NodeTransformer):
lambda_counter = 0
def __init__(self):
self.lifted_functions = []
def visit_Lambda(self, node):
self.generic_visit(node)
macro_name = "LAMBDA_" + str(self.lambda_counter)
LambdaLifter.lambda_counter += 1
node = PyBasicConversions().visit(node)
node.name = macro_name
macro = CppDefine(macro_name, node.params, node.defn[0].value)
self.lifted_functions.append(macro)
return SymbolRef(macro_name)
class BaseNpFunctionalTransformer(NodeTransformer):
lifted_functions = []
func_count = 0
def __init__(self, array_type):
self.array_type = array_type
def visit_Call(self, node):
self.generic_visit(node)
if getattr(node.func, "id", None) != self.func_name:
return node
return self.convert(node)
def convert(self, node):
inner_function = node.args[0]
if not isinstance(inner_function, Lambda):
raise Exception(
self.func_name + " requires lambda to be specialized")
lambda_lifter = LambdaLifter()
inner_function = lambda_lifter.visit(inner_function)
self.lifted_functions.extend(lambda_lifter.lifted_functions)
func_def = self.get_func_def(inner_function)
BaseNpFunctionalTransformer.lifted_functions.append(func_def)
c_node = FunctionCall(SymbolRef(func_def.name), node.args[1:])
return c_node
@property
def gen_func_name(self):
name = "%s_%s" % (self.func_name, str(type(self).func_count))
type(self).func_count += 1
return name
@property
def func_name(self):
raise NotImplementedError("Class %s should override func_name()"
% type(self))
def get_func_def(self, inner_function_name):
raise NotImplementedError("Class %s should override get_func_def()"
% type(self))
class NpMapTransformer(BaseNpFunctionalTransformer):
func_name = "np_map"
def get_func_def(self, inner_function):
number_items = np.prod(self.array_type._shape_)
params = [SymbolRef("A", self.array_type())]
return_type = self.array_type()
defn = [
For(Assign(SymbolRef("i", c_int()), Constant(0)),
Lt(SymbolRef("i"), Constant(number_items)),
PreInc(SymbolRef("i")),
[
Assign(ArrayRef(SymbolRef("A"), SymbolRef("i")),
FunctionCall(inner_function,
[ArrayRef(SymbolRef("A"),
SymbolRef("i"))])),
]),
Return(SymbolRef("A")),
]
return FunctionDecl(return_type, self.gen_func_name, params, defn)
class NpReduceTransformer(BaseNpFunctionalTransformer):
func_name = "np_reduce"
def get_func_def(self, inner_function):
number_items = np.prod(self.array_type._shape_)
params = [SymbolRef("A", self.array_type())]
elements_type = self.array_type._dtype_.type()
return_type = elements_type
defn = [
Assign(SymbolRef("accumulator", elements_type),
ArrayRef(SymbolRef("A"), Constant(0))),
For(Assign(SymbolRef("i", c_int()), Constant(1)),
Lt(SymbolRef("i"), Constant(number_items)),
PreInc(SymbolRef("i")),
[Assign(
SymbolRef("accumulator"),
FunctionCall(inner_function, [SymbolRef("accumulator"),
ArrayRef(SymbolRef("A"),
SymbolRef("i"))])
)]
),
Return(SymbolRef("accumulator")),
]
return FunctionDecl(return_type, self.gen_func_name, params, defn)
class NpElementwiseTransformer(BaseNpFunctionalTransformer):
func_name = "np_elementwise"
def get_func_def(self, inner_function):
number_items = np.prod(self.array_type._shape_)
params = [SymbolRef("A", self.array_type()),
SymbolRef("B", self.array_type())]
return_type = self.array_type()
defn = [
For(Assign(SymbolRef("i", c_int()), Constant(0)),
Lt(SymbolRef("i"), Constant(number_items)),
PreInc(SymbolRef("i")),
[
Assign(ArrayRef(SymbolRef("A"), SymbolRef("i")),
FunctionCall(inner_function,
[ArrayRef(SymbolRef("A"),
SymbolRef("i")),
ArrayRef(SymbolRef("B"),
SymbolRef("i"))])),
]),
Return(SymbolRef("A")),
]
return FunctionDecl(return_type, self.gen_func_name, params, defn)
class NpFunctionalTransformer(object):
transformers = [NpMapTransformer,
NpReduceTransformer,
NpElementwiseTransformer]
def __init__(self, array_type):
self.array_type = array_type
def visit(self, tree):
for transformer in self.transformers:
transformer(self.array_type).visit(tree)
return tree
@staticmethod
def lifted_functions():
return BaseNpFunctionalTransformer.lifted_functions
def sum_array(a):
np_map(lambda x: x*2, a)
np_elementwise(lambda x, y: x+y, a, a)
return np_reduce(lambda x, y: x+y, np_map(lambda x: x/4, a))
class BasicTranslator(LazySpecializedFunction):
def args_to_subconfig(self, args):
arg = args[0]
arg_type = np.ctypeslib.ndpointer(arg.dtype, arg.ndim, arg.shape)
return {'arg_type': arg_type}
def transform(self, tree, program_config):
arg_type = program_config.args_subconfig['arg_type']
tree = NpFunctionalTransformer(arg_type).visit(tree)
tree = PyBasicConversions().visit(tree)
fn = tree.find(FunctionDecl, name="apply")
fn.params[0].type = arg_type()
fn.return_type = arg_type._dtype_.type()
lifted_functions = NpFunctionalTransformer.lifted_functions()
c_translator = CFile("generated", [lifted_functions, tree])
return [c_translator]
def finalize(self, transform_result, program_config):
proj = Project(transform_result)
arg_config, tuner_config = program_config
arg_type = arg_config['arg_type']
entry_type = ctypes.CFUNCTYPE(arg_type._dtype_.type, arg_type)
return BasicFunction("apply", proj, entry_type)
class BasicFunction(ConcreteSpecializedFunction):
def __init__(self, entry_name, project_node, entry_typesig):
self._c_function = self._compile(entry_name, project_node, entry_typesig)
def __call__(self, *args, **kwargs):
return self._c_function(*args, **kwargs)
if __name__ == '__main__':
c_sum_array = BasicTranslator.from_function(sum_array)
test_array = np.array([range(10), range(10, 20)])
a = sum_array(test_array)
print a
b = c_sum_array(test_array)
print b