File: kernel-diagnostics.py

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (220 lines) | stat: -rw-r--r-- 6,928 bytes parent folder | download
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
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

import argparse
import copy
import os
from collections import OrderedDict

import yaml

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))


def parse_spec():
    specdict = OrderedDict()
    with open(os.path.join(CURRENT_DIR, "..", "kernel-specification.yml")) as specfile:
        indspec = yaml.safe_load(specfile)["kernels"]
        for spec in indspec:
            specdict[spec["name"]] = []
            for childfunc in spec["specializations"]:
                specdict[spec["name"]].append(childfunc["name"])
    return specdict


def sort_specializations(keystring):
    ordering = [
        "bool",
        "int8",
        "int16",
        "int32",
        "int64",
        "u8",
        "uint8",
        "u16",
        "uint16",
        "u32",
        "uint32",
        "u64",
        "uint64",
        "float16",
        "float32",
        "float64",
        "float128",
        "complex64",
        "complex128",
        "complex256",
    ]
    elemsfound = []
    keystring = keystring.lower()
    while any(element in keystring for element in ordering):
        for i, element in enumerate(ordering):
            if element in keystring and not (
                element.startswith("int")
                and keystring[keystring.find(element) - 1] == "u"
            ):
                elemsfound.append((keystring.find(element), i))
                keystring = keystring.replace(element, "", 1)
    elemsfound.sort()
    if len(elemsfound) == 0:
        return keystring
    elif len(elemsfound) == 1:
        return (elemsfound[0][1], 0)
    else:
        return (elemsfound[0][1], elemsfound[1][1])


def check_specorder(kerneldict):
    print("Checking kernel specification order...")
    kernelnames = []
    with open(os.path.join(CURRENT_DIR, "..", "kernel-specification.yml")) as specfile:
        indspec = yaml.safe_load(specfile)["kernels"]
        for spec in indspec:
            kernelnames.append(spec["name"])
    count = 0
    for dictkernel in kerneldict.keys():
        if dictkernel != kernelnames[count]:
            print("Order in specification: ", kerneldict.keys())
            print("Sorted order: ", kernelnames)
            raise Exception("Kernels not sorted in specification")
        count += 1
    for kernel, specializations in kerneldict.items():
        display = []
        try:
            count = 0
            display = []
            flag = False
            for specialization in sorted(
                copy.copy(specializations), key=sort_specializations
            ):
                if specialization != specializations[count]:
                    flag = True
                display.append(specialization)
                count += 1
        except TypeError:
            count = 0
            display = []
            flag = False
            for specialization in sorted(copy.copy(specializations)):
                if specialization != specializations[count]:
                    flag = True
                display.append(specialization)
                count += 1
        if flag:
            print("For kernel: " + kernel)
            print("Order in specification = ", specializations)
            print("Sorted order = ", display)
            raise Exception("Kernel specializations not sorted in specification")
    print("Kernel specification file is properly sorted")


def check_spec_implementation():
    count = 0
    with open(os.path.join(CURRENT_DIR, "..", "kernel-specification.yml")) as specfile:
        indspec = yaml.safe_load(specfile)["kernels"]
        for spec in indspec:
            if "def awkward" not in spec["definition"]:
                if count == 0:
                    print("\nKernels not implemented in specification file - ")
                print(spec["name"])
                count += 1


def check_cpu_implementation(kerneldict):
    count = 0
    for kernelname, specializations in kerneldict.items():
        if os.path.isfile(
            os.path.join(
                CURRENT_DIR,
                "..",
                "awkward-cpp",
                "src",
                "cpu-kernels",
                kernelname + ".cpp",
            )
        ):
            with open(
                os.path.join(
                    CURRENT_DIR,
                    "..",
                    "awkward-cpp",
                    "src",
                    "cpu-kernels",
                    kernelname + ".cpp",
                )
            ) as kernelfile:
                contents = kernelfile.read()
                for childname in specializations:
                    if childname not in contents:
                        if count == 0:
                            print("\nKernels not implemented as a CPU kernel - ")
                        print(kernelname + ": " + childname)
                        count += 1
        else:
            if count == 0:
                print("\nKernels not implemented as a CPU kernel - ")
            print(kernelname)
            count += 1


def check_cuda_implementation(kerneldict):
    count = 0
    for kernelname in kerneldict.keys():
        if not (
            os.path.isfile(
                os.path.join(
                    CURRENT_DIR,
                    "..",
                    "awkward-cpp",
                    "src",
                    "cuda-kernels",
                    kernelname + ".cu",
                )
            )
            or os.path.isfile(
                os.path.join(
                    CURRENT_DIR,
                    "..",
                    "awkward-cpp",
                    "src",
                    "cuda-kernels",
                    "manual_" + kernelname + ".cu",
                )
            )
        ):
            if count == 0:
                print("\nKernels not implemented as a CUDA kernel - ")
            print(kernelname)
            count += 1


def check_implementations(kerneldict):
    print("Checking if kernels are implemented everywhere...")
    check_spec_implementation()
    check_cpu_implementation(kerneldict)
    check_cuda_implementation(kerneldict)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Awkward Kernel Diagnostics Tool")
    parser.add_argument(
        "--check-spec-sorted",
        action="store_true",
        help="Check if kernel specification file is sorted",
    )
    parser.add_argument(
        "--check-implemented",
        action="store_true",
        help="Check if kernel is present in specification file, as a CPU kernel and as a CUDA kernel",
    )
    args = parser.parse_args()
    kernels = parse_spec()
    count = 0
    if args.check_spec_sorted:
        check_specorder(kernels)
        count += 1
    if args.check_implemented:
        if count != 0:
            print("\n")
        check_implementations(kernels)