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

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