xref: /aosp_15_r20/external/tensorflow/tensorflow/core/kernels/uniform_quant_ops/tensor_utils.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_CORE_KERNELS_UNIFORM_QUANT_OPS_TENSOR_UTILS_H_
16 #define TENSORFLOW_CORE_KERNELS_UNIFORM_QUANT_OPS_TENSOR_UTILS_H_
17 
18 #include "tensorflow/core/framework/tensor.h"
19 
20 namespace tensorflow {
21 
22 // Returns if all elements in given tensors are positive.
23 template <typename T>
AllElementsPositive(const Tensor & tensor)24 bool AllElementsPositive(const Tensor& tensor) {
25   Eigen::Tensor<bool, 0, Eigen::RowMajor> positive =
26       (tensor.flat<T>() > 0).all();
27   return positive();
28 }
29 
30 // Given data tensor's shape and quantization params, returns if the shapes are
31 // valid.
32 Status QuantizationAxisAndShapeValid(const TensorShape& data_shape,
33                                      const TensorShape& scales_shape,
34                                      const TensorShape& zero_points_shape,
35                                      int quantization_axis);
36 
37 }  // namespace tensorflow
38 
39 #endif  // TENSORFLOW_CORE_KERNELS_UNIFORM_QUANT_OPS_TENSOR_UTILS_H_
40