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.

common_utils_test.cc 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <vector>
  17. #include "common/common_test.h"
  18. #include "kernel/common_utils.h"
  19. namespace mindspore {
  20. namespace kernel {
  21. class CommonUtilTest : public UT::Common {
  22. public:
  23. CommonUtilTest() = default;
  24. };
  25. TEST_F(CommonUtilTest, DeduplicateIndexedSlicesTest1) {
  26. // The indices is a vector and the grad is a tensor with shape (6, 2)
  27. /* 0
  28. * 0
  29. * 1
  30. * 1
  31. * 0
  32. * 3
  33. */
  34. std::vector<int> indices{0, 0, 1, 1, 0, 3};
  35. /* 0 1
  36. * 2 3
  37. * 4 5
  38. * 6 7
  39. * 8 9
  40. * 10 11
  41. */
  42. std::vector<float> grad;
  43. for (int i = 0; i < 6 * 2; i++) {
  44. grad.push_back(i);
  45. }
  46. std::vector<int> unique_indices(3);
  47. std::vector<float> summed_grad(6);
  48. SparseGradient unique_grad({summed_grad.data(), unique_indices.data(), 0});
  49. DeduplicateIndexedSlices(SparseGradient({grad.data(), indices.data(), 6}), &unique_grad, 6, 2);
  50. EXPECT_EQ(unique_grad.indices_size_, 3);
  51. EXPECT_EQ(unique_indices, std::vector<int>({0, 1, 3}));
  52. /* 10 13
  53. * 10 12
  54. * 10 11
  55. */
  56. EXPECT_EQ(summed_grad, std::vector<float>({10, 13, 10, 12, 10, 11}));
  57. }
  58. TEST_F(CommonUtilTest, DeduplicateIndexedSlicesTest2) {
  59. // The indices is a vector and the grad is a tensor with shape (6, 2)
  60. /* 0
  61. * 0
  62. * 1
  63. * 1
  64. * 0
  65. * 6
  66. */
  67. std::vector<int> indices{0, 0, 1, 1, 0, 6};
  68. /* 0 1
  69. * 2 3
  70. * 4 5
  71. * 6 7
  72. * 8 9
  73. * 10 11
  74. */
  75. std::vector<float> grad;
  76. for (int i = 0; i < 6 * 2; i++) {
  77. grad.push_back(i);
  78. }
  79. std::vector<int> unique_indices(2);
  80. std::vector<float> summed_grad(4);
  81. SparseGradient unique_grad({summed_grad.data(), unique_indices.data(), 0});
  82. DeduplicateIndexedSlices(SparseGradient({grad.data(), indices.data(), 6}), &unique_grad, 6, 2);
  83. EXPECT_EQ(unique_grad.indices_size_, 2);
  84. EXPECT_EQ(unique_indices, std::vector<int>({0, 1}));
  85. /* 10 13
  86. * 10 12
  87. */
  88. EXPECT_EQ(summed_grad, std::vector<float>({10, 13, 10, 12}));
  89. }
  90. } // namespace kernel
  91. } // namespace mindspore