forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_attributes.py
728 lines (608 loc) · 24.7 KB
/
array_attributes.py
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# 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
import weakref
import ctypes
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))
################################################################################
# Tests of the list of attributes .get() factory method
################################################################################
# CHECK-LABEL: TEST: testGetDenseElementsFromList
@run
def testGetDenseElementsFromList():
with Context(), Location.unknown():
attrs = [FloatAttr.get(F64Type.get(), 1.0), FloatAttr.get(F64Type.get(), 2.0)]
attr = DenseElementsAttr.get(attrs)
# CHECK: dense<[1.000000e+00, 2.000000e+00]> : tensor<2xf64>
print(attr)
# CHECK-LABEL: TEST: testGetDenseElementsFromListWithExplicitType
@run
def testGetDenseElementsFromListWithExplicitType():
with Context(), Location.unknown():
attrs = [FloatAttr.get(F64Type.get(), 1.0), FloatAttr.get(F64Type.get(), 2.0)]
shaped_type = ShapedType(Type.parse("tensor<2xf64>"))
attr = DenseElementsAttr.get(attrs, shaped_type)
# CHECK: dense<[1.000000e+00, 2.000000e+00]> : tensor<2xf64>
print(attr)
# CHECK-LABEL: TEST: testGetDenseElementsFromListEmptyList
@run
def testGetDenseElementsFromListEmptyList():
with Context(), Location.unknown():
attrs = []
try:
attr = DenseElementsAttr.get(attrs)
except ValueError as e:
# CHECK: Attributes list must be non-empty
print(e)
# CHECK-LABEL: TEST: testGetDenseElementsFromListNonAttributeType
@run
def testGetDenseElementsFromListNonAttributeType():
with Context(), Location.unknown():
attrs = [1.0]
try:
attr = DenseElementsAttr.get(attrs)
except RuntimeError as e:
# CHECK: Invalid attribute when attempting to create an ArrayAttribute
print(e)
# CHECK-LABEL: TEST: testGetDenseElementsFromListMismatchedType
@run
def testGetDenseElementsFromListMismatchedType():
with Context(), Location.unknown():
attrs = [FloatAttr.get(F64Type.get(), 1.0), FloatAttr.get(F64Type.get(), 2.0)]
shaped_type = ShapedType(Type.parse("tensor<2xf32>"))
try:
attr = DenseElementsAttr.get(attrs, shaped_type)
except ValueError as e:
# CHECK: All attributes must be of the same type and match the type parameter
print(e)
# CHECK-LABEL: TEST: testGetDenseElementsFromListMixedTypes
@run
def testGetDenseElementsFromListMixedTypes():
with Context(), Location.unknown():
attrs = [FloatAttr.get(F64Type.get(), 1.0), FloatAttr.get(F32Type.get(), 2.0)]
try:
attr = DenseElementsAttr.get(attrs)
except ValueError as e:
# CHECK: All attributes must be of the same type and match the type parameter
print(e)
################################################################################
# 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.int8)
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))
### 1 bit/boolean integer arrays
# CHECK-LABEL: TEST: testGetDenseElementsI1Signless
@run
def testGetDenseElementsI1Signless():
with Context():
array = np.array([True], dtype=np.bool_)
attr = DenseElementsAttr.get(array)
# CHECK: dense<true> : tensor<1xi1>
print(attr)
# CHECK{LITERAL}: [ True]
print(np.array(attr))
array = np.array([[True, False, True], [True, True, False]], dtype=np.bool_)
attr = DenseElementsAttr.get(array)
# CHECK{LITERAL}: dense<[[true, false, true], [true, true, false]]> : tensor<2x3xi1>
print(attr)
# CHECK{LITERAL}: [[ True False True]
# CHECK{LITERAL}: [ True True False]]
print(np.array(attr))
array = np.array(
[[True, True, False, False], [True, False, True, False]], dtype=np.bool_
)
attr = DenseElementsAttr.get(array)
# CHECK{LITERAL}: dense<[[true, true, false, false], [true, false, true, false]]> : tensor<2x4xi1>
print(attr)
# CHECK{LITERAL}: [[ True True False False]
# CHECK{LITERAL}: [ True False True False]]
print(np.array(attr))
array = np.array(
[
[True, True, False, False],
[True, False, True, False],
[False, False, False, False],
[True, True, True, True],
[True, False, False, True],
],
dtype=np.bool_,
)
attr = DenseElementsAttr.get(array)
# 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>
print(attr)
# CHECK{LITERAL}: [[ True True False False]
# CHECK{LITERAL}: [ True False True False]
# CHECK{LITERAL}: [False False False False]
# CHECK{LITERAL}: [ True True True True]
# CHECK{LITERAL}: [ True False False True]]
print(np.array(attr))
array = np.array(
[
[True, True, False, False, True, True, False, False, False],
[False, False, False, True, False, True, True, False, True],
],
dtype=np.bool_,
)
attr = DenseElementsAttr.get(array)
# CHECK{LITERAL}: dense<[[true, true, false, false, true, true, false, false, false], [false, false, false, true, false, true, true, false, true]]> : tensor<2x9xi1>
print(attr)
# CHECK{LITERAL}: [[ True True False False True True False False False]
# CHECK{LITERAL}: [False False False True False True True False True]]
print(np.array(attr))
array = np.array([], dtype=np.bool_)
attr = DenseElementsAttr.get(array)
# CHECK: dense<> : tensor<0xi1>
print(attr)
# CHECK{LITERAL}: []
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)
array = np.array([1, 2, 3], dtype=np.int64)
attr = DenseIntElementsAttr.get(array, type=VectorType.get([3], idx_type))
# CHECK: [1, 2, 3]
print(list(DenseIntElementsAttr(attr)))
# CHECK-LABEL: TEST: testGetDenseResourceElementsAttr
@run
def testGetDenseResourceElementsAttr():
def on_delete(_):
print("BACKING MEMORY DELETED")
context = Context()
mview = memoryview(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
ref = weakref.ref(mview, on_delete)
def test_attribute(context, mview):
with context, Location.unknown():
element_type = IntegerType.get_signless(32)
tensor_type = RankedTensorType.get((2, 3), element_type)
resource = DenseResourceElementsAttr.get_from_buffer(
mview, "from_py", tensor_type
)
module = Module.parse("module {}")
module.operation.attributes["test.resource"] = resource
# CHECK: test.resource = dense_resource<from_py> : tensor<2x3xi32>
# CHECK: from_py: "0x04000000010000000200000003000000040000000500000006000000"
print(module)
# Verifies type casting.
# CHECK: dense_resource<from_py> : tensor<2x3xi32>
print(
DenseResourceElementsAttr(module.operation.attributes["test.resource"])
)
test_attribute(context, mview)
mview = None
gc.collect()
# CHECK: FREEING CONTEXT
print("FREEING CONTEXT")
context = None
gc.collect()
# CHECK: BACKING MEMORY DELETED
# CHECK: EXIT FUNCTION
print("EXIT FUNCTION")
# CHECK-LABEL: TEST: testGetDenseResourceElementsAttrNdarrayI32
@run
def testGetDenseResourceElementsAttrNdarrayI32():
class DLPackWrapper:
def __init__(self, array: np.ndarray):
self.dlpack_capsule = array.__dlpack__()
def __del__(self):
print("DLPACK MEMORY DELETED")
def get_capsule(self):
return self.dlpack_capsule
context = Context()
mview_int32 = DLPackWrapper(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
def test_attribute_int32(context, mview_int32):
with context, Location.unknown():
element_type = IntegerType.get_signless(32)
tensor_type = RankedTensorType.get((2, 3), element_type)
resource = DenseResourceElementsAttr.get_from_ndarray(
mview_int32.get_capsule(), "from_py", tensor_type
)
module = Module.parse("module {}")
module.operation.attributes["test.resource"] = resource
# CHECK: test.resource = dense_resource<from_py> : tensor<2x3xi32>
# CHECK: from_py: "0x01000000010000000200000003000000040000000500000006000000"
print(module)
# Verifies type casting.
# CHECK: dense_resource<from_py> : tensor<2x3xi32>
print(
DenseResourceElementsAttr(module.operation.attributes["test.resource"])
)
test_attribute_int32(context, mview_int32)
del mview_int32
gc.collect()
# CHECK: DLPACK MEMORY DELETED
# CHECK: FREEING CONTEXT
print("FREEING CONTEXT")
context = None
gc.collect()
# CHECK: EXIT FUNCTION
print("EXIT FUNCTION")
# CHECK-LABEL: TEST: testGetDenseResourceElementsAttrNdarrayF32
@run
def testGetDenseResourceElementsAttrNdarrayF32():
class DLPackWrapper:
def __init__(self, array: np.ndarray):
self.dlpack_capsule = array.__dlpack__()
def __del__(self):
print("DLPACK MEMORY DELETED")
def get_capsule(self):
return self.dlpack_capsule
context = Context()
mview_float32 = DLPackWrapper(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32))
def test_attribute_float32(context, mview_float32):
with context, Location.unknown():
element_type = FloatAttr.get_f32(32.0)
tensor_type = RankedTensorType.get((2, 3), element_type.type)
resource = DenseResourceElementsAttr.get_from_ndarray(
mview_float32.get_capsule(), "from_py", tensor_type
)
module = Module.parse("module {}")
module.operation.attributes["test.resource"] = resource
# CHECK: test.resource = dense_resource<from_py> : tensor<2x3xf32>
# CHECK: from_py: "0x010000000000803F0000004000004040000080400000A0400000C040"
print(module)
# Verifies type casting.
# CHECK: dense_resource<from_py> : tensor<2x3xf32>
print(
DenseResourceElementsAttr(module.operation.attributes["test.resource"])
)
test_attribute_float32(context, mview_float32)
del mview_float32
gc.collect()
# CHECK: DLPACK MEMORY DELETED
# CHECK: FREEING CONTEXT
print("FREEING CONTEXT")
context = None
gc.collect()
# CHECK: EXIT FUNCTION
print("EXIT FUNCTION")
# CHECK-LABEL: TEST: testGetDenseResourceElementsAttrNonShapedType
@run
def testGetDenseResourceElementsAttrNonShapedType():
with Context(), Location.unknown():
mview = np.array([1], dtype=np.int32).__dlpack__()
t = F32Type.get()
try:
attr = DenseResourceElementsAttr.get_from_ndarray(mview, "from_py", t)
except ValueError as e:
# CHECK: Constructing a DenseResourceElementsAttr requires a ShapedType.
print(e)