You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

rnn.cpp 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "rnn.h"
  15. #include <math.h>
  16. namespace ncnn {
  17. DEFINE_LAYER_CREATOR(RNN)
  18. RNN::RNN()
  19. {
  20. one_blob_only = false;
  21. support_inplace = false;
  22. }
  23. int RNN::load_param(const ParamDict& pd)
  24. {
  25. num_output = pd.get(0, 0);
  26. weight_data_size = pd.get(1, 0);
  27. return 0;
  28. }
  29. int RNN::load_model(const ModelBin& mb)
  30. {
  31. int size = (weight_data_size - num_output * num_output) / 2 / num_output;
  32. // raw weight data
  33. weight_hh_data = mb.load(size, num_output, 1);
  34. if (weight_hh_data.empty())
  35. return -100;
  36. weight_xh_data = mb.load(size, num_output, 1);
  37. if (weight_xh_data.empty())
  38. return -100;
  39. weight_ho_data = mb.load(num_output, num_output, 1);
  40. if (weight_ho_data.empty())
  41. return -100;
  42. bias_h_data = mb.load(num_output, 1);
  43. if (bias_h_data.empty())
  44. return -100;
  45. bias_o_data = mb.load(num_output, 1);
  46. if (bias_o_data.empty())
  47. return -100;
  48. return 0;
  49. }
  50. int RNN::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  51. {
  52. // size x 1 x T
  53. const Mat& input_blob = bottom_blobs[0];
  54. size_t elemsize = input_blob.elemsize;
  55. // T, 0 or 1 each
  56. const Mat& cont_blob = bottom_blobs[1];
  57. int T = input_blob.c;
  58. int size = input_blob.w;
  59. // initial hidden state
  60. Mat hidden(num_output, 4u, opt.workspace_allocator);
  61. if (hidden.empty())
  62. return -100;
  63. hidden.fill(0.f);
  64. Mat& top_blob = top_blobs[0];
  65. top_blob.create(num_output, 1, T, elemsize, opt.blob_allocator);
  66. if (top_blob.empty())
  67. return -100;
  68. // unroll
  69. for (int t=0; t<T; t++)
  70. {
  71. // clip hidden by continuation indicator
  72. // h_cont_{t-1} = cont_t * h_{t-1}
  73. // h_cont_{t-1} = h_{t-1} if cont_t == 1
  74. // 0 otherwise
  75. // calculate hidden
  76. // h_t = tanh( W_hh * h_cont_{t-1} + W_xh * x_t + b_h )
  77. const float cont = cont_blob[t];
  78. const Mat x = input_blob.channel(t);
  79. float* hidden_data = hidden;
  80. for (int q=0; q<num_output; q++)
  81. {
  82. float h_cont = cont ? hidden_data[q] : 0.f;
  83. const float* weight_hh_data_ptr = (const float*)weight_hh_data + weight_hh_data.w * q;
  84. const float* weight_xh_data_ptr = (const float*)weight_xh_data + weight_xh_data.w * q;
  85. const float* x_data = x;
  86. float s0 = bias_h_data[q];
  87. for (int i=0; i<size; i++)
  88. {
  89. s0 += weight_hh_data_ptr[i] * h_cont + weight_xh_data_ptr[i] * x_data[i];
  90. }
  91. hidden_data[q] = tanh(s0);
  92. }
  93. // calculate output
  94. // o_t = tanh( W_ho * h_t + b_o )
  95. Mat output = top_blob.channel(t);
  96. float* output_data = output;
  97. for (int q=0; q<num_output; q++)
  98. {
  99. const float* weight_ho_data_ptr = (const float*)weight_ho_data + weight_ho_data.w * q;
  100. float s0 = bias_o_data[q];
  101. for (int i=0; i<size; i++)
  102. {
  103. s0 += weight_ho_data_ptr[i] * hidden_data[i];
  104. }
  105. output_data[q] = tanh(s0);
  106. }
  107. // no hidden output here
  108. }
  109. return 0;
  110. }
  111. } // namespace ncnn