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/shape_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/compiler/xla/client/computation_builder.h"
     22 #include "tensorflow/compiler/xla/xla_data.pb.h"
     23 #include "tensorflow/core/framework/kernel_def_builder.h"
     24 #include "tensorflow/core/framework/types.h"
     25 
     26 namespace tensorflow {
     27 namespace {
     28 
     29 class SendOp : public XlaOpKernel {
     30  public:
     31   explicit SendOp(OpKernelConstruction* ctx);
     32   void Compile(XlaOpKernelContext* ctx) override;
     33 
     34  private:
     35   string tensor_name_;
     36 
     37   TF_DISALLOW_COPY_AND_ASSIGN(SendOp);
     38 };
     39 
     40 SendOp::SendOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {
     41   OP_REQUIRES_OK(ctx, ctx->GetAttr("tensor_name", &tensor_name_));
     42 }
     43 
     44 void SendOp::Compile(XlaOpKernelContext* ctx) {
     45   XlaCompiler* compiler = XlaContext::Get(ctx).compiler();
     46   xla::ChannelHandle channel;
     47   OP_REQUIRES_OK(ctx, compiler->GetChannelHandle(tensor_name_, &channel));
     48   ctx->builder()->Send(ctx->Input(0), channel);
     49 }
     50 
     51 REGISTER_XLA_OP(Name("_XLASend"), SendOp);
     52 
     53 class RecvOp : public XlaOpKernel {
     54  public:
     55   explicit RecvOp(OpKernelConstruction* ctx);
     56   void Compile(XlaOpKernelContext* ctx) override;
     57 
     58  private:
     59   string tensor_name_;
     60   xla::Shape shape_;
     61 
     62   TF_DISALLOW_COPY_AND_ASSIGN(RecvOp);
     63 };
     64 
     65 RecvOp::RecvOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {
     66   OP_REQUIRES_OK(ctx, ctx->GetAttr("tensor_name", &tensor_name_));
     67 
     68   TensorShape tensor_shape;
     69   DataType dtype;
     70   OP_REQUIRES_OK(ctx, ctx->GetAttr("shape", &tensor_shape));
     71   OP_REQUIRES_OK(ctx, ctx->GetAttr("T", &dtype));
     72   OP_REQUIRES_OK(ctx, TensorShapeToXLAShape(dtype, tensor_shape, &shape_));
     73 }
     74 
     75 void RecvOp::Compile(XlaOpKernelContext* ctx) {
     76   XlaCompiler* compiler = XlaContext::Get(ctx).compiler();
     77   xla::ChannelHandle channel;
     78   OP_REQUIRES_OK(ctx, compiler->GetChannelHandle(tensor_name_, &channel));
     79   ctx->SetOutput(0, ctx->builder()->Recv(shape_, channel));
     80 }
     81 
     82 REGISTER_XLA_OP(Name("_XLARecv"), RecvOp);
     83 
     84 }  // namespace
     85 }  // namespace tensorflow
     86