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 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) # 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 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.name = 'Body_' + str(for_guid) counter_value = values.NumberValue(0) counter_value.name = 'for_counter_' + str(for_guid) cond_value = values.BoolValue(True) cond_value.name = 'for_cond_' + str(for_guid) iter_value = try_get_value(iter_, 'for', lineprop) # create a node to lookup a value from sequence node_forgen = nodes.NodeForGenerator(counter_value, iter_value) # estimate type # TODO : more types if isinstance(iter_value, values.RangeValue): target_value = values.NumberValue(None) target_value.dtype = np.array(0).dtype else: target_value = values.Value() target_obj = values.Object(target_value) node_forgen.set_outputs([target_value]) target_attribute = local_field.get_attribute(target_name) target_attribute.revise(target_obj) 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 = [] # 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 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_obj().revise(output_value) else: field.get_attribute(name).revise(values.Object(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(iter_value, inputs, body_graph, astc.lineno) node.set_outputs(outputs) graph.add_node(node) return None
def veval_ast_for(astc : 'AstContext', local_field : 'values.Field', graph : 'Graph'): ''' for target in iter: ... ''' assert(isinstance(astc.nast, gast.gast.For)) # for target in iter: iter_ = veval_ast(astc.c(astc.nast.iter), local_field, graph) # 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 for_id = 'for_' + str(utils.get_guid()) body_id = 'body_' + str(utils.get_guid()) values.commit(for_id) # Body body_graph = Graph() body_graph.name = 'Body' counter_value = values.NumberValue(0) counter_value.name = 'for_counter' cond_value = values.BoolValue(True) cond_value.name = 'for_cond' iter_value = iter_.get_value() # node to lookup a value from sequence node_forgen = nodes.NodeForGenerator(counter_value, iter_value) target_value = values.Value() node_forgen.set_outputs([target_value]) target_attribute = local_field.get_attribute(target_name) target_attribute.revise(target_value) body_graph.add_node(node_forgen) body = veval_ast(astc.c(astc.nast.body), local_field, body_graph) values.commit(body_id) body_input_attributes = get_input_attritubtes(local_field, for_id, body_id) body_output_attributes = get_output_attritubtes(local_field, for_id, body_id) input_attributes_2_values = {} for attribute in body_input_attributes: input_attributes_2_values[attribute] = attribute.get_value() output_attributes_2_values = {} for attribute in body_output_attributes: output_attributes_2_values[attribute] = attribute.get_value() # Exports values.checkout(for_id) # generate attribute pairs name2attributes = {} for attribute in body_input_attributes: key = str(attribute.parent.id) + '_' + attribute.name if key in name2attributes.keys(): name2attributes[key][0] = attribute else: name2attributes[key] = [attribute, None] for attribute in body_output_attributes: key = str(attribute.parent.id) + '_' + attribute.name if key in name2attributes.keys(): name2attributes[key][1] = attribute else: name2attributes[key] = [None, attribute] # remove defaule values name_removing = [] defaule_values = [counter_value, cond_value, iter_value] for k, v in name2attributes.items(): if v[0] in defaule_values: name_removing.append(k) if v[1] in defaule_values: name_removing.append(k) for nr in name_removing: if nr in name2attributes.keys(): name2attributes.pop(nr) # inputs = [] outputs = [] non_volatiles = [] # 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 subgrap'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)) for attributes in name2attributes.values(): name = '' parent = None input_value = None output_value = None if attributes[0] is not None: name = attributes[0].name parent = attributes[0].parent if attributes[1] is not None: name = attributes[1].name parent = attributes[1].parent if attributes[0] is not None: input_value = input_attributes_2_values[attributes[0]] else: # value with same type input_value = functions.generate_value_with_same_type(output_attributes_2_values[attributes[1]]) if attributes[1] is not None: output_value = output_attributes_2_values[attributes[1]] else: # copy value output_value = input_attributes_2_values[attributes[0]] output_value_in_node = functions.generate_value_with_same_type(output_value) inputs.append(input_value) outputs.append(output_value_in_node) body_graph.add_input_value(input_value) body_graph.add_output_value(output_value) if attributes[1].is_non_volatile: non_volatiles.append((attribute[1].initial_value,output_value_in_node)) attributes[1].parent.get_attribute(name).revise(output_value_in_node) node = nodes.NodeFor(iter_value, inputs, body_graph, astc.lineno) node.set_outputs(outputs) graph.add_node(node) # add non-volatiles for tv, v in non_volatiles: node_nv = nodes.NodeNonVolatileAssign(tv, v) graph.add_node(node_nv) return None
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) # 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 for_id = 'for_' + str(utils.get_guid()) body_id = 'body_' + str(utils.get_guid()) values.commit(for_id) # body body_graph = Graph() body_graph.name = 'Body' counter_value = values.NumberValue(0) counter_value.name = 'for_counter' cond_value = values.BoolValue(True) cond_value.name = 'for_cond' iter_value = try_get_value(iter_, 'for', lineprop) # create a node to lookup a value from sequence node_forgen = nodes.NodeForGenerator(counter_value, iter_value) # estimate type # TODO : more types if isinstance(iter_value, values.RangeValue): target_value = values.NumberValue(None) target_value.dtype = np.array(0).dtype else: target_value = values.Value() target_obj = values.Object(target_value) node_forgen.set_outputs([target_value]) target_attribute = local_field.get_attribute(target_name) target_attribute.revise(target_obj) body_graph.add_node(node_forgen) # veval body body = veval_ast(astc.c(astc.nast.body), local_field, body_graph) values.commit(body_id) # get changed attributes body_input_attributes = get_input_attritubtes(local_field, for_id, body_id) body_output_attributes = get_output_attritubtes(local_field, for_id, body_id) body_input_attributes = filter_attributes(body_input_attributes) body_output_attributes = filter_attributes(body_output_attributes) # get objects whose values are changed value_changed_objs = get_output_objs(local_field, for_id, body_id) changed_values = {} for obj in value_changed_objs: in_value = obj.get_value_log(for_id) out_value = obj.get_value_log(body_id) changed_values[in_value] = out_value # get outputs output_attributes_2_values = {} for attribute in body_output_attributes: output_attributes_2_values[attribute] = attribute.get_obj().get_value() # get inputs values.checkout(for_id) input_attributes_2_values = {} for attribute in body_input_attributes: input_attributes_2_values[attribute] = attribute.get_obj().get_value() # export values.checkout(for_id) # generate attribute pairs name2attributes = {} for attribute in body_input_attributes: key = str(attribute.parent.id) + '_' + attribute.name if key in name2attributes.keys(): name2attributes[key][0] = attribute else: name2attributes[key] = [attribute, None] for attribute in body_output_attributes: key = str(attribute.parent.id) + '_' + attribute.name if key in name2attributes.keys(): name2attributes[key][1] = attribute else: name2attributes[key] = [None, attribute] # remove defaule values name_removing = [] defaule_values = [counter_value, cond_value, iter_value] for k, v in name2attributes.items(): if v[0] in defaule_values: name_removing.append(k) if v[1] in defaule_values: name_removing.append(k) for nr in name_removing: if nr in name2attributes.keys(): name2attributes.pop(nr) # inputs = [] outputs = [] non_volatiles = [] # 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 subgrap'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)) for attributes in name2attributes.values(): name = '' parent = None input_value = None output_value = None if attributes[0] is not None: name = attributes[0].name parent = attributes[0].parent if attributes[1] is not None: name = attributes[1].name parent = attributes[1].parent if attributes[0] is not None: input_value = input_attributes_2_values[attributes[0]] else: # value with same type input_value = functions.generate_value_with_same_type( output_attributes_2_values[attributes[1]]) if attributes[1] is not None: output_value = output_attributes_2_values[attributes[1]] else: if input_attributes_2_values[ attributes[0]] in changed_values.keys(): # change only values output_value = changed_values[input_attributes_2_values[ attributes[0]]] else: # copy value output_value = input_attributes_2_values[attributes[0]] output_value_in_node = functions.generate_value_with_same_type( output_value) inputs.append(input_value) outputs.append(output_value_in_node) body_graph.add_input_value(input_value) body_graph.add_output_value(output_value) if attributes[1] is not None and attributes[1].is_non_volatile: non_volatiles.append( (attributes[1].initial_obj.get_value(), output_value_in_node)) output_obj_in_node = values.Object(output_value_in_node) parent.get_attribute(name).revise(output_obj_in_node) for changed_value_in, changed_value_out in changed_values.items(): if changed_value_in is None: continue if changed_value_in in inputs: continue inputs.append(changed_value_in) body_graph.add_input_value(changed_value_in) body_graph.add_output_value(changed_value_out) value = functions.generate_value_with_same_type(changed_value_out) obj.revise(value) outputs.append(value) node = nodes.NodeFor(iter_value, inputs, body_graph, astc.lineno) node.set_outputs(outputs) graph.add_node(node) # add non-volatiles for tv, v in non_volatiles: node_nv = nodes.NodeNonVolatileAssign(tv, v) graph.add_node(node_nv) return None
def veval_ast_for(astc: 'AstContext', local_field: 'values.Field', graph: 'Graph'): ''' for target in iter: ... ''' assert (isinstance(astc.nast, gast.gast.For)) # for target in iter: iter_ = veval_ast(astc.c(astc.nast.iter), local_field, graph) # 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 for_id = 'for_' + str(utils.get_guid()) body_id = 'body_' + str(utils.get_guid()) values.commit(for_id) # Body body_graph = Graph() body_graph.name = 'Body' counter_value = values.NumberValue(0) counter_value.name = 'for_counter' cond_value = values.BoolValue(True) cond_value.name = 'for_cond' iter_value = iter_.get_value() node_forgen = nodes.NodeForGenerator(counter_value, iter_value) target_value = values.Value() node_forgen.set_outputs([target_value]) target_attribute = local_field.get_attribute(target_name) target_attribute.revise(target_value) body_graph.add_node(node_forgen) body = veval_ast(astc.c(astc.nast.body), local_field, body_graph) values.commit(body_id) body_output_attributes = get_input_attritubtes(local_field, for_id, body_id) body_intput_attributes = get_output_attritubtes(local_field, for_id, body_id) input_attributes_2_values = {} for attribute in body_intput_attributes: input_attributes_2_values[attribute] = attribute.get_value() output_attributes_2_values = {} for attribute in body_output_attributes: output_attributes_2_values[attribute] = attribute.get_value() # Exports values.checkout(for_id) input_attributes = set(body_intput_attributes) inputs = [i.get_value() for i in input_attributes if i.has_value()] output_attributes = body_output_attributes output_attributes.remove(target_attribute) output_attributes = [target_attribute] + output_attributes outputs = [] non_volatiles = [] # default output body_graph.add_output_value(cond_value) body_graph.add_output_value(iter_value) for attribute in output_attributes: name = attribute.name body_graph.add_output_value(output_attributes_2_values[attribute]) value = functions.generate_value_with_same_type( output_attributes_2_values[attribute]) outputs.append(value) if attribute.is_non_volatile: non_volatiles.append((attribute.initial_value, value)) attribute.parent.get_attribute(name).revise(value) # default input ''' body_graph.add_input_value(counter_value) body_graph.add_input_value(cond_value) body_graph.add_input_value(iter_value) inputs.remove(counter_value) inputs.remove(cond_value) inputs.remove(iter_value) ''' for input in inputs: body_graph.add_input_value(input) node = nodes.NodeFor(iter_value, inputs, body_graph, astc.lineno) node.set_outputs(outputs) graph.add_node(node) # add non-volatiles for tv, v in non_volatiles: node_nv = nodes.NodeNonVolatileAssign(tv, v) graph.add_node(node_nv) return None