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/compiler/tf2xla/type_util.h"
     17 #include "tensorflow/compiler/tf2xla/xla_compiler.h"
     18 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
     19 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
     20 #include "tensorflow/core/framework/kernel_def_builder.h"
     21 #include "tensorflow/core/framework/tensor.pb.h"
     22 
     23 namespace tensorflow {
     24 namespace {
     25 
     26 class ConstOp : public XlaOpKernel {
     27  public:
     28   explicit ConstOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {
     29     const TensorProto* proto = nullptr;
     30     OP_REQUIRES_OK(ctx, ctx->GetAttr("value", &proto));
     31     proto_ = *proto;
     32     OP_REQUIRES(
     33         ctx, ctx->output_type(0) == proto_.dtype(),
     34         errors::InvalidArgument("Type mismatch between value (",
     35                                 DataTypeString(proto_.dtype()), ") and dtype (",
     36                                 DataTypeString(ctx->output_type(0)), ")"));
     37     OP_REQUIRES_OK(ctx, TensorShape::IsValidShape(proto_.tensor_shape()));
     38   }
     39 
     40   void Compile(XlaOpKernelContext* ctx) override {
     41     TensorShape shape(proto_.tensor_shape());
     42 
     43     if (proto_.dtype() == DT_STRING) {
     44       LOG(WARNING) << "Not computing Const of type DT_STRING";
     45       ctx->SetInvalidOutput(0);
     46       return;
     47     }
     48     xla::ComputationBuilder* b = ctx->builder();
     49 
     50     // To avoid blowups for large constants filled with the same value,
     51     // recognize that case and emit a scalar broadcast instead.
     52     if (shape.num_elements() > 1) {
     53       switch (proto_.dtype()) {
     54         case DT_BOOL:
     55           if (proto_.bool_val_size() == 1) {
     56             ctx->SetOutput(0,
     57                            b->Broadcast(b->ConstantR0<bool>(proto_.bool_val(0)),
     58                                         shape.dim_sizes()));
     59             return;
     60           }
     61           break;
     62         case DT_FLOAT:
     63           if (proto_.float_val_size() == 1) {
     64             ctx->SetOutput(
     65                 0, b->Broadcast(b->ConstantR0<float>(proto_.float_val(0)),
     66                                 shape.dim_sizes()));
     67             return;
     68           }
     69           break;
     70         case DT_DOUBLE:
     71           if (proto_.double_val_size() == 1) {
     72             ctx->SetOutput(
     73                 0, b->Broadcast(b->ConstantR0<double>(proto_.double_val(0)),
     74                                 shape.dim_sizes()));
     75             return;
     76           }
     77           break;
     78         case DT_INT32:
     79           if (proto_.int_val_size() == 1) {
     80             ctx->SetOutput(0,
     81                            b->Broadcast(b->ConstantR0<int32>(proto_.int_val(0)),
     82                                         shape.dim_sizes()));
     83             return;
     84           }
     85           break;
     86         case DT_INT64:
     87           if (proto_.int64_val_size() == 1) {
     88             ctx->SetOutput(
     89                 0, b->Broadcast(b->ConstantR0<int64>(proto_.int64_val(0)),
     90                                 shape.dim_sizes()));
     91             return;
     92           }
     93           break;
     94         default:
     95           break;
     96       }
     97     }
     98 
     99     // General case
    100     Tensor tensor(proto_.dtype());
    101     OP_REQUIRES(ctx, tensor.FromProto(cpu_allocator(), proto_),
    102                 errors::InvalidArgument("Cannot parse tensor from proto: ",
    103                                         proto_.DebugString()));
    104     ctx->SetConstantOutput(0, tensor);
    105   }
    106 
    107  private:
    108   TensorProto proto_;
    109   TF_DISALLOW_COPY_AND_ASSIGN(ConstOp);
    110 };
    111 
    112 // XLA_* devices also register a "real" Const operator so we suppress the
    113 // dummy operator using CompilationOnly().
    114 REGISTER_XLA_OP(Name("Const").CompilationOnly(), ConstOp);
    115 
    116 }  // namespace
    117 }  // namespace tensorflow
    118