def veval_ast_attribute(astc: 'AstContext', local_field: 'values.Field', graph: 'Graph', option: 'VEvalOption' = None) -> 'Attribute': assert (isinstance(astc.nast, gast.gast.Attribute)) lineprop = utils.LineProperty(astc.lineno) from_module = True if option is not None and option.eval_as_written_target: from_module = False value = veval_ast(astc.c(astc.nast.value), local_field, graph) value_obj = try_get_obj(value, 'attribute', lineprop) attr = value_obj.get_field().get_attribute(astc.nast.attr, from_module) # property(getter) if attr.has_obj() and isinstance( attr.get_obj(False).get_value(), values.FuncValue ) and attr.get_obj(False).get_value().func.is_property: func_value = attr.get_obj(False).get_value() ret = func_value.func.vcall(local_field.module, graph, func_value.obj, functions.FunctionArgInput(), lineprop) return ret if attr.has_obj(): return attr # if attr is not found gotten_obj = value_obj.try_get_and_store_obj(astc.nast.attr) if gotten_obj is not None: return value_obj.get_field().get_attribute(astc.nast.attr, from_module) return attr
def vcall(self, module: 'Field', graph: 'Graph', inst: 'values.ValueRef', args: 'functions.FunctionArgInput', line=-1): args = functions.FunctionArgInput() args.inputs.append(inst) args.keywords['self'] = inst value = values.ListValue(self.owner.children) return values.ValueRef(value)
def veval_ast_call(astc: 'AstContext', local_field: 'values.Field', graph: 'Graph') -> 'Attribute': assert (isinstance(astc.nast, gast.gast.Call)) lineprop = utils.LineProperty(astc.lineno) func = veval_ast(astc.c(astc.nast.func), local_field, graph) if func == None or not func.has_obj(): if config.show_warnings: print('Unknown function {} is called in L.{}'.format( get_ast_name_forcibly(astc.nast.func), astc.lineno)) return None func_obj = try_get_ref(func, 'call', lineprop) func_value = try_get_value(func, 'call', lineprop) finput = functions.FunctionArgInput() for arg in astc.nast.args: arg_ = veval_ast(astc.c(arg), local_field, graph) finput.inputs.append(try_get_ref(arg_, 'call', lineprop)) for keyword in astc.nast.keywords: arg_ = veval_ast(astc.c(keyword.value), local_field, graph) finput.keywords[keyword.arg] = try_get_ref(arg_, 'call', lineprop) lineprop = utils.LineProperty(astc.lineno) ret = None if isinstance(func_value, values.FuncValue): ret = func_value.func.vcall(local_field.module, graph, func_value.obj, finput, lineprop) return ret elif isinstance(func_value, values.Instance): # __call__ call_func_ref = func_obj.try_get_and_store_obj('__call__', graph.root_graph) if call_func_ref is not None: ret = call_func_ref.get_value().func.vcall(local_field.module, graph, func_obj, finput, lineprop) return ret if config.show_warnings: print('Unknown function is called in L.{}'.format(astc.lineno)) return None
def vcall(self, module: 'Field', graph: 'Graph', inst: 'values.ValueRef', args: 'functions.FunctionArgInput', line=-1): args = functions.FunctionArgInput() args.inputs.append(inst) args.keywords['self'] = inst node = nodes.NodeCall(self, args, line) value = values.NumberValue(None) value.dtype = np.array(0).dtype value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) graph.add_node(node) return values.ValueRef(value)
def vcall(self, module: 'Field', graph: 'Graph', inst: 'values.ValueRef', args: 'functions.FunctionArgInput', line=-1): args = functions.FunctionArgInput() args.inputs.append(inst) args.keywords['self'] = inst node = nodes.NodeCall(self, args, line) value = values.ListValue() value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) # TODO should make tuple graph.add_node(node) return values.ValueRef(value)
def veval_ast_listcomp(astc: 'AstContext', local_field: 'values.Field', graph: 'Graph'): ''' Ex. [x for x in xx] [elt for target in iter] ''' assert (isinstance(astc.nast, gast.gast.ListComp)) lineprop = utils.LineProperty(astc.lineno) listcomp_guid = str(utils.get_guid()) listcomp_id = 'listcomp_' + listcomp_guid body_id = 'listcomp_body_' + listcomp_guid internal_counter_id = '@internal/listcomp_counter_' + listcomp_guid internal_list_id = '@internal/listcomp_list_' + listcomp_guid internal_cond_id = '@internal/listcomp_cond_' + listcomp_guid generator = astc.nast.generators[0] iter_value = try_get_value( veval_ast(astc.c(generator.iter), local_field, graph), 'generator', lineprop) list_value = values.ListValue() list_obj = values.ValueRef(list_value) node_generate_list = nodes.NodeGenerate('List', [], lineprop) node_generate_list.set_outputs([list_value]) graph.add_node(node_generate_list) # body target_name = '' if isinstance(generator.target, gast.gast.Name): target_name = generator.target.id else: if config.show_warnings: print('This for is not supported. in L.{}'.format(astc.lineno)) return None counter_value = values.NumberValue(None) counter_value.dtype = np.array(0).dtype counter_value.name = internal_counter_id cond_value = values.BoolValue(None) cond_value.name = internal_cond_id # set values with internal name local_field.get_attribute(internal_list_id).revise(list_obj) values.push_history(listcomp_id) body_graph = Graph() body_graph.root_graph = graph.root_graph body_graph.name = 'Body_' + listcomp_guid node_forgen = nodes.NodeForGenerator(counter_value, iter_value) target_ref = iter_value.get_iterator() if target_ref is None: target_ref = values.ValueRef(values.UnknownValue()) if config.show_warnings: print('unknown iteratable type in L.{}'.format(astc.lineno)) target_value = target_ref.get_value() node_forgen.set_outputs([target_ref.get_value()]) local_field.get_attribute(target_name, from_module=False).revise(target_ref) body_graph.add_node(node_forgen) elt = veval_ast(astc.c(astc.nast.elt), local_field, body_graph) elt_obj = try_get_ref(elt, 'listcomp', lineprop) finput = functions.FunctionArgInput() finput.inputs.append(elt_obj) append_value = local_field.get_attribute(internal_list_id).get_ref( ).get_field().get_attribute('append').get_ref().get_value() append_value.func.vcall( local_field.module, body_graph, local_field.get_attribute(internal_list_id).get_ref(), finput, lineprop) value_inputs = values.get_inputs() value_outputs = values.get_outputs() values.pop_history() inputs = [] outputs = [] # default input for subgraph's input body_graph.add_input_value(counter_value) body_graph.add_input_value(cond_value) body_graph.add_input_value(iter_value) # default output for subgraph's output body_graph.add_output_value(cond_value) body_graph.add_output_value(iter_value) # default output outputs.append(functions.generate_value_with_same_type(iter_value)) # generate pairs value_pairs = {} for v in value_inputs: key = str(v.field.id) + '_' + v.name if not (key in value_pairs.keys()): value_pairs[key] = {} value_pairs[key]['field'] = v.field value_pairs[key]['name'] = v.name value_pairs[key]['input_value'] = v.input_value value_pairs[key]['input_body_value'] = v.value for v in value_outputs: key = str(v.field.id) + '_' + v.name if not (key in value_pairs.keys()): value_pairs[key] = {} value_pairs[key]['field'] = v.field value_pairs[key]['name'] = v.name value_pairs[key]['output_body_value'] = v.value # remove iterator removed_name = str(local_field.id) + '_' + target_value.name del value_pairs[removed_name] for k, v in value_pairs.items(): name = v['name'] field = v['field'] if 'input_body_value' in v: inputs.append(v['input_value']) body_graph.add_input_value(v['input_body_value']) else: temp_value1 = functions.generate_value_with_same_type( v['output_body_value']) temp_value2 = functions.generate_value_with_same_type( v['output_body_value']) inputs.append(temp_value1) body_graph.add_input_value(temp_value2) if 'output_body_value' in v: body_graph.add_output_value(v['output_body_value']) output_value = functions.generate_value_with_same_type( v['output_body_value']) outputs.append(output_value) if field.get_attribute(name).has_obj(): field.get_attribute(name).get_ref().revise(output_value) else: field.get_attribute(name).revise(values.ValueRef(output_value)) else: temp_value1 = v['input_body_value'] temp_value2 = functions.generate_value_with_same_type( v['input_body_value']) body_graph.add_output_value(temp_value1) outputs.append(temp_value2) node = nodes.NodeListcomp(iter_value, inputs, body_graph, astc.lineno) node.set_outputs(outputs) graph.add_node(node) return local_field.get_attribute(internal_list_id).get_ref()
def convert_model(model: 'chainer.Chain', args=[]): # reset values values.reset_field_and_attributes() utils.reset_guid() values.instance_converters.clear() def instance_converter(m, i): if links_builtin.is_builtin_chainer_link(i): return links_builtin.ChainerLinkInstance(m, i) return None values.instance_converters.append(instance_converter) # generate default module default_module = values.Module(sys.modules[model.__module__]) # chainer chainer_module_name = get_module_name( C, default_module.internal_module) if chainer_module_name != '': c_dict = values.ValueRef(values.ModuleValue()) # a substitute of Variable c_variable = values.FuncValue(functions_ndarray.NDArrayFunction(), None) c_dict.get_field().get_attribute('Variable').revise(values.ValueRef(c_variable)) default_module.set_default_value(chainer_module_name, c_dict) # chainer.functions chainer_functions_module_name = get_module_name( F, default_module.internal_module) if chainer_functions_module_name != '': f_dict = values.ValueRef(values.ModuleValue()) def add_chainer_funtion(name:'str', func, ret_value_func = None): if ret_value_func is None: f = values.FuncValue( functions_builtin.ChainerFunction(func), None) else: f = values.FuncValue( functions_builtin.ChainerFunction(func, ret_value_func=ret_value_func), None) f_dict.get_field().get_attribute(name).revise(values.ValueRef(f)) values.function_converters[func] = f def ret_tuple(): return values.TupleValue() add_chainer_funtion('relu', F.relu) add_chainer_funtion('softmax', F.softmax) add_chainer_funtion('softmax_cross_entropy', F.softmax_cross_entropy) add_chainer_funtion('pad_sequence', F.pad_sequence) add_chainer_funtion('average_pooling_2d', F.average_pooling_2d) add_chainer_funtion('unpooling_2d', F.unpooling_2d) add_chainer_funtion('reshape', F.reshape) add_chainer_funtion('split_axis', F.split_axis, ret_value_func=ret_tuple) add_chainer_funtion('reshape', F.reshape) add_chainer_funtion('swapaxes', F.swapaxes) add_chainer_funtion('dropout', F.dropout) add_chainer_funtion('concat', F.concat) add_chainer_funtion('matmul', F.matmul) add_chainer_funtion('max_pooling_2d', F.max_pooling_2d) add_chainer_funtion('resize_images', F.resize_images) if int(chainer.__version__[0]) >= 6: add_chainer_funtion('roi_max_pooling_2d', F.roi_max_pooling_2d) add_chainer_funtion('roi_average_pooling_2d', F.roi_average_pooling_2d) add_chainer_funtion('roi_max_align_2d', F.roi_max_align_2d) add_chainer_funtion('roi_average_align_2d', F.roi_average_align_2d) default_module.set_default_value(chainer_functions_module_name, f_dict) # numpy numpy_module_name = get_module_name(np, default_module.internal_module) if numpy_module_name != '': f_dict = values.ValueRef(values.ModuleValue()) f_array = values.FuncValue(functions_ndarray.NDArrayFunction(), None) f_dict.get_field().get_attribute('array').revise(values.ValueRef(f_array)) f_zeros = values.FuncValue(functions_ndarray.NDArrayZerosFunction(), None) f_dict.get_field().get_attribute('zeros').revise(values.ValueRef(f_zeros)) f_full = values.FuncValue(functions_ndarray.NDArrayFullFunction(), None) f_dict.get_field().get_attribute('full').revise(values.ValueRef(f_full)) f_ceil = values.FuncValue(functions_ndarray.NDArrayCeilFunction(), None) f_dict.get_field().get_attribute('ceil').revise(values.ValueRef(f_ceil)) f_dict.get_field().get_attribute('int32').revise( values.ValueRef(values.NumberValue(utils.numpy_type_2_int(np.int32)))) f_dict.get_field().get_attribute('float32').revise( values.ValueRef(values.NumberValue(utils.numpy_type_2_int(np.float32)))) default_module.set_default_value(numpy_module_name, f_dict) m_range = values.FuncValue(functions_builtin.RangeFunction(), None) default_module.set_default_value('range', values.ValueRef(m_range)) m_list = values.FuncValue(functions_builtin.ListFunction(), None) default_module.set_default_value('list', values.ValueRef(m_list)) model_inst = values.parse_instance(default_module, '', model) forward_func = model_inst.try_get_and_store_obj('forward') # convert args finput = functions.FunctionArgInput() value_args = [] ind = 0 node_input = nodes.NodeInput('input') for arg in args: varg = values.parse_instance(default_module, '', arg, None, True) varg.name = 'in_' + str(ind) varg.get_value().name = 'in_' + str(ind) # make value unknown # if isinstance(varg.get_value(), values.TupleValue): # for i in range(len(varg.get_value().internal_value)): # varg.get_value().internal_value[i] = None # else: varg.get_value().internal_value = None finput.inputs.append(varg) value_args.append(varg.get_value()) ind += 1 node_input.set_outputs(value_args) graph = Graph() graph.add_node(node_input) forward_func_value = forward_func.get_value() ret = forward_func_value.func.vcall( default_module, graph, forward_func_value.obj, finput) assert(ret is None or isinstance(ret, values.ValueRef)) def try_get_value(value) -> 'values.Value': if isinstance(value, values.Value): return value if isinstance(value, values.ValueRef): return value.get_value() if isinstance(value, values.Attribute): return value.get_ref().get_value() if ret is None or isinstance(ret, values.NoneValue): if config.show_warnings: print('Failed to compile. output is None.') return (value_args, None, graph) ret_ = [] if isinstance(ret.get_value(), values.TupleValue): if ret.get_value().internal_value is not None: for v in ret.get_value().internal_value: assert(v is not None) ret_.append(try_get_value(v)) else: ret_ = [ret.get_value()] elif isinstance(ret, list): ret_ = [r.get_value() for r in ret] else: ret_ = [ret.get_value()] for v in value_args: graph.add_input_value(v) for v in ret_: graph.add_output_value(v) return (value_args, ret_, graph)
def convert_model(model: 'chainer.Chain', args=[]): # reset values values.reset_field_and_attributes() utils.reset_guid() # generate default module default_module = values.Module(sys.modules[model.__module__]) # chainer.functions chainer_functions_module_name = get_module_name( F, default_module.internal_module) if chainer_functions_module_name != '': f_dict = values.Object(values.ModuleValue()) f_relu = values.FuncValue(functions_builtin.ChainerFunction(F.relu), None) f_dict.get_field().get_attribute('relu').revise(values.Object(f_relu)) f_softmax = values.FuncValue( functions_builtin.ChainerFunction(F.softmax), None) f_dict.get_field().get_attribute('softmax').revise( values.Object(f_softmax)) f_softmax_cross_entropy = values.FuncValue( functions_builtin.ChainerFunction(F.softmax_cross_entropy), None) f_dict.get_field().get_attribute('softmax_cross_entropy').revise( values.Object(f_softmax_cross_entropy)) f_pad_sequence = values.FuncValue( functions_builtin.ChainerFunction(F.pad_sequence), None) f_dict.get_field().get_attribute('pad_sequence').revise( values.Object(f_pad_sequence)) default_module.set_default_value(chainer_functions_module_name, f_dict) # numpy numpy_module_name = get_module_name(np, default_module.internal_module) if numpy_module_name != '': f_dict = values.Object(values.ModuleValue()) f_array = values.FuncValue(functions_builtin.NDArrayFunction(), None) f_dict.get_field().get_attribute('array').revise( values.Object(f_array)) f_dict.get_field().get_attribute('int32').revise( values.Object(values.NumberValue(utils.numpy_type_2_int( np.int32)))) f_dict.get_field().get_attribute('float32').revise( values.Object( values.NumberValue(utils.numpy_type_2_int(np.float32)))) default_module.set_default_value(numpy_module_name, f_dict) m_range = values.FuncValue(functions_builtin.RangeFunction(), None) default_module.set_default_value('range', values.Object(m_range)) m_list = values.FuncValue(functions_builtin.ListFunction(), None) default_module.set_default_value('list', values.Object(m_list)) model_inst = values.parse_instance(default_module, '', model) forward_func = model_inst.try_get_and_store_obj('forward') # convert args finput = functions.FunctionArgInput() value_args = [] ind = 0 for arg in args: varg = values.parse_instance(default_module, '', arg, None, True) varg.name = 'in_' + str(ind) varg.get_value().name = 'in_' + str(ind) finput.inputs.append(varg) value_args.append(varg.get_value()) ind += 1 graph = Graph() forward_func_value = forward_func.get_value() ret = forward_func_value.func.vcall(default_module, graph, forward_func_value.obj, finput) assert (ret is None or isinstance(ret, values.Object)) ret_ = [] if isinstance(ret.get_value(), values.TupleValue): ret_.extend([v.get_obj().get_value() for v in ret.get_value().values]) elif isinstance(ret, list): ret_ = [r.get_value() for r in ret] else: ret_ = [ret.get_value()] for v in value_args: graph.add_input_value(v) for v in ret_: graph.add_output_value(v) return (value_args, ret_, graph)