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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 __init__(
  62. self,
  63. data: Union["Tensor", np.ndarray, list, int, float],
  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 name is None:
  71. name = ""
  72. else:
  73. self._set_name(name)
  74. self._custom_name = name
  75. self._name = name
  76. self._short_name = name
  77. self._prefix = None
  78. @property
  79. def shape(self) -> Union[tuple, "Tensor"]:
  80. r"""Returns a :class:`tuple` or a :class:`~.Tensor` represents tensor dimensions.
  81. Note:
  82. The shape of a tensor was usually represented by a :class:`tuple`.
  83. But if a tensor was treated as symbolic placeholder with tracing,
  84. it's shape could also be a :class:`~.Tensor`. See :class:`~.trace` for more details.
  85. The shape property is usually used to get the current shape of a tensor,
  86. but may also be used to reshape the tensor in-place by assigning a tuple of tensor dimensions to it.
  87. As with :func:`~.reshape`, one of the new shape dimensions can be -1,
  88. in which case its value is inferred from the size of the tensor and the remaining dimensions.
  89. """
  90. shape = super().shape
  91. if shape == () or not use_symbolic_shape():
  92. return shape
  93. return apply(GetVarShape(), self)[0]
  94. @property
  95. def _tuple_shape(self):
  96. return super().shape
  97. @property
  98. def device(self) -> CompNode:
  99. r"""Returns a string represents the device a :class:`~.Tensor` storaged on."""
  100. return super().device
  101. @property
  102. def dtype(self) -> np.dtype:
  103. r"""Returns a :class:`numpy.dtype` object represents the data type of a :class:`~.Tensor`."""
  104. return super().dtype
  105. @property
  106. def qparams(self):
  107. r"""Returns a :class:`~.QParams` object containing quantization params of a :class:`~.Tensor`."""
  108. from .quantization.utils import create_qparams # pylint: disable=all
  109. if self._qparams is None:
  110. self._qparams = create_qparams()
  111. return self._qparams
  112. def numpy(self) -> np.ndarray:
  113. r"""Returns self :class:`~.Tensor` as a :class:`numpy.ndarray`."""
  114. return super().numpy()
  115. def detach(self):
  116. r"""Returns a new :class:`~.Tensor`, detached from the current graph."""
  117. return super().detach()
  118. def _reset(self, other):
  119. if not isinstance(other, _Tensor):
  120. other = Tensor(other, dtype=self.dtype, device=self.device)
  121. super()._reset(other)
  122. def __repr__(self):
  123. piece = "{}(".format(self.__class__.__name__)
  124. with np.printoptions(precision=4, suppress=True):
  125. piece += "{}".format(str(self.numpy()))
  126. if self.dtype != np.float32:
  127. piece += ", dtype={}".format(np.dtype(self.dtype).name)
  128. piece += ", device={}".format(self.device) + ")"
  129. return piece
  130. @property
  131. def name(self):
  132. return self._custom_name
  133. @name.setter
  134. def name(self, name):
  135. self._custom_name = name
  136. self._name = self._prefix + "." + name if self._prefix else name
  137. self._set_name(self._name)
  138. @deprecated(
  139. version="1.0", reason="please use ``tensor_name[...] = value``",
  140. )
  141. def set_value(self, value):
  142. self._reset(value)
  143. @deprecated(version="1.0", reason="use ``*= 0`` instead")
  144. def reset_zero(self):
  145. self *= 0
  146. def to(self, device, *, _borrow=False):
  147. r"""Copy self :class:`~.Tensor` to specified device. See :func:`~.copy`"""
  148. if isinstance(device, str) and not _valid_device(device):
  149. raise ValueError(
  150. "invalid device name {}. For the correct format of the device name, please refer to the instruction of megengine.device.set_default_device()".format(
  151. device
  152. )
  153. )
  154. cn = as_device(device).to_c()
  155. op = Borrow(comp_node=cn) if _borrow else Copy(comp_node=cn)
  156. return apply(op, self)[0]
  157. @property
  158. def requires_grad(self):
  159. raise AttributeError("requires_grad is reserved for future use")
  160. @requires_grad.setter
  161. def requires_grad(self, value):
  162. raise AttributeError("requires_grad is reserved for future use")
  163. @requires_grad.deleter
  164. def requires_grad(self):
  165. raise AttributeError("requires_grad is reserved for future use")
  166. def __hash__(self):
  167. return id(self)
  168. def __getnewargs__(self):
  169. r"""__getnewargs__ will be called for pickle serialization or deep copy"""
  170. return (self.numpy(), self.dtype, self.device.logical_name)
  171. def __getstate__(self):
  172. r"""__getstate__ will be called for pickle serialization or deep copy"""
  173. state = {}
  174. if self._qparams is not None:
  175. state["qparams"] = self._qparams
  176. return state
  177. def __setstate__(self, state):
  178. # for compatibility with old version not using fastcore
  179. if "data" in state:
  180. data = state.pop("data")
  181. device = state.pop("device")
  182. dtype = state.pop("dtype")
  183. self._reset(Tensor(data, dtype=dtype, device=device))
  184. # quantize related state for deepcopy
  185. if "qdict" in state:
  186. qparams = state.pop("qdict")
  187. logger.warning(
  188. "Tensor's 'qdict' state is depreciated. Use 'qparams' instead"
  189. )
  190. elif "qparams" in state:
  191. qparams = state.pop("qparams")
  192. else:
  193. qparams = None
  194. self._qparams = qparams
  195. set_py_tensor_type(Tensor)
  196. tensor = Tensor
  197. class Parameter(Tensor):
  198. r"""A kind of Tensor that is to be considered a module parameter.
  199. Note:
  200. Operations happened on Parameter usually return a Tensor instead of Parameter.
  201. For example, with a Parameter ``x``, ``x.reshape/to/sum/...`` will result into a Tensor.
  202. Any operations between Parameter and Tensor will have Tensor as outputs.
  203. """