1/* Copyright 2018 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#ifndef TENSORFLOW_CORE_KERNELS_RESOURCE_VARIABLE_OPS_H_
16#define TENSORFLOW_CORE_KERNELS_RESOURCE_VARIABLE_OPS_H_
17
18#include "tensorflow/core/framework/op_kernel.h"
19#include "tensorflow/core/framework/resource_mgr.h"
20#include "tensorflow/core/framework/resource_var.h"
21
22namespace tensorflow {
23
24class VarHandleOp : public OpKernel {
25 public:
26 explicit VarHandleOp(OpKernelConstruction* c);
27 void Compute(OpKernelContext* ctx) override;
28 const Tensor* const_tensor() const override {
29 return is_anonymous_ ? nullptr : &const_tensor_;
30 }
31
32 private:
33 // Same fields as in ResourceHandleOp.
34 bool is_anonymous_;
35 string container_;
36 string name_;
37 Tensor const_tensor_;
38
39 DtypeAndPartialTensorShape dtype_and_shape_;
40};
41
42class ReadVariableOp : public OpKernel {
43 public:
44 explicit ReadVariableOp(OpKernelConstruction* c);
45 void Compute(OpKernelContext* ctx) override;
46
47 private:
48 DataType dtype_;
49};
50
51class ReadVariablesOp : public OpKernel {
52 public:
53 explicit ReadVariablesOp(OpKernelConstruction* c);
54 void Compute(OpKernelContext* ctx) override;
55 bool IsExpensive() override { return false; }
56
57 private:
58 DataTypeVector dtypes_;
59};
60
61class DestroyResourceOp : public OpKernel {
62 public:
63 explicit DestroyResourceOp(OpKernelConstruction* ctx);
64 void Compute(OpKernelContext* ctx) override;
65
66 private:
67 bool ignore_lookup_error_;
68};
69
70class DisableCopyOnReadOp : public OpKernel {
71 public:
72 explicit DisableCopyOnReadOp(OpKernelConstruction* c) : OpKernel(c) {}
73 void Compute(OpKernelContext* ctx) override;
74};
75
76template <typename T>
77class VariableShapeOp : public OpKernel {
78 public:
79 explicit VariableShapeOp(OpKernelConstruction* c) : OpKernel(c) {}
80
81 void Compute(OpKernelContext* ctx) override {
82 core::RefCountPtr<Var> variable;
83 OP_REQUIRES_OK(ctx,
84 LookupResource(ctx, HandleFromInput(ctx, 0), &variable));
85 variable->mu()->lock_shared();
86 TensorShape shape = variable->tensor()->shape();
87 variable->mu()->unlock_shared();
88 Tensor* output;
89 OP_REQUIRES_OK(ctx, ctx->allocate_output(0, {shape.dims()}, &output));
90 for (int i = 0; i < shape.dims(); ++i) {
91 output->flat<T>()(i) = shape.dim_size(i);
92 }
93 }
94};
95
96} // namespace tensorflow
97
98#endif // TENSORFLOW_CORE_KERNELS_RESOURCE_VARIABLE_OPS_H_
99