Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2017 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/framework/op_kernel.h"
     17 #include "tensorflow/core/lib/core/errors.h"
     18 
     19 namespace tensorflow {
     20 namespace {
     21 
     22 // Refer to the Op description for detailed comments.
     23 class GuaranteeConstOp : public OpKernel {
     24  public:
     25   explicit GuaranteeConstOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
     26 
     27   void Compute(OpKernelContext* ctx) override {
     28     const DataType input_dtype = ctx->input_dtype(0);
     29     OP_REQUIRES(ctx, input_dtype != DT_RESOURCE,
     30                 errors::InvalidArgument(
     31                     "Input tensor cannot be a resource variable handle."));
     32     const Tensor& input_tensor = ctx->input(0);
     33     Tensor* output = nullptr;
     34     if (!ctx->forward_input_to_output_with_shape(0, 0, input_tensor.shape(),
     35                                                  &output)) {
     36       ctx->set_output(0, input_tensor);
     37     }
     38   }
     39 
     40   bool IsExpensive() override { return false; }
     41 };
     42 
     43 REGISTER_KERNEL_BUILDER(Name("GuaranteeConst").Device(DEVICE_CPU),
     44                         GuaranteeConstOp);
     45 
     46 }  // namespace
     47 }  // namespace tensorflow
     48