提交 67b911ab authored 作者: Olivier Breuleux's avatar Olivier Breuleux

added gof.op.Dispatch

上级 cca46fc2
...@@ -15,7 +15,7 @@ from link import \ ...@@ -15,7 +15,7 @@ from link import \
Linker, LocalLinker, PerformLinker, MetaLinker, Profiler Linker, LocalLinker, PerformLinker, MetaLinker, Profiler
from op import \ from op import \
Op, Macro Op, Macro, Dispatch
from opt import \ from opt import \
Optimizer, SeqOptimizer, \ Optimizer, SeqOptimizer, \
...@@ -23,7 +23,7 @@ from opt import \ ...@@ -23,7 +23,7 @@ from opt import \
LocalOptimizer, LocalOptGroup, LocalOpKeyOptGroup, \ LocalOptimizer, LocalOptGroup, LocalOpKeyOptGroup, \
ExpandMacro, OpSub, OpRemove, PatternSub, \ ExpandMacro, OpSub, OpRemove, PatternSub, \
NavigatorOptimizer, TopoOptimizer, OpKeyOptimizer, \ NavigatorOptimizer, TopoOptimizer, OpKeyOptimizer, \
expand_macros ExpandMacros
from toolbox import \ from toolbox import \
Bookkeeper, History, Validator, ReplaceValidate, NodeFinder, PrintListener Bookkeeper, History, Validator, ReplaceValidate, NodeFinder, PrintListener
......
...@@ -22,7 +22,7 @@ class Op(utils.object2): ...@@ -22,7 +22,7 @@ class Op(utils.object2):
This function should return an Apply instance representing the This function should return an Apply instance representing the
application of this Op on the provided inputs. application of this Op on the provided inputs.
""" """
raise utils.AbstractFunctionError() raise utils.AbstractFunctionError(self)
def __call__(self, *inputs): def __call__(self, *inputs):
""" """
...@@ -61,7 +61,7 @@ class Op(utils.object2): ...@@ -61,7 +61,7 @@ class Op(utils.object2):
by a previous call to impl and impl is free to reuse it as it by a previous call to impl and impl is free to reuse it as it
sees fit. sees fit.
""" """
raise utils.AbstractFunctionError() raise utils.AbstractFunctionError(self)
##################### #####################
# C code generation # # C code generation #
...@@ -132,9 +132,42 @@ class Macro(Op): ...@@ -132,9 +132,42 @@ class Macro(Op):
into a computable graph with its expand() method. into a computable graph with its expand() method.
""" """
def expand(self, *outputs): def expand(self, node):
""" """
Returns a node representing the expansion of this macro. Returns a node representing the expansion of this macro.
""" """
raise utils.AbstractFunctionError() raise utils.AbstractFunctionError()
class Dispatch(Macro):
"""
Dispatches inputs to one of a list of candidate ops.
Tries each candidate in order.
"""
def __init__(self, name, candidates):
if not isinstance(name, str):
raise TypeError("name should be a string, not:", name, type(name))
self.candidates = candidates
self.name = name
def __node(self, *inputs):
for candidate in self.candidates:
try:
return candidate.make_node(*inputs)
except:
continue
raise TypeError("No suitable candidate found for %s(%s)" % (self, inputs))
def make_node(self, *inputs):
node = self.__node(*inputs)
node.op = self
return node
def expand(self, node):
return self.__node(*node.inputs)
def __str__(self):
return self.name
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论