Skip to content

Commit

Permalink
Revert "Reland: implement a function to convert a storage to copy-on-…
Browse files Browse the repository at this point in the history
…write (pytorch#110022)"

This reverts commit dddf07e.

Reverted pytorch#110022 on behalf of https://github.com/atalman due to New tests are failing in internal CI ([comment](pytorch#110022 (comment)))
  • Loading branch information
pytorchmergebot committed Sep 27, 2023
1 parent 7dbdf3b commit 1265400
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 283 deletions.
22 changes: 20 additions & 2 deletions c10/core/build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,30 @@ def define_targets(rules):
[
"*.cpp",
"impl/*.cpp",
"impl/cow/*.cpp",
],
exclude = [
"CPUAllocator.cpp",
"impl/alloc_cpu.cpp",
"impl/cow/*.cpp",
],
),
hdrs = rules.glob(
[
"*.h",
"impl/*.h",
"impl/cow/*.h",
],
exclude = [
"CPUAllocator.h",
"impl/alloc_cpu.h",
"impl/cow/*.h",
],
),
linkstatic = True,
local_defines = ["C10_BUILD_MAIN_LIB"],
visibility = ["//visibility:public"],
deps = [
":ScalarType",
":impl_cow_context",
"//third_party/cpuinfo",
"//c10/macros",
"//c10/util:TypeCast",
Expand All @@ -92,6 +93,23 @@ def define_targets(rules):
alwayslink = True,
)

rules.cc_library(
name = "impl_cow_context",
srcs = [
"impl/cow/context.cpp",
"impl/cow/deleter.cpp",
],
hdrs = [
"impl/cow/context.h",
"impl/cow/deleter.h",
],
deps = [
"//c10/macros",
"//c10/util:base",
],
visibility = ["//c10/test:__pkg__"],
)

rules.filegroup(
name = "headers",
srcs = rules.glob(
Expand Down
96 changes: 0 additions & 96 deletions c10/core/impl/cow/try_ensure.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions c10/core/impl/cow/try_ensure.h

This file was deleted.

10 changes: 9 additions & 1 deletion c10/test/build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ def define_targets(rules):
visibility = ["//:__pkg__"],
)

rules.cc_test(
name = "core_impl_cow_context_test",
srcs = ["core/impl/cow/context_test.cpp"],
deps = [
"//c10/core:impl_cow_context",
"@com_google_googletest//:gtest_main",
],
)

rules.cc_test(
name = "core_tests",
size = "small",
Expand All @@ -20,7 +29,6 @@ def define_targets(rules):
deps = [
"//c10/core:base",
"//c10/util:base",
"//c10/core:CPUAllocator",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
76 changes: 76 additions & 0 deletions c10/test/core/impl/cow/context_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <c10/core/impl/cow/context.h>

#include <c10/core/impl/cow/deleter.h>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace c10::impl {
namespace {

class DeleteTracker {
public:
explicit DeleteTracker(int& delete_count) : delete_count_(delete_count) {}
~DeleteTracker() {
++delete_count_;
}

private:
int& delete_count_;
};

class ContextTest : public testing::Test {
protected:
auto delete_count() const -> int {
return delete_count_;
}
auto new_delete_tracker() -> std::unique_ptr<void, DeleterFnPtr> {
return {new DeleteTracker(delete_count_), +[](void* ptr) {
delete static_cast<DeleteTracker*>(ptr);
}};
}

private:
int delete_count_ = 0;
};

TEST_F(ContextTest, Basic) {
auto& context = *new cow::Context(new_delete_tracker());
ASSERT_THAT(delete_count(), testing::Eq(0));

context.increment_refcount();

{
// This is in a sub-scope because this call to decrement_refcount
// is expected to give us a shared lock.
auto result = context.decrement_refcount();
ASSERT_THAT(
std::holds_alternative<cow::Context::NotLastReference>(result),
testing::IsTrue());
ASSERT_THAT(delete_count(), testing::Eq(0));
}

{
auto result = context.decrement_refcount();
ASSERT_THAT(
std::holds_alternative<cow::Context::LastReference>(result),
testing::IsTrue());
// Result holds the DeleteTracker.
ASSERT_THAT(delete_count(), testing::Eq(0));
}

// When result is deleted, the DeleteTracker is also deleted.
ASSERT_THAT(delete_count(), testing::Eq(1));
}

TEST_F(ContextTest, delete_context) {
// This is effectively the same thing as decrement_refcount() above.
auto& context = *new cow::Context(new_delete_tracker());
ASSERT_THAT(delete_count(), testing::Eq(0));

cow::delete_context(&context);
ASSERT_THAT(delete_count(), testing::Eq(1));
}

} // namespace
} // namespace c10::impl
Loading

0 comments on commit 1265400

Please sign in to comment.