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.

env.py 2.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. # The MIT License (MIT)
  3. #
  4. # Copyright (c) 2018 Laurent LAPORTE
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in all
  14. # copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. import contextlib
  24. import os
  25. # modified_environ codes come from https://github.com/laurent-laporte-pro/stackoverflow-q2059482/blob/master/demo/environ_ctx.py
  26. @contextlib.contextmanager
  27. def modified_environ(*remove, **update):
  28. """
  29. Temporarily updates the ``os.environ`` dictionary in-place.
  30. The ``os.environ`` dictionary is updated in-place so that the modification
  31. is sure to work in all situations.
  32. :param remove: Environment variables to remove.
  33. :param update: Dictionary of environment variables and values to add/update.
  34. """
  35. env = os.environ
  36. update = update or {}
  37. remove = remove or []
  38. # List of environment variables being updated or removed.
  39. stomped = (set(update.keys()) | set(remove)) & set(env.keys())
  40. # Environment variables and values to restore on exit.
  41. update_after = {k: env[k] for k in stomped}
  42. # Environment variables and values to remove on exit.
  43. remove_after = frozenset(k for k in update if k not in env)
  44. try:
  45. env.update(update)
  46. [env.pop(k, None) for k in remove]
  47. yield
  48. finally:
  49. env.update(update_after)
  50. [env.pop(k) for k in remove_after]

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

Contributors (1)