1 /* Copyright 2022 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 #include "tensorflow/core/framework/common_shape_fns.h"
16 #include "tensorflow/core/framework/op.h"
17 #include "tensorflow/core/framework/shape_inference.h"
18 #include "tensorflow/core/platform/errors.h"
19 #include "tensorflow/core/platform/status.h"
20
21 namespace tensorflow {
22 namespace {
23
24 using shape_inference::DimensionHandle;
25 using shape_inference::ShapeHandle;
26 using tensorflow::errors::InvalidArgument;
27
ScalesZeroPointsShapeValid(shape_inference::InferenceContext * context,DimensionHandle match_dimension_handle,ShapeHandle scales,ShapeHandle zero_points)28 Status ScalesZeroPointsShapeValid(shape_inference::InferenceContext* context,
29 DimensionHandle match_dimension_handle,
30 ShapeHandle scales, ShapeHandle zero_points) {
31 const int32_t scales_rank = shape_inference::InferenceContext::Rank(scales);
32 const int32_t zero_points_rank =
33 shape_inference::InferenceContext::Rank(zero_points);
34 // Skip validation when rank is unknown.
35 if (scales_rank == shape_inference::InferenceContext::kUnknownRank ||
36 zero_points_rank == shape_inference::InferenceContext::kUnknownRank) {
37 return Status::OK();
38 }
39
40 if (scales_rank != zero_points_rank) {
41 return InvalidArgument("scales and zero_points must have same rank.");
42 }
43 if (scales_rank == 0) {
44 return Status::OK();
45 }
46 DimensionHandle scales_size = context->Dim(scales, 0);
47 DimensionHandle zero_points_size = context->Dim(zero_points, 0);
48 DimensionHandle merged_scales;
49 TF_RETURN_IF_ERROR(
50 context->Merge(scales_size, match_dimension_handle, &merged_scales));
51 DimensionHandle merged_zero_points;
52 TF_RETURN_IF_ERROR(context->Merge(zero_points_size, match_dimension_handle,
53 &merged_zero_points));
54 return Status::OK();
55 }
56
DotHybridShape(shape_inference::InferenceContext * context)57 Status DotHybridShape(shape_inference::InferenceContext* context) {
58 ShapeHandle lhs;
59 TF_RETURN_IF_ERROR(context->WithRank(context->input(0), 2, &lhs));
60 ShapeHandle rhs;
61 TF_RETURN_IF_ERROR(context->WithRank(context->input(1), 2, &rhs));
62 ShapeHandle rhs_scales;
63 TF_RETURN_IF_ERROR(
64 context->WithRankAtMost(context->input(2), 1, &rhs_scales));
65 ShapeHandle rhs_zero_points;
66 TF_RETURN_IF_ERROR(
67 context->WithRankAtMost(context->input(3), 1, &rhs_zero_points));
68
69 // Validate that the inner shapes are compatible.
70 DimensionHandle inner_lhs = context->Dim(lhs, 1);
71 DimensionHandle inner_rhs = context->Dim(rhs, 0);
72 DimensionHandle merged;
73 TF_RETURN_IF_ERROR(context->Merge(inner_lhs, inner_rhs, &merged));
74
75 DimensionHandle output_rows = context->Dim(lhs, 0);
76 DimensionHandle output_cols = context->Dim(rhs, 1);
77
78 TF_RETURN_IF_ERROR(ScalesZeroPointsShapeValid(context, output_cols,
79 rhs_scales, rhs_zero_points));
80
81 context->set_output(0, context->Matrix(output_rows, output_cols));
82 return OkStatus();
83 }
84
85 } // namespace
86
87 REGISTER_OP("UniformQuantize")
88 .Input("input: Tin")
89 .Input("scales: float")
90 .Input("zero_points: int32")
91 .Output("output: Tout")
92 .Attr("Tin: {float}")
93 .Attr("Tout: {qint8, qint32}")
94 .Attr("quantization_axis: int = -1")
95 .Attr("quantization_min_val: int")
96 .Attr("quantization_max_val: int")
97 .SetShapeFn(shape_inference::UnchangedShape);
98
99 REGISTER_OP("UniformRequantize")
100 .Input("input: Tin")
101 .Input("input_scales: float")
102 .Input("input_zero_points: int32")
103 .Input("output_scales: float")
104 .Input("output_zero_points: int32")
105 .Output("output: Tout")
106 .Attr("Tin: {qint8, qint32}")
107 .Attr("Tout: {qint8, qint32}")
108 .Attr("input_quantization_axis: int = -1")
109 .Attr("input_quantization_min_val: int")
110 .Attr("input_quantization_max_val: int")
111 .Attr("output_quantization_axis: int = -1")
112 .Attr("output_quantization_min_val: int")
113 .Attr("output_quantization_max_val: int")
114 .SetShapeFn(shape_inference::UnchangedShape);
115
116 REGISTER_OP("UniformDequantize")
117 .Input("input: Tin")
118 .Input("scales: float")
119 .Input("zero_points: int32")
120 .Output("output: Tout")
121 .Attr("Tin: {qint8, qint32}")
122 .Attr("Tout: {float}")
123 .Attr("quantization_axis: int = -1")
124 .Attr("quantization_min_val: int")
125 .Attr("quantization_max_val: int")
126 .SetShapeFn(shape_inference::UnchangedShape);
127
128 REGISTER_OP("UniformQuantizedDotHybrid")
129 .Input("lhs: Tlhs")
130 .Input("rhs: Trhs")
131 .Input("rhs_scales: float")
132 .Input("rhs_zero_points: int32")
133 .Output("output: Tout")
134 .Attr("Tlhs: {float}")
135 .Attr("Trhs: {qint8}")
136 .Attr("Tout: {float}")
137 .Attr("rhs_quantization_axis: int = -1")
138 .Attr("rhs_quantization_min_val: int")
139 .Attr("rhs_quantization_max_val: int")
140 .SetShapeFn(DotHybridShape);
141
142 } // namespace tensorflow
143