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.

example.h 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * \file example/cpp_example/example.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. #pragma once
  12. #include <lite_build_config.h>
  13. #include "lite/global.h"
  14. #include "lite/network.h"
  15. #include "lite/tensor.h"
  16. #include "npy.h"
  17. #include <string.h>
  18. #include <memory>
  19. #include <unordered_map>
  20. #include <vector>
  21. namespace lite {
  22. namespace example {
  23. void set_cpu_affinity(const std::vector<int>& cpuset);
  24. struct Args {
  25. int args_parse_ret = 0;
  26. std::string example_name;
  27. std::string model_path;
  28. std::string input_path;
  29. std::string output_path;
  30. std::string loader_path;
  31. static Args from_argv(int argc, char** argv);
  32. };
  33. std::shared_ptr<Tensor> parse_npy(
  34. const std::string& path, LiteBackend backend = LiteBackend::LITE_DEFAULT);
  35. using ExampleFunc = std::function<bool(const Args&)>;
  36. using ExampleFuncMap = std::unordered_map<std::string, ExampleFunc>;
  37. ExampleFuncMap* get_example_function_map();
  38. bool register_example(std::string example_name, const ExampleFunc& fuction);
  39. template <int>
  40. struct Register;
  41. #if LITE_BUILD_WITH_MGE
  42. #if LITE_WITH_CUDA
  43. bool load_from_path_run_cuda(const Args& args);
  44. #endif
  45. bool basic_load_from_path(const Args& args);
  46. bool basic_load_from_path_with_loader(const Args& args);
  47. bool basic_load_from_memory(const Args& args);
  48. bool cpu_affinity(const Args& args);
  49. bool network_share_same_weights(const Args& args);
  50. bool reset_input(const Args& args);
  51. bool reset_input_output(const Args& args);
  52. bool config_user_allocator(const Args& args);
  53. bool register_cryption_method(const Args& args);
  54. bool update_cryption_key(const Args& args);
  55. bool async_forward(const Args& args);
  56. bool set_input_callback(const Args& arg);
  57. bool set_output_callback(const Args& arg);
  58. #if LITE_WITH_CUDA
  59. bool device_input(const Args& args);
  60. bool device_input_output(const Args& args);
  61. bool pinned_host_input(const Args& args);
  62. #endif
  63. #endif
  64. } // namespace example
  65. } // namespace lite
  66. #if LITE_BUILD_WITH_MGE
  67. bool basic_c_interface(const lite::example::Args& args);
  68. bool device_io_c_interface(const lite::example::Args& args);
  69. bool async_c_interface(const lite::example::Args& args);
  70. #endif
  71. #define CONCAT_IMPL(a, b) a##b
  72. #define MACRO_CONCAT(a, b) CONCAT_IMPL(a, b)
  73. #define REGIST_EXAMPLE(name_, func_) REGIST_EXAMPLE_WITH_NUM(__COUNTER__, name_, func_)
  74. #define REGIST_EXAMPLE_WITH_NUM(number_, name_, func_) \
  75. template <> \
  76. struct Register<number_> { \
  77. Register() { register_example(name_, func_); } \
  78. }; \
  79. namespace { \
  80. Register<number_> MACRO_CONCAT(example_function_, number_); \
  81. }
  82. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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