Return false instead of crashing in Tensor::SharesBufferWith if neither tensor has...
authorA. Unique TensorFlower <gardener@tensorflow.org>
Mon, 12 Feb 2018 22:01:51 +0000 (14:01 -0800)
committerTensorFlower Gardener <gardener@tensorflow.org>
Mon, 12 Feb 2018 22:05:39 +0000 (14:05 -0800)
  buf_ != nullptr && b.buf_ != nullptr &&  buf_->root_buffer() == b.buf_->root_buffer();

still satisfies the contract in the header, i.e. "True iff the two tensors use the same underlying refcounted storage."

PiperOrigin-RevId: 185431574

tensorflow/core/framework/tensor.cc
tensorflow/core/framework/tensor_test.cc

index 0645ec4..5d32b71 100644 (file)
@@ -1025,9 +1025,8 @@ StringPiece Tensor::tensor_data() const {
 }
 
 bool Tensor::SharesBufferWith(const Tensor& b) const {
-  CHECK_NE(nullptr, buf_);
-  CHECK_NE(nullptr, b.buf_);
-  return buf_->root_buffer() == b.buf_->root_buffer();
+  return buf_ != nullptr && b.buf_ != nullptr &&
+         buf_->root_buffer() == b.buf_->root_buffer();
 }
 
 string Tensor::DebugString() const {
index 8164438..b613eff 100644 (file)
@@ -1085,6 +1085,21 @@ class DummyCPUAllocator : public Allocator {
   void DeallocateRaw(void* ptr) override {}
 };
 
+TEST(Tensor, SharesBufferWith) {
+  Tensor a_empty;
+  Tensor b_empty;
+  Tensor a(DT_FLOAT, TensorShape({1}));
+  Tensor b(DT_FLOAT, TensorShape({1}));
+  Tensor copy(a);
+  EXPECT_FALSE(a_empty.SharesBufferWith(a_empty));
+  EXPECT_FALSE(a_empty.SharesBufferWith(b_empty));
+  EXPECT_FALSE(a_empty.SharesBufferWith(a));
+  EXPECT_FALSE(a_empty.SharesBufferWith(copy));
+  EXPECT_TRUE(a.SharesBufferWith(a));
+  EXPECT_FALSE(a.SharesBufferWith(b));
+  EXPECT_TRUE(a.SharesBufferWith(copy));
+}
+
 TEST(Tensor, FailureToAllocate) {
   TensorShape shape({1});
   DummyCPUAllocator allocator;