6
6
from mlir .ir import *
7
7
import numpy as np
8
8
import weakref
9
+ import ctypes
9
10
10
11
11
12
def run (f ):
@@ -617,3 +618,111 @@ def test_attribute(context, mview):
617
618
# CHECK: BACKING MEMORY DELETED
618
619
# CHECK: EXIT FUNCTION
619
620
print ("EXIT FUNCTION" )
621
+
622
+
623
+ # CHECK-LABEL: TEST: testGetDenseResourceElementsAttrNdarrayI32
624
+ @run
625
+ def testGetDenseResourceElementsAttrNdarrayI32 ():
626
+ class DLPackWrapper :
627
+ def __init__ (self , array : np .ndarray ):
628
+ self .dlpack_capsule = array .__dlpack__ ()
629
+
630
+ def __del__ (self ):
631
+ print ("BACKING MEMORY DELETED" )
632
+
633
+ def get_capsule (self ):
634
+ return self .dlpack_capsule
635
+
636
+ context = Context ()
637
+ mview_int32 = DLPackWrapper (np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]], dtype = np .int32 ))
638
+
639
+ def test_attribute_int32 (context , mview_int32 ):
640
+ with context , Location .unknown ():
641
+ element_type = IntegerType .get_signless (32 )
642
+ tensor_type = RankedTensorType .get ((2 , 3 ), element_type )
643
+ resource = DenseResourceElementsAttr .get_from_ndarray (
644
+ mview_int32 .get_capsule (), "from_py" , tensor_type
645
+ )
646
+ module = Module .parse ("module {}" )
647
+ module .operation .attributes ["test.resource" ] = resource
648
+ # CHECK: test.resource = dense_resource<from_py> : tensor<2x3xi32>
649
+ # CHECK: from_py: "0x01000000010000000200000003000000040000000500000006000000"
650
+ print (module )
651
+
652
+ # Verifies type casting.
653
+ # CHECK: dense_resource<from_py> : tensor<2x3xi32>
654
+ print (
655
+ DenseResourceElementsAttr (module .operation .attributes ["test.resource" ])
656
+ )
657
+
658
+ test_attribute_int32 (context , mview_int32 )
659
+ del mview_int32
660
+ gc .collect ()
661
+ # CHECK: BACKING MEMORY DELETED
662
+ # CHECK: FREEING CONTEXT
663
+ print ("FREEING CONTEXT" )
664
+ context = None
665
+ gc .collect ()
666
+ # CHECK: EXIT FUNCTION
667
+ print ("EXIT FUNCTION" )
668
+
669
+
670
+ # CHECK-LABEL: TEST: testGetDenseResourceElementsAttrNdarrayF32
671
+ @run
672
+ def testGetDenseResourceElementsAttrNdarrayF32 ():
673
+ class DLPackWrapper :
674
+ def __init__ (self , array : np .ndarray ):
675
+ self .dlpack_capsule = array .__dlpack__ ()
676
+
677
+ def __del__ (self ):
678
+ print ("BACKING MEMORY DELETED" )
679
+
680
+ def get_capsule (self ):
681
+ return self .dlpack_capsule
682
+
683
+ context = Context ()
684
+ mview_float32 = DLPackWrapper (np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]], dtype = np .float32 ))
685
+
686
+ def test_attribute_float32 (context , mview_float32 ):
687
+ with context , Location .unknown ():
688
+ element_type = FloatAttr .get_f32 (32.0 )
689
+ tensor_type = RankedTensorType .get ((2 , 3 ), element_type .type )
690
+ resource = DenseResourceElementsAttr .get_from_ndarray (
691
+ mview_float32 .get_capsule (), "from_py" , tensor_type
692
+ )
693
+ module = Module .parse ("module {}" )
694
+ module .operation .attributes ["test.resource" ] = resource
695
+ # CHECK: test.resource = dense_resource<from_py> : tensor<2x3xf32>
696
+ # CHECK: from_py: "0x010000000000803F0000004000004040000080400000A0400000C040"
697
+ print (module )
698
+
699
+ # Verifies type casting.
700
+ # CHECK: dense_resource<from_py> : tensor<2x3xf32>
701
+ print (
702
+ DenseResourceElementsAttr (module .operation .attributes ["test.resource" ])
703
+ )
704
+
705
+ test_attribute_float32 (context , mview_float32 )
706
+ del mview_float32
707
+ gc .collect ()
708
+ # CHECK: BACKING MEMORY DELETED
709
+ # CHECK: FREEING CONTEXT
710
+ print ("FREEING CONTEXT" )
711
+ context = None
712
+ gc .collect ()
713
+ # CHECK: EXIT FUNCTION
714
+ print ("EXIT FUNCTION" )
715
+
716
+
717
+ # CHECK-LABEL: TEST: testGetDenseResourceElementsAttrNonShapedType
718
+ @run
719
+ def testGetDenseResourceElementsAttrNonShapedType ():
720
+ with Context (), Location .unknown ():
721
+ mview = np .array ([1 ], dtype = np .int32 ).__dlpack__ ()
722
+ t = F32Type .get ()
723
+
724
+ try :
725
+ attr = DenseResourceElementsAttr .get_from_ndarray (mview , "from_py" , t )
726
+ except ValueError as e :
727
+ # CHECK: Constructing a DenseResourceElementsAttr requires a ShapedType.
728
+ print (e )
0 commit comments