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.

module.cpp 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * \file imperative/python/src/module.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 <pybind11/eval.h>
  12. #define DO_IMPORT_ARRAY
  13. #include "./numpy_dtypes.h"
  14. #include "./helper.h"
  15. #include "./common.h"
  16. #include "./utils.h"
  17. #include "./imperative_rt.h"
  18. #include "./graph_rt.h"
  19. #include "./ops.h"
  20. #include "./tensor.h"
  21. namespace py = pybind11;
  22. using namespace mgb::imperative::python;
  23. #ifndef MODULE_NAME
  24. #define MODULE_NAME imperative_rt
  25. #endif
  26. PYBIND11_MODULE(MODULE_NAME, m) {
  27. // initialize numpy
  28. if ([]() {import_array1(1); return 0;}()) {
  29. throw py::error_already_set();
  30. }
  31. py::module::import("sys").attr("modules")[m.attr("__name__")] = m;
  32. m.attr("__package__") = m.attr("__name__");
  33. m.attr("__builtins__") = py::module::import("builtins");
  34. auto atexit = py::module::import("atexit");
  35. atexit.attr("register")(py::cpp_function([]() {
  36. py::gil_scoped_release _;
  37. py_task_q.wait_all_task_finish();
  38. }));
  39. auto common = submodule(m, "common");
  40. auto utils = submodule(m, "utils");
  41. auto imperative = submodule(m, "imperative");
  42. auto graph = submodule(m, "graph");
  43. auto ops = submodule(m, "ops");
  44. init_common(common);
  45. init_utils(utils);
  46. init_imperative_rt(imperative);
  47. init_graph_rt(graph);
  48. init_ops(ops);
  49. py::exec(R"(
  50. from .common import *
  51. from .utils import *
  52. from .imperative import *
  53. from .graph import *
  54. from .ops import OpDef
  55. )",
  56. py::getattr(m, "__dict__"));
  57. init_tensor(submodule(m, "core2"));
  58. }

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