Home | History | Annotate | Download | only in kernels
      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 namespace tensorflow {
     21 
     22 namespace {
     23 class MemmappedTensorAllocator : public Allocator {
     24  public:
     25   MemmappedTensorAllocator() {}
     26 
     27   Status InitializeFromRegion(const string& name, Env* env) {
     28     const auto status =
     29         env->NewReadOnlyMemoryRegionFromFile(name, &memory_region_);
     30     if (!status.ok()) {
     31       return status;
     32     }
     33     return Status::OK();
     34   }
     35   string Name() override { return "MemmappedTensorAllocator"; }
     36 
     37   void* AllocateRaw(size_t alignment, size_t num_bytes) override {
     38     if ((reinterpret_cast<intptr_t>(memory_region_->data())) % alignment != 0) {
     39       allocation_status_ =
     40           errors::Internal("Readonly memory region has wrong alignment");
     41       return nullptr;
     42     }
     43     if (num_bytes > memory_region_->length()) {
     44       allocation_status_ = errors::Internal(
     45           "Readonly memory region has wrong length (", memory_region_->length(),
     46           ") when allocating ", num_bytes);
     47       return nullptr;
     48     }
     49     return const_cast<void*>(memory_region_->data());
     50   }
     51 
     52   void DeallocateRaw(void* ptr) override {
     53     if (ptr != memory_region_->data()) {
     54       LOG(ERROR)
     55           << "Deallocating not allocated region for readonly memory region";
     56     }
     57     if (delete_on_deallocate_) {
     58       delete this;
     59     }
     60   }
     61   const Status& allocation_status() const { return allocation_status_; }
     62 
     63   void set_delete_on_deallocate() { delete_on_deallocate_ = true; }
     64 
     65  private:
     66   std::unique_ptr<ReadOnlyMemoryRegion> memory_region_;
     67   // If there is an error during allocation we keep it in this status.
     68   Status allocation_status_;
     69 
     70   // When the allocator is owned by TensorBuffer it will be deleted on
     71   // de-allocation.
     72   bool delete_on_deallocate_ = false;
     73 
     74   TF_DISALLOW_COPY_AND_ASSIGN(MemmappedTensorAllocator);
     75 };
     76 }  // namespace
     77 
     78 ImmutableConstantOp::ImmutableConstantOp(OpKernelConstruction* context)
     79     : OpKernel(context) {
     80   OP_REQUIRES_OK(context,
     81                  context->GetAttr(kMemoryRegionNameAttr, &region_name_));
     82   OP_REQUIRES_OK(context, context->GetAttr(kDTypeAttr, &dtype_));
     83   OP_REQUIRES_OK(context, context->GetAttr(kShapeAttr, &shape_));
     84 }
     85 
     86 void ImmutableConstantOp::Compute(OpKernelContext* ctx) {
     87   std::unique_ptr<MemmappedTensorAllocator> allocator(
     88       new MemmappedTensorAllocator());
     89 
     90   OP_REQUIRES_OK(ctx,
     91                  allocator->InitializeFromRegion(region_name_, ctx->env()));
     92   ctx->set_output(0, Tensor(allocator.get(), dtype_, shape_));
     93   OP_REQUIRES_OK(ctx, allocator->allocation_status());
     94   // Allocator is owned by the tensor from this point.
     95   allocator.release()->set_delete_on_deallocate();
     96 }
     97 
     98 ImmutableConstantOp::~ImmutableConstantOp() {}
     99 constexpr char const* ImmutableConstantOp::kDTypeAttr;
    100 constexpr char const* ImmutableConstantOp::kShapeAttr;
    101 constexpr char const* ImmutableConstantOp::kMemoryRegionNameAttr;
    102 
    103 REGISTER_KERNEL_BUILDER(Name("ImmutableConst").Device(DEVICE_CPU),
    104                         ImmutableConstantOp);
    105 }  // namespace tensorflow
    106