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.

network_share_weights.cpp 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * \file example/network_share_weights.cpp
  3. *
  4. * This file is part of MegEngine, a deep learning framework developed by
  5. * Megvii.
  6. *
  7. * \copyright Copyright (c) 2020-2021 Megvii Inc. All rights reserved.
  8. */
  9. #include "../example.h"
  10. #if LITE_BUILD_WITH_MGE
  11. using namespace lite;
  12. using namespace example;
  13. bool lite::example::network_share_same_weights(const Args& args) {
  14. std::string network_path = args.model_path;
  15. std::string input_path = args.input_path;
  16. //! create and load the network
  17. std::shared_ptr<Network> network = std::make_shared<Network>();
  18. network->load_model(network_path);
  19. //! load a new network from the created network and share the same weights,
  20. Config config_new;
  21. config_new.options.const_shape = true;
  22. NetworkIO network_io_new;
  23. std::shared_ptr<Network> weight_shared_network =
  24. std::make_shared<Network>(config_new, network_io_new);
  25. Runtime::shared_weight_with_network(weight_shared_network, network);
  26. //! set input data to input tensor
  27. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  28. void* dst_ptr = input_tensor->get_memory_ptr();
  29. std::shared_ptr<Tensor> input_tensor2 =
  30. weight_shared_network->get_input_tensor(0);
  31. void* dst_ptr2 = input_tensor2->get_memory_ptr();
  32. //! copy or forward data to network
  33. size_t length = input_tensor->get_tensor_total_size_in_byte();
  34. auto src_tensor = parse_npy(input_path);
  35. void* src = src_tensor->get_memory_ptr();
  36. memcpy(dst_ptr, src, length);
  37. memcpy(dst_ptr2, src, length);
  38. //! forward
  39. network->forward();
  40. network->wait();
  41. weight_shared_network->forward();
  42. weight_shared_network->wait();
  43. //! get the output data or read tensor set in network_in
  44. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  45. std::shared_ptr<Tensor> output_tensor2 =
  46. weight_shared_network->get_output_tensor(0);
  47. void* out_data = output_tensor->get_memory_ptr();
  48. void* out_data2 = output_tensor2->get_memory_ptr();
  49. size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
  50. output_tensor->get_layout().get_elem_size();
  51. printf("length=%zu\n", length);
  52. float max = -1.0f;
  53. float sum = 0.0f;
  54. for (size_t i = 0; i < out_length; i++) {
  55. float data = static_cast<float*>(out_data)[i];
  56. float data2 = static_cast<float*>(out_data2)[i];
  57. if (data != data2) {
  58. printf("the result between the origin network and weight share "
  59. "netwrok is different.\n");
  60. }
  61. sum += data;
  62. if (max < data)
  63. max = data;
  64. }
  65. printf("max=%e, sum=%e\n", max, sum);
  66. return true;
  67. }
  68. #endif
  69. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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