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.

common.cpp 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * \file imperative/python/src/common.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 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 "./common.h"
  12. #include <pybind11/operators.h>
  13. #include "megbrain/comp_node.h"
  14. #include "megbrain/graph.h"
  15. #include "megbrain/imperative/physical_tensor.h"
  16. #include "./numpy_dtypes.h"
  17. #include "./helper.h"
  18. namespace py = pybind11;
  19. using namespace mgb;
  20. using namespace imperative;
  21. namespace {
  22. template<typename XTensorND>
  23. auto def_TensorND(py::object parent, const char* name) {
  24. return py::class_<XTensorND>(parent, name)
  25. .def_property_readonly("shape", py::overload_cast<>(&XTensorND::shape, py::const_))
  26. .def_property_readonly("dtype", py::overload_cast<>(&XTensorND::dtype, py::const_))
  27. .def_property_readonly("comp_node", py::overload_cast<>(&XTensorND::comp_node, py::const_))
  28. .def("copy_from", &XTensorND::template copy_from<DeviceTensorStorage>)
  29. .def("copy_from", &XTensorND::template copy_from<HostTensorStorage>)
  30. .def("copy_from_fixlayout", py::overload_cast<const DeviceTensorND&>(
  31. &XTensorND::template copy_from_fixlayout<DeviceTensorStorage>))
  32. .def("copy_from_fixlayout", py::overload_cast<const HostTensorND&>(
  33. &XTensorND::template copy_from_fixlayout<HostTensorStorage>));
  34. }
  35. std::string default_device = "xpux";
  36. } // namespace
  37. void set_default_device(const std::string &device) {
  38. default_device = device;
  39. }
  40. std::string get_default_device() {
  41. return default_device;
  42. }
  43. void init_common(py::module m) {
  44. auto&& PyCompNode = py::class_<CompNode>(m, "CompNode")
  45. .def(py::init())
  46. .def(py::init(py::overload_cast<const std::string&>(&CompNode::load)))
  47. .def_property_readonly("logical_name", [](const CompNode& cn) {
  48. return cn.to_string_logical();
  49. })
  50. .def_property_readonly("get_mem_status_bytes", [](const CompNode& cn) {
  51. return cn.get_mem_status_bytes();
  52. })
  53. .def("create_event", &CompNode::create_event, py::arg("flags") = 0ul)
  54. .def_static("_set_default_device", &set_default_device)
  55. .def_static("_get_default_device", &get_default_device)
  56. .def("__str__", &CompNode::to_string_logical)
  57. .def("__repr__", [](const CompNode& cn) {
  58. return mgb::ssprintf("CompNode(\"%s\" from \"%s\")",
  59. cn.to_string_physical().c_str(),
  60. cn.to_string_logical().c_str());
  61. })
  62. .def_static("_sync_all", &CompNode::sync_all)
  63. .def(py::self == py::self)
  64. .def_static("_get_device_count", &CompNode::get_device_count,
  65. "Get total number of specific devices on this system")
  66. .def(py::pickle(
  67. [](const CompNode& cn) {
  68. return py::str(cn.to_string_logical());
  69. },
  70. [](py::str cn) {
  71. return CompNode::load(cn);
  72. }));
  73. py::class_<CompNode::Event, std::shared_ptr<CompNode::Event>>(PyCompNode, "Event")
  74. .def("record", &CompNode::Event::record)
  75. .def("wait", &CompNode::Event::host_wait);
  76. py::implicitly_convertible<std::string, CompNode>();
  77. def_TensorND<DeviceTensorND>(m, "DeviceTensorND")
  78. .def("numpy", [](const DeviceTensorND& self) {
  79. HostTensorND hv;
  80. hv.copy_from(self).sync();
  81. return py::handle(npy::ndarray_from_tensor(hv, npy::ShareType::TRY_SHARE));
  82. });
  83. def_TensorND<HostTensorND>(m, "HostTensorND")
  84. .def(py::init([](py::array data, CompNode cn, DType dtype) {
  85. if (!cn.valid()) {
  86. throw py::type_error("device must not be None");
  87. }
  88. return npy::np2tensor(data.ptr(), npy::Meth::borrow(cn), dtype);
  89. }))
  90. .def("numpy", [](const HostTensorND& self) {
  91. return py::reinterpret_steal<py::object>(npy::ndarray_from_tensor(self, npy::ShareType::TRY_SHARE));
  92. });
  93. py::class_<cg::OperatorNodeConfig>(m, "OperatorNodeConfig")
  94. .def(py::init())
  95. .def_property("name",
  96. [](const OperatorNodeConfig& config) -> py::object {
  97. auto name = config.name();
  98. if (name.valid()) {
  99. return py::str(name.val());
  100. } else {
  101. return py::none();
  102. }
  103. },
  104. [](OperatorNodeConfig& config, std::string name){
  105. config.name(std::move(name));
  106. })
  107. .def_property("dtype",
  108. [](const OperatorNodeConfig& config) {
  109. return config.output_dtype();
  110. },
  111. [](OperatorNodeConfig& config, DType dtype) {
  112. config.output_dtype(dtype);
  113. })
  114. .def_property("comp_node_arr",
  115. [](const OperatorNodeConfig& config) -> py::tuple {
  116. auto arr = config.comp_node();
  117. std::vector<CompNode> tmp(arr.begin(), arr.end());
  118. return py::cast(tmp);
  119. },
  120. [](OperatorNodeConfig& config, std::vector<CompNode> cns) {
  121. config.comp_node_arr({cns.begin(), cns.end()});
  122. })
  123. .def_property("comp_node",
  124. [](const OperatorNodeConfig& config) {
  125. auto arr = config.comp_node();
  126. if (arr.size() != 1) {
  127. throw py::value_error("invalid number of comp_node");
  128. }
  129. return arr[0];
  130. },
  131. [](OperatorNodeConfig& config, CompNode cn) {
  132. OperatorNodeConfig::CompNodeArray arr{cn};
  133. config.comp_node_arr(arr);
  134. });
  135. py::class_<LogicalTensorDesc>(m, "TensorAttr")
  136. .def(py::init())
  137. .def(py::init([](const TensorShape& shape, const DType& dtype, const CompNode& comp_node){
  138. return LogicalTensorDesc{TensorLayout{shape, dtype}, comp_node};
  139. }))
  140. .def_property("shape",
  141. [](const LogicalTensorDesc& desc) {
  142. return static_cast<TensorShape>(desc.layout);
  143. },
  144. [](LogicalTensorDesc& desc, TensorShape shape) {
  145. })
  146. .def_property("dtype",
  147. [](const LogicalTensorDesc& desc) {
  148. return desc.layout.dtype;
  149. },
  150. [](LogicalTensorDesc& desc, DType dtype) {
  151. desc.layout.dtype = dtype;
  152. })
  153. .def_readwrite("comp_node", &LogicalTensorDesc::comp_node);
  154. py::enum_<CompNode::DeviceType>(m, "DeviceType")
  155. .value("UNSPEC", CompNode::DeviceType::UNSPEC)
  156. .value("CUDA", CompNode::DeviceType::CUDA)
  157. .value("CPU", CompNode::DeviceType::CPU)
  158. .value("CAMBRICON", CompNode::DeviceType::CAMBRICON)
  159. .value("ATLAS", CompNode::DeviceType::ATLAS)
  160. .value("MULTITHREAD", CompNode::DeviceType::MULTITHREAD)
  161. .value("MAX_DEVICE_ID", CompNode::DeviceType::MAX_DEVICE_ID);
  162. m.def("set_prealloc_config", &CompNode::set_prealloc_config,
  163. "specifies how to pre-allocate from raw dev allocator");
  164. m.def("what_is_xpu", []{
  165. return CompNode::Locator::parse("xpux").to_physical().type;
  166. });
  167. init_npy_num_bfloat16(m);
  168. init_npy_num_intbx(m);
  169. init_dtypes(m);
  170. }

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