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.

topk.h 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * \file dnn/test/common/topk.h
  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. #pragma once
  12. #include "megdnn/handle.h"
  13. #include "megdnn/oprs/general.h"
  14. #include "test/common/opr_proxy.h"
  15. namespace megdnn {
  16. namespace test {
  17. template <>
  18. struct OprProxy<TopK> {
  19. private:
  20. int m_k = 0;
  21. WorkspaceWrapper m_workspace;
  22. public:
  23. OprProxy() = default;
  24. OprProxy(int k) : m_k{k} {}
  25. void deduce_layout(TopK* opr, TensorLayoutArray& layouts) {
  26. if (layouts.size() == 3) {
  27. opr->deduce_layout(m_k, layouts[0], layouts[1], layouts[2]);
  28. } else {
  29. megdnn_assert(layouts.size() == 2);
  30. TensorLayout l;
  31. opr->deduce_layout(m_k, layouts[0], layouts[1], l);
  32. }
  33. }
  34. void exec(TopK* opr, const TensorNDArray& tensors) {
  35. if (!m_workspace.valid()) {
  36. m_workspace = {opr->handle(), 0};
  37. }
  38. if (tensors.size() == 3) {
  39. m_workspace.update(opr->get_workspace_in_bytes(
  40. m_k, tensors[0].layout, tensors[1].layout, tensors[2].layout));
  41. opr->exec(m_k, tensors[0], tensors[1], tensors[2], m_workspace.workspace());
  42. } else {
  43. m_workspace.update(opr->get_workspace_in_bytes(
  44. m_k, tensors[0].layout, tensors[1].layout, {}));
  45. opr->exec(m_k, tensors[0], tensors[1], {}, m_workspace.workspace());
  46. }
  47. }
  48. };
  49. template <typename Dtype>
  50. void run_topk_test(Handle* handle);
  51. } // namespace test
  52. } // namespace megdnn
  53. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台