1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/core/kernels/immutable_constant_op.h"
17
18 #include <unordered_set>
19
20 #include "tensorflow/core/framework/types.pb.h"
21
22 namespace tensorflow {
23
24 namespace {
25 class MemmappedTensorAllocator : public Allocator {
26 public:
MemmappedTensorAllocator()27 MemmappedTensorAllocator() {}
28
InitializeFromRegion(const string & name,Env * env)29 Status InitializeFromRegion(const string& name, Env* env) {
30 const auto status =
31 env->NewReadOnlyMemoryRegionFromFile(name, &memory_region_);
32 if (!status.ok()) {
33 return status;
34 }
35 return OkStatus();
36 }
Name()37 string Name() override { return "MemmappedTensorAllocator"; }
38
AllocateRaw(size_t alignment,size_t num_bytes)39 void* AllocateRaw(size_t alignment, size_t num_bytes) override {
40 if ((reinterpret_cast<intptr_t>(memory_region_->data())) % alignment != 0) {
41 allocation_status_ =
42 errors::Internal("Readonly memory region has wrong alignment");
43 return nullptr;
44 }
45 if (num_bytes > memory_region_->length()) {
46 allocation_status_ = errors::Internal(
47 "Readonly memory region has wrong length (", memory_region_->length(),
48 ") when allocating ", num_bytes);
49 return nullptr;
50 }
51 return const_cast<void*>(memory_region_->data());
52 }
53
DeallocateRaw(void * ptr)54 void DeallocateRaw(void* ptr) override {
55 if (ptr != memory_region_->data()) {
56 LOG(ERROR)
57 << "Deallocating not allocated region for readonly memory region";
58 }
59 if (delete_on_deallocate_) {
60 delete this;
61 }
62 }
allocation_status() const63 const Status& allocation_status() const { return allocation_status_; }
64
set_delete_on_deallocate()65 void set_delete_on_deallocate() { delete_on_deallocate_ = true; }
66
67 // Make sure tensors or complex types (strings, variants, resources) don't get
68 // their constructor called via a placement new since that would require
69 // writing to immutable data.
70 // See also: tensorflow/core/framework/typed_allocator.h
AllocatesOpaqueHandle() const71 bool AllocatesOpaqueHandle() const override { return true; }
72
73 private:
74 std::unique_ptr<ReadOnlyMemoryRegion> memory_region_;
75 // If there is an error during allocation we keep it in this status.
76 Status allocation_status_;
77
78 // When the allocator is owned by TensorBuffer it will be deleted on
79 // de-allocation.
80 bool delete_on_deallocate_ = false;
81
82 TF_DISALLOW_COPY_AND_ASSIGN(MemmappedTensorAllocator);
83 };
84 } // namespace
85
ImmutableConstantOp(OpKernelConstruction * context)86 ImmutableConstantOp::ImmutableConstantOp(OpKernelConstruction* context)
87 : OpKernel(context) {
88 OP_REQUIRES_OK(context,
89 context->GetAttr(kMemoryRegionNameAttr, ®ion_name_));
90 OP_REQUIRES_OK(context, context->GetAttr(kDTypeAttr, &dtype_));
91 OP_REQUIRES(context, dtype_ != DT_RESOURCE && dtype_ != DT_VARIANT,
92 errors::InvalidArgument(
93 "Resource and variant dtypes are invalid for this op."));
94 OP_REQUIRES_OK(context, context->GetAttr(kShapeAttr, &shape_));
95 }
96
Compute(OpKernelContext * ctx)97 void ImmutableConstantOp::Compute(OpKernelContext* ctx) {
98 std::unique_ptr<MemmappedTensorAllocator> allocator(
99 new MemmappedTensorAllocator());
100
101 OP_REQUIRES_OK(ctx,
102 allocator->InitializeFromRegion(region_name_, ctx->env()));
103 OP_REQUIRES(ctx, dtype_ != DT_STRING,
104 errors::Unimplemented("Sorry, DT_STRING is not currently "
105 "supported for ImmutableConstOp."));
106 ctx->set_output(0, Tensor(allocator.get(), dtype_, shape_));
107 OP_REQUIRES_OK(ctx, allocator->allocation_status());
108 // Allocator is owned by the tensor from this point.
109 allocator.release()->set_delete_on_deallocate();
110 }
111
~ImmutableConstantOp()112 ImmutableConstantOp::~ImmutableConstantOp() {}
113 constexpr char const* ImmutableConstantOp::kDTypeAttr;
114 constexpr char const* ImmutableConstantOp::kShapeAttr;
115 constexpr char const* ImmutableConstantOp::kMemoryRegionNameAttr;
116
117 REGISTER_KERNEL_BUILDER(Name("ImmutableConst").Device(DEVICE_CPU),
118 ImmutableConstantOp);
119 } // namespace tensorflow
120