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.

grad_override.cpp 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * \file imperative/python/src/grad_override.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 "./grad.h"
  12. #include "megbrain/imperative/ops/autogen.h"
  13. namespace mgb::imperative::python {
  14. namespace {
  15. std::shared_ptr<Tensor> get_shape(Tensor* x) {
  16. static auto op = GetVarShape::make();
  17. return python::apply(op, x)[0];
  18. }
  19. std::shared_ptr<Tensor> reduce_to(Tensor* x, Tensor* s) {
  20. static auto op = Reduce::make();
  21. return python::apply(op, x, s)[0];
  22. }
  23. apply_result_t elemwise_grad_rule(ApplyContext& ctx, CustomBackward::Maker& maker) {
  24. auto& op = ctx.op->cast_final_safe<Elemwise>();
  25. if (op.mode == Elemwise::Mode::ADD) {
  26. mgb_assert(ctx.nargs == 2);
  27. std::array<std::shared_ptr<Tensor>, 2> input_shapes;
  28. for (size_t i = 0; i < 2; ++i) {
  29. if (input_requires_grad(ctx, i)) {
  30. input_shapes[i] = get_shape(ctx.args[i]);
  31. }
  32. }
  33. maker.output_size(1).output_captured(0, false);
  34. maker.backward([shapes=std::move(input_shapes)](BackwardContext&, Tensor*const* grads, size_t ngrads) {
  35. mgb_assert(ngrads == 1);
  36. Tensor* grad = grads[0];
  37. apply_result_t ret(2);
  38. for (size_t i = 0; i < 2; ++i) {
  39. if (shapes[i]) {
  40. ret[i] = reduce_to(grad, shapes[i].get());
  41. }
  42. }
  43. return ret;
  44. });
  45. return apply(ctx);
  46. }
  47. throw GradRuleFallback();
  48. }
  49. struct Init {
  50. Init() {
  51. auto& reg = grad_rule_registry();
  52. reg.emplace(Elemwise::typeinfo(), elemwise_grad_rule);
  53. }
  54. } _;
  55. } // namespace
  56. } // namespace mgb::imperative::python

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