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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 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. from typing import Union
  10. import numpy as np
  11. from .core._imperative_rt import CompNode
  12. from .core._imperative_rt.core2 import Tensor as _Tensor
  13. from .core._imperative_rt.core2 import apply, set_py_tensor_type
  14. from .core._trace_option import use_symbolic_shape
  15. from .core._wrap import as_device
  16. from .core.ops.builtin import Borrow, Copy, GetVarShape
  17. from .core.tensor.array_method import ArrayMethodMixin
  18. from .device import _valid_device, get_default_device
  19. from .logger import get_logger
  20. from .utils.deprecation import deprecated
  21. logger = get_logger(__name__)
  22. class Tensor(_Tensor, ArrayMethodMixin):
  23. r"""A tensor object represents a multidimensional, homogeneous array of fixed-size items.
  24. Tensor is the primary MegEngine data structure.
  25. Data type(dtype) describes the format of each element, such as ``float32``, ``int8`` and so on,
  26. see :ref:`tensor-dtype` for more details.
  27. It is similar to :class:`numpy.ndarray` but not the same in the design.
  28. For example, GPU devices can be used to store Tensors and execute calculations in MegEngine.
  29. The concept of `view <https://numpy.org/doc/stable/reference/generated/numpy.ndarray.view.html>`_
  30. does not exist in MegEngine so indexing and other behaviors might be different with NumPy.
  31. All manipulations and operations on/between Tensors could be found in the :mod:`~.megengine.functional` module.
  32. Keep in mind that they are **not in-place**, a new Tensor will always be returned and
  33. the original data will remain constant.
  34. For more information, refer to the :ref:`tensor-guide` topic.
  35. Args:
  36. data(Tensor, :class:`~.numpy.ndarray`, :class:`list` or Python number):
  37. The data used for construcing Tensor.
  38. Tensor could be constructed from a Python :class:`list` / :class:`tuple` or sequence;
  39. a NumPy :class:`~.numpy.ndarray` data structure; MegEngine builtin methods and so on.
  40. Refer to :ref:`tensor-creation` for more details.
  41. dtype(:attr:`~.Tensor.dtype`): The data type of returned Tensor. Infer from ``data`` if not specified.
  42. device(:attr:`~.Tensor.device`): The desired device of returned Tensor. Uses :func:`get_default_device` if not specified.
  43. is_const: Whether make it a ``ImutableTensor`` in tracing mode, refer to :class:`.jit.trace`.
  44. no_cache: Whether cache it for memory sharing.
  45. name: Used to improve convenience in graph operation on dumped model.
  46. .. note::
  47. There are some methods like :meth:`~.Tensor.reshape` / :meth:`~.Tensor.flatten` /
  48. :meth:`~.Tensor.transpose` / :meth:`~.Tensor.min` / :meth:`~.Tensor.max` /
  49. :meth:`~.Tensor.mean` / :meth:`~.Tensor.sum` / :meth:`~.Tensor.prod` implemented
  50. in ``Tensor`` class for convenience and historical reasons.
  51. But other methods implemented in the :mod:`~.megengine.functional` module will not be added here anymore,
  52. it is hard for maintaining and too many candidates will affect code completion experience.
  53. """
  54. grad = None
  55. dmap_callback = None
  56. _qparams = None
  57. _custom_name = ""
  58. _name = None
  59. _short_name = None
  60. _prefix = None
  61. def __new__(
  62. cls,
  63. data: Union["Tensor", np.ndarray, list, int, float] = None,
  64. dtype: np.dtype = None,
  65. device: str = None,
  66. is_const: bool = False,
  67. no_cache: bool = False,
  68. name: str = None,
  69. ):
  70. if data is None:
  71. data = []
  72. if device is None:
  73. cn = get_default_device()
  74. elif isinstance(device, str):
  75. if cls.dmap_callback is not None:
  76. cn = CompNode(cls.dmap_callback(device))
  77. else:
  78. cn = CompNode(device)
  79. else:
  80. if isinstance(device, CompNode):
  81. cn = device
  82. else:
  83. cn = device._cn
  84. if isinstance(data, _Tensor):
  85. obj = _Tensor.__new__(cls, data)
  86. else:
  87. if isinstance(data, np.ndarray):
  88. if 0 in data.strides:
  89. data = data.squeeze().reshape(data.shape)
  90. obj = _Tensor.__new__(cls, data, dtype, cn, is_const, no_cache, name)
  91. return obj
  92. def __init__(
  93. self,
  94. data: Union["Tensor", np.ndarray, list, int, float],
  95. dtype: np.dtype = None,
  96. device: str = None,
  97. is_const: bool = False,
  98. no_cache: bool = False,
  99. name: str = None,
  100. ):
  101. if name is None:
  102. name = ""
  103. else:
  104. self._set_name(name)
  105. self._custom_name = name
  106. self._name = name
  107. self._short_name = name
  108. self._prefix = None
  109. @property
  110. def shape(self) -> Union[tuple, "Tensor"]:
  111. r"""Returns a :class:`tuple` or a :class:`~.Tensor` represents tensor dimensions.
  112. Note:
  113. The shape of a tensor was usually represented by a :class:`tuple`.
  114. But if a tensor was treated as symbolic placeholder with tracing,
  115. it's shape could also be a :class:`~.Tensor`. See :class:`~.trace` for more details.
  116. The shape property is usually used to get the current shape of a tensor,
  117. but may also be used to reshape the tensor in-place by assigning a tuple of tensor dimensions to it.
  118. As with :func:`~.reshape`, one of the new shape dimensions can be -1,
  119. in which case its value is inferred from the size of the tensor and the remaining dimensions.
  120. """
  121. shape = super().shape
  122. if shape == () or not use_symbolic_shape():
  123. return shape
  124. return apply(GetVarShape(), self)[0]
  125. @property
  126. def _tuple_shape(self):
  127. return super().shape
  128. @property
  129. def device(self) -> CompNode:
  130. r"""Returns a string represents the device a :class:`~.Tensor` storaged on."""
  131. return super().device
  132. @property
  133. def dtype(self) -> np.dtype:
  134. r"""Returns a :class:`numpy.dtype` object represents the data type of a :class:`~.Tensor`."""
  135. return super().dtype
  136. @property
  137. def qparams(self):
  138. r"""Returns a :class:`~.QParams` object containing quantization params of a :class:`~.Tensor`."""
  139. from .quantization.utils import create_qparams # pylint: disable=all
  140. if self._qparams is None:
  141. self._qparams = create_qparams()
  142. return self._qparams
  143. def numpy(self) -> np.ndarray:
  144. r"""Returns self :class:`~.Tensor` as a :class:`numpy.ndarray`."""
  145. return super().numpy()
  146. def detach(self):
  147. r"""Returns a new :class:`~.Tensor`, detached from the current graph."""
  148. return super().detach()
  149. def _reset(self, other):
  150. if not isinstance(other, _Tensor):
  151. other = Tensor(other, dtype=self.dtype, device=self.device)
  152. super()._reset(other)
  153. def __repr__(self):
  154. piece = "{}(".format(self.__class__.__name__)
  155. with np.printoptions(precision=4, suppress=True):
  156. piece += "{}".format(str(self.numpy()))
  157. if self.dtype != np.float32:
  158. piece += ", dtype={}".format(np.dtype(self.dtype).name)
  159. piece += ", device={}".format(self.device) + ")"
  160. return piece
  161. @property
  162. def name(self):
  163. return self._custom_name
  164. @name.setter
  165. def name(self, name):
  166. self._custom_name = name
  167. self._name = self._prefix + "." + name if self._prefix else name
  168. self._set_name(self._name)
  169. @deprecated(
  170. version="1.0", reason="please use ``tensor_name[...] = value``",
  171. )
  172. def set_value(self, value):
  173. self._reset(value)
  174. @deprecated(version="1.0", reason="use ``*= 0`` instead")
  175. def reset_zero(self):
  176. self *= 0
  177. def to(self, device, *, _borrow=False):
  178. r"""Copy self :class:`~.Tensor` to specified device. See :func:`~.copy`"""
  179. if isinstance(device, str) and not _valid_device(device):
  180. raise ValueError(
  181. "invalid device name {}. For the correct format of the device name, please refer to the instruction of megengine.device.set_default_device()".format(
  182. device
  183. )
  184. )
  185. cn = as_device(device).to_c()
  186. op = Borrow(comp_node=cn) if _borrow else Copy(comp_node=cn)
  187. return apply(op, self)[0]
  188. @property
  189. def requires_grad(self):
  190. raise AttributeError("requires_grad is reserved for future use")
  191. @requires_grad.setter
  192. def requires_grad(self, value):
  193. raise AttributeError("requires_grad is reserved for future use")
  194. @requires_grad.deleter
  195. def requires_grad(self):
  196. raise AttributeError("requires_grad is reserved for future use")
  197. def __hash__(self):
  198. return id(self)
  199. def __getnewargs__(self):
  200. r"""__getnewargs__ will be called for pickle serialization or deep copy"""
  201. return (self.numpy(), self.dtype, self.device.logical_name)
  202. def __getstate__(self):
  203. r"""__getstate__ will be called for pickle serialization or deep copy"""
  204. state = {}
  205. if self._qparams is not None:
  206. state["qparams"] = self._qparams
  207. return state
  208. def __setstate__(self, state):
  209. # for compatibility with old version not using fastcore
  210. if "data" in state:
  211. data = state.pop("data")
  212. device = state.pop("device")
  213. dtype = state.pop("dtype")
  214. self._reset(Tensor(data, dtype=dtype, device=device))
  215. # quantize related state for deepcopy
  216. if "qdict" in state:
  217. qparams = state.pop("qdict")
  218. logger.warning(
  219. "Tensor's 'qdict' state is depreciated. Use 'qparams' instead"
  220. )
  221. elif "qparams" in state:
  222. qparams = state.pop("qparams")
  223. else:
  224. qparams = None
  225. self._qparams = qparams
  226. set_py_tensor_type(Tensor)
  227. tensor = Tensor
  228. class Parameter(Tensor):
  229. r"""A kind of Tensor that is to be considered a module parameter.
  230. Note:
  231. Operations happened on Parameter usually return a Tensor instead of Parameter.
  232. For example, with a Parameter ``x``, ``x.reshape/to/sum/...`` will result into a Tensor.
  233. Any operations between Parameter and Tensor will have Tensor as outputs.
  234. """