aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/tf2xla/kernels/clip_by_value_op.cc
blob: 547fe48046e8c934e3bc14d02c8448e107c1a406 (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
/* 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/xla_op_kernel.h"
#include "tensorflow/compiler/tf2xla/xla_op_registry.h"
#include "tensorflow/compiler/xla/client/xla_builder.h"
#include "tensorflow/core/framework/tensor_shape.h"

namespace tensorflow {
namespace {

class ClipByValueOp : public XlaOpKernel {
 public:
  explicit ClipByValueOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}

  void Compile(XlaOpKernelContext* ctx) override {
    const TensorShape shape = ctx->InputShape(0);
    const TensorShape min_shape = ctx->InputShape(1);
    const TensorShape max_shape = ctx->InputShape(2);

    auto input = ctx->Input(0);
    auto min = ctx->Input(1);
    auto max = ctx->Input(2);

    auto shape_error = [&]() -> tensorflow::Status {
      return errors::InvalidArgument(
          "clip_value_min and clip_value_max must be either of "
          "the same shape as input, or a scalar. ",
          "Input shape: ", shape.DebugString(),
          " clip_value_min shape: ", min_shape.DebugString(),
          " clip_value_max shape: ", max_shape.DebugString());
    };

    if (shape != min_shape) {
      OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(min_shape), shape_error());
      min = xla::Broadcast(min, shape.dim_sizes());
    }
    if (shape != max_shape) {
      OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(max_shape), shape_error());
      max = xla::Broadcast(max, shape.dim_sizes());
    }
    ctx->SetOutput(0, xla::Clamp(min, input, max));
  }
};

REGISTER_XLA_OP(Name("ClipByValue"), ClipByValueOp);

}  // namespace
}  // namespace tensorflow