示例#1
0
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()
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()
示例#3
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)
示例#4
0
def veval_ast_bin_op(astc: 'AstContext', local_field: 'values.Field',
                     graph: 'Graph'):
    """
    eval binary operation.
    Ex. a + b, b // c, etc
    """
    assert (isinstance(astc.nast, gast.gast.BinOp))
    lineprop = utils.LineProperty(astc.lineno)

    left = veval_ast(astc.c(astc.nast.left), local_field, graph)
    right = veval_ast(astc.c(astc.nast.right), local_field, graph)

    left_value = try_get_value(left, 'compare', lineprop)
    right_value = try_get_value(right, 'compare', lineprop)

    # TODO : adhoc code
    if isinstance(left_value, values.TupleValue):
        value = values.TupleValue()
        value.values = [
            try_get_value(v, 'compare', lineprop) for v in left_value.values
        ]
        left_value = value

    if isinstance(right_value, values.TupleValue):
        value = values.TupleValue()
        value.values = [
            try_get_value(v, 'compare', lineprop) for v in right_value.values
        ]
        right_value = value

    binop = nodes.BinOpType.Unknown
    if isinstance(astc.nast.op, gast.Add):
        binop = nodes.BinOpType.Add
    if isinstance(astc.nast.op, gast.Sub):
        binop = nodes.BinOpType.Sub
    if isinstance(astc.nast.op, gast.Mult):
        binop = nodes.BinOpType.Mul

    node_bin_op = nodes.NodeBinOp(left_value, right_value, binop, astc.lineno)

    ret_value = veval_bin.veval(binop, left_value, right_value)

    node_bin_op.set_outputs([ret_value])
    graph.add_node(node_bin_op)

    return values.Object(ret_value)
def veval_ast_tuple(astc : 'AstContext', local_field : 'values.Field', graph : 'Graph'):
    assert(isinstance(astc.nast, gast.gast.Tuple))
    vs = []
    for v in astc.nast.elts:
        v_ = veval_ast(astc.c(v), local_field, graph)
        vs.append(v_)

    return values.TupleValue(vs)
示例#6
0
def remove_ref(value):
    '''
    remove ref
    '''
    if isinstance(value, list):
        for i in range(len(value)):
            value[i] = remove_ref(value[i])

    if isinstance(value, functions.FunctionArgValueInput):
        converted = {}

        ret = functions.FunctionArgValueInput()

        for v in value.inputs:
            converted_v = remove_ref(v)
            ret.inputs.append(converted_v)
            converted[v] = converted_v

        keywords_ = {}
        for k, v in value.keywords.items():
            if v in converted.keys():
                keywords_[k] = converted[v]
            else:
                keywords_[k] = remove_ref(v)
        ret.keywords = keywords_
        return ret

    if isinstance(value,
                  values.TupleValue) and value.internal_value is not None:
        vs = []
        for v in value.internal_value:
            if isinstance(v, values.ValueRef):
                v = v.get_value()

            vs.append(v)

        return values.TupleValue(vs)

    if isinstance(value, values.ValueRef):
        return value.get_value()

    return value
示例#7
0
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
示例#8
0
 def ret_tuple():
     return values.TupleValue()
示例#9
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