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_embed.cpp 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_embed(int words, int num_output, int input_dim, int bias)
  16. {
  17. ncnn::ParamDict pd;
  18. pd.set(0, num_output);
  19. pd.set(1, input_dim);
  20. pd.set(2, bias);
  21. pd.set(3, num_output * input_dim);
  22. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  23. weights[0] = RandomMat(num_output * input_dim);
  24. if (bias)
  25. weights[1] = RandomMat(num_output);
  26. ncnn::Mat a(words);
  27. RandomizeInt(a, 0, input_dim);
  28. int ret = test_layer("Embed", pd, weights, a);
  29. if (ret != 0)
  30. {
  31. fprintf(stderr, "test_embed failed words=%d num_output=%d input_dim=%d bias=%d\n", words, num_output, input_dim, bias);
  32. }
  33. return ret;
  34. }
  35. static int test_embed_0()
  36. {
  37. return 0
  38. || test_embed(128, 128, 128, 0)
  39. || test_embed(128, 128, 128, 1)
  40. || test_embed(127, 127, 127, 0)
  41. || test_embed(127, 127, 127, 1)
  42. || test_embed(124, 124, 124, 0)
  43. || test_embed(124, 124, 124, 1);
  44. }
  45. #if NCNN_INT8
  46. static int test_embed_int8(int words, int num_output, int input_dim, int bias)
  47. {
  48. ncnn::ParamDict pd;
  49. pd.set(0, num_output);
  50. pd.set(1, input_dim);
  51. pd.set(2, bias);
  52. pd.set(3, num_output * input_dim);
  53. pd.set(18, 2);
  54. std::vector<ncnn::Mat> weights(bias ? 3 : 2);
  55. weights[0] = RandomS8Mat(num_output * input_dim);
  56. if (bias)
  57. {
  58. weights[1] = RandomMat(num_output);
  59. weights[2] = RandomMat(1, 100.f, 200.f);
  60. }
  61. else
  62. {
  63. weights[1] = RandomMat(1, 100.f, 200.f);
  64. }
  65. ncnn::Mat a(words);
  66. RandomizeInt(a, 0, input_dim);
  67. int ret = test_layer("Embed", pd, weights, a);
  68. if (ret != 0)
  69. {
  70. fprintf(stderr, "test_embed_int8 failed words=%d num_output=%d input_dim=%d bias=%d\n", words, num_output, input_dim, bias);
  71. }
  72. return ret;
  73. }
  74. static int test_embed_1()
  75. {
  76. return 0
  77. || test_embed_int8(128, 128, 128, 0)
  78. || test_embed_int8(128, 128, 128, 1)
  79. || test_embed_int8(127, 127, 127, 0)
  80. || test_embed_int8(127, 127, 127, 1)
  81. || test_embed_int8(124, 124, 124, 0)
  82. || test_embed_int8(124, 124, 124, 1);
  83. }
  84. #endif // NCNN_INT8
  85. int main()
  86. {
  87. SRAND(7767517);
  88. #if NCNN_INT8
  89. return test_embed_0() || test_embed_1();
  90. #else
  91. return test_embed_0();
  92. #endif
  93. }