/* Copyright 2019 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 #include #include #include "absl/status/status.h" #include "absl/strings/str_cat.h" #include "tensorflow/compiler/tf2xla/xla_helpers.h" #include "tensorflow/compiler/tf2xla/xla_op_kernel.h" #include "tensorflow/compiler/tf2xla/xla_op_registry.h" #include "xla/hlo/builder/lib/constants.h" #include "xla/hlo/builder/lib/matrix.h" #include "xla/hlo/builder/xla_builder.h" #include "xla/xla_data.pb.h" #include "tensorflow/core/framework/numeric_types.h" #include "tensorflow/core/framework/op_kernel.h" #include "tensorflow/core/framework/types.pb.h" namespace tensorflow { namespace { // TODO(mingyao|ylc): Support 16bits and 32 bits. constexpr std::array kQuantizedType = {{DT_QINT8, DT_QUINT8}}; template float get_fullrange() { return static_cast(std::numeric_limits::max()) - std::numeric_limits::min(); } class DequantizeOp : public XlaOpKernel { public: explicit DequantizeOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) { std::string mode_string; int axis; bool narrow_range; OP_REQUIRES_OK(ctx, ctx->GetAttr("mode", &mode_string)); OP_REQUIRES( ctx, (mode_string == "MIN_COMBINED"), absl::InvalidArgumentError("Mode string must be 'MIN_COMBINED' is " + mode_string + "'")); OP_REQUIRES_OK(ctx, ctx->GetAttr("narrow_range", &narrow_range)); OP_REQUIRES(ctx, narrow_range == false, absl::InvalidArgumentError("narrow_range must be false")); OP_REQUIRES_OK(ctx, ctx->GetAttr("axis", &axis)); OP_REQUIRES( ctx, axis == -1, absl::InvalidArgumentError(absl::StrCat("axis must be -1' is ", axis))); OP_REQUIRES_OK(ctx, ctx->GetAttr("dtype", &dtype_)); } ~DequantizeOp() override = default; void Compile(XlaOpKernelContext* ctx) override { DataType input_type = ctx->input_type(0); xla::XlaOp input = ctx->Input(0); xla::XlaOp output = xla::ConvertElementType(input, xla::F32); xla::XlaOp min_range = xla::ConvertElementType(ctx->Input(1), xla::F32); xla::XlaOp max_range = xla::ConvertElementType(ctx->Input(2), xla::F32); xla::XlaOp full_range; xla::XlaOp half_range; if (input_type == DT_QINT8) { full_range = ScalarLike(output, get_fullrange()); half_range = (full_range + ScalarLike(output, 1.0f)) / ScalarLike(output, 2.0f); } else { OP_REQUIRES(ctx, input_type == DT_QUINT8, absl::InvalidArgumentError(absl::StrCat( "Only support DT_QINT8 or DT_QUINT8, got ", input_type))); full_range = ScalarLike(output, get_fullrange()); half_range = ScalarLike(output, 0.0f); } xla::XlaOp scale = (max_range - min_range) / full_range; output = xla::Add(xla::Mul(xla::Add(output, half_range), scale), min_range); if (dtype_ == DT_BFLOAT16) { output = xla::ConvertElementType(output, xla::BF16); } ctx->SetOutput(0, output); } private: DataType dtype_; }; REGISTER_XLA_OP(Name("Dequantize").TypeConstraint("T", kQuantizedType), DequantizeOp); } // namespace } // namespace tensorflow