def op_generate(opdef): check_error(opdef.status) func_name = _create_func_name(opdef.name) inputs = opdef.inputs outputs = opdef.outputs attrs = opdef.attrs derivate_attrs = set() default_attrs = set() for i in inputs: if i.type.repeated == pybind.OpDefineItem.RepeatType.no_repeat: if i.type.type.attr != "": derivate_attrs.add(i.type.type.attr) elif i.type.repeated == pybind.OpDefineItem.RepeatType.type_and_size: if i.type.type.attr != "": derivate_attrs.add(i.type.type.attr) if i.type.size.attr != "": derivate_attrs.add(i.type.size.attr) elif i.type.repeated == pybind.OpDefineItem.RepeatType.type_list: derivate_attrs.add(i.type.type_list) for i in attrs: if i.default_value.attr_type != pybind.AttrValue.Type.none: default_attrs.add(i.name) func_input = [] func_output = [] for i in inputs: func_input.append(i.name) for i in attrs: if i.name not in derivate_attrs: if i.name not in default_attrs: func_input.append(i.name) for i in attrs: if i.name in derivate_attrs or i.name in default_attrs: func_input.append(i.name + " = None") for i in outputs: func_output.append(i.name) builder = PyBuilder() # def if len(func_input) == 0: def_fmt = "def {0}({1}name_ = None):" else: def_fmt = "def {0}({1}, name_ = None):" with builder.indent_stmt(def_fmt, func_name, ', '.join(func_input)): with builder.indent_stmt("try:"): # convert to tensor for i in inputs: if i.type.repeated == pybind.OpDefineItem.RepeatType.no_repeat: builder.append_stmt("{0} = convert_to_tensor({0})", i.name) elif i.type.repeated == pybind.OpDefineItem.RepeatType.type_and_size: builder.append_stmt("{0} = [convert_to_tensor(i) for i in {0}]", i.name) elif i.type.repeated == pybind.OpDefineItem.RepeatType.type_list: builder.append_stmt("{0} = [convert_to_tensor(i) for i in {0}]", i.name) builder.append_stmt("") # derivate_attrs for i in inputs: if i.type.repeated == pybind.OpDefineItem.RepeatType.no_repeat: if i.type.type.attr != "": _generate_check_set_attr(builder, i.type.type.attr, i.name + ".dtype") builder.append_stmt("") elif i.type.repeated == pybind.OpDefineItem.RepeatType.type_and_size: if i.type.type.attr != "": with builder.indent_stmt("for i in {0}:", i.name): _generate_check_set_attr(builder, i.type.type.attr, "i.dtype") builder.append_stmt("") if i.type.size.attr != "": _generate_check_set_attr(builder, i.type.size.attr, "len({0})".format(i.name)) builder.append_stmt("") elif i.type.repeated == pybind.OpDefineItem.RepeatType.type_list: with builder.indent_stmt("if {0} is None:", i.type.type_list): builder.append_stmt("{0} = []", i.type.type_list) with builder.indent_stmt("for i in {0}:", i.name): builder.append_stmt("{0}.append(i.dtype)", i.type.type_list) builder.append_stmt("") # append default attr for i in attrs: if i.default_value.attr_type == pybind.AttrValue.Type.none: continue elif i.default_value.attr_type == pybind.AttrValue.Type.string: _generate_default_attr(builder, i.name, repr(i.default_value.s)) elif i.default_value.attr_type == pybind.AttrValue.Type.int: _generate_default_attr(builder, i.name, repr(i.default_value.i)) elif i.default_value.attr_type == pybind.AttrValue.Type.float: _generate_default_attr(builder, i.name, repr(i.default_value.f)) elif i.default_value.attr_type == pybind.AttrValue.Type.bool: _generate_default_attr(builder, i.name, repr(i.default_value.b)) elif i.default_value.attr_type == pybind.AttrValue.Type.shape: _generate_default_attr(builder, i.name, repr(TensorShape(i.default_value.shape))) elif i.default_value.attr_type == pybind.AttrValue.Type.type: _generate_default_attr(builder, i.name, repr(DataType(i.default_value.type))) else: _generate_default_attr(builder, i.name, repr(i.default_value.type_list)) builder.append_stmt("") # check attr for i in attrs: if i.type == pybind.AttrValue.Type.none: raise ValueError("Attr Type should not be none") elif i.type == pybind.AttrValue.Type.string: _generate_check_attr(builder, i.name, "(str, unicode, bytes)") elif i.type == pybind.AttrValue.Type.int: _generate_check_attr(builder, i.name, "(int, long)") elif i.type == pybind.AttrValue.Type.float: _generate_check_attr(builder, i.name, "(float)") elif i.type == pybind.AttrValue.Type.bool: _generate_check_attr(builder, i.name, "(bool)") elif i.type == pybind.AttrValue.Type.shape: _generate_check_attr(builder, i.name, "(TensorShape, list, tuple)") elif i.type == pybind.AttrValue.Type.type: _generate_check_attr(builder, i.name, "(DataType)") elif i.type == pybind.AttrValue.Type.type_list: _generate_check_attr(builder, i.name, "(list, tuple)") else: raise NotImplementedError builder.append_stmt("") # check input dtype for i in inputs: if i.type.type.attr != "": dtype = i.type.type.attr else: dtype = repr(DataType(i.type.type.raw)) if i.type.size.attr != "": dsize = i.type.size.attr else: dsize = repr(i.type.size.raw) if i.type.repeated == pybind.OpDefineItem.RepeatType.no_repeat: with builder.indent_stmt("if {0}.dtype != {1}:", i.name, dtype): builder.append_stmt("raise ValueError('Check Input {0} type should be' + repr({1}) + ' but is ' + repr({0}.dtype))", i.name, dtype) elif i.type.repeated == pybind.OpDefineItem.RepeatType.type_and_size: with builder.indent_stmt("if len({0}) != {1}:", i.name, dsize): builder.append_stmt("raise ValueError('Check Input {0} size should be ' + repr({1}) + ' but is ' + repr(len({0})))", i.name, dsize) with builder.indent_stmt("for i in range({0}):", dsize): with builder.indent_stmt("if {0}[i].dtype != {1}:", i.name, dtype): builder.append_stmt("raise ValueError('Check Input {0} type should be ' + repr({1}) + ' but is ' + repr({0}[i].dtype))", i.name, dtype) elif i.type.repeated == pybind.OpDefineItem.RepeatType.type_list: with builder.indent_stmt("if len({0}) != len({1}):", i.name, i.type.type_list): builder.append_stmt("raise ValueError('Check Input {0} size should be ' + repr(len({1})) + ' but is ' + repr(len({0})))", i.name, i.type.type_list) with builder.indent_stmt("for i in range(len({0})):", i.name): with builder.indent_stmt("if {0}[i].dtype != {1}[i]:", i.name, i.type.type_list): builder.append_stmt("raise ValueError('Check Input {0}[' + repr(i) + '] type should be ' + repr({1}[i]) + ' but is ' + repr({0}[i].dtype))", i.name, i.type.type_list) else: raise NotImplementedError builder.append_stmt("") # prepare CreateOp builder.append_stmt("__input = [{0}]", ", ".join([i.name for i in inputs])) builder.append_stmt("__args = {0}{2}{1}", "{", "}", ", ".join(["'{0}' : {0}".format(i.name) for i in attrs])) output_spec = [] for i in outputs: if i.type.type.attr != "": dtype = i.type.type.attr else: dtype = repr(DataType(i.type.type.raw)) if i.type.size.attr != "": dsize = i.type.size.attr else: dsize = repr(i.type.size.raw) if i.type.repeated == pybind.OpDefineItem.RepeatType.no_repeat: output_spec.append("{0}".format(dtype)) elif i.type.repeated == pybind.OpDefineItem.RepeatType.type_and_size: output_spec.append("[{0}] * {1}".format(dtype, dsize)) elif i.type.repeated == pybind.OpDefineItem.RepeatType.type_list: output_spec.append(i.type.type_list) builder.append_stmt("__output_spec = [{0}]", ", ".join(output_spec)) builder.append_stmt("") builder.append_stmt("__op = create_op(_op_def['{0}'], name_, __input, __args, __output_spec)", opdef.name) builder.append_stmt("") if len(outputs) == 0: builder.append_stmt("return __op.depend()") elif len(outputs) == 1: builder.append_stmt("return __op.outputs[0]") else: builder.append_stmt("return __op.outputs") with builder.indent_stmt("except Exception as e:"): builder.append_stmt("print") builder.append_stmt("print _func_code['{0}']", opdef.name) builder.append_stmt("raise") builder.append_stmt("result_func = {0}", func_name) exec(builder.get_stmts()) _func_code[opdef.name] = builder.get_numbered_stmts() _op_def[opdef.name] = opdef result_func.code_text = _func_code[opdef.name] return func_name, result_func
def execute_loop_wait(): check_error(pybind.execute_loop_wait())
def init(self): check_error(self.service.init())
def execute(self, outputs, run_option=None, run_statistic=None): if run_option and run_option.perf: if run_statistic is None: raise 'run_statistic must be specified when perf is turned on' output_define = [] def recursive_feed_output(x, k): if isinstance(x, Tensor): x = x.define if isinstance(x, (str, unicode)): output_define.append(x) if x[0] == '^': return None, k else: return k, k + 1 elif isinstance(x, (list, tuple, set)): rst = [] for i in x: y, k = recursive_feed_output(i, k) rst += [y] return x.__class__(rst), k elif isinstance(x, dict): rst = {} for i in x: y, k = recursive_feed_output(x[i], k) rst[i] = y return rst, k else: raise ValueError("cannot execute type {}".format(x)) output_spec, _ = recursive_feed_output(outputs, 0) xdl_output_spec = pybind.OutputSpec() xdl_output_spec.output = pybind.StringVector(output_define) xdl_output_spec.output_device = Graph.default_device() run_option = run_option if run_option is not None else pybind.RunOption( ) result = pybind.execute(self._graph_def, xdl_output_spec, run_option) check_error(result.status) outputs = result.outputs if run_option and run_option.perf: run_statistic.perf_result = result.run_statistic.perf_result def recursive_build_result(x): if x is None: return None if isinstance(x, (int, long)): return numpy.array(outputs[x], copy=False) elif isinstance(x, (list, tuple, set)): rst = [] for i in x: y = recursive_build_result(i) rst += [y] return x.__class__(rst) elif isinstance(x, dict): rst = {} for i in x: y = recursive_build_result(x[i]) rst[i] = y return rst else: raise ValueError("Internal Error") return recursive_build_result(output_spec)