-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add semver_t type into common to handle version numbers, based on semver 2.0.0 but more permissive since TT-KMD reports non-compliant version strings like "1.29" and "1.28-bh2". Add logic to PCIDevice class to report KMD version during device initialization.
- Loading branch information
1 parent
4b5dffd
commit d70cd71
Showing
9 changed files
with
224 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* SPDX-FileCopyrightText: (c) 2024 Tenstorrent Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "semver.hpp" | ||
|
||
#include <fmt/format.h> | ||
|
||
#include <sstream> | ||
#include <tuple> | ||
|
||
namespace tt::umd { | ||
|
||
static semver_t parse(const std::string& version_str) { | ||
std::istringstream iss(version_str); | ||
std::string token; | ||
uint64_t major = 0; | ||
uint64_t minor = 0; | ||
uint64_t patch = 0; | ||
|
||
if (std::getline(iss, token, '.')) { | ||
major = std::stoull(token); | ||
|
||
if (std::getline(iss, token, '.')) { | ||
minor = std::stoull(token); | ||
|
||
if (std::getline(iss, token, '.')) { | ||
patch = std::stoull(token); | ||
} | ||
} | ||
} | ||
return semver_t(major, minor, patch); | ||
} | ||
|
||
semver_t::semver_t(uint64_t major, uint64_t minor, uint64_t patch) : major(major), minor(minor), patch(patch) {} | ||
|
||
semver_t::semver_t(const std::string& version_str) : semver_t(parse(version_str)) {} | ||
|
||
bool semver_t::operator<(const semver_t& other) const { | ||
return std::tie(major, minor, patch) < std::tie(other.major, other.minor, other.patch); | ||
} | ||
|
||
bool semver_t::operator>(const semver_t& other) const { return other < *this; } | ||
|
||
bool semver_t::operator==(const semver_t& other) const { | ||
return std::tie(major, minor, patch) == std::tie(other.major, other.minor, other.patch); | ||
} | ||
|
||
bool semver_t::operator!=(const semver_t& other) const { return !(*this == other); } | ||
|
||
bool semver_t::operator<=(const semver_t& other) const { return !(other < *this); } | ||
|
||
bool semver_t::operator>=(const semver_t& other) const { return !(*this < other); } | ||
|
||
std::string semver_t::to_string() const { return fmt::format("{}.{}.{}", major, minor, patch); } | ||
|
||
} // namespace tt::umd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* SPDX-FileCopyrightText: (c) 2024 Tenstorrent Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <string> | ||
|
||
namespace tt::umd { | ||
|
||
/** | ||
* Based on Semantic Versioning 2.0.0 (https://semver.org/) but more permissive. | ||
* TT-KMD reports version strings that are technically not semver compliant. | ||
*/ | ||
struct semver_t { | ||
const uint64_t major; | ||
const uint64_t minor; | ||
const uint64_t patch; | ||
|
||
semver_t(uint64_t major, uint64_t minor, uint64_t patch); | ||
semver_t(const std::string& version_str); | ||
|
||
bool operator<(const semver_t& other) const; | ||
bool operator>(const semver_t& other) const; | ||
bool operator==(const semver_t& other) const; | ||
bool operator!=(const semver_t& other) const; | ||
bool operator<=(const semver_t& other) const; | ||
bool operator>=(const semver_t& other) const; | ||
std::string to_string() const; | ||
}; | ||
|
||
} // namespace tt::umd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
set(UMD_MISC_TESTS_SRCS test_semver.cpp) | ||
|
||
add_executable(umd_misc_tests ${UMD_MISC_TESTS_SRCS}) | ||
target_link_libraries(umd_misc_tests PRIVATE test_common) | ||
set_target_properties( | ||
umd_misc_tests | ||
PROPERTIES | ||
RUNTIME_OUTPUT_DIRECTORY | ||
${CMAKE_BINARY_DIR}/test/umd/misc | ||
OUTPUT_NAME | ||
umd_misc_tests | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* SPDX-FileCopyrightText: (c) 2024 Tenstorrent Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
#include <map> | ||
|
||
#include "common/semver.hpp" | ||
|
||
using tt::umd::semver_t; | ||
|
||
TEST(Semver, Valid) { | ||
const std::map<std::string, semver_t> valid_test_cases = { | ||
{"1.29", semver_t(1, 29, 0)}, // technically invalid, but seen from TT-KMD | ||
{"1.28-bh2", semver_t(1, 28, 0)}, // technically invalid, but seen from TT-KMD | ||
{"0.0.4", semver_t(0, 0, 4)}, | ||
{"1.2.3", semver_t(1, 2, 3)}, | ||
{"10.20.30", semver_t(10, 20, 30)}, | ||
{"1.1.2-prerelease+meta", semver_t(1, 1, 2)}, | ||
{"1.1.2+meta", semver_t(1, 1, 2)}, | ||
{"1.1.2+meta-valid", semver_t(1, 1, 2)}, | ||
{"1.0.0-alpha", semver_t(1, 0, 0)}, | ||
{"1.0.0-beta", semver_t(1, 0, 0)}, | ||
{"1.0.0-alpha.beta", semver_t(1, 0, 0)}, | ||
{"1.0.0-alpha.beta.1", semver_t(1, 0, 0)}, | ||
{"1.0.0-alpha.1", semver_t(1, 0, 0)}, | ||
{"1.0.0-alpha0.valid", semver_t(1, 0, 0)}, | ||
{"1.0.0-alpha.0valid", semver_t(1, 0, 0)}, | ||
{"1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay", semver_t(1, 0, 0)}, | ||
{"1.0.0-rc.1+build.1", semver_t(1, 0, 0)}, | ||
{"2.0.0-rc.1+build.123", semver_t(2, 0, 0)}, | ||
{"1.2.3-beta", semver_t(1, 2, 3)}, | ||
{"10.2.3-DEV-SNAPSHOT", semver_t(10, 2, 3)}, | ||
{"1.2.3-SNAPSHOT-123", semver_t(1, 2, 3)}, | ||
{"1.0.0", semver_t(1, 0, 0)}, | ||
{"2.0.0", semver_t(2, 0, 0)}, | ||
{"1.1.7", semver_t(1, 1, 7)}, | ||
{"2.0.0+build.1848", semver_t(2, 0, 0)}, | ||
{"2.0.1-alpha.1227", semver_t(2, 0, 1)}, | ||
{"1.0.0-alpha+beta", semver_t(1, 0, 0)}, | ||
{"1.2.3----RC-SNAPSHOT.12.9.1--.12+788", semver_t(1, 2, 3)}, | ||
{"1.2.3----R-S.12.9.1--.12+meta", semver_t(1, 2, 3)}, | ||
{"1.2.3----RC-SNAPSHOT.12.9.1--.12", semver_t(1, 2, 3)}, | ||
{"1.0.0+0.build.1-rc.10000aaa-kk-0.1", semver_t(1, 0, 0)}, | ||
{"1.0.0-0A.is.legal", semver_t(1, 0, 0)} | ||
}; | ||
|
||
for (const auto &[version_str, expected] : valid_test_cases) { | ||
semver_t actual(version_str); | ||
EXPECT_EQ(actual.major, expected.major); | ||
EXPECT_EQ(actual.minor, expected.minor); | ||
EXPECT_EQ(actual.patch, expected.patch); | ||
} | ||
} | ||
|
||
TEST(Semver, Invalid) { | ||
std::vector<std::string> invalid_test_cases = { | ||
"+invalid", | ||
"-invalid", | ||
"-invalid+invalid", | ||
"-invalid.01", | ||
"alpha", | ||
"alpha.beta", | ||
"alpha.beta.1", | ||
"alpha.1", | ||
"alpha+beta", | ||
"alpha_beta", | ||
"alpha.", | ||
"alpha..", | ||
"beta", | ||
"-alpha.", | ||
"+justmeta", | ||
}; | ||
|
||
for (const auto &version_str : invalid_test_cases) { | ||
EXPECT_THROW(semver_t{version_str}, std::exception) << "'" << version_str << "' should be invalid"; | ||
} | ||
} | ||
|