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.

math.py 32 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. import collections
  10. import math
  11. from functools import lru_cache
  12. from typing import Iterable, Optional, Sequence, Tuple, Union
  13. from ..core import _config
  14. from ..core._imperative_rt.core2 import apply, dtype_promotion
  15. from ..core._imperative_rt.ops import SubgraphBuilder as _SubgraphBuilder
  16. from ..core._trace_option import use_symbolic_shape
  17. from ..core.ops import builtin
  18. from ..core.ops.builtin import BatchNorm, Elemwise, GetVarShape, Reduce, TypeCvt
  19. from ..core.ops.special import Const
  20. from ..core.tensor import amp
  21. from ..core.tensor.utils import _normalize_axis, cast_tensors, setscalar, subgraph
  22. from ..jit import exclude_from_trace
  23. from ..tensor import Tensor
  24. from .debug_param import get_execution_strategy
  25. from .elemwise import clip, minimum
  26. from .tensor import broadcast_to, concat, expand_dims, squeeze
  27. __all__ = [
  28. "argmax",
  29. "argmin",
  30. "argsort",
  31. "dot",
  32. "isinf",
  33. "isnan",
  34. "matinv",
  35. "matmul",
  36. "max",
  37. "mean",
  38. "min",
  39. "norm",
  40. "normalize",
  41. "prod",
  42. "sign",
  43. "sort",
  44. "std",
  45. "sum",
  46. "svd",
  47. "topk",
  48. "var",
  49. ]
  50. def isnan(inp: Tensor) -> Tensor:
  51. r"""Returns a new tensor representing if each element is ``NaN`` or not.
  52. Args:
  53. inp: input tensor.
  54. Returns:
  55. result tensor.
  56. Examples:
  57. .. testcode::
  58. from megengine import tensor
  59. import megengine.functional as F
  60. x = tensor([1, float("nan"), 0])
  61. print(F.isnan(x).numpy())
  62. Outputs:
  63. .. testoutput::
  64. [False True False]
  65. """
  66. return inp != inp
  67. def isinf(inp: Tensor) -> Tensor:
  68. r"""Returns a new tensor representing if each element is ``Inf`` or not.
  69. Args:
  70. inp: input tensor.
  71. Returns:
  72. result tensor.
  73. Examples:
  74. .. testcode::
  75. from megengine import tensor
  76. import megengine.functional as F
  77. x = tensor([1, float("inf"), 0])
  78. print(F.isinf(x).numpy())
  79. Outputs:
  80. .. testoutput::
  81. [False True False]
  82. """
  83. return abs(inp).astype("float32") == float("inf")
  84. def sign(inp: Tensor):
  85. r"""Returns a new tensor representing the sign of each element in input tensor.
  86. Args:
  87. inp: Tensor:
  88. Returns:
  89. the sign of input tensor.
  90. Examples:
  91. .. testcode::
  92. from megengine import tensor
  93. import megengine.functional as F
  94. x = tensor([1, -1, 0])
  95. print(F.sign(x).numpy())
  96. Outputs:
  97. .. testoutput::
  98. [ 1 -1 0]
  99. """
  100. return (inp > 0).astype(inp.dtype) - (inp < 0).astype(inp.dtype)
  101. def sum(
  102. inp: Tensor,
  103. axis: Optional[Union[int, Sequence[int]]] = None,
  104. keepdims: bool = False,
  105. ) -> Tensor:
  106. r"""Returns the sum of input tensor along given axis. If axis is a list of dimensions,
  107. reduce over all of them.
  108. Args:
  109. inp: input tensor.
  110. axis: dimension to reduce. If None, all dimensions will be reduced.
  111. Default: None
  112. keepdims: whether the output tensor has axis retained or not.
  113. Default: False
  114. Returns:
  115. output tensor.
  116. Examples:
  117. .. testcode::
  118. import numpy as np
  119. from megengine import tensor
  120. import megengine.functional as F
  121. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
  122. out = F.sum(x)
  123. print(out.numpy())
  124. Outputs:
  125. .. testoutput::
  126. 21
  127. """
  128. return inp.sum(axis=axis, keepdims=keepdims)
  129. def prod(
  130. inp: Tensor, axis: Optional[Union[int, Sequence[int]]] = None, keepdims=False
  131. ) -> Tensor:
  132. r"""Returns the product of input tensor along given axis. If axis is a list of dimensions,
  133. reduce over all of them.
  134. Args:
  135. inp: input tensor.
  136. axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  137. keepdims: whether the output tensor has axis retained or not. Default: False
  138. Returns:
  139. output tensor.
  140. Examples:
  141. .. testcode::
  142. import numpy as np
  143. from megengine import tensor
  144. import megengine.functional as F
  145. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
  146. out = F.prod(x)
  147. print(out.numpy())
  148. Outputs:
  149. .. testoutput::
  150. 720
  151. """
  152. return inp.prod(axis=axis, keepdims=keepdims)
  153. def mean(
  154. inp: Tensor,
  155. axis: Optional[Union[int, Sequence[int]]] = None,
  156. keepdims: bool = False,
  157. ) -> Tensor:
  158. r"""Returns the mean value of input tensor along
  159. given axis. If axis is a list of dimensions,
  160. reduce over all of them.
  161. Args:
  162. inp: input tensor.
  163. axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  164. keepdims: whether the output tensor has axis retained or not. Default: False
  165. Returns:
  166. output tensor.
  167. Examples:
  168. .. testcode::
  169. import numpy as np
  170. from megengine import tensor
  171. import megengine.functional as F
  172. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
  173. out = F.mean(x)
  174. print(out.numpy())
  175. Outputs:
  176. .. testoutput::
  177. 3.5
  178. """
  179. return inp.mean(axis=axis, keepdims=keepdims)
  180. def var(
  181. inp: Tensor,
  182. axis: Optional[Union[int, Sequence[int]]] = None,
  183. keepdims: bool = False,
  184. ) -> Tensor:
  185. r"""Returns the variance value of input tensor along
  186. given axis. If axis is a list of dimensions,
  187. reduce over all of them.
  188. Args:
  189. inp: input tensor.
  190. axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  191. keepdims: whether the output tensor has axis retained or not. Default: False
  192. Returns:
  193. output tensor.
  194. Examples:
  195. .. testcode::
  196. import numpy as np
  197. from megengine import tensor
  198. import megengine.functional as F
  199. data = tensor(np.arange(1, 7, dtype=np.float32).reshape(2, 3))
  200. out = F.var(data)
  201. print(out.numpy().round(decimals=4))
  202. Outputs:
  203. .. testoutput::
  204. 2.9167
  205. """
  206. if axis is None:
  207. m = mean(inp, axis=axis, keepdims=False)
  208. else:
  209. m = mean(inp, axis=axis, keepdims=True)
  210. v = inp - m
  211. return mean(v ** 2, axis=axis, keepdims=keepdims)
  212. def std(
  213. inp: Tensor,
  214. axis: Optional[Union[int, Sequence[int]]] = None,
  215. keepdims: bool = False,
  216. ) -> Tensor:
  217. r"""Returns the standard deviation of input tensor along
  218. given axis. If axis is a list of dimensions,
  219. reduce over all of them.
  220. Args:
  221. inp: input tensor.
  222. axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  223. keepdims: whether the output tensor has axis retained or not. Default: False
  224. Returns:
  225. output tensor.
  226. Examples:
  227. .. testcode::
  228. import numpy as np
  229. from megengine import tensor
  230. import megengine.functional as F
  231. data = tensor(np.arange(1, 7, dtype=np.float32).reshape(2, 3))
  232. out = F.std(data, axis=1)
  233. print(out.numpy().round(decimals=4))
  234. Outputs:
  235. .. testoutput::
  236. [0.8165 0.8165]
  237. """
  238. return var(inp, axis=axis, keepdims=keepdims) ** 0.5
  239. def min(
  240. inp: Tensor,
  241. axis: Optional[Union[int, Sequence[int]]] = None,
  242. keepdims: bool = False,
  243. ) -> Tensor:
  244. r"""Returns the min value of input tensor along
  245. given axis. If axis is a list of dimensions,
  246. reduce over all of them.
  247. Args:
  248. inp: input tensor.
  249. axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  250. keepdims: whether the output tensor has axis retained or not. Default: False
  251. Returns:
  252. output tensor.
  253. Examples:
  254. .. testcode::
  255. import numpy as np
  256. from megengine import tensor
  257. import megengine.functional as F
  258. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  259. out = F.min(x)
  260. print(out.numpy())
  261. Outputs:
  262. .. testoutput::
  263. 1
  264. """
  265. return inp.min(axis=axis, keepdims=keepdims)
  266. def max(
  267. inp: Tensor,
  268. axis: Optional[Union[int, Sequence[int]]] = None,
  269. keepdims: bool = False,
  270. ) -> Tensor:
  271. r"""Returns the max value of the input tensor along
  272. given axis. If axis is a list of dimensions,
  273. reduce over all of them.
  274. Args:
  275. inp: input tensor.
  276. axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  277. keepdims: whether the output tensor has axis retained or not. Default: False
  278. Returns:
  279. output tensor.
  280. Examples:
  281. .. testcode::
  282. import numpy as np
  283. from megengine import tensor
  284. import megengine.functional as F
  285. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  286. out = F.max(x)
  287. print(out.numpy())
  288. Outputs:
  289. .. testoutput::
  290. 6
  291. """
  292. return inp.max(axis=axis, keepdims=keepdims)
  293. def norm(
  294. inp: Tensor, ord: float = None, axis: int = None, keepdims=False,
  295. ):
  296. r"""Calculates ``p``-norm of input tensor along
  297. given axis.
  298. Args:
  299. inp: input tensor.
  300. ord: power of value applied to inp. Default: 2
  301. axis: dimension to reduce. If None, input must be a vector. Default: None
  302. keepdims: whether the output tensor has axis retained or not. Default: False
  303. Returns:
  304. output tensor.
  305. Examples:
  306. .. testcode::
  307. import numpy as np
  308. from megengine import tensor
  309. import megengine.functional as F
  310. x = tensor(np.arange(-3, 3, dtype=np.float32))
  311. out = F.norm(x)
  312. print(out.numpy().round(decimals=4))
  313. Outputs:
  314. .. testoutput::
  315. 4.3589
  316. """
  317. if axis is None:
  318. if inp.ndim != 1:
  319. raise TypeError("axis is required unless input is a vector")
  320. if ord is None:
  321. ord = 2
  322. if ord == 0:
  323. return sum(inp != 0, axis=axis, keepdims=keepdims)
  324. if ord == math.inf:
  325. return max(abs(inp))
  326. if ord == -math.inf:
  327. return min(abs(inp))
  328. return sum(abs(inp) ** ord, axis=axis, keepdims=keepdims) ** (1.0 / ord)
  329. def argmin(
  330. inp: Tensor,
  331. axis: Optional[Union[int, Sequence[int]]] = None,
  332. keepdims: bool = False,
  333. ) -> Tensor:
  334. r"""Returns the indices of the minimum values along
  335. given axis. If axis is a list of dimensions,
  336. reduce over all of them.
  337. Args:
  338. inp: input tensor.
  339. axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  340. keepdims: whether the output tensor has axis retained or not. Default: False
  341. Returns:
  342. output tensor.
  343. Examples:
  344. .. testcode::
  345. import numpy as np
  346. from megengine import tensor
  347. import megengine.functional as F
  348. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  349. out = F.argmin(x)
  350. print(out.numpy())
  351. Outputs:
  352. .. testoutput::
  353. 0
  354. """
  355. if axis is None:
  356. assert not keepdims, "can not set axis=None and keepdims=True"
  357. inp = inp.flatten()
  358. axis = 0
  359. axis = _normalize_axis(inp.ndim, axis, reverse=True)
  360. if isinstance(axis, collections.abc.Iterable):
  361. for ai in axis:
  362. op = builtin.Argmin(axis=ai)
  363. (inp,) = apply(op, inp)
  364. if not keepdims:
  365. inp = squeeze(inp, ai)
  366. return inp
  367. op = builtin.Argmin(axis=axis)
  368. (result,) = apply(op, inp)
  369. if not keepdims:
  370. result = squeeze(result, axis)
  371. return result
  372. def argmax(
  373. inp: Tensor,
  374. axis: Optional[Union[int, Sequence[int]]] = None,
  375. keepdims: bool = False,
  376. ) -> Tensor:
  377. r"""Returns the indices of the maximum values along
  378. given axis. If axis is a list of dimensions,
  379. reduce over all of them.
  380. Args:
  381. inp: input tensor.
  382. axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  383. keepdims: whether the output tensor has axis retained or not. Default: False
  384. Returns:
  385. output tensor.
  386. Examples:
  387. .. testcode::
  388. import numpy as np
  389. from megengine import tensor
  390. import megengine.functional as F
  391. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  392. out = F.argmax(x)
  393. print(out.numpy())
  394. Outputs:
  395. .. testoutput::
  396. 5
  397. """
  398. if axis is None:
  399. assert not keepdims, "can not set axis=None and keepdims=True"
  400. inp = inp.flatten()
  401. axis = 0
  402. axis = _normalize_axis(inp.ndim, axis, reverse=True)
  403. if isinstance(axis, collections.abc.Iterable):
  404. for ai in axis:
  405. op = builtin.Argmax(axis=ai)
  406. (inp,) = apply(op, inp)
  407. if not keepdims:
  408. inp = squeeze(inp, ai)
  409. return inp
  410. op = builtin.Argmax(axis=axis)
  411. (result,) = apply(op, inp)
  412. if not keepdims:
  413. result = squeeze(result, axis)
  414. return result
  415. def normalize(
  416. inp: Tensor, ord: float = None, axis: int = None, eps: float = 1e-12,
  417. ) -> Tensor:
  418. r"""Performs :math:`L_p` normalization of input tensor along
  419. given axis.
  420. For a tensor of shape :math:`(n_0, ..., n_{dim}, ..., n_k)`, each
  421. :math:`n_{dim}` -element vector :math:`v` along dimension :attr:`axis` is transformed as:
  422. .. math::
  423. v = \frac{v}{\max(\lVert v \rVert_p, \epsilon)}.
  424. Args:
  425. inp: input tensor.
  426. ord: power of value applied to input tensor. Default: 2
  427. axis: dimension to reduce.If None, input must be a vector. Default: None
  428. eps: a small value to avoid division by zero. Default: 1e-12
  429. Returns:
  430. normalized output tensor.
  431. """
  432. if axis is None:
  433. return inp / clip(norm(inp, ord, axis), lower=eps)
  434. else:
  435. return inp / clip(norm(inp, ord, axis, keepdims=True), lower=eps)
  436. def argsort(inp: Tensor, descending: bool = False) -> Tensor:
  437. r"""Returns the indices that would sort the input tensor.
  438. Args:
  439. inp: input tensor. If it's 2d, the result would be array of indices show how to sort each row in the input tensor.
  440. descending: sort in descending order, where the largest comes first. Default: False
  441. inp: Tensor:
  442. descending: bool:
  443. Returns:
  444. indices of int32 indicates how to sort the input.
  445. Examples:
  446. .. testcode::
  447. import numpy as np
  448. from megengine import tensor
  449. import megengine.functional as F
  450. x = tensor(np.array([1,2], dtype=np.float32))
  451. indices = F.argsort(x)
  452. print(indices.numpy())
  453. Outputs:
  454. .. testoutput::
  455. [0 1]
  456. """
  457. assert len(inp.shape) <= 2, "Input should be 1d or 2d"
  458. if descending:
  459. order = "descending"
  460. else:
  461. order = "ascending"
  462. op = builtin.Argsort(order=order)
  463. if len(inp.shape) == 1:
  464. inp = inp.reshape(1, -1)
  465. _, result = apply(op, inp)
  466. return result[0]
  467. _, result = apply(op, inp)
  468. return result
  469. def sort(inp: Tensor, descending: bool = False) -> Tuple[Tensor, Tensor]:
  470. r"""Returns sorted tensor and the indices would sort the input tensor.
  471. Args:
  472. inp: input tensor. If it's 2d, the result would be sorted by row.
  473. descending: sort in descending order, where the largest comes first. Default: False
  474. Returns:
  475. tuple of two tensors `(sorted_tensor, indices_of_int32)`.
  476. Examples:
  477. .. testcode::
  478. import numpy as np
  479. from megengine import tensor
  480. import megengine.functional as F
  481. x = tensor(np.array([1,2], dtype=np.float32))
  482. out, indices = F.sort(x)
  483. print(out.numpy())
  484. Outputs:
  485. .. testoutput::
  486. [1. 2.]
  487. """
  488. assert len(inp.shape) <= 2, "Input should be 1d or 2d"
  489. if descending:
  490. order = "descending"
  491. else:
  492. order = "ascending"
  493. op = builtin.Argsort(order=order)
  494. if len(inp.shape) == 1:
  495. inp = inp.reshape(1, -1)
  496. tns, ind = apply(op, inp)
  497. return tns[0], ind[0]
  498. tns, ind = apply(op, inp)
  499. return tns, ind
  500. def topk(
  501. inp: Tensor,
  502. k: int,
  503. descending: bool = False,
  504. kth_only: bool = False,
  505. no_sort: bool = False,
  506. ) -> Tuple[Tensor, Tensor]:
  507. r"""Selects the ``Top-K`` (by default) smallest elements of 2d matrix by row.
  508. Args:
  509. inp: input tensor. If input tensor is 2d, each row will be sorted.
  510. k: number of elements needed.
  511. descending: if True, return the largest elements instead. Default: False
  512. kth_only: if True, only the k-th element will be returned. Default: False
  513. no_sort: if True, the returned elements can be unordered. Default: False
  514. Returns:
  515. tuple of two tensors ``(topk_tensor, indices_of_int32)``
  516. Examples:
  517. .. testcode::
  518. import numpy as np
  519. from megengine import tensor
  520. import megengine.functional as F
  521. x = tensor(np.array([2, 4, 6, 8, 7, 5, 3, 1], dtype=np.float32))
  522. top, indices = F.topk(x, 5)
  523. print(top.numpy(), indices.numpy())
  524. Outputs:
  525. .. testoutput::
  526. [1. 2. 3. 4. 5.] [7 0 6 1 5]
  527. """
  528. if descending:
  529. k = -k
  530. if kth_only:
  531. mode = "kth_only"
  532. elif no_sort:
  533. mode = "value_idx_nosort"
  534. else:
  535. mode = "value_idx_sorted"
  536. op = builtin.TopK(mode=mode)
  537. if not isinstance(k, Tensor):
  538. (k,) = Const(k, dtype="int32", device=inp.device)()
  539. if len(inp.shape) == 1:
  540. if kth_only:
  541. (tns,) = apply(op, expand_dims(inp, 0), k)
  542. # FIXME:
  543. # could use a dedicated kernel
  544. # gradient may be routed to other indices if k-th value is not unique
  545. ind = argmax((tns == inp).astype("int8"))
  546. tns = squeeze(tns, 0)
  547. else:
  548. tns, ind = apply(op, expand_dims(inp, 0), k)
  549. tns = squeeze(tns, 0)
  550. ind = squeeze(ind, 0)
  551. else:
  552. if kth_only:
  553. (tns,) = apply(op, inp, k)
  554. # FIXME: same as above
  555. ind = argmax((expand_dims(tns, 1) == inp).astype("int8"), 1)
  556. else:
  557. tns, ind = apply(op, inp, k)
  558. return tns, ind
  559. def matinv(inp: Tensor) -> Tensor:
  560. r"""Computes the inverse of a batch of matrices; input must has shape [..., n, n].
  561. Args:
  562. inp: input tensor.
  563. Returns:
  564. output tensor.
  565. Examples:
  566. .. testcode::
  567. import numpy as np
  568. from megengine import tensor
  569. import megengine.functional as F
  570. data = tensor([[1.0, 0.0], [1.0, 1.0]])
  571. out = F.matinv(data)
  572. print(out.numpy())
  573. Outputs:
  574. .. testoutput::
  575. [[ 1. 0.]
  576. [-1. 1.]]
  577. """
  578. (result,) = apply(builtin.MatrixInverse(), inp)
  579. return result
  580. class _Hashable:
  581. def __init__(self, value) -> None:
  582. self.value = value
  583. def __hash__(self) -> int:
  584. return hash(str(self.value))
  585. def __eq__(self, o: object) -> bool:
  586. if not isinstance(o, _Hashable):
  587. return False
  588. return self.value == o.value
  589. @lru_cache(maxsize=None)
  590. def _get_extentedMatrixMulOp(
  591. device, dtype, dim1, dim2, transpose_a, transpose_b, compute_mode, format, strategy,
  592. ):
  593. @subgraph("extentedMatrixMulOp", dtype, device, 2, gopt_level=2)
  594. def extentedMatrixMulOp(inputs, f, c):
  595. assert len(inputs) == 2
  596. inp1, inp2 = inputs
  597. _dim1, _dim2 = dim1, dim2
  598. def build_shape_head(shape, idx=-1):
  599. # shape[:idx]
  600. return f(
  601. builtin.Subtensor(items=[[0, False, True, False, False]]),
  602. shape,
  603. c(idx, "int32"),
  604. )
  605. def build_shape_tail(shape, idx=-1):
  606. # shape[idx:]
  607. return f(
  608. builtin.Subtensor(items=[[0, True, False, False, False]]),
  609. shape,
  610. c(idx, "int32"),
  611. )
  612. remove_row, remove_col = False, False
  613. if _dim1 == 1:
  614. _dim1 = 2
  615. remove_row = True
  616. if _dim2 == 1:
  617. _dim2 = 2
  618. remove_col = True
  619. if remove_row:
  620. inp1 = f(builtin.AddAxis(axis=[0,]), inp1)
  621. if remove_col:
  622. inp2 = f(builtin.AddAxis(axis=[1,]), inp2)
  623. shape1 = f(GetVarShape(), inp1)
  624. shape2 = f(GetVarShape(), inp2)
  625. if _dim1 > 2:
  626. inp1 = f(
  627. builtin.Reshape(),
  628. inp1,
  629. f(
  630. builtin.Concat(axis=0, comp_node=device),
  631. f(builtin.Reduce(mode="product", axis=0), build_shape_head(shape1)),
  632. build_shape_tail(shape1),
  633. ),
  634. )
  635. if _dim2 > 2:
  636. inp2 = f(
  637. builtin.Reshape(),
  638. inp2,
  639. f(
  640. builtin.Concat(axis=0, comp_node=device),
  641. f(builtin.Reduce(mode="product", axis=0), build_shape_head(shape2)),
  642. build_shape_tail(shape2),
  643. ),
  644. )
  645. op = builtin.MatrixMul(
  646. transposeA=transpose_a,
  647. transposeB=transpose_b,
  648. compute_mode=compute_mode,
  649. format=format,
  650. strategy=strategy.value,
  651. )
  652. result = f(op, inp1, inp2)
  653. result_shape = f(GetVarShape(), result)
  654. if _dim1 > 2:
  655. result = f(
  656. builtin.Reshape(),
  657. result,
  658. f(
  659. builtin.Concat(axis=0, comp_node=device),
  660. build_shape_head(shape1),
  661. build_shape_tail(result_shape),
  662. ),
  663. )
  664. if _dim2 > 2:
  665. result = f(
  666. builtin.Reshape(),
  667. result,
  668. f(
  669. builtin.Concat(axis=0, comp_node=device),
  670. build_shape_head(shape2),
  671. build_shape_tail(result_shape),
  672. ),
  673. )
  674. maxdim = _dim1 if _dim1 > _dim2 else _dim2
  675. if remove_row:
  676. result = f(builtin.RemoveAxis(axis=[maxdim - 2]), result)
  677. if remove_col:
  678. result = f(builtin.RemoveAxis(axis=[maxdim - 1]), result)
  679. return (result,), (True,)
  680. return extentedMatrixMulOp
  681. @lru_cache(maxsize=None)
  682. def _get_extentedBatchedMatrixMulOp(
  683. device, dtype, dim1, dim2, transpose_a, transpose_b, compute_mode, format, strategy,
  684. ):
  685. @subgraph("extentedBatchedMatrixMulOp", dtype, device, 2, gopt_level=2)
  686. def extentedBatchedMatrixMulOp(inputs, f, c):
  687. assert len(inputs) == 2
  688. inp1, inp2 = inputs
  689. _dim1, _dim2 = dim1, dim2
  690. def build_shape_head(shape, idx=-2):
  691. # shape[:idx]
  692. return f(
  693. builtin.Subtensor(items=[[0, False, True, False, False]]),
  694. shape,
  695. c(idx, "int32"),
  696. )
  697. def build_shape_tail(shape, idx=-2):
  698. # shape[idx:]
  699. return f(
  700. builtin.Subtensor(items=[[0, True, False, False, False]]),
  701. shape,
  702. c(idx, "int32"),
  703. )
  704. remove_row, remove_col = False, False
  705. if _dim1 == 1:
  706. _dim1 = 2
  707. remove_row = True
  708. if _dim2 == 1:
  709. _dim2 = 2
  710. remove_col = True
  711. if remove_row:
  712. inp1 = f(builtin.AddAxis(axis=[0,]), inp1)
  713. if remove_col:
  714. inp2 = f(builtin.AddAxis(axis=[1,]), inp2)
  715. shape1 = f(GetVarShape(), inp1)
  716. shape2 = f(GetVarShape(), inp2)
  717. maxdim = _dim1 if _dim1 > _dim2 else _dim2
  718. if _dim1 > _dim2:
  719. # broadcast
  720. shape2 = f(
  721. builtin.Concat(axis=0, comp_node=device),
  722. build_shape_head(shape1, idx=-_dim2), # shape1[:-_dim2]
  723. shape2,
  724. )
  725. inp2 = f(builtin.Broadcast(), inp2, shape2)
  726. batch_shape = build_shape_head(shape1)
  727. if _dim2 > _dim1:
  728. # broadcast
  729. shape1 = f(
  730. builtin.Concat(axis=0, comp_node=device),
  731. build_shape_head(shape2, idx=-_dim1), # shape2[:-_dim1]
  732. shape1,
  733. )
  734. inp1 = f(builtin.Broadcast(), inp1, shape1)
  735. batch_shape = build_shape_head(shape2)
  736. if _dim1 == _dim2:
  737. batch_shape = build_shape_head(shape1)
  738. # compress inputs to 3d
  739. if maxdim > 3:
  740. inp1 = f(
  741. builtin.Reshape(),
  742. inp1,
  743. f(
  744. builtin.Concat(axis=0, comp_node=device),
  745. f(builtin.Reduce(mode="product", axis=0), batch_shape),
  746. build_shape_tail(shape1),
  747. ),
  748. )
  749. inp2 = f(
  750. builtin.Reshape(),
  751. inp2,
  752. f(
  753. builtin.Concat(axis=0, comp_node=device),
  754. f(builtin.Reduce(mode="product", axis=0), batch_shape),
  755. build_shape_tail(shape2),
  756. ),
  757. )
  758. op = builtin.BatchedMatrixMul(
  759. transposeA=transpose_a,
  760. transposeB=transpose_b,
  761. compute_mode=compute_mode,
  762. format=format,
  763. strategy=strategy.value,
  764. )
  765. result = f(op, inp1, inp2)
  766. if maxdim > 3:
  767. result = f(
  768. builtin.Reshape(),
  769. result,
  770. f(
  771. builtin.Concat(axis=0, comp_node=device),
  772. batch_shape,
  773. build_shape_tail(f(GetVarShape(), result)),
  774. ),
  775. )
  776. if remove_row:
  777. result = f(builtin.RemoveAxis(axis=[maxdim - 2]), result)
  778. if remove_col:
  779. result = f(builtin.RemoveAxis(axis=[maxdim - 1]), result)
  780. return (result,), (True,)
  781. return extentedBatchedMatrixMulOp
  782. def matmul(
  783. inp1: Tensor,
  784. inp2: Tensor,
  785. transpose_a=False,
  786. transpose_b=False,
  787. compute_mode="default",
  788. format="default",
  789. ) -> Tensor:
  790. r"""Performs a matrix multiplication of the matrices ``inp1`` and ``inp2``.
  791. With different inputs dim, this function behaves differently:
  792. * Both 1-D tensor, simply forward to ``dot``.
  793. * Both 2-D tensor, normal matrix multiplication.
  794. * If one input tensor is 1-D, matrix vector multiplication.
  795. * If at least one tensor are 3-dimensional or >3-dimensional, the other tensor should have dim >= 2,
  796. the batched matrix-matrix is returned, and the tensor with smaller dimension will be broadcasted.
  797. For example:
  798. * inp1: `(n, k, m)`, inp2: `(n, m, p)`, return: `(n, k, p)`
  799. * inp1: `(n, k, m)`, inp2: `(m, p)`, return: `(n, k, p)`
  800. * inp1: `(n, j, k, m)`, inp2: `(n, j, m, p)`, return: `(n, j, k, p)`
  801. Args:
  802. inp1: first matrix to be multiplied.
  803. inp2: second matrix to be multiplied.
  804. Returns:
  805. output tensor.
  806. Examples:
  807. .. testcode::
  808. import numpy as np
  809. from megengine import tensor
  810. import megengine.functional as F
  811. data1 = tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3))
  812. data2 = tensor(np.arange(0, 6, dtype=np.float32).reshape(3, 2))
  813. out = F.matmul(data1, data2)
  814. print(out.numpy())
  815. Outputs:
  816. .. testoutput::
  817. [[10. 13.]
  818. [28. 40.]]
  819. """
  820. if amp._enabled:
  821. compute_mode = "float32"
  822. inp1, inp2 = cast_tensors(inp1, inp2)
  823. else:
  824. dtype = dtype_promotion(inp1, inp2)
  825. if inp1.dtype != dtype:
  826. inp1 = inp1.astype(dtype)
  827. if inp2.dtype != dtype:
  828. inp2 = inp2.astype(dtype)
  829. dim1, dim2 = inp1.ndim, inp2.ndim
  830. assert dim1 > 0 and dim2 > 0
  831. maxdim = dim1 if dim1 > dim2 else dim2
  832. compute_mode = _config._get_actual_op_param(compute_mode, _config.__compute_mode)
  833. if dim1 == 1 and dim2 == 1: # dispatch to Dot
  834. return dot(inp1, inp2)
  835. elif maxdim <= 2 or dim2 <= 2: # dispath to MatrixMul
  836. extentedMatrixMulOp = _get_extentedMatrixMulOp(
  837. inp1.device,
  838. inp1.dtype,
  839. dim1,
  840. dim2,
  841. transpose_a,
  842. transpose_b,
  843. compute_mode,
  844. format,
  845. strategy=_Hashable(get_execution_strategy()),
  846. )
  847. (result,) = apply(extentedMatrixMulOp(), inp1, inp2)
  848. return result
  849. else: # dispath to BatchedMatrixMul
  850. extentedBatchedMatrixMulOp = _get_extentedBatchedMatrixMulOp(
  851. inp1.device,
  852. inp1.dtype,
  853. dim1,
  854. dim2,
  855. transpose_a,
  856. transpose_b,
  857. compute_mode,
  858. format,
  859. strategy=_Hashable(get_execution_strategy()),
  860. )
  861. (result,) = apply(extentedBatchedMatrixMulOp(), inp1, inp2)
  862. return result
  863. def dot(inp1: Tensor, inp2: Tensor) -> Tensor:
  864. r"""Computes dot-product of two vectors ``inp1`` and ``inp2``.
  865. inputs must be 1-dimensional or scalar. A scalar input is automatically broadcasted.
  866. Refer to :func:`~.matmul` for more general usage.
  867. Args:
  868. inp1: first vector.
  869. inp2: second vector.
  870. Returns:
  871. output value.
  872. Examples:
  873. .. testcode::
  874. import numpy as np
  875. from megengine import tensor
  876. import megengine.functional as F
  877. data1 = tensor(np.arange(0, 6, dtype=np.float32))
  878. data2 = tensor(np.arange(0, 6, dtype=np.float32))
  879. out = F.dot(data1, data2)
  880. print(out.numpy())
  881. Outputs:
  882. .. testoutput::
  883. 55.
  884. """
  885. op = builtin.Dot()
  886. assert (
  887. inp1.ndim <= 1 and inp2.ndim <= 1
  888. ), "Input tensors for dot must be 1-dimensional or scalar"
  889. (result,) = apply(op, inp1, inp2)
  890. setscalar(result)
  891. return result
  892. def svd(inp: Tensor, full_matrices=False, compute_uv=True) -> Tensor:
  893. r"""Computes the singular value decompositions of input matrix.
  894. Args:
  895. inp: input matrix, must has shape `[..., M, N]`.
  896. Returns:
  897. output matrices, `(U, sigma, V)`.
  898. Examples:
  899. .. testcode::
  900. import numpy as np
  901. from megengine import tensor
  902. import megengine.functional as F
  903. x = tensor(np.arange(0, 6, dtype=np.float32).reshape(2,3))
  904. _, y, _ = F.svd(x)
  905. print(y.numpy().round(decimals=3))
  906. Outputs:
  907. .. testoutput::
  908. [7.348 1. ]
  909. """
  910. op = builtin.SVD(full_matrices=full_matrices, compute_uv=compute_uv)
  911. U, sigma, V = apply(op, inp)
  912. return U, sigma, V
  913. def _check_non_finite(inps: Iterable[Tensor], scale=1.0) -> Tensor:
  914. r"""Check whether input contains infinite or nan value.
  915. Args:
  916. inp: a tensor to be checked.
  917. Returns:
  918. a int32 scalar tensor, 0 for False and 1 for True.
  919. """
  920. op = builtin.CheckNonFinite(scale=scale)
  921. oups = apply(op, *inps)
  922. out = oups[-1]
  923. for i in range(len(inps)):
  924. inps[i]._reset(oups[i])
  925. out._setscalar()
  926. return out