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 2.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_0()
  44. {
  45. return 0
  46. || test_multiheadattention(RandomMat(64, 128), 4)
  47. || test_multiheadattention(RandomMat(64, 127), 16);
  48. }
  49. int main()
  50. {
  51. SRAND(7767517);
  52. return 0
  53. || test_multiheadattention_0();
  54. }