def veval_ast_for(astc: 'AstContext', local_field: 'values.Field', graph: 'Graph'): ''' for target in iter: ... ''' assert (isinstance(astc.nast, gast.gast.For)) lineprop = utils.LineProperty(astc.lineno) # for target in iter: iter_ = veval_ast(astc.c(astc.nast.iter), local_field, graph) input_iter_value = try_get_value(iter_, 'for', lineprop) body_iter_value = functions.generate_value_with_same_type( input_iter_value, suffix_type=functions.SuffixType.Input) # get target name target_name = '' if isinstance(astc.nast.target, gast.gast.Name): target_name = astc.nast.target.id else: if config.show_warnings: print('This for is not supported. in L.{}'.format(astc.lineno)) return None # unroll? if isinstance(input_iter_value, values.ListValue) and input_iter_value.has_constant_value( ) and input_iter_value.dtype is None: return veval_ast_for_unroll(astc, target_name, input_iter_value, local_field, graph) for_guid = utils.get_guid() for_id = 'for_' + str(for_guid) body_id = 'body_' + str(for_guid) values.push_history(for_id) # body body_graph = Graph() body_graph.root_graph = graph.root_graph body_graph.name = 'Body_' + str(for_guid) # generate a node for input node_input = nodes.NodeInput('input') body_graph.add_node(node_input) body_counter_value = values.NumberValue(None) body_counter_value.dtype = np.array(0).dtype body_counter_value.name = 'for_counter_' + str(for_guid) body_cond_value = values.BoolValue(None) body_cond_value.name = 'for_cond_' + str(for_guid) # create a node to lookup a value from sequence node_forgen = nodes.NodeForGenerator(body_counter_value, body_iter_value) # generate iterator target_ref = input_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()]) target_attribute = local_field.get_attribute(target_name) target_attribute.revise(target_ref) body_graph.add_node(node_forgen) # veval body body = veval_ast(astc.c(astc.nast.body), local_field, body_graph) value_inputs = values.get_inputs() value_outputs = values.get_outputs() values.pop_history() inputs = [] outputs = [] node_input_outputs = [] # default input for subgraph's input body_graph.add_input_value(body_counter_value) body_graph.add_input_value(body_cond_value) body_graph.add_input_value(body_iter_value) # default output for subgraph's output body_graph.add_output_value(body_cond_value) body_graph.add_output_value(body_iter_value) # default output outputs.append(functions.generate_value_with_same_type(input_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 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'], is_dummy_value=True, suffix_type=functions.SuffixType.Dummy) temp_value2 = functions.generate_value_with_same_type( v['output_body_value'], suffix_type=functions.SuffixType.Dummy) inputs.append(temp_value1) body_graph.add_input_value(temp_value2) node_input_outputs.append(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.NodeFor(input_iter_value, inputs, body_graph, astc.lineno) node.set_outputs(outputs) node_input.set_outputs(node_input_outputs) graph.add_node(node) return None
def generate_value_with_same_type(value: 'values.Value', is_dummy_value=False, suffix_type=SuffixType.Unknown): assert (isinstance(value, values.Value)) ret = None if isinstance(value, values.TensorValue): ret = values.TensorValue() ret.shape = value.shape ret.dtype = value.dtype elif isinstance(value, values.NumberValue): dtype = None if value.internal_value is None: dtype = value.dtype elif isinstance(value.internal_value, int): dtype = np.array(value.internal_value).dtype elif isinstance(value.internal_value, float): dtype = np.array(value.internal_value).dtype ret = values.NumberValue(None) ret.dtype = dtype elif isinstance(value, values.StrValue): ret = values.StrValue(None) elif isinstance(value, values.BoolValue): ret = values.BoolValue(None) elif isinstance(value, values.ListValue): ret = values.ListValue(None) elif isinstance(value, values.NoneValue): ret = values.NoneValue() elif isinstance(value, values.TupleValue): ret = values.TupleValue() elif isinstance(value, values.RangeValue): ret = values.RangeValue() elif isinstance(value, values.UnknownValue): ret = values.UnknownValue() elif ret is None and isinstance(value, values.Value): ret = values.Value() else: assert (False) assert (ret is not None) ret.is_dummy_value = is_dummy_value if suffix_type == SuffixType.Unknown: ret.name = value.name + '_st' elif suffix_type == SuffixType.Unused: ret.name = value.name + '_unused' elif suffix_type == SuffixType.Dummy: ret.name = value.name + '_dummy' elif suffix_type == SuffixType.Input: ret.name = value.name + '_in' else: assert (False) return ret
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 generate_value_with_same_type(value: 'values.Value', has_default=False, suffix_type=SuffixType.Unknown): assert (isinstance(value, values.Value)) ret = None if isinstance(value, values.TensorValue): ret = values.TensorValue() ret.shape = value.shape ret.dtype = value.dtype if isinstance(value, values.NumberValue): dtype = None if value.internal_value is None: dtype = value.dtype elif isinstance(value.internal_value, int): dtype = np.array(value.internal_value).dtype elif isinstance(value.internal_value, float): dtype = np.array(value.internal_value).dtype if has_default: if dtype == np.array(0).dtype: ret = values.NumberValue(0) elif dtype == np.array(0.0).dtype: ret = values.NumberValue(0.0) else: ret = values.NumberValue(None) else: ret = values.NumberValue(None) ret.dtype = dtype if isinstance(value, values.StrValue): if has_default: ret = values.StrValue('') else: ret = values.StrValue(None) if isinstance(value, values.BoolValue): if has_default: ret = values.BoolValue(False) else: ret = values.BoolValue(None) if isinstance(value, values.ListValue): ret = values.ListValue(None) if isinstance(value, values.NoneValue): ret = values.NoneValue() if isinstance(value, values.TupleValue): ret = values.TupleValue() if isinstance(value, values.UnknownValue): ret = values.UnknownValue() if has_default: ret.internal_value = 0 if ret is None and isinstance(value, values.Value): ret = values.Value() if ret is not None: if suffix_type == SuffixType.Unknown: ret.name = value.name + '_st' if suffix_type == SuffixType.Unused: ret.name = value.name + '_unused' return ret