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_helpers.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/node_def.pb.h" 22 #include "tensorflow/core/kernels/partitioned_function_ops.h" 23 24 namespace tensorflow { 25 namespace { 26 27 const char* const kGradientOp = "SymbolicGradient"; 28 29 // Implementations of _ListToArray and _ArrayToList for functions. 30 class PassOn : public XlaOpKernel { 31 public: PassOn(OpKernelConstruction * ctx)32 explicit PassOn(OpKernelConstruction* ctx) : XlaOpKernel(ctx) { 33 OP_REQUIRES(ctx, ctx->num_inputs() == ctx->num_outputs(), 34 errors::Internal("#inputs != #outputs : ", ctx->num_inputs(), 35 " vs. ", ctx->num_outputs())); 36 for (int i = 0; i < ctx->num_inputs(); ++i) { 37 OP_REQUIRES( 38 ctx, input_type(i) == output_type(i), 39 errors::Internal("Input and output types for position ", i, 40 " do not match: ", DataTypeString(input_type(i)), 41 " vs. ", DataTypeString(output_type(i)))); 42 } 43 } 44 Compile(XlaOpKernelContext * ctx)45 void Compile(XlaOpKernelContext* ctx) override { 46 for (int i = 0; i < ctx->num_inputs(); ++i) { 47 ctx->SetOutput(i, ctx->Input(i)); 48 } 49 } 50 }; 51 52 REGISTER_XLA_OP(Name("_ListToArray"), PassOn); 53 REGISTER_XLA_OP(Name("_ArrayToList"), PassOn); 54 55 // TODO(phawkins): this is an almost exact copy of the SymbolicGradientOp 56 // implementation from regular Tensorflow. Once XLA has been open sourced 57 // merge the two implementations. (Note: this implementation propagates the 58 // step_resource_manager). 59 class SymbolicGradientOp : public AsyncOpKernel { 60 public: SymbolicGradientOp(OpKernelConstruction * ctx)61 explicit SymbolicGradientOp(OpKernelConstruction* ctx) 62 : AsyncOpKernel(ctx), handle_(-1) {} 63 ~SymbolicGradientOp()64 ~SymbolicGradientOp() override {} 65 ComputeAsync(OpKernelContext * ctx,DoneCallback done)66 void ComputeAsync(OpKernelContext* ctx, DoneCallback done) override { 67 FunctionLibraryRuntime* lib = ctx->function_library(); 68 OP_REQUIRES_ASYNC(ctx, lib != nullptr, 69 errors::Internal("No function library is provided."), 70 done); 71 72 OP_REQUIRES_OK_ASYNC( 73 ctx, lib->Instantiate(kGradientOp, AttrSlice(&def().attr()), &handle_), 74 done); 75 76 FunctionLibraryRuntime::Options opts; 77 opts.step_id = ctx->step_id(); 78 opts.runner = ctx->runner(); 79 opts.step_container = ctx->step_container(); 80 std::vector<Tensor> args; 81 args.reserve(ctx->num_inputs()); 82 for (int i = 0; i < ctx->num_inputs(); ++i) { 83 args.push_back(ctx->input(i)); 84 } 85 std::vector<Tensor>* rets = new std::vector<Tensor>; 86 lib->Run( 87 opts, handle_, args, rets, [ctx, done, rets](const Status& status) { 88 if (!status.ok()) { 89 ctx->SetStatus(status); 90 } else if (rets->size() != ctx->num_outputs()) { 91 ctx->SetStatus(errors::InvalidArgument( 92 "SymGrad expects to return ", ctx->num_outputs(), 93 " tensor(s), but get ", rets->size(), " tensor(s) instead.")); 94 } else { 95 for (size_t i = 0; i < rets->size(); ++i) { 96 ctx->set_output(i, (*rets)[i]); 97 } 98 } 99 delete rets; 100 done(); 101 }); 102 } 103 104 private: 105 FunctionLibraryRuntime::Handle handle_; 106 107 TF_DISALLOW_COPY_AND_ASSIGN(SymbolicGradientOp); 108 }; 109 110 REGISTER_XLA_OP(Name(kGradientOp), SymbolicGradientOp); 111 REGISTER_XLA_OP(Name("PartitionedCall").AllowResourceTypes(), 112 PartitionedCallOp); 113 REGISTER_XLA_OP(Name("StatefulPartitionedCall").AllowResourceTypes(), 114 PartitionedCallOp); 115 116 } // namespace 117 } // namespace tensorflow 118