run_verification.py 5.12 KB
Newer Older
yangzhong's avatar
yangzhong committed
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
#! /usr/bin/env python3
# Copyright 2018-2022 The MLPerf Authors. All Rights Reserved.
#
# 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 os
import sys
import shutil
import argparse
import json

import numpy as np

DTYPE_MAP = {
    "int64": np.int64,
    "int32": np.int32,
    "int16": np.int16,
    "float32": np.float32,
}


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--compliance_dir",
        "-c",
        help="Specifies the path to the directory containing the logs from the compliance test run.",
        required=True,
    )
    parser.add_argument(
        "--output_dir",
        "-o",
        help="Specifies the path to the output directory where compliance logs will be uploaded from, i.e. inference_results_v0.7/closed/NVIDIA/compliance/T4x8/resnet/Offline.",
        required=True,
    )
    parser.add_argument(
        "--eos_token_id", "-e", default=2, help="EOS token id of the tokenizer"
    )
    parser.add_argument(
        "--dtype", "-d", default="int32", choices=["int64", "int32", "int16", "float32"]
    )
    parser.add_argument(
        "--scenario",
        "-s",
        required=True,
        choices=["Offline", "Server", "SingleStream", "MultiStream"],
    )
    args = parser.parse_args()
    return args


def eos_check(acc_data, dtype, eos_token_id=2):
    for sample in acc_data:
        data = np.frombuffer(bytes.fromhex(sample["data"]), dtype=dtype)
        i = data.shape[0] - 1
        n_eos_tokens = 0
        while i > 0:
            if data[i] == eos_token_id:
                n_eos_tokens += 1
            if n_eos_tokens >= 2:
                return False
            if data[i] != eos_token_id:
                break
            i -= 1
    return True


def first_token_check(acc_data, dtype):
    for sample in acc_data:
        data = np.frombuffer(bytes.fromhex(sample["data"]), dtype=dtype)
        token_data = np.frombuffer(
            bytes.fromhex(
                sample["token_data"]),
            dtype=dtype)
        for t1, t2 in zip(data, token_data):
            if t1 != t2:
                return False

    return True


def sample_len_check(acc_data, dtype):
    for sample in acc_data:
        data = np.frombuffer(bytes.fromhex(sample["data"]), dtype=dtype)
        token_count = int(sample["token_count"])
        if len(data) != token_count:
            return False
    return True


def main():
    args = get_args()
    accuracy_file = os.path.join(
        args.compliance_dir,
        "mlperf_log_accuracy.json")

    with open(accuracy_file, "r") as acc_json:
        acc_data = json.load(acc_json)

    try:
        eos_pass = eos_check(acc_data,
                             DTYPE_MAP[args.dtype],
                             args.eos_token_id)
    except Exception:
        print("Unexpected error occured while doing the EOS check")
        eos_pass = False

    need_first_token_check = args.scenario != "Offline"
    first_token_pass = True
    if need_first_token_check:
        try:
            first_token_pass = first_token_check(
                acc_data, DTYPE_MAP[args.dtype])
        except Exception:
            print("Unexpected error occured while doing the first token check")
            first_token_pass = False

    sample_len_pass = sample_len_check(acc_data, DTYPE_MAP[args.dtype])

    # Construct output based on the results of checks
    output = ""
    # Add first token check
    if need_first_token_check:
        output += f"First token check pass: {first_token_pass}\n"
    else:
        output += f"First token check pass: Skipped\n"

    # Add EOS check
    output += f"EOS check pass: {eos_pass}\n"

    # Add sample length check
    output += f"Sample length check pass: {sample_len_pass}\n"

    if eos_pass and first_token_pass and sample_len_pass:
        output += "TEST06 verification complete\n"
    else:
        output += "TEST06 verification failed\n"

    # Output test output to console and folder
    output_dir = os.path.join(args.output_dir, "TEST06")
    output_accuracy_dir = os.path.join(output_dir, "accuracy")

    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)
    if not os.path.isdir(output_accuracy_dir):
        os.makedirs(output_accuracy_dir)

    with open(os.path.join(output_dir, "verify_accuracy.txt"), "w") as f:
        f.write(output)

    try:
        shutil.copy2(accuracy_file, output_accuracy_dir)
    except Exception:
        print(
            "Exception occured trying to copy "
            + accuracy_file
            + " to "
            + output_accuracy_dir
        )
    print(output)


if __name__ == "__main__":
    main()