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_observer.py 1.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import multiprocessing as mp
  2. import platform
  3. import numpy as np
  4. import pytest
  5. import megengine as mge
  6. import megengine.distributed as dist
  7. import megengine.quantization.observer as ob
  8. from megengine.distributed.helper import get_device_count_by_fork
  9. def test_min_max_observer():
  10. x = np.random.rand(3, 3, 3, 3).astype("float32")
  11. np_min, np_max = x.min(), x.max()
  12. x = mge.tensor(x)
  13. m = ob.MinMaxObserver()
  14. m(x)
  15. assert m.min_val == np_min and m.max_val == np_max
  16. @pytest.mark.skipif(
  17. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  18. )
  19. @pytest.mark.skipif(
  20. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  21. )
  22. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  23. @pytest.mark.isolated_distributed
  24. def test_sync_min_max_observer():
  25. x = np.random.rand(6, 3, 3, 3).astype("float32")
  26. np_min, np_max = x.min(), x.max()
  27. world_size = 2
  28. port = dist.get_free_ports(1)[0]
  29. server = dist.Server(port)
  30. def worker(rank, slc):
  31. dist.init_process_group("localhost", port, world_size, rank, rank)
  32. m = ob.SyncMinMaxObserver()
  33. y = mge.tensor(x[slc])
  34. m(y)
  35. assert m.min_val == np_min and m.max_val == np_max
  36. procs = []
  37. for rank in range(world_size):
  38. slc = slice(rank * 3, (rank + 1) * 3)
  39. p = mp.Process(target=worker, args=(rank, slc,), daemon=True)
  40. p.start()
  41. procs.append(p)
  42. for p in procs:
  43. p.join(20)
  44. assert p.exitcode == 0

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