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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. Copyright 2017 Leon Merten Lohse
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. /*
  20. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  21. *
  22. * Copyright (c) 2020-2021 Megvii Inc. All rights reserved.
  23. *
  24. * Unless required by applicable law or agreed to in writing,
  25. * software distributed under the License is distributed on an
  26. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
  27. * implied.
  28. */
  29. #ifndef NPY_H
  30. #define NPY_H
  31. #include <algorithm>
  32. #include <complex>
  33. #include <cstdint>
  34. #include <cstring>
  35. #include <fstream>
  36. #include <iostream>
  37. #include <regex>
  38. #include <sstream>
  39. #include <stdexcept>
  40. #include <string>
  41. #include <unordered_map>
  42. #include <vector>
  43. namespace npy {
  44. /* Compile-time test for byte order.
  45. If your compiler does not define these per default, you may want to define
  46. one of these constants manually.
  47. Defaults to little endian order. */
  48. #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
  49. defined(__BIG_ENDIAN__) || defined(__ARMEB__) || \
  50. defined(__THUMBEB__) || defined(__AARCH64EB__) || defined(_MIBSEB) || \
  51. defined(__MIBSEB) || defined(__MIBSEB__)
  52. const bool big_endian = true;
  53. #else
  54. const bool big_endian = false;
  55. #endif
  56. const char magic_string[] = "\x93NUMPY";
  57. const size_t magic_string_length = 6;
  58. const char little_endian_char = '<';
  59. const char big_endian_char = '>';
  60. const char no_endian_char = '|';
  61. constexpr char host_endian_char =
  62. (big_endian ? big_endian_char : little_endian_char);
  63. /* npy array length */
  64. typedef unsigned long int ndarray_len_t;
  65. inline void write_magic(std::ostream& ostream, unsigned char v_major = 1,
  66. unsigned char v_minor = 0) {
  67. ostream.write(magic_string, magic_string_length);
  68. ostream.put(v_major);
  69. ostream.put(v_minor);
  70. }
  71. inline void read_magic(std::istream& istream, unsigned char& v_major,
  72. unsigned char& v_minor) {
  73. char buf[magic_string_length + 2];
  74. istream.read(buf, magic_string_length + 2);
  75. if (!istream) {
  76. fprintf(stderr, "io error: failed reading file");
  77. }
  78. if (0 != std::memcmp(buf, magic_string, magic_string_length)) {
  79. fprintf(stderr, "this file does not have a valid npy format.");
  80. }
  81. v_major = buf[magic_string_length];
  82. v_minor = buf[magic_string_length + 1];
  83. }
  84. // typestring magic
  85. struct Typestring {
  86. private:
  87. char c_endian;
  88. char c_type;
  89. int len;
  90. public:
  91. inline std::string str() {
  92. const size_t max_buflen = 16;
  93. char buf[max_buflen];
  94. std::sprintf(buf, "%c%c%u", c_endian, c_type, len);
  95. return std::string(buf);
  96. }
  97. Typestring(const std::vector<float>&)
  98. : c_endian{host_endian_char}, c_type{'f'}, len{sizeof(float)} {}
  99. Typestring(const std::vector<double>&)
  100. : c_endian{host_endian_char}, c_type{'f'}, len{sizeof(double)} {}
  101. Typestring(const std::vector<long double>&)
  102. : c_endian{host_endian_char},
  103. c_type{'f'},
  104. len{sizeof(long double)} {}
  105. Typestring(const std::vector<char>&)
  106. : c_endian{no_endian_char}, c_type{'i'}, len{sizeof(char)} {}
  107. Typestring(const std::vector<short>&)
  108. : c_endian{host_endian_char}, c_type{'i'}, len{sizeof(short)} {}
  109. Typestring(const std::vector<int>&)
  110. : c_endian{host_endian_char}, c_type{'i'}, len{sizeof(int)} {}
  111. Typestring(const std::vector<long>&)
  112. : c_endian{host_endian_char}, c_type{'i'}, len{sizeof(long)} {}
  113. Typestring(const std::vector<long long>&)
  114. : c_endian{host_endian_char}, c_type{'i'}, len{sizeof(long long)} {}
  115. Typestring(const std::vector<unsigned char>&)
  116. : c_endian{no_endian_char},
  117. c_type{'u'},
  118. len{sizeof(unsigned char)} {}
  119. Typestring(const std::vector<unsigned short>&)
  120. : c_endian{host_endian_char},
  121. c_type{'u'},
  122. len{sizeof(unsigned short)} {}
  123. Typestring(const std::vector<unsigned int>&)
  124. : c_endian{host_endian_char},
  125. c_type{'u'},
  126. len{sizeof(unsigned int)} {}
  127. Typestring(const std::vector<unsigned long>&)
  128. : c_endian{host_endian_char},
  129. c_type{'u'},
  130. len{sizeof(unsigned long)} {}
  131. Typestring(const std::vector<unsigned long long>&)
  132. : c_endian{host_endian_char},
  133. c_type{'u'},
  134. len{sizeof(unsigned long long)} {}
  135. Typestring(const std::vector<std::complex<float>>&)
  136. : c_endian{host_endian_char},
  137. c_type{'c'},
  138. len{sizeof(std::complex<float>)} {}
  139. Typestring(const std::vector<std::complex<double>>&)
  140. : c_endian{host_endian_char},
  141. c_type{'c'},
  142. len{sizeof(std::complex<double>)} {}
  143. Typestring(const std::vector<std::complex<long double>>&)
  144. : c_endian{host_endian_char},
  145. c_type{'c'},
  146. len{sizeof(std::complex<long double>)} {}
  147. };
  148. inline void parse_typestring(std::string typestring) {
  149. std::regex re("'([<>|])([ifuc])(\\d+)'");
  150. std::smatch sm;
  151. std::regex_match(typestring, sm, re);
  152. if (sm.size() != 4) {
  153. fprintf(stderr, "invalid typestring");
  154. }
  155. }
  156. namespace pyparse {
  157. /**
  158. Removes leading and trailing whitespaces
  159. */
  160. inline std::string trim(const std::string& str) {
  161. const std::string whitespace = " \t";
  162. auto begin = str.find_first_not_of(whitespace);
  163. if (begin == std::string::npos)
  164. return "";
  165. auto end = str.find_last_not_of(whitespace);
  166. return str.substr(begin, end - begin + 1);
  167. }
  168. inline std::string get_value_from_map(const std::string& mapstr) {
  169. size_t sep_pos = mapstr.find_first_of(":");
  170. if (sep_pos == std::string::npos)
  171. return "";
  172. std::string tmp = mapstr.substr(sep_pos + 1);
  173. return trim(tmp);
  174. }
  175. /**
  176. Parses the string representation of a Python dict
  177. The keys need to be known and may not appear anywhere else in the data.
  178. */
  179. inline std::unordered_map<std::string, std::string> parse_dict(
  180. std::string in, std::vector<std::string>& keys) {
  181. std::unordered_map<std::string, std::string> map;
  182. if (keys.size() == 0)
  183. return map;
  184. in = trim(in);
  185. // unwrap dictionary
  186. if ((in.front() == '{') && (in.back() == '}'))
  187. in = in.substr(1, in.length() - 2);
  188. else {
  189. fprintf(stderr, "Not a Python dictionary.");
  190. }
  191. std::vector<std::pair<size_t, std::string>> positions;
  192. for (auto const& value : keys) {
  193. size_t pos = in.find("'" + value + "'");
  194. if (pos == std::string::npos) {
  195. fprintf(stderr, "Missing %s key.", value.c_str());
  196. }
  197. std::pair<size_t, std::string> position_pair{pos, value};
  198. positions.push_back(position_pair);
  199. }
  200. // sort by position in dict
  201. std::sort(positions.begin(), positions.end());
  202. for (size_t i = 0; i < positions.size(); ++i) {
  203. std::string raw_value;
  204. size_t begin{positions[i].first};
  205. size_t end{std::string::npos};
  206. std::string key = positions[i].second;
  207. if (i + 1 < positions.size())
  208. end = positions[i + 1].first;
  209. raw_value = in.substr(begin, end - begin);
  210. raw_value = trim(raw_value);
  211. if (raw_value.back() == ',')
  212. raw_value.pop_back();
  213. map[key] = get_value_from_map(raw_value);
  214. }
  215. return map;
  216. }
  217. /**
  218. Parses the string representation of a Python boolean
  219. */
  220. inline bool parse_bool(const std::string& in) {
  221. if (in == "True")
  222. return true;
  223. if (in == "False")
  224. return false;
  225. fprintf(stderr, "Invalid python boolan.");
  226. return false;
  227. }
  228. /**
  229. Parses the string representation of a Python str
  230. */
  231. inline std::string parse_str(const std::string& in) {
  232. if ((in.front() == '\'') && (in.back() == '\''))
  233. return in.substr(1, in.length() - 2);
  234. fprintf(stderr, "Invalid python string.");
  235. return "";
  236. }
  237. /**
  238. Parses the string represenatation of a Python tuple into a vector of its items
  239. */
  240. inline std::vector<std::string> parse_tuple(std::string in) {
  241. std::vector<std::string> v;
  242. const char seperator = ',';
  243. in = trim(in);
  244. if ((in.front() == '(') && (in.back() == ')'))
  245. in = in.substr(1, in.length() - 2);
  246. else {
  247. fprintf(stderr, "Invalid Python tuple.");
  248. }
  249. std::istringstream iss(in);
  250. for (std::string token; std::getline(iss, token, seperator);) {
  251. v.push_back(token);
  252. }
  253. return v;
  254. }
  255. template <typename T>
  256. inline std::string write_tuple(const std::vector<T>& v) {
  257. if (v.size() == 0)
  258. return "";
  259. std::ostringstream ss;
  260. if (v.size() == 1) {
  261. ss << "(" << v.front() << ",)";
  262. } else {
  263. const std::string delimiter = ", ";
  264. // v.size() > 1
  265. ss << "(";
  266. std::copy(v.begin(), v.end() - 1,
  267. std::ostream_iterator<T>(ss, delimiter.c_str()));
  268. ss << v.back();
  269. ss << ")";
  270. }
  271. return ss.str();
  272. }
  273. inline std::string write_boolean(bool b) {
  274. if (b)
  275. return "True";
  276. else
  277. return "False";
  278. }
  279. } // namespace pyparse
  280. inline void parse_header(std::string header, std::string& descr) {
  281. /*
  282. The first 6 bytes are a magic string: exactly "x93NUMPY".
  283. The next 1 byte is an unsigned byte: the major version number of the file
  284. format, e.g. x01. The next 1 byte is an unsigned byte: the minor version
  285. number of the file format, e.g. x00. Note: the version of the file format
  286. is not tied to the version of the numpy package. The next 2 bytes form a
  287. little-endian unsigned short int: the length of the header data
  288. HEADER_LEN. The next HEADER_LEN bytes form the header data describing the
  289. array's format. It is an ASCII string which contains a Python literal
  290. expression of a dictionary. It is terminated by a newline ('n') and
  291. padded with spaces
  292. ('x20') to make the total length of the magic string + 4 + HEADER_LEN be
  293. evenly divisible by 16 for alignment purposes. The dictionary contains
  294. three keys:
  295. "descr" : dtype.descr
  296. An object that can be passed as an argument to the numpy.dtype()
  297. constructor to create the array's dtype. For repeatability and
  298. readability, this dictionary is formatted using pprint.pformat() so the
  299. keys are in alphabetic order.
  300. */
  301. // remove trailing newline
  302. if (header.back() != '\n')
  303. fprintf(stderr, "invalid header");
  304. header.pop_back();
  305. // parse the dictionary
  306. std::vector<std::string> keys{"descr"};
  307. auto dict_map = npy::pyparse::parse_dict(header, keys);
  308. if (dict_map.size() == 0)
  309. fprintf(stderr, "invalid dictionary in header");
  310. std::string descr_s = dict_map["descr"];
  311. parse_typestring(descr_s);
  312. // remove
  313. descr = npy::pyparse::parse_str(descr_s);
  314. return;
  315. }
  316. inline void parse_header(std::string header, std::string& descr,
  317. bool& fortran_order,
  318. std::vector<ndarray_len_t>& shape) {
  319. /*
  320. The first 6 bytes are a magic string: exactly "x93NUMPY".
  321. The next 1 byte is an unsigned byte: the major version number of the file
  322. format, e.g. x01. The next 1 byte is an unsigned byte: the minor version
  323. number of the file format, e.g. x00. Note: the version of the file format
  324. is not tied to the version of the numpy package. The next 2 bytes form a
  325. little-endian unsigned short int: the length of the header data
  326. HEADER_LEN. The next HEADER_LEN bytes form the header data describing the
  327. array's format. It is an ASCII string which contains a Python literal
  328. expression of a dictionary. It is terminated by a newline ('n') and
  329. padded with spaces
  330. ('x20') to make the total length of the magic string + 4 + HEADER_LEN be
  331. evenly divisible by 16 for alignment purposes. The dictionary contains
  332. three keys:
  333. "descr" : dtype.descr
  334. An object that can be passed as an argument to the numpy.dtype()
  335. constructor to create the array's dtype. "fortran_order" : bool Whether
  336. the array data is Fortran-contiguous or not. Since Fortran-contiguous
  337. arrays are a common form of non-C-contiguity, we allow them to be written
  338. directly to disk for efficiency. "shape" : tuple of int The shape of the
  339. array. For repeatability and readability, this dictionary is formatted
  340. using pprint.pformat() so the keys are in alphabetic order.
  341. */
  342. // remove trailing newline
  343. if (header.back() != '\n')
  344. fprintf(stderr, "invalid header");
  345. header.pop_back();
  346. // parse the dictionary
  347. std::vector<std::string> keys{"descr", "fortran_order", "shape"};
  348. auto dict_map = npy::pyparse::parse_dict(header, keys);
  349. if (dict_map.size() == 0)
  350. fprintf(stderr, "invalid dictionary in header");
  351. std::string descr_s = dict_map["descr"];
  352. std::string fortran_s = dict_map["fortran_order"];
  353. std::string shape_s = dict_map["shape"];
  354. // TODO: extract info from typestring
  355. parse_typestring(descr_s);
  356. // remove
  357. descr = npy::pyparse::parse_str(descr_s);
  358. // convert literal Python bool to C++ bool
  359. fortran_order = npy::pyparse::parse_bool(fortran_s);
  360. // parse the shape tuple
  361. auto shape_v = npy::pyparse::parse_tuple(shape_s);
  362. if (shape_v.size() == 0)
  363. fprintf(stderr, "invalid shape tuple in header");
  364. for (auto item : shape_v) {
  365. ndarray_len_t dim = static_cast<ndarray_len_t>(std::stoul(item));
  366. shape.push_back(dim);
  367. }
  368. }
  369. inline std::string write_header_dict(const std::string& descr,
  370. bool fortran_order,
  371. const std::vector<ndarray_len_t>& shape) {
  372. std::string s_fortran_order = npy::pyparse::write_boolean(fortran_order);
  373. std::string shape_s = npy::pyparse::write_tuple(shape);
  374. return "{'descr': '" + descr + "', 'fortran_order': " + s_fortran_order +
  375. ", 'shape': " + shape_s + ", }";
  376. }
  377. inline void write_header(std::ostream& out, const std::string& descr,
  378. bool fortran_order,
  379. const std::vector<ndarray_len_t>& shape_v) {
  380. std::string header_dict = write_header_dict(descr, fortran_order, shape_v);
  381. size_t length = magic_string_length + 2 + 2 + header_dict.length() + 1;
  382. unsigned char version[2] = {1, 0};
  383. if (length >= 255 * 255) {
  384. length = magic_string_length + 2 + 4 + header_dict.length() + 1;
  385. version[0] = 2;
  386. version[1] = 0;
  387. }
  388. size_t padding_len = 16 - length % 16;
  389. std::string padding(padding_len, ' ');
  390. // write magic
  391. write_magic(out, version[0], version[1]);
  392. // write header length
  393. if (version[0] == 1 && version[1] == 0) {
  394. char header_len_le16[2];
  395. uint16_t header_len = static_cast<uint16_t>(header_dict.length() +
  396. padding.length() + 1);
  397. header_len_le16[0] = (header_len >> 0) & 0xff;
  398. header_len_le16[1] = (header_len >> 8) & 0xff;
  399. out.write(reinterpret_cast<char*>(header_len_le16), 2);
  400. } else {
  401. char header_len_le32[4];
  402. uint32_t header_len = static_cast<uint32_t>(header_dict.length() +
  403. padding.length() + 1);
  404. header_len_le32[0] = (header_len >> 0) & 0xff;
  405. header_len_le32[1] = (header_len >> 8) & 0xff;
  406. header_len_le32[2] = (header_len >> 16) & 0xff;
  407. header_len_le32[3] = (header_len >> 24) & 0xff;
  408. out.write(reinterpret_cast<char*>(header_len_le32), 4);
  409. }
  410. out << header_dict << padding << '\n';
  411. }
  412. inline std::string read_header(std::istream& istream) {
  413. // check magic bytes an version number
  414. unsigned char v_major, v_minor;
  415. read_magic(istream, v_major, v_minor);
  416. uint32_t header_length = 0;
  417. if (v_major == 1 && v_minor == 0) {
  418. char header_len_le16[2];
  419. istream.read(header_len_le16, 2);
  420. header_length = (header_len_le16[0] << 0) | (header_len_le16[1] << 8);
  421. if ((magic_string_length + 2 + 2 + header_length) % 16 != 0) {
  422. // TODO: display warning
  423. }
  424. } else if (v_major == 2 && v_minor == 0) {
  425. char header_len_le32[4];
  426. istream.read(header_len_le32, 4);
  427. header_length = (header_len_le32[0] << 0) | (header_len_le32[1] << 8) |
  428. (header_len_le32[2] << 16) | (header_len_le32[3] << 24);
  429. if ((magic_string_length + 2 + 4 + header_length) % 16 != 0) {
  430. // TODO: display warning
  431. }
  432. } else {
  433. fprintf(stderr, "unsupported file format version");
  434. }
  435. auto buf_v = std::vector<char>();
  436. buf_v.reserve(header_length);
  437. istream.read(buf_v.data(), header_length);
  438. std::string header(buf_v.data(), header_length);
  439. return header;
  440. }
  441. inline ndarray_len_t comp_size(const std::vector<ndarray_len_t>& shape) {
  442. ndarray_len_t size = 1;
  443. for (ndarray_len_t i : shape)
  444. size *= i;
  445. return size;
  446. }
  447. template <typename Scalar>
  448. inline void SaveArrayAsNumpy(const std::string& filename, bool fortran_order,
  449. unsigned int n_dims, const unsigned long shape[],
  450. const std::vector<Scalar>& data) {
  451. Typestring typestring_o(data);
  452. std::string typestring = typestring_o.str();
  453. std::ofstream stream(filename, std::ofstream::binary);
  454. if (!stream) {
  455. fprintf(stderr, "io error: failed to open a file.");
  456. }
  457. std::vector<ndarray_len_t> shape_v(shape, shape + n_dims);
  458. write_header(stream, typestring, fortran_order, shape_v);
  459. auto size = static_cast<size_t>(comp_size(shape_v));
  460. stream.write(reinterpret_cast<const char*>(data.data()),
  461. sizeof(Scalar) * size);
  462. }
  463. template <typename Scalar>
  464. inline void LoadArrayFromNumpy(const std::string& filename,
  465. std::vector<unsigned long>& shape,
  466. std::vector<Scalar>& data) {
  467. bool fortran_order;
  468. LoadArrayFromNumpy<Scalar>(filename, shape, fortran_order, data);
  469. }
  470. template <typename Scalar>
  471. inline void LoadArrayFromNumpy(const std::string& filename,
  472. std::vector<unsigned long>& shape,
  473. bool& fortran_order, std::vector<Scalar>& data) {
  474. std::ifstream stream(filename, std::ifstream::binary);
  475. if (!stream) {
  476. fprintf(stderr, "io error: failed to open a file.");
  477. }
  478. std::string header = read_header(stream);
  479. // parse header
  480. std::string typestr;
  481. parse_header(header, typestr, fortran_order, shape);
  482. // check if the typestring matches the given one
  483. Typestring typestring_o{data};
  484. std::string expect_typestr = typestring_o.str();
  485. if (typestr != expect_typestr) {
  486. fprintf(stderr, "formatting error: typestrings not matching");
  487. }
  488. // compute the data size based on the shape
  489. auto size = static_cast<size_t>(comp_size(shape));
  490. data.resize(size);
  491. // read the data
  492. stream.read(reinterpret_cast<char*>(data.data()), sizeof(Scalar) * size);
  493. }
  494. inline void LoadArrayFromNumpy(const std::string& filename,
  495. std::string& type_str,
  496. std::vector<ndarray_len_t>& shape,
  497. std::vector<int8_t>& data) {
  498. std::ifstream stream(filename, std::ifstream::binary);
  499. if (!stream) {
  500. fprintf(stderr, "io error: failed to open a file.");
  501. }
  502. std::string header = read_header(stream);
  503. bool fortran_order;
  504. // parse header
  505. parse_header(header, type_str, fortran_order, shape);
  506. // check if the typestring matches the given one
  507. std::string size_str = type_str.substr(type_str.size() - 1);
  508. size_t elem_size = atoi(size_str.c_str());
  509. // compute the data size based on the shape
  510. auto byte_size = elem_size * static_cast<size_t>(comp_size(shape));
  511. data.resize(byte_size);
  512. // read the data
  513. stream.read(reinterpret_cast<char*>(data.data()), byte_size);
  514. }
  515. } // namespace npy
  516. #endif // NPY_H

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