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_helpers.h"
     19 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
     20 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
     21 #include "tensorflow/core/framework/kernel_def_builder.h"
     22 
     23 namespace tensorflow {
     24 
     25 // This OpKernel implements the _Arg Op for XLA JIT devices. It
     26 // associates its output with one of the arguments to a
     27 // subcomputation.
     28 class XlaArgOp : public XlaOpKernel {
     29  public:
     30   explicit XlaArgOp(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     // If 'frame' is non-null, this is a function call inside an outer JIT
     37     // compilation. Use the usual implementation of _Arg.
     38     auto frame = ctx->call_frame();
     39     if (frame != nullptr) {
     40       Tensor val;
     41       OP_REQUIRES_OK(ctx, frame->GetArg(index_, &val));
     42       OP_REQUIRES(ctx, val.dtype() == dtype_,
     43                   errors::InvalidArgument(
     44                       "Type mismatch: actual ", DataTypeString(val.dtype()),
     45                       " vs. expect ", DataTypeString(dtype_)));
     46       // Forwards the argument from the frame.
     47       ctx->op_kernel_context()->set_output(0, val);
     48       return;
     49     }
     50 
     51     const XlaExpression& arg = XlaContext::Get(ctx).args()[index_];
     52     if (arg.resource() != nullptr) {
     53       ctx->SetResourceOutput(0, arg.resource());
     54     } else if (arg.has_constant_value()) {
     55       ctx->SetConstantOutput(0, arg.constant_value());
     56     } else {
     57       ctx->SetOutput(0, arg.handle());
     58     }
     59   }
     60 
     61  private:
     62   int index_;
     63   DataType dtype_;
     64 
     65   TF_DISALLOW_COPY_AND_ASSIGN(XlaArgOp);
     66 };
     67 
     68 REGISTER_XLA_OP(Name("_Arg").AllowResourceTypes(), XlaArgOp);
     69 
     70 }  // namespace tensorflow
     71