Home | History | Annotate | Download | only in kernels
      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 #include "tensorflow/core/framework/op_kernel.h"
     17 #include "tensorflow/core/kernels/variable_ops.h"
     18 #include "tensorflow/core/lib/core/errors.h"
     19 #include "tensorflow/core/platform/mutex.h"
     20 #include "tensorflow/core/platform/types.h"
     21 
     22 namespace tensorflow {
     23 
     24 template <class T>
     25 class CountUpToOp : public OpKernel {
     26  public:
     27   explicit CountUpToOp(OpKernelConstruction* context) : OpKernel(context) {
     28     OP_REQUIRES_OK(context, context->GetAttr("limit", &limit_));
     29   }
     30 
     31   void Compute(OpKernelContext* context) override {
     32     T before_increment;
     33     {
     34       mutex_lock l(*context->input_ref_mutex(0));
     35       Tensor tensor = context->mutable_input(0, true);
     36       OP_REQUIRES(context, TensorShapeUtils::IsScalar(tensor.shape()),
     37                   errors::InvalidArgument("input is not a scalar: ",
     38                                           tensor.shape().DebugString()));
     39       T* ptr = &tensor.scalar<T>()();
     40       before_increment = *ptr;
     41       if (*ptr >= limit_) {
     42         context->SetStatus(errors::OutOfRange("Reached limit of ", limit_));
     43         return;
     44       }
     45       ++*ptr;
     46     }
     47     // Output if no error.
     48     Tensor* out_tensor;
     49     OP_REQUIRES_OK(context, context->allocate_output("output", TensorShape({}),
     50                                                      &out_tensor));
     51     out_tensor->scalar<T>()() = before_increment;
     52   }
     53 
     54  private:
     55   T limit_;
     56 };
     57 
     58 template <class T>
     59 class ResourceCountUpToOp : public OpKernel {
     60  public:
     61   explicit ResourceCountUpToOp(OpKernelConstruction* context)
     62       : OpKernel(context) {
     63     OP_REQUIRES_OK(context, context->GetAttr("limit", &limit_));
     64     OP_REQUIRES_OK(context, context->GetAttr("T", &dtype_));
     65   }
     66 
     67   void Compute(OpKernelContext* context) override {
     68     Var* variable = nullptr;
     69     OP_REQUIRES_OK(
     70         context,
     71         LookupResource<Var>(context, HandleFromInput(context, 0), &variable));
     72     core::ScopedUnref s(variable);
     73     mutex_lock l(*variable->mu());
     74     Tensor before_increment = *variable->tensor();
     75     OP_REQUIRES(
     76         context, TensorShapeUtils::IsScalar(before_increment.shape()),
     77         errors::InvalidArgument("input is not a scalar: ",
     78                                 before_increment.shape().DebugString()));
     79     if (before_increment.scalar<T>()() >= limit_) {
     80       context->SetStatus(errors::OutOfRange("Reached limit of ", limit_));
     81       return;
     82     }
     83     // Allocate new buffer
     84     AllocatorAttributes attr;
     85     attr.set_gpu_compatible(true);
     86     attr.set_nic_compatible(true);
     87     PersistentTensor unused;
     88     Tensor* tmp;
     89     OP_REQUIRES_OK(context, context->allocate_persistent(
     90                                 dtype_, TensorShape({}), &unused, &tmp, attr));
     91     *variable->tensor() = *tmp;
     92     tmp->scalar<T>()() = before_increment.scalar<T>()() + 1;
     93     context->set_output(0, before_increment);
     94   }
     95 
     96  private:
     97   T limit_;
     98   DataType dtype_;
     99 };
    100 
    101 #define REGISTER(TYPE)                                                        \
    102   REGISTER_KERNEL_BUILDER(                                                    \
    103       Name("CountUpTo").TypeConstraint<TYPE>("T").Device(DEVICE_CPU),         \
    104       CountUpToOp<TYPE>)                                                      \
    105   REGISTER_KERNEL_BUILDER(                                                    \
    106       Name("ResourceCountUpTo").TypeConstraint<TYPE>("T").Device(DEVICE_CPU), \
    107       ResourceCountUpToOp<TYPE>)
    108 
    109 REGISTER(int32);
    110 REGISTER(int64);
    111 
    112 #undef REGISTER
    113 
    114 }  // namespace tensorflow
    115