aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/tf2xla/kernels/xla_select_and_scatter_op.cc
blob: 089776fcf74fcf6b363dfff5de8d86d7449eacd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/tf2xla/kernels/while_op.h"

#include "tensorflow/compiler/tf2xla/shape_util.h"
#include "tensorflow/compiler/tf2xla/xla_compiler.h"
#include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
#include "tensorflow/compiler/tf2xla/xla_op_registry.h"
#include "tensorflow/compiler/xla/client/xla_builder.h"
#include "tensorflow/compiler/xla/client/xla_computation.h"
#include "tensorflow/core/framework/function.h"
#include "tensorflow/core/framework/op_kernel.h"

namespace tensorflow {
namespace {

class XlaSelectAndScatterOp : public XlaOpKernel {
 public:
  explicit XlaSelectAndScatterOp(OpKernelConstruction* context)
      : XlaOpKernel(context) {
    OP_REQUIRES_OK(context, context->GetAttr("select", &select_computation_));
    OP_REQUIRES_OK(context, context->GetAttr("scatter", &scatter_computation_));
  }

  void Compile(XlaOpKernelContext* context) override {
    const TensorShape input_shape = context->InputShape(0);
    const DataType dtype = context->input_type(0);

    std::vector<int64> window_dimensions;
    std::vector<int64> window_strides;
    OP_REQUIRES_OK(context, context->ConstantInputAsIntVector(
                                "window_dimensions", &window_dimensions));
    OP_REQUIRES_OK(context, context->ConstantInputAsIntVector("window_strides",
                                                              &window_strides));

    const int rank = input_shape.dims();
    OP_REQUIRES(context, rank == window_dimensions.size(),
                errors::InvalidArgument(
                    "The size of window_dimensions must be equal to the input "
                    "rank (",
                    window_dimensions.size(), " vs. ", rank, ")"));
    OP_REQUIRES(context, rank == window_strides.size(),
                errors::InvalidArgument(
                    "The size of window_strides must be equal to the input "
                    "rank (",
                    window_strides.size(), " vs. ", rank, ")"));

    XlaCompiler::CompileOptions compile_options;
    compile_options.use_tuple_arg = false;
    compile_options.resolve_compile_time_constants = false;
    compile_options.is_entry_computation = false;
    compile_options.always_return_tuple = false;

    // Build the select function.
    XlaCompiler::Argument select_arg;
    select_arg.kind = XlaCompiler::Argument::kParameter;
    select_arg.type = dtype;
    select_arg.shape = TensorShape();

    XlaCompiler::CompilationResult select;
    OP_REQUIRES_OK(context, context->compiler()->CompileFunction(
                                compile_options, *select_computation_,
                                {select_arg, select_arg}, &select));

    xla::Shape select_output_shape = xla::ShapeUtil::MakeShape(xla::PRED, {});
    OP_REQUIRES(
        context,
        xla::ShapeUtil::Compatible(select.xla_output_shape,
                                   select_output_shape),
        errors::InvalidArgument(
            "Invalid output shape of XlaSelectAndScatter select. Expected ",
            xla::ShapeUtil::HumanString(select_output_shape), " got ",
            xla::ShapeUtil::HumanString(select.xla_output_shape)));

    // Build the scatter function.
    XlaCompiler::Argument scatter_arg;
    scatter_arg.kind = XlaCompiler::Argument::kParameter;
    scatter_arg.type = dtype;
    scatter_arg.shape = TensorShape();

    XlaCompiler::CompilationResult scatter;
    OP_REQUIRES_OK(context, context->compiler()->CompileFunction(
                                compile_options, *scatter_computation_,
                                {scatter_arg, scatter_arg}, &scatter));

    xla::Shape scalar_shape;
    OP_REQUIRES_OK(context,
                   TensorShapeToXLAShape(dtype, TensorShape(), &scalar_shape));
    OP_REQUIRES(
        context,
        xla::ShapeUtil::Compatible(scatter.xla_output_shape, scalar_shape),
        errors::InvalidArgument(
            "Invalid output shape of scatter. Expected ",
            xla::ShapeUtil::HumanString(scalar_shape), " got ",
            xla::ShapeUtil::HumanString(scatter.xla_output_shape)));

    const TensorShape padding_shape = context->InputShape("padding");
    OP_REQUIRES(context,
                TensorShapeUtils::IsMatrix(padding_shape) &&
                    padding_shape.dim_size(1) == 2,
                errors::InvalidArgument(
                    "padding must be a matrix with minor dimension 2, got ",
                    padding_shape.DebugString()));
    xla::Literal padding_literal;
    OP_REQUIRES_OK(context, context->ConstantInputAsInt64Literal(
                                "padding", &padding_literal));
    std::vector<std::pair<int64, int64>> padding(padding_shape.dim_size(0));
    for (int i = 0; i < padding.size(); ++i) {
      padding[i] = {padding_literal.Get<int64>({i, 0}),
                    padding_literal.Get<int64>({i, 1})};
    }

    xla::XlaOp output = xla::SelectAndScatterWithGeneralPadding(
        context->Input("operand"), *select.computation, window_dimensions,
        window_strides, padding, context->Input("source"),
        context->Input("init_value"), *scatter.computation);
    context->SetOutput(0, output);
  }

 private:
  const NameAttrList* select_computation_;
  const NameAttrList* scatter_computation_;

  TF_DISALLOW_COPY_AND_ASSIGN(XlaSelectAndScatterOp);
};

REGISTER_XLA_OP(Name("XlaSelectAndScatter")
                    .CompileTimeConstInput("window_dimensions")
                    .CompileTimeConstInput("window_strides")
                    .CompileTimeConstInput("padding"),
                XlaSelectAndScatterOp);

}  // namespace
}  // namespace tensorflow