|
| 1 | +# Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | +import sys |
| 17 | +import yaml |
| 18 | +import os |
| 19 | +import glob |
| 20 | + |
| 21 | + |
| 22 | +class TestOutputValidator: |
| 23 | + """ |
| 24 | + Functions that validate the output |
| 25 | + of the test |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, config, test_name, results_path, tolerance): |
| 29 | + self._config = config |
| 30 | + self._models = list(config['profile_models']) |
| 31 | + self._result_path = results_path |
| 32 | + self._tolerance = tolerance |
| 33 | + |
| 34 | + check_function = self.__getattribute__(f'check_{test_name}') |
| 35 | + |
| 36 | + if check_function(): |
| 37 | + sys.exit(0) |
| 38 | + else: |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + def check_metrics_stability(self): |
| 42 | + """ |
| 43 | + Makes sure that the same configuration |
| 44 | + appears as the best across iterations for |
| 45 | + each model |
| 46 | + """ |
| 47 | + |
| 48 | + # There should be 4 csv files the results path |
| 49 | + pathname = os.path.join(self._result_path, 'result_*.csv') |
| 50 | + csv_contents = [] |
| 51 | + for filename in glob.glob(pathname): |
| 52 | + with open(filename, 'r+') as f: |
| 53 | + csv_contents.append(f.read()) |
| 54 | + |
| 55 | + # Now in the first csv, get the metrics |
| 56 | + metric_values = {} |
| 57 | + for csv in csv_contents: |
| 58 | + csv_lines = csv.split('\n') |
| 59 | + for line in csv_lines[1:-2]: |
| 60 | + model, _, _, gpu_memory, gpu_utilization = line.split(',') |
| 61 | + if model in metric_values: |
| 62 | + metric_values[model]['gpu_memory'].append(float(gpu_memory)) |
| 63 | + metric_values[model]['gpu_utilization'].append( |
| 64 | + float(gpu_utilization)) |
| 65 | + else: |
| 66 | + metric_values[model] = { |
| 67 | + 'gpu_memory': [float(gpu_memory)], |
| 68 | + 'gpu_utilization': [float(gpu_utilization)] |
| 69 | + } |
| 70 | + |
| 71 | + # Compare metrics |
| 72 | + for model in metric_values: |
| 73 | + for metric, values in metric_values[model].items(): |
| 74 | + start_value = values[0] |
| 75 | + for value in values[1:]: |
| 76 | + deviation_percent = abs( |
| 77 | + (value - start_value) / start_value) * 100 |
| 78 | + if deviation_percent > self._tolerance: |
| 79 | + print( |
| 80 | + f"\n***" |
| 81 | + f"\n*** For model {model}, value for metric {metric}" |
| 82 | + "\n*** is unstable.\n***\n" |
| 83 | + f"\n***\n*** Expected: {start_value} +/- {self._tolerance*start_value/100}." |
| 84 | + f"\n*** Found: {values[1:]}.\n***") |
| 85 | + return False |
| 86 | + return True |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + parser = argparse.ArgumentParser() |
| 91 | + parser.add_argument('-f', |
| 92 | + '--config-file', |
| 93 | + type=str, |
| 94 | + required=True, |
| 95 | + help='The path to the config yaml file.') |
| 96 | + parser.add_argument('-r', |
| 97 | + '--inference-results-path', |
| 98 | + type=str, |
| 99 | + required=True, |
| 100 | + help='The full path to the analyzer log.') |
| 101 | + parser.add_argument('-t', |
| 102 | + '--test-name', |
| 103 | + type=str, |
| 104 | + required=True, |
| 105 | + help='The name of the test to be run.') |
| 106 | + parser.add_argument( |
| 107 | + '--tolerance', |
| 108 | + type=int, |
| 109 | + default=10, |
| 110 | + help='The percent tolerance allowed for the metrics to vary.') |
| 111 | + |
| 112 | + args = parser.parse_args() |
| 113 | + |
| 114 | + with open(args.config_file, 'r') as f: |
| 115 | + config = yaml.safe_load(f) |
| 116 | + |
| 117 | + TestOutputValidator(config, args.test_name, args.inference_results_path, |
| 118 | + args.tolerance) |
0 commit comments