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
more UB removal 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 48211e973867a2bd78a2a6b86d35bfa96f25b9ff
33 changes: 8 additions & 25 deletions aten/src/ATen/core/ivalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ struct CAFFE2_API IValue final {
// make this abundantly clear.
//
// payload.as_tensor.~Tensor();
copyNontensorPayload(rhs.payload, rhs.tag);
payload.u = rhs.payload.u;
new (&rhs.payload.as_tensor) at::Tensor(std::move(t));
} else if (rhs.isTensor()) {
rhs.swap(*this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is potentially slow because it needs to do the isTensor checks again (depending on how smart the compiler is with inlining this and proving that the extra branches are never executed). Not sure if relevant in practice, but if you want to optimize it, you could just move lines 332 to 335 into their own subfunction swapWithTensor(lhs, rhs) or something like that and call it from both the isTensor() and rhs.isTensor() case.

return;
} else {
std::swap(reinterpret_cast<char(&)[sizeof(payload)]>(*&payload), reinterpret_cast<char(&)[sizeof(payload)]>(*&rhs.payload));
std::swap(payload.u, rhs.payload.u);
}
std::swap(is_intrusive_ptr, rhs.is_intrusive_ptr);
std::swap(tag, rhs.tag);
Expand Down Expand Up @@ -883,7 +883,7 @@ struct CAFFE2_API IValue final {
//
// rhs.payload.as_tensor.~Tensor();
} else {
copyNontensorPayload(rhs.payload, rhs.tag);
payload.u = rhs.payload.u;
}
tag = rhs.tag;
is_intrusive_ptr = rhs.is_intrusive_ptr;
Expand Down Expand Up @@ -925,22 +925,18 @@ struct CAFFE2_API IValue final {
if (isTensor()) {
new (&payload.as_tensor) at::Tensor(p.as_tensor);
} else {
copyNontensorPayload(p, t);
payload.u = p.u;
}
}

void copyNontensorPayload(const Payload& from, Tag t) noexcept {
payload.u = from.u;
}

Payload payload;
Tag tag;
bool is_intrusive_ptr;
friend struct WeakIValue;
};

struct CAFFE2_API WeakIValue final {
WeakIValue() : payload{0}, tag(IValue::Tag::None), is_intrusive_ptr(false) {}
WeakIValue() : tag(IValue::Tag::None), is_intrusive_ptr(false) {}

WeakIValue(const WeakIValue& rhs)
: payload(rhs.payload),
Expand All @@ -957,8 +953,7 @@ struct CAFFE2_API WeakIValue final {
payload.as_intrusive_ptr = rhs.unsafeToTensorImpl();
is_intrusive_ptr = true;
} else {
static_assert(sizeof(payload) == sizeof(rhs.payload.u), "WeakIValue payload is out of sync");
memcpy(&payload, &rhs.payload.u, sizeof(payload));
payload = rhs.payload.u;
}
if (is_intrusive_ptr) {
if (payload.as_intrusive_ptr != c10::UndefinedTensorImpl::singleton()) {
Expand Down Expand Up @@ -996,8 +991,7 @@ struct CAFFE2_API WeakIValue final {
IValue lock() const {
if (!is_intrusive_ptr) {
IValue::Payload newPayload;
static_assert(sizeof(payload) == sizeof(newPayload.u), "WeakIValue payload is out of sync");
memcpy(&newPayload.u, &payload, sizeof(payload));
newPayload.u = payload;
return IValue(newPayload, tag, false);
}
if (IValue::Tag::Tensor == tag) {
Expand Down Expand Up @@ -1052,18 +1046,7 @@ struct CAFFE2_API WeakIValue final {
}

private:
union Payload {
int64_t as_int;
double as_double;
bool as_bool;
// Invariant: never nullptr; null state is represented as
// UndefinedTensorImpl::singleton() for consistency with Tensor.
c10::intrusive_ptr_target* as_intrusive_ptr;
struct {
DeviceType type;
DeviceIndex index;
} as_device;
};
using Payload = IValue::Payload::TriviallyCopyablePayload;
Payload payload;
IValue::Tag tag;
bool is_intrusive_ptr;
Expand Down
2 changes: 0 additions & 2 deletions aten/src/ATen/core/ivalue_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1296,8 +1296,6 @@ inline bool IValue::isSameIdentity(const IValue& rhs) const {
// for bool type, do equality check
return this->toBool() == rhs.toBool();
} else if (this->isTensor() && rhs.isTensor()) {
// for tensor type, just check the as_intrusive_ptr since is_intrusive_ptr
// is false for undefined tensor
return this->payload.as_tensor.is_same(rhs.payload.as_tensor);
} else if (this->isTensor() && rhs.isNone()) {
// special case: undefined tensor and None are the same identity
Expand Down