arm_nn_lstm_step_s8.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /* ----------------------------------------------------------------------
  19. * Project: CMSIS NN Library
  20. * Title: arm_nn_lstm_step_s8.c
  21. * Description: Update LSTM function for a single iteration step.
  22. *
  23. * $Date: 19 January 2024
  24. * $Revision: V.1.0.0
  25. *
  26. * Target : Arm(R) M-Profile Architecture
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. /**
  32. * @ingroup groupSupport
  33. */
  34. /**
  35. * @addtogroup supportLSTM
  36. * @{
  37. */
  38. /*
  39. * Calculate the output state tensor of an LSTM step, s8 input/output/weights and s16 internal buffers version.
  40. * Refer to header file for details.
  41. */
  42. arm_cmsis_nn_status arm_nn_lstm_step_s8(const int8_t *data_in,
  43. const int8_t *hidden_in,
  44. int8_t *hidden_out,
  45. const cmsis_nn_lstm_params *params,
  46. cmsis_nn_lstm_context *buffers,
  47. const int32_t batch_offset)
  48. {
  49. int16_t *forget_gate = buffers->temp1;
  50. int16_t *input_gate = buffers->temp1;
  51. int16_t *cell_gate = buffers->temp2;
  52. int16_t *output_gate = buffers->temp1;
  53. int16_t *hidden_temp = buffers->temp2;
  54. int16_t *cell_state = buffers->cell_state;
  55. arm_nn_lstm_calculate_gate_s8_s16(data_in, hidden_in, &params->forget_gate, params, forget_gate, batch_offset);
  56. // Calculate first term of cell state in place early to maximise reuse of scratch-buffers
  57. arm_elementwise_mul_s16(forget_gate,
  58. cell_state,
  59. 0,
  60. 0,
  61. cell_state,
  62. 0,
  63. params->forget_to_cell_multiplier,
  64. params->forget_to_cell_shift,
  65. NN_Q15_MIN,
  66. NN_Q15_MAX,
  67. params->hidden_size * params->batch_size);
  68. arm_nn_lstm_calculate_gate_s8_s16(data_in, hidden_in, &params->input_gate, params, input_gate, batch_offset);
  69. arm_nn_lstm_calculate_gate_s8_s16(data_in, hidden_in, &params->cell_gate, params, cell_gate, batch_offset);
  70. // Reminder of cell state calculation, multiply and add to previous result.
  71. arm_elementwise_mul_acc_s16(forget_gate,
  72. cell_gate,
  73. 0,
  74. 0,
  75. cell_state,
  76. 0,
  77. params->input_to_cell_multiplier,
  78. params->input_to_cell_shift,
  79. -params->cell_clip,
  80. params->cell_clip,
  81. params->hidden_size * params->batch_size);
  82. arm_nn_lstm_calculate_gate_s8_s16(data_in, hidden_in, &params->output_gate, params, output_gate, batch_offset);
  83. // Calculate hidden state directly to output.
  84. arm_nn_activation_s16(
  85. cell_state, hidden_temp, params->hidden_size * params->batch_size, params->cell_scale_power + 12, ARM_TANH);
  86. arm_elementwise_mul_s16_s8(output_gate,
  87. hidden_temp,
  88. hidden_out,
  89. params->output_offset,
  90. params->output_multiplier,
  91. params->output_shift,
  92. params->hidden_size,
  93. params->batch_size,
  94. batch_offset);
  95. return ARM_CMSIS_NN_SUCCESS;
  96. }
  97. /**
  98. * @} end of supportLSTM group
  99. */