File: synthdnn.py

package info (click to toggle)
onednn 3.9.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,124 kB
  • sloc: cpp: 850,217; ansic: 37,403; lisp: 16,757; python: 3,463; asm: 831; sh: 78; javascript: 66; makefile: 41
file content (195 lines) | stat: -rwxr-xr-x 6,259 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
#! /bin/python3
################################################################################
# Copyright 2025 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################

import argparse
import os
import sys
from tempfile import NamedTemporaryFile

from matmul import sampler as matmul_sampler
from matmul import primitive as matmul


def log(output):
    print("synthdnn: " + output)


def error(output):
    print("synthdnn: error: " + output)
    exit(1)


def write_batch_file(batch_file, samples):
    batch_file.write("#### Auto-generated by synthdnn\n")
    batch_file.write(f"#### python3 synthdnn.py {' '.join(sys.argv[1:])}\n\n")
    for s in samples:
        batch_file.write(f"--reset {s.benchdnn_str()}\n")
    batch_file.flush()


def setup_collect_subparser(subparsers):
    collect_parser = subparsers.add_parser(
        "collect", help="call with -h for information"
    )
    collect_parser.add_argument(
        "--subprogram_main", default=collect_main, help=argparse.SUPPRESS
    )

    collect_parser.add_argument(
        "-b",
        "--batch-file",
        required=True,
        help="batch file used for the operation",
    )

    # Interface with benchdnn
    collect_parser.add_argument(
        "benchdnn", help="path to benchdnn executable"
    )
    collect_parser.add_argument(
        "--engine", default="cpu", help="engine used for benchdnn execution"
    )
    collect_parser.add_argument(
        "--impl",
        default=None,
        help="implementation to use in benchdnn execution",
    )
    collect_parser.add_argument(
        "--skip-impl",
        default=None,
        help="implementation to skip in benchdnn execution",
    )
    collect_parser.add_argument(
        "--collect",
        default="corr",
        help="benchdnn collection type, can be one of [corr, perf]",
    )
    collect_parser.add_argument("-n", "--name", default="", help="sample name")


def get_optional_args(args):
    optional_args = []
    if args.impl:
        optional_args.append(f"--impl={args.impl}")
    if args.skip_impl:
        optional_args.append(f"--skip-impl={args.skip_impl}")

    if len(optional_args) > 0:
        return " ".join(optional_args) + " "

    return ""


def collect_main(args):
    # args.benchdnn may be a list depending on command line setup
    benchdnn = args.benchdnn
    if type(benchdnn) is list:
        benchdnn = benchdnn[0]

    if not os.path.exists(benchdnn):
        error(f"cannot execute {benchdnn}, no such file exists")

    if args.collect == "corr":
        benchdnn_args = f"--engine={args.engine} --matmul --mode-modifier=P {get_optional_args(args)}"
    elif args.collect == "perf":
        benchdnn_args = f"--engine={args.engine} --matmul --mode=F --cold-cache=all --perf-template=sample,{args.name},%prb%,%0Gflops%,%0Gbw% --memory-kind=usm_device --attr-scratchpad=user {get_optional_args(args)}"
        if args.name.find(",") != -1:
            error(f"sample name {args.name} contains invalid character: ,")
    else:
        error(f"unknown collection method {args.collect}")

    cmd = f"{benchdnn} {benchdnn_args} --batch={args.batch_file}"
    log(f"executing: {cmd}")
    ret = os.system(cmd)
    log("execution complete")
    if ret != 0:
        error(f"execution of {cmd} failed with return code {ret}")


def setup_matmul_subparser(subparsers):
    matmul_parser = subparsers.add_parser(
        "matmul", help="call with -h for information"
    )
    matmul_parser.add_argument(
        "--subprogram_main", default=matmul_main, help=argparse.SUPPRESS
    )

    matmul_parser.add_argument(
        "-b",
        "--batch-file",
        default=None,
        help="batch file to write results to",
    )

    # Sampler Arguments
    matmul_parser.add_argument(
        "-l",
        "--layouts",
        default="all",
        help='stag:wtag:dtag, comma separated list of layouts or "all" for every supported layout',
    )
    matmul_parser.add_argument(
        "-m",
        "--iter-mode",
        default="zip",
        help="iteration mode, must be one of zip or product",
    )
    matmul_parser.add_argument(
        "-r",
        "--region",
        default="(1,1,1,1):(8,8192,8192,8192):(1,1,1,1)",
        help="([b_min,]m_min,n_min,k_min):([b_max,]m_max,n_max,k_max):([b_align,]m_align,n_align,k_align)",
    )
    matmul_parser.add_argument(
        "-s", "--samples", default=1000, help="number of samples to collect"
    )
    matmul_parser.add_argument(
        "-t",
        "--types",
        default="*",
        help='dt:dt:dt(optional fpmath-mode), comma separated list of type configurations. "%%N" will match the Nth given data type. Giving a single data type instead of 3 will match any supported configurations using that type.',
    )


def matmul_main(args):
    batch_file = (
        open(args.batch_file, "w+t") if args.batch_file is not None else None
    )

    region = matmul_sampler.Region(args.region)
    types = matmul.Types(args.types)
    layouts = matmul.Layouts(args.layouts, region.ndims - 1)
    samples = matmul_sampler.Sampler(
        int(args.samples), args.iter_mode, types, layouts, region
    )
    if batch_file:
        log(f"generating batch file: {args.batch_file}")
        write_batch_file(batch_file, samples)
        log(f"generation complete")
    else:
        write_batch_file(sys.stdout, samples)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(
        help="primitive targeted for data collection"
    )
    setup_collect_subparser(subparsers)
    setup_matmul_subparser(subparsers)
    args = parser.parse_args()
    args.subprogram_main(args)