Skip to content

[PyTorch] Store Tensor explicitly in IValue #48824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix isAliasOf and isPtrType on "[PyTorch] Store Tensor explicitly in …
…IValue"

Enables following diff, which will make toTensor() return
`const Tensor&` and allow callers to avoid refcounting overhead.

Differential Revision: [D25324617](https://our.internmc.facebook.com/intern/diff/D25324617/)

[ghstack-poisoned]
  • Loading branch information
swolchok committed Dec 19, 2020
commit bad7e5e0bd2530466ab4900388db3a24200f5fd7
20 changes: 12 additions & 8 deletions aten/src/ATen/core/ivalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,26 +273,30 @@ struct CAFFE2_API IValue final {
return false;
}

if (!this->is_intrusive_ptr) {
// Primitive types don't alias anything
return false;
}

AT_ASSERT(rhs.is_intrusive_ptr);

// Tensors should be compared based on internal storage
if (this->isTensor()) {
const auto thisTensor = this->toTensor();
const auto rhsTensor = rhs.toTensor();
return thisTensor.is_alias_of(rhsTensor);
}

if (!this->is_intrusive_ptr) {
// Primitive types don't alias anything
return false;
}

AT_ASSERT(rhs.is_intrusive_ptr);

// Other types can be compared by their ptr value
return this->payload.u.as_intrusive_ptr == rhs.payload.u.as_intrusive_ptr;
}

/// @private [doxygen private]
size_t use_count() const noexcept {
if (isTensor()) {
return payload.as_tensor.use_count();
}

if (!is_intrusive_ptr) {
return 1;
}
Expand Down Expand Up @@ -780,7 +784,7 @@ struct CAFFE2_API IValue final {
const IValue& v);

bool isPtrType() const {
return is_intrusive_ptr;
return isTensor() || is_intrusive_ptr;
}

/// @private [doxygen private]
Expand Down
23 changes: 23 additions & 0 deletions aten/src/ATen/test/ivalue_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,28 @@ TEST(IValueTest, EnumEquality) {
);
}

TEST(IValueTest, isPtrType) {
IValue tensor(at::rand({3, 4}));
IValue integer(42);
IValue str("hello");

EXPECT_TRUE(tensor.isPtrType());
EXPECT_FALSE(integer.isPtrType());
EXPECT_TRUE(str.isPtrType());
}

TEST(IValueTest, isAliasOf) {
auto sampleIValues = makeSampleIValues();
for (auto& iv: sampleIValues) {
for (auto& iv2: sampleIValues) {
if (&iv == &iv2 && iv.isPtrType()) {
EXPECT_TRUE(iv.isAliasOf(iv2));
} else {
EXPECT_FALSE(iv.isAliasOf(iv2));
}
}
}
}

// TODO(gmagogsfm): Add type conversion test?
} // namespace c10