-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
84 additions
and
21 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
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,50 @@ | ||
#ifndef XLA_CLIENT_UNIQUE_H_ | ||
#define XLA_CLIENT_UNIQUE_H_ | ||
|
||
#include <functional> | ||
#include <set> | ||
|
||
#include "absl/types/optional.h" | ||
#include "torch_xla/csrc/runtime/debug_macros.h" | ||
|
||
namespace torch_xla { | ||
namespace runtime { | ||
namespace util { | ||
|
||
// Helper class to allow tracking zero or more things, which should be forcibly | ||
// be one only thing. | ||
template <typename T, typename C = std::equal_to<T>> | ||
class Unique { | ||
public: | ||
std::pair<bool, const T&> set(const T& value) { | ||
if (value_) { | ||
XLA_CHECK(C()(*value_, value)) | ||
<< "'" << *value_ << "' vs '" << value << "'"; | ||
return std::pair<bool, const T&>(false, *value_); | ||
} | ||
value_ = value; | ||
return std::pair<bool, const T&>(true, *value_); | ||
} | ||
|
||
operator bool() const { return value_.has_value(); } | ||
operator const T&() const { return *value_; } | ||
const T& operator*() const { return *value_; } | ||
const T* operator->() const { return value_.operator->(); } | ||
|
||
std::set<T> AsSet() const { | ||
std::set<T> vset; | ||
if (value_.has_value()) { | ||
vset.insert(*value_); | ||
} | ||
return vset; | ||
} | ||
|
||
private: | ||
absl::optional<T> value_; | ||
}; | ||
|
||
} // namespace util | ||
} // namespace runtime | ||
} // namespace torch_xla | ||
|
||
#endif // XLA_CLIENT_UNIQUE_H_ |
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