From 461f643d59658edcd4cf6a466434b180cb6b6979 Mon Sep 17 00:00:00 2001 From: Will Cromar Date: Thu, 9 Nov 2023 20:15:33 +0000 Subject: [PATCH] more random cleanup --- torch_xla/csrc/runtime/util.h | 102 ---------------------------- torch_xla/csrc/runtime/util_test.cc | 38 ----------- torch_xla/csrc/tensor_util.cpp | 2 +- 3 files changed, 1 insertion(+), 141 deletions(-) diff --git a/torch_xla/csrc/runtime/util.h b/torch_xla/csrc/runtime/util.h index 047c1d8ea0b..722a6591f78 100644 --- a/torch_xla/csrc/runtime/util.h +++ b/torch_xla/csrc/runtime/util.h @@ -24,57 +24,6 @@ namespace torch_xla { namespace runtime { namespace util { -template -xla::Status CheckedCall(const F& fn) { - try { - fn(); - } catch (const std::exception& ex) { - return tsl::errors::Internal(ex.what()); - } - return xla::Status(); -} - -template -class Cleanup { - public: - using StatusType = T; - - explicit Cleanup(std::function func) - : func_(std::move(func)) {} - Cleanup(Cleanup&& ref) - : func_(std::move(ref.func_)), status_(std::move(ref.status_)) {} - Cleanup(const Cleanup&) = delete; - - ~Cleanup() { - if (func_ != nullptr) { - func_(std::move(status_)); - } - } - - Cleanup& operator=(const Cleanup&) = delete; - - Cleanup& operator=(Cleanup&& ref) { - if (this != &ref) { - func_ = std::move(ref.func_); - status_ = std::move(ref.status_); - } - return *this; - } - - void Release() { func_ = nullptr; } - - void SetStatus(StatusType status) { status_ = std::move(status); } - - const StatusType& GetStatus() const { return status_; } - - private: - std::function func_; - StatusType status_; -}; - -using ExceptionCleanup = Cleanup; -using StatusCleanup = Cleanup; - // Allows APIs which might return const references and values, to not be forced // to return values in the signature. template @@ -96,10 +45,6 @@ class MaybeRef { const T& ref_; }; -struct MidPolicy { - size_t operator()(size_t size) const { return size / 2; } -}; - template class MaybePtr { public: @@ -121,48 +66,6 @@ class MaybePtr { absl::optional storage_; }; -template -std::vector GetConstSharedPointers( - const C& shared_pointers) { - std::vector pointers; - pointers.reserve(shared_pointers.size()); - for (auto& shared_pointer : shared_pointers) { - pointers.push_back(shared_pointer.get()); - } - return pointers; -} - -template -std::vector GetSharedPointers( - const C& shared_pointers) { - std::vector pointers; - pointers.reserve(shared_pointers.size()); - for (auto& shared_pointer : shared_pointers) { - pointers.push_back(shared_pointer.get()); - } - return pointers; -} - -template -void InsertCombined(C* map, const K& key, const T& value, const F& combiner) { - auto it = map->find(key); - if (it == map->end()) { - map->emplace(key, value); - } else { - it->second = combiner(it->second, value); - } -} - -template -std::vector Iota(size_t size, T init = 0, T incr = 1) { - std::vector result(size); - T value = init; - for (size_t i = 0; i < size; ++i, value += incr) { - result[i] = value; - } - return result; -} - template std::vector Range(T start, T end, T step = 1) { std::vector result; @@ -220,11 +123,6 @@ const typename T::mapped_type& MapInsert(T* cont, return it->second; } -template -typename std::underlying_type::type GetEnumValue(T value) { - return static_cast::type>(value); -} - template T Multiply(const S& input) { return std::accumulate(input.begin(), input.end(), T(1), diff --git a/torch_xla/csrc/runtime/util_test.cc b/torch_xla/csrc/runtime/util_test.cc index c7a3c5ac285..f65eea30ca7 100644 --- a/torch_xla/csrc/runtime/util_test.cc +++ b/torch_xla/csrc/runtime/util_test.cc @@ -15,36 +15,6 @@ namespace util { using ::testing::ElementsAre; -TEST(UtilTest, Cleanup) { - bool notify = false; - - // Set to true. - { - Cleanup c([¬ify](bool b) { notify = b; }); - c.SetStatus(true); - } - EXPECT_TRUE(notify); - - // Set to false. - { - Cleanup c([¬ify](bool b) { notify = b; }); - c.SetStatus(false); - } - EXPECT_FALSE(notify); - - // Releasing the cleanup will not change the `notify` to true. - { - Cleanup c([¬ify](bool b) { notify = b; }); - c.SetStatus(true); - c.Release(); - } - EXPECT_FALSE(notify); -} - -TEST(UtilTest, Iota) { - EXPECT_THAT(Iota(5, 0, 2), ElementsAre(0, 2, 4, 6, 8)); -} - TEST(UtilTest, Range) { EXPECT_THAT(Range(0, 10, 2), ElementsAre(0, 2, 4, 6, 8)); EXPECT_THAT(Range(10, 0, -2), ElementsAre(10, 8, 6, 4, 2)); @@ -75,14 +45,6 @@ TEST(UtilTest, MapInsert) { EXPECT_EQ(MapInsert(&v, 1, [] { return 12; }), 1); } -TEST(UtilTest, GetEnumValue) { - enum E { A = 0, B, C, D }; - EXPECT_EQ(GetEnumValue(E::A), 0); - EXPECT_EQ(GetEnumValue(E::B), 1); - EXPECT_EQ(GetEnumValue(E::C), 2); - EXPECT_EQ(GetEnumValue(E::D), 3); -} - TEST(UtilTest, Multiply) { std::vector t = {1, 2, 3, 4, 5}; EXPECT_EQ(Multiply(t), 120); diff --git a/torch_xla/csrc/tensor_util.cpp b/torch_xla/csrc/tensor_util.cpp index a419bd98b7e..29697963c8a 100644 --- a/torch_xla/csrc/tensor_util.cpp +++ b/torch_xla/csrc/tensor_util.cpp @@ -36,7 +36,7 @@ namespace { struct DataAsync { std::vector source_tensors; std::vector async_datas; - std::vector handle_unlockers; + std::vector handle_unlockers; }; bool ShouldUseBF16() {