Debian Codegraph @

tensilelite/rocisa/test/test_base.py

raw
1 ################################################################################
2 #
3 # Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cop-
9 # ies of the Software, and to permit persons to whom the Software is furnished
10 # to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in all
13 # copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM-
16 # PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNE-
20 # CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ################################################################################
22
23 import rocisa
24 from copy import deepcopy
25 import pickle
26 import os
27
28 isa = (9,0,10)
29
30 def fastdeepcopy(x):
31 # Note: Some object can't be pickled
32 return pickle.loads(pickle.dumps(x))
33
34 def getInstance():
35 return rocisa.rocIsa.getInstance()
36
37 def getGfxName(isa):
38 return rocisa.isaToGfx(isa)
39
40 def test_rocisa():
41 rocm_path = os.environ.get("ROCM_PATH", "/opt/rocm")
42 global_isa = rocisa.rocIsa.getInstance()
43 global_isa.init(isa, rocm_path + "/bin/amdclang++", False)
44 global_isa.setKernel(isa, 64)
45
46 ki = global_isa.getKernel()
47
48 assert global_isa.getAsmCaps()["v_mac_f16"] == 1
49 assert global_isa.getKernel().isa == isa
50
51 assert ki.isa == isa
52 assert isa == ki.isa
53 assert (0,0,0,0) != ki.isa
54 assert ki.isa == ki.isa
55
56 class Item2(rocisa.base.Item):
57 def __init__(self, name):
58 super().__init__(name)
59 self.itemList = []
60
61 def __getstate__(self):
62 base_state = super().__getstate__()
63 py_state = {key: value for key, value in self.__dict__.items() if not key.startswith("__")}
64 return (base_state, py_state)
65
66 def __setstate__(self, state):
67 base_state, py_state = state
68 super().__setstate__(base_state)
69 self.__dict__.update(py_state)
70
71 def __deepcopy__(self, memo):
72 assert 0, "Not implemented"
73
74 def print(self):
75 for i in self.itemList:
76 print(i)
77
78 def test_item():
79 item = rocisa.base.Item("Tested Item")
80 print("Test property 1:", item.asmCaps["v_mac_f16"])
81 print("Test property 2:", item.kernel.isa)
82 print("PrettyPrint:", item.prettyPrint(""))
83
84
85 item2 = Item2("Tested Item 2")
86 item2.itemList.append(rocisa.base.Item("Tested Item 3"))
87 item2.print()
88
89 def test_copy():
90 getInstance2 = fastdeepcopy(getInstance)
91 global_isa = rocisa.rocIsa.getInstance()
92 global_isa2 = getInstance2()
93 assert global_isa is global_isa2
94
95 ki = global_isa.getKernel()
96 ki2 = fastdeepcopy(ki)
97
98 item = rocisa.base.Item("Tested Item")
99 item2 = deepcopy(item)
100 item3 = fastdeepcopy(item)
101 print("Copied item using deepcopy:", item2.kernel.isa)
102 print("Copied item using pickle:", item3.kernel.isa)
103 item2 = Item2("Tested Item 2")
104 item2.itemList.append(Item2("Tested Item 3"))
105 item2.itemList[0].parent = item2
106 print(item2.itemList[0].parent.name)
107 item3 = fastdeepcopy(item2)
108 print("Copied item using pickle:", item3.name)
109
110 deepcopiedFunction = fastdeepcopy(getGfxName)
111 print("This is a deepcopied function:", deepcopiedFunction(isa))
112
113 def test_functions():
114 print("GLC:", rocisa.getGlcBitName(True))
115 print("SLC:", rocisa.getSlcBitName(False))
116
117 test_rocisa()
118 test_item()
119 test_copy()
120 test_functions()