Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2018 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/compiler/tf2xla/lib/scatter.h"
     17 #include "tensorflow/compiler/tf2xla/shape_util.h"
     18 #include "tensorflow/compiler/tf2xla/type_util.h"
     19 #include "tensorflow/compiler/tf2xla/xla_helpers.h"
     20 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
     21 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
     22 #include "tensorflow/compiler/xla/status_macros.h"
     23 #include "tensorflow/core/framework/kernel_def_builder.h"
     24 #include "tensorflow/core/framework/op_kernel.h"
     25 
     26 namespace tensorflow {
     27 namespace {
     28 
     29 // Check whether updates.shape = indices.shape[:batch_dim] +
     30 // buffer_shape[num_index_dims:]
     31 Status ValidateUpdateShape(const TensorShape& buffer_shape,
     32                            const TensorShape& indices_shape,
     33                            const TensorShape& updates_shape) {
     34   if (indices_shape.dims() < 1) {
     35     return errors::InvalidArgument(
     36         "indices shape must have >= 1 dimension; got ",
     37         indices_shape.DebugString());
     38   }
     39 
     40   const int64 num_index_dims = indices_shape.dim_size(indices_shape.dims() - 1);
     41   const int64 batch_dim = indices_shape.dims() - 1;
     42 
     43   auto shape_err = [&]() {
     44     return errors::InvalidArgument(
     45         "Must have updates.shape = indices.shape[:batch_dim] + ",
     46         "buffer_shape[num_index_dims:], got updates.shape: ",
     47         updates_shape.DebugString(),
     48         ", indices.shape: ", indices_shape.DebugString(),
     49         ", buffer_shape: ", buffer_shape.DebugString(),
     50         ", num_index_dims: ", num_index_dims, ", and batch_dim: ", batch_dim);
     51   };
     52 
     53   if (updates_shape.dims() < batch_dim) return shape_err();
     54   if (buffer_shape.dims() <
     55       num_index_dims + (updates_shape.dims() - batch_dim)) {
     56     return shape_err();
     57   }
     58   if (updates_shape.dims() !=
     59       batch_dim + buffer_shape.dims() - num_index_dims) {
     60     return shape_err();
     61   }
     62   for (int d = 0; d < batch_dim; ++d) {
     63     if (updates_shape.dim_size(d) != indices_shape.dim_size(d)) {
     64       return shape_err();
     65     }
     66   }
     67   for (int d = 0; d < updates_shape.dims() - batch_dim; ++d) {
     68     if (updates_shape.dim_size(d + batch_dim) !=
     69         buffer_shape.dim_size(d + num_index_dims)) {
     70       return shape_err();
     71     }
     72   }
     73   return Status::OK();
     74 }
     75 
     76 class ScatterNdOp : public XlaOpKernel {
     77  public:
     78   explicit ScatterNdOp(OpKernelConstruction* context) : XlaOpKernel(context) {}
     79 
     80   void Compile(XlaOpKernelContext* context) override {
     81     DataType dtype = context->input_type(1);
     82 
     83     TensorShape indices_shape = context->InputShape(0);
     84     TensorShape updates_shape = context->InputShape(1);
     85 
     86     TensorShape buffer_shape;
     87     OP_REQUIRES_OK(context, context->ConstantInputAsShape(2, &buffer_shape));
     88 
     89     OP_REQUIRES(
     90         context, TensorShapeUtils::IsVectorOrHigher(buffer_shape),
     91         errors::InvalidArgument("Output must be at least 1-D, ",
     92                                 "got shape: ", buffer_shape.DebugString()));
     93 
     94     OP_REQUIRES(
     95         context,
     96         buffer_shape.num_elements() > 0 || (indices_shape.num_elements() == 0 &&
     97                                             updates_shape.num_elements() == 0),
     98         errors::InvalidArgument(
     99             "Indices and updates specified for empty output. indices shape: ",
    100             indices_shape.DebugString()));
    101 
    102     OP_REQUIRES_OK(context, ValidateUpdateShape(buffer_shape, indices_shape,
    103                                                 updates_shape));
    104 
    105     xla::ComputationBuilder* builder = context->builder();
    106     auto buffer = builder->Broadcast(XlaHelpers::Zero(builder, dtype),
    107                                      buffer_shape.dim_sizes());
    108     auto indices = context->Input(0);
    109     auto updates = context->Input(1);
    110     auto result =
    111         XlaScatter(buffer, updates, indices,
    112                    /*indices_are_vectors=*/true, /*combiner=*/{}, builder);
    113     OP_REQUIRES_OK(context, result.status());
    114     context->SetOutput(0, result.ValueOrDie());
    115   }
    116 };
    117 
    118 REGISTER_XLA_OP(Name("ScatterNd").CompileTimeConstInput("shape"), ScatterNdOp);
    119 
    120 }  // namespace
    121 }  // namespace tensorflow
    122