1 /* Copyright 2015 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 #ifndef TENSORFLOW_CORE_KERNELS_ASSIGN_OP_H_ 17 #define TENSORFLOW_CORE_KERNELS_ASSIGN_OP_H_ 18 19 #define EIGEN_USE_THREADS 20 21 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/framework/ref_var.h" 24 #include "tensorflow/core/framework/tensor_types.h" 25 26 namespace tensorflow { 27 28 // TODO(jeff): Get rid of use_exclusive_lock_ option 29 30 // Computes *input[0] = input[1] 31 class AssignOp : public OpKernel { 32 public: AssignOp(OpKernelConstruction * context)33 explicit AssignOp(OpKernelConstruction* context) : OpKernel(context) { 34 OP_REQUIRES_OK(context, 35 context->GetAttr("use_locking", &use_exclusive_lock_)); 36 OP_REQUIRES_OK(context, 37 context->GetAttr("validate_shape", &validate_shape_)); 38 OP_REQUIRES(context, IsRefType(context->input_type(0)), 39 errors::InvalidArgument("lhs input needs to be a ref type")); 40 if (!context 41 ->GetAttr("_grappler_relax_allocator_constraints", 42 &relax_constraints_) 43 .ok()) { 44 relax_constraints_ = false; 45 } 46 } 47 Compute(OpKernelContext * context)48 void Compute(OpKernelContext* context) override { 49 constexpr int input_ref_index = 0; 50 constexpr int output_ref_index = 0; 51 constexpr int value_index = 1; 52 53 auto copy = [this](OpKernelContext* cc_ctx, Tensor* lhs, 54 const Tensor& rhs) { Copy(cc_ctx, lhs, rhs); }; 55 56 AssignRefVariable(context, input_ref_index, output_ref_index, value_index, 57 use_exclusive_lock_, validate_shape_, relax_constraints_, 58 copy); 59 } 60 61 virtual void Copy(OpKernelContext* context, Tensor* lhs, 62 const Tensor& rhs) = 0; 63 64 bool use_exclusive_lock_; 65 bool validate_shape_; 66 bool relax_constraints_; 67 }; 68 69 } // end namespace tensorflow 70 71 #endif // TENSORFLOW_CORE_KERNELS_ASSIGN_OP_H_ 72