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_oom.cpp 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2024 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 "testutil.h"
  15. static int test_multiheadattention_oom(const ncnn::Mat& q, const ncnn::Mat& k, const ncnn::Mat& v, int embed_dim, int num_heads, int attn_mask)
  16. {
  17. const int qdim = q.w;
  18. const int kdim = k.w;
  19. const int vdim = v.w;
  20. ncnn::ParamDict pd;
  21. pd.set(0, embed_dim);
  22. pd.set(1, num_heads);
  23. pd.set(2, embed_dim * qdim);
  24. pd.set(3, kdim);
  25. pd.set(4, vdim);
  26. pd.set(5, attn_mask);
  27. std::vector<ncnn::Mat> weights(8);
  28. weights[0] = RandomMat(embed_dim * qdim);
  29. weights[1] = RandomMat(embed_dim);
  30. weights[2] = RandomMat(embed_dim * kdim);
  31. weights[3] = RandomMat(embed_dim);
  32. weights[4] = RandomMat(embed_dim * vdim);
  33. weights[5] = RandomMat(embed_dim);
  34. weights[6] = RandomMat(qdim * embed_dim);
  35. weights[7] = RandomMat(qdim);
  36. std::vector<ncnn::Mat> as(3);
  37. as[0] = q;
  38. as[1] = k;
  39. as[2] = v;
  40. if (attn_mask)
  41. {
  42. as.push_back(RandomMat(k.h, q.h));
  43. }
  44. int ret = test_layer_oom("MultiHeadAttention", pd, weights, as, 1);
  45. if (ret != 0)
  46. {
  47. fprintf(stderr, "test_multiheadattention_oom failed q=(%d %d) k=(%d %d) v=(%d %d) embed_dim=%d num_heads=%d kdim=%d vdim=%d attn_mask=%d\n", q.w, q.h, k.w, k.h, v.w, v.h, embed_dim, num_heads, kdim, vdim, attn_mask);
  48. }
  49. return ret;
  50. }
  51. static int test_multiheadattention_0()
  52. {
  53. return 0
  54. || test_multiheadattention_oom(RandomMat(62, 66), RandomMat(32, 66), RandomMat(20, 66), 62, 2, 0)
  55. || test_multiheadattention_oom(RandomMat(26, 64), RandomMat(32, 64), RandomMat(18, 64), 26, 2, 1)
  56. || test_multiheadattention_oom(RandomMat(12, 17), RandomMat(28, 127), RandomMat(32, 127), 12, 3, 0)
  57. || test_multiheadattention_oom(RandomMat(12, 17), RandomMat(28, 32), RandomMat(11, 32), 12, 3, 1);
  58. }
  59. int main()
  60. {
  61. SRAND(7767517);
  62. return test_multiheadattention_0();
  63. }