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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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 functools
  11. import math
  12. import numbers
  13. from typing import Optional, Sequence, Tuple, Union
  14. from ..core._imperative_rt.core2 import apply
  15. from ..core._trace_option import use_symbolic_shape
  16. from ..core.ops import builtin
  17. from ..core.ops.special import Const
  18. from ..core.tensor import utils
  19. from ..tensor import Tensor
  20. from .debug_param import get_conv_execution_strategy
  21. from .elemwise import clip, exp, log, log1p
  22. from .tensor import broadcast_to, concat, expand_dims, reshape, squeeze
  23. __all__ = [
  24. "argmax",
  25. "argmin",
  26. "argsort",
  27. "dot",
  28. "isinf",
  29. "isnan",
  30. "matmul",
  31. "max",
  32. "mean",
  33. "min",
  34. "norm",
  35. "normalize",
  36. "prod",
  37. "sign",
  38. "sort",
  39. "std",
  40. "sum",
  41. "svd",
  42. "topk",
  43. "var",
  44. ]
  45. def isnan(inp: Tensor) -> Tensor:
  46. r"""
  47. Returns a new tensor representing if each element is ``NaN`` or not.
  48. :param inp: input tensor.
  49. :return: result tensor.
  50. Examples:
  51. .. testcode::
  52. from megengine import tensor
  53. import megengine.functional as F
  54. x = tensor([1, float("nan"), 0])
  55. print(F.isnan(x).numpy())
  56. Outputs:
  57. .. testoutput::
  58. [False True False]
  59. """
  60. return inp != inp
  61. def isinf(inp: Tensor) -> Tensor:
  62. r"""
  63. Returns a new tensor representing if each element is ``Inf`` or not.
  64. :param inp: input tensor.
  65. :return: result tensor.
  66. Examples:
  67. .. testcode::
  68. from megengine import tensor
  69. import megengine.functional as F
  70. x = tensor([1, float("inf"), 0])
  71. print(F.isinf(x).numpy())
  72. Outputs:
  73. .. testoutput::
  74. [False True False]
  75. """
  76. return abs(inp).astype("float32") == float("inf")
  77. def sign(inp: Tensor):
  78. r"""
  79. Returns a new tensor representing the sign of each element in input tensor.
  80. :param: input tensor.
  81. :return: the sign of input tensor.
  82. Examples:
  83. .. testcode::
  84. from megengine import tensor
  85. import megengine.functional as F
  86. x = tensor([1, -1, 0])
  87. print(F.sign(x).numpy())
  88. Outputs:
  89. .. testoutput::
  90. [ 1 -1 0]
  91. """
  92. return (inp > 0).astype(inp.dtype) - (inp < 0).astype(inp.dtype)
  93. def sum(
  94. inp: Tensor,
  95. axis: Optional[Union[int, Sequence[int]]] = None,
  96. keepdims: bool = False,
  97. ) -> Tensor:
  98. r"""
  99. Returns the sum of input tensor along given axis. If axis is a list of dimensions,
  100. reduce over all of them.
  101. :param inp: input tensor.
  102. :param axis: dimension to reduce. If None, all dimensions will be reduced.
  103. Default: None
  104. :param keepdims: whether the output tensor has axis retained or not.
  105. Default: False
  106. :return: output tensor.
  107. Examples:
  108. .. testcode::
  109. import numpy as np
  110. from megengine import tensor
  111. import megengine.functional as F
  112. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
  113. out = F.sum(x)
  114. print(out.numpy())
  115. Outputs:
  116. .. testoutput::
  117. 21
  118. """
  119. return inp.sum(axis=axis, keepdims=keepdims)
  120. def prod(
  121. inp: Tensor, axis: Optional[Union[int, Sequence[int]]] = None, keepdims=False
  122. ) -> Tensor:
  123. r"""
  124. Returns the product of input tensor along given axis. If axis is a list of dimensions,
  125. reduce over all of them.
  126. :param inp: input tensor.
  127. :param axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  128. :param keepdims: whether the output tensor has axis retained or not. Default: False
  129. :return: output tensor.
  130. Examples:
  131. .. testcode::
  132. import numpy as np
  133. from megengine import tensor
  134. import megengine.functional as F
  135. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
  136. out = F.prod(x)
  137. print(out.numpy())
  138. Outputs:
  139. .. testoutput::
  140. 720
  141. """
  142. return inp.prod(axis=axis, keepdims=keepdims)
  143. def mean(
  144. inp: Tensor,
  145. axis: Optional[Union[int, Sequence[int]]] = None,
  146. keepdims: bool = False,
  147. ) -> Tensor:
  148. """
  149. Returns the mean value of input tensor along
  150. given axis. If axis is a list of dimensions,
  151. reduce over all of them.
  152. :param inp: input tensor.
  153. :param axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  154. :param keepdims: whether the output tensor has axis retained or not. Default: False
  155. :return: output tensor.
  156. Examples:
  157. .. testcode::
  158. import numpy as np
  159. from megengine import tensor
  160. import megengine.functional as F
  161. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
  162. out = F.mean(x)
  163. print(out.numpy())
  164. Outputs:
  165. .. testoutput::
  166. 3.5
  167. """
  168. return inp.mean(axis=axis, keepdims=keepdims)
  169. def var(
  170. inp: Tensor,
  171. axis: Optional[Union[int, Sequence[int]]] = None,
  172. keepdims: bool = False,
  173. ) -> Tensor:
  174. """
  175. Returns the variance value of input tensor along
  176. given axis. If axis is a list of dimensions,
  177. reduce over all of them.
  178. :param inp: input tensor.
  179. :param axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  180. :param keepdims: whether the output tensor has axis retained or not. Default: False
  181. :return: output tensor.
  182. Examples:
  183. .. testcode::
  184. import numpy as np
  185. from megengine import tensor
  186. import megengine.functional as F
  187. data = tensor(np.arange(1, 7, dtype=np.float32).reshape(2, 3))
  188. out = F.var(data)
  189. print(out.numpy().round(decimals=4))
  190. Outputs:
  191. .. testoutput::
  192. 2.9167
  193. """
  194. if axis is None:
  195. m = mean(inp, axis=axis, keepdims=False)
  196. else:
  197. m = mean(inp, axis=axis, keepdims=True)
  198. v = inp - m
  199. return mean(v ** 2, axis=axis, keepdims=keepdims)
  200. def std(
  201. inp: Tensor,
  202. axis: Optional[Union[int, Sequence[int]]] = None,
  203. keepdims: bool = False,
  204. ) -> Tensor:
  205. """
  206. Returns the standard deviation of input tensor along
  207. given axis. If axis is a list of dimensions,
  208. reduce over all of them.
  209. :param inp: input tensor.
  210. :param axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  211. :param keepdims: whether the output tensor has axis retained or not. Default: False
  212. :return: output tensor.
  213. Examples:
  214. .. testcode::
  215. import numpy as np
  216. from megengine import tensor
  217. import megengine.functional as F
  218. data = tensor(np.arange(1, 7, dtype=np.float32).reshape(2, 3))
  219. out = F.std(data, axis=1)
  220. print(out.numpy().round(decimals=4))
  221. Outputs:
  222. .. testoutput::
  223. [0.8165 0.8165]
  224. """
  225. return var(inp, axis=axis, keepdims=keepdims) ** 0.5
  226. def min(
  227. inp: Tensor,
  228. axis: Optional[Union[int, Sequence[int]]] = None,
  229. keepdims: bool = False,
  230. ) -> Tensor:
  231. r"""
  232. Returns the min value of input tensor along
  233. given axis. If axis is a list of dimensions,
  234. reduce over all of them.
  235. :param inp: input tensor.
  236. :param axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  237. :param keepdims: whether the output tensor has axis retained or not. Default: False
  238. :return: output tensor.
  239. Examples:
  240. .. testcode::
  241. import numpy as np
  242. from megengine import tensor
  243. import megengine.functional as F
  244. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  245. out = F.min(x)
  246. print(out.numpy())
  247. Outputs:
  248. .. testoutput::
  249. 1
  250. """
  251. return inp.min(axis=axis, keepdims=keepdims)
  252. def max(
  253. inp: Tensor,
  254. axis: Optional[Union[int, Sequence[int]]] = None,
  255. keepdims: bool = False,
  256. ) -> Tensor:
  257. r"""
  258. Returns the max value of the input tensor along
  259. given axis. If axis is a list of dimensions,
  260. reduce over all of them.
  261. :param inp: input tensor.
  262. :param axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  263. :param keepdims: whether the output tensor has axis retained or not. Default: False
  264. :return: output tensor.
  265. Examples:
  266. .. testcode::
  267. import numpy as np
  268. from megengine import tensor
  269. import megengine.functional as F
  270. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  271. out = F.max(x)
  272. print(out.numpy())
  273. Outputs:
  274. .. testoutput::
  275. 6
  276. """
  277. return inp.max(axis=axis, keepdims=keepdims)
  278. def norm(
  279. inp: Tensor, ord: float = None, axis: int = None, keepdims=False,
  280. ):
  281. """
  282. Calculates ``p``-norm of input tensor along
  283. given axis.
  284. :param inp: input tensor.
  285. :param ord: power of value applied to inp. Default: 2
  286. :param axis: dimension to reduce. If None, input must be a vector. Default: None
  287. :param keepdims: whether the output tensor has axis retained or not. Default: False
  288. :return: output tensor.
  289. Examples:
  290. .. testcode::
  291. import numpy as np
  292. from megengine import tensor
  293. import megengine.functional as F
  294. x = tensor(np.arange(-3, 3, dtype=np.float32))
  295. out = F.norm(x)
  296. print(out.numpy().round(decimals=4))
  297. Outputs:
  298. .. testoutput::
  299. 4.3589
  300. """
  301. if axis is None:
  302. if inp.ndim != 1:
  303. raise TypeError("axis is required unless input is a vector")
  304. if ord is None:
  305. ord = 2
  306. if ord == 0:
  307. return sum(inp != 0, axis=axis, keepdims=keepdims)
  308. if ord == math.inf:
  309. return max(abs(inp))
  310. if ord == -math.inf:
  311. return min(abs(inp))
  312. return sum(abs(inp) ** ord, axis=axis, keepdims=keepdims) ** (1.0 / ord)
  313. def argmin(
  314. inp: Tensor,
  315. axis: Optional[Union[int, Sequence[int]]] = None,
  316. keepdims: bool = False,
  317. ) -> Tensor:
  318. r"""
  319. Returns the indices of the minimum values along
  320. given axis. If axis is a list of dimensions,
  321. reduce over all of them.
  322. :param inp: input tensor.
  323. :param axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  324. :param keepdims: whether the output tensor has axis retained or not. Default: False
  325. :return: output tensor.
  326. Examples:
  327. .. testcode::
  328. import numpy as np
  329. from megengine import tensor
  330. import megengine.functional as F
  331. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  332. out = F.argmin(x)
  333. print(out.numpy())
  334. Outputs:
  335. .. testoutput::
  336. 0
  337. """
  338. if isinstance(axis, collections.abc.Iterable):
  339. axis = list(axis)
  340. axis.sort(reverse=True)
  341. for ai in axis:
  342. op = builtin.Argmin(axis=ai)
  343. (inp,) = apply(op, inp)
  344. if not keepdims:
  345. inp = squeeze(inp, ai)
  346. return inp
  347. if axis is None:
  348. assert not keepdims, "can not set axis=None and keepdims=True"
  349. inp = inp.flatten()
  350. axis = 0
  351. op = builtin.Argmin(axis=axis)
  352. (result,) = apply(op, inp)
  353. if not keepdims:
  354. result = squeeze(result, axis)
  355. return result
  356. def argmax(
  357. inp: Tensor,
  358. axis: Optional[Union[int, Sequence[int]]] = None,
  359. keepdims: bool = False,
  360. ) -> Tensor:
  361. r"""
  362. Returns the indices of the maximum values along
  363. given axis. If axis is a list of dimensions,
  364. reduce over all of them.
  365. :param inp: input tensor.
  366. :param axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
  367. :param keepdims: whether the output tensor has axis retained or not. Default: False
  368. :return: output tensor.
  369. Examples:
  370. .. testcode::
  371. import numpy as np
  372. from megengine import tensor
  373. import megengine.functional as F
  374. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  375. out = F.argmax(x)
  376. print(out.numpy())
  377. Outputs:
  378. .. testoutput::
  379. 5
  380. """
  381. if isinstance(axis, collections.abc.Iterable):
  382. axis = list(axis)
  383. axis.sort(reverse=True)
  384. for ai in axis:
  385. op = builtin.Argmax(axis=ai)
  386. (inp,) = apply(op, inp)
  387. if not keepdims:
  388. inp = squeeze(inp, ai)
  389. return inp
  390. if axis is None:
  391. assert not keepdims, "can not set axis=None and keepdims=True"
  392. inp = inp.flatten()
  393. axis = 0
  394. op = builtin.Argmax(axis=axis)
  395. (result,) = apply(op, inp)
  396. if not keepdims:
  397. result = squeeze(result, axis)
  398. return result
  399. def normalize(
  400. inp: Tensor, ord: float = None, axis: int = None, eps: float = 1e-12,
  401. ) -> Tensor:
  402. r"""
  403. Performs :math:`L_p` normalization of input tensor along
  404. given axis.
  405. For a tensor of shape :math:`(n_0, ..., n_{dim}, ..., n_k)`, each
  406. :math:`n_{dim}` -element vector :math:`v` along dimension :attr:`axis` is transformed as:
  407. .. math::
  408. v = \frac{v}{\max(\lVert v \rVert_p, \epsilon)}.
  409. :param inp: input tensor.
  410. :param ord: power of value applied to input tensor. Default: 2
  411. :param axis: dimension to reduce.If None, input must be a vector. Default: None
  412. :param eps: a small value to avoid division by zero. Default: 1e-12
  413. :return: normalized output tensor.
  414. """
  415. if axis is None:
  416. return inp / clip(norm(inp, ord, axis), lower=eps)
  417. else:
  418. return inp / clip(norm(inp, ord, axis, keepdims=True), lower=eps)
  419. def argsort(inp: Tensor, descending: bool = False) -> Tensor:
  420. r"""
  421. Returns the indices that would sort the input tensor.
  422. :param inp: input tensor. If it's 2d, the result would be array of indices show how to sort each row in the input tensor.
  423. :param descending: sort in descending order, where the largest comes first. Default: False
  424. :return: indices of int32 indicates how to sort the input.
  425. Examples:
  426. .. testcode::
  427. import numpy as np
  428. from megengine import tensor
  429. import megengine.functional as F
  430. x = tensor(np.array([1,2], dtype=np.float32))
  431. indices = F.argsort(x)
  432. print(indices.numpy())
  433. Outputs:
  434. .. testoutput::
  435. [0 1]
  436. """
  437. assert len(inp.shape) <= 2, "Input should be 1d or 2d"
  438. if descending:
  439. order = "DESCENDING"
  440. else:
  441. order = "ASCENDING"
  442. op = builtin.Argsort(order=order)
  443. if len(inp.shape) == 1:
  444. inp = inp.reshape(1, -1)
  445. _, result = apply(op, inp)
  446. return result[0]
  447. _, result = apply(op, inp)
  448. return result
  449. def sort(inp: Tensor, descending: bool = False) -> Tuple[Tensor, Tensor]:
  450. r"""
  451. Returns sorted tensor and the indices would sort the input tensor.
  452. :param inp: input tensor. If it's 2d, the result would be sorted by row.
  453. :param descending: sort in descending order, where the largest comes first. Default: False
  454. :return: tuple of two tensors `(sorted_tensor, indices_of_int32)`.
  455. Examples:
  456. .. testcode::
  457. import numpy as np
  458. from megengine import tensor
  459. import megengine.functional as F
  460. x = tensor(np.array([1,2], dtype=np.float32))
  461. out, indices = F.sort(x)
  462. print(out.numpy())
  463. Outputs:
  464. .. testoutput::
  465. [1. 2.]
  466. """
  467. assert len(inp.shape) <= 2, "Input should be 1d or 2d"
  468. if descending:
  469. order = "DESCENDING"
  470. else:
  471. order = "ASCENDING"
  472. op = builtin.Argsort(order=order)
  473. if len(inp.shape) == 1:
  474. inp = inp.reshape(1, -1)
  475. tns, ind = apply(op, inp)
  476. return tns[0], ind[0]
  477. tns, ind = apply(op, inp)
  478. return tns, ind
  479. def topk(
  480. inp: Tensor,
  481. k: int,
  482. descending: bool = False,
  483. kth_only: bool = False,
  484. no_sort: bool = False,
  485. ) -> Tuple[Tensor, Tensor]:
  486. r"""
  487. Selects the ``Top-K`` (by default) smallest elements of 2d matrix by row.
  488. :param inp: input tensor. If input tensor is 2d, each row will be sorted.
  489. :param k: number of elements needed.
  490. :param descending: if True, return the largest elements instead. Default: False
  491. :param kth_only: if True, only the k-th element will be returned. Default: False
  492. :param no_sort: if True, the returned elements can be unordered. Default: False
  493. :return: tuple of two tensors `(topk_tensor, indices_of_int32)`.
  494. Examples:
  495. .. testcode::
  496. import numpy as np
  497. from megengine import tensor
  498. import megengine.functional as F
  499. x = tensor(np.array([2, 4, 6, 8, 7, 5, 3, 1], dtype=np.float32))
  500. top, indices = F.topk(x, 5)
  501. print(top.numpy(), indices.numpy())
  502. Outputs:
  503. .. testoutput::
  504. [1. 2. 3. 4. 5.] [7 0 6 1 5]
  505. """
  506. if descending:
  507. inp = -inp
  508. if kth_only:
  509. mode = "KTH_ONLY"
  510. elif no_sort:
  511. mode = "VALUE_IDX_NOSORT"
  512. else:
  513. mode = "VALUE_IDX_SORTED"
  514. op = builtin.TopK(mode=mode)
  515. if not isinstance(k, Tensor):
  516. (k,) = Const(k, dtype="int32", device=inp.device)()
  517. if len(inp.shape) == 1:
  518. inp = inp.reshape(1, -1)
  519. res = apply(op, inp, k)
  520. if kth_only:
  521. tns = res[0]
  522. else:
  523. tns, ind = res[0][0], res[1][0]
  524. else:
  525. res = apply(op, inp, k)
  526. if kth_only:
  527. tns = res
  528. else:
  529. tns, ind = res[0], res[1]
  530. if descending:
  531. tns = -tns
  532. return tns, ind
  533. def matmul(
  534. inp1: Tensor,
  535. inp2: Tensor,
  536. transpose_a=False,
  537. transpose_b=False,
  538. compute_mode="DEFAULT",
  539. format="DEFAULT",
  540. ) -> Tensor:
  541. """
  542. Performs a matrix multiplication of the matrices ``inp1`` and ``inp2``.
  543. With different inputs dim, this function behaves differently:
  544. - Both 1-D tensor, simply forward to ``dot``.
  545. - Both 2-D tensor, normal matrix multiplication.
  546. - If one input tensor is 1-D, matrix vector multiplication.
  547. - If at least one tensor are 3-dimensional or >3-dimensional, the other tensor should have dim >= 2, the batched matrix-matrix is returned, and the tensor with smaller dimension will be broadcasted. For example:
  548. - inp1: `(n, k, m)`, inp2: `(n, m, p)`, return: `(n, k, p)`
  549. - inp1: `(n, k, m)`, inp2: `(m, p)`, return: `(n, k, p)`
  550. - inp1: `(n, j, k, m)`, inp2: `(n, j, m, p)`, return: `(n, j, k, p)`
  551. :param inp1: first matrix to be multiplied.
  552. :param inp2: second matrix to be multiplied.
  553. :return: output tensor.
  554. Examples:
  555. .. testcode::
  556. import numpy as np
  557. from megengine import tensor
  558. import megengine.functional as F
  559. data1 = tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3))
  560. data2 = tensor(np.arange(0, 6, dtype=np.float32).reshape(3, 2))
  561. out = F.matmul(data1, data2)
  562. print(out.numpy())
  563. Outputs:
  564. .. testoutput::
  565. [[10. 13.]
  566. [28. 40.]]
  567. """
  568. remove_row, remove_col = False, False
  569. inp1, inp2 = utils.convert_inputs(inp1, inp2)
  570. dim1, dim2 = inp1.ndim, inp2.ndim
  571. # handle dim=1 cases, dot and matrix-vector multiplication
  572. if dim1 == 1 and dim2 == 1:
  573. return dot(inp1, inp2)
  574. # the underlying matmul op requires input dims to be at least 2
  575. if dim1 == 1:
  576. inp1 = expand_dims(inp1, 0)
  577. dim1 = 2
  578. remove_row = True
  579. if dim2 == 1:
  580. inp2 = expand_dims(inp2, 1)
  581. dim2 = 2
  582. remove_col = True
  583. batch_shape = None
  584. shape1 = inp1.shape
  585. shape2 = inp2.shape
  586. maxdim = dim1 if dim1 > dim2 else dim2
  587. if dim1 >= 3 or dim2 >= 3:
  588. if use_symbolic_shape():
  589. if dim1 > dim2:
  590. shape2 = concat([shape1[:-2], shape2[-2:]])
  591. inp2 = broadcast_to(inp2, shape2)
  592. if dim1 < dim2:
  593. shape1 = concat([shape2[:-2], shape1[-2:]])
  594. inp1 = broadcast_to(inp1, shape1)
  595. if maxdim > 3:
  596. batch_shape = shape1[:-2]
  597. # compress inputs to 3d
  598. (inp1,) = apply(
  599. builtin.Reshape(), inp1, concat([prod(shape1[:-2]), shape1[-2:]])
  600. )
  601. (inp2,) = apply(
  602. builtin.Reshape(), inp2, concat([prod(shape2[:-2]), shape2[-2:]])
  603. )
  604. else:
  605. if dim1 > dim2:
  606. shape2 = shape1[:-2] + shape2[-2:]
  607. inp2 = broadcast_to(inp2, shape2)
  608. if dim1 < dim2:
  609. shape1 = shape2[:-2] + shape1[-2:]
  610. inp1 = broadcast_to(inp1, shape1)
  611. if maxdim > 3:
  612. batch_shape = shape1[:-2]
  613. # compress inputs to 3d
  614. inp1 = inp1.reshape((-1, shape1[-2], shape1[-1]))
  615. inp2 = inp2.reshape((-1, shape2[-2], shape2[-1]))
  616. op = builtin.BatchedMatrixMul(
  617. transposeA=transpose_a,
  618. transposeB=transpose_b,
  619. compute_mode=compute_mode,
  620. format=format,
  621. strategy=get_conv_execution_strategy(),
  622. )
  623. else:
  624. op = builtin.MatrixMul(
  625. transposeA=transpose_a,
  626. transposeB=transpose_b,
  627. compute_mode=compute_mode,
  628. format=format,
  629. strategy=get_conv_execution_strategy(),
  630. )
  631. (result,) = apply(op, inp1, inp2)
  632. if maxdim > 3:
  633. if use_symbolic_shape():
  634. (result,) = apply(
  635. builtin.Reshape(), result, concat([batch_shape, result.shape[-2:]])
  636. )
  637. else:
  638. result = result.reshape(batch_shape + result.shape[-2:])
  639. if remove_row:
  640. result = squeeze(result, axis=-2)
  641. if remove_col:
  642. result = squeeze(result, axis=-1)
  643. return result
  644. def dot(inp1: Tensor, inp2: Tensor) -> Tensor:
  645. """
  646. Computes dot-product of two vectors ``inp1`` and ``inp2``.
  647. inputs must be 1-dimensional or scalar. A scalar input is automatically broadcasted.
  648. Refer to :func:`~.matmul` for more general usage.
  649. :param inp1: first vector.
  650. :param inp2: second vector.
  651. :return: output value.
  652. Examples:
  653. .. testcode::
  654. import numpy as np
  655. from megengine import tensor
  656. import megengine.functional as F
  657. data1 = tensor(np.arange(0, 6, dtype=np.float32))
  658. data2 = tensor(np.arange(0, 6, dtype=np.float32))
  659. out = F.dot(data1, data2)
  660. print(out.numpy())
  661. Outputs:
  662. .. testoutput::
  663. 55.
  664. """
  665. op = builtin.Dot()
  666. inp1, inp2 = utils.convert_inputs(inp1, inp2)
  667. assert (
  668. inp1.ndim <= 1 and inp2.ndim <= 1
  669. ), "Input tensors for dot must be 1-dimensional or scalar"
  670. (result,) = apply(op, inp1, inp2)
  671. utils.setscalar(result)
  672. return result
  673. def svd(inp: Tensor, full_matrices=False, compute_uv=True) -> Tensor:
  674. """
  675. Computes the singular value decompositions of input matrix.
  676. :param inp: input matrix, must has shape `[..., M, N]`.
  677. :return: output matrices, `(U, sigma, V)`.
  678. Examples:
  679. .. testcode::
  680. import numpy as np
  681. from megengine import tensor
  682. import megengine.functional as F
  683. x = tensor(np.arange(0, 6, dtype=np.float32).reshape(2,3))
  684. _, y, _ = F.svd(x)
  685. print(y.numpy().round(decimals=3))
  686. Outputs:
  687. .. testoutput::
  688. [7.348 1. ]
  689. """
  690. op = builtin.SVD(full_matrices=full_matrices, compute_uv=compute_uv)
  691. U, sigma, V = apply(op, inp)
  692. return U, sigma, V

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台