Skip to content

Commit

Permalink
Move the helper functions to the class
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunsuresh committed Oct 21, 2024
1 parent ba4f25a commit 824ece0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 98 deletions.
2 changes: 1 addition & 1 deletion loadgen/test_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ struct TestSettings {

/// \brief Load mlperf parameter config from file.
int FromConfig(const std::string &path, const std::string &model,
const std::string &scenario, bool is_mlperf_conf = false);
const std::string &scenario);
/**@}*/

// ==================================
Expand Down
185 changes: 88 additions & 97 deletions loadgen/test_settings_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -518,106 +518,106 @@ void TestSettingsInternal::LogSummary(AsyncSummary &summary) const {

} // namespace loadgen

int TestSettings::FromMLPerfConfig(const std::string &model,
const std::string &scenario) {
std::map<std::string, std::string> kv;
const std::string path = MLPERF_CONF_PATH;

// Helper function to parse the config file and populate kv map
auto parse_config_file = [&]() -> int {
std::ifstream fss(path);
if (!fss.is_open()) {
LogDetail([p = path](AsyncDetail &detail) {
// Helper function for key-value lookup and value assignment
auto TestSettings::lookup_and_assign =
[&](const std::string &key, uint64_t *val_l, double *val_d = nullptr,
double multiplier = 1.0) {
std::string found;
const std::vector<std::string> key_patterns = {
model + "." + scenario + "." + key, "*." + scenario + "." + key,
model + ".*." + key, "*.*." + key};
for (const auto &pattern : key_patterns) {
auto it = kv.find(pattern);
if (it != kv.end()) {
found = it->second;
break;
}
}
if (found.empty()) {
return false;
}
if (val_l) {
*val_l = strtoull(found.c_str(), nullptr, 0) *
static_cast<uint64_t>(multiplier);
}
if (val_d) {
*val_d = strtod(found.c_str(), nullptr) * multiplier;
}
return true;
};

// Helper function to parse the config file and populate kv map
auto TestSettings::parse_config_file = [&]() -> int {
std::ifstream fss(path);
if (!fss.is_open()) {
LogDetail([p = path](AsyncDetail &detail) {
#if USE_NEW_LOGGING_FORMAT
std::stringstream ss;
ss << "can't open file " << p;
MLPERF_LOG_ERROR(detail, "error_invalid_config", ss.str());
std::stringstream ss;
ss << "can't open file " << p;
MLPERF_LOG_ERROR(detail, "error_invalid_config", ss.str());
#else
detail.Error("can't open file ", p);
detail.Error("can't open file ", p);
#endif
});
return -ENOENT;
}
});
return -ENOENT;
}

std::string line;
int line_nr = 0;
int errors = 0;
while (std::getline(fss, line)) {
line_nr++;
std::istringstream iss(line);
std::string s, k;
int looking_for = 0; // 0=key, 1=equal, 2=value
while (iss >> s) {
if (s == "#" && looking_for != 2) {
break; // comment
std::string line;
int line_nr = 0;
int errors = 0;
while (std::getline(fss, line)) {
line_nr++;
std::istringstream iss(line);
std::string s, k;
int looking_for = 0; // 0=key, 1=equal, 2=value
while (iss >> s) {
if (s == "#" && looking_for != 2) {
break; // comment
}
if (looking_for == 2) {
const char *start = s.c_str();
char *stop;
(void)strtoul(start, &stop, 0);
if (start + s.size() == stop || (void)strtod(start, &stop)) {
kv[k] = s;
continue;
}
if (looking_for == 2) {
const char *start = s.c_str();
char *stop;
(void)strtoul(start, &stop, 0);
if (start + s.size() == stop || (void)strtod(start, &stop)) {
kv[k] = s;
continue;
}
errors++;
LogDetail([l = line_nr](AsyncDetail &detail) {
errors++;
LogDetail([l = line_nr](AsyncDetail &detail) {
#if USE_NEW_LOGGING_FORMAT
std::stringstream ss;
ss << "value needs to be integer or double, line=" << l;
MLPERF_LOG_ERROR(detail, "error_invalid_config", ss.str());
std::stringstream ss;
ss << "value needs to be integer or double, line=" << l;
MLPERF_LOG_ERROR(detail, "error_invalid_config", ss.str());
#else
detail.Error("value needs to be integer or double, line=", l);
detail.Error("value needs to be integer or double, line=", l);
#endif
});
break;
}
if (looking_for == 1 && s != "=") {
errors++;
LogDetail([l = line_nr](AsyncDetail &detail) {
});
break;
}
if (looking_for == 1 && s != "=") {
errors++;
LogDetail([l = line_nr](AsyncDetail &detail) {
#if USE_NEW_LOGGING_FORMAT
std::stringstream ss;
ss << "expected 'key=value', line=" << l;
MLPERF_LOG_ERROR(detail, "error_invalid_config", ss.str());
std::stringstream ss;
ss << "expected 'key=value', line=" << l;
MLPERF_LOG_ERROR(detail, "error_invalid_config", ss.str());
#else
detail.Error("expected 'key=value', line=", l);
detail.Error("expected 'key=value', line=", l);
#endif
});
break;
}
if (looking_for == 0) k = s;
looking_for++;
}
}
return errors == 0 ? 0 : -EINVAL;
};

// Helper function for key-value lookup and value assignment
auto lookup_and_assign = [&](const std::string &key, uint64_t *val_l,
double *val_d = nullptr,
double multiplier = 1.0) {
std::string found;
const std::vector<std::string> key_patterns = {
model + "." + scenario + "." + key, "*." + scenario + "." + key,
model + ".*." + key, "*.*." + key};
for (const auto &pattern : key_patterns) {
auto it = kv.find(pattern);
if (it != kv.end()) {
found = it->second;
});
break;
}
if (looking_for == 0) k = s;
looking_for++;
}
if (found.empty()) {
return false;
}
if (val_l) {
*val_l = strtoull(found.c_str(), nullptr, 0) *
static_cast<uint64_t>(multiplier);
}
if (val_d) {
*val_d = strtod(found.c_str(), nullptr) * multiplier;
}
return true;
};
}
return errors == 0 ? 0 : -EINVAL;
};

int TestSettings::FromMLPerfConfig(const std::string &model,
const std::string &scenario) {
std::map<std::string, std::string> kv;
const std::string path = MLPERF_CONF_PATH;

// Parse the config file first
if (parse_config_file() != 0) {
Expand Down Expand Up @@ -718,7 +718,7 @@ int TestSettings::FromMLPerfConfig(const std::string &model,
}

int TestSettings::FromConfig(const std::string &path, const std::string &model,
const std::string &scenario, bool is_mlperf_conf) {
const std::string &scenario) {
std::map<std::string, std::string> kv;
static int configCount = 0;
// Parse the config file
Expand All @@ -729,19 +729,10 @@ int TestSettings::FromConfig(const std::string &path, const std::string &model,
if (configCount == 0 && !is_mlperf_conf) {
// Only allow userConf as the single configFile and loadgen loads the
// mlperfConf automatically
FromConfig(MLPERF_CONF_PATH, model, scenario, true);
FromMLPerfConfig(MLPERF_CONF_PATH, model, scenario);
configCount++;
}

if (is_mlperf_conf) {
if (path != MLPERF_CONF_PATH) {
std::stringstream ss;
ss << "Custom mlperf.conf file is used. This is not valid for official "
"submission.";
MLPERF_LOG_ERROR(detail, "error_invalid_config", ss.str());
}
}

else if (configCount > 1) {
std::stringstream ss;
ss << "Multiple conf files are used. This is not valid for official "
Expand Down

0 comments on commit 824ece0

Please sign in to comment.