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_reshape_1.cpp 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2025 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_reshape(const ncnn::Mat& a, const char* shape_expr)
  16. {
  17. ncnn::ParamDict pd;
  18. pd.set(6, std::string(shape_expr));
  19. std::vector<ncnn::Mat> weights(0);
  20. int ret = test_layer("Reshape", pd, weights, a);
  21. if (ret != 0)
  22. {
  23. fprintf(stderr, "test_reshape failed a.dims=%d a=(%d %d %d %d) shape_expr=%s\n", a.dims, a.w, a.h, a.d, a.c, shape_expr);
  24. }
  25. return ret;
  26. }
  27. static int test_reshape_refs(const std::vector<ncnn::Mat>& as, const char* shape_expr)
  28. {
  29. ncnn::ParamDict pd;
  30. pd.set(6, std::string(shape_expr));
  31. std::vector<ncnn::Mat> weights(0);
  32. int ret = test_layer("Reshape", pd, weights, as, 1);
  33. if (ret != 0)
  34. {
  35. fprintf(stderr, "test_reshape_refs failed a.dims=%d a=(%d %d %d %d) shape_expr=%s\n", as[0].dims, as[0].w, as[0].h, as[0].d, as[0].c, shape_expr);
  36. }
  37. return ret;
  38. }
  39. static int test_reshape_refs(const ncnn::Mat& a, const char* shape_expr)
  40. {
  41. ncnn::ParamDict pd;
  42. pd.set(6, std::string(shape_expr));
  43. std::vector<ncnn::Mat> weights(0);
  44. int ret = test_layer("Reshape", pd, weights, a);
  45. if (ret != 0)
  46. {
  47. fprintf(stderr, "test_reshape_refs failed a.dims=%d a=(%d %d %d %d) shape_expr=%s\n", a.dims, a.w, a.h, a.d, a.c, shape_expr);
  48. }
  49. return ret;
  50. }
  51. static int test_reshape_0()
  52. {
  53. ncnn::Mat a = RandomMat(3, 2, 25, 32);
  54. return 0
  55. || test_reshape(a, "0w,0h,*(0d,2),-1")
  56. || test_reshape(a, "-1,0w,5,4")
  57. || test_reshape(a, "-1,square(neg(abs(max(min(//(*(-(+(0w,1),-1),4),4),100),-4))))")
  58. || test_reshape(a, "-1,square(neg(abs(max(min(//(*(-(+(0w,1.0),-1.0),2.0),2.0),100.3),-2.2)))")
  59. || test_reshape(a, "-1,trunc(*(round(-(floor(+(ceil(*(0w,1.2)),0.7)),-0.4)),1.0001))")
  60. || test_reshape(a, "-1,ceil(sqrt(square(*(asinh(sinh(atanh(tanh(atan(tan(acos(cos(asin(sin(/(0w,2))))))))))),16))))");
  61. }
  62. static int test_reshape_1()
  63. {
  64. std::vector<ncnn::Mat> as(2);
  65. as[0] = RandomMat(14, 15, 16);
  66. as[1] = RandomMat(28, 45, 48);
  67. return 0
  68. || test_reshape_refs(as, "*(1w,0.5),/(1h,3),-(1c,32)")
  69. || test_reshape_refs(as, "*(0w,0h),-(-(1c,0c),16)");
  70. }
  71. static int test_reshape_2()
  72. {
  73. ncnn::Mat a = RandomMat(14, 15, 16);
  74. return 0
  75. || test_reshape_refs(a, "*(0w,0.5),/(0h,3),-1")
  76. || test_reshape_refs(a, "-1")
  77. || test_reshape_refs(a, "*(0w,0h),0c");
  78. }
  79. int main()
  80. {
  81. SRAND(7767517);
  82. return 0
  83. || test_reshape_0()
  84. || test_reshape_1()
  85. || test_reshape_2();
  86. }