|
| 1 | +import warnings |
| 2 | +import os |
| 3 | + |
| 4 | +from ..compliance_checker import mlp_parser |
| 5 | + |
| 6 | +# What are source files? |
| 7 | +SOURCE_FILE_EXT = { |
| 8 | + '.py', '.cc', '.cpp', '.cxx', '.c', '.h', '.hh', '.hpp', '.hxx', '.sh', |
| 9 | + '.sub', '.cu', '.cuh' |
| 10 | +} |
| 11 | + |
| 12 | + |
| 13 | +def is_source_file(path): |
| 14 | + """ Check if a file is considered as a "source file" by extensions. |
| 15 | +
|
| 16 | + The extensions that are considered as "source file" are listed in |
| 17 | + SOURCE_FILE_EXT. |
| 18 | +
|
| 19 | + Args: |
| 20 | + path: The absolute path, relative path or name to/of the file. |
| 21 | + """ |
| 22 | + return os.path.splitext(path)[1].lower() in SOURCE_FILE_EXT |
| 23 | + |
| 24 | + |
| 25 | +def find_source_files_under(path): |
| 26 | + """ Find all source files in all sub-directories under a directory. |
| 27 | +
|
| 28 | + Args: |
| 29 | + path: The absolute or relative path to the directory under query. |
| 30 | + """ |
| 31 | + source_files = [] |
| 32 | + for root, subdirs, files in os.walk(path): |
| 33 | + for file_name in files: |
| 34 | + if is_source_file(file_name): |
| 35 | + source_files.append(os.path.join(root, file_name)) |
| 36 | + return source_files |
| 37 | + |
| 38 | + |
| 39 | +class SeedChecker: |
| 40 | + """ Check if the seeds fit MLPerf submission requirements. |
| 41 | + Current requirements are: |
| 42 | +
|
| 43 | + 1. All seeds must be logged through mllog (if choose to log seeds). Any seed |
| 44 | + logged via any other method will be discarded. |
| 45 | + 2. All seeds, if choose to be logged, must be valid integer (convertible via |
| 46 | + int()). |
| 47 | + 3. If any run log at least one seed, we expect all runs to log at least |
| 48 | + one seed. |
| 49 | + 4. The set of seed(s) that one run logs must be completely different from |
| 50 | + the set of seed(s) any other run logs. |
| 51 | + 4. If one run logs one seed on a certain line in a certain source file, no |
| 52 | + other run can log the same seed on the same line in the same file. |
| 53 | +
|
| 54 | + Unsatisfying any of the above requirements results in check failure. |
| 55 | +
|
| 56 | + A warning is raised for the following situations: |
| 57 | +
|
| 58 | + 1. Any run logs more than one seed. |
| 59 | + 2. No seed is logged. However, the source files contain the keyword whose |
| 60 | + lowercase is "seed". What files are considered as source files are |
| 61 | + defined in SOURCE_FILE_EXT and is_source_file(). |
| 62 | +
|
| 63 | + """ |
| 64 | + def __init__(self, ruleset): |
| 65 | + self._ruleset = ruleset |
| 66 | + |
| 67 | + def _get_seed_records(self, result_file): |
| 68 | + loglines, errors = mlp_parser.parse_file( |
| 69 | + result_file, |
| 70 | + ruleset=self._ruleset, |
| 71 | + ) |
| 72 | + if len(errors) > 0: |
| 73 | + raise ValueError('\n'.join( |
| 74 | + ['Found parsing errors:'] + |
| 75 | + ['{}\n ^^ {}'.format(line, error) |
| 76 | + for line, error in errors] + |
| 77 | + ['', 'Log lines had parsing errors.'])) |
| 78 | + return [( |
| 79 | + line.value['metadata']['file'], |
| 80 | + line.value['metadata']['lineno'], |
| 81 | + int(line.value['value']), |
| 82 | + ) for line in loglines if line.key == 'seed'] |
| 83 | + |
| 84 | + def _assert_unique_seed_per_run(self, result_files): |
| 85 | + no_logged_seed = True |
| 86 | + error_messages = [] |
| 87 | + seed_to_result_file = {} |
| 88 | + for result_file in result_files: |
| 89 | + try: |
| 90 | + seed_records = self._get_seed_records(result_file) |
| 91 | + except Exception as e: |
| 92 | + error_messages.append("Error found when querying seeds from " |
| 93 | + "{}: {}".format(result_file, e)) |
| 94 | + continue |
| 95 | + |
| 96 | + if not no_logged_seed and len(seed_records) == 0: |
| 97 | + error_messages.append( |
| 98 | + "Result file {} logs no seed. However, other " |
| 99 | + "result files, including {}, already logs some " |
| 100 | + "seeds.".format(result_file, |
| 101 | + list(seed_to_result_file.keys()))) |
| 102 | + if no_logged_seed and len(seed_records) > 0: |
| 103 | + no_logged_seed = False |
| 104 | + if len(seed_records) > 1: |
| 105 | + warnings.warn( |
| 106 | + "Result file {} logs more than one seeds {}!".format( |
| 107 | + result_file, seed_records)) |
| 108 | + for f, ln, s in seed_records: |
| 109 | + if (f, ln, s) in seed_to_result_file: |
| 110 | + error_messages.append( |
| 111 | + "Result file {} logs seed {} on {}:{}. However, " |
| 112 | + "result file {} already logs the same seed on the same " |
| 113 | + "line.".format( |
| 114 | + result_file, |
| 115 | + s, |
| 116 | + f, |
| 117 | + ln, |
| 118 | + seed_to_result_file[(f, ln, s)], |
| 119 | + )) |
| 120 | + else: |
| 121 | + seed_to_result_file[(f, ln, s)] = result_file |
| 122 | + |
| 123 | + return no_logged_seed, error_messages |
| 124 | + |
| 125 | + def _has_seed_keyword(self, source_file): |
| 126 | + with open(source_file, 'r') as file_handle: |
| 127 | + for line in file_handle.readlines(): |
| 128 | + if 'seed' in line.lower(): |
| 129 | + return True |
| 130 | + return False |
| 131 | + |
| 132 | + def check_seeds(self, result_files, source_files): |
| 133 | + """ Check the seeds for a specific benchmark submission. |
| 134 | +
|
| 135 | + Args: |
| 136 | + result_files: An iterable contains paths to all the result files for |
| 137 | + this benchmark. |
| 138 | + source_files: An iterable contains paths to all the source files for |
| 139 | + this benchmark. |
| 140 | +
|
| 141 | + """ |
| 142 | + no_logged_seed, error_messages = self._assert_unique_seed_per_run( |
| 143 | + result_files) |
| 144 | + |
| 145 | + if len(error_messages) > 0: |
| 146 | + print("Seed checker failed and found the following " |
| 147 | + "errors:\n{}".format('\n'.join(error_messages))) |
| 148 | + return False |
| 149 | + |
| 150 | + if no_logged_seed: |
| 151 | + for source_file in source_files: |
| 152 | + if self._has_seed_keyword(source_file): |
| 153 | + warnings.warn( |
| 154 | + "Source file {} contains the keyword 'seed' but no " |
| 155 | + "seed value is logged!".format(source_file)) |
| 156 | + return True |
0 commit comments