def vcall(self,
              module: 'values.Field',
              graph: 'graphs.Graph',
              inst: 'values.Object',
              args: 'functions.FunctionArgInput',
              context: 'functions.VEvalContext' = None,
              line=-1):
        assert (inst is None)

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

        dtype_value = vargs[1]
        if isinstance(dtype_value, values.StrValue):
            if not dtype_value.has_constant_value():
                utils.print_error('Failed to get dtype str ', line)
                return None

            dtype = utils.str_2_dtype(dtype_value.get_constant_value())

        elif dtype_value is not None and not isinstance(
                dtype_value, values.NoneValue):
            # TODO : make better
            dtype = np.array(1, dtype=dtype_value.func.dtype).dtype
        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.Object(value)
Esempio n. 2
0
    def vcall(self,
              module: 'values.Field',
              graph: 'graphs.Graph',
              inst: 'values.Object',
              args: 'functions.FunctionArgInput',
              context: 'functions.VEvalContext' = None,
              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)

            if vargs[0].has_constant_value():
                refs = []
                for attr_or_ref in vargs[0].internal_value:
                    refs.append(
                        utils.try_get_obj(attr_or_ref, 'list',
                                          utils.LineProperty()))

                value.internal_value = refs

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

        if inst.in_container:
            raise Exception('Invalid operation')

        key_hashes = inst.get_value().internal_keys.keys()
        attributes = inst.get_value().internal_values
        vargs = []
        vargs_ref = []
        for hash in key_hashes:
            varg = attributes.get_attribute(hash)
            if varg.has_obj():
                vargs.append(utils.try_get_obj(varg, 'dict_values', utils.LineProperty()).get_value())
                vargs_ref.append(utils.try_get_obj(varg, 'dict_values', utils.LineProperty()))
            else:
                assert(False)

        node = nodes.NodeGenerate('List', vargs , line)
        graph.add_node(node)

        value = values.ListValue(vargs_ref)
        value.name = '@F.{}.{}'.format(line, self.name)
        node.set_outputs([value])
        return value
Esempio n. 4
0
    def vcall(self,
              module: 'values.Field',
              graph: 'graphs.Graph',
              inst: 'values.Object',
              args: 'functions.FunctionArgInput',
              context: 'functions.VEvalContext' = None,
              line=-1):

        if context._for_unroll:
            for ref in args.inputs:
                if not ref.get_value().has_constant_value():
                    assert False, 'Loop unrolling was requested for non-constant sequence at %s' % line

            refs = []
            for num in range(*(ref.get_value().internal_value
                               for ref in args.inputs)):
                refs.append(values.Object(values.NumberValue(num)))

            value = values.ListValue(refs)
            return values.Object(value)
        else:
            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.Object(value)
Esempio n. 5
0
 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)
Esempio n. 6
0
    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)
    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 isinstance(dtype_value, values.StrValue):
            if not dtype_value.has_constant_value():
                utils.print_error('Failed to get dtype str ', line)
                return None

            if dtype_value.get_constant_value() == 'q':
                dtype = np.int64
            elif dtype_value.get_constant_value() == 'i':
                dtype = np.int32
            elif dtype_value.get_constant_value() == 'g':
                dtype = np.float64
            elif dtype_value.get_constant_value() == 'f':
                dtype = np.float32
            else:
                assert (False)
        elif dtype_value is not None and not isinstance(
                dtype_value, values.NoneValue):
            # TODO : make better
            dtype = np.array(1, dtype=dtype_value.func.dtype).dtype
        elif isinstance(vargs[0], values.TensorValue):
            dtype = vargs[0].dtype
        else:
            dtype = None

        node = nodes.NodeGenerate('array', 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)
Esempio n. 8
0
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, astc.filename)

    elts = []
    for elt in astc.nast.elts:
        elt_ = veval_ast(astc.c(elt), local_field, graph)
        elt_obj = try_get_ref(elt_, 'list', lineprop)
        elts.append(elt_obj)

    node = nodes.NodeGenerate('List', [elt.get_value() for elt in elts],
                              lineprop)
    graph.add_node(node)
    value = values.ListValue(elts)
    node.set_outputs([value])

    return values.ValueRef(value)
Esempio n. 9
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, astc.filename)

    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)

            if v_ is None:
                utils.print_warning('Unknown tuple element {}'.format(v),
                                    lineprop)
                return None

            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)
Esempio n. 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, astc.filename)

    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(lineprop))
    target_value = target_ref.get_value()

    node_forgen.set_outputs([target_ref.get_value()])
    local_field.get_attribute(target_name).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(
        None, 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
        value_pairs[key]['output_obj'] = v.obj

    # 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 'output_obj' in v:
                obj = v['output_obj']
                obj.revise(output_value)
                field.get_attribute(name).revise(obj)
            elif 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()
Esempio n. 11
0
def parse_instance(default_module,
                   name,
                   instance,
                   self_instance=None,
                   from_member=False,
                   root_graph: 'graphs.Graph' = None) -> "Object":

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

    #if inspect.ismethod(instance) or inspect.isfunction(instance) or isinstance(instance, np.ufunc):
    if isinstance(instance, collections.Hashable):
        if instance in function_converters.keys():
            func = function_converters[instance]
            return Object(func)

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

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

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

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

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

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

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

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

    if instance is inspect._empty:
        return None

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

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

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

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

    if isinstance(instance, dict):
        keys = []
        values = []
        for key, value in instance.items():
            keys.append(parse_instance(default_module, '', key))
            values.append(parse_instance(default_module, '', value))
        ret = DictValue(keys, values)
        return Object(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 Object(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 Object(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 Object(ret)

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

    if isinstance(instance, chainer.Variable):
        tensorValue = TensorValue(instance.data)
        tensorValue.value = instance.data
        tensorValue.shape = instance.data.shape
        return Object(tensorValue)

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

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

    if utils.is_disabled_module(instance):
        return None

    if inspect.ismodule(instance):
        value = ModuleValue(instance)
        return Object(value)

    module = Object(ModuleValue(sys.modules[instance.__module__]))
    model_inst = UserDefinedInstance(module, instance, None)
    return Object(model_inst)