sub.h 24 KB

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