1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
# RUN: %PYTHON %s | FileCheck %s
# Note that this is separate from ir_attributes.py since it depends on numpy,
# and we may want to disable if not available.
import gc
from mlir.ir import *
import numpy as np
def run(f):
print("\nTEST:", f.__name__)
f()
gc.collect()
assert Context._get_live_count() == 0
return f
################################################################################
# Tests of the array/buffer .get() factory method on unsupported dtype.
################################################################################
@run
def testGetDenseElementsUnsupported():
with Context():
array = np.array([["hello", "goodbye"]])
try:
attr = DenseElementsAttr.get(array)
except ValueError as e:
# CHECK: unimplemented array format conversion from format:
print(e)
# CHECK-LABEL: TEST: testGetDenseElementsUnSupportedTypeOkIfExplicitTypeProvided
@run
def testGetDenseElementsUnSupportedTypeOkIfExplicitTypeProvided():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)
# datetime64 specifically isn't important: it's just a 64-bit type that
# doesn't have a format under the Python buffer protocol. A more
# realistic example would be a NumPy extension type like the bfloat16
# type from the ml_dtypes package, which isn't a dependency of this
# test.
attr = DenseElementsAttr.get(array.view(np.datetime64),
type=IntegerType.get_signless(64))
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi64>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
################################################################################
# Splats.
################################################################################
# CHECK-LABEL: TEST: testGetDenseElementsSplatInt
@run
def testGetDenseElementsSplatInt():
with Context(), Location.unknown():
t = IntegerType.get_signless(32)
element = IntegerAttr.get(t, 555)
shaped_type = RankedTensorType.get((2, 3, 4), t)
attr = DenseElementsAttr.get_splat(shaped_type, element)
# CHECK: dense<555> : tensor<2x3x4xi32>
print(attr)
# CHECK: is_splat: True
print("is_splat:", attr.is_splat)
# CHECK: splat_value: IntegerAttr(555 : i32)
splat_value = attr.get_splat_value()
print("splat_value:", repr(splat_value))
assert splat_value == element
# CHECK-LABEL: TEST: testGetDenseElementsSplatFloat
@run
def testGetDenseElementsSplatFloat():
with Context(), Location.unknown():
t = F32Type.get()
element = FloatAttr.get(t, 1.2)
shaped_type = RankedTensorType.get((2, 3, 4), t)
attr = DenseElementsAttr.get_splat(shaped_type, element)
# CHECK: dense<1.200000e+00> : tensor<2x3x4xf32>
print(attr)
assert attr.get_splat_value() == element
# CHECK-LABEL: TEST: testGetDenseElementsSplatErrors
@run
def testGetDenseElementsSplatErrors():
with Context(), Location.unknown():
t = F32Type.get()
other_t = F64Type.get()
element = FloatAttr.get(t, 1.2)
other_element = FloatAttr.get(other_t, 1.2)
shaped_type = RankedTensorType.get((2, 3, 4), t)
dynamic_shaped_type = UnrankedTensorType.get(t)
non_shaped_type = t
try:
attr = DenseElementsAttr.get_splat(non_shaped_type, element)
except ValueError as e:
# CHECK: Expected a static ShapedType for the shaped_type parameter: Type(f32)
print(e)
try:
attr = DenseElementsAttr.get_splat(dynamic_shaped_type, element)
except ValueError as e:
# CHECK: Expected a static ShapedType for the shaped_type parameter: Type(tensor<*xf32>)
print(e)
try:
attr = DenseElementsAttr.get_splat(shaped_type, other_element)
except ValueError as e:
# CHECK: Shaped element type and attribute type must be equal: shaped=Type(tensor<2x3x4xf32>), element=Attribute(1.200000e+00 : f64)
print(e)
# CHECK-LABEL: TEST: testRepeatedValuesSplat
@run
def testRepeatedValuesSplat():
with Context():
array = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], dtype=np.float32)
attr = DenseElementsAttr.get(array)
# CHECK: dense<1.000000e+00> : tensor<2x3xf32>
print(attr)
# CHECK: is_splat: True
print("is_splat:", attr.is_splat)
# CHECK{LITERAL}: [[1. 1. 1.]
# CHECK{LITERAL}: [1. 1. 1.]]
print(np.array(attr))
# CHECK-LABEL: TEST: testNonSplat
@run
def testNonSplat():
with Context():
array = np.array([2.0, 1.0, 1.0], dtype=np.float32)
attr = DenseElementsAttr.get(array)
# CHECK: is_splat: False
print("is_splat:", attr.is_splat)
################################################################################
# Tests of the array/buffer .get() factory method, in all of its permutations.
################################################################################
### explicitly provided types
@run
def testGetDenseElementsBF16():
with Context():
array = np.array([[2, 4, 8], [16, 32, 64]], dtype=np.uint16)
attr = DenseElementsAttr.get(array, type=BF16Type.get())
# Note: These values don't mean much since just bit-casting. But they
# shouldn't change.
# CHECK: dense<{{\[}}[1.836710e-40, 3.673420e-40, 7.346840e-40], [1.469370e-39, 2.938740e-39, 5.877470e-39]]> : tensor<2x3xbf16>
print(attr)
@run
def testGetDenseElementsInteger4():
with Context():
array = np.array([[2, 4, 7], [-2, -4, -8]], dtype=np.uint8)
attr = DenseElementsAttr.get(array, type=IntegerType.get_signless(4))
# Note: These values don't mean much since just bit-casting. But they
# shouldn't change.
# CHECK: dense<{{\[}}[2, 4, 7], [-2, -4, -8]]> : tensor<2x3xi4>
print(attr)
@run
def testGetDenseElementsBool():
with Context():
bool_array = np.array([[1, 0, 1], [0, 1, 0]], dtype=np.bool_)
array = np.packbits(bool_array, axis=None, bitorder="little")
attr = DenseElementsAttr.get(
array, type=IntegerType.get_signless(1), shape=bool_array.shape
)
# CHECK: dense<{{\[}}[true, false, true], [false, true, false]]> : tensor<2x3xi1>
print(attr)
@run
def testGetDenseElementsBoolSplat():
with Context():
zero = np.array(0, dtype=np.uint8)
one = np.array(255, dtype=np.uint8)
print(one)
# CHECK: dense<false> : tensor<4x2x5xi1>
print(
DenseElementsAttr.get(
zero, type=IntegerType.get_signless(1), shape=(4, 2, 5)
)
)
# CHECK: dense<true> : tensor<4x2x5xi1>
print(
DenseElementsAttr.get(
one, type=IntegerType.get_signless(1), shape=(4, 2, 5)
)
)
### float and double arrays.
# CHECK-LABEL: TEST: testGetDenseElementsF16
@run
def testGetDenseElementsF16():
with Context():
array = np.array([[2.0, 4.0, 8.0], [16.0, 32.0, 64.0]], dtype=np.float16)
attr = DenseElementsAttr.get(array)
# CHECK: dense<{{\[}}[2.000000e+00, 4.000000e+00, 8.000000e+00], [1.600000e+01, 3.200000e+01, 6.400000e+01]]> : tensor<2x3xf16>
print(attr)
# CHECK: {{\[}}[ 2. 4. 8.]
# CHECK: {{\[}}16. 32. 64.]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsF32
@run
def testGetDenseElementsF32():
with Context():
array = np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float32)
attr = DenseElementsAttr.get(array)
# CHECK: dense<{{\[}}[1.100000e+00, 2.200000e+00, 3.300000e+00], [4.400000e+00, 5.500000e+00, 6.600000e+00]]> : tensor<2x3xf32>
print(attr)
# CHECK: {{\[}}[1.1 2.2 3.3]
# CHECK: {{\[}}4.4 5.5 6.6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsF64
@run
def testGetDenseElementsF64():
with Context():
array = np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float64)
attr = DenseElementsAttr.get(array)
# CHECK: dense<{{\[}}[1.100000e+00, 2.200000e+00, 3.300000e+00], [4.400000e+00, 5.500000e+00, 6.600000e+00]]> : tensor<2x3xf64>
print(attr)
# CHECK: {{\[}}[1.1 2.2 3.3]
# CHECK: {{\[}}4.4 5.5 6.6]]
print(np.array(attr))
### 16 bit integer arrays
# CHECK-LABEL: TEST: testGetDenseElementsI16Signless
@run
def testGetDenseElementsI16Signless():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16)
attr = DenseElementsAttr.get(array)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi16>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsUI16Signless
@run
def testGetDenseElementsUI16Signless():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint16)
attr = DenseElementsAttr.get(array)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi16>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsI16
@run
def testGetDenseElementsI16():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16)
attr = DenseElementsAttr.get(array, signless=False)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xsi16>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsUI16
@run
def testGetDenseElementsUI16():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint16)
attr = DenseElementsAttr.get(array, signless=False)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xui16>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
### 32 bit integer arrays
# CHECK-LABEL: TEST: testGetDenseElementsI32Signless
@run
def testGetDenseElementsI32Signless():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)
attr = DenseElementsAttr.get(array)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi32>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsUI32Signless
@run
def testGetDenseElementsUI32Signless():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint32)
attr = DenseElementsAttr.get(array)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi32>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsI32
@run
def testGetDenseElementsI32():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)
attr = DenseElementsAttr.get(array, signless=False)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xsi32>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsUI32
@run
def testGetDenseElementsUI32():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint32)
attr = DenseElementsAttr.get(array, signless=False)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xui32>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
## 64bit integer arrays
# CHECK-LABEL: TEST: testGetDenseElementsI64Signless
@run
def testGetDenseElementsI64Signless():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)
attr = DenseElementsAttr.get(array)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi64>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsUI64Signless
@run
def testGetDenseElementsUI64Signless():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint64)
attr = DenseElementsAttr.get(array)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xi64>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsI64
@run
def testGetDenseElementsI64():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)
attr = DenseElementsAttr.get(array, signless=False)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xsi64>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsUI64
@run
def testGetDenseElementsUI64():
with Context():
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint64)
attr = DenseElementsAttr.get(array, signless=False)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xui64>
print(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(np.array(attr))
# CHECK-LABEL: TEST: testGetDenseElementsIndex
@run
def testGetDenseElementsIndex():
with Context(), Location.unknown():
idx_type = IndexType.get()
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)
attr = DenseElementsAttr.get(array, type=idx_type)
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xindex>
print(attr)
arr = np.array(attr)
# CHECK: {{\[}}[1 2 3]
# CHECK: {{\[}}4 5 6]]
print(arr)
# CHECK: True
print(arr.dtype == np.int64)
|