ops.py 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only
  15. from .. import _C_ops
  16. from ..base.data_feeder import check_variable_and_dtype
  17. from ..framework import LayerHelper, in_dynamic_or_pir_mode
  18. from .layer_function_generator import (
  19. generate_activation_fn,
  20. generate_inplace_fn,
  21. generate_layer_fn,
  22. )
  23. __inplace_unary_func__ = [
  24. 'exp_',
  25. 'sqrt_',
  26. 'rsqrt_',
  27. 'ceil_',
  28. 'floor_',
  29. 'round_',
  30. 'reciprocal_',
  31. 'sigmoid_',
  32. 'abs_',
  33. 'sin_',
  34. 'sinh_',
  35. 'asin_',
  36. 'asinh_',
  37. 'cos_',
  38. 'cosh_',
  39. 'acos_',
  40. 'acosh_',
  41. 'tan_',
  42. 'atan_',
  43. 'atanh_',
  44. 'expm1_',
  45. 'erf_',
  46. 'square_',
  47. ]
  48. __all__ = []
  49. # It is a hot fix in some unittest using:
  50. # paddle.scale(x=x, scale=10.0, out=out_var)
  51. # e.g.: test_program_code.py, test_dist_train.py
  52. globals()['_scale'] = generate_layer_fn('scale')
  53. for _OP in set(__inplace_unary_func__):
  54. func = generate_inplace_fn(_OP)
  55. func.__module__ = __name__
  56. _func = inplace_apis_in_dygraph_only(func)
  57. globals()[_OP] = _func
  58. def abs(x, name=None):
  59. """
  60. Perform elementwise abs for input `x`.
  61. .. math::
  62. out = |x|
  63. Args:
  64. x (Tensor): The input Tensor with data type int32, int64, float16, float32 and float64.
  65. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  66. Returns:
  67. Tensor.A Tensor with the same data type and shape as :math:`x`.
  68. Examples:
  69. .. code-block:: python
  70. >>> import paddle
  71. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  72. >>> out = paddle.abs(x)
  73. >>> print(out)
  74. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  75. [0.40000001, 0.20000000, 0.10000000, 0.30000001])
  76. """
  77. return generate_activation_fn('abs')(x, name)
  78. def acos(x, name=None):
  79. """
  80. Acos Activation Operator.
  81. .. math::
  82. out = cos^{-1}(x)
  83. Args:
  84. x (Tensor): Input of Acos operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128.
  85. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  86. Returns:
  87. Tensor. Output of Acos operator, a Tensor with shape same as input.
  88. Examples:
  89. .. code-block:: python
  90. >>> import paddle
  91. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  92. >>> out = paddle.acos(x)
  93. >>> print(out)
  94. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  95. [1.98231316, 1.77215421, 1.47062886, 1.26610363])
  96. """
  97. if in_dynamic_or_pir_mode():
  98. return _C_ops.acos(x)
  99. else:
  100. check_variable_and_dtype(
  101. x,
  102. 'x',
  103. [
  104. 'float16',
  105. 'uint16',
  106. 'float32',
  107. 'float64',
  108. 'complex64',
  109. 'complex128',
  110. ],
  111. 'acos',
  112. )
  113. helper = LayerHelper('acos', **locals())
  114. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  115. helper.append_op(type='acos', inputs={"X": x}, outputs={"Out": out})
  116. return out
  117. def acosh(x, name=None):
  118. """
  119. Acosh Activation Operator.
  120. .. math::
  121. out = acosh(x)
  122. Args:
  123. x (Tensor): Input of Acosh operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128.
  124. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  125. Returns:
  126. Tensor. Output of Acosh operator, a Tensor with shape same as input.
  127. Examples:
  128. .. code-block:: python
  129. >>> import paddle
  130. >>> x = paddle.to_tensor([1., 3., 4., 5.])
  131. >>> out = paddle.acosh(x)
  132. >>> print(out)
  133. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  134. [0. , 1.76274717, 2.06343699, 2.29243159])
  135. """
  136. if in_dynamic_or_pir_mode():
  137. return _C_ops.acosh(x)
  138. else:
  139. check_variable_and_dtype(
  140. x,
  141. 'x',
  142. [
  143. 'float16',
  144. 'uint16',
  145. 'float32',
  146. 'float64',
  147. 'complex64',
  148. 'complex128',
  149. ],
  150. 'acosh',
  151. )
  152. helper = LayerHelper('acosh', **locals())
  153. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  154. helper.append_op(type='acosh', inputs={"X": x}, outputs={"Out": out})
  155. return out
  156. def asin(x, name=None):
  157. """
  158. Arcsine Operator.
  159. .. math::
  160. out = sin^{-1}(x)
  161. Args:
  162. x (Tensor): Input of Asin operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128.
  163. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  164. Returns:
  165. Tensor. Same shape and dtype as input.
  166. Examples:
  167. .. code-block:: python
  168. >>> import paddle
  169. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  170. >>> out = paddle.asin(x)
  171. >>> print(out)
  172. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  173. [-0.41151685, -0.20135793, 0.10016742, 0.30469266])
  174. """
  175. if in_dynamic_or_pir_mode():
  176. return _C_ops.asin(x)
  177. else:
  178. check_variable_and_dtype(
  179. x,
  180. 'x',
  181. [
  182. 'float16',
  183. 'uint16',
  184. 'float32',
  185. 'float64',
  186. 'complex64',
  187. 'complex128',
  188. ],
  189. 'asin',
  190. )
  191. helper = LayerHelper('asin', **locals())
  192. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  193. helper.append_op(type='asin', inputs={"X": x}, outputs={"Out": out})
  194. return out
  195. def asinh(x, name=None):
  196. """
  197. Asinh Activation Operator.
  198. .. math::
  199. out = asinh(x)
  200. Args:
  201. x (Tensor): Input of Asinh operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128.
  202. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  203. Returns:
  204. Tensor. Output of Asinh operator, a Tensor with shape same as input.
  205. Examples:
  206. .. code-block:: python
  207. >>> import paddle
  208. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  209. >>> out = paddle.asinh(x)
  210. >>> print(out)
  211. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  212. [-0.39003533, -0.19869010, 0.09983408, 0.29567307])
  213. """
  214. if in_dynamic_or_pir_mode():
  215. return _C_ops.asinh(x)
  216. else:
  217. check_variable_and_dtype(
  218. x,
  219. 'x',
  220. [
  221. 'float16',
  222. 'uint16',
  223. 'float32',
  224. 'float64',
  225. 'complex64',
  226. 'complex128',
  227. ],
  228. 'asinh',
  229. )
  230. helper = LayerHelper('asinh', **locals())
  231. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  232. helper.append_op(type='asinh', inputs={"X": x}, outputs={"Out": out})
  233. return out
  234. def atan(x, name=None):
  235. """
  236. Arctangent Operator.
  237. .. math::
  238. out = tan^{-1}(x)
  239. Args:
  240. x (Tensor): Input of Atan operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128.
  241. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  242. Returns:
  243. Tensor. Same shape and dtype as input x.
  244. Examples:
  245. .. code-block:: python
  246. >>> import paddle
  247. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  248. >>> out = paddle.atan(x)
  249. >>> print(out)
  250. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  251. [-0.38050640, -0.19739556, 0.09966865, 0.29145682])
  252. """
  253. if in_dynamic_or_pir_mode():
  254. return _C_ops.atan(x)
  255. else:
  256. check_variable_and_dtype(
  257. x,
  258. 'x',
  259. [
  260. 'float16',
  261. 'uint16',
  262. 'float32',
  263. 'float64',
  264. 'complex64',
  265. 'complex128',
  266. ],
  267. 'atan',
  268. )
  269. helper = LayerHelper('atan', **locals())
  270. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  271. helper.append_op(type='atan', inputs={"X": x}, outputs={"Out": out})
  272. return out
  273. def atanh(x, name=None):
  274. """
  275. Atanh Activation Operator.
  276. .. math::
  277. out = atanh(x)
  278. Args:
  279. x (Tensor): Input of Atan operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128.
  280. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  281. Returns:
  282. Tensor. Output of Atanh operator, a Tensor with shape same as input.
  283. Examples:
  284. .. code-block:: python
  285. >>> import paddle
  286. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  287. >>> out = paddle.atanh(x)
  288. >>> print(out)
  289. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  290. [-0.42364895, -0.20273255, 0.10033534, 0.30951962])
  291. """
  292. if in_dynamic_or_pir_mode():
  293. return _C_ops.atanh(x)
  294. else:
  295. check_variable_and_dtype(
  296. x,
  297. 'x',
  298. [
  299. 'float16',
  300. 'uint16',
  301. 'float32',
  302. 'float64',
  303. 'complex64',
  304. 'complex128',
  305. ],
  306. 'atanh',
  307. )
  308. helper = LayerHelper('atanh', **locals())
  309. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  310. helper.append_op(type='atanh', inputs={"X": x}, outputs={"Out": out})
  311. return out
  312. def ceil(x, name=None):
  313. """
  314. Ceil Operator. Computes ceil of x element-wise.
  315. .. math::
  316. out = \\left \\lceil x \\right \\rceil
  317. Args:
  318. x (Tensor): Input of Ceil operator, an N-D Tensor, with data type float32, float64 or float16.
  319. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  320. Returns:
  321. Tensor. Output of Ceil operator, a Tensor with shape same as input.
  322. Examples:
  323. .. code-block:: python
  324. >>> import paddle
  325. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  326. >>> out = paddle.ceil(x)
  327. >>> print(out)
  328. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  329. [-0., -0., 1. , 1. ])
  330. """
  331. if in_dynamic_or_pir_mode():
  332. return _C_ops.ceil(x)
  333. else:
  334. check_variable_and_dtype(
  335. x, 'x', ['float16', 'uint16', 'float32', 'float64'], 'ceil'
  336. )
  337. helper = LayerHelper('ceil', **locals())
  338. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  339. helper.append_op(type='ceil', inputs={"X": x}, outputs={"Out": out})
  340. return out
  341. def cos(x, name=None):
  342. """
  343. Cosine Operator. Computes cosine of x element-wise.
  344. Input range is `(-inf, inf)` and output range is `[-1,1]`.
  345. .. math::
  346. out = cos(x)
  347. Args:
  348. x (Tensor): Input of Cos operator, an N-D Tensor, with data type float32, float64 or float16.
  349. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  350. Returns:
  351. Tensor. Output of Cos operator, a Tensor with shape same as input.
  352. Examples:
  353. .. code-block:: python
  354. >>> import paddle
  355. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  356. >>> out = paddle.cos(x)
  357. >>> print(out)
  358. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  359. [0.92106098, 0.98006660, 0.99500418, 0.95533651])
  360. """
  361. if in_dynamic_or_pir_mode():
  362. return _C_ops.cos(x)
  363. else:
  364. check_variable_and_dtype(
  365. x,
  366. 'x',
  367. ['float16', 'float32', 'float64', 'complex64', 'complex128'],
  368. 'cos',
  369. )
  370. helper = LayerHelper('cos', **locals())
  371. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  372. helper.append_op(type='cos', inputs={"X": x}, outputs={"Out": out})
  373. return out
  374. def cosh(x, name=None):
  375. """
  376. Cosh Activation Operator.
  377. Input range `(-inf, inf)`, output range `(1, inf)`.
  378. .. math::
  379. out = \\frac{exp(x)+exp(-x)}{2}
  380. Args:
  381. x (Tensor): Input of Cosh operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128.
  382. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  383. Returns:
  384. Tensor. Output of Cosh operator, a Tensor with shape same as input.
  385. Examples:
  386. .. code-block:: python
  387. >>> import paddle
  388. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  389. >>> out = paddle.cosh(x)
  390. >>> print(out)
  391. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  392. [1.08107233, 1.02006674, 1.00500417, 1.04533851])
  393. """
  394. if in_dynamic_or_pir_mode():
  395. return _C_ops.cosh(x)
  396. else:
  397. check_variable_and_dtype(
  398. x,
  399. 'x',
  400. [
  401. 'float16',
  402. 'uint16',
  403. 'float32',
  404. 'float64',
  405. 'complex64',
  406. 'complex128',
  407. ],
  408. 'cosh',
  409. )
  410. helper = LayerHelper('cosh', **locals())
  411. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  412. helper.append_op(type='cosh', inputs={"X": x}, outputs={"Out": out})
  413. return out
  414. def exp(x, name=None):
  415. """
  416. Computes exp of x element-wise with a natural number `e` as the base.
  417. .. math::
  418. out = e^x
  419. Args:
  420. x (Tensor): Input of Exp operator, an N-D Tensor, with data type int32, int64, float16, float32, float64, complex64 or complex128.
  421. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  422. Returns:
  423. Tensor. Output of Exp operator, a Tensor with shape same as input.
  424. Examples:
  425. .. code-block:: python
  426. >>> import paddle
  427. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  428. >>> out = paddle.exp(x)
  429. >>> print(out)
  430. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  431. [0.67032003, 0.81873077, 1.10517097, 1.34985888])
  432. """
  433. if in_dynamic_or_pir_mode():
  434. return _C_ops.exp(x)
  435. else:
  436. check_variable_and_dtype(
  437. x,
  438. 'x',
  439. [
  440. 'int32',
  441. 'int64',
  442. 'uint16',
  443. 'float16',
  444. 'float32',
  445. 'float64',
  446. 'complex64',
  447. 'complex128',
  448. ],
  449. 'exp',
  450. )
  451. helper = LayerHelper('exp', **locals())
  452. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  453. helper.append_op(type='exp', inputs={"X": x}, outputs={"Out": out})
  454. return out
  455. def expm1(x, name=None):
  456. """
  457. Expm1 Operator. Computes expm1 of x element-wise with a natural number :math:`e` as the base.
  458. .. math::
  459. out = e^x - 1
  460. Args:
  461. x (Tensor): Input of Expm1 operator, an N-D Tensor, with data type int32, int64, float16, float32, float64, complex64 or complex128.
  462. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  463. Returns:
  464. Tensor. Output of Expm1 operator, a Tensor with shape same as input.
  465. Examples:
  466. .. code-block:: python
  467. >>> import paddle
  468. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  469. >>> out = paddle.expm1(x)
  470. >>> print(out)
  471. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  472. [-0.32967997, -0.18126924, 0.10517092, 0.34985882])
  473. """
  474. if in_dynamic_or_pir_mode():
  475. return _C_ops.expm1(x)
  476. else:
  477. check_variable_and_dtype(
  478. x,
  479. 'x',
  480. [
  481. 'float16',
  482. 'uint16',
  483. 'float32',
  484. 'float64',
  485. 'int32',
  486. 'int64',
  487. 'complex64',
  488. 'complex128',
  489. ],
  490. 'expm1',
  491. )
  492. helper = LayerHelper('expm1', **locals())
  493. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  494. helper.append_op(type='expm1', inputs={"X": x}, outputs={"Out": out})
  495. return out
  496. def floor(x, name=None):
  497. """
  498. Floor Activation Operator. Computes floor of x element-wise.
  499. .. math::
  500. out = \\lfloor x \\rfloor
  501. Args:
  502. x (Tensor): Input of Floor operator, an N-D Tensor, with data type float32, float64 or float16.
  503. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  504. Returns:
  505. Tensor. Output of Floor operator, a Tensor with shape same as input.
  506. Examples:
  507. .. code-block:: python
  508. >>> import paddle
  509. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  510. >>> out = paddle.floor(x)
  511. >>> print(out)
  512. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  513. [-1., -1., 0., 0.])
  514. """
  515. if in_dynamic_or_pir_mode():
  516. return _C_ops.floor(x)
  517. else:
  518. check_variable_and_dtype(
  519. x, 'x', ['float16', 'uint16', 'float32', 'float64'], 'floor'
  520. )
  521. helper = LayerHelper('floor', **locals())
  522. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  523. helper.append_op(type='floor', inputs={"X": x}, outputs={"Out": out})
  524. return out
  525. def reciprocal(x, name=None):
  526. """
  527. Reciprocal Activation Operator.
  528. .. math::
  529. out = \\frac{1}{x}
  530. Args:
  531. x (Tensor): Input of Reciprocal operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128.
  532. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  533. Returns:
  534. Tensor. Output of Reciprocal operator, a Tensor with shape same as input.
  535. Examples:
  536. .. code-block:: python
  537. >>> import paddle
  538. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  539. >>> out = paddle.reciprocal(x)
  540. >>> print(out)
  541. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  542. [-2.50000000, -5. , 10. , 3.33333325])
  543. """
  544. if in_dynamic_or_pir_mode():
  545. return _C_ops.reciprocal(x)
  546. else:
  547. check_variable_and_dtype(
  548. x, 'x', ['float16', 'uint16', 'float32', 'float64'], 'reciprocal'
  549. )
  550. helper = LayerHelper('reciprocal', **locals())
  551. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  552. helper.append_op(
  553. type='reciprocal', inputs={"X": x}, outputs={"Out": out}
  554. )
  555. return out
  556. def round(x, name=None):
  557. """
  558. Round the values in the input to the nearest integer value.
  559. .. code-block:: text
  560. input:
  561. x.shape = [4]
  562. x.data = [1.2, -0.9, 3.4, 0.9]
  563. output:
  564. out.shape = [4]
  565. out.data = [1., -1., 3., 1.]
  566. Args:
  567. x (Tensor): Input of Round operator, an N-D Tensor, with data type float32, float64 or float16.
  568. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  569. Returns:
  570. Tensor. Output of Round operator, a Tensor with shape same as input.
  571. Examples:
  572. .. code-block:: python
  573. >>> import paddle
  574. >>> x = paddle.to_tensor([-0.5, -0.2, 0.6, 1.5])
  575. >>> out = paddle.round(x)
  576. >>> print(out)
  577. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  578. [-1., -0., 1., 2.])
  579. """
  580. if in_dynamic_or_pir_mode():
  581. return _C_ops.round(x)
  582. else:
  583. check_variable_and_dtype(
  584. x, 'x', ['float16', 'uint16', 'float32', 'float64'], 'round'
  585. )
  586. helper = LayerHelper('round', **locals())
  587. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  588. helper.append_op(type='round', inputs={"X": x}, outputs={"Out": out})
  589. return out
  590. def rsqrt(x, name=None):
  591. """
  592. Rsqrt Activation Operator.
  593. Please make sure input is legal in case of numeric errors.
  594. .. math::
  595. out = \\frac{1}{\\sqrt{x}}
  596. Args:
  597. x (Tensor): Input of Rsqrt operator, an N-D Tensor, with data type float32, float64 or float16.
  598. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  599. Returns:
  600. Tensor. Output of Rsqrt operator, a Tensor with shape same as input.
  601. Examples:
  602. .. code-block:: python
  603. >>> import paddle
  604. >>> x = paddle.to_tensor([0.1, 0.2, 0.3, 0.4])
  605. >>> out = paddle.rsqrt(x)
  606. >>> print(out)
  607. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  608. [3.16227770, 2.23606801, 1.82574177, 1.58113885])
  609. """
  610. if in_dynamic_or_pir_mode():
  611. return _C_ops.rsqrt(x)
  612. else:
  613. check_variable_and_dtype(
  614. x, 'x', ['float16', 'uint16', 'float32', 'float64'], 'rsqrt'
  615. )
  616. helper = LayerHelper('rsqrt', **locals())
  617. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  618. helper.append_op(type='rsqrt', inputs={"X": x}, outputs={"Out": out})
  619. return out
  620. def sigmoid(x, name=None):
  621. """
  622. Sigmoid Activation.
  623. .. math::
  624. out = \\frac{1}{1 + e^{-x}}
  625. Args:
  626. x (Tensor): Input of Sigmoid operator, an N-D Tensor, with data type float16, float32, float64, complex64 or complex128.
  627. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  628. Returns:
  629. Tensor. Output of Sigmoid operator, a Tensor with shape same as input.
  630. Examples:
  631. .. code-block:: python
  632. >>> import paddle
  633. >>> import paddle.nn.functional as F
  634. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  635. >>> out = F.sigmoid(x)
  636. >>> print(out)
  637. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  638. [0.40131235, 0.45016602, 0.52497917, 0.57444251])
  639. """
  640. if in_dynamic_or_pir_mode():
  641. return _C_ops.sigmoid(x)
  642. else:
  643. check_variable_and_dtype(
  644. x,
  645. 'x',
  646. [
  647. 'float16',
  648. 'float32',
  649. 'float64',
  650. 'uint16',
  651. 'complex64',
  652. 'complex128',
  653. ],
  654. 'sigmoid',
  655. )
  656. helper = LayerHelper('sigmoid', **locals())
  657. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  658. helper.append_op(type='sigmoid', inputs={"X": x}, outputs={"Out": out})
  659. return out
  660. def sin(x, name=None):
  661. """
  662. Sine Activation Operator.
  663. .. math::
  664. out = sin(x)
  665. Args:
  666. x (Tensor): Input of Sin operator, an N-D Tensor, with data type float32, float64 or float16.
  667. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  668. Returns:
  669. Tensor. Output of Sin operator, a Tensor with shape same as input.
  670. Examples:
  671. .. code-block:: python
  672. >>> import paddle
  673. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  674. >>> out = paddle.sin(x)
  675. >>> print(out)
  676. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  677. [-0.38941833, -0.19866933, 0.09983342, 0.29552022])
  678. """
  679. if in_dynamic_or_pir_mode():
  680. return _C_ops.sin(x)
  681. else:
  682. check_variable_and_dtype(
  683. x,
  684. 'x',
  685. [
  686. 'float16',
  687. 'uint16',
  688. 'float32',
  689. 'float64',
  690. 'complex64',
  691. 'complex128',
  692. ],
  693. 'sin',
  694. )
  695. helper = LayerHelper('sin', **locals())
  696. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  697. helper.append_op(type='sin', inputs={"X": x}, outputs={"Out": out})
  698. return out
  699. def sinh(x, name=None):
  700. """
  701. Sinh Activation Operator.
  702. .. math::
  703. out = sinh(x)
  704. Args:
  705. x (Tensor): Input of Sinh operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128.
  706. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  707. Returns:
  708. Tensor. Output of Sinh operator, a Tensor with shape same as input.
  709. Examples:
  710. .. code-block:: python
  711. >>> import paddle
  712. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  713. >>> out = paddle.sinh(x)
  714. >>> print(out)
  715. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  716. [-0.41075233, -0.20133601, 0.10016675, 0.30452031])
  717. """
  718. if in_dynamic_or_pir_mode():
  719. return _C_ops.sinh(x)
  720. else:
  721. check_variable_and_dtype(
  722. x,
  723. 'x',
  724. [
  725. 'float16',
  726. 'uint16',
  727. 'float32',
  728. 'float64',
  729. 'complex64',
  730. 'complex128',
  731. ],
  732. 'sinh',
  733. )
  734. helper = LayerHelper('sinh', **locals())
  735. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  736. helper.append_op(type='sinh', inputs={"X": x}, outputs={"Out": out})
  737. return out
  738. def sqrt(x, name=None):
  739. """
  740. Sqrt Activation Operator.
  741. .. math::
  742. out=\\sqrt{x}=x^{1/2}
  743. Args:
  744. x (Tensor): Input of Sqrt operator, an N-D Tensor, with data type float32, float64 or float16.
  745. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  746. Returns:
  747. Tensor. Output of Sqrt operator, a Tensor with shape same as input.
  748. Examples:
  749. .. code-block:: python
  750. >>> import paddle
  751. >>> x = paddle.to_tensor([0.1, 0.2, 0.3, 0.4])
  752. >>> out = paddle.sqrt(x)
  753. >>> print(out)
  754. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  755. [0.31622776, 0.44721359, 0.54772258, 0.63245553])
  756. """
  757. if in_dynamic_or_pir_mode():
  758. return _C_ops.sqrt(x)
  759. else:
  760. check_variable_and_dtype(
  761. x,
  762. 'x',
  763. ['float16', 'uint16', 'float32', 'float64'],
  764. 'sqrt',
  765. )
  766. helper = LayerHelper('sqrt', **locals())
  767. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  768. helper.append_op(type='sqrt', inputs={"X": x}, outputs={"Out": out})
  769. return out
  770. def square(x, name=None):
  771. """
  772. Square each elements of the inputs.
  773. .. math::
  774. out = x^2
  775. Args:
  776. x (Tensor): Input of Square operator, an N-D Tensor, with data type float32, float64, float16, complex64 or complex128.
  777. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  778. Returns:
  779. Tensor. Output of Square operator, a Tensor with shape same as input.
  780. Examples:
  781. .. code-block:: python
  782. >>> import paddle
  783. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  784. >>> out = paddle.square(x)
  785. >>> print(out)
  786. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  787. [0.16000001, 0.04000000, 0.01000000, 0.09000000])
  788. """
  789. if in_dynamic_or_pir_mode():
  790. return _C_ops.square(x)
  791. else:
  792. check_variable_and_dtype(
  793. x,
  794. 'x',
  795. [
  796. 'int32',
  797. 'int64',
  798. 'float16',
  799. 'float32',
  800. 'float64',
  801. 'complex64',
  802. 'complex128',
  803. ],
  804. 'square',
  805. )
  806. helper = LayerHelper('square', **locals())
  807. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  808. helper.append_op(type='square', inputs={"X": x}, outputs={"Out": out})
  809. return out
  810. def tan(x, name=None):
  811. """
  812. Tangent Operator. Computes tangent of x element-wise.
  813. Input range is `(k*pi-pi/2, k*pi+pi/2)` and output range is `(-inf, inf)`.
  814. .. math::
  815. out = tan(x)
  816. Args:
  817. x (Tensor): Input of Tan operator, an N-D Tensor, with data type float32, float64 or float16.
  818. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  819. Returns:
  820. Tensor. Output of Tan operator, a Tensor with shape same as input.
  821. Examples:
  822. .. code-block:: python
  823. >>> import paddle
  824. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  825. >>> out = paddle.tan(x)
  826. >>> print(out)
  827. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  828. [-0.42279324, -0.20271003, 0.10033467, 0.30933627])
  829. """
  830. if in_dynamic_or_pir_mode():
  831. return _C_ops.tan(x)
  832. else:
  833. check_variable_and_dtype(
  834. x,
  835. 'x',
  836. [
  837. 'float16',
  838. 'uint16',
  839. 'float32',
  840. 'float64',
  841. 'complex64',
  842. 'complex128',
  843. ],
  844. 'tan',
  845. )
  846. helper = LayerHelper('tan', **locals())
  847. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  848. helper.append_op(type='tan', inputs={"X": x}, outputs={"Out": out})
  849. return out
  850. def erf(x, name=None):
  851. r"""
  852. The error function.
  853. For more details, see `Error function <https://en.wikipedia.org/wiki/Error_function>`_.
  854. Equation:
  855. .. math::
  856. out = \frac{2}{\sqrt{\pi}} \int_{0}^{x}e^{- \eta^{2}}d\eta
  857. Args:
  858. x (Tensor): The input tensor, it's data type should be float32, float64.
  859. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  860. Returns:
  861. Tensor: The output of Erf, dtype: float32 or float64, the same as the input, shape: the same as the input.
  862. Examples:
  863. .. code-block:: python
  864. >>> import paddle
  865. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  866. >>> out = paddle.erf(x)
  867. >>> print(out)
  868. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  869. [-0.42839241, -0.22270259, 0.11246292, 0.32862678])
  870. """
  871. if in_dynamic_or_pir_mode():
  872. return _C_ops.erf(x)
  873. locals_var = locals().copy()
  874. kwargs = {}
  875. for name, val in locals_var.items():
  876. if val is not None:
  877. kwargs[name] = val
  878. return generate_layer_fn('erf')(**kwargs)