• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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 #include "tensorflow/core/kernels/data/cache_ops.h"
16 
17 #include "tensorflow/core/framework/dataset.h"
18 #include "tensorflow/core/framework/partial_tensor_shape.h"
19 #include "tensorflow/core/framework/resource_mgr.h"
20 #include "tensorflow/core/framework/tensor.h"
21 #include "tensorflow/core/kernels/data/dataset_utils.h"
22 #include "tensorflow/core/lib/random/philox_random.h"
23 #include "tensorflow/core/lib/random/random.h"
24 #include "tensorflow/core/lib/random/random_distributions.h"
25 
26 namespace tensorflow {
27 namespace data {
28 namespace {
29 
30 constexpr char kMemoryCache[] = "MemoryCache";
31 
32 }  // namespace
33 
DebugString() const34 string MemoryCacheManager::DebugString() const { return kMemoryCache; }
35 
Complete(std::vector<std::vector<Tensor>> && cache)36 void MemoryCache::Complete(std::vector<std::vector<Tensor>>&& cache) {
37   mutex_lock l(mu_);
38   if (!completed_) {
39     cache_ = std::move(cache);
40     completed_ = true;
41   }
42 }
43 
IsCompleted()44 bool MemoryCache::IsCompleted() {
45   tf_shared_lock l(mu_);
46   return completed_;
47 }
48 
Reset()49 void MemoryCache::Reset() {
50   mutex_lock l(mu_);
51   completed_ = false;
52   cache_.clear();
53 }
54 
at(int64 index)55 const std::vector<Tensor>& MemoryCache::at(int64 index) {
56   tf_shared_lock l(mu_);
57   DCHECK(index < cache_.size());
58   return cache_[index];
59 }
60 
size()61 size_t MemoryCache::size() {
62   tf_shared_lock l(mu_);
63   return cache_.size();
64 }
65 
data()66 const std::vector<std::vector<Tensor>>& MemoryCache::data() {
67   tf_shared_lock l(mu_);
68   return cache_;
69 }
70 
AnonymousMemoryCacheHandleOp(OpKernelConstruction * ctx)71 AnonymousMemoryCacheHandleOp::AnonymousMemoryCacheHandleOp(
72     OpKernelConstruction* ctx)
73     : AnonymousResourceOp<MemoryCacheManager>(ctx) {}
74 
name()75 string AnonymousMemoryCacheHandleOp::name() { return kMemoryCache; }
76 
CreateResource(OpKernelContext * ctx,std::unique_ptr<FunctionLibraryDefinition> flib_def,std::unique_ptr<ProcessFunctionLibraryRuntime> pflr,FunctionLibraryRuntime * lib,MemoryCacheManager ** manager)77 Status AnonymousMemoryCacheHandleOp::CreateResource(
78     OpKernelContext* ctx, std::unique_ptr<FunctionLibraryDefinition> flib_def,
79     std::unique_ptr<ProcessFunctionLibraryRuntime> pflr,
80     FunctionLibraryRuntime* lib, MemoryCacheManager** manager) {
81   *manager = new MemoryCacheManager();
82   return Status::OK();
83 }
84 
Compute(OpKernelContext * ctx)85 void DeleteMemoryCacheOp::Compute(OpKernelContext* ctx) {
86   const ResourceHandle& handle = ctx->input(0).flat<ResourceHandle>()(0);
87   // The resource might have been already deleted by the dataset.
88   Status s = ctx->resource_manager()->Delete(handle);
89   if (!errors::IsNotFound(s)) {
90     OP_REQUIRES_OK(ctx, s);
91   }
92 }
93 
94 namespace {
95 
96 REGISTER_KERNEL_BUILDER(Name("AnonymousMemoryCache").Device(DEVICE_CPU),
97                         AnonymousMemoryCacheHandleOp);
98 
99 REGISTER_KERNEL_BUILDER(Name("DeleteMemoryCache").Device(DEVICE_CPU),
100                         DeleteMemoryCacheOp);
101 
102 REGISTER_KERNEL_BUILDER(Name("DummyMemoryCache").Device(DEVICE_CPU),
103                         DummyResourceOp<MemoryCache>);
104 
105 }  // namespace
106 }  // namespace data
107 }  // namespace tensorflow
108