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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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 functools import lru_cache
  10. from typing import Iterable, Optional, Sequence, Tuple, Union
  11. import numpy as np
  12. from ..core._imperative_rt import CompNode
  13. from ..core._imperative_rt.core2 import SymbolVar, apply, dtype_promotion, split_cpp
  14. from ..core._wrap import as_device
  15. from ..core.ops import builtin
  16. from ..core.ops.builtin import Copy, Identity
  17. from ..core.ops.special import Const
  18. from ..core.tensor.array_method import _broadcast, _remove_axis
  19. from ..core.tensor.utils import astensor1d, convert_inputs, get_device, subgraph_fn
  20. from ..device import get_default_device
  21. from ..tensor import Tensor
  22. from .elemwise import ceil
  23. __all__ = [
  24. "arange",
  25. "broadcast_to",
  26. "concat",
  27. "cond_take",
  28. "cumsum",
  29. "diag",
  30. "expand_dims",
  31. "eye",
  32. "flatten",
  33. "full",
  34. "full_like",
  35. "gather",
  36. "linspace",
  37. "ones",
  38. "ones_like",
  39. "repeat",
  40. "reshape",
  41. "roll",
  42. "split",
  43. "squeeze",
  44. "stack",
  45. "scatter",
  46. "tile",
  47. "copy",
  48. "transpose",
  49. "where",
  50. "zeros",
  51. "zeros_like",
  52. ]
  53. def diag(inp, k=0) -> Tensor:
  54. r"""If ``inp`` is a 1D tensor, then returns a 2D tensor with the elements of ``inp`` as the diagonal.
  55. If ``inp`` is a 2D tensor, then returns a 1D tensor with the diagonal elements of ``inp``.
  56. Args:
  57. inp: input tensor.
  58. k: diagonal in consider. Use :math:`k=0` for the main diagonal, :math:`k>0` for diagonals above the
  59. main diagonal, and :math:`k<0` for diagonals below the main diagonal. Default: 0.
  60. Returns:
  61. the extracted diagonal or constructed diagonal array.
  62. Examples:
  63. >>> inp = F.arange(6, dtype='int32').reshape(2,3)
  64. >>> out = F.diag(inp, k=1)
  65. >>> out
  66. Tensor([1 5], dtype=int32, device=xpux:0)
  67. >>> F.diag(out)
  68. Tensor([[1 0]
  69. [0 5]], dtype=int32, device=xpux:0)
  70. """
  71. op = builtin.Diag(k=k)
  72. (result,) = apply(op, inp)
  73. return result
  74. def eye(N, M=None, *, dtype="float32", device: Optional[CompNode] = None) -> Tensor:
  75. r"""Returns a 2D tensor with ones on the diagonal and zeros elsewhere.
  76. Args:
  77. shape: a list, tuple or integer defining the shape of the output tensor.
  78. dtype: the desired data type of the output tensor. Default: ``float32``.
  79. device: the desired device of the output tensor. Default: if ``None``,
  80. use the default device (see :func:`~.megengine.get_default_device`).
  81. Returns:
  82. eye matrix.
  83. Examples:
  84. .. testcode::
  85. import numpy as np
  86. import megengine.functional as F
  87. out = F.eye(4, 6, dtype=np.float32)
  88. print(out.numpy())
  89. Outputs:
  90. .. testoutput::
  91. [[1. 0. 0. 0. 0. 0.]
  92. [0. 1. 0. 0. 0. 0.]
  93. [0. 0. 1. 0. 0. 0.]
  94. [0. 0. 0. 1. 0. 0.]]
  95. """
  96. if M is not None:
  97. if isinstance(N, Tensor) or isinstance(M, Tensor):
  98. shape = astensor1d((N, M))
  99. else:
  100. shape = Tensor([N, M], dtype="int32", device=device)
  101. elif isinstance(N, Tensor):
  102. shape = N
  103. else:
  104. shape = Tensor(N, dtype="int32", device=device)
  105. op = builtin.Eye(k=0, dtype=dtype, comp_node=device)
  106. (result,) = apply(op, shape)
  107. return result
  108. def full(
  109. shape: Union[int, tuple, list],
  110. value: Union[bool, int, float, Tensor],
  111. dtype=None,
  112. device=None,
  113. ) -> Tensor:
  114. r"""Creates a tensor of shape ``shape`` filled with ``value``.
  115. Args:
  116. shape: output tensor shape.
  117. value: fill value.
  118. dtype: output tensor data type. If ``dtype`` is ``None``, the output tensor
  119. data type must be inferred from ``value``. If the value is an ``int``,
  120. the output tensor data type must be the default integer data type. If the
  121. value is a ``float``, the output tensor data type must be the default
  122. floating-point data type. If the value is a ``bool``, the output tensor
  123. must have boolean data type. Default: ``None``.
  124. device: device on which to place the created tensor. Default: ``None``.
  125. Returns:
  126. a tensor where every element is equal to ``value``.
  127. Examples:
  128. .. testcode::
  129. import numpy as np
  130. import megengine.functional as F
  131. out = F.full([2,3], 1.5)
  132. print(out.numpy())
  133. Outputs:
  134. .. testoutput::
  135. [[1.5 1.5 1.5]
  136. [1.5 1.5 1.5]]
  137. """
  138. if isinstance(shape, int):
  139. shape = (shape,)
  140. if device is None:
  141. device = get_default_device()
  142. (x,) = Const(value, dtype=dtype, device=device)()
  143. if type(shape) in (list, tuple) and len(shape) == 0:
  144. return x
  145. return broadcast_to(x, shape)
  146. def ones(
  147. shape: Union[int, Tuple[int, ...]],
  148. *,
  149. dtype="float32",
  150. device: Optional[CompNode] = None
  151. ) -> Tensor:
  152. r"""Returns a new tensor having a specified shape and filled with ones.
  153. Args:
  154. shape (int or sequence of ints): the shape of the output tensor.
  155. Keyword args:
  156. dtype (:attr:`.Tensor.dtype`): output tensor data type. Default: ``float32``.
  157. device (:attr:`.Tensor.device`): device on which to place the created tensor. Default: ``None``.
  158. Returns:
  159. a tensor containing ones.
  160. Examples:
  161. .. testcode::
  162. import megengine.functional as F
  163. out = F.ones(5)
  164. print(out.numpy())
  165. out = F.ones((5, ), dtype='int32')
  166. print(out.numpy())
  167. out = F.ones((2, 2))
  168. print(out.numpy())
  169. out = F.ones([2, 1])
  170. print(out.numpy())
  171. Outputs:
  172. .. testoutput::
  173. [1. 1. 1. 1. 1.]
  174. [1 1 1 1 1]
  175. [[1. 1.]
  176. [1. 1.]]
  177. [[1.]
  178. [1.]]
  179. """
  180. return full(shape, 1.0, dtype=dtype, device=device)
  181. def zeros(
  182. shape: Union[int, Tuple[int, ...]],
  183. *,
  184. dtype="float32",
  185. device: Optional[CompNode] = None
  186. ) -> Tensor:
  187. r"""Returns a new tensor having a specified shape and filled with zeros.
  188. Args:
  189. shape (int or sequence of ints): the shape of the output tensor.
  190. Keyword args:
  191. dtype (:attr:`.Tensor.dtype`): output tensor data type. Default: ``float32``.
  192. device (:attr:`.Tensor.device`): device on which to place the created tensor. Default: ``None``.
  193. Returns:
  194. a tensor containing zeros.
  195. Examples:
  196. >>> F.zeros((2, 1))
  197. Tensor([[0.]
  198. [0.]], device=xpux:0)
  199. """
  200. return full(shape, 0.0, dtype=dtype, device=device)
  201. def zeros_like(inp: Union[Tensor, SymbolVar]) -> Union[Tensor, SymbolVar]:
  202. r"""Returns a tensor filled with zeros with the same shape and data type as input tensor.
  203. Args:
  204. inp (Tensor): input tensor.
  205. Return:
  206. a tensor containing zeros.
  207. Examples:
  208. >>> input = F.arange(9, dtype='int32').reshape(3,3)
  209. >>> F.zeros_like(input)
  210. Tensor([[0 0 0]
  211. [0 0 0]
  212. [0 0 0]], dtype=int32, device=xpux:0)
  213. """
  214. return full_like(inp, 0.0)
  215. def ones_like(inp: Union[Tensor, SymbolVar]) -> Union[Tensor, SymbolVar]:
  216. r"""Returns a tensor filled with ones with the same shape and data type as input tensor.
  217. Args:
  218. inp (Tensor): input tensor.
  219. Return:
  220. a tensor containing ones.
  221. Examples:
  222. >>> input = F.arange(6, dtype='int32').reshape(2,3)
  223. >>> F.ones_like(input)
  224. Tensor([[1 1 1]
  225. [1 1 1]], dtype=int32, device=xpux:0)
  226. """
  227. return full_like(inp, 1.0)
  228. def full_like(
  229. inp: Union[Tensor, SymbolVar], value: Union[int, float]
  230. ) -> Union[Tensor, SymbolVar]:
  231. r"""Returns a tensor filled with given value with the same shape as input tensor.
  232. Args:
  233. inp: input tensor.
  234. value: target value.
  235. Return:
  236. output tensor.
  237. Examples:
  238. .. testcode::
  239. import numpy as np
  240. from megengine import tensor
  241. import megengine.functional as F
  242. inp = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  243. out = F.full_like(inp, 2)
  244. print(out.numpy())
  245. Outputs:
  246. .. testoutput::
  247. [[2 2 2]
  248. [2 2 2]]
  249. """
  250. (x,) = Const(value, dtype=inp.dtype, device=inp.device)(inp)
  251. if inp.ndim == 0:
  252. return x
  253. return broadcast_to(x, inp.shape)
  254. def broadcast_to(inp: Tensor, shape: Union[int, Iterable[int]]) -> Tensor:
  255. r"""Broadcasts a tensor to given shape.
  256. Args:
  257. inp: input tensor.
  258. shape: target shape.
  259. Returns:
  260. output tensor.
  261. Examples:
  262. .. testcode::
  263. import numpy as np
  264. from megengine import tensor
  265. import megengine.functional as F
  266. data = tensor(np.arange(0, 3, dtype=np.float32).reshape(3))
  267. out = F.broadcast_to(data, (2, 3))
  268. print(out.numpy())
  269. Outputs:
  270. .. testoutput::
  271. [[0. 1. 2.]
  272. [0. 1. 2.]]
  273. """
  274. return _broadcast(inp, shape)
  275. def concat(inps: Iterable[Tensor], axis: int = 0, device=None) -> Tensor:
  276. r"""Concat some tensors
  277. Args:
  278. inps: input tensors to concat.
  279. axis: over which dimension the tensors are concatenated. Default: 0
  280. device: which device output will be. Default: None
  281. Returns:
  282. output tensor.
  283. Examples:
  284. .. testcode::
  285. import numpy as np
  286. from megengine import tensor
  287. import megengine.functional as F
  288. data1 = tensor(np.arange(0, 6, dtype=np.float32).reshape((2, 3)))
  289. data2 = tensor(np.arange(6, 12, dtype=np.float32).reshape((2, 3)))
  290. out = F.concat([data1, data2])
  291. print(out.numpy())
  292. Outputs:
  293. .. testoutput::
  294. [[ 0. 1. 2.]
  295. [ 3. 4. 5.]
  296. [ 6. 7. 8.]
  297. [ 9. 10. 11.]]
  298. """
  299. if len(inps) == 1:
  300. return inps[0]
  301. # FIXME: remove this convert_inputs
  302. inps = convert_inputs(*inps, device=device)
  303. if device is None:
  304. device = get_device(inps)
  305. device = as_device(device)
  306. (result,) = apply(builtin.Concat(axis=axis, comp_node=device.to_c()), *inps)
  307. return result
  308. def stack(inps, axis=0, device=None):
  309. r"""Concats a sequence of tensors along a new axis.
  310. The input tensors must have the same shape.
  311. Args:
  312. inps: input tensors.
  313. axis: which axis will be concatenated.
  314. device: the device output will be. Default: None
  315. Returns:
  316. output concatenated tensor.
  317. Examples:
  318. .. testcode::
  319. import numpy as np
  320. from megengine import tensor
  321. import megengine.functional as F
  322. x1 = tensor(np.arange(0, 3, dtype=np.float32).reshape((3)))
  323. x2 = tensor(np.arange(6, 9, dtype=np.float32).reshape((3)))
  324. out = F.stack([x1, x2], axis=0)
  325. print(out.numpy())
  326. Outputs:
  327. .. testoutput::
  328. [[0. 1. 2.]
  329. [6. 7. 8.]]
  330. """
  331. if len(inps) > 0 and not isinstance(inps[0].shape, inps[0].__class__):
  332. shapes = {arr.shape for arr in inps}
  333. if len(shapes) != 1:
  334. raise ValueError("All input tensors must have the same shape")
  335. inps = [expand_dims(inp, axis=axis) for inp in inps]
  336. return concat(inps, axis=axis, device=device)
  337. def split(inp, nsplits_or_sections, axis=0):
  338. r"""Splits the input tensor into several smaller tensors.
  339. When nsplits_or_sections is int, the last tensor may be smaller than others.
  340. Args:
  341. inp: input tensor.
  342. nsplits_or_sections: number of sub tensors or sections information list.
  343. axis: which axis will be splited.
  344. Returns:
  345. output tensor list.
  346. Examples:
  347. .. testcode::
  348. import os
  349. import numpy as np
  350. from megengine import tensor
  351. import megengine.functional as F
  352. x = tensor(np.random.random((10, 20)), dtype=np.float32)
  353. y = F.split(x, 3)
  354. z = F.split(x, [6, 17], axis=1)
  355. print([i.numpy().shape for i in y])
  356. print([i.numpy().shape for i in z])
  357. Outputs:
  358. .. testoutput::
  359. [(4, 20), (3, 20), (3, 20)]
  360. [(10, 6), (10, 11), (10, 3)]
  361. """
  362. return split_cpp(inp, nsplits_or_sections, axis)
  363. def _get_idx(index, axis):
  364. index_dims = len(index.shape)
  365. idx = []
  366. for i in range(index_dims):
  367. if i != axis:
  368. shape = [1] * index_dims
  369. shape[i] = index.shape[i]
  370. arange = linspace(
  371. 0, index.shape[i] - 1, index.shape[i], device=index.device,
  372. )
  373. arange = (
  374. broadcast_to(arange.reshape(*shape), index.shape)
  375. .reshape(-1)
  376. .astype(np.int32)
  377. )
  378. idx.append(arange)
  379. else:
  380. idx.append(index.reshape(-1))
  381. return tuple(idx)
  382. def gather(inp: Tensor, axis: int, index: Tensor) -> Tensor:
  383. # TODO: rewrite doc
  384. r"""
  385. Gathers data from input tensor on axis using index.
  386. For a 3-D tensor, the output is specified by:
  387. .. code-block::
  388. out[i][j][k] = inp[index[i][j][k]][j][k] # if axis == 0
  389. out[i][j][k] = inp[i][index[i][j][k]][k] # if axis == 1
  390. out[i][j][k] = inp[i][j][index[i][j][k]] # if axis == 2
  391. if input tensor is a n-dimensional tensor with size
  392. :math:`(x_0,x_1,...,x_{i-1},x_i,x_{i+1},...,x_{n-1})` and axis=i,
  393. then index must be a n-dimensional tensor with size
  394. :math:`(x_0,x_1,...,x_{i-1},y,x_{i+1},...,x_{n-1})` where :math:`y\ge 1` and
  395. output will have the same size as index.
  396. Args:
  397. inp: input tensor.
  398. axis: along which axis to index.
  399. index: indices of elements to gather.
  400. Return:
  401. output tensor.
  402. Examples:
  403. .. testcode::
  404. import megengine.functional as F
  405. from megengine import tensor
  406. inp = tensor([
  407. [1,2], [3,4], [5,6],
  408. ])
  409. index = tensor([[0,2], [1,0]])
  410. oup = F.gather(inp, 0, index)
  411. print(oup.numpy())
  412. Outputs:
  413. .. testoutput::
  414. [[1 6]
  415. [3 2]]
  416. """
  417. input_shape = inp.shape
  418. index_shape = index.shape
  419. input_dims = len(input_shape)
  420. index_dims = len(index_shape)
  421. if input_dims != index_dims:
  422. raise ValueError(
  423. "The index tensor must have same dimensions as input tensor, "
  424. "But the input dims:{}, the index dims:{}".format(input_dims, index_dims)
  425. )
  426. if axis < 0 or axis >= input_dims:
  427. raise ValueError(
  428. "Index axis {} is output of bounds, should in range [0 {})".format(
  429. axis, input_dims
  430. )
  431. )
  432. for i in range(input_dims):
  433. if i != axis and input_shape[i] != index_shape[i]:
  434. raise ValueError(
  435. "The input {} and index {} must have the same size apart from axis {}".format(
  436. input_shape, index_shape, axis
  437. )
  438. )
  439. idx = _get_idx(index, axis)
  440. return inp[idx].reshape(index.shape) # pylint: disable=no-member
  441. def scatter(inp: Tensor, axis: int, index: Tensor, source: Tensor) -> Tensor:
  442. # TODO: rewrite doc
  443. r"""
  444. Writes all values from the tensor source into input tensor
  445. at the indices specified in the index tensor.
  446. For each value in source, its output index is specified by its index
  447. in source for ``axis != dimension`` and by the corresponding value in
  448. index for ``axis = dimension``.
  449. For a 3-D tensor, input tensor is updated as:
  450. .. code-block::
  451. inp[index[i][j][k]][j][k] = source[i][j][k] # if axis == 0
  452. inp[i][index[i][j][k]][k] = source[i][j][k] # if axis == 1
  453. inp[i][j][index[i][j][k]] = source[i][j][k] # if axis == 2
  454. ``inp``, ``index`` and ``source`` should have same number of dimensions.
  455. It is also required that ``source.shape(d) <= inp.shape(d)`` and ``index.shape(d) == source.shape(d)``
  456. for all dimensions ``d``.
  457. Moreover, the values of index must be between ``0`` and ``inp.shape(axis) - 1`` inclusive.
  458. Note:
  459. Please notice that, due to performance issues, the result is uncertain on the GPU device
  460. if scattering different positions from source to the same destination position
  461. regard to index tensor.
  462. Check the following examples, the oup[0][2] is maybe
  463. from source[0][2] which value is 0.2256 or source[1][2] which value is 0.5339
  464. if set the index[1][2] from 1 to 0.
  465. Args:
  466. inp: inp tensor which to be scattered.
  467. axis: axis along which to index.
  468. index: indices of elements to scatter.
  469. source: source element(s) to scatter.
  470. Return:
  471. output tensor.
  472. Examples:
  473. .. testcode::
  474. import numpy as np
  475. import megengine.functional as F
  476. from megengine import tensor
  477. inp = tensor(np.zeros(shape=(3,5),dtype=np.float32))
  478. source = tensor([[0.9935,0.9465,0.2256,0.8926,0.4396],[0.7723,0.0718,0.5939,0.357,0.4576]])
  479. index = tensor([[0,2,0,2,1],[2,0,1,1,2]])
  480. oup = F.scatter(inp, 0, index,source)
  481. print(oup.numpy())
  482. Outputs:
  483. .. testoutput::
  484. [[0.9935 0.0718 0.2256 0. 0. ]
  485. [0. 0. 0.5939 0.357 0.4396]
  486. [0.7723 0.9465 0. 0.8926 0.4576]]
  487. """
  488. input_shape = inp.shape
  489. index_shape = index.shape
  490. source_shape = source.shape
  491. input_dims = len(input_shape)
  492. index_dims = len(index_shape)
  493. source_dims = len(source_shape)
  494. if input_dims != index_dims or input_dims != source_dims:
  495. raise ValueError("The input, source and index tensor must have same dimensions")
  496. if axis < 0 or axis >= input_dims:
  497. raise ValueError(
  498. "Index axis {} is output of bounds, should in range [0 {})".format(
  499. axis, input_dims
  500. )
  501. )
  502. for i in range(source_dims):
  503. if source_shape[i] > input_shape[i]:
  504. raise ValueError(
  505. "The each shape size for source {} must be less than or equal to input {} ".format(
  506. source_shape, input_shape
  507. )
  508. )
  509. for i in range(index_dims):
  510. if index_shape[i] != source_shape[i]:
  511. raise ValueError(
  512. "The each shape size for index {} must be equal to source {} ".format(
  513. index_shape, source_shape
  514. )
  515. )
  516. for i in range(index_dims):
  517. if i != axis and index_shape[i] > input_shape[i]:
  518. raise ValueError(
  519. "The index {} must be less than or equal to input {} size apart from axis {}".format(
  520. index_shape, input_shape, axis
  521. )
  522. )
  523. idx = _get_idx(index, axis)
  524. inp[idx] = source.flatten()
  525. return inp
  526. @lru_cache(maxsize=None)
  527. def _get_where_op(dtype=None, device=None):
  528. @subgraph_fn(
  529. "Where",
  530. dtype=dtype,
  531. device=device,
  532. nr_inputs=3,
  533. jit_fusion=True,
  534. custom_grad=True,
  535. )
  536. def where(inputs, f, c):
  537. (mask, x, y) = inputs[0:3]
  538. oup = f("switch_gt0", mask, x)
  539. ksam = f("-", c(1), mask)
  540. oup = f("+", oup, f("switch_gt0", ksam, y))
  541. (oup_grad,) = yield (oup,)
  542. x_grad = f("switch_gt0", mask, oup_grad)
  543. y_grad = f("switch_gt0", ksam, oup_grad)
  544. yield (None, x_grad, y_grad)
  545. return where
  546. def where(mask: Tensor, x: Tensor, y: Tensor) -> Tensor:
  547. r"""Selects elements either from Tensor x or Tensor y, according to mask.
  548. .. math::
  549. \textrm{out}_i = x_i \textrm{ if } \textrm{mask}_i \textrm{ is True else } y_i
  550. Args:
  551. mask: a mask used for choosing ``x`` or ``y``.
  552. x: first choice.
  553. y: second choice.
  554. Returns:
  555. output tensor.
  556. Examples:
  557. .. testcode::
  558. import numpy as np
  559. from megengine import tensor
  560. import megengine.functional as F
  561. mask = tensor(np.array([[True, False], [False, True]], dtype=np.bool))
  562. x = tensor(np.array([[1, np.inf], [np.nan, 4]],
  563. dtype=np.float32))
  564. y = tensor(np.array([[5, 6], [7, 8]], dtype=np.float32))
  565. out = F.where(mask, x, y)
  566. print(out.numpy())
  567. Outputs:
  568. .. testoutput::
  569. [[1. 6.]
  570. [7. 4.]]
  571. """
  572. if not isinstance(x, Tensor):
  573. raise TypeError("input x must be a tensor")
  574. if not isinstance(y, Tensor):
  575. raise TypeError("input y must be a tensor")
  576. if not isinstance(mask, Tensor):
  577. raise TypeError("mask must be a tensor")
  578. if mask.dtype != np.bool_:
  579. raise ValueError("mask must be bool")
  580. if x.device != mask.device:
  581. raise ValueError("ambiguous device: {} vs {}".format(x.device, mask.device))
  582. dtype = dtype_promotion(x, y)
  583. device = x.device
  584. if x.dtype != dtype:
  585. x = x.astype(dtype)
  586. if y.dtype != dtype:
  587. y = y.astype(dtype)
  588. mask = mask.astype(dtype)
  589. where = _get_where_op(dtype=dtype, device=device)
  590. (oup,) = where(mask, x, y)
  591. return oup
  592. def cond_take(mask: Tensor, x: Tensor) -> Tensor:
  593. r"""Takes elements from data if specific condition is satisfied on mask.
  594. This operator has two outputs: the first is the elements taken,
  595. and the second is the indices corresponding to those elements;
  596. they are both 1-dimensional. High-dimension input would first be flattened.
  597. Args:
  598. mask: condition param; must be the same shape with data.
  599. x: input tensor from which to take elements.
  600. Examples:
  601. .. testcode::
  602. import numpy as np
  603. from megengine import tensor
  604. import megengine.functional as F
  605. mask = tensor(np.array([[True, False], [False, True]], dtype=np.bool_))
  606. x = tensor(np.array([[1, np.inf], [np.nan, 4]],
  607. dtype=np.float32))
  608. v, index = F.cond_take(mask, x)
  609. print(v.numpy(), index.numpy())
  610. Outputs:
  611. .. testoutput::
  612. [1. 4.] [0 3]
  613. """
  614. if not isinstance(x, (Tensor, SymbolVar)):
  615. raise TypeError("input must be a tensor")
  616. if not isinstance(mask, (Tensor, SymbolVar)):
  617. raise TypeError("mask must be a tensor")
  618. if mask.dtype != np.bool_:
  619. raise ValueError("mask must be bool")
  620. if x.device != mask.device:
  621. raise ValueError("ambiguous device: {} vs {}".format(x.device, mask.device))
  622. op = builtin.CondTake()
  623. v, index = apply(op, x, mask)
  624. return v, index
  625. def transpose(inp: Tensor, pattern: Iterable[int]) -> Tensor:
  626. r"""Swaps shapes and strides according to given pattern.
  627. Args:
  628. inp: input tensor.
  629. pattern: a list of integers including 0, 1, ... , ``ndim``-1,
  630. and any number of ``'x'`` char in dimensions where this tensor should be broadcasted.
  631. For examples:
  632. * (``'x'``) -> make a 0d (scalar) into a 1d vector
  633. * (0, 1) -> identity for 2d vectors
  634. * (1, 0) -> inverts the first and second dimensions
  635. * (``'x'``, 0) -> make a row out of a 1d vector (N to 1xN)
  636. * (0, ``'x'``) -> make a column out of a 1d vector (N to Nx1)
  637. * (2, 0, 1) -> AxBxC to CxAxB
  638. * (0, ``'x'``, 1) -> AxB to Ax1xB
  639. * (1, ``'x'``, 0) -> AxB to Bx1xA
  640. * (1,) -> this removes dimensions 0. It must be a broadcastable dimension (1xA to A)
  641. Returns:
  642. output tensor.
  643. Examples:
  644. .. testcode::
  645. import numpy as np
  646. from megengine import tensor
  647. import megengine.functional as F
  648. x = tensor(np.array([[1, 1], [0, 0]], dtype=np.int32))
  649. out = F.transpose(x, (1, 0))
  650. print(out.numpy())
  651. Outputs:
  652. .. testoutput::
  653. [[1 0]
  654. [1 0]]
  655. """
  656. return inp.transpose(list(-1 if _ == "x" else _ for _ in pattern))
  657. def reshape(inp: Tensor, target_shape: Iterable[int]) -> Tensor:
  658. r"""Reshapes a tensor without changing its data.
  659. Args:
  660. inp: input tensor to reshape.
  661. target_shape: target shape compatible with the original shape. One shape dimension is allowed
  662. to be `-1` . When a shape dimension is `-1` , the corresponding output tensor shape dimension
  663. must be inferred from the length of the tensor and the remaining dimensions.
  664. Returns:
  665. an output tensor having the same data type, elements, and underlying element order as `inp` .
  666. Examples:
  667. >>> x = F.arange(12)
  668. >>> x
  669. Tensor([ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.], device=xpux:0)
  670. >>> F.reshape(x, (3, 4))
  671. Tensor([[ 0. 1. 2. 3.]
  672. [ 4. 5. 6. 7.]
  673. [ 8. 9. 10. 11.]], device=xpux:0)
  674. >>> F.reshape(x, (2, -1))
  675. Tensor([[ 0. 1. 2. 3. 4. 5.]
  676. [ 6. 7. 8. 9. 10. 11.]], device=xpux:0)
  677. """
  678. return inp.reshape(target_shape)
  679. def flatten(inp: Tensor, start_axis: int = 0, end_axis: int = -1) -> Tensor:
  680. r"""Reshapes the tensor by flattening the sub-tensor from dimension ``start_axis`` to dimension ``end_axis``.
  681. Args:
  682. inp: input tensor.
  683. start_axis: start dimension that the sub-tensor to be flattened. Default: 0
  684. end_axis: end dimension that the sub-tensor to be flattened. Default: -1
  685. Returns:
  686. output tensor.
  687. Examples:
  688. .. testcode::
  689. import numpy as np
  690. from megengine import tensor
  691. import megengine.functional as F
  692. inp_shape = (2, 2, 3, 3)
  693. x = tensor(
  694. np.arange(36, dtype=np.int32).reshape(inp_shape),
  695. )
  696. out = F.flatten(x, 2)
  697. print(x.numpy().shape)
  698. print(out.numpy().shape)
  699. Outputs:
  700. .. testoutput::
  701. (2, 2, 3, 3)
  702. (2, 2, 9)
  703. """
  704. target_shape = tuple(inp.shape[i] for i in range(start_axis)) + (-1,)
  705. if end_axis != -1:
  706. target_shape += (*inp.shape[end_axis + 1 :],)
  707. return inp.reshape(*target_shape)
  708. def expand_dims(inp: Tensor, axis: Union[int, Sequence[int]]) -> Tensor:
  709. r"""Adds dimension before given axis.
  710. Args:
  711. inp: input tensor.
  712. axis: place of new axes.
  713. Returns:
  714. output tensor.
  715. Examples:
  716. .. testcode::
  717. import numpy as np
  718. from megengine import tensor
  719. import megengine.functional as F
  720. x = tensor([1, 2])
  721. out = F.expand_dims(x, 0)
  722. print(out.numpy().shape)
  723. Outputs:
  724. .. testoutput::
  725. (1, 2)
  726. """
  727. def get_axes():
  728. try:
  729. return [int(axis)]
  730. except (TypeError, ValueError):
  731. pass
  732. return list(map(int, axis))
  733. axis = get_axes()
  734. try:
  735. ndim = inp.ndim + len(axis)
  736. axis = sorted(i + ndim if i < 0 else i for i in axis)
  737. except ValueError:
  738. if any([ind < 0 for ind in axis]):
  739. raise IndexError(
  740. "Does not support negative index when tensor's ndim is unknown"
  741. )
  742. axis = sorted(axis)
  743. assert axis, "axis could not be empty"
  744. op = builtin.AddAxis(axis=axis)
  745. (result,) = apply(op, inp)
  746. return result
  747. def squeeze(inp: Tensor, axis: Optional[Union[int, Sequence[int]]] = None) -> Tensor:
  748. r"""Removes dimension of shape 1.
  749. Args:
  750. inp: input tensor.
  751. axis: place of axis to be removed.
  752. Returns:
  753. output tensor.
  754. Examples:
  755. .. testcode::
  756. import numpy as np
  757. from megengine import tensor
  758. import megengine.functional as F
  759. x = tensor(np.array([1, 2], dtype=np.int32).reshape(1, 1, 2, 1))
  760. out = F.squeeze(x, 3)
  761. print(out.numpy().shape)
  762. Outputs:
  763. .. testoutput::
  764. (1, 1, 2)
  765. """
  766. return _remove_axis(inp, axis)
  767. def linspace(
  768. start: Union[int, float, Tensor],
  769. stop: Union[int, float, Tensor],
  770. num: Union[int, Tensor],
  771. dtype="float32",
  772. device: Optional[CompNode] = None,
  773. ) -> Tensor:
  774. r"""Returns equally spaced numbers over a specified interval.
  775. Args:
  776. start: starting value of the squence, shoule be scalar.
  777. stop: last value of the squence, shoule be scalar.
  778. num: number of values to generate.
  779. dtype: result data type.
  780. Returns:
  781. generated tensor.
  782. Examples:
  783. .. testcode::
  784. import numpy as np
  785. import megengine.functional as F
  786. a = F.linspace(3, 10, 5)
  787. print(a.numpy())
  788. Outputs:
  789. .. testoutput::
  790. [ 3. 4.75 6.5 8.25 10. ]
  791. """
  792. for item in (start, stop, num):
  793. cur_device = getattr(item, "device", None)
  794. if device is None:
  795. device = cur_device
  796. else:
  797. if not (cur_device is None or device == cur_device):
  798. raise ("ambiguous device for linspace opr")
  799. is_symbolvar = list(isinstance(x, SymbolVar) for x in [start, stop, num])
  800. if any(is_symbolvar) and not all(is_symbolvar):
  801. raise TypeError("start, stop and num should all be VarNode or none of them")
  802. if not isinstance(start, (Tensor, SymbolVar)):
  803. start = Tensor(start, device=device)
  804. if not isinstance(stop, (Tensor, SymbolVar)):
  805. stop = Tensor(stop, device=device)
  806. if not isinstance(num, (Tensor, SymbolVar)):
  807. num = Tensor(num, device=device)
  808. op = builtin.Linspace(comp_node=device)
  809. (result,) = apply(op, start, stop, num)
  810. if np.dtype(dtype) != np.float32:
  811. return result.astype(dtype)
  812. return result
  813. def arange(
  814. start: Union[int, float, Tensor] = 0,
  815. stop: Optional[Union[int, float, Tensor]] = None,
  816. step: Union[int, float, Tensor] = 1,
  817. dtype="float32",
  818. device: Optional[CompNode] = None,
  819. ) -> Tensor:
  820. r"""Returns evenly spaced values within the half-open interval ``[start, stop)`` as a one-dimensional tensor.
  821. Note:
  822. This function cannot guarantee that the interval does not include the stop value in those cases
  823. where step is not an integer and floating-point rounding errors affect the length of the output tensor.
  824. Args:
  825. start: if ``stop`` is specified, the start of interval (inclusive); otherwise,
  826. the end of the interval (exclusive). If ``stop`` is not specified, the default starting value is ``0``.
  827. stop: the end of the interval. Default: ``None``.
  828. step: the distance between two adjacent elements ( ``out[i+1] - out[i]`` ). Must not be 0 ;
  829. may be negative, this results i an empty tensor if stop >= start . Default: 1 .
  830. Keyword args:
  831. dtype( :attr:`.Tensor.dtype` ): output tensor data type. Default: ``float32``.
  832. device( :attr:`.Tensor.device` ): device on which to place the created tensor. Default: ``None``.
  833. Returns:
  834. A one-dimensional tensor containing evenly spaced values.
  835. The length of the output tensor must be ``ceil((stop-start)/step)``
  836. if ``stop - start`` and ``step`` have the same sign, and length 0 otherwise.
  837. Examples:
  838. >>> F.arange(5)
  839. Tensor([0. 1. 2. 3. 4.], device=xpux:0)
  840. >>> F.arange(1, 4)
  841. Tensor([1. 2. 3.], device=xpux:0)
  842. """
  843. if stop is None:
  844. start, stop = 0, start
  845. start = Tensor(start, dtype="float32")
  846. stop = Tensor(stop, dtype="float32")
  847. step = Tensor(step, dtype="float32")
  848. num = ceil((stop - start) / step)
  849. stop = start + step * (num - 1)
  850. result = linspace(start, stop, num, device=device)
  851. if np.dtype(dtype) != np.float32:
  852. return result.astype(dtype)
  853. return result
  854. def repeat(inp: Tensor, repeats: int, axis: Optional[int] = None):
  855. r"""Repeat elements of an array.
  856. Args:
  857. inp: input tensor.
  858. repeats: the number of repetitions for each element.
  859. axis: the axis along which to repeat values. By default, use the
  860. flattened input array, and return a flat output array.
  861. Returns:
  862. output tensor.
  863. Examples:
  864. .. testcode::
  865. import numpy as np
  866. import megengine.functional as F
  867. from megengine import tensor
  868. x = tensor([[1, 2], [3, 4]], np.int32)
  869. y = F.repeat(x, 2, axis=0)
  870. print(y.numpy())
  871. Outputs:
  872. .. testoutput::
  873. [[1 2]
  874. [1 2]
  875. [3 4]
  876. [3 4]]
  877. """
  878. if axis is None:
  879. inp = inp.reshape(-1) # flatten
  880. axis = 0
  881. shape = astensor1d(inp.shape, inp, dtype="int32", device=inp.device)
  882. # assume inp.ndim is not changed during trace
  883. max_axis = len(shape) - 1
  884. assert axis >= 0 and axis <= max_axis
  885. assert repeats >= 1
  886. base_shape, bcast_shape, target_shape = [], [], []
  887. if axis != 0:
  888. target_shape.append(shape[:axis])
  889. base_shape.extend([shape[: axis + 1], [1,]])
  890. bcast_shape.extend([shape[: axis + 1], [repeats,]])
  891. target_shape.extend(
  892. [shape[axis] * repeats,]
  893. )
  894. if axis + 1 <= max_axis:
  895. base_shape.append(shape[axis + 1 :])
  896. bcast_shape.append(shape[axis + 1 :])
  897. target_shape.append(shape[axis + 1 :])
  898. out = broadcast_to(inp.reshape(concat(base_shape)), concat(bcast_shape)).reshape(
  899. concat(target_shape)
  900. )
  901. return out
  902. def _tile_one_dim(inp, rep, axis):
  903. shape = astensor1d(inp.shape, inp, dtype="int32", device=inp.device)
  904. # assume inp.ndim is not changed during trace
  905. max_axis = len(shape) - 1
  906. base_shape, bcast_shape, target_shape = [], [], []
  907. if axis != 0:
  908. base_shape.append(shape[:axis])
  909. bcast_shape.append(shape[:axis])
  910. target_shape.append(shape[:axis])
  911. base_shape.extend([[1,], shape[axis:]])
  912. bcast_shape.extend([rep, shape[axis:]])
  913. target_shape.append(shape[axis] * rep)
  914. if axis + 1 <= max_axis:
  915. target_shape.append(shape[axis + 1 :])
  916. out = broadcast_to(inp.reshape(concat(base_shape)), concat(bcast_shape)).reshape(
  917. concat(target_shape)
  918. )
  919. return out
  920. def tile(inp: Tensor, reps: Iterable[int]):
  921. r"""Construct an array by repeating ``inp`` the number of times given by ``reps``. If reps has length d,
  922. the result will have dimension of ``max(d, inp.ndim)``. It is required that ``d >= inp.dim``. If ``inp.ndim < d``,
  923. ``inp`` is promoted to be ``d``-dimensional by prepending new axis.
  924. Args:
  925. inp: input tensor.
  926. reps: The number of repetitions of inp along each axis.
  927. Returns:
  928. output tensor.
  929. Examples:
  930. .. testcode::
  931. import numpy as np
  932. import megengine.functional as F
  933. from megengine import tensor
  934. x = tensor([[1, 2], [3, 4]], np.int32)
  935. y = F.tile(x, (2,1))
  936. print(y.numpy())
  937. Outputs:
  938. .. testoutput::
  939. [[1 2]
  940. [3 4]
  941. [1 2]
  942. [3 4]]
  943. """
  944. shape = astensor1d(inp.shape, inp, dtype="int32", device=inp.device)
  945. reps = astensor1d(reps, inp, dtype="int32", device=inp.device)
  946. l_shape = len(shape)
  947. l_reps = len(reps)
  948. assert (
  949. l_reps >= l_shape
  950. ), "Number of dimensions of tiled dims can not be smaller than number of dimensions of tensor"
  951. for i in range(l_shape):
  952. rep = reps[i + (l_reps - l_shape)]
  953. inp = _tile_one_dim(inp, rep, i)
  954. if l_reps > l_shape:
  955. shape = inp.shape
  956. extra = reps[:-l_shape]
  957. extra_ones = ones_like(extra)
  958. base_shape = concat([extra_ones, shape])
  959. bcast_shape = concat([extra, shape])
  960. target_shape = concat([extra, shape])
  961. inp = broadcast_to(inp.reshape(base_shape), bcast_shape).reshape(target_shape)
  962. return inp
  963. def copy(inp, device=None):
  964. r"""Copies tensor to another device.
  965. Args:
  966. inp: input tensor.
  967. device: destination device.
  968. Examples:
  969. .. testcode::
  970. import numpy as np
  971. import platform
  972. from megengine import tensor
  973. from megengine.device import get_device_count
  974. import megengine.functional as F
  975. x = tensor([1, 2, 3], np.int32)
  976. if 1 == get_device_count("gpu"):
  977. y = F.copy(x, "cpu1")
  978. print(y.numpy())
  979. else:
  980. y = F.copy(x, "xpu1")
  981. print(y.numpy())
  982. Outputs:
  983. .. testoutput::
  984. [1 2 3]
  985. """
  986. if device is None:
  987. return apply(Identity(), inp)[0]
  988. return apply(Copy(comp_node=as_device(device).to_c()), inp)[0]
  989. def roll(
  990. inp: Tensor,
  991. shift: Union[int, Iterable[int]],
  992. axis: Optional[Union[int, Iterable[int]]] = None,
  993. ):
  994. r"""Roll the tensor along the given axis(or axes). Elements that are shifted
  995. beyond the last position are re-introduced at the first position.
  996. Args:
  997. inp: input tensor.
  998. shift: the number of places by which the elements of the tensor are
  999. shifted. If shift is a tuple, axis must be a tuple of the same size,
  1000. and each axis will be rolled by the corresponding shift value.
  1001. axis: axis along which to roll. If axis is not specified, the tensor
  1002. will be flattened before rolling and then restored to the original shape.
  1003. Duplicate axes is allowed if it is a tuple. Default: None.
  1004. Examples:
  1005. .. testcode::
  1006. import numpy as np
  1007. from megengine import tensor
  1008. import megengine.functional as F
  1009. x = tensor([[1,2],[3,4],[5,6]], np.int32)
  1010. y = F.roll(x, 1, 0)
  1011. print(y.numpy())
  1012. Outputs:
  1013. .. testoutput::
  1014. [[5 6]
  1015. [1 2]
  1016. [3 4]]
  1017. """
  1018. shp_bak = None
  1019. if axis is None:
  1020. shp_bak = inp.shape
  1021. inp = inp.flatten()
  1022. axis = 0
  1023. shp = inp.shape
  1024. dim = len(shp)
  1025. if isinstance(shift, int):
  1026. assert isinstance(axis, int)
  1027. shift, axis = [shift,], [axis,]
  1028. assert len(shift) == len(axis)
  1029. out = inp
  1030. for i in range(len(shift)):
  1031. axis_ = axis[i]
  1032. shift_ = shift[i]
  1033. axis_normalized_ = axis_ + dim if axis_ < 0 else axis_
  1034. assert (
  1035. dim > axis_normalized_ >= 0
  1036. ), "axis out of range (expected to be in range of [{}, {}], but got {})".format(
  1037. -dim, dim - 1, axis_
  1038. )
  1039. if shift_ == 0:
  1040. continue
  1041. size = shp[axis_normalized_]
  1042. shift_normalized_ = 0 if size == 0 else shift_ % size
  1043. if shift_normalized_ > 0:
  1044. a, b = split(out, [size - shift_normalized_,], axis=axis_normalized_)
  1045. else:
  1046. a, b = split(out, [-shift_normalized_,], axis=axis_normalized_)
  1047. out = concat((b, a), axis=axis_normalized_)
  1048. if shp_bak is not None:
  1049. out = out.reshape(shp_bak)
  1050. return out
  1051. def cumsum(inp: Tensor, axis: int):
  1052. r"""Computes the cumulative sum of elements along given axis.
  1053. Args:
  1054. inp: input tensor.
  1055. axis: axis along which cumsum is performed.
  1056. Examples:
  1057. .. testcode::
  1058. from megengine import tensor
  1059. import megengine.functional as F
  1060. x = tensor([[1, 2, 3], [4, 5, 6]], "int32")
  1061. y = F.cumsum(x, 1)
  1062. print(y.numpy())
  1063. Outputs:
  1064. .. testoutput::
  1065. [[ 1 3 6]
  1066. [ 4 9 15]]
  1067. """
  1068. assert isinstance(inp, Tensor), "input of cumsum must be type of Tensor"
  1069. assert axis >= 0 and axis < inp.ndim, "input axis {} out of bound".format(axis)
  1070. op = builtin.Cumsum(axis=axis, exclusive=False, reverse=False)
  1071. return apply(op, inp)[0]