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 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/imperative/proxy_graph_detail.h"
  13. #include "megbrain/opr/basic_arith.h"
  14. #include "../dnn_op_helper.h"
  15. #include "../op_trait.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(
  27. inputs[0], reduce.param(), (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. // TODO: using this for apply_on_physical_tensor
  35. bool memory_forward_success(const OpDef& def, SmallVector<TensorPtr> inputs) {
  36. auto&& reduce = static_cast<const Reduce&>(def);
  37. if (reduce.mode != Reduce::Mode::SUM_SQR && inputs.size() == 2) {
  38. auto shape_tensor = inputs[1]->get_value();
  39. TensorShape shape;
  40. cg::copy_tensor_value_to_shape(shape, shape_tensor.proxy_to_default_cpu());
  41. if (shape.eq_shape(inputs[0]->shape())) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. SmallVector<TensorPtr> apply_on_physical_tensor(
  48. const OpDef& def, const SmallVector<TensorPtr>& inputs) {
  49. if (memory_forward_success(def, inputs)) {
  50. return {Tensor::make(inputs[0]->blob(), 0, inputs[0]->layout())};
  51. }
  52. return proxy_graph_detail::apply_on_physical_tensor(def, inputs);
  53. }
  54. OP_TRAIT_REG(Reduce, Reduce, opr::Reduce)
  55. .make_from_op_node(make_from_op_node)
  56. .apply_on_var_node(apply_on_var_node)
  57. .apply_on_physical_tensor(apply_on_physical_tensor)
  58. .fallback();
  59. } // namespace reduce
  60. } // namespace
  61. } // namespace imperative
  62. } // namespace mgb
  63. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}