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

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

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