Skip to content

Commit

Permalink
Add IsNonNegativeFloat util function
Browse files Browse the repository at this point in the history
  • Loading branch information
nv-hwoo committed Aug 14, 2023
1 parent 2e80c06 commit 9a312de
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/c++/perf_analyzer/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -940,9 +940,8 @@ CLParser::ParseCommandLine(int argc, char** argv)
}
case 9:
case 's': {
// TODO
std::string stability_threshold{optarg};
if (std::stof(stability_threshold) >= 0.0) {
if (IsNonNegativeFloat(stability_threshold)) {
params_->stability_threshold = std::stof(optarg) / 100;
} else {
Usage(
Expand Down Expand Up @@ -1361,7 +1360,6 @@ CLParser::ParseCommandLine(int argc, char** argv)
break;
}
case 47: {
// TODO
std::string trace_count{optarg};
if (std::stoi(trace_count) >= -1) {
params_->trace_options["trace_count"] = {trace_count};
Expand Down
15 changes: 13 additions & 2 deletions src/c++/perf_analyzer/perf_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,6 @@ IsPositiveInteger(const std::string& str)
return false;
} else if (str.size() == 1 && str[0] == '0') {
return false;
} else if (str.size() > 1 && str[0] == '0') {
return false;
}
return std::all_of(
str.begin(), str.end(),
Expand All @@ -418,4 +416,17 @@ IsNonNegativeInteger(const std::string& str)
return IsPositiveInteger(str);
}

bool
IsNonNegativeFloat(const std::string& str)
{
size_t pos{0};
size_t dot_pos = str.find(".", pos);
if (dot_pos == std::string::npos) {
return IsNonNegativeInteger(str);
} else {
return IsNonNegativeInteger(str.substr(0, dot_pos)) &&
IsNonNegativeInteger(str.substr(dot_pos + 1));
}
}

}} // namespace triton::perfanalyzer
3 changes: 3 additions & 0 deletions src/c++/perf_analyzer/perf_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,7 @@ bool IsPositiveInteger(const std::string& str);
// Check if the given string is a valid non-negative integer.
bool IsNonNegativeInteger(const std::string& str);

// Check if the given string is a valid non-negative floating point number.
bool IsNonNegativeFloat(const std::string& str);

}} // namespace triton::perfanalyzer
33 changes: 27 additions & 6 deletions src/c++/perf_analyzer/test_perf_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,17 @@ TEST_CASE("perf_utils: Test string input numbers")
std::string pos{"123"};
std::string neg{"-123"};
std::string zero{"0"};
std::string not_number{"helloworld"};
std::string almost_number{"123hello"};
std::string not_integer{"helloworld"};
std::string almost_integer{"123hello"};
std::string empty{""};

SUBCASE("IsPositiveInteger")
{
CHECK(IsPositiveInteger(pos));
CHECK(!IsPositiveInteger(neg));
CHECK(!IsPositiveInteger(zero));
CHECK(!IsPositiveInteger(not_number));
CHECK(!IsPositiveInteger(almost_number));
CHECK(!IsPositiveInteger(not_integer));
CHECK(!IsPositiveInteger(almost_integer));
CHECK(!IsPositiveInteger(empty));
}

Expand All @@ -394,10 +394,31 @@ TEST_CASE("perf_utils: Test string input numbers")
CHECK(IsNonNegativeInteger(pos));
CHECK(!IsNonNegativeInteger(neg));
CHECK(IsNonNegativeInteger(zero));
CHECK(!IsNonNegativeInteger(not_number));
CHECK(!IsNonNegativeInteger(almost_number));
CHECK(!IsNonNegativeInteger(not_integer));
CHECK(!IsNonNegativeInteger(almost_integer));
CHECK(!IsNonNegativeInteger(empty));
}

SUBCASE("IsNonNegativeFloat")
{
std::string pos_float{"123.456"};
std::string neg_float{"-123.456"};
std::string zero_float{"0.0"};
std::string fraction{"0.01"};
std::string almost_float{"123.01hello"};

CHECK(IsNonNegativeFloat(pos_float));
CHECK(!IsNonNegativeFloat(neg_float));
CHECK(IsNonNegativeFloat(zero_float));
CHECK(IsNonNegativeFloat(fraction));
CHECK(IsNonNegativeFloat(pos));
CHECK(!IsNonNegativeFloat(neg));
CHECK(IsNonNegativeFloat(zero));
CHECK(!IsNonNegativeFloat(not_integer));
CHECK(!IsNonNegativeFloat(almost_integer));
CHECK(!IsNonNegativeFloat(almost_float));
CHECK(!IsNonNegativeFloat(empty));
}
}


Expand Down

0 comments on commit 9a312de

Please sign in to comment.