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.

memory_swap.cpp 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * \file src/core/test/memory_swap.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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 "megbrain/test/helper.h"
  12. #include "megbrain/opr/basic_arith_wrapper.h"
  13. #include "megbrain/opr/blas.h"
  14. #include "megbrain/opr/dnn/convolution.h"
  15. #include "megbrain/opr/io.h"
  16. using namespace mgb;
  17. using Elemwise = opr::Elemwise;
  18. using Mode = Elemwise::Mode;
  19. #if MGB_ENABLE_MEMORY_SWAP
  20. auto run = [](const int flag) {
  21. auto KEY = "MGB_MEMORY_SWAP_PARAM_BUCKET_IMPLEMENT";
  22. auto old_value = getenv(KEY);
  23. if (flag)
  24. setenv(KEY, "1", 1);
  25. else
  26. setenv(KEY, "0", 1);
  27. HostTensorGenerator<> gen_;
  28. auto gen = [&](const TensorShape& shp) { return gen_(shp, "gpu0"); };
  29. constexpr size_t batch_size = 5, C = 8, H = 100, W = 128;
  30. constexpr size_t limit = 200;
  31. auto host_data = gen({batch_size, C, H, W});
  32. auto graph = ComputingGraph::make();
  33. SymbolVarArray kernels;
  34. SymbolVarArray conv_res;
  35. auto data = opr::Host2DeviceCopy::make(*graph, host_data).rename("data");
  36. conv_res.push_back(data);
  37. size_t out_chl = host_data->shape(1), layer_count = 0;
  38. auto add_layer = [&](size_t oc, size_t kernal_shape, size_t padding) {
  39. gen_.std(sqrt(2.0 / (out_chl * kernal_shape * kernal_shape)));
  40. auto host_kern = gen({oc, out_chl, kernal_shape, kernal_shape});
  41. auto dev_kern = std::make_shared<DeviceTensorND>();
  42. dev_kern->copy_from(*host_kern);
  43. auto current_param = opr::Convolution::Param();
  44. kernels.emplace_back(opr::SharedDeviceTensor::make(*graph, dev_kern));
  45. current_param.pad_h = current_param.pad_w = padding;
  46. conv_res.push_back(opr::relu(opr::Convolution::make(
  47. conv_res[layer_count],
  48. kernels.back().rename(ssprintf("param%zu", layer_count)),
  49. current_param)));
  50. layer_count++;
  51. out_chl = oc;
  52. };
  53. for (size_t i = 1; i <= limit; ++i)
  54. add_layer(30, 5, 2);
  55. auto loss = opr::Dot::make(conv_res[limit].flatten(),
  56. conv_res[limit].flatten());
  57. std::vector<HostTensorND> grad_kernels_get(kernels.size());
  58. ComputingGraph::OutputSpec out_spec;
  59. for (size_t i = 0; i < kernels.size(); ++i) {
  60. out_spec.emplace_back(make_callback_copy(cg::grad(loss, kernels[i]),
  61. grad_kernels_get[i]));
  62. }
  63. std::vector<HostTensorND> grad_kernels_expect(grad_kernels_get.size());
  64. for (bool swap : {false, true}) {
  65. graph->options().enable_memory_swap = swap;
  66. auto func = graph->compile(out_spec);
  67. func->execute();
  68. if (!swap) {
  69. for (size_t i = 0; i < grad_kernels_get.size(); ++i)
  70. grad_kernels_expect[i].copy_from(grad_kernels_get[i]);
  71. }
  72. }
  73. for (size_t i = 0; i < grad_kernels_get.size(); ++i)
  74. MGB_ASSERT_TENSOR_NEAR(grad_kernels_get[i], grad_kernels_expect[i],
  75. 1e-3);
  76. if (old_value) {
  77. setenv(KEY, old_value, 1);
  78. } else {
  79. unsetenv(KEY);
  80. }
  81. };
  82. TEST(TestMemorySwap, FullConvSerial) {
  83. REQUIRE_GPU(1);
  84. run(0);
  85. }
  86. TEST(TestMemorySwap, FullConvParallel) {
  87. REQUIRE_GPU(1);
  88. run(0);
  89. }
  90. #endif // MGB_ENABLE_MEMORY_SWAP
  91. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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