• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 <algorithm>
17 
18 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
19 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
20 #include "tensorflow/compiler/xla/client/lib/arithmetic.h"
21 #include "tensorflow/compiler/xla/client/xla_builder.h"
22 #include "tensorflow/core/framework/op_kernel.h"
23 
24 namespace tensorflow {
25 namespace {
26 
27 class BucketizeOp : public XlaOpKernel {
28  public:
BucketizeOp(OpKernelConstruction * context)29   explicit BucketizeOp(OpKernelConstruction* context) : XlaOpKernel(context) {
30     OP_REQUIRES_OK(context, context->GetAttr("boundaries", &boundaries_));
31     OP_REQUIRES(context, std::is_sorted(boundaries_.begin(), boundaries_.end()),
32                 errors::InvalidArgument("Expected sorted boundaries"));
33   }
34 
Compile(XlaOpKernelContext * context)35   void Compile(XlaOpKernelContext* context) override {
36     xla::XlaBuilder* builder = context->builder();
37     const DataType dtype = context->input_type(0);
38     xla::XlaOp input = context->Input(0);
39 
40     xla::XlaOp boundaries = xla::ConstantR1<float>(builder, boundaries_);
41     // TODO(phawkins): the following behavior matches the behavior of the core
42     // Bucketize kernel. However, comparing an int32 or int64 against float may
43     // lead to inaccurate bucketing due to rounding.
44     if (dtype == DT_DOUBLE) {
45       input = xla::ConvertElementType(input, xla::F64);
46       boundaries = xla::ConvertElementType(boundaries, xla::F64);
47     } else {
48       input = xla::ConvertElementType(input, xla::F32);
49     }
50     xla::XlaOp comparison =
51         xla::ConvertElementType(xla::Ge(xla::Broadcast(input, {1}), boundaries,
52                                         /*broadcast_dimensions=*/{0}),
53                                 xla::S32);
54     xla::XlaOp buckets = xla::Reduce(
55         comparison, /*init_value=*/xla::ConstantR0<int32>(builder, 0),
56         /*computation=*/xla::CreateScalarAddComputation(xla::S32, builder),
57         /*dimensions_to_reduce=*/{0});
58     context->SetOutput(0, buckets);
59   }
60 
61  private:
62   std::vector<float> boundaries_;
63 };
64 
65 REGISTER_XLA_OP(Name("Bucketize"), BucketizeOp);
66 
67 }  // namespace
68 }  // namespace tensorflow
69