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 // XLA-specific Fill Op.
     17 
     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/core/framework/kernel_def_builder.h"
     23 #include "tensorflow/core/framework/register_types.h"
     24 
     25 namespace tensorflow {
     26 namespace {
     27 
     28 class FillOp : public XlaOpKernel {
     29  public:
     30   explicit FillOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
     31 
     32   void Compile(XlaOpKernelContext* ctx) override {
     33     // The output of this Op is a tensor of shape 'dims_shape' with each
     34     // element set to the scalar 'dims_literal'.
     35     const TensorShape dims_shape = ctx->InputShape(0);
     36     const TensorShape value_shape = ctx->InputShape(1);
     37     OP_REQUIRES(
     38         ctx, IsLegacyVector(dims_shape),
     39         errors::InvalidArgument("dims must be a vector of int32, got shape ",
     40                                 dims_shape.DebugString()));
     41     OP_REQUIRES(ctx, IsLegacyScalar(value_shape),
     42                 errors::InvalidArgument("value must be a scalar, got shape ",
     43                                         value_shape.DebugString()));
     44     // Evaluate the 'dims' constant input, reshaping to a vector if it
     45     // was a 'legacy' vector (secretly a scalar).
     46     xla::Literal dims_literal;
     47     OP_REQUIRES_OK(ctx, ctx->ConstantInputReshaped(
     48                             0, {dims_shape.num_elements()}, &dims_literal));
     49 
     50     // Convert the dims literal into a vector that we can pass to
     51     // ComputationBuilder.
     52     std::vector<int64> broadcast;
     53     broadcast.reserve(dims_literal.shape().dimensions(0));
     54     for (int i = 0; i < dims_literal.shape().dimensions(0); ++i) {
     55       broadcast.push_back(dims_literal.Get<int>({i}));
     56     }
     57     // Look up the value input, reshaping to a scalar if it was a
     58     // 'legacy' scalar (secretly a vector).
     59     xla::ComputationDataHandle data = ctx->Input(1);
     60     if (value_shape.dims() > 0) {
     61       CHECK_EQ(value_shape.dims(), 1);
     62       data = ctx->builder()->Reshape(data, {});
     63     }
     64     // Emit the actual computation, which broadcasts the scalar to the
     65     // desired shape.
     66     auto result = ctx->builder()->Broadcast(data, broadcast);
     67 
     68     ctx->SetOutput(0, result);
     69   }
     70 };
     71 
     72 REGISTER_XLA_OP(Name("Fill").CompileTimeConstInput("dims"), FillOp);
     73 
     74 }  // namespace
     75 }  // namespace tensorflow
     76