sub.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* Copyright 2020 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. #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SUB_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SUB_H_
  14. #include "fixedpoint/fixedpoint.h"
  15. #include "ruy/profiler/instrumentation.h" // from @ruy
  16. #include "tensorflow/lite/kernels/internal/common.h"
  17. namespace tflite {
  18. namespace reference_ops {
  19. inline void SubNonBroadcast(const ArithmeticParams& params,
  20. const RuntimeShape& input1_shape,
  21. const float* input1_data,
  22. const RuntimeShape& input2_shape,
  23. const float* input2_data,
  24. const RuntimeShape& output_shape,
  25. float* output_data) {
  26. const int flat_size =
  27. MatchingElementsSize(input1_shape, input2_shape, output_shape);
  28. for (int i = 0; i < flat_size; ++i) {
  29. output_data[i] = ActivationFunctionWithMinMax(
  30. input1_data[i] - input2_data[i], params.float_activation_min,
  31. params.float_activation_max);
  32. }
  33. }
  34. inline void SubNonBroadcast(const ArithmeticParams& params,
  35. const RuntimeShape& input1_shape,
  36. const int32* input1_data,
  37. const RuntimeShape& input2_shape,
  38. const int32* input2_data,
  39. const RuntimeShape& output_shape,
  40. int32* output_data) {
  41. const int flat_size =
  42. MatchingElementsSize(input1_shape, input2_shape, output_shape);
  43. for (int i = 0; i < flat_size; ++i) {
  44. output_data[i] = ActivationFunctionWithMinMax(
  45. input1_data[i] - input2_data[i], params.quantized_activation_min,
  46. params.quantized_activation_max);
  47. }
  48. }
  49. // TODO(b/151345304): We can implement BroadcastSub on buffers of arbitrary
  50. // dimensionality if the runtime code does a single loop over one dimension
  51. // that handles broadcasting as the base case. The code generator would then
  52. // generate max(D1, D2) nested for loops.
  53. // TODO(b/151345101): BroadcastSub is intentionally duplicated from
  54. // reference_ops.h. Once an optimized version is implemented and NdArrayDesc<T>
  55. // is no longer referenced in this file, move NdArrayDesc<T> from types.h to
  56. // reference_ops.h.
  57. template <int N = 5>
  58. inline void BroadcastSubSlow(const ArithmeticParams& params,
  59. const RuntimeShape& input1_shape,
  60. const float* input1_data,
  61. const RuntimeShape& input2_shape,
  62. const float* input2_data,
  63. const RuntimeShape& output_shape,
  64. float* output_data) {
  65. ruy::profiler::ScopeLabel label("BroadcastSubSlow/float");
  66. TFLITE_DCHECK_LE(input1_shape.DimensionsCount(), N);
  67. TFLITE_DCHECK_LE(input2_shape.DimensionsCount(), N);
  68. TFLITE_DCHECK_LE(output_shape.DimensionsCount(), N);
  69. NdArrayDesc<N> desc1;
  70. NdArrayDesc<N> desc2;
  71. NdArrayDesc<N> output_desc;
  72. NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1,
  73. &desc2);
  74. CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc);
  75. // In Tensorflow, the dimensions are canonically named (batch_number, row,
  76. // col, channel), with extents (batches, height, width, depth), with the
  77. // trailing dimension changing most rapidly (channels has the smallest stride,
  78. // typically 1 element).
  79. //
  80. // In generated C code, we store arrays with the dimensions reversed. The
  81. // first dimension has smallest stride.
  82. //
  83. // We name our variables by their Tensorflow convention, but generate C code
  84. // nesting loops such that the innermost loop has the smallest stride for the
  85. // best cache behavior.
  86. auto sub_func = [&](int indexes[N]) {
  87. output_data[SubscriptToIndex(output_desc, indexes)] =
  88. ActivationFunctionWithMinMax(
  89. input1_data[SubscriptToIndex(desc1, indexes)] -
  90. input2_data[SubscriptToIndex(desc2, indexes)],
  91. params.float_activation_min, params.float_activation_max);
  92. };
  93. NDOpsHelper<N>(output_desc, sub_func);
  94. }
  95. template <int N = 5>
  96. inline void BroadcastSubSlow(const ArithmeticParams& params,
  97. const RuntimeShape& input1_shape,
  98. const uint8* input1_data,
  99. const RuntimeShape& input2_shape,
  100. const uint8* input2_data,
  101. const RuntimeShape& output_shape,
  102. uint8* output_data) {
  103. ruy::profiler::ScopeLabel label("BroadcastSubSlow/uint8");
  104. TFLITE_DCHECK_LE(input1_shape.DimensionsCount(), N);
  105. TFLITE_DCHECK_LE(input2_shape.DimensionsCount(), N);
  106. TFLITE_DCHECK_LE(output_shape.DimensionsCount(), N);
  107. NdArrayDesc<N> desc1;
  108. NdArrayDesc<N> desc2;
  109. NdArrayDesc<N> output_desc;
  110. NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1,
  111. &desc2);
  112. CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc);
  113. // In Tensorflow, the dimensions are canonically named (batch_number, row,
  114. // col, channel), with extents (batches, height, width, depth), with the
  115. // trailing dimension changing most rapidly (channels has the smallest stride,
  116. // typically 1 element).
  117. //
  118. // In generated C code, we store arrays with the dimensions reversed. The
  119. // first dimension has smallest stride.
  120. //
  121. // We name our variables by their Tensorflow convention, but generate C code
  122. // nesting loops such that the innermost loop has the smallest stride for the
  123. // best cache behavior.
  124. auto sub_func = [&](int indexes[N]) {
  125. const int32 input1_val =
  126. params.input1_offset + input1_data[SubscriptToIndex(desc1, indexes)];
  127. const int32 input2_val =
  128. params.input2_offset + input2_data[SubscriptToIndex(desc2, indexes)];
  129. const int32 shifted_input1_val = input1_val * (1 << params.left_shift);
  130. const int32 shifted_input2_val = input2_val * (1 << params.left_shift);
  131. const int32 scaled_input1_val =
  132. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  133. shifted_input1_val, params.input1_multiplier, params.input1_shift);
  134. const int32 scaled_input2_val =
  135. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  136. shifted_input2_val, params.input2_multiplier, params.input2_shift);
  137. const int32 raw_sub = scaled_input1_val - scaled_input2_val;
  138. const int32 raw_output =
  139. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  140. raw_sub, params.output_multiplier, params.output_shift) +
  141. params.output_offset;
  142. const int32 clamped_output =
  143. std::min(params.quantized_activation_max,
  144. std::max(params.quantized_activation_min, raw_output));
  145. output_data[SubscriptToIndex(output_desc, indexes)] =
  146. static_cast<uint8>(clamped_output);
  147. };
  148. NDOpsHelper<N>(output_desc, sub_func);
  149. }
  150. template <int N = 5>
  151. inline void BroadcastSubSlow(const ArithmeticParams& params,
  152. const RuntimeShape& input1_shape,
  153. const int32* input1_data,
  154. const RuntimeShape& input2_shape,
  155. const int32* input2_data,
  156. const RuntimeShape& output_shape,
  157. int32* output_data) {
  158. ruy::profiler::ScopeLabel label("BroadcastSubSlow/int32");
  159. TFLITE_DCHECK_LE(input1_shape.DimensionsCount(), N);
  160. TFLITE_DCHECK_LE(input2_shape.DimensionsCount(), N);
  161. TFLITE_DCHECK_LE(output_shape.DimensionsCount(), N);
  162. NdArrayDesc<N> desc1;
  163. NdArrayDesc<N> desc2;
  164. NdArrayDesc<N> output_desc;
  165. NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1,
  166. &desc2);
  167. CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc);
  168. // In Tensorflow, the dimensions are canonically named (batch_number, row,
  169. // col, channel), with extents (batches, height, width, depth), with the
  170. // trailing dimension changing most rapidly (channels has the smallest stride,
  171. // typically 1 element).
  172. //
  173. // In generated C code, we store arrays with the dimensions reversed. The
  174. // first dimension has smallest stride.
  175. //
  176. // We name our variables by their Tensorflow convention, but generate C code
  177. // nesting loops such that the innermost loop has the smallest stride for the
  178. // best cache behavior.
  179. auto sub_func = [&](int indexes[N]) {
  180. output_data[SubscriptToIndex(output_desc, indexes)] =
  181. ActivationFunctionWithMinMax(
  182. input1_data[SubscriptToIndex(desc1, indexes)] -
  183. input2_data[SubscriptToIndex(desc2, indexes)],
  184. params.quantized_activation_min, params.quantized_activation_max);
  185. };
  186. NDOpsHelper<N>(output_desc, sub_func);
  187. }
  188. template <int N = 5>
  189. inline void BroadcastSubSlow(const ArithmeticParams& params,
  190. const RuntimeShape& input1_shape,
  191. const int8_t* input1_data,
  192. const RuntimeShape& input2_shape,
  193. const int8_t* input2_data,
  194. const RuntimeShape& output_shape,
  195. int8_t* output_data) {
  196. ruy::profiler::ScopeLabel label("BroadcastSubSlow/int8");
  197. NdArrayDesc<N> desc1;
  198. NdArrayDesc<N> desc2;
  199. NdArrayDesc<N> output_desc;
  200. NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1,
  201. &desc2);
  202. CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc);
  203. // In Tensorflow, the dimensions are canonically named (batch_number, row,
  204. // col, channel), with extents (batches, height, width, depth), with the
  205. // trailing dimension changing most rapidly (channels has the smallest stride,
  206. // typically 1 element).
  207. //
  208. // In generated C code, we store arrays with the dimensions reversed. The
  209. // first dimension has smallest stride.
  210. //
  211. // We name our variables by their Tensorflow convention, but generate C code
  212. // nesting loops such that the innermost loop has the smallest stride for the
  213. // best cache behavior.
  214. auto sub_func = [&](int indexes[N]) {
  215. const int32_t input1_val =
  216. params.input1_offset + input1_data[SubscriptToIndex(desc1, indexes)];
  217. const int32_t input2_val =
  218. params.input2_offset + input2_data[SubscriptToIndex(desc2, indexes)];
  219. const int32_t shifted_input1_val = input1_val * (1 << params.left_shift);
  220. const int32_t shifted_input2_val = input2_val * (1 << params.left_shift);
  221. const int32_t scaled_input1_val =
  222. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  223. shifted_input1_val, params.input1_multiplier, params.input1_shift);
  224. const int32_t scaled_input2_val =
  225. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  226. shifted_input2_val, params.input2_multiplier, params.input2_shift);
  227. const int32_t raw_sub = scaled_input1_val - scaled_input2_val;
  228. const int32_t raw_output =
  229. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  230. raw_sub, params.output_multiplier, params.output_shift) +
  231. params.output_offset;
  232. const int32_t clamped_output =
  233. std::min(params.quantized_activation_max,
  234. std::max(params.quantized_activation_min, raw_output));
  235. output_data[SubscriptToIndex(output_desc, indexes)] =
  236. static_cast<int8_t>(clamped_output);
  237. };
  238. NDOpsHelper<N>(output_desc, sub_func);
  239. }
  240. template <typename T, int N = 5>
  241. void BroadcastSubSlow(const ArithmeticParams& params,
  242. const RuntimeShape& input1_shape, const T* input1_data,
  243. const RuntimeShape& input2_shape, const T* input2_data,
  244. const RuntimeShape& output_shape, T* output_data) {
  245. ruy::profiler::ScopeLabel label("BroadcastSubSlow/templated");
  246. TFLITE_DCHECK_LE(input1_shape.DimensionsCount(), N);
  247. TFLITE_DCHECK_LE(input2_shape.DimensionsCount(), N);
  248. TFLITE_DCHECK_LE(output_shape.DimensionsCount(), N);
  249. NdArrayDesc<N> desc1;
  250. NdArrayDesc<N> desc2;
  251. NdArrayDesc<N> output_desc;
  252. NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1,
  253. &desc2);
  254. CopyDimsToDesc(RuntimeShape::ExtendedShape(N, output_shape), &output_desc);
  255. // In Tensorflow, the dimensions are canonically named (batch_number, row,
  256. // col, channel), with extents (batches, height, width, depth), with the
  257. // trailing dimension changing most rapidly (channels has the smallest stride,
  258. // typically 1 element).
  259. //
  260. // In generated C code, we store arrays with the dimensions reversed. The
  261. // first dimension has smallest stride.
  262. //
  263. // We name our variables by their Tensorflow convention, but generate C code
  264. // nesting loops such that the innermost loop has the smallest stride for the
  265. // best cache behavior.
  266. auto sub_func = [&](int indexes[N]) {
  267. output_data[SubscriptToIndex(output_desc, indexes)] =
  268. ActivationFunctionWithMinMax(
  269. input1_data[SubscriptToIndex(desc1, indexes)] -
  270. input2_data[SubscriptToIndex(desc2, indexes)],
  271. params.quantized_activation_min, params.quantized_activation_max);
  272. };
  273. NDOpsHelper<N>(output_desc, sub_func);
  274. }
  275. // Element-wise Sub that can often be used for inner loop of broadcast sub as
  276. // well as the non-broadcast sub.
  277. inline void SubElementwise(int size, const ArithmeticParams& params,
  278. const uint8* input1_data, const uint8* input2_data,
  279. uint8* output_data) {
  280. TFLITE_DCHECK_GT(params.input1_offset, -256);
  281. TFLITE_DCHECK_GT(params.input2_offset, -256);
  282. TFLITE_DCHECK_LT(params.input1_offset, 256);
  283. TFLITE_DCHECK_LT(params.input2_offset, 256);
  284. for (int i = 0; i < size; ++i) {
  285. const int32 input1_val = params.input1_offset + input1_data[i];
  286. const int32 input2_val = params.input2_offset + input2_data[i];
  287. const int32 shifted_input1_val = input1_val * (1 << params.left_shift);
  288. const int32 shifted_input2_val = input2_val * (1 << params.left_shift);
  289. const int32 scaled_input1_val =
  290. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  291. shifted_input1_val, params.input1_multiplier, params.input1_shift);
  292. const int32 scaled_input2_val =
  293. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  294. shifted_input2_val, params.input2_multiplier, params.input2_shift);
  295. const int32 raw_sub = scaled_input1_val - scaled_input2_val;
  296. const int32 raw_output =
  297. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  298. raw_sub, params.output_multiplier, params.output_shift) +
  299. params.output_offset;
  300. const int32 clamped_output =
  301. std::min(params.quantized_activation_max,
  302. std::max(params.quantized_activation_min, raw_output));
  303. output_data[i] = static_cast<uint8>(clamped_output);
  304. }
  305. }
  306. // Element-wise add that can often be used for inner loop of broadcast add as
  307. // well as the non-broadcast add.
  308. inline void SubElementwise(int size, const ArithmeticParams& params,
  309. const int8_t* input1_data, const int8_t* input2_data,
  310. int8_t* output_data) {
  311. const int32_t int8_max_value = std::numeric_limits<int8_t>::max();
  312. TFLITE_DCHECK_GE(params.input1_offset, -1 * int8_max_value);
  313. TFLITE_DCHECK_GE(params.input2_offset, -1 * int8_max_value);
  314. TFLITE_DCHECK_LE(params.input1_offset, int8_max_value);
  315. TFLITE_DCHECK_LE(params.input2_offset, int8_max_value);
  316. for (int i = 0; i < size; ++i) {
  317. const int32 input1_val = params.input1_offset + input1_data[i];
  318. const int32 input2_val = params.input2_offset + input2_data[i];
  319. const int32 shifted_input1_val = input1_val * (1 << params.left_shift);
  320. const int32 shifted_input2_val = input2_val * (1 << params.left_shift);
  321. const int32 scaled_input1_val =
  322. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  323. shifted_input1_val, params.input1_multiplier, params.input1_shift);
  324. const int32 scaled_input2_val =
  325. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  326. shifted_input2_val, params.input2_multiplier, params.input2_shift);
  327. const int32 raw_sub = scaled_input1_val - scaled_input2_val;
  328. const int32 raw_output =
  329. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  330. raw_sub, params.output_multiplier, params.output_shift) +
  331. params.output_offset;
  332. const int32 clamped_output =
  333. std::min(params.quantized_activation_max,
  334. std::max(params.quantized_activation_min, raw_output));
  335. output_data[i] = static_cast<int8_t>(clamped_output);
  336. }
  337. }
  338. inline void Sub(const ArithmeticParams& params,
  339. const RuntimeShape& input1_shape, const uint8* input1_data,
  340. const RuntimeShape& input2_shape, const uint8* input2_data,
  341. const RuntimeShape& output_shape, uint8* output_data) {
  342. TFLITE_DCHECK_LE(params.quantized_activation_min,
  343. params.quantized_activation_max);
  344. const int flat_size =
  345. MatchingElementsSize(input1_shape, input2_shape, output_shape);
  346. TFLITE_DCHECK_GT(params.input1_offset, -256);
  347. TFLITE_DCHECK_GT(params.input2_offset, -256);
  348. TFLITE_DCHECK_LT(params.input1_offset, 256);
  349. TFLITE_DCHECK_LT(params.input2_offset, 256);
  350. SubElementwise(flat_size, params, input1_data, input2_data, output_data);
  351. }
  352. inline void Sub(const ArithmeticParams& params,
  353. const RuntimeShape& input1_shape, const int8_t* input1_data,
  354. const RuntimeShape& input2_shape, const int8_t* input2_data,
  355. const RuntimeShape& output_shape, int8_t* output_data) {
  356. TFLITE_DCHECK_LE(params.quantized_activation_min,
  357. params.quantized_activation_max);
  358. const int flat_size =
  359. MatchingElementsSize(input1_shape, input2_shape, output_shape);
  360. const int32_t int8_max_value = std::numeric_limits<int8_t>::max();
  361. TFLITE_DCHECK_GE(params.input1_offset, -1 * int8_max_value);
  362. TFLITE_DCHECK_GE(params.input2_offset, -1 * int8_max_value);
  363. TFLITE_DCHECK_LE(params.input1_offset, int8_max_value);
  364. TFLITE_DCHECK_LE(params.input2_offset, int8_max_value);
  365. SubElementwise(flat_size, params, input1_data, input2_data, output_data);
  366. }
  367. template <typename T>
  368. void Sub(const ArithmeticParams& params, const RuntimeShape& input1_shape,
  369. const T* input1_data, const RuntimeShape& input2_shape,
  370. const T* input2_data, const RuntimeShape& output_shape,
  371. T* output_data) {
  372. NdArrayDesc<4> desc1;
  373. NdArrayDesc<4> desc2;
  374. NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1,
  375. &desc2);
  376. const RuntimeShape extended_output_shape =
  377. RuntimeShape::ExtendedShape(4, output_shape);
  378. // In Tensorflow, the dimensions are canonically named (batch_number, row,
  379. // col, channel), with extents (batches, height, width, depth), with the
  380. // trailing dimension changing most rapidly (channels has the smallest stride,
  381. // typically 1 element).
  382. //
  383. // In generated C code, we store arrays with the dimensions reversed. The
  384. // first dimension has smallest stride.
  385. //
  386. // We name our variables by their Tensorflow convention, but generate C code
  387. // nesting loops such that the innermost loop has the smallest stride for the
  388. // best cache behavior.
  389. for (int b = 0; b < extended_output_shape.Dims(0); ++b) {
  390. for (int y = 0; y < extended_output_shape.Dims(1); ++y) {
  391. for (int x = 0; x < extended_output_shape.Dims(2); ++x) {
  392. for (int c = 0; c < extended_output_shape.Dims(3); ++c) {
  393. output_data[Offset(extended_output_shape, b, y, x, c)] =
  394. input1_data[SubscriptToIndex(desc1, b, y, x, c)] -
  395. input2_data[SubscriptToIndex(desc2, b, y, x, c)];
  396. }
  397. }
  398. }
  399. }
  400. }
  401. inline void SubWithActivation(const ArithmeticParams& params,
  402. const RuntimeShape& input1_shape,
  403. const int32* input1_data,
  404. const RuntimeShape& input2_shape,
  405. const int32* input2_data,
  406. const RuntimeShape& output_shape,
  407. int32* output_data) {
  408. ruy::profiler::ScopeLabel label("SubWithActivation");
  409. const int flat_size =
  410. MatchingElementsSize(input1_shape, input2_shape, output_shape);
  411. for (int i = 0; i < flat_size; ++i) {
  412. output_data[i] = ActivationFunctionWithMinMax(
  413. input1_data[i] - input2_data[i], params.quantized_activation_min,
  414. params.quantized_activation_max);
  415. }
  416. }
  417. inline void SubWithActivation(const ArithmeticParams& params,
  418. const RuntimeShape& input1_shape,
  419. const float* input1_data,
  420. const RuntimeShape& input2_shape,
  421. const float* input2_data,
  422. const RuntimeShape& output_shape,
  423. float* output_data) {
  424. const int flat_size =
  425. MatchingElementsSize(input1_shape, input2_shape, output_shape);
  426. for (int i = 0; i < flat_size; ++i) {
  427. output_data[i] = ActivationFunctionWithMinMax(
  428. input1_data[i] - input2_data[i], params.float_activation_min,
  429. params.float_activation_max);
  430. }
  431. }
  432. } // namespace reference_ops
  433. } // namespace tflite
  434. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SUB_H_