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/xla_context.h" 17 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h" 18 #include "tensorflow/compiler/tf2xla/xla_op_registry.h" 19 #include "tensorflow/compiler/xla/client/computation_builder.h" 20 #include "tensorflow/core/framework/kernel_def_builder.h" 21 #include "tensorflow/core/framework/op_kernel.h" 22 23 namespace tensorflow { 24 namespace { 25 26 // This TensorFlow op indicates that its input should be treated as a 27 // specific return value from a function. 28 class RetvalOp : public XlaOpKernel { 29 public: 30 explicit RetvalOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) { 31 OP_REQUIRES_OK(ctx, ctx->GetAttr("T", &dtype_)); 32 OP_REQUIRES_OK(ctx, ctx->GetAttr("index", &index_)); 33 } 34 35 void Compile(XlaOpKernelContext* ctx) override { 36 const Tensor& input = ctx->op_kernel_context()->input(0); 37 38 OP_REQUIRES(ctx, input.dtype() == dtype_, 39 errors::InvalidArgument( 40 "Type mismatch: actual ", DataTypeString(input.dtype()), 41 " vs. expect ", DataTypeString(dtype_))); 42 auto frame = ctx->call_frame(); 43 if (frame) { 44 // If 'frame' is non-null, this is an inner function call inside a JIT 45 // compilation. 46 OP_REQUIRES_OK(ctx, frame->SetRetval(index_, input)); 47 } else { 48 xla::ComputationDataHandle input = ctx->Input(0); 49 const TensorShape input_shape = ctx->InputShape(0); 50 51 auto is_constant = ctx->builder()->IsConstant(input); 52 if (!is_constant.ok()) { 53 ctx->SetStatus(is_constant.status()); 54 return; 55 } 56 57 XlaContext& tc = XlaContext::Get(ctx); 58 if (input_shape.num_elements() == 0 || is_constant.ValueOrDie()) { 59 xla::Literal literal; 60 OP_REQUIRES_OK(ctx, ctx->ConstantInput(0, &literal)); 61 OP_REQUIRES_OK(ctx, tc.AddConstRetval(index_, dtype_, literal)); 62 } else { 63 // The core from which a return value is returned depends on the core 64 // assignment of the input to the retval .Since we can't change the core 65 // assignment of <input> as this point, create a tuple/get-tuple-element 66 // combination so that the core will be set on them. 67 auto tuple_elem = 68 ctx->builder()->GetTupleElement(ctx->builder()->Tuple({input}), 0); 69 tc.AddRetval(index_, dtype_, tuple_elem); 70 } 71 } 72 } 73 74 private: 75 // The index of this return value in the returned tuple. 76 int index_; 77 DataType dtype_; 78 79 TF_DISALLOW_COPY_AND_ASSIGN(RetvalOp); 80 }; 81 82 REGISTER_XLA_OP(Name("_Retval"), RetvalOp); 83 84 } // anonymous namespace 85 } // namespace tensorflow 86