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.

test_distributed.py 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import multiprocessing as mp
  10. import platform
  11. import subprocess
  12. import sys
  13. import numpy as np
  14. import pytest
  15. def worker(master_ip, master_port, world_size, rank, dev, trace):
  16. import megengine.distributed as dist
  17. import megengine.functional as F
  18. from megengine import is_cuda_available
  19. from megengine import jit
  20. from megengine.module import Linear, Module
  21. from megengine.optimizer import SGD
  22. if not is_cuda_available():
  23. return
  24. class MLP(Module):
  25. def __init__(self):
  26. super().__init__()
  27. self.fc0 = Linear(3 * 224 * 224, 500)
  28. self.fc1 = Linear(500, 10)
  29. def forward(self, x):
  30. x = self.fc0(x)
  31. x = F.relu(x)
  32. x = self.fc1(x)
  33. return x
  34. dist.init_process_group(
  35. master_ip=master_ip, master_port=3456, world_size=world_size, rank=rank, dev=dev
  36. )
  37. net = MLP()
  38. opt = SGD(net.parameters(requires_grad=True), lr=0.02)
  39. data = np.random.random((64, 3 * 224 * 224)).astype(np.float32)
  40. label = np.random.randint(0, 10, size=(64,)).astype(np.int32)
  41. jit.trace.enabled = trace
  42. @jit.trace()
  43. def train_func(data, label):
  44. pred = net(data)
  45. loss = F.cross_entropy_with_softmax(pred, label)
  46. opt.backward(loss)
  47. return loss
  48. for i in range(5):
  49. opt.zero_grad()
  50. loss = train_func(data, label)
  51. opt.step()
  52. def start_workers(worker, world_size, trace=False):
  53. def run_subproc(rank):
  54. cmd = "from test.integration.test_distributed import worker\n"
  55. cmd += "worker('localhost', 3456, {}, {}, {}, {})".format(
  56. world_size, rank, rank, "True" if trace else "False"
  57. )
  58. cmd = [sys.executable, "-c", cmd]
  59. ret = subprocess.run(
  60. cmd, stdout=sys.stdout, stderr=sys.stderr, universal_newlines=True
  61. )
  62. assert ret.returncode == 0, "subprocess failed"
  63. procs = []
  64. for rank in range(world_size):
  65. p = mp.Process(target=run_subproc, args=(rank,))
  66. p.start()
  67. procs.append(p)
  68. for p in procs:
  69. p.join()
  70. assert p.exitcode == 0
  71. @pytest.mark.skipif(
  72. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  73. )
  74. def test_distributed():
  75. start_workers(worker, 2, trace=True)
  76. start_workers(worker, 2, trace=False)

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