comparisons.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "tensorflow/lite/kernels/internal/reference/comparisons.h"
  13. #include "tensorflow/lite/c/common.h"
  14. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  15. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  16. #include "tensorflow/lite/kernels/kernel_util.h"
  17. namespace tflite {
  18. namespace ops {
  19. namespace micro {
  20. namespace comparisons {
  21. namespace {
  22. constexpr int kInputTensor1 = 0;
  23. constexpr int kInputTensor2 = 1;
  24. constexpr int kOutputTensor = 0;
  25. // TODO(ruic): optimize macros below to using template functions.
  26. #define TF_LITE_QUANTIZE_COMPARISON(opname) \
  27. template <typename input_dtype> \
  28. void EvalQuantized##opname(TfLiteContext* context, TfLiteNode* node, \
  29. const TfLiteTensor* input1, \
  30. const TfLiteTensor* input2, TfLiteTensor* output, \
  31. bool requires_broadcast) { \
  32. if (input1->type == kTfLiteUInt8 || input1->type == kTfLiteInt8) { \
  33. auto input1_offset = -input1->params.zero_point; \
  34. auto input2_offset = -input2->params.zero_point; \
  35. const int left_shift = 8; \
  36. \
  37. int32 input1_multiplier; \
  38. int input1_shift; \
  39. QuantizeMultiplierSmallerThanOneExp( \
  40. static_cast<double>(input1->params.scale), &input1_multiplier, \
  41. &input1_shift); \
  42. int32 input2_multiplier; \
  43. int input2_shift; \
  44. QuantizeMultiplierSmallerThanOneExp( \
  45. static_cast<double>(input2->params.scale), &input2_multiplier, \
  46. &input2_shift); \
  47. \
  48. ComparisonParams op_params; \
  49. op_params.left_shift = left_shift; \
  50. op_params.input1_offset = input1_offset; \
  51. op_params.input1_multiplier = input1_multiplier; \
  52. op_params.input1_shift = input1_shift; \
  53. op_params.input2_offset = input2_offset; \
  54. op_params.input2_multiplier = input2_multiplier; \
  55. op_params.input2_shift = input2_shift; \
  56. if (requires_broadcast) { \
  57. reference_ops::Broadcast4DSlow##opname##WithScaling( \
  58. op_params, GetTensorShape(input1), \
  59. GetTensorData<input_dtype>(input1), GetTensorShape(input2), \
  60. GetTensorData<input_dtype>(input2), GetTensorShape(output), \
  61. GetTensorData<bool>(output)); \
  62. } else { \
  63. reference_ops::opname##WithScaling( \
  64. op_params, GetTensorShape(input1), \
  65. GetTensorData<input_dtype>(input1), GetTensorShape(input2), \
  66. GetTensorData<input_dtype>(input2), GetTensorShape(output), \
  67. GetTensorData<bool>(output)); \
  68. } \
  69. } \
  70. }
  71. TF_LITE_QUANTIZE_COMPARISON(Equal);
  72. TF_LITE_QUANTIZE_COMPARISON(NotEqual);
  73. TF_LITE_QUANTIZE_COMPARISON(Greater);
  74. TF_LITE_QUANTIZE_COMPARISON(GreaterEqual);
  75. TF_LITE_QUANTIZE_COMPARISON(Less);
  76. TF_LITE_QUANTIZE_COMPARISON(LessEqual);
  77. #undef TF_LITE_QUANTIZE_COMPARISON
  78. #define TF_LITE_COMPARISON(type, opname, requires_broadcast) \
  79. { \
  80. ComparisonParams op_params; \
  81. requires_broadcast \
  82. ? reference_ops::Broadcast4DSlow##opname##NoScaling( \
  83. op_params, GetTensorShape(input1), GetTensorData<type>(input1), \
  84. GetTensorShape(input2), GetTensorData<type>(input2), \
  85. GetTensorShape(output), GetTensorData<bool>(output)) \
  86. : reference_ops::opname##NoScaling( \
  87. op_params, GetTensorShape(input1), GetTensorData<type>(input1), \
  88. GetTensorShape(input2), GetTensorData<type>(input2), \
  89. GetTensorShape(output), GetTensorData<bool>(output)); \
  90. }
  91. TfLiteStatus EqualEval(TfLiteContext* context, TfLiteNode* node) {
  92. const TfLiteTensor* input1 = GetInput(context, node, kInputTensor1);
  93. const TfLiteTensor* input2 = GetInput(context, node, kInputTensor2);
  94. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  95. bool requires_broadcast = !HaveSameShapes(input1, input2);
  96. switch (input1->type) {
  97. case kTfLiteBool:
  98. TF_LITE_COMPARISON(bool, Equal, requires_broadcast);
  99. break;
  100. case kTfLiteFloat32:
  101. TF_LITE_COMPARISON(float, Equal, requires_broadcast);
  102. break;
  103. case kTfLiteInt32:
  104. TF_LITE_COMPARISON(int32_t, Equal, requires_broadcast);
  105. break;
  106. case kTfLiteInt64:
  107. TF_LITE_COMPARISON(int64_t, Equal, requires_broadcast);
  108. break;
  109. case kTfLiteUInt8:
  110. EvalQuantizedEqual<uint8_t>(context, node, input1, input2, output,
  111. requires_broadcast);
  112. break;
  113. case kTfLiteInt8:
  114. EvalQuantizedEqual<int8_t>(context, node, input1, input2, output,
  115. requires_broadcast);
  116. break;
  117. default:
  118. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  119. TfLiteTypeGetName(input1->type), input1->type);
  120. return kTfLiteError;
  121. }
  122. return kTfLiteOk;
  123. }
  124. // TODO(renjieliu): Refactor the logic to avoid duplications.
  125. TfLiteStatus NotEqualEval(TfLiteContext* context, TfLiteNode* node) {
  126. const TfLiteTensor* input1 = GetInput(context, node, kInputTensor1);
  127. const TfLiteTensor* input2 = GetInput(context, node, kInputTensor2);
  128. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  129. bool requires_broadcast = !HaveSameShapes(input1, input2);
  130. switch (input1->type) {
  131. case kTfLiteBool:
  132. TF_LITE_COMPARISON(bool, NotEqual, requires_broadcast);
  133. break;
  134. case kTfLiteFloat32:
  135. TF_LITE_COMPARISON(float, NotEqual, requires_broadcast);
  136. break;
  137. case kTfLiteInt32:
  138. TF_LITE_COMPARISON(int32_t, NotEqual, requires_broadcast);
  139. break;
  140. case kTfLiteInt64:
  141. TF_LITE_COMPARISON(int64_t, NotEqual, requires_broadcast);
  142. break;
  143. case kTfLiteUInt8:
  144. EvalQuantizedNotEqual<uint8_t>(context, node, input1, input2, output,
  145. requires_broadcast);
  146. break;
  147. case kTfLiteInt8:
  148. EvalQuantizedNotEqual<int8_t>(context, node, input1, input2, output,
  149. requires_broadcast);
  150. break;
  151. default:
  152. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  153. TfLiteTypeGetName(input1->type), input1->type);
  154. return kTfLiteError;
  155. }
  156. return kTfLiteOk;
  157. }
  158. TfLiteStatus GreaterEval(TfLiteContext* context, TfLiteNode* node) {
  159. const TfLiteTensor* input1 = GetInput(context, node, kInputTensor1);
  160. const TfLiteTensor* input2 = GetInput(context, node, kInputTensor2);
  161. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  162. bool requires_broadcast = !HaveSameShapes(input1, input2);
  163. switch (input1->type) {
  164. case kTfLiteFloat32:
  165. TF_LITE_COMPARISON(float, Greater, requires_broadcast);
  166. break;
  167. case kTfLiteInt32:
  168. TF_LITE_COMPARISON(int32_t, Greater, requires_broadcast);
  169. break;
  170. case kTfLiteInt64:
  171. TF_LITE_COMPARISON(int64_t, Greater, requires_broadcast);
  172. break;
  173. case kTfLiteUInt8:
  174. EvalQuantizedGreater<uint8_t>(context, node, input1, input2, output,
  175. requires_broadcast);
  176. break;
  177. case kTfLiteInt8:
  178. EvalQuantizedGreater<int8_t>(context, node, input1, input2, output,
  179. requires_broadcast);
  180. break;
  181. default:
  182. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  183. TfLiteTypeGetName(input1->type), input1->type);
  184. return kTfLiteError;
  185. }
  186. return kTfLiteOk;
  187. }
  188. TfLiteStatus GreaterEqualEval(TfLiteContext* context, TfLiteNode* node) {
  189. const TfLiteTensor* input1 = GetInput(context, node, kInputTensor1);
  190. const TfLiteTensor* input2 = GetInput(context, node, kInputTensor2);
  191. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  192. bool requires_broadcast = !HaveSameShapes(input1, input2);
  193. switch (input1->type) {
  194. case kTfLiteFloat32:
  195. TF_LITE_COMPARISON(float, GreaterEqual, requires_broadcast);
  196. break;
  197. case kTfLiteInt32:
  198. TF_LITE_COMPARISON(int32_t, GreaterEqual, requires_broadcast);
  199. break;
  200. case kTfLiteInt64:
  201. TF_LITE_COMPARISON(int64_t, GreaterEqual, requires_broadcast);
  202. break;
  203. case kTfLiteUInt8:
  204. EvalQuantizedGreaterEqual<uint8_t>(context, node, input1, input2, output,
  205. requires_broadcast);
  206. break;
  207. case kTfLiteInt8:
  208. EvalQuantizedGreaterEqual<int8_t>(context, node, input1, input2, output,
  209. requires_broadcast);
  210. break;
  211. default:
  212. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  213. TfLiteTypeGetName(input1->type), input1->type);
  214. return kTfLiteError;
  215. }
  216. return kTfLiteOk;
  217. }
  218. TfLiteStatus LessEval(TfLiteContext* context, TfLiteNode* node) {
  219. const TfLiteTensor* input1 = GetInput(context, node, kInputTensor1);
  220. const TfLiteTensor* input2 = GetInput(context, node, kInputTensor2);
  221. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  222. bool requires_broadcast = !HaveSameShapes(input1, input2);
  223. switch (input1->type) {
  224. case kTfLiteFloat32:
  225. TF_LITE_COMPARISON(float, Less, requires_broadcast);
  226. break;
  227. case kTfLiteInt32:
  228. TF_LITE_COMPARISON(int32_t, Less, requires_broadcast);
  229. break;
  230. case kTfLiteInt64:
  231. TF_LITE_COMPARISON(int64_t, Less, requires_broadcast);
  232. break;
  233. case kTfLiteUInt8:
  234. EvalQuantizedLess<uint8_t>(context, node, input1, input2, output,
  235. requires_broadcast);
  236. break;
  237. case kTfLiteInt8:
  238. EvalQuantizedLess<int8_t>(context, node, input1, input2, output,
  239. requires_broadcast);
  240. break;
  241. default:
  242. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  243. TfLiteTypeGetName(input1->type), input1->type);
  244. return kTfLiteError;
  245. }
  246. return kTfLiteOk;
  247. }
  248. TfLiteStatus LessEqualEval(TfLiteContext* context, TfLiteNode* node) {
  249. const TfLiteTensor* input1 = GetInput(context, node, kInputTensor1);
  250. const TfLiteTensor* input2 = GetInput(context, node, kInputTensor2);
  251. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  252. bool requires_broadcast = !HaveSameShapes(input1, input2);
  253. switch (input1->type) {
  254. case kTfLiteFloat32:
  255. TF_LITE_COMPARISON(float, LessEqual, requires_broadcast);
  256. break;
  257. case kTfLiteInt32:
  258. TF_LITE_COMPARISON(int32_t, LessEqual, requires_broadcast);
  259. break;
  260. case kTfLiteInt64:
  261. TF_LITE_COMPARISON(int64_t, LessEqual, requires_broadcast);
  262. break;
  263. case kTfLiteUInt8:
  264. EvalQuantizedLessEqual<uint8_t>(context, node, input1, input2, output,
  265. requires_broadcast);
  266. break;
  267. case kTfLiteInt8:
  268. EvalQuantizedLessEqual<int8_t>(context, node, input1, input2, output,
  269. requires_broadcast);
  270. break;
  271. default:
  272. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  273. TfLiteTypeGetName(input1->type), input1->type);
  274. return kTfLiteError;
  275. }
  276. return kTfLiteOk;
  277. }
  278. } // namespace
  279. } // namespace comparisons
  280. TfLiteRegistration* Register_EQUAL() {
  281. static TfLiteRegistration r = {/*init=*/nullptr,
  282. /*free=*/nullptr,
  283. /*prepare=*/nullptr,
  284. /*invoke=*/comparisons::EqualEval,
  285. /*profiling_string=*/nullptr,
  286. /*builtin_code=*/0,
  287. /*custom_name=*/nullptr,
  288. /*version=*/0};
  289. return &r;
  290. }
  291. TfLiteRegistration* Register_NOT_EQUAL() {
  292. static TfLiteRegistration r = {/*init=*/nullptr,
  293. /*free=*/nullptr,
  294. /*prepare=*/nullptr,
  295. /*invoke=*/comparisons::NotEqualEval,
  296. /*profiling_string=*/nullptr,
  297. /*builtin_code=*/0,
  298. /*custom_name=*/nullptr,
  299. /*version=*/0};
  300. return &r;
  301. }
  302. TfLiteRegistration* Register_GREATER() {
  303. static TfLiteRegistration r = {/*init=*/nullptr,
  304. /*free=*/nullptr,
  305. /*prepare=*/nullptr,
  306. /*invoke=*/comparisons::GreaterEval,
  307. /*profiling_string=*/nullptr,
  308. /*builtin_code=*/0,
  309. /*custom_name=*/nullptr,
  310. /*version=*/0};
  311. return &r;
  312. }
  313. TfLiteRegistration* Register_GREATER_EQUAL() {
  314. static TfLiteRegistration r = {/*init=*/nullptr,
  315. /*free=*/nullptr,
  316. /*prepare=*/nullptr,
  317. /*invoke=*/comparisons::GreaterEqualEval,
  318. /*profiling_string=*/nullptr,
  319. /*builtin_code=*/0,
  320. /*custom_name=*/nullptr,
  321. /*version=*/0};
  322. return &r;
  323. }
  324. TfLiteRegistration* Register_LESS() {
  325. static TfLiteRegistration r = {/*init=*/nullptr,
  326. /*free=*/nullptr,
  327. /*prepare=*/nullptr,
  328. /*invoke=*/comparisons::LessEval,
  329. /*profiling_string=*/nullptr,
  330. /*builtin_code=*/0,
  331. /*custom_name=*/nullptr,
  332. /*version=*/0};
  333. return &r;
  334. }
  335. TfLiteRegistration* Register_LESS_EQUAL() {
  336. static TfLiteRegistration r = {/*init=*/nullptr,
  337. /*free=*/nullptr,
  338. /*prepare=*/nullptr,
  339. /*invoke=*/comparisons::LessEqualEval,
  340. /*profiling_string=*/nullptr,
  341. /*builtin_code=*/0,
  342. /*custom_name=*/nullptr,
  343. /*version=*/0};
  344. return &r;
  345. }
  346. } // namespace micro
  347. } // namespace ops
  348. } // namespace tflite