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.

test_multiheadattention.cpp 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2021 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 "layer/multiheadattention.h"
  15. #include "testutil.h"
  16. static int test_multiheadattention(const ncnn::Mat& a, int num_heads)
  17. {
  18. int embed_dim = a.w;
  19. ncnn::ParamDict pd;
  20. pd.set(0, embed_dim);
  21. pd.set(1, num_heads);
  22. pd.set(2, embed_dim * embed_dim);
  23. std::vector<ncnn::Mat> weights(8);
  24. weights[0] = RandomMat(embed_dim * embed_dim);
  25. weights[1] = RandomMat(embed_dim);
  26. weights[2] = RandomMat(embed_dim * embed_dim);
  27. weights[3] = RandomMat(embed_dim);
  28. weights[4] = RandomMat(embed_dim * embed_dim);
  29. weights[5] = RandomMat(embed_dim);
  30. weights[6] = RandomMat(embed_dim * embed_dim);
  31. weights[7] = RandomMat(embed_dim);
  32. std::vector<ncnn::Mat> as(3);
  33. as[0] = a;
  34. as[1] = a;
  35. as[2] = a;
  36. int ret = test_layer<ncnn::MultiHeadAttention>("MultiHeadAttention", pd, weights, as);
  37. if (ret != 0)
  38. {
  39. fprintf(stderr, "test_multiheadattention failed a=(%d %d)\n", a.w, a.h);
  40. }
  41. return ret;
  42. }
  43. static int test_multiheadattention_sameqkv(const ncnn::Mat& a, int num_heads)
  44. {
  45. int embed_dim = a.w;
  46. ncnn::ParamDict pd;
  47. pd.set(0, embed_dim);
  48. pd.set(1, num_heads);
  49. pd.set(2, embed_dim * embed_dim);
  50. std::vector<ncnn::Mat> weights(8);
  51. weights[0] = RandomMat(embed_dim * embed_dim);
  52. weights[1] = RandomMat(embed_dim);
  53. weights[2] = RandomMat(embed_dim * embed_dim);
  54. weights[3] = RandomMat(embed_dim);
  55. weights[4] = RandomMat(embed_dim * embed_dim);
  56. weights[5] = RandomMat(embed_dim);
  57. weights[6] = RandomMat(embed_dim * embed_dim);
  58. weights[7] = RandomMat(embed_dim);
  59. std::vector<ncnn::Mat> as(1);
  60. as[0] = a;
  61. int ret = test_layer<ncnn::MultiHeadAttention>("MultiHeadAttention", pd, weights, as);
  62. if (ret != 0)
  63. {
  64. fprintf(stderr, "test_multiheadattention failed a=(%d %d)\n", a.w, a.h);
  65. }
  66. return ret;
  67. }
  68. static int test_multiheadattention_0()
  69. {
  70. return 0
  71. || test_multiheadattention(RandomMat(64, 128), 4)
  72. || test_multiheadattention(RandomMat(64, 127), 16);
  73. }
  74. static int test_multiheadattention_1()
  75. {
  76. return 0
  77. || test_multiheadattention_sameqkv(RandomMat(64, 128), 8)
  78. || test_multiheadattention_sameqkv(RandomMat(64, 127), 32);
  79. }
  80. int main()
  81. {
  82. SRAND(7767517);
  83. return 0
  84. || test_multiheadattention_0()
  85. || test_multiheadattention_1();
  86. }