コード例 #1
0
def generate_value_with_same_type(value: 'values.Value'):
    assert (isinstance(value, values.Value))
    ret = None
    if isinstance(value, values.TensorValue):
        ret = values.TensorValue()
        ret.shape = value.shape

    if isinstance(value, values.NumberValue):
        ret = values.NumberValue(None)

    if isinstance(value, values.StrValue):
        ret = values.StrValue(None)

    if isinstance(value, values.BoolValue):
        ret = values.BoolValue(None)

    if isinstance(value, values.ListValue):
        ret = values.ListValue(None)

    if isinstance(value, values.NoneValue):
        ret = values.NoneValue()

    if ret is None and isinstance(value, values.Value):
        ret = values.Value()

    if ret is not None:
        ret.name = value.name + '_st'

    return ret
コード例 #2
0
    def __init__(self):
        super().__init__()
        self.name = 'array'

        fa = functions.FunctionArg()
        fa.name = 'object'
        fa.obj = values.Object(values.NoneValue())
        self.funcArgs.append(fa)

        fa = functions.FunctionArg()
        fa.name = 'dtype'
        fa.obj = values.Object(values.NoneValue())
        self.funcArgs.append(fa)

        fa = functions.FunctionArg()
        fa.name = 'copy'
        fa.obj = values.Object(values.BoolValue(True))
        self.funcArgs.append(fa)

        fa = functions.FunctionArg()
        fa.name = 'order'
        fa.obj = values.Object(values.StrValue('K'))
        self.funcArgs.append(fa)

        fa = functions.FunctionArg()
        fa.name = 'subok'
        fa.obj = values.Object(values.BoolValue(False))
        self.funcArgs.append(fa)

        fa = functions.FunctionArg()
        fa.name = 'ndmin'
        fa.obj = values.Object(values.NumberValue(0))
        self.funcArgs.append(fa)
コード例 #3
0
 def veval_with_default(nast, default_value):
     if nast is None:
         ret = values.NumberValue(default_value)
         ret.name = '@SliceDefault'
         return ret
     obj = veval_ast(astc.c(nast), local_field, graph)
     return try_get_value(obj, 'subscript', lineprop)
コード例 #4
0
def generate_value_with_same_type(value: 'values.Value'):
    assert (isinstance(value, values.Value))
    ret = None
    if isinstance(value, values.TensorValue):
        ret = values.TensorValue()
        ret.shape = value.shape

    if isinstance(value, values.NumberValue):
        ret = values.NumberValue(None)
        if value.internal_value is None:
            ret.dtype = value.dtype
        elif isinstance(value.internal_value, int):
            ret.dtype = np.array(value.internal_value).dtype
        elif isinstance(value.internal_value, float):
            ret.dtype = np.array(value.internal_value).dtype

    if isinstance(value, values.StrValue):
        ret = values.StrValue(None)

    if isinstance(value, values.BoolValue):
        ret = values.BoolValue(None)

    if isinstance(value, values.ListValue):
        ret = values.ListValue(None)

    if isinstance(value, values.NoneValue):
        ret = values.NoneValue()

    if ret is None and isinstance(value, values.Value):
        ret = values.Value()

    if ret is not None:
        ret.name = value.name + '_st'

    return ret
コード例 #5
0
def generate_copied_value(value : 'values.Value'):
    if isinstance(value, values.NumberValue):
        copied = values.NumberValue(value.internal_value)
        return copied

    if isinstance(value, values.TensorValue):
        copied = values.TensorValue()
        copied.shape = value.shape
        return copied

    if isinstance(value, values.NoneValue):
        copied = values.NoneValue()
        return copied

    if isinstance(value, values.BoolValue):
        copied = values.BoolValue(value.internal_value)
        return copied

    if isinstance(value, values.StrValue):
        copied = values.StrValue(value.internal_value)
        return copied

    if isinstance(value, values.RangeValue):
        copied = values.RangeValue()
        return copied

    if isinstance(value, values.TupleValue):
        copied = values.TupleValue(value.values)
        return copied

    if config.show_warnings:
        print('Unknown type {} is copied'.format(value))

    return values.Value()
コード例 #6
0
def generate_value_with_same_type(value : 'values.Value'):
    if isinstance(value, values.TensorValue):
        ret = values.TensorValue()
        ret.shape = value.shape
        return ret

    if isinstance(value, values.NumberValue):
        ret = values.NumberValue(None)
        return ret

    if isinstance(value, values.StrValue):
        ret = values.StrValue(None)
        return ret

    if isinstance(value, values.BoolValue):
        ret = values.BoolValue(None)
        return ret

    if isinstance(value, values.ListValue):
        ret = values.ListValue(None)
        return ret

    if isinstance(value, values.NoneValue):
        ret = values.NoneValue()
        return ret

    if isinstance(value, values.Value):
        ret = values.Value()
        return ret

    return None
コード例 #7
0
def veval_ast_num(astc : 'AstContext', local_field : 'values.Field', graph : 'Graph'):
    '''
    Ex. 1, 2, ...
    '''
    assert(isinstance(astc.nast, gast.gast.Num))
    lineprop = utils.LineProperty(astc.lineno)
    value = values.NumberValue(astc.nast.n)
    return value
コード例 #8
0
    def __init__(self):
        super().__init__()
        self.name = 'array'

        self.args.add_arg('object', values.NoneValue())
        self.args.add_arg('dtype', values.NoneValue())
        self.args.add_arg('copy', values.BoolValue(True))
        self.args.add_arg('order', values.StrValue('K'))
        self.args.add_arg('subok', values.BoolValue(False))
        self.args.add_arg('ndmin', values.NumberValue(0))
コード例 #9
0
def veval(op: 'nodes.UnaryOpType', value: 'values.Value'):

    if isinstance(value, values.NumberValue):
        if value.internal_value is not None:
            if op == nodes.UnaryOpType.UAdd:
                return values.NumberValue(value.internal_value)
            if op == nodes.UnaryOpType.USub:
                return values.NumberValue(-value.internal_value)
            else:
                return functions.generate_value_with_same_type(value)
        else:
            return functions.generate_value_with_same_type(value)

    if isinstance(value, values.BoolValue):
        return functions.generate_value_with_same_type(value)

    if op == nodes.UnaryOpType.Not and isinstance(value, values.ListValue):
        return values.BoolValue(None)

    return values.Value()
コード例 #10
0
 def vcall(self,
           module: 'Field',
           graph: 'Graph',
           inst: 'values.ValueRef',
           args: 'functions.FunctionArgInput',
           line=-1):
     node = nodes.NodeLen(
         args.inputs[0].get_value(),  # TODO: Check this.
         line)
     graph.add_node(node)
     value = values.NumberValue(None)
     value.name = '@F.{}.{}'.format(line, self.name)
     node.set_outputs([value])
     return values.ValueRef(value)
コード例 #11
0
def veval_ast_num(astc: 'AstContext', local_field: 'values.Field',
                  graph: 'Graph'):
    '''
    Ex. 1, 2, ...
    '''
    assert (isinstance(astc.nast, gast.gast.Num))
    lineprop = utils.LineProperty(astc.lineno)
    value = values.NumberValue(astc.nast.n)
    ret = values.ValueRef(value)

    name = values.create_ref_value_name_with_constant(ret)
    ret.name = name
    ret.get_value().name = name
    return ret
コード例 #12
0
ファイル: functions.py プロジェクト: disktnk/chainer-compiler
def generate_copied_value(value: 'values.Value'):
    assert (isinstance(value, values.Value))

    if isinstance(value, values.NumberValue):
        copied = values.NumberValue(value.internal_value)
        copied.dtype = value.dtype
        return copied

    if isinstance(value, values.TensorValue):
        copied = values.TensorValue()
        copied.value = value.value
        copied.shape = value.shape
        copied.dtype = value.dtype
        return copied

    if isinstance(value, values.ListValue):
        copied = values.ListValue()
        copied.is_any = value.is_any
        if value.internal_value is not None:
            copied.internal_value = value.internal_value.copy()
        return copied

    if isinstance(value, values.NoneValue):
        copied = values.NoneValue()
        return copied

    if isinstance(value, values.BoolValue):
        copied = values.BoolValue(value.internal_value)
        return copied

    if isinstance(value, values.StrValue):
        copied = values.StrValue(value.internal_value)
        return copied

    if isinstance(value, values.RangeValue):
        copied = values.RangeValue()
        return copied

    if isinstance(value, values.TupleValue):
        if value.internal_value is not None:
            copied = values.TupleValue(value.internal_value.copy())
        else:
            copied = values.TupleValue(value.internal_value)
        return copied

    if config.show_warnings:
        print('Unknown type {} is copied'.format(value))

    return values.Value()
コード例 #13
0
def generate_copied_value(value : 'values.Value'):
    if isinstance(value, values.NumberValue):
        copied = values.NumberValue(value.internal_value)
        return copied

    if isinstance(value, values.TensorValue):
        copied = values.TensorValue()
        copied.shape = value.shape
        return copied

    if isinstance(value, values.NoneValue):
        copied = values.NoneValue()
        return copied

    if config.show_warnings:
        print('Warning : Unimplemented copied_value {}'.format(value))

    return values.Value()
コード例 #14
0
    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)
コード例 #15
0
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
コード例 #16
0
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
コード例 #17
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()
コード例 #18
0
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
コード例 #19
0
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.ReluFunction(), None)
        f_dict.get_field().get_attribute('relu').revise(values.Object(f_relu))
        f_softmax = values.FuncValue(functions_builtin.SoftmaxFunction(), None)
        f_dict.get_field().get_attribute('softmax').revise(
            values.Object(f_softmax))
        f_softmax_cross_entropy = values.FuncValue(
            functions_builtin.SoftmaxCrossEntropyFunction(), None)
        f_dict.get_field().get_attribute('softmax_cross_entropy').revise(
            values.Object(f_softmax_cross_entropy))
        f_pad_sequence = values.FuncValue(
            functions_builtin.PadSequenceFunction(), 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
    value_args = []
    function_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)
        farg = functions.FunctionArg()
        farg.obj = varg
        value_args.append(varg.get_value())
        function_args.append(farg)
        ind += 1

    graph = Graph()
    forward_func_value = forward_func.get_value()
    ret = forward_func_value.func.vcall(default_module, graph,
                                        forward_func_value.obj, function_args)
    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)
コード例 #20
0
ファイル: core.py プロジェクト: neutronest/chainer-compiler
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)
コード例 #21
0
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
コード例 #22
0
ファイル: functions.py プロジェクト: disktnk/chainer-compiler
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
コード例 #23
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
コード例 #24
0
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
コード例 #25
0
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
コード例 #26
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