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.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "./dispatcher.h"
  21. namespace py = pybind11;
  22. #ifndef MODULE_NAME
  23. #define MODULE_NAME imperative_rt
  24. #endif
  25. PYBIND11_MODULE(MODULE_NAME, m) {
  26. // initialize numpy
  27. if ([]() {import_array1(1); return 0;}()) {
  28. throw py::error_already_set();
  29. }
  30. py::module::import("sys").attr("modules")[m.attr("__name__")] = m;
  31. m.attr("__package__") = m.attr("__name__");
  32. m.attr("__builtins__") = py::module::import("builtins");
  33. auto atexit = py::module::import("atexit");
  34. atexit.attr("register")(py::cpp_function([]() {
  35. py::gil_scoped_release _;
  36. py_task_q.wait_all_task_finish();
  37. }));
  38. auto common = submodule(m, "common");
  39. auto utils = submodule(m, "utils");
  40. auto imperative = submodule(m, "imperative");
  41. auto graph = submodule(m, "graph");
  42. auto ops = submodule(m, "ops");
  43. init_common(common);
  44. init_utils(utils);
  45. init_imperative_rt(imperative);
  46. init_graph_rt(graph);
  47. init_ops(ops);
  48. py::exec(R"(
  49. from .common import *
  50. from .utils import *
  51. from .imperative import *
  52. from .graph import *
  53. )",
  54. py::getattr(m, "__dict__"));
  55. init_dispatcher(submodule(m, "dispatcher"));
  56. }

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