예제 #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.dtype = value.dtype
        copied.vtype = value.vtype
        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)
        copied.dtype = value.dtype
        copied.vtype = value.vtype
        return copied

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

    return values.Value()
예제 #2
0
def veval(op: 'nodes.BinOpType', left: 'values.Value', right: 'values.Value',
          lineprop: 'utils.LineProperty'):

    initialize_lazy()

    if isinstance(left, values.ListValue):
        return functions.generate_value_with_same_type(left)

    if isinstance(left, values.TupleValue):
        return functions.generate_value_with_same_type(left)

    if isinstance(right, values.NumberValue) or isinstance(
            right, values.TensorValue):

        if not isinstance(left, values.NumberValue) and not isinstance(
                left, values.TensorValue):
            utils.print_warning('Unestimated type is on left', lineprop)
            left = values.NumberValue(0.0)

        left_type = left.dtype
        right_type = right.dtype

        if not config.float_restrict:
            if left_type == utils.dtype_float64:
                left_type = utils.dtype_float32
            if right_type == utils.dtype_float64:
                right_type = utils.dtype_float32

        if left_type is None:
            left_type = np.array(0.0, np.float32).dtype

        if right_type is None:
            right_type = np.array(0.0, np.float32).dtype

        result_type = None

        for v in binop_type_table:
            if v[0] == op and v[1] == left_type and v[2] == right_type:
                result_type = v[3]
                break

        assert (result_type is not None)

        if isinstance(right, values.TensorValue):
            return functions.generate_value_with_type(right, type_=result_type)
        else:
            return functions.generate_value_with_type(left, type_=result_type)

    return values.Value()
예제 #3
0
def veval(op: 'nodes.MultiaryOpType', values_list: 'values.Value'):
    if all([isinstance(value_, values.BoolValue) for value_ in values_list]):
        if op == nodes.MultiaryOpType.And:
            bool_value = True
            for value_ in values_list:
                bool_value = bool_value and value_.internal_value
            return values.BoolValue(bool_value)
        elif op == nodes.MultiaryOpType.Or:
            bool_value = False
            for value_ in values_list:
                bool_value = bool_value or value_.internal_value
            return values.BoolValue(bool_value)

        return values.BoolValue(None)

    return values.Value()
예제 #4
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()
예제 #5
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)
        ret.dtype = value.dtype
        ret.vtype = value.vtype

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

    elif isinstance(value, values.TupleValue):
        ret = values.TupleValue()
        ret.dtype = value.dtype
        ret.vtype = value.vtype

    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
예제 #6
0
def veval(op: 'nodes.BinOpType', left: 'values.Value', right: 'values.Value',
          lineprop: 'utils.LineProperty'):

    initialize_lazy()

    if isinstance(left, values.ListValue):
        return functions.generate_value_with_same_type(left)

    if isinstance(left, values.TupleValue):
        return functions.generate_value_with_same_type(left)

    if isinstance(left, values.StrValue):
        if not (isinstance(right, values.NumberValue) or isinstance(
                right, values.StrValue) or isinstance(right, values.BoolValue)
                or isinstance(right, values.TupleValue)):
            assert False

        if not (left.has_constant_value() or right.has_constant_value()):
            assert False

        if op == nodes.BinOpType.Add:
            if not isinstance(right, values.StrValue):
                assert False
            return values.StrValue(left.internal_value + right.internal_value)

        elif op == nodes.BinOpType.Mod:
            right_internal_value = right.internal_value

            if isinstance(right, values.TupleValue):
                values_ = []
                for ref in right_internal_value:
                    if not (isinstance(ref.get_value(), values.NumberValue)
                            or isinstance(ref.get_value(), values.StrValue)
                            or isinstance(ref.get_value(), values.BoolValue)):
                        assert False
                    if not ref.get_value().has_constant_value():
                        assert False
                    values_.append(ref.get_value())

                right_internal_value = tuple(value.internal_value
                                             for value in values_)
            return values.StrValue(left.internal_value % right_internal_value)

        return values.StrValue("")

    if isinstance(right, values.NumberValue) or isinstance(
            right, values.TensorValue):

        if not isinstance(left, values.NumberValue) and not isinstance(
                left, values.TensorValue):
            utils.print_warning('Unestimated type is on left', lineprop)
            left = values.NumberValue(0.0)

        left_type = left.dtype
        right_type = right.dtype

        if not config.float_restrict:
            if left_type == utils.dtype_float64:
                left_type = utils.dtype_float32
            if right_type == utils.dtype_float64:
                right_type = utils.dtype_float32

        if left_type is None:
            left_type = np.array(0.0, np.float32).dtype

        if right_type is None:
            right_type = np.array(0.0, np.float32).dtype

        result_type = None

        for v in binop_type_table:
            if v[0] == op and v[1] == left_type and v[2] == right_type:
                result_type = v[3]
                break

        assert (result_type is not None)

        if isinstance(right, values.TensorValue):
            return functions.generate_value_with_type(right, type_=result_type)
        else:
            return functions.generate_value_with_type(left, type_=result_type)

    return values.Value()