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.

tensor_remap.cpp 1.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * \file dnn/test/common/tensor_remap.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "test/common/tensor_remap.h"
  12. #include <cstring>
  13. #include "test/common/random_state.h"
  14. namespace megdnn {
  15. namespace test {
  16. namespace tensor_remap {
  17. dt_float32 MapRNG::gen_single_val() {
  18. auto&& gen = RandomState::generator();
  19. std::uniform_int_distribution<int> dist(0, m_src[m_cnt] - 1);
  20. m_cnt++;
  21. if (m_cnt == m_src.ndim)
  22. m_cnt -= m_src.ndim;
  23. return dist(gen);
  24. }
  25. NonoverlappingMapRNG::NonoverlappingMapRNG(TensorShape src)
  26. : m_cnt(0), m_src(src), m_idx(TensorLayout(src, dtype::Byte()), 0) {}
  27. dt_float32 NonoverlappingMapRNG::gen_single_val() {
  28. auto res = m_idx.array()[m_cnt];
  29. m_cnt++;
  30. if (m_cnt == m_src.ndim) {
  31. m_cnt -= m_src.ndim;
  32. m_idx = Index(m_idx.layout(), m_idx.linear_index() + 1);
  33. }
  34. return res;
  35. }
  36. } // namespace tensor_remap
  37. } // namespace test
  38. } // namespace megdnn
  39. // vim: syntax=cpp.doxygen