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.

tensor.py 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 collections
  10. from .core import Tensor as _Tensor
  11. from .core.ops.builtin import Copy
  12. from .core.tensor.core import apply
  13. from .device import get_default_device
  14. from .utils.deprecation import deprecated
  15. class Tensor(_Tensor):
  16. grad = None
  17. dmap_callback = None
  18. def __init__(self, data, dtype=None, device=None):
  19. if device is None:
  20. device = get_default_device()
  21. self.q_dict = {"mode": None, "scale": None, "zero_point": None}
  22. super().__init__(data, dtype=dtype, device=device)
  23. @deprecated(version="1.0", reason="no need to reuse an existing tensor since 1.0")
  24. def set_value(self, value):
  25. self._reset(value)
  26. @deprecated(version="1.0", reason="use *= 0 instead")
  27. def reset_zero(self):
  28. self *= 0
  29. def to(self, cn):
  30. return apply(Copy(comp_node=cn), self)[0]
  31. @property
  32. def requires_grad(self):
  33. raise AttributeError("requires_grad is reserved for future use")
  34. @requires_grad.setter
  35. def requires_grad(self, value):
  36. raise AttributeError("requires_grad is reserved for future use")
  37. @requires_grad.deleter
  38. def requires_grad(self):
  39. raise AttributeError("requires_grad is reserved for future use")
  40. def __hash__(self):
  41. return id(self)
  42. def __getstate__(self):
  43. r""" __getstate__ will be called for pickle serialization or deep copy
  44. """
  45. state = {
  46. "data": self.numpy(),
  47. "device": str(self.device),
  48. "dtype": self.dtype,
  49. "qdict": self.q_dict,
  50. }
  51. return state
  52. def __setstate__(self, state):
  53. data = state.pop("data")
  54. device = state.pop("device")
  55. if self.dmap_callback is not None:
  56. assert isinstance(device, str)
  57. device = self.dmap_callback(device)
  58. dtype = state.pop("dtype")
  59. self.q_dict = state.pop("qdict")
  60. super().__init__(data, dtype=dtype, device=device)
  61. def detach(self):
  62. r"""
  63. Returns a new tensor which is treated as constant during backward gradient calcuation,
  64. i.e. its gradient is zero.
  65. :param inp: input tensor
  66. """
  67. Wrapper = type(self)
  68. Tensor = type(self.__wrapped__)
  69. return Wrapper(Tensor(self.__wrapped__._data))
  70. tensor = Tensor
  71. class Parameter(Tensor):
  72. r"""A kind of Tensor that is to be considered a module parameter.
  73. """

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