Skip to content

Commit

Permalink
Revert "Add lazy_clone_storage to create COW storages (pytorch#110192
Browse files Browse the repository at this point in the history
…)"

This reverts commit 1c30814.

Reverted pytorch#110192 on behalf of https://github.com/jeanschmidt due to Breaking internal builds, @ezyang please support the author providing further details ([comment](pytorch#110192 (comment)))
  • Loading branch information
pytorchmergebot committed Oct 16, 2023
1 parent c271df9 commit 97a513e
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 248 deletions.
20 changes: 2 additions & 18 deletions c10/core/build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ 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,
Expand All @@ -92,22 +92,6 @@ def define_targets(rules):
alwayslink = True,
)

rules.cc_library(
name = "impl_cow",
srcs = rules.glob([
"impl/cow/*.cpp",
]),
hdrs = rules.glob([
"impl/cow/*.h",
]),
deps = [
":base",
":CPUAllocator",
],
visibility = ["//c10/test:__pkg__"],

)

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

This file was deleted.

29 changes: 0 additions & 29 deletions c10/core/impl/cow/COW.h

This file was deleted.

1 change: 0 additions & 1 deletion c10/test/build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def define_targets(rules):
"//c10/core:base",
"//c10/util:base",
"//c10/core:CPUAllocator",
"//c10/core:impl_cow",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
89 changes: 0 additions & 89 deletions c10/test/core/impl/cow_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <c10/core/impl/cow/COW.h>
#include <c10/core/impl/cow/COWDeleter.h>

#include <c10/core/CPUAllocator.h>
Expand Down Expand Up @@ -79,94 +78,6 @@ TEST_F(ContextTest, cow_deleter) {
ASSERT_THAT(delete_count(), testing::Eq(1));
}

MATCHER(is_copy_on_write, "") {
const c10::StorageImpl& storage = std::ref(arg);
return cow::is_cow_data_ptr(storage.data_ptr());
}

TEST(lazy_clone_storage_test, no_context) {
StorageImpl original_storage(
{}, /*size_bytes=*/7, GetDefaultCPUAllocator(), /*resizable=*/false);
ASSERT_THAT(original_storage, testing::Not(is_copy_on_write()));
ASSERT_TRUE(cow::has_simple_data_ptr(original_storage));

intrusive_ptr<StorageImpl> new_storage =
cow::lazy_clone_storage(original_storage);
ASSERT_THAT(new_storage.get(), testing::NotNull());

// The original storage was modified in-place to now hold a copy on
// write context.
ASSERT_THAT(original_storage, is_copy_on_write());

// The result is a different storage impl.
ASSERT_THAT(&*new_storage, testing::Ne(&original_storage));
// But it is also copy-on-write.
ASSERT_THAT(*new_storage, is_copy_on_write());
// But they share the same data!
ASSERT_THAT(new_storage->data(), testing::Eq(original_storage.data()));
}

struct MyDeleterContext {
MyDeleterContext(void* bytes) : bytes(bytes) {}

~MyDeleterContext() {
delete[] static_cast<std::byte*>(bytes);
}

void* bytes;
};

void my_deleter(void* ctx) {
delete static_cast<MyDeleterContext*>(ctx);
}

TEST(lazy_clone_storage_test, different_context) {
void* bytes = new std::byte[5];
StorageImpl storage(
{},
/*size_bytes=*/5,
at::DataPtr(
/*data=*/bytes,
/*ctx=*/new MyDeleterContext(bytes),
/*ctx_deleter=*/my_deleter,
/*device=*/Device(Device::Type::CPU)),
/*allocator=*/nullptr,
/*resizable=*/false);

// We can't handle an arbitrary context.
ASSERT_THAT(cow::lazy_clone_storage(storage), testing::IsNull());
}

TEST(lazy_clone_storage_test, already_copy_on_write) {
std::unique_ptr<void, DeleterFnPtr> data(
new std::byte[5],
+[](void* bytes) { delete[] static_cast<std::byte*>(bytes); });
void* data_ptr = data.get();
StorageImpl original_storage(
{},
/*size_bytes=*/5,
at::DataPtr(
/*data=*/data_ptr,
/*ctx=*/new cow::COWDeleterContext(std::move(data)),
cow::cow_deleter,
Device(Device::Type::CPU)),
/*allocator=*/nullptr,
/*resizable=*/false);

ASSERT_THAT(original_storage, is_copy_on_write());

intrusive_ptr<StorageImpl> new_storage =
cow::lazy_clone_storage(original_storage);
ASSERT_THAT(new_storage.get(), testing::NotNull());

// The result is a different storage.
ASSERT_THAT(&*new_storage, testing::Ne(&original_storage));
// But it is also copy-on-write.
ASSERT_THAT(*new_storage, is_copy_on_write());
// But they share the same data!
ASSERT_THAT(new_storage->data(), testing::Eq(original_storage.data()));
}

} // namespace
} // namespace c10::impl
// NOLINTEND(clang-analyzer-cplusplus*)

0 comments on commit 97a513e

Please sign in to comment.