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.

reset_io.cpp 3.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * \file example/reset_io.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 "../example.h"
  12. #if LITE_BUILD_WITH_MGE
  13. using namespace lite;
  14. using namespace example;
  15. bool lite::example::reset_input(const Args& args) {
  16. std::string network_path = args.model_path;
  17. std::string input_path = args.input_path;
  18. lite::Config config;
  19. //! create and load the network
  20. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  21. network->load_model(network_path);
  22. //! set input data to input tensor
  23. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  24. auto layout = input_tensor->get_layout();
  25. auto src_tensor = parse_npy(input_path);
  26. void* src = src_tensor->get_memory_ptr();
  27. input_tensor->reset(src, layout);
  28. //! forward
  29. network->forward();
  30. network->wait();
  31. //! 6. get the output data or read tensor set in network_in
  32. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  33. void* out_data = output_tensor->get_memory_ptr();
  34. size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
  35. output_tensor->get_layout().get_elem_size();
  36. float max = -1.0f;
  37. float sum = 0.0f;
  38. for (size_t i = 0; i < out_length; i++) {
  39. float data = static_cast<float*>(out_data)[i];
  40. sum += data;
  41. if (max < data)
  42. max = data;
  43. }
  44. printf("max=%e, sum=%e\n", max, sum);
  45. return true;
  46. }
  47. bool lite::example::reset_input_output(const Args& args) {
  48. std::string network_path = args.model_path;
  49. std::string input_path = args.input_path;
  50. lite::Config config;
  51. //! create and load the network
  52. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  53. network->load_model(network_path);
  54. //! set input data to input tensor
  55. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  56. auto layout = input_tensor->get_layout();
  57. auto src_tensor = parse_npy(input_path);
  58. void* src = src_tensor->get_memory_ptr();
  59. input_tensor->reset(src, layout);
  60. //! set output ptr to store the network output
  61. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  62. auto result_tensor = std::make_shared<Tensor>(
  63. LiteDeviceType::LITE_CPU,
  64. Layout{{1, 1000}, 2, LiteDataType::LITE_FLOAT});
  65. void* out_data = result_tensor->get_memory_ptr();
  66. output_tensor->reset(out_data, result_tensor->get_layout());
  67. network->forward();
  68. network->wait();
  69. float max = -1.0f;
  70. float sum = 0.0f;
  71. for (size_t i = 0; i < 1000; i++) {
  72. float data = static_cast<float*>(out_data)[i];
  73. sum += data;
  74. if (max < data)
  75. max = data;
  76. }
  77. printf("max=%e, sum=%e\n", max, sum);
  78. return true;
  79. }
  80. #endif
  81. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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