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.

naming.py 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. from ..core._imperative_rt.core2 import pop_scope, push_scope
  9. class AutoNaming:
  10. r"""
  11. Name all executed operators automaticlly during tracing and record all tensors
  12. renamed by the user.
  13. """
  14. def __init__(self):
  15. self.scopes = []
  16. self.c_ops = []
  17. self.name2ops = {}
  18. self.handle2names = {}
  19. def clear(self):
  20. for var in vars(self).values():
  21. var.clear()
  22. def push_scope(self, scope):
  23. push_scope(scope)
  24. self.scopes.append(scope)
  25. def pop_scope(self):
  26. scope = self.scopes.pop()
  27. pop_scope(scope)
  28. def get_scope(self):
  29. return ".".join(self.scopes)
  30. def record_var_name(self, handle, name):
  31. self.handle2names[handle] = name
  32. def get_var_name(self, handle):
  33. return self.handle2names.pop(handle, None)
  34. def record_opnode(self, op):
  35. ops = self.name2ops.get(op.name, [])
  36. ops.append(op)
  37. self.name2ops[op.name] = ops
  38. def remove_duplicate_names(self):
  39. for key, ops in self.name2ops.items():
  40. if len(ops) == 1:
  41. continue
  42. for i, op in enumerate(ops):
  43. op.name = key + "[%s]" % str(i)
  44. if len(op.outputs) == 1:
  45. continue
  46. for var in op.outputs:
  47. var.name = var.name.replace(key, op.name)
  48. self.name2ops.clear()
  49. auto_naming = AutoNaming()

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