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 <string>
     17 #include <unordered_set>
     18 #include <utility>
     19 
     20 #include "tensorflow/core/framework/op_kernel.h"
     21 #include "tensorflow/core/framework/register_types.h"
     22 #include "tensorflow/core/framework/tensor.h"
     23 #include "tensorflow/core/framework/tensor_shape.h"
     24 #include "tensorflow/core/lib/core/status.h"
     25 
     26 namespace tensorflow {
     27 template <typename T, typename Tidx>
     28 class ListDiffOp : public OpKernel {
     29  public:
     30   explicit ListDiffOp(OpKernelConstruction* context) : OpKernel(context) {
     31     const DataType dt = DataTypeToEnum<T>::v();
     32     const DataType dtidx = DataTypeToEnum<Tidx>::v();
     33     OP_REQUIRES_OK(context, context->MatchSignature({dt, dt}, {dt, dtidx}));
     34   }
     35 
     36   void Compute(OpKernelContext* context) override {
     37     const Tensor& x = context->input(0);
     38     const Tensor& y = context->input(1);
     39 
     40     OP_REQUIRES(context, TensorShapeUtils::IsVector(x.shape()),
     41                 errors::InvalidArgument("x should be a 1D vector."));
     42 
     43     OP_REQUIRES(context, TensorShapeUtils::IsVector(y.shape()),
     44                 errors::InvalidArgument("y should be a 1D vector."));
     45 
     46     const auto Tx = x.vec<T>();
     47     const size_t x_size = Tx.size();
     48     const auto Ty = y.vec<T>();
     49     const size_t y_size = Ty.size();
     50 
     51     OP_REQUIRES(context, x_size < std::numeric_limits<int32>::max(),
     52                 errors::InvalidArgument("x too large for int32 indexing"));
     53 
     54     std::unordered_set<T> y_set;
     55     y_set.reserve(y_size);
     56     for (size_t i = 0; i < y_size; ++i) {
     57       y_set.insert(Ty(i));
     58     }
     59 
     60     // Compute the size of the output.
     61 
     62     int64 out_size = 0;
     63     for (size_t i = 0; i < x_size; ++i) {
     64       if (y_set.count(Tx(i)) == 0) {
     65         ++out_size;
     66       }
     67     }
     68 
     69     // Allocate and populate outputs.
     70     Tensor* out = nullptr;
     71     OP_REQUIRES_OK(context, context->allocate_output(0, {out_size}, &out));
     72     auto Tout = out->vec<T>();
     73 
     74     Tensor* indices = nullptr;
     75     OP_REQUIRES_OK(context, context->allocate_output(1, {out_size}, &indices));
     76     auto Tindices = indices->vec<Tidx>();
     77 
     78     for (Tidx i = 0, p = 0; i < static_cast<Tidx>(x_size); ++i) {
     79       if (y_set.count(Tx(i)) == 0) {
     80         OP_REQUIRES(context, p < out_size,
     81                     errors::InvalidArgument(
     82                         "Tried to set output index ", p,
     83                         " when output Tensor only had ", out_size,
     84                         " elements. Check that your "
     85                         "input tensors are not being concurrently mutated."));
     86         Tout(p) = Tx(i);
     87         Tindices(p) = i;
     88         p++;
     89       }
     90     }
     91   }
     92 };
     93 
     94 #define REGISTER_LISTDIFF(type)                                  \
     95   REGISTER_KERNEL_BUILDER(Name("ListDiff")                       \
     96                               .Device(DEVICE_CPU)                \
     97                               .TypeConstraint<type>("T")         \
     98                               .TypeConstraint<int32>("out_idx"), \
     99                           ListDiffOp<type, int32>)               \
    100   REGISTER_KERNEL_BUILDER(Name("ListDiff")                       \
    101                               .Device(DEVICE_CPU)                \
    102                               .TypeConstraint<type>("T")         \
    103                               .TypeConstraint<int64>("out_idx"), \
    104                           ListDiffOp<type, int64>)
    105 
    106 TF_CALL_REAL_NUMBER_TYPES(REGISTER_LISTDIFF);
    107 REGISTER_LISTDIFF(string);
    108 #undef REGISTER_LISTDIFF
    109 
    110 }  // namespace tensorflow
    111