logic.py 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. # Copyright (c) 2020 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. # TODO: define logic functions of a tensor
  15. import paddle
  16. from ..base.data_feeder import check_type, check_variable_and_dtype
  17. from ..common_ops_import import Variable
  18. Tensor = paddle.base.framework.core.eager.Tensor
  19. from paddle import _C_ops
  20. from paddle.tensor.creation import full
  21. from paddle.tensor.math import broadcast_shape
  22. from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only
  23. from ..framework import (
  24. LayerHelper,
  25. in_dynamic_mode,
  26. in_dynamic_or_pir_mode,
  27. in_pir_mode,
  28. )
  29. __all__ = []
  30. def _logical_op(op_name, x, y, out=None, name=None, binary_op=True):
  31. if in_dynamic_mode():
  32. op = getattr(_C_ops, op_name)
  33. if binary_op:
  34. return op(x, y)
  35. else:
  36. return op(x)
  37. else:
  38. check_variable_and_dtype(
  39. x,
  40. "x",
  41. [
  42. "bool",
  43. "int8",
  44. "int16",
  45. "int32",
  46. "int64",
  47. "float16",
  48. "float32",
  49. "float64",
  50. "uint16",
  51. "complex64",
  52. "complex128",
  53. ],
  54. op_name,
  55. )
  56. if y is not None:
  57. check_variable_and_dtype(
  58. y,
  59. "y",
  60. [
  61. "bool",
  62. "int8",
  63. "int16",
  64. "int32",
  65. "int64",
  66. "float16",
  67. "float32",
  68. "float64",
  69. "uint16",
  70. "complex64",
  71. "complex128",
  72. ],
  73. op_name,
  74. )
  75. if out is not None:
  76. check_type(out, "out", Variable, op_name)
  77. helper = LayerHelper(op_name, **locals())
  78. if out is None:
  79. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  80. if binary_op:
  81. helper.append_op(
  82. type=op_name, inputs={"X": x, "Y": y}, outputs={"Out": out}
  83. )
  84. else:
  85. helper.append_op(
  86. type=op_name, inputs={"X": x}, outputs={"Out": out}
  87. )
  88. return out
  89. def logical_and(x, y, out=None, name=None):
  90. r"""
  91. Compute element-wise logical AND on ``x`` and ``y``, and return ``out``. ``out`` is N-dim boolean ``Tensor``.
  92. Each element of ``out`` is calculated by
  93. .. math::
  94. out = x \&\& y
  95. Note:
  96. ``paddle.logical_and`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
  97. .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
  98. Args:
  99. x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128.
  100. y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128.
  101. out(Tensor, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output.
  102. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  103. Returns:
  104. N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``.
  105. Examples:
  106. .. code-block:: python
  107. >>> import paddle
  108. >>> x = paddle.to_tensor([True])
  109. >>> y = paddle.to_tensor([True, False, True, False])
  110. >>> res = paddle.logical_and(x, y)
  111. >>> print(res)
  112. Tensor(shape=[4], dtype=bool, place=Place(cpu), stop_gradient=True,
  113. [True , False, True , False])
  114. """
  115. if in_dynamic_or_pir_mode():
  116. return _C_ops.logical_and(x, y)
  117. return _logical_op(
  118. op_name="logical_and", x=x, y=y, name=name, out=out, binary_op=True
  119. )
  120. @inplace_apis_in_dygraph_only
  121. def logical_and_(x, y, name=None):
  122. r"""
  123. Inplace version of ``logical_and`` API, the output Tensor will be inplaced with input ``x``.
  124. Please refer to :ref:`api_paddle_logical_and`.
  125. """
  126. out_shape = broadcast_shape(x.shape, y.shape)
  127. if out_shape != x.shape:
  128. raise ValueError(
  129. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  130. )
  131. if in_dynamic_mode():
  132. return _C_ops.logical_and_(x, y)
  133. def logical_or(x, y, out=None, name=None):
  134. """
  135. ``logical_or`` operator computes element-wise logical OR on ``x`` and ``y``, and returns ``out``. ``out`` is N-dim boolean ``Tensor``.
  136. Each element of ``out`` is calculated by
  137. .. math::
  138. out = x || y
  139. Note:
  140. ``paddle.logical_or`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
  141. .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
  142. Args:
  143. x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128.
  144. y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128.
  145. out(Tensor): The ``Variable`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output.
  146. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  147. Returns:
  148. N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``.
  149. Examples:
  150. .. code-block:: python
  151. >>> import paddle
  152. >>> x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1])
  153. >>> y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2])
  154. >>> res = paddle.logical_or(x, y)
  155. >>> print(res)
  156. Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True,
  157. [[True , True ],
  158. [True , False]])
  159. """
  160. if in_dynamic_or_pir_mode():
  161. return _C_ops.logical_or(x, y)
  162. return _logical_op(
  163. op_name="logical_or", x=x, y=y, name=name, out=out, binary_op=True
  164. )
  165. @inplace_apis_in_dygraph_only
  166. def logical_or_(x, y, name=None):
  167. r"""
  168. Inplace version of ``logical_or`` API, the output Tensor will be inplaced with input ``x``.
  169. Please refer to :ref:`api_paddle_logical_or`.
  170. """
  171. out_shape = broadcast_shape(x.shape, y.shape)
  172. if out_shape != x.shape:
  173. raise ValueError(
  174. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  175. )
  176. if in_dynamic_mode():
  177. return _C_ops.logical_or_(x, y)
  178. def logical_xor(x, y, out=None, name=None):
  179. r"""
  180. ``logical_xor`` operator computes element-wise logical XOR on ``x`` and ``y``, and returns ``out``. ``out`` is N-dim boolean ``Tensor``.
  181. Each element of ``out`` is calculated by
  182. .. math::
  183. out = (x || y) \&\& !(x \&\& y)
  184. Note:
  185. ``paddle.logical_xor`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
  186. .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
  187. Args:
  188. x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, float16, float32, float64, complex64, complex128.
  189. y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, float16, float32, float64, complex64, complex128.
  190. out(Tensor): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output.
  191. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  192. Returns:
  193. N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``.
  194. Examples:
  195. .. code-block:: python
  196. >>> import paddle
  197. >>> x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1])
  198. >>> y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2])
  199. >>> res = paddle.logical_xor(x, y)
  200. >>> print(res)
  201. Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True,
  202. [[False, True ],
  203. [True , False]])
  204. """
  205. if in_dynamic_or_pir_mode():
  206. return _C_ops.logical_xor(x, y)
  207. return _logical_op(
  208. op_name="logical_xor", x=x, y=y, name=name, out=out, binary_op=True
  209. )
  210. @inplace_apis_in_dygraph_only
  211. def logical_xor_(x, y, name=None):
  212. r"""
  213. Inplace version of ``logical_xor`` API, the output Tensor will be inplaced with input ``x``.
  214. Please refer to :ref:`api_paddle_logical_xor`.
  215. """
  216. out_shape = broadcast_shape(x.shape, y.shape)
  217. if out_shape != x.shape:
  218. raise ValueError(
  219. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  220. )
  221. if in_dynamic_mode():
  222. return _C_ops.logical_xor_(x, y)
  223. def logical_not(x, out=None, name=None):
  224. """
  225. ``logical_not`` operator computes element-wise logical NOT on ``x``, and returns ``out``. ``out`` is N-dim boolean ``Variable``.
  226. Each element of ``out`` is calculated by
  227. .. math::
  228. out = !x
  229. Note:
  230. ``paddle.logical_not`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
  231. .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
  232. Args:
  233. x(Tensor): Operand of logical_not operator. Must be a Tensor of type bool, int8, int16, in32, in64, float16, float32, or float64, complex64, complex128.
  234. out(Tensor): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor` will be created to save the output.
  235. name(str|None): The default value is None. Normally there is no need for users to set this property. For more information, please refer to :ref:`api_guide_Name`.
  236. Returns:
  237. N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``.
  238. Examples:
  239. .. code-block:: python
  240. >>> import paddle
  241. >>> x = paddle.to_tensor([True, False, True, False])
  242. >>> res = paddle.logical_not(x)
  243. >>> print(res)
  244. Tensor(shape=[4], dtype=bool, place=Place(cpu), stop_gradient=True,
  245. [False, True , False, True ])
  246. """
  247. if in_dynamic_or_pir_mode():
  248. return _C_ops.logical_not(x)
  249. return _logical_op(
  250. op_name="logical_not", x=x, y=None, name=name, out=out, binary_op=False
  251. )
  252. @inplace_apis_in_dygraph_only
  253. def logical_not_(x, name=None):
  254. r"""
  255. Inplace version of ``logical_not`` API, the output Tensor will be inplaced with input ``x``.
  256. Please refer to :ref:`api_paddle_logical_not`.
  257. """
  258. if in_dynamic_mode():
  259. return _C_ops.logical_not_(x)
  260. def is_empty(x, name=None):
  261. """
  262. Test whether a Tensor is empty.
  263. Args:
  264. x (Tensor): The Tensor to be tested.
  265. name (str, optional): The default value is ``None`` . Normally users don't have to set this parameter. For more information, please refer to :ref:`api_guide_Name` .
  266. Returns:
  267. Tensor: A bool scalar Tensor. True if 'x' is an empty Tensor.
  268. Examples:
  269. .. code-block:: python
  270. >>> import paddle
  271. >>> input = paddle.rand(shape=[4, 32, 32], dtype='float32')
  272. >>> res = paddle.is_empty(x=input)
  273. >>> print(res)
  274. Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
  275. False)
  276. """
  277. if in_dynamic_mode():
  278. return _C_ops.is_empty(x)
  279. check_variable_and_dtype(
  280. x, 'x', ['float32', 'float64', 'int32', 'int64'], 'is_empty'
  281. )
  282. check_type(name, "name", (str, type(None)), "is_empty")
  283. if in_pir_mode():
  284. return _C_ops.is_empty(x)
  285. else:
  286. helper = LayerHelper("is_empty", **locals())
  287. cond = helper.create_variable_for_type_inference(dtype='bool')
  288. cond.stop_gradient = True
  289. helper.append_op(
  290. type='is_empty', inputs={'X': [x]}, outputs={'Out': [cond]}
  291. )
  292. return cond
  293. def equal_all(x, y, name=None):
  294. """
  295. Returns the truth value of :math:`x == y`. True if two inputs have the same elements, False otherwise.
  296. Note:
  297. The output has no gradient.
  298. Args:
  299. x(Tensor): Tensor, data type is bool, float32, float64, int32, int64.
  300. y(Tensor): Tensor, data type is bool, float32, float64, int32, int64.
  301. name(str, optional): The default value is None. Normally there is no need for
  302. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  303. Returns:
  304. Tensor: output Tensor, data type is bool, value is [False] or [True].
  305. Examples:
  306. .. code-block:: python
  307. >>> import paddle
  308. >>> x = paddle.to_tensor([1, 2, 3])
  309. >>> y = paddle.to_tensor([1, 2, 3])
  310. >>> z = paddle.to_tensor([1, 4, 3])
  311. >>> result1 = paddle.equal_all(x, y)
  312. >>> print(result1)
  313. Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
  314. True)
  315. >>> result2 = paddle.equal_all(x, z)
  316. >>> print(result2)
  317. Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
  318. False)
  319. """
  320. if in_dynamic_or_pir_mode():
  321. return _C_ops.equal_all(x, y)
  322. else:
  323. helper = LayerHelper("equal_all", **locals())
  324. out = helper.create_variable_for_type_inference(dtype='bool')
  325. helper.append_op(
  326. type='equal_all',
  327. inputs={'X': [x], 'Y': [y]},
  328. outputs={'Out': [out]},
  329. )
  330. return out
  331. def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None):
  332. r"""
  333. Check if all :math:`x` and :math:`y` satisfy the condition:
  334. .. math::
  335. \left| x - y \right| \leq atol + rtol \times \left| y \right|
  336. elementwise, for all elements of :math:`x` and :math:`y`. This is analogous to :math:`numpy.allclose`, namely that it returns :math:`True` if
  337. two tensors are elementwise equal within a tolerance.
  338. Args:
  339. x (Tensor): The input tensor, it's data type should be float16, float32, float64.
  340. y (Tensor): The input tensor, it's data type should be float16, float32, float64.
  341. rtol (rtoltype, optional): The relative tolerance. Default: :math:`1e-5` .
  342. atol (atoltype, optional): The absolute tolerance. Default: :math:`1e-8` .
  343. equal_nan (bool, optional): Whether to compare nan as equal. Default: False.
  344. name (str, optional): Name for the operation. For more information, please
  345. refer to :ref:`api_guide_Name`. Default: None.
  346. Returns:
  347. Tensor: The output tensor, it's data type is bool.
  348. Examples:
  349. .. code-block:: python
  350. >>> import paddle
  351. >>> x = paddle.to_tensor([10000., 1e-07])
  352. >>> y = paddle.to_tensor([10000.1, 1e-08])
  353. >>> result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name="ignore_nan")
  354. >>> print(result1)
  355. Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
  356. False)
  357. >>> result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=True, name="equal_nan")
  358. >>> print(result2)
  359. Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
  360. False)
  361. >>> x = paddle.to_tensor([1.0, float('nan')])
  362. >>> y = paddle.to_tensor([1.0, float('nan')])
  363. >>> result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name="ignore_nan")
  364. >>> print(result1)
  365. Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
  366. False)
  367. >>> result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=True, name="equal_nan")
  368. >>> print(result2)
  369. Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
  370. True)
  371. """
  372. if in_dynamic_or_pir_mode():
  373. return _C_ops.allclose(x, y, rtol, atol, equal_nan)
  374. else:
  375. check_variable_and_dtype(
  376. x, "input", ['float16', 'float32', 'float64'], 'allclose'
  377. )
  378. check_variable_and_dtype(
  379. y, "input", ['float16', 'float32', 'float64'], 'allclose'
  380. )
  381. check_type(rtol, 'rtol', float, 'allclose')
  382. check_type(atol, 'atol', float, 'allclose')
  383. check_type(equal_nan, 'equal_nan', bool, 'allclose')
  384. helper = LayerHelper("allclose", **locals())
  385. out = helper.create_variable_for_type_inference(dtype='bool')
  386. inputs = {'Input': x, 'Other': y}
  387. outputs = {'Out': out}
  388. attrs = {'rtol': str(rtol), 'atol': str(atol), 'equal_nan': equal_nan}
  389. helper.append_op(
  390. type='allclose', inputs=inputs, outputs=outputs, attrs=attrs
  391. )
  392. return out
  393. def equal(x, y, name=None):
  394. """
  395. This layer returns the truth value of :math:`x == y` elementwise.
  396. Note:
  397. The output has no gradient.
  398. Args:
  399. x (Tensor): Tensor, data type is bool, float16, float32, float64, uint8, int8, int16, int32, int64.
  400. y (Tensor): Tensor, data type is bool, float16, float32, float64, uint8, int8, int16, int32, int64.
  401. name (str, optional): The default value is None. Normally there is no need for
  402. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  403. Returns:
  404. Tensor: output Tensor, it's shape is the same as the input's Tensor,
  405. and the data type is bool. The result of this op is stop_gradient.
  406. Examples:
  407. .. code-block:: python
  408. >>> import paddle
  409. >>> x = paddle.to_tensor([1, 2, 3])
  410. >>> y = paddle.to_tensor([1, 3, 2])
  411. >>> result1 = paddle.equal(x, y)
  412. >>> print(result1)
  413. Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True,
  414. [True , False, False])
  415. """
  416. if not isinstance(y, (int, bool, float, Variable, paddle.pir.Value)):
  417. raise TypeError(
  418. f"Type of input args must be float, bool, int or Tensor, but received type {type(y)}"
  419. )
  420. if not isinstance(y, (Variable, paddle.pir.Value)):
  421. y = full(shape=[], dtype=x.dtype, fill_value=y)
  422. if in_dynamic_or_pir_mode():
  423. return _C_ops.equal(x, y)
  424. else:
  425. check_variable_and_dtype(
  426. x,
  427. "x",
  428. [
  429. "bool",
  430. "float16",
  431. "float32",
  432. "float64",
  433. "uint8",
  434. "int8",
  435. "int16",
  436. "int32",
  437. "int64",
  438. "uint16",
  439. ],
  440. "equal",
  441. )
  442. check_variable_and_dtype(
  443. y,
  444. "y",
  445. [
  446. "bool",
  447. "float16",
  448. "float32",
  449. "float64",
  450. "uint8",
  451. "int8",
  452. "int16",
  453. "int32",
  454. "int64",
  455. "uint16",
  456. ],
  457. "equal",
  458. )
  459. helper = LayerHelper("equal", **locals())
  460. out = helper.create_variable_for_type_inference(dtype='bool')
  461. out.stop_gradient = True
  462. helper.append_op(
  463. type='equal',
  464. inputs={'X': [x], 'Y': [y]},
  465. outputs={'Out': [out]},
  466. )
  467. return out
  468. @inplace_apis_in_dygraph_only
  469. def equal_(x, y, name=None):
  470. r"""
  471. Inplace version of ``equal`` API, the output Tensor will be inplaced with input ``x``.
  472. Please refer to :ref:`api_paddle_equal`.
  473. """
  474. out_shape = broadcast_shape(x.shape, y.shape)
  475. if out_shape != x.shape:
  476. raise ValueError(
  477. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  478. )
  479. if in_dynamic_or_pir_mode():
  480. return _C_ops.equal_(x, y)
  481. def greater_equal(x, y, name=None):
  482. """
  483. Returns the truth value of :math:`x >= y` elementwise, which is equivalent function to the overloaded operator `>=`.
  484. Note:
  485. The output has no gradient.
  486. Args:
  487. x (Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, uint8, int8, int16, int32, int64.
  488. y (Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, uint8, int8, int16, int32, int64.
  489. name (str, optional): The default value is None. Normally there is no need for
  490. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  491. Returns:
  492. Tensor: The output shape is same as input :attr:`x`. The output data type is bool.
  493. Examples:
  494. .. code-block:: python
  495. >>> import paddle
  496. >>> x = paddle.to_tensor([1, 2, 3])
  497. >>> y = paddle.to_tensor([1, 3, 2])
  498. >>> result1 = paddle.greater_equal(x, y)
  499. >>> print(result1)
  500. Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True,
  501. [True , False, True ])
  502. """
  503. if in_dynamic_or_pir_mode():
  504. return _C_ops.greater_equal(x, y)
  505. else:
  506. check_variable_and_dtype(
  507. x,
  508. "x",
  509. [
  510. "bool",
  511. "float16",
  512. "float32",
  513. "float64",
  514. "uint8",
  515. "int8",
  516. "int16",
  517. "int32",
  518. "int64",
  519. "uint16",
  520. ],
  521. "greater_equal",
  522. )
  523. check_variable_and_dtype(
  524. y,
  525. "y",
  526. [
  527. "bool",
  528. "float16",
  529. "float32",
  530. "float64",
  531. "uint8",
  532. "int8",
  533. "int16",
  534. "int32",
  535. "int64",
  536. "uint16",
  537. ],
  538. "greater_equal",
  539. )
  540. helper = LayerHelper("greater_equal", **locals())
  541. out = helper.create_variable_for_type_inference(dtype='bool')
  542. out.stop_gradient = True
  543. helper.append_op(
  544. type='greater_equal',
  545. inputs={'X': [x], 'Y': [y]},
  546. outputs={'Out': [out]},
  547. )
  548. return out
  549. @inplace_apis_in_dygraph_only
  550. def greater_equal_(x, y, name=None):
  551. r"""
  552. Inplace version of ``greater_equal`` API, the output Tensor will be inplaced with input ``x``.
  553. Please refer to :ref:`api_paddle_greater_equal`.
  554. """
  555. out_shape = broadcast_shape(x.shape, y.shape)
  556. if out_shape != x.shape:
  557. raise ValueError(
  558. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  559. )
  560. if in_dynamic_mode():
  561. return _C_ops.greater_equal_(x, y)
  562. def greater_than(x, y, name=None):
  563. """
  564. Returns the truth value of :math:`x > y` elementwise, which is equivalent function to the overloaded operator `>`.
  565. Note:
  566. The output has no gradient.
  567. Args:
  568. x (Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, uint8, int8, int16, int32, int64.
  569. y (Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, uint8, int8, int16, int32, int64.
  570. name (str, optional): The default value is None. Normally there is no need for
  571. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  572. Returns:
  573. Tensor: The output shape is same as input :attr:`x`. The output data type is bool.
  574. Examples:
  575. .. code-block:: python
  576. >>> import paddle
  577. >>> x = paddle.to_tensor([1, 2, 3])
  578. >>> y = paddle.to_tensor([1, 3, 2])
  579. >>> result1 = paddle.greater_than(x, y)
  580. >>> print(result1)
  581. Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True,
  582. [False, False, True ])
  583. """
  584. if in_dynamic_or_pir_mode():
  585. return _C_ops.greater_than(x, y)
  586. else:
  587. check_variable_and_dtype(
  588. x,
  589. "x",
  590. [
  591. "bool",
  592. "float16",
  593. "float32",
  594. "float64",
  595. "uint8",
  596. "int8",
  597. "int16",
  598. "int32",
  599. "int64",
  600. "uint16",
  601. ],
  602. "greater_than",
  603. )
  604. check_variable_and_dtype(
  605. y,
  606. "y",
  607. [
  608. "bool",
  609. "float16",
  610. "float32",
  611. "float64",
  612. "uint8",
  613. "int8",
  614. "int16",
  615. "int32",
  616. "int64",
  617. "uint16",
  618. ],
  619. "greater_than",
  620. )
  621. helper = LayerHelper("greater_than", **locals())
  622. out = helper.create_variable_for_type_inference(dtype='bool')
  623. out.stop_gradient = True
  624. helper.append_op(
  625. type='greater_than',
  626. inputs={'X': [x], 'Y': [y]},
  627. outputs={'Out': [out]},
  628. )
  629. return out
  630. @inplace_apis_in_dygraph_only
  631. def greater_than_(x, y, name=None):
  632. r"""
  633. Inplace version of ``greater_than`` API, the output Tensor will be inplaced with input ``x``.
  634. Please refer to :ref:`api_paddle_greater_than`.
  635. """
  636. out_shape = broadcast_shape(x.shape, y.shape)
  637. if out_shape != x.shape:
  638. raise ValueError(
  639. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  640. )
  641. if in_dynamic_mode():
  642. return _C_ops.greater_than_(x, y)
  643. def less_equal(x, y, name=None):
  644. """
  645. Returns the truth value of :math:`x <= y` elementwise, which is equivalent function to the overloaded operator `<=`.
  646. Note:
  647. The output has no gradient.
  648. Args:
  649. x (Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, uint8, int8, int16, int32, int64.
  650. y (Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, uint8, int8, int16, int32, int64.
  651. name (str, optional): The default value is None. Normally there is no need for
  652. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  653. Returns:
  654. Tensor: The output shape is same as input :attr:`x`. The output data type is bool.
  655. Examples:
  656. .. code-block:: python
  657. >>> import paddle
  658. >>> x = paddle.to_tensor([1, 2, 3])
  659. >>> y = paddle.to_tensor([1, 3, 2])
  660. >>> result1 = paddle.less_equal(x, y)
  661. >>> print(result1)
  662. Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True,
  663. [True , True , False])
  664. """
  665. if in_dynamic_or_pir_mode():
  666. return _C_ops.less_equal(x, y)
  667. else:
  668. check_variable_and_dtype(
  669. x,
  670. "x",
  671. [
  672. "bool",
  673. "float16",
  674. "float32",
  675. "float64",
  676. "uint8",
  677. "int8",
  678. "int16",
  679. "int32",
  680. "int64",
  681. "uint16",
  682. ],
  683. "less_equal",
  684. )
  685. check_variable_and_dtype(
  686. y,
  687. "y",
  688. [
  689. "bool",
  690. "float16",
  691. "float32",
  692. "float64",
  693. "uint8",
  694. "int8",
  695. "int16",
  696. "int32",
  697. "int64",
  698. "uint16",
  699. ],
  700. "less_equal",
  701. )
  702. helper = LayerHelper("less_equal", **locals())
  703. out = helper.create_variable_for_type_inference(dtype='bool')
  704. out.stop_gradient = True
  705. helper.append_op(
  706. type='less_equal',
  707. inputs={'X': [x], 'Y': [y]},
  708. outputs={'Out': [out]},
  709. )
  710. return out
  711. @inplace_apis_in_dygraph_only
  712. def less_equal_(x, y, name=None):
  713. r"""
  714. Inplace version of ``less_equal`` API, the output Tensor will be inplaced with input ``x``.
  715. Please refer to :ref:`api_paddle_less_equal`.
  716. """
  717. out_shape = broadcast_shape(x.shape, y.shape)
  718. if out_shape != x.shape:
  719. raise ValueError(
  720. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  721. )
  722. if in_dynamic_mode():
  723. return _C_ops.less_equal_(x, y)
  724. def less_than(x, y, name=None):
  725. """
  726. Returns the truth value of :math:`x < y` elementwise, which is equivalent function to the overloaded operator `<`.
  727. Note:
  728. The output has no gradient.
  729. Args:
  730. x (Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, uint8, int8, int16, int32, int64.
  731. y (Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, uint8, int8, int16, int32, int64.
  732. name (str, optional): The default value is None. Normally there is no need for
  733. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  734. Returns:
  735. Tensor: The output shape is same as input :attr:`x`. The output data type is bool.
  736. Examples:
  737. .. code-block:: python
  738. >>> import paddle
  739. >>> x = paddle.to_tensor([1, 2, 3])
  740. >>> y = paddle.to_tensor([1, 3, 2])
  741. >>> result1 = paddle.less_than(x, y)
  742. >>> print(result1)
  743. Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True,
  744. [False, True , False])
  745. """
  746. if in_dynamic_or_pir_mode():
  747. return _C_ops.less_than(x, y)
  748. else:
  749. check_variable_and_dtype(
  750. x,
  751. "x",
  752. [
  753. "bool",
  754. "float16",
  755. "float32",
  756. "float64",
  757. "uint8",
  758. "int8",
  759. "int16",
  760. "int32",
  761. "int64",
  762. "uint16",
  763. ],
  764. "less_than",
  765. )
  766. check_variable_and_dtype(
  767. y,
  768. "y",
  769. [
  770. "bool",
  771. "float16",
  772. "float32",
  773. "float64",
  774. "uint8",
  775. "int8",
  776. "int16",
  777. "int32",
  778. "int64",
  779. "uint16",
  780. ],
  781. "less_than",
  782. )
  783. helper = LayerHelper("less_than", **locals())
  784. out = helper.create_variable_for_type_inference(dtype='bool')
  785. out.stop_gradient = True
  786. helper.append_op(
  787. type='less_than',
  788. inputs={'X': [x], 'Y': [y]},
  789. outputs={'Out': [out]},
  790. )
  791. return out
  792. @inplace_apis_in_dygraph_only
  793. def less_than_(x, y, name=None):
  794. r"""
  795. Inplace version of ``less_than`` API, the output Tensor will be inplaced with input ``x``.
  796. Please refer to :ref:`api_paddle_less_than`.
  797. """
  798. out_shape = broadcast_shape(x.shape, y.shape)
  799. if out_shape != x.shape:
  800. raise ValueError(
  801. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  802. )
  803. if in_dynamic_mode():
  804. return _C_ops.less_than_(x, y)
  805. def not_equal(x, y, name=None):
  806. """
  807. Returns the truth value of :math:`x != y` elementwise, which is equivalent function to the overloaded operator `!=`.
  808. Note:
  809. The output has no gradient.
  810. Args:
  811. x (Tensor): First input to compare which is N-D tensor. The input data type should be bool, float32, float64, uint8, int8, int16, int32, int64.
  812. y (Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float32, float64, uint8, int8, int16, int32, int64.
  813. name (str, optional): The default value is None. Normally there is no need for
  814. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  815. Returns:
  816. Tensor: The output shape is same as input :attr:`x`. The output data type is bool.
  817. Examples:
  818. .. code-block:: python
  819. >>> import paddle
  820. >>> x = paddle.to_tensor([1, 2, 3])
  821. >>> y = paddle.to_tensor([1, 3, 2])
  822. >>> result1 = paddle.not_equal(x, y)
  823. >>> print(result1)
  824. Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True,
  825. [False, True , True ])
  826. """
  827. if in_dynamic_or_pir_mode():
  828. return _C_ops.not_equal(x, y)
  829. else:
  830. check_variable_and_dtype(
  831. x,
  832. "x",
  833. [
  834. "bool",
  835. "float16",
  836. "float32",
  837. "float64",
  838. "uint8",
  839. "int8",
  840. "int16",
  841. "int32",
  842. "int64",
  843. "uint16",
  844. ],
  845. "not_equal",
  846. )
  847. check_variable_and_dtype(
  848. y,
  849. "y",
  850. [
  851. "bool",
  852. "float16",
  853. "float32",
  854. "float64",
  855. "uint8",
  856. "int8",
  857. "int16",
  858. "int32",
  859. "int64",
  860. "uint16",
  861. ],
  862. "not_equal",
  863. )
  864. helper = LayerHelper("not_equal", **locals())
  865. out = helper.create_variable_for_type_inference(dtype='bool')
  866. out.stop_gradient = True
  867. helper.append_op(
  868. type='not_equal',
  869. inputs={'X': [x], 'Y': [y]},
  870. outputs={'Out': [out]},
  871. )
  872. return out
  873. @inplace_apis_in_dygraph_only
  874. def not_equal_(x, y, name=None):
  875. r"""
  876. Inplace version of ``not_equal`` API, the output Tensor will be inplaced with input ``x``.
  877. Please refer to :ref:`api_paddle_not_equal`.
  878. """
  879. out_shape = broadcast_shape(x.shape, y.shape)
  880. if out_shape != x.shape:
  881. raise ValueError(
  882. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  883. )
  884. if in_dynamic_mode():
  885. return _C_ops.not_equal_(x, y)
  886. def is_tensor(x):
  887. """
  888. Tests whether input object is a paddle.Tensor.
  889. Args:
  890. x (object): Object to test.
  891. Returns:
  892. A boolean value. True if ``x`` is a paddle.Tensor, otherwise False.
  893. Examples:
  894. .. code-block:: python
  895. >>> import paddle
  896. >>> input1 = paddle.rand(shape=[2, 3, 5], dtype='float32')
  897. >>> check = paddle.is_tensor(input1)
  898. >>> print(check)
  899. True
  900. >>> input3 = [1, 4]
  901. >>> check = paddle.is_tensor(input3)
  902. >>> print(check)
  903. False
  904. """
  905. if in_dynamic_or_pir_mode():
  906. return isinstance(
  907. x, (Tensor, paddle.base.core.eager.Tensor, paddle.pir.Value)
  908. )
  909. else:
  910. return isinstance(x, Variable)
  911. def _bitwise_op(op_name, x, y, out=None, name=None, binary_op=True):
  912. if in_dynamic_mode():
  913. op = getattr(_C_ops, op_name)
  914. if binary_op:
  915. return op(x, y)
  916. else:
  917. return op(x)
  918. else:
  919. check_variable_and_dtype(
  920. x,
  921. "x",
  922. ["bool", "uint8", "int8", "int16", "int32", "int64"],
  923. op_name,
  924. )
  925. if y is not None:
  926. check_variable_and_dtype(
  927. y,
  928. "y",
  929. ["bool", "uint8", "int8", "int16", "int32", "int64"],
  930. op_name,
  931. )
  932. if out is not None:
  933. check_type(out, "out", Variable, op_name)
  934. helper = LayerHelper(op_name, **locals())
  935. if binary_op:
  936. assert x.dtype == y.dtype
  937. if out is None:
  938. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  939. if binary_op:
  940. helper.append_op(
  941. type=op_name, inputs={"X": x, "Y": y}, outputs={"Out": out}
  942. )
  943. else:
  944. helper.append_op(
  945. type=op_name, inputs={"X": x}, outputs={"Out": out}
  946. )
  947. return out
  948. def bitwise_and(x, y, out=None, name=None):
  949. r"""
  950. Apply ``bitwise_and`` on Tensor ``X`` and ``Y`` .
  951. .. math::
  952. Out = X \& Y
  953. Note:
  954. ``paddle.bitwise_and`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ .
  955. .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
  956. Args:
  957. x (Tensor): Input Tensor of ``bitwise_and`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
  958. y (Tensor): Input Tensor of ``bitwise_and`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
  959. out (Tensor, optional): Result of ``bitwise_and`` . It is a N-D Tensor with the same data type of input Tensor. Default: None.
  960. name (str, optional): The default value is None. Normally there is no need for
  961. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  962. Returns:
  963. Tensor: Result of ``bitwise_and`` . It is a N-D Tensor with the same data type of input Tensor.
  964. Examples:
  965. .. code-block:: python
  966. >>> import paddle
  967. >>> x = paddle.to_tensor([-5, -1, 1])
  968. >>> y = paddle.to_tensor([4, 2, -3])
  969. >>> res = paddle.bitwise_and(x, y)
  970. >>> print(res)
  971. Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
  972. [0, 2, 1])
  973. """
  974. if in_dynamic_or_pir_mode() and out is None:
  975. return _C_ops.bitwise_and(x, y)
  976. return _bitwise_op(
  977. op_name="bitwise_and", x=x, y=y, name=name, out=out, binary_op=True
  978. )
  979. @inplace_apis_in_dygraph_only
  980. def bitwise_and_(x, y, name=None):
  981. r"""
  982. Inplace version of ``bitwise_and`` API, the output Tensor will be inplaced with input ``x``.
  983. Please refer to :ref:`api_paddle_bitwise_and`.
  984. """
  985. out_shape = broadcast_shape(x.shape, y.shape)
  986. if out_shape != x.shape:
  987. raise ValueError(
  988. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  989. )
  990. if in_dynamic_or_pir_mode():
  991. return _C_ops.bitwise_and_(x, y)
  992. def bitwise_or(x, y, out=None, name=None):
  993. r"""
  994. Apply ``bitwise_or`` on Tensor ``X`` and ``Y`` .
  995. .. math::
  996. Out = X | Y
  997. Note:
  998. ``paddle.bitwise_or`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ .
  999. .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
  1000. Args:
  1001. x (Tensor): Input Tensor of ``bitwise_or`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
  1002. y (Tensor): Input Tensor of ``bitwise_or`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
  1003. out (Tensor, optional): Result of ``bitwise_or`` . It is a N-D Tensor with the same data type of input Tensor. Default: None.
  1004. name (str, optional): The default value is None. Normally there is no need for
  1005. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  1006. Returns:
  1007. Tensor: Result of ``bitwise_or`` . It is a N-D Tensor with the same data type of input Tensor.
  1008. Examples:
  1009. .. code-block:: python
  1010. >>> import paddle
  1011. >>> x = paddle.to_tensor([-5, -1, 1])
  1012. >>> y = paddle.to_tensor([4, 2, -3])
  1013. >>> res = paddle.bitwise_or(x, y)
  1014. >>> print(res)
  1015. Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
  1016. [-1, -1, -3])
  1017. """
  1018. if in_dynamic_or_pir_mode() and out is None:
  1019. return _C_ops.bitwise_or(x, y)
  1020. return _bitwise_op(
  1021. op_name="bitwise_or", x=x, y=y, name=name, out=out, binary_op=True
  1022. )
  1023. @inplace_apis_in_dygraph_only
  1024. def bitwise_or_(x, y, name=None):
  1025. r"""
  1026. Inplace version of ``bitwise_or`` API, the output Tensor will be inplaced with input ``x``.
  1027. Please refer to :ref:`api_paddle_bitwise_or`.
  1028. """
  1029. out_shape = broadcast_shape(x.shape, y.shape)
  1030. if out_shape != x.shape:
  1031. raise ValueError(
  1032. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  1033. )
  1034. if in_dynamic_mode():
  1035. return _C_ops.bitwise_or_(x, y)
  1036. def bitwise_xor(x, y, out=None, name=None):
  1037. r"""
  1038. Apply ``bitwise_xor`` on Tensor ``X`` and ``Y`` .
  1039. .. math::
  1040. Out = X ^\wedge Y
  1041. Note:
  1042. ``paddle.bitwise_xor`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ .
  1043. .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
  1044. Args:
  1045. x (Tensor): Input Tensor of ``bitwise_xor`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
  1046. y (Tensor): Input Tensor of ``bitwise_xor`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
  1047. out (Tensor, optional): Result of ``bitwise_xor`` . It is a N-D Tensor with the same data type of input Tensor. Default: None.
  1048. name (str, optional): The default value is None. Normally there is no need for
  1049. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  1050. Returns:
  1051. Tensor: Result of ``bitwise_xor`` . It is a N-D Tensor with the same data type of input Tensor.
  1052. Examples:
  1053. .. code-block:: python
  1054. >>> import paddle
  1055. >>> x = paddle.to_tensor([-5, -1, 1])
  1056. >>> y = paddle.to_tensor([4, 2, -3])
  1057. >>> res = paddle.bitwise_xor(x, y)
  1058. >>> print(res)
  1059. Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
  1060. [-1, -3, -4])
  1061. """
  1062. if in_dynamic_or_pir_mode() and out is None:
  1063. return _C_ops.bitwise_xor(x, y)
  1064. return _bitwise_op(
  1065. op_name="bitwise_xor", x=x, y=y, name=name, out=out, binary_op=True
  1066. )
  1067. @inplace_apis_in_dygraph_only
  1068. def bitwise_xor_(x, y, name=None):
  1069. r"""
  1070. Inplace version of ``bitwise_xor`` API, the output Tensor will be inplaced with input ``x``.
  1071. Please refer to :ref:`api_paddle_bitwise_xor`.
  1072. """
  1073. out_shape = broadcast_shape(x.shape, y.shape)
  1074. if out_shape != x.shape:
  1075. raise ValueError(
  1076. f"The shape of broadcast output {out_shape} is different from that of inplace tensor {x.shape} in the Inplace operation."
  1077. )
  1078. if in_dynamic_mode():
  1079. return _C_ops.bitwise_xor_(x, y)
  1080. def bitwise_not(x, out=None, name=None):
  1081. r"""
  1082. Apply ``bitwise_not`` on Tensor ``X``.
  1083. .. math::
  1084. Out = \sim X
  1085. Note:
  1086. ``paddle.bitwise_not`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ .
  1087. .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
  1088. Args:
  1089. x (Tensor): Input Tensor of ``bitwise_not`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
  1090. out (Tensor, optional): Result of ``bitwise_not`` . It is a N-D Tensor with the same data type of input Tensor. Default: None.
  1091. name (str, optional): The default value is None. Normally there is no need for
  1092. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  1093. Returns:
  1094. Tensor: Result of ``bitwise_not`` . It is a N-D Tensor with the same data type of input Tensor.
  1095. Examples:
  1096. .. code-block:: python
  1097. >>> import paddle
  1098. >>> x = paddle.to_tensor([-5, -1, 1])
  1099. >>> res = paddle.bitwise_not(x)
  1100. >>> print(res)
  1101. Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
  1102. [ 4, 0, -2])
  1103. """
  1104. if in_dynamic_or_pir_mode() and out is None:
  1105. return _C_ops.bitwise_not(x)
  1106. return _bitwise_op(
  1107. op_name="bitwise_not", x=x, y=None, name=name, out=out, binary_op=False
  1108. )
  1109. @inplace_apis_in_dygraph_only
  1110. def bitwise_not_(x, name=None):
  1111. r"""
  1112. Inplace version of ``bitwise_not`` API, the output Tensor will be inplaced with input ``x``.
  1113. Please refer to :ref:`api_paddle_bitwise_not`.
  1114. """
  1115. if in_dynamic_mode():
  1116. return _C_ops.bitwise_not_(x)
  1117. def isclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None):
  1118. r"""
  1119. Check if all :math:`x` and :math:`y` satisfy the condition:
  1120. .. math::
  1121. \left| x - y \right| \leq atol + rtol \times \left| y \right|
  1122. elementwise, for all elements of :math:`x` and :math:`y`. The behaviour of this
  1123. operator is analogous to :math:`numpy.isclose`, namely that it returns :math:`True` if
  1124. two tensors are elementwise equal within a tolerance.
  1125. Args:
  1126. x(Tensor): The input tensor, it's data type should be float16, float32, float64, complex64, complex128.
  1127. y(Tensor): The input tensor, it's data type should be float16, float32, float64, complex64, complex128.
  1128. rtol(rtoltype, optional): The relative tolerance. Default: :math:`1e-5` .
  1129. atol(atoltype, optional): The absolute tolerance. Default: :math:`1e-8` .
  1130. equal_nan(equalnantype, optional): If :math:`True` , then two :math:`NaNs` will be compared as equal. Default: :math:`False` .
  1131. name (str, optional): Name for the operation. For more information, please
  1132. refer to :ref:`api_guide_Name`. Default: None.
  1133. Returns:
  1134. Tensor: The output tensor, it's data type is bool.
  1135. Examples:
  1136. .. code-block:: python
  1137. >>> import paddle
  1138. >>> x = paddle.to_tensor([10000., 1e-07])
  1139. >>> y = paddle.to_tensor([10000.1, 1e-08])
  1140. >>> result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
  1141. ... equal_nan=False, name="ignore_nan")
  1142. >>> print(result1)
  1143. Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
  1144. [True , False])
  1145. >>> result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
  1146. ... equal_nan=True, name="equal_nan")
  1147. >>> print(result2)
  1148. Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
  1149. [True , False])
  1150. >>> x = paddle.to_tensor([1.0, float('nan')])
  1151. >>> y = paddle.to_tensor([1.0, float('nan')])
  1152. >>> result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
  1153. ... equal_nan=False, name="ignore_nan")
  1154. >>> print(result1)
  1155. Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
  1156. [True , False])
  1157. >>> result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
  1158. ... equal_nan=True, name="equal_nan")
  1159. >>> print(result2)
  1160. Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
  1161. [True, True])
  1162. """
  1163. if in_dynamic_or_pir_mode():
  1164. return _C_ops.isclose(x, y, rtol, atol, equal_nan)
  1165. else:
  1166. check_variable_and_dtype(
  1167. x,
  1168. "input",
  1169. ['float16', 'float32', 'float64', 'complex64', 'complex128'],
  1170. 'isclose',
  1171. )
  1172. check_variable_and_dtype(
  1173. y,
  1174. "input",
  1175. ['float16', 'float32', 'float64', 'complex64', 'complex128'],
  1176. 'isclose',
  1177. )
  1178. check_type(rtol, 'rtol', float, 'isclose')
  1179. check_type(atol, 'atol', float, 'isclose')
  1180. check_type(equal_nan, 'equal_nan', bool, 'isclose')
  1181. helper = LayerHelper("isclose", **locals())
  1182. out = helper.create_variable_for_type_inference(dtype='bool')
  1183. inputs = {'Input': x, 'Other': y}
  1184. outputs = {'Out': out}
  1185. attrs = {'rtol': str(rtol), 'atol': str(atol), 'equal_nan': equal_nan}
  1186. helper.append_op(
  1187. type='isclose', inputs=inputs, outputs=outputs, attrs=attrs
  1188. )
  1189. return out