Skip to content
Permalink
Browse files Browse the repository at this point in the history
Mark MemmappedTensorAllocator as returning opaque handle.
This allocator is used for `ImmutableConstantOp` and it returns a handle to the contents of a memory mapped file which is supposed to represent a tensor.

For tensors of complex types (resources, variables and strings), allocators which are not marked as returning opaque handles will call placement new to initialize each element. This means writing to the buffer. However, in our case, the buffer is immutable and already contains the tensor data. Hence, writing to it is both destructive and causes a crash.

PiperOrigin-RevId: 345786451
Change-Id: I46369c50fa60b3431709ffe068a728d3061f49c4
  • Loading branch information
mihaimaruseac authored and tensorflower-gardener committed Dec 5, 2020
1 parent acdf3c0 commit c1e1fc8
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tensorflow/core/kernels/immutable_constant_op.cc
Expand Up @@ -62,6 +62,12 @@ class MemmappedTensorAllocator : public Allocator {

void set_delete_on_deallocate() { delete_on_deallocate_ = true; }

// Make sure tensors or complex types (strings, variants, resources) don't get
// their constructor called via a placement new since that would require
// writing to immutable data.
// See also: tensorflow/core/framework/typed_allocator.h
bool AllocatesOpaqueHandle() const override { return true; }

private:
std::unique_ptr<ReadOnlyMemoryRegion> memory_region_;
// If there is an error during allocation we keep it in this status.
Expand Down

0 comments on commit c1e1fc8

Please sign in to comment.