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.

reduce.cpp 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * \file imperative/src/impl/ops/reduce.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 "megbrain/imperative/ops/autogen.h"
  12. #include "megbrain/opr/basic_arith.h"
  13. #include "megbrain/imperative/proxy_graph_detail.h"
  14. #include "../op_trait.h"
  15. #include "../dnn_op_helper.h"
  16. namespace mgb {
  17. namespace imperative {
  18. namespace {
  19. namespace reduce {
  20. auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
  21. auto&& reduce = static_cast<const Reduce&>(def);
  22. OperatorNodeConfig config{reduce.make_name()};
  23. if (inputs.size() > 1) {
  24. return opr::Reduce::make(inputs[0], reduce.param(), inputs[1], config);
  25. } else {
  26. return opr::Reduce::make(inputs[0], reduce.param(),
  27. (cg::VarNode*)nullptr, config);
  28. }
  29. }
  30. std::shared_ptr<OpDef> make_from_op_node(cg::OperatorNodeBase* node_) {
  31. auto* node = &node_->cast_final_safe<opr::Reduce>();
  32. return Reduce::make(node->param());
  33. }
  34. bool memory_forward_success(
  35. const OpDef& def,
  36. SmallVector<TensorPtr> inputs) {
  37. auto&& reduce = static_cast<const Reduce&>(def);
  38. if (reduce.mode != Reduce::Mode::SUM_SQR && inputs.size() == 2) {
  39. auto shape_tensor = inputs[1]->get_value();
  40. TensorShape shape;
  41. cg::copy_tensor_value_to_shape(shape, shape_tensor.proxy_to_default_cpu());
  42. if (shape.eq_shape(inputs[0]->shape())) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. std::tuple<SmallVector<MemoryDesc>, SmallVector<MemoryDesc>> infer_output_mem_desc(
  49. const OpDef& def,
  50. const SmallVector<TensorPtr>& inputs_tensors,
  51. const SmallVector<MemoryDesc>& inputs_mems) {
  52. if (memory_forward_success(def, inputs_tensors)) {
  53. auto& src_desc = inputs_mems[0];
  54. return {{{src_desc.layout, 0, src_desc.cn, StorageIdentifier::make(&src_desc)}}, {}};
  55. }
  56. return proxy_graph_detail::infer_output_mem_desc(def, inputs_tensors, inputs_mems);
  57. }
  58. void execute(const OpDef& def,
  59. SmallVector<TensorPtr> inputs,
  60. SmallVector<TensorPtr> outputs,
  61. SmallVector<TensorPtr> workspace) {
  62. if (memory_forward_success(def, inputs)) {
  63. return;
  64. }
  65. return proxy_graph_detail::execute(def, inputs, outputs, workspace);
  66. }
  67. OP_TRAIT_REG(Reduce, Reduce, opr::Reduce)
  68. .make_from_op_node(make_from_op_node)
  69. .apply_on_var_node(apply_on_var_node)
  70. .infer_output_mem_desc(infer_output_mem_desc)
  71. .execute(execute)
  72. .fallback();
  73. } // namespace reduce
  74. } // namespace
  75. } // namespace imperative
  76. } // namespace mgb
  77. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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