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