xref: /llvm-project/mlir/test/python/ir/array_attributes.py (revision 5d3ae5161210c068d01ffba36c8e0761e9971179)
19f3f6d7bSStella Laurenzo# RUN: %PYTHON %s | FileCheck %s
29f3f6d7bSStella Laurenzo# Note that this is separate from ir_attributes.py since it depends on numpy,
39f3f6d7bSStella Laurenzo# and we may want to disable if not available.
49f3f6d7bSStella Laurenzo
59f3f6d7bSStella Laurenzoimport gc
69f3f6d7bSStella Laurenzofrom mlir.ir import *
79f3f6d7bSStella Laurenzoimport numpy as np
8f66cd9e9SStella Laurenzoimport weakref
99f3f6d7bSStella Laurenzo
10f9008e63STobias Hieta
119f3f6d7bSStella Laurenzodef run(f):
129f3f6d7bSStella Laurenzo    print("\nTEST:", f.__name__)
139f3f6d7bSStella Laurenzo    f()
149f3f6d7bSStella Laurenzo    gc.collect()
159f3f6d7bSStella Laurenzo    assert Context._get_live_count() == 0
165d6d30edSStella Laurenzo    return f
179f3f6d7bSStella Laurenzo
18f9008e63STobias Hieta
199f3f6d7bSStella Laurenzo################################################################################
209f3f6d7bSStella Laurenzo# Tests of the array/buffer .get() factory method on unsupported dtype.
219f3f6d7bSStella Laurenzo################################################################################
229f3f6d7bSStella Laurenzo
23f9008e63STobias Hieta
245d6d30edSStella Laurenzo@run
259f3f6d7bSStella Laurenzodef testGetDenseElementsUnsupported():
269f3f6d7bSStella Laurenzo    with Context():
279f3f6d7bSStella Laurenzo        array = np.array([["hello", "goodbye"]])
289f3f6d7bSStella Laurenzo        try:
299f3f6d7bSStella Laurenzo            attr = DenseElementsAttr.get(array)
309f3f6d7bSStella Laurenzo        except ValueError as e:
319f3f6d7bSStella Laurenzo            # CHECK: unimplemented array format conversion from format:
329f3f6d7bSStella Laurenzo            print(e)
339f3f6d7bSStella Laurenzo
3471a25454SPeter Hawkins# CHECK-LABEL: TEST: testGetDenseElementsUnSupportedTypeOkIfExplicitTypeProvided
3571a25454SPeter Hawkins@run
3671a25454SPeter Hawkinsdef testGetDenseElementsUnSupportedTypeOkIfExplicitTypeProvided():
3771a25454SPeter Hawkins    with Context():
3871a25454SPeter Hawkins        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)
3971a25454SPeter Hawkins        # datetime64 specifically isn't important: it's just a 64-bit type that
4071a25454SPeter Hawkins        # doesn't have a format under the Python buffer protocol. A more
4171a25454SPeter Hawkins        # realistic example would be a NumPy extension type like the bfloat16
4271a25454SPeter Hawkins        # type from the ml_dtypes package, which isn't a dependency of this
4371a25454SPeter Hawkins        # test.
4471a25454SPeter Hawkins        attr = DenseElementsAttr.get(array.view(np.datetime64),
4571a25454SPeter Hawkins                                     type=IntegerType.get_signless(64))
4671a25454SPeter Hawkins        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi64>
4771a25454SPeter Hawkins        print(attr)
4871a25454SPeter Hawkins        # CHECK: {{\[}}[1 2 3]
4971a25454SPeter Hawkins        # CHECK: {{\[}}4 5 6]]
5071a25454SPeter Hawkins        print(np.array(attr))
5171a25454SPeter Hawkins
52f9008e63STobias Hieta
539f3f6d7bSStella Laurenzo################################################################################
54c912f0e7Spranavm-nvidia# Tests of the list of attributes .get() factory method
55c912f0e7Spranavm-nvidia################################################################################
56c912f0e7Spranavm-nvidia
57c912f0e7Spranavm-nvidia
58c912f0e7Spranavm-nvidia# CHECK-LABEL: TEST: testGetDenseElementsFromList
59c912f0e7Spranavm-nvidia@run
60c912f0e7Spranavm-nvidiadef testGetDenseElementsFromList():
61c912f0e7Spranavm-nvidia    with Context(), Location.unknown():
62c912f0e7Spranavm-nvidia        attrs = [FloatAttr.get(F64Type.get(), 1.0), FloatAttr.get(F64Type.get(), 2.0)]
63c912f0e7Spranavm-nvidia        attr = DenseElementsAttr.get(attrs)
64c912f0e7Spranavm-nvidia
65c912f0e7Spranavm-nvidia        # CHECK: dense<[1.000000e+00, 2.000000e+00]> : tensor<2xf64>
66c912f0e7Spranavm-nvidia        print(attr)
67c912f0e7Spranavm-nvidia
68c912f0e7Spranavm-nvidia
69c912f0e7Spranavm-nvidia# CHECK-LABEL: TEST: testGetDenseElementsFromListWithExplicitType
70c912f0e7Spranavm-nvidia@run
71c912f0e7Spranavm-nvidiadef testGetDenseElementsFromListWithExplicitType():
72c912f0e7Spranavm-nvidia    with Context(), Location.unknown():
73c912f0e7Spranavm-nvidia        attrs = [FloatAttr.get(F64Type.get(), 1.0), FloatAttr.get(F64Type.get(), 2.0)]
74c912f0e7Spranavm-nvidia        shaped_type = ShapedType(Type.parse("tensor<2xf64>"))
75c912f0e7Spranavm-nvidia        attr = DenseElementsAttr.get(attrs, shaped_type)
76c912f0e7Spranavm-nvidia
77c912f0e7Spranavm-nvidia        # CHECK: dense<[1.000000e+00, 2.000000e+00]> : tensor<2xf64>
78c912f0e7Spranavm-nvidia        print(attr)
79c912f0e7Spranavm-nvidia
80c912f0e7Spranavm-nvidia
81c912f0e7Spranavm-nvidia# CHECK-LABEL: TEST: testGetDenseElementsFromListEmptyList
82c912f0e7Spranavm-nvidia@run
83c912f0e7Spranavm-nvidiadef testGetDenseElementsFromListEmptyList():
84c912f0e7Spranavm-nvidia    with Context(), Location.unknown():
85c912f0e7Spranavm-nvidia        attrs = []
86c912f0e7Spranavm-nvidia
87c912f0e7Spranavm-nvidia        try:
88c912f0e7Spranavm-nvidia            attr = DenseElementsAttr.get(attrs)
89c912f0e7Spranavm-nvidia        except ValueError as e:
90c912f0e7Spranavm-nvidia            # CHECK: Attributes list must be non-empty
91c912f0e7Spranavm-nvidia            print(e)
92c912f0e7Spranavm-nvidia
93c912f0e7Spranavm-nvidia
94c912f0e7Spranavm-nvidia# CHECK-LABEL: TEST: testGetDenseElementsFromListNonAttributeType
95c912f0e7Spranavm-nvidia@run
96c912f0e7Spranavm-nvidiadef testGetDenseElementsFromListNonAttributeType():
97c912f0e7Spranavm-nvidia    with Context(), Location.unknown():
98c912f0e7Spranavm-nvidia        attrs = [1.0]
99c912f0e7Spranavm-nvidia
100c912f0e7Spranavm-nvidia        try:
101c912f0e7Spranavm-nvidia            attr = DenseElementsAttr.get(attrs)
102c912f0e7Spranavm-nvidia        except RuntimeError as e:
103c912f0e7Spranavm-nvidia            # CHECK: Invalid attribute when attempting to create an ArrayAttribute
104c912f0e7Spranavm-nvidia            print(e)
105c912f0e7Spranavm-nvidia
106c912f0e7Spranavm-nvidia
107c912f0e7Spranavm-nvidia# CHECK-LABEL: TEST: testGetDenseElementsFromListMismatchedType
108c912f0e7Spranavm-nvidia@run
109c912f0e7Spranavm-nvidiadef testGetDenseElementsFromListMismatchedType():
110c912f0e7Spranavm-nvidia    with Context(), Location.unknown():
111c912f0e7Spranavm-nvidia        attrs = [FloatAttr.get(F64Type.get(), 1.0), FloatAttr.get(F64Type.get(), 2.0)]
112c912f0e7Spranavm-nvidia        shaped_type = ShapedType(Type.parse("tensor<2xf32>"))
113c912f0e7Spranavm-nvidia
114c912f0e7Spranavm-nvidia        try:
115c912f0e7Spranavm-nvidia            attr = DenseElementsAttr.get(attrs, shaped_type)
116c912f0e7Spranavm-nvidia        except ValueError as e:
117c912f0e7Spranavm-nvidia            # CHECK: All attributes must be of the same type and match the type parameter
118c912f0e7Spranavm-nvidia            print(e)
119c912f0e7Spranavm-nvidia
120c912f0e7Spranavm-nvidia
121c912f0e7Spranavm-nvidia# CHECK-LABEL: TEST: testGetDenseElementsFromListMixedTypes
122c912f0e7Spranavm-nvidia@run
123c912f0e7Spranavm-nvidiadef testGetDenseElementsFromListMixedTypes():
124c912f0e7Spranavm-nvidia    with Context(), Location.unknown():
125c912f0e7Spranavm-nvidia        attrs = [FloatAttr.get(F64Type.get(), 1.0), FloatAttr.get(F32Type.get(), 2.0)]
126c912f0e7Spranavm-nvidia
127c912f0e7Spranavm-nvidia        try:
128c912f0e7Spranavm-nvidia            attr = DenseElementsAttr.get(attrs)
129c912f0e7Spranavm-nvidia        except ValueError as e:
130c912f0e7Spranavm-nvidia            # CHECK: All attributes must be of the same type and match the type parameter
131c912f0e7Spranavm-nvidia            print(e)
132c912f0e7Spranavm-nvidia
133c912f0e7Spranavm-nvidia
134c912f0e7Spranavm-nvidia################################################################################
1359f3f6d7bSStella Laurenzo# Splats.
1369f3f6d7bSStella Laurenzo################################################################################
1379f3f6d7bSStella Laurenzo
1389f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsSplatInt
1395d6d30edSStella Laurenzo@run
1409f3f6d7bSStella Laurenzodef testGetDenseElementsSplatInt():
1419f3f6d7bSStella Laurenzo    with Context(), Location.unknown():
1429f3f6d7bSStella Laurenzo        t = IntegerType.get_signless(32)
1439f3f6d7bSStella Laurenzo        element = IntegerAttr.get(t, 555)
1449f3f6d7bSStella Laurenzo        shaped_type = RankedTensorType.get((2, 3, 4), t)
1459f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get_splat(shaped_type, element)
1469f3f6d7bSStella Laurenzo        # CHECK: dense<555> : tensor<2x3x4xi32>
1479f3f6d7bSStella Laurenzo        print(attr)
1489f3f6d7bSStella Laurenzo        # CHECK: is_splat: True
1499f3f6d7bSStella Laurenzo        print("is_splat:", attr.is_splat)
150974c1596SRahul Kayaith
151974c1596SRahul Kayaith        # CHECK: splat_value: IntegerAttr(555 : i32)
152974c1596SRahul Kayaith        splat_value = attr.get_splat_value()
153974c1596SRahul Kayaith        print("splat_value:", repr(splat_value))
154974c1596SRahul Kayaith        assert splat_value == element
1559f3f6d7bSStella Laurenzo
1569f3f6d7bSStella Laurenzo
1579f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsSplatFloat
1585d6d30edSStella Laurenzo@run
1599f3f6d7bSStella Laurenzodef testGetDenseElementsSplatFloat():
1609f3f6d7bSStella Laurenzo    with Context(), Location.unknown():
1619f3f6d7bSStella Laurenzo        t = F32Type.get()
1629f3f6d7bSStella Laurenzo        element = FloatAttr.get(t, 1.2)
1639f3f6d7bSStella Laurenzo        shaped_type = RankedTensorType.get((2, 3, 4), t)
1649f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get_splat(shaped_type, element)
1659f3f6d7bSStella Laurenzo        # CHECK: dense<1.200000e+00> : tensor<2x3x4xf32>
1669f3f6d7bSStella Laurenzo        print(attr)
16791259963SAdam Paszke        assert attr.get_splat_value() == element
1689f3f6d7bSStella Laurenzo
1699f3f6d7bSStella Laurenzo
1709f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsSplatErrors
1715d6d30edSStella Laurenzo@run
1729f3f6d7bSStella Laurenzodef testGetDenseElementsSplatErrors():
1739f3f6d7bSStella Laurenzo    with Context(), Location.unknown():
1749f3f6d7bSStella Laurenzo        t = F32Type.get()
1759f3f6d7bSStella Laurenzo        other_t = F64Type.get()
1769f3f6d7bSStella Laurenzo        element = FloatAttr.get(t, 1.2)
1779f3f6d7bSStella Laurenzo        other_element = FloatAttr.get(other_t, 1.2)
1789f3f6d7bSStella Laurenzo        shaped_type = RankedTensorType.get((2, 3, 4), t)
1799f3f6d7bSStella Laurenzo        dynamic_shaped_type = UnrankedTensorType.get(t)
1809f3f6d7bSStella Laurenzo        non_shaped_type = t
1819f3f6d7bSStella Laurenzo
1829f3f6d7bSStella Laurenzo        try:
1839f3f6d7bSStella Laurenzo            attr = DenseElementsAttr.get_splat(non_shaped_type, element)
1849f3f6d7bSStella Laurenzo        except ValueError as e:
1859f3f6d7bSStella Laurenzo            # CHECK: Expected a static ShapedType for the shaped_type parameter: Type(f32)
1869f3f6d7bSStella Laurenzo            print(e)
1879f3f6d7bSStella Laurenzo
1889f3f6d7bSStella Laurenzo        try:
1899f3f6d7bSStella Laurenzo            attr = DenseElementsAttr.get_splat(dynamic_shaped_type, element)
1909f3f6d7bSStella Laurenzo        except ValueError as e:
1919f3f6d7bSStella Laurenzo            # CHECK: Expected a static ShapedType for the shaped_type parameter: Type(tensor<*xf32>)
1929f3f6d7bSStella Laurenzo            print(e)
1939f3f6d7bSStella Laurenzo
1949f3f6d7bSStella Laurenzo        try:
1959f3f6d7bSStella Laurenzo            attr = DenseElementsAttr.get_splat(shaped_type, other_element)
1969f3f6d7bSStella Laurenzo        except ValueError as e:
1979f3f6d7bSStella Laurenzo            # CHECK: Shaped element type and attribute type must be equal: shaped=Type(tensor<2x3x4xf32>), element=Attribute(1.200000e+00 : f64)
1989f3f6d7bSStella Laurenzo            print(e)
1999f3f6d7bSStella Laurenzo
2005d6d30edSStella Laurenzo
2015d6d30edSStella Laurenzo# CHECK-LABEL: TEST: testRepeatedValuesSplat
2025d6d30edSStella Laurenzo@run
2035d6d30edSStella Laurenzodef testRepeatedValuesSplat():
2045d6d30edSStella Laurenzo    with Context():
2055d6d30edSStella Laurenzo        array = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], dtype=np.float32)
2065d6d30edSStella Laurenzo        attr = DenseElementsAttr.get(array)
2075d6d30edSStella Laurenzo        # CHECK: dense<1.000000e+00> : tensor<2x3xf32>
2085d6d30edSStella Laurenzo        print(attr)
2095d6d30edSStella Laurenzo        # CHECK: is_splat: True
2105d6d30edSStella Laurenzo        print("is_splat:", attr.is_splat)
211f0e847d0SRahul Kayaith        # CHECK{LITERAL}: [[1. 1. 1.]
212f0e847d0SRahul Kayaith        # CHECK{LITERAL}:  [1. 1. 1.]]
213f0e847d0SRahul Kayaith        print(np.array(attr))
2145d6d30edSStella Laurenzo
2155d6d30edSStella Laurenzo
2165d6d30edSStella Laurenzo# CHECK-LABEL: TEST: testNonSplat
2175d6d30edSStella Laurenzo@run
2185d6d30edSStella Laurenzodef testNonSplat():
2195d6d30edSStella Laurenzo    with Context():
2205d6d30edSStella Laurenzo        array = np.array([2.0, 1.0, 1.0], dtype=np.float32)
2215d6d30edSStella Laurenzo        attr = DenseElementsAttr.get(array)
2225d6d30edSStella Laurenzo        # CHECK: is_splat: False
2235d6d30edSStella Laurenzo        print("is_splat:", attr.is_splat)
2249f3f6d7bSStella Laurenzo
2259f3f6d7bSStella Laurenzo
2269f3f6d7bSStella Laurenzo################################################################################
2279f3f6d7bSStella Laurenzo# Tests of the array/buffer .get() factory method, in all of its permutations.
2289f3f6d7bSStella Laurenzo################################################################################
2299f3f6d7bSStella Laurenzo
2305d6d30edSStella Laurenzo### explicitly provided types
2315d6d30edSStella Laurenzo
232f9008e63STobias Hieta
2335d6d30edSStella Laurenzo@run
2345d6d30edSStella Laurenzodef testGetDenseElementsBF16():
2355d6d30edSStella Laurenzo    with Context():
2365d6d30edSStella Laurenzo        array = np.array([[2, 4, 8], [16, 32, 64]], dtype=np.uint16)
2375d6d30edSStella Laurenzo        attr = DenseElementsAttr.get(array, type=BF16Type.get())
2385d6d30edSStella Laurenzo        # Note: These values don't mean much since just bit-casting. But they
2395d6d30edSStella Laurenzo        # shouldn't change.
2405d6d30edSStella Laurenzo        # CHECK: dense<{{\[}}[1.836710e-40, 3.673420e-40, 7.346840e-40], [1.469370e-39, 2.938740e-39, 5.877470e-39]]> : tensor<2x3xbf16>
2415d6d30edSStella Laurenzo        print(attr)
2425d6d30edSStella Laurenzo
243f9008e63STobias Hieta
2445d6d30edSStella Laurenzo@run
2455d6d30edSStella Laurenzodef testGetDenseElementsInteger4():
2465d6d30edSStella Laurenzo    with Context():
247f66cd9e9SStella Laurenzo        array = np.array([[2, 4, 7], [-2, -4, -8]], dtype=np.int8)
2485d6d30edSStella Laurenzo        attr = DenseElementsAttr.get(array, type=IntegerType.get_signless(4))
2495d6d30edSStella Laurenzo        # Note: These values don't mean much since just bit-casting. But they
2505d6d30edSStella Laurenzo        # shouldn't change.
2515d6d30edSStella Laurenzo        # CHECK: dense<{{\[}}[2, 4, 7], [-2, -4, -8]]> : tensor<2x3xi4>
2525d6d30edSStella Laurenzo        print(attr)
2535d6d30edSStella Laurenzo
2545d6d30edSStella Laurenzo
2555d6d30edSStella Laurenzo@run
2565d6d30edSStella Laurenzodef testGetDenseElementsBool():
2575d6d30edSStella Laurenzo    with Context():
2585d6d30edSStella Laurenzo        bool_array = np.array([[1, 0, 1], [0, 1, 0]], dtype=np.bool_)
2595d6d30edSStella Laurenzo        array = np.packbits(bool_array, axis=None, bitorder="little")
2605d6d30edSStella Laurenzo        attr = DenseElementsAttr.get(
261f9008e63STobias Hieta            array, type=IntegerType.get_signless(1), shape=bool_array.shape
262f9008e63STobias Hieta        )
2635d6d30edSStella Laurenzo        # CHECK: dense<{{\[}}[true, false, true], [false, true, false]]> : tensor<2x3xi1>
2645d6d30edSStella Laurenzo        print(attr)
2655d6d30edSStella Laurenzo
2665d6d30edSStella Laurenzo
2675d6d30edSStella Laurenzo@run
2685d6d30edSStella Laurenzodef testGetDenseElementsBoolSplat():
2695d6d30edSStella Laurenzo    with Context():
2705d6d30edSStella Laurenzo        zero = np.array(0, dtype=np.uint8)
2715d6d30edSStella Laurenzo        one = np.array(255, dtype=np.uint8)
2725d6d30edSStella Laurenzo        print(one)
2735d6d30edSStella Laurenzo        # CHECK: dense<false> : tensor<4x2x5xi1>
274f9008e63STobias Hieta        print(
275f9008e63STobias Hieta            DenseElementsAttr.get(
276f9008e63STobias Hieta                zero, type=IntegerType.get_signless(1), shape=(4, 2, 5)
277f9008e63STobias Hieta            )
278f9008e63STobias Hieta        )
2795d6d30edSStella Laurenzo        # CHECK: dense<true> : tensor<4x2x5xi1>
280f9008e63STobias Hieta        print(
281f9008e63STobias Hieta            DenseElementsAttr.get(
282f9008e63STobias Hieta                one, type=IntegerType.get_signless(1), shape=(4, 2, 5)
283f9008e63STobias Hieta            )
284f9008e63STobias Hieta        )
2855d6d30edSStella Laurenzo
2865d6d30edSStella Laurenzo
2879f3f6d7bSStella Laurenzo### float and double arrays.
2889f3f6d7bSStella Laurenzo
289c912f0e7Spranavm-nvidia
2905d6d30edSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsF16
2915d6d30edSStella Laurenzo@run
2925d6d30edSStella Laurenzodef testGetDenseElementsF16():
2935d6d30edSStella Laurenzo    with Context():
2945d6d30edSStella Laurenzo        array = np.array([[2.0, 4.0, 8.0], [16.0, 32.0, 64.0]], dtype=np.float16)
2955d6d30edSStella Laurenzo        attr = DenseElementsAttr.get(array)
2965d6d30edSStella Laurenzo        # CHECK: dense<{{\[}}[2.000000e+00, 4.000000e+00, 8.000000e+00], [1.600000e+01, 3.200000e+01, 6.400000e+01]]> : tensor<2x3xf16>
2975d6d30edSStella Laurenzo        print(attr)
2985d6d30edSStella Laurenzo        # CHECK: {{\[}}[ 2. 4. 8.]
2995d6d30edSStella Laurenzo        # CHECK: {{\[}}16. 32. 64.]]
3005d6d30edSStella Laurenzo        print(np.array(attr))
3015d6d30edSStella Laurenzo
3025d6d30edSStella Laurenzo
3039f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsF32
3045d6d30edSStella Laurenzo@run
3059f3f6d7bSStella Laurenzodef testGetDenseElementsF32():
3069f3f6d7bSStella Laurenzo    with Context():
3079f3f6d7bSStella Laurenzo        array = np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float32)
3089f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get(array)
3099f3f6d7bSStella Laurenzo        # CHECK: dense<{{\[}}[1.100000e+00, 2.200000e+00, 3.300000e+00], [4.400000e+00, 5.500000e+00, 6.600000e+00]]> : tensor<2x3xf32>
3109f3f6d7bSStella Laurenzo        print(attr)
3119f3f6d7bSStella Laurenzo        # CHECK: {{\[}}[1.1 2.2 3.3]
3129f3f6d7bSStella Laurenzo        # CHECK: {{\[}}4.4 5.5 6.6]]
3139f3f6d7bSStella Laurenzo        print(np.array(attr))
3149f3f6d7bSStella Laurenzo
3159f3f6d7bSStella Laurenzo
3169f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsF64
3175d6d30edSStella Laurenzo@run
3189f3f6d7bSStella Laurenzodef testGetDenseElementsF64():
3199f3f6d7bSStella Laurenzo    with Context():
3209f3f6d7bSStella Laurenzo        array = np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float64)
3219f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get(array)
3229f3f6d7bSStella Laurenzo        # CHECK: dense<{{\[}}[1.100000e+00, 2.200000e+00, 3.300000e+00], [4.400000e+00, 5.500000e+00, 6.600000e+00]]> : tensor<2x3xf64>
3239f3f6d7bSStella Laurenzo        print(attr)
3249f3f6d7bSStella Laurenzo        # CHECK: {{\[}}[1.1 2.2 3.3]
3259f3f6d7bSStella Laurenzo        # CHECK: {{\[}}4.4 5.5 6.6]]
3269f3f6d7bSStella Laurenzo        print(np.array(attr))
3279f3f6d7bSStella Laurenzo
3289f3f6d7bSStella Laurenzo
3291824e45cSKasper Nielsen### 1 bit/boolean integer arrays
3301824e45cSKasper Nielsen# CHECK-LABEL: TEST: testGetDenseElementsI1Signless
3311824e45cSKasper Nielsen@run
3321824e45cSKasper Nielsendef testGetDenseElementsI1Signless():
3331824e45cSKasper Nielsen    with Context():
3341824e45cSKasper Nielsen        array = np.array([True], dtype=np.bool_)
3351824e45cSKasper Nielsen        attr = DenseElementsAttr.get(array)
3361824e45cSKasper Nielsen        # CHECK: dense<true> : tensor<1xi1>
3371824e45cSKasper Nielsen        print(attr)
3381824e45cSKasper Nielsen        # CHECK{LITERAL}: [ True]
3391824e45cSKasper Nielsen        print(np.array(attr))
3401824e45cSKasper Nielsen
3411824e45cSKasper Nielsen        array = np.array([[True, False, True], [True, True, False]], dtype=np.bool_)
3421824e45cSKasper Nielsen        attr = DenseElementsAttr.get(array)
3431824e45cSKasper Nielsen        # CHECK{LITERAL}: dense<[[true, false, true], [true, true, false]]> : tensor<2x3xi1>
3441824e45cSKasper Nielsen        print(attr)
3451824e45cSKasper Nielsen        # CHECK{LITERAL}: [[ True False True]
3461824e45cSKasper Nielsen        # CHECK{LITERAL}:  [ True True False]]
3471824e45cSKasper Nielsen        print(np.array(attr))
3481824e45cSKasper Nielsen
3491824e45cSKasper Nielsen        array = np.array(
3501824e45cSKasper Nielsen            [[True, True, False, False], [True, False, True, False]], dtype=np.bool_
3511824e45cSKasper Nielsen        )
3521824e45cSKasper Nielsen        attr = DenseElementsAttr.get(array)
3531824e45cSKasper Nielsen        # CHECK{LITERAL}: dense<[[true, true, false, false], [true, false, true, false]]> : tensor<2x4xi1>
3541824e45cSKasper Nielsen        print(attr)
3551824e45cSKasper Nielsen        # CHECK{LITERAL}: [[ True True False False]
3561824e45cSKasper Nielsen        # CHECK{LITERAL}:  [ True False True False]]
3571824e45cSKasper Nielsen        print(np.array(attr))
3581824e45cSKasper Nielsen
3591824e45cSKasper Nielsen        array = np.array(
3601824e45cSKasper Nielsen            [
3611824e45cSKasper Nielsen                [True, True, False, False],
3621824e45cSKasper Nielsen                [True, False, True, False],
3631824e45cSKasper Nielsen                [False, False, False, False],
3641824e45cSKasper Nielsen                [True, True, True, True],
3651824e45cSKasper Nielsen                [True, False, False, True],
3661824e45cSKasper Nielsen            ],
3671824e45cSKasper Nielsen            dtype=np.bool_,
3681824e45cSKasper Nielsen        )
3691824e45cSKasper Nielsen        attr = DenseElementsAttr.get(array)
3701824e45cSKasper Nielsen        # CHECK{LITERAL}: dense<[[true, true, false, false], [true, false, true, false], [false, false, false, false], [true, true, true, true], [true, false, false, true]]> : tensor<5x4xi1>
3711824e45cSKasper Nielsen        print(attr)
3721824e45cSKasper Nielsen        # CHECK{LITERAL}: [[ True True False False]
3731824e45cSKasper Nielsen        # CHECK{LITERAL}:  [ True False True False]
3741824e45cSKasper Nielsen        # CHECK{LITERAL}:  [False False False False]
3751824e45cSKasper Nielsen        # CHECK{LITERAL}:  [ True True True True]
3761824e45cSKasper Nielsen        # CHECK{LITERAL}:  [ True False False True]]
3771824e45cSKasper Nielsen        print(np.array(attr))
3781824e45cSKasper Nielsen
3791824e45cSKasper Nielsen        array = np.array(
3801824e45cSKasper Nielsen            [
3811824e45cSKasper Nielsen                [True, True, False, False, True, True, False, False, False],
3821824e45cSKasper Nielsen                [False, False, False, True, False, True, True, False, True],
3831824e45cSKasper Nielsen            ],
3841824e45cSKasper Nielsen            dtype=np.bool_,
3851824e45cSKasper Nielsen        )
3861824e45cSKasper Nielsen        attr = DenseElementsAttr.get(array)
3871824e45cSKasper Nielsen        # CHECK{LITERAL}: dense<[[true, true, false, false, true, true, false, false, false], [false, false, false, true, false, true, true, false, true]]> : tensor<2x9xi1>
3881824e45cSKasper Nielsen        print(attr)
3891824e45cSKasper Nielsen        # CHECK{LITERAL}: [[ True True False False True True False False False]
3901824e45cSKasper Nielsen        # CHECK{LITERAL}:  [False False False True False True True False True]]
3911824e45cSKasper Nielsen        print(np.array(attr))
3921824e45cSKasper Nielsen
3931824e45cSKasper Nielsen        array = np.array([], dtype=np.bool_)
3941824e45cSKasper Nielsen        attr = DenseElementsAttr.get(array)
3951824e45cSKasper Nielsen        # CHECK: dense<> : tensor<0xi1>
3961824e45cSKasper Nielsen        print(attr)
3971824e45cSKasper Nielsen        # CHECK{LITERAL}: []
3981824e45cSKasper Nielsen        print(np.array(attr))
3991824e45cSKasper Nielsen
4001824e45cSKasper Nielsen
4015d6d30edSStella Laurenzo### 16 bit integer arrays
4025d6d30edSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsI16Signless
4035d6d30edSStella Laurenzo@run
4045d6d30edSStella Laurenzodef testGetDenseElementsI16Signless():
4055d6d30edSStella Laurenzo    with Context():
4065d6d30edSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16)
4075d6d30edSStella Laurenzo        attr = DenseElementsAttr.get(array)
4085d6d30edSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi16>
4095d6d30edSStella Laurenzo        print(attr)
4105d6d30edSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
4115d6d30edSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
4125d6d30edSStella Laurenzo        print(np.array(attr))
4135d6d30edSStella Laurenzo
4145d6d30edSStella Laurenzo
4155d6d30edSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsUI16Signless
4165d6d30edSStella Laurenzo@run
4175d6d30edSStella Laurenzodef testGetDenseElementsUI16Signless():
4185d6d30edSStella Laurenzo    with Context():
4195d6d30edSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint16)
4205d6d30edSStella Laurenzo        attr = DenseElementsAttr.get(array)
4215d6d30edSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi16>
4225d6d30edSStella Laurenzo        print(attr)
4235d6d30edSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
4245d6d30edSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
4255d6d30edSStella Laurenzo        print(np.array(attr))
4265d6d30edSStella Laurenzo
4275d6d30edSStella Laurenzo
4285d6d30edSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsI16
4295d6d30edSStella Laurenzo@run
4305d6d30edSStella Laurenzodef testGetDenseElementsI16():
4315d6d30edSStella Laurenzo    with Context():
4325d6d30edSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16)
4335d6d30edSStella Laurenzo        attr = DenseElementsAttr.get(array, signless=False)
4345d6d30edSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xsi16>
4355d6d30edSStella Laurenzo        print(attr)
4365d6d30edSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
4375d6d30edSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
4385d6d30edSStella Laurenzo        print(np.array(attr))
4395d6d30edSStella Laurenzo
4405d6d30edSStella Laurenzo
4415d6d30edSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsUI16
4425d6d30edSStella Laurenzo@run
4435d6d30edSStella Laurenzodef testGetDenseElementsUI16():
4445d6d30edSStella Laurenzo    with Context():
4455d6d30edSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint16)
4465d6d30edSStella Laurenzo        attr = DenseElementsAttr.get(array, signless=False)
4475d6d30edSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xui16>
4485d6d30edSStella Laurenzo        print(attr)
4495d6d30edSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
4505d6d30edSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
4515d6d30edSStella Laurenzo        print(np.array(attr))
4529f3f6d7bSStella Laurenzo
453f9008e63STobias Hieta
4549f3f6d7bSStella Laurenzo### 32 bit integer arrays
4559f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsI32Signless
4565d6d30edSStella Laurenzo@run
4579f3f6d7bSStella Laurenzodef testGetDenseElementsI32Signless():
4589f3f6d7bSStella Laurenzo    with Context():
4599f3f6d7bSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)
4609f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get(array)
4619f3f6d7bSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi32>
4629f3f6d7bSStella Laurenzo        print(attr)
4639f3f6d7bSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
4649f3f6d7bSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
4659f3f6d7bSStella Laurenzo        print(np.array(attr))
4669f3f6d7bSStella Laurenzo
4679f3f6d7bSStella Laurenzo
4689f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsUI32Signless
4695d6d30edSStella Laurenzo@run
4709f3f6d7bSStella Laurenzodef testGetDenseElementsUI32Signless():
4719f3f6d7bSStella Laurenzo    with Context():
4729f3f6d7bSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint32)
4739f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get(array)
4749f3f6d7bSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi32>
4759f3f6d7bSStella Laurenzo        print(attr)
4769f3f6d7bSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
4779f3f6d7bSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
4789f3f6d7bSStella Laurenzo        print(np.array(attr))
4799f3f6d7bSStella Laurenzo
4809f3f6d7bSStella Laurenzo
4819f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsI32
4825d6d30edSStella Laurenzo@run
4839f3f6d7bSStella Laurenzodef testGetDenseElementsI32():
4849f3f6d7bSStella Laurenzo    with Context():
4859f3f6d7bSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)
4869f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get(array, signless=False)
4879f3f6d7bSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xsi32>
4889f3f6d7bSStella Laurenzo        print(attr)
4899f3f6d7bSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
4909f3f6d7bSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
4919f3f6d7bSStella Laurenzo        print(np.array(attr))
4929f3f6d7bSStella Laurenzo
4939f3f6d7bSStella Laurenzo
4949f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsUI32
4955d6d30edSStella Laurenzo@run
4969f3f6d7bSStella Laurenzodef testGetDenseElementsUI32():
4979f3f6d7bSStella Laurenzo    with Context():
4989f3f6d7bSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint32)
4999f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get(array, signless=False)
5009f3f6d7bSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xui32>
5019f3f6d7bSStella Laurenzo        print(attr)
5029f3f6d7bSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
5039f3f6d7bSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
5049f3f6d7bSStella Laurenzo        print(np.array(attr))
5059f3f6d7bSStella Laurenzo
5069f3f6d7bSStella Laurenzo
5079f3f6d7bSStella Laurenzo## 64bit integer arrays
5089f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsI64Signless
5095d6d30edSStella Laurenzo@run
5109f3f6d7bSStella Laurenzodef testGetDenseElementsI64Signless():
5119f3f6d7bSStella Laurenzo    with Context():
5129f3f6d7bSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)
5139f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get(array)
5149f3f6d7bSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi64>
5159f3f6d7bSStella Laurenzo        print(attr)
5169f3f6d7bSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
5179f3f6d7bSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
5189f3f6d7bSStella Laurenzo        print(np.array(attr))
5199f3f6d7bSStella Laurenzo
5209f3f6d7bSStella Laurenzo
5219f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsUI64Signless
5225d6d30edSStella Laurenzo@run
5239f3f6d7bSStella Laurenzodef testGetDenseElementsUI64Signless():
5249f3f6d7bSStella Laurenzo    with Context():
5259f3f6d7bSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint64)
5269f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get(array)
5279f3f6d7bSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi64>
5289f3f6d7bSStella Laurenzo        print(attr)
5299f3f6d7bSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
5309f3f6d7bSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
5319f3f6d7bSStella Laurenzo        print(np.array(attr))
5329f3f6d7bSStella Laurenzo
5339f3f6d7bSStella Laurenzo
5349f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsI64
5355d6d30edSStella Laurenzo@run
5369f3f6d7bSStella Laurenzodef testGetDenseElementsI64():
5379f3f6d7bSStella Laurenzo    with Context():
5389f3f6d7bSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)
5399f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get(array, signless=False)
5409f3f6d7bSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xsi64>
5419f3f6d7bSStella Laurenzo        print(attr)
5429f3f6d7bSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
5439f3f6d7bSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
5449f3f6d7bSStella Laurenzo        print(np.array(attr))
5459f3f6d7bSStella Laurenzo
5469f3f6d7bSStella Laurenzo
5479f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testGetDenseElementsUI64
5485d6d30edSStella Laurenzo@run
5499f3f6d7bSStella Laurenzodef testGetDenseElementsUI64():
5509f3f6d7bSStella Laurenzo    with Context():
5519f3f6d7bSStella Laurenzo        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint64)
5529f3f6d7bSStella Laurenzo        attr = DenseElementsAttr.get(array, signless=False)
5539f3f6d7bSStella Laurenzo        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xui64>
5549f3f6d7bSStella Laurenzo        print(attr)
5559f3f6d7bSStella Laurenzo        # CHECK: {{\[}}[1 2 3]
5569f3f6d7bSStella Laurenzo        # CHECK: {{\[}}4 5 6]]
5579f3f6d7bSStella Laurenzo        print(np.array(attr))
5589f3f6d7bSStella Laurenzo
559ef1b735dSmax
560ef1b735dSmax# CHECK-LABEL: TEST: testGetDenseElementsIndex
561ef1b735dSmax@run
562ef1b735dSmaxdef testGetDenseElementsIndex():
563ef1b735dSmax    with Context(), Location.unknown():
564ef1b735dSmax        idx_type = IndexType.get()
565ef1b735dSmax        array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)
566ef1b735dSmax        attr = DenseElementsAttr.get(array, type=idx_type)
567ef1b735dSmax        # CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xindex>
568ef1b735dSmax        print(attr)
569ef1b735dSmax        arr = np.array(attr)
570ef1b735dSmax        # CHECK: {{\[}}[1 2 3]
571ef1b735dSmax        # CHECK: {{\[}}4 5 6]]
572ef1b735dSmax        print(arr)
573ef1b735dSmax        # CHECK: True
574ef1b735dSmax        print(arr.dtype == np.int64)
575*5d3ae516SMatthias Gehre        array = np.array([1, 2, 3], dtype=np.int64)
576*5d3ae516SMatthias Gehre        attr = DenseIntElementsAttr.get(array, type=VectorType.get([3], idx_type))
577*5d3ae516SMatthias Gehre        # CHECK: [1, 2, 3]
578*5d3ae516SMatthias Gehre        print(list(DenseIntElementsAttr(attr)))
579f66cd9e9SStella Laurenzo
580f66cd9e9SStella Laurenzo
581f66cd9e9SStella Laurenzo# CHECK-LABEL: TEST: testGetDenseResourceElementsAttr
582f66cd9e9SStella Laurenzo@run
583f66cd9e9SStella Laurenzodef testGetDenseResourceElementsAttr():
584f66cd9e9SStella Laurenzo    def on_delete(_):
585f66cd9e9SStella Laurenzo        print("BACKING MEMORY DELETED")
586f66cd9e9SStella Laurenzo
587f66cd9e9SStella Laurenzo    context = Context()
588f66cd9e9SStella Laurenzo    mview = memoryview(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
589f66cd9e9SStella Laurenzo    ref = weakref.ref(mview, on_delete)
590f66cd9e9SStella Laurenzo
591f66cd9e9SStella Laurenzo    def test_attribute(context, mview):
592f66cd9e9SStella Laurenzo        with context, Location.unknown():
593f66cd9e9SStella Laurenzo            element_type = IntegerType.get_signless(32)
594f66cd9e9SStella Laurenzo            tensor_type = RankedTensorType.get((2, 3), element_type)
595f66cd9e9SStella Laurenzo            resource = DenseResourceElementsAttr.get_from_buffer(
596f66cd9e9SStella Laurenzo                mview, "from_py", tensor_type
597f66cd9e9SStella Laurenzo            )
598f66cd9e9SStella Laurenzo            module = Module.parse("module {}")
599f66cd9e9SStella Laurenzo            module.operation.attributes["test.resource"] = resource
600f66cd9e9SStella Laurenzo            # CHECK: test.resource = dense_resource<from_py> : tensor<2x3xi32>
601f66cd9e9SStella Laurenzo            # CHECK: from_py: "0x04000000010000000200000003000000040000000500000006000000"
602f66cd9e9SStella Laurenzo            print(module)
603f66cd9e9SStella Laurenzo
604f66cd9e9SStella Laurenzo            # Verifies type casting.
605f66cd9e9SStella Laurenzo            # CHECK: dense_resource<from_py> : tensor<2x3xi32>
606f66cd9e9SStella Laurenzo            print(
607f66cd9e9SStella Laurenzo                DenseResourceElementsAttr(module.operation.attributes["test.resource"])
608f66cd9e9SStella Laurenzo            )
609f66cd9e9SStella Laurenzo
610f66cd9e9SStella Laurenzo    test_attribute(context, mview)
611f66cd9e9SStella Laurenzo    mview = None
612f66cd9e9SStella Laurenzo    gc.collect()
613f66cd9e9SStella Laurenzo    # CHECK: FREEING CONTEXT
614f66cd9e9SStella Laurenzo    print("FREEING CONTEXT")
615f66cd9e9SStella Laurenzo    context = None
616f66cd9e9SStella Laurenzo    gc.collect()
617f66cd9e9SStella Laurenzo    # CHECK: BACKING MEMORY DELETED
618f66cd9e9SStella Laurenzo    # CHECK: EXIT FUNCTION
619f66cd9e9SStella Laurenzo    print("EXIT FUNCTION")
620