示例#1
0
 def vcall(self, module : 'Field', graph : 'Graph', inst : 'values.Object', args = [], line = -1):
     node = nodes.NodeGenerate('range', [v.obj.get_value() for v in args], line)
     graph.add_node(node)
     value = values.RangeValue()
     value.name = '@F.{}.{}'.format(line, self.name)
     node.set_outputs([value])
     return values.Object(value)
示例#2
0
def veval_ast_tuple(astc: 'AstContext',
                    local_field: 'values.Field',
                    graph: 'Graph',
                    option: 'VEvalOption' = None):
    assert (isinstance(astc.nast, gast.gast.Tuple))
    lineprop = utils.LineProperty(astc.lineno)

    if option is not None and option.eval_as_written_target:
        vs = []
        for v in astc.nast.elts:
            a_ = veval_ast(astc.c(v), local_field, graph, option=option)
            vs.append(a_)
        return vs
    else:
        vs_ref = []
        vs = []

        for v in astc.nast.elts:
            a_ = veval_ast(astc.c(v), local_field, graph, option=option)
            v_ = try_get_ref(a_, 'tuple', lineprop)
            vs_ref.append(v_)
            vs.append(v_.get_value())
            v_.in_container = True

        tuple_value = values.TupleValue(vs_ref)

        node = nodes.NodeGenerate('Tuple', vs, line=lineprop)
        node.set_outputs([tuple_value])
        graph.add_node(node)

        return values.ValueRef(tuple_value)
    def vcall(self,
              module: 'Field',
              graph: 'Graph',
              inst: 'values.ValueRef',
              args: 'functions.FunctionArgInput',
              line=-1):
        assert (inst is None)

        funcArgs = self.args.merge_inputs(inst, args)
        vargs = funcArgs.get_value().inputs

        dtype_value = vargs[1]
        if dtype_value is not None and not isinstance(dtype_value,
                                                      values.NoneValue):
            # TODO : make better
            dtype = utils.int_2_numpy_type(dtype_value.internal_value)
        else:
            dtype = np.array(vargs[1].internal_value).dtype

        node = nodes.NodeGenerate('zeros', funcArgs, line)
        graph.add_node(node)
        value = values.TensorValue()
        value.dtype = dtype
        value.name = '@F.{}.{}'.format(line, self.name)
        node.set_outputs([value])
        return values.ValueRef(value)
 def vcall(self, module: 'Field', graph: 'Graph', inst: 'values.ValueRef', args: 'functions.FunctionArgInput', line=-1):
     node = nodes.NodeGenerate(
         'range', [v.get_value() for v in args.inputs], line)
     graph.add_node(node)
     value = values.RangeValue()
     value.name = '@F.{}.{}'.format(line, self.name)
     node.set_outputs([value])
     return values.ValueRef(value)
 def vcall(self,
           module: 'values.Field',
           graph: 'core.Graph',
           inst: 'Value',
           args=[],
           line=-1):
     node = nodes.NodeGenerate('range', [v.value for v in args], line)
     graph.add_node(node)
     value = values.RangeValue()
     node.set_outputs([value])
     return value
示例#6
0
    def vcall(self, module : 'Field', graph : 'Graph', inst : 'values.Object', args = [], line = -1):
        funcArgs = self.parse_args(args)
        vargs = self.get_values(funcArgs)
        value = values.ListValue()

        if isinstance(vargs[0], values.NoneValue):
            node = nodes.NodeGenerate('List', [], line)
            graph.add_node(node)
        else:
            node = nodes.NodeConvert('List', vargs[0], line)
            graph.add_node(node)

        value.name = '@F.{}.{}'.format(line, self.name)
        node.set_outputs([value])
        return values.Object(value)
    def vcall(self, module: 'Field', graph: 'Graph', inst: 'values.ValueRef', args: 'functions.FunctionArgInput', line=-1):
        assert(inst is None)

        funcArgs = self.args.merge_inputs(inst, args)
        vargs = funcArgs.get_value().inputs
        value = values.ListValue()

        if isinstance(vargs[0], values.NoneValue):
            node = nodes.NodeGenerate('List', [], line)
            graph.add_node(node)
        else:
            node = nodes.NodeConvert('List', vargs[0], line)
            graph.add_node(node)

        value.name = '@F.{}.{}'.format(line, self.name)
        node.set_outputs([value])
        return values.ValueRef(value)
示例#8
0
    def vcall(self, module : 'Field', graph : 'Graph', inst : 'values.Object', args = [], line = -1):
        funcArgs = self.parse_args(args)
        vargs = self.get_values(funcArgs)

        dtype_value = vargs[1]
        if dtype_value is not None and not isinstance(dtype_value, values.NoneValue):
            # TODO : make better
            dtype = utils.int_2_numpy_type(dtype_value.internal_value)
        else:
            dtype = None

        node = nodes.NodeGenerate('array', vargs, line)
        graph.add_node(node)
        value = values.TensorValue()
        value.dtype = dtype
        value.name = '@F.{}.{}'.format(line, self.name)
        node.set_outputs([value])
        return values.Object(value)
def veval_ast_list(astc : 'AstContext', local_field : 'values.Field', graph : 'Graph'):
    assert(isinstance(astc.nast, gast.gast.List))
    '''
    Ex. [],[x,y,z]
    TODO : Initializer
    '''
    lineprop = utils.LineProperty(astc.lineno)

    elts = []
    for elt in astc.nast.elts:
        elt_ = veval_ast(astc.c(elt), local_field, graph)
        elt_value = elt_.get_value()
        elts.append(elt_value)

    node = nodes.NodeGenerate('List', elts, lineprop)
    graph.add_node(node)
    value = values.ListValue()
    value.values.extend(elts)

    node.set_outputs([value])
    return value
示例#10
0
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()
示例#11
0
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_iter_id = '@internal/iter_' + 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.Object(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(0)
    counter_value.name = 'listcomp_counter_' + listcomp_guid

    cond_value = values.BoolValue(True)
    cond_value.name = 'listcomp_cond_' + listcomp_guid

    body_field = values.Field()
    body_field.set_module(local_field.module)
    body_field.set_parent(local_field)

    # set iter with internal name
    body_field.get_attribute(internal_iter_id).revise(list_obj)

    values.commit(listcomp_id)

    body_graph = Graph()
    body_graph.name = 'Body_' + listcomp_guid

    node_forgen = nodes.NodeForGenerator(counter_value, iter_value)
    target_value = values.Value()
    target_obj = values.Object(target_value)
    node_forgen.set_outputs([target_value])

    body_field.get_attribute(target_name).revise(target_obj)

    body_graph.add_node(node_forgen)

    elt = veval_ast(astc.c(astc.nast.elt), body_field, body_graph)
    elt_obj = try_get_obj(elt, 'listcomp', lineprop)

    farg = functions.FunctionArg()
    farg.name = ''
    farg.obj = elt_obj
    append_value = list_obj.get_field().get_attribute(
        'append').get_obj().get_value()
    append_value.func.vcall(local_field.module, body_graph, list_obj, [farg],
                            lineprop)

    values.commit(body_id)

    body_input_attributes = get_input_attritubtes(
        body_field, listcomp_id, body_id) + get_input_attritubtes(
            local_field, listcomp_id, body_id)
    body_output_attributes = get_output_attritubtes(
        body_field, listcomp_id, body_id) + get_output_attritubtes(
            local_field, listcomp_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(body_field, listcomp_id,
                                         body_id) + get_output_objs(
                                             local_field, listcomp_id, body_id)

    changed_values = {}

    for obj in value_changed_objs:
        in_value = obj.get_value_log(listcomp_id)
        out_value = obj.get_value_log(body_id)
        changed_values[in_value] = (obj, out_value)

    output_attributes_2_values = {}

    for attribute in body_output_attributes:
        output_attributes_2_values[attribute] = attribute.get_obj().get_value()

    # get inputs
    values.checkout(listcomp_id)

    input_attributes_2_values = {}

    for attribute in body_input_attributes:
        input_attributes_2_values[attribute] = attribute.get_obj().get_value()

    # Exports
    values.checkout(listcomp_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] is not None and input_attributes_2_values[
                v[0]] in defaule_values:
            name_removing.append(k)

        if v[1] is not None and output_attributes_2_values[
                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(
                (attribute[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_obj_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_obj_value_out[1])
        value = functions.generate_value_with_same_type(
            changed_obj_value_out[1])
        changed_obj_value_out[0].revise(value)
        outputs.append(value)

    node = nodes.NodeListcomp(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)

    if body_field.get_attribute(internal_iter_id).has_obj():
        return body_field.get_attribute(internal_iter_id).get_obj()
    else:
        return list_obj
示例#12
0
def parse_instance(default_module, name, instance, self_instance=None, from_member = False, root_graph : 'graphs.Graph' = None) -> "ValueRef":

    for converter in instance_converters:
        ret = converter(default_module, instance)
        if ret is not None:
            return ValueRef(ret)

    if inspect.ismethod(instance) or inspect.isfunction(instance):
        if instance in function_converters.keys():
            func = function_converters[instance]
            return ValueRef(func)

    # need to check whether is value bool before check whether is value int
    if isinstance(instance, bool):
        return ValueRef(BoolValue(instance))

    if isinstance(instance, int):
        return ValueRef(NumberValue(instance))

    if isinstance(instance, np.int32):
        return ValueRef(NumberValue(instance))

    if isinstance(instance, np.int64):
        return ValueRef(NumberValue(instance))

    if isinstance(instance, float):
        return ValueRef(NumberValue(instance))

    if isinstance(instance, np.float32):
        return ValueRef(NumberValue(instance))

    if isinstance(instance, np.float64):
        return ValueRef(NumberValue(instance))

    if isinstance(instance, str):
        return ValueRef(StrValue(instance))

    if instance is inspect._empty:
        return None

    if inspect.ismethod(instance):
        func = UserDefinedFunction(instance)
        return ValueRef(FuncValue(func, self_instance))

    if inspect.isfunction(instance):
        func = UserDefinedFunction(instance)
        if from_member:
            return ValueRef(FuncValue(func, self_instance))
        else:
            return ValueRef(FuncValue(func, None))

    if inspect.isclass(instance):
        func = functions.UserDefinedClassConstructorFunction(instance)
        return ValueRef(FuncValue(func, None))

    if isinstance(instance, list):
        if root_graph is None:
            value_in_tuple = []
            for v in instance:
                o = parse_instance(default_module, '', v)
                value_in_tuple.append(o)
            ret = ListValue(value_in_tuple)
        else:
            value_in_tuple = []
            vs = []
            for v in instance:
                o = parse_instance(default_module, '', v)
                value_in_tuple.append(o)
                value = o.get_value()

                if isinstance(value, TupleValue):
                    assert(False)

                if isinstance(value, ListValue):
                    assert(False)

                vs.append(value)

            node = nodes.NodeGenerate('List', vs)
            ret = ListValue(value_in_tuple)
            node.set_outputs([ret])
            root_graph.add_initial_node(node)

        ret.estimate_type()
        return ValueRef(ret)

    if isinstance(instance, tuple) and 'Undefined' in instance:
        shape = list(instance)
        shape = -1 if shape == 'Undefined' else shape
        tensorValue = TensorValue()
        tensorValue.shape = tuple(shape)
        return ValueRef(tensorValue)

    if isinstance(instance, tuple):
        if root_graph is None:
            value_in_tuple = []
            for v in instance:
                o = parse_instance(default_module, '', v)
                value_in_tuple.append(o)

            return ValueRef(TupleValue(value_in_tuple))
        else:
            value_in_tuple = []
            vs = []
            for v in instance:
                o = parse_instance(default_module, '', v)
                value_in_tuple.append(o)
                value = o.get_value()

                if isinstance(value, TupleValue):
                    assert(False)

                if isinstance(value, ListValue):
                    assert(False)

                vs.append(value)

            node = nodes.NodeGenerate('Tuple', vs)
            ret = TupleValue(value_in_tuple)
            node.set_outputs([ret])
            root_graph.add_initial_node(node)
            return ValueRef(ret)


    if isinstance(instance, np.ndarray):
        tensorValue = TensorValue(instance)
        tensorValue.value = instance
        tensorValue.shape = instance.shape
        return ValueRef(tensorValue)

    if instance == inspect._empty:
        return ValueRef(NoneValue())

    if instance is None:
        return ValueRef(NoneValue())

    model_inst = UserDefinedInstance(default_module, instance, None)
    return ValueRef(model_inst)
示例#13
0
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_id = 'listcomp_' + str(utils.get_guid())
    body_id = 'listcomp_body_' + str(utils.get_guid())

    values.commit(listcomp_id)

    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()

    node_generate_list = nodes.NodeGenerate('List', [], lineprop)
    graph.add_node(node_generate_list)
    node_generate_list.set_outputs([list_value])

    # 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(0)
    counter_value.name = 'for_counter'

    node_forgen = nodes.NodeForGenerator(counter_value, iter_value)
    target_value = values.Value()
    node_forgen.set_outputs([target_value])

    body_field = values.Field()
    body_field.set_module(local_field.module)
    body_field.set_parent(local_field)
    body_field.get_attribute(target_name).revise(target_value)
    body_graph = Graph()
    body_graph.name = 'Body'

    body_graph.add_node(node_forgen)

    elt = veval_ast(astc.c(astc.nast.elt), body_field, body_graph)
    farg = functions.FunctionArg()
    farg.name = ''
    farg.value = elt
    list_value.append_func.func.vcall(local_field.module, body_graph,
                                      list_value, [farg], lineprop)

    values.commit(body_id)

    body_output_attributes = get_input_attritubtes(body_field, listcomp_id,
                                                   body_id)
    body_intput_attributes = get_output_attritubtes(body_field, listcomp_id,
                                                    body_id)

    # Exports
    values.checkout(listcomp_id)

    input_attributes = set(body_intput_attributes)
    inputs = [i.get_value() for i in input_attributes]

    output_attributes = set(body_output_attributes)
    outputs = []

    for attribute in output_attributes:
        value = values.Value()
        outputs.append(value)
        attribute.revise(value)

    node = nodes.NodeListcomp(iter_value, inputs, body_graph, astc.lineno)
    node.set_outputs(outputs)

    graph.add_node(node)

    # compare

    return list_value