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.

argsort.cpp 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * \file dnn/src/common/argsort.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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 "megdnn/oprs/general.h"
  12. #include "src/common/utils.h"
  13. using namespace megdnn;
  14. void ArgsortForward::deduce_layout(const TensorLayout& src, TensorLayout& dst,
  15. TensorLayout& indices) {
  16. megdnn_assert(src.ndim == 2 && src.is_contiguous(),
  17. "invalid src layout: %s", src.to_string().c_str());
  18. dst = src;
  19. indices = src;
  20. indices.dtype = dtype::Int32();
  21. }
  22. void ArgsortForward::check_exec(const TensorLayout& src,
  23. const TensorLayout& dst,
  24. const TensorLayout& indices,
  25. size_t workspace_in_bytes) {
  26. auto errmsg = [&]() {
  27. return megdnn_layout_msg(src) + ", " + megdnn_layout_msg(dst) + ", " +
  28. megdnn_layout_msg(indices);
  29. };
  30. MEGDNN_MARK_USED_VAR(errmsg);
  31. megdnn_assert_contiguous(src);
  32. megdnn_assert(src.ndim == 2_z, "%s", errmsg().c_str());
  33. megdnn_assert_eq_layout(src, dst);
  34. megdnn_assert_eq_shape(src, indices);
  35. megdnn_assert_contiguous(indices);
  36. megdnn_assert(src.dtype == dst.dtype);
  37. megdnn_assert(indices.dtype == dtype::Int32());
  38. auto required_workspace_in_bytes =
  39. get_workspace_in_bytes(src, dst, indices);
  40. megdnn_assert(workspace_in_bytes >= required_workspace_in_bytes);
  41. }
  42. void ArgsortBackward::check_exec(const TensorLayout& diff,
  43. const TensorLayout& indices,
  44. const TensorLayout& grad,
  45. size_t workspace_in_bytes) {
  46. megdnn_assert(diff.eq_shape(indices) && diff.dtype == grad.dtype &&
  47. indices.dtype == dtype::Int32{} &&
  48. diff.is_contiguous() && indices.is_contiguous() &&
  49. grad.is_contiguous() && diff.ndim == 2 &&
  50. grad.ndim == 2 && diff[0] == grad[0] &&
  51. diff[1] <= grad[1],
  52. "invalid layouts: diff=%s indices=%s grad=%s",
  53. diff.to_string().c_str(), indices.to_string().c_str(),
  54. grad.to_string().c_str());
  55. auto required_workspace_in_bytes =
  56. get_workspace_in_bytes(diff, indices, grad);
  57. megdnn_assert(workspace_in_bytes >= required_workspace_in_bytes);
  58. }
  59. // vim: syntax=cpp.doxygen

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

Contributors (1)