1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
22namespace tensorflow {
23
24namespace {
25class MemmappedTensorAllocator : public Allocator {
26 public:
27 MemmappedTensorAllocator() {}
28
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 }
37 string Name() override { return "MemmappedTensorAllocator"; }
38
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
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 }
63 const Status& allocation_status() const { return allocation_status_; }
64
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
71 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
86ImmutableConstantOp::ImmutableConstantOp(OpKernelConstruction* context)
87 : OpKernel(context) {
88 OP_REQUIRES_OK(context,
89 context->GetAttr(kMemoryRegionNameAttr, &region_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
97void 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
112ImmutableConstantOp::~ImmutableConstantOp() {}
113constexpr char const* ImmutableConstantOp::kDTypeAttr;
114constexpr char const* ImmutableConstantOp::kShapeAttr;
115constexpr char const* ImmutableConstantOp::kMemoryRegionNameAttr;
116
117REGISTER_KERNEL_BUILDER(Name("ImmutableConst").Device(DEVICE_CPU),
118 ImmutableConstantOp);
119} // namespace tensorflow
120