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.

op_def.cpp 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * \file imperative/src/impl/op_def.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 "megbrain/imperative/op_def.h"
  12. #include "megbrain/imperative/ops/opr_attr.h"
  13. #include "./op_trait.h"
  14. namespace mgb {
  15. namespace imperative {
  16. std::shared_ptr<OpDef> OpDef::make_from_op_node(
  17. cg::OperatorNodeBase* node) {
  18. OpTrait* trait;
  19. trait = OpTrait::find_by_typeinfo(node->dyn_typeinfo());
  20. if (!trait) {
  21. // TODO: register `make_from_op_node` for each OperatorNode
  22. // instead of forwarding to OprAttr
  23. trait = OpTrait::find_by_typeinfo(OprAttr::typeinfo());
  24. }
  25. mgb_assert(trait);
  26. return trait->make_from_op_node(node);
  27. }
  28. SmallVector<TensorPtr> OpDef::apply_on_physical_tensor(
  29. const OpDef& def,
  30. const SmallVector<TensorPtr>& inputs) {
  31. return def.trait()->apply_on_physical_tensor(def, inputs);
  32. }
  33. void OpDef::exec(
  34. const OpDef& def,
  35. const SmallVector<TensorPtr>& inputs,
  36. const SmallVector<TensorPtr>& outputs) {
  37. def.trait()->exec(def, inputs, outputs);
  38. }
  39. cg::OperatorNodeBase* OpDef::apply_on_var_node(
  40. const OpDef& def,
  41. const VarNodeArray& inputs) {
  42. return def.trait()->apply_on_var_node(def, inputs);
  43. }
  44. SmallVector<LogicalTensorDesc> OpDef::infer_output_attrs_fallible(
  45. const OpDef& def,
  46. const SmallVector<LogicalTensorDesc>& inputs) {
  47. return def.trait()->infer_output_attrs_fallible(def, inputs);
  48. }
  49. SmallVector<LogicalTensorDesc> OpDef::infer_output_attrs(
  50. const OpDef& def,
  51. const SmallVector<TensorPtr>& inputs) {
  52. return def.trait()->infer_output_attrs(def, inputs);
  53. }
  54. BackwardGraphResult OpDef::make_backward_graph(
  55. const OpDef& def,
  56. const SmallVector<LogicalTensorDesc>& inputs,
  57. const SmallVector<bool>& input_requires_grad,
  58. const SmallVector<bool>& output_has_grad) {
  59. return def.trait()->make_backward_graph(def, inputs, input_requires_grad, output_has_grad);
  60. }
  61. const OpTrait* OpDef::trait() const {
  62. if (!m_trait) {
  63. m_trait = OpTrait::find_by_typeinfo(dyn_typeinfo());
  64. mgb_throw_if(!m_trait, MegBrainError,
  65. "can not find op_trait by %s", dyn_typeinfo()->name);
  66. }
  67. return m_trait;
  68. }
  69. } // namespace imperative
  70. } // namespace mgb
  71. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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