functional.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. # mypy: allow-untyped-defs
  2. r"""Functional interface (quantized)."""
  3. import warnings
  4. from typing import Optional
  5. import torch
  6. from torch import Tensor
  7. from torch.jit.annotations import BroadcastingList2
  8. from torch.nn.modules.utils import _pair, _triple
  9. from .modules.utils import _pair_from_first
  10. # Although some of the functions and docstrings are mirrored from the torch.nn,
  11. # we want to have them here for future changes.
  12. __all__ = [
  13. "avg_pool2d",
  14. "avg_pool3d",
  15. "adaptive_avg_pool2d",
  16. "adaptive_avg_pool3d",
  17. "conv1d",
  18. "conv2d",
  19. "conv3d",
  20. "interpolate",
  21. "linear",
  22. "max_pool1d",
  23. "max_pool2d",
  24. "celu",
  25. "leaky_relu",
  26. "hardtanh",
  27. "hardswish",
  28. "threshold",
  29. "elu",
  30. "hardsigmoid",
  31. "clamp",
  32. "upsample",
  33. "upsample_bilinear",
  34. "upsample_nearest",
  35. ]
  36. def avg_pool2d(
  37. input,
  38. kernel_size,
  39. stride=None,
  40. padding=0,
  41. ceil_mode=False,
  42. count_include_pad=True,
  43. divisor_override=None,
  44. ):
  45. r"""
  46. Applies 2D average-pooling operation in :math:`kH \times kW` regions by step size
  47. :math:`sH \times sW` steps. The number of output features is equal to the number of
  48. input planes.
  49. .. note:: The input quantization parameters propagate to the output.
  50. See :class:`~torch.ao.nn.quantized.AvgPool2d` for details and output shape.
  51. Args:
  52. input: quantized input tensor :math:`(\text{minibatch} , \text{in\_channels} , iH , iW)`
  53. kernel_size: size of the pooling region. Can be a single number or a
  54. tuple `(kH, kW)`
  55. stride: stride of the pooling operation. Can be a single number or a
  56. tuple `(sH, sW)`. Default: :attr:`kernel_size`
  57. padding: implicit zero paddings on both sides of the input. Can be a
  58. single number or a tuple `(padH, padW)`. Default: 0
  59. ceil_mode: when True, will use `ceil` instead of `floor` in the formula
  60. to compute the output shape. Default: ``False``
  61. count_include_pad: when True, will include the zero-padding in the
  62. averaging calculation. Default: ``True``
  63. divisor_override: if specified, it will be used as divisor, otherwise
  64. size of the pooling region will be used. Default: None
  65. """
  66. if not input.is_quantized:
  67. raise ValueError("Input to 'quantized.avg_pool2d' must be quantized!")
  68. return torch.nn.functional.avg_pool2d(
  69. input,
  70. kernel_size,
  71. stride,
  72. padding,
  73. ceil_mode,
  74. count_include_pad,
  75. divisor_override,
  76. )
  77. def avg_pool3d(
  78. input,
  79. kernel_size,
  80. stride=None,
  81. padding=0,
  82. ceil_mode=False,
  83. count_include_pad=True,
  84. divisor_override=None,
  85. ):
  86. r"""
  87. Applies 3D average-pooling operation in :math:`kD \ times kH \times kW` regions by step size
  88. :math:`sD \times sH \times sW` steps. The number of output features is equal to the number of
  89. input planes.
  90. .. note:: The input quantization parameters propagate to the output.
  91. Args:
  92. input: quantized input tensor :math:`(\text{minibatch} , \text{in\_channels} , iH , iW)`
  93. kernel_size: size of the pooling region. Can be a single number or a
  94. tuple `(kD, kH, kW)`
  95. stride: stride of the pooling operation. Can be a single number or a
  96. tuple `(sD, sH, sW)`. Default: :attr:`kernel_size`
  97. padding: implicit zero paddings on both sides of the input. Can be a
  98. single number or a tuple `(padD, padH, padW)`. Default: 0
  99. ceil_mode: when True, will use `ceil` instead of `floor` in the formula
  100. to compute the output shape. Default: ``False``
  101. count_include_pad: when True, will include the zero-padding in the
  102. averaging calculation. Default: ``True``
  103. divisor_override: if specified, it will be used as divisor, otherwise
  104. size of the pooling region will be used. Default: None
  105. """
  106. if not input.is_quantized:
  107. raise ValueError("Input to 'quantized.avg_pool3d' must be quantized!")
  108. return torch.nn.functional.avg_pool3d(
  109. input,
  110. kernel_size,
  111. stride,
  112. padding,
  113. ceil_mode,
  114. count_include_pad,
  115. divisor_override,
  116. )
  117. def adaptive_avg_pool2d(input: Tensor, output_size: BroadcastingList2[int]) -> Tensor:
  118. r"""
  119. Applies a 2D adaptive average pooling over a quantized input signal composed
  120. of several quantized input planes.
  121. .. note:: The input quantization parameters propagate to the output.
  122. See :class:`~torch.ao.nn.quantized.AdaptiveAvgPool2d` for details and output shape.
  123. Args:
  124. output_size: the target output size (single integer or
  125. double-integer tuple)
  126. """
  127. if not input.is_quantized:
  128. raise ValueError(
  129. "Input to 'quantized.functional.adaptive_avg_pool2d' must be quantized!"
  130. )
  131. return torch.nn.functional.adaptive_avg_pool2d(input, output_size)
  132. def adaptive_avg_pool3d(input: Tensor, output_size: BroadcastingList2[int]) -> Tensor:
  133. r"""
  134. Applies a 3D adaptive average pooling over a quantized input signal composed
  135. of several quantized input planes.
  136. .. note:: The input quantization parameters propagate to the output.
  137. See :class:`~torch.ao.nn.quantized.AdaptiveAvgPool3d` for details and output shape.
  138. Args:
  139. output_size: the target output size (single integer or
  140. double-integer tuple)
  141. """
  142. if not input.is_quantized:
  143. raise ValueError(
  144. "Input to 'quantized.functional.adaptive_avg_pool3d' must be quantized!"
  145. )
  146. return torch.nn.functional.adaptive_avg_pool3d(input, output_size)
  147. def conv1d(
  148. input,
  149. weight,
  150. bias,
  151. stride=1,
  152. padding=0,
  153. dilation=1,
  154. groups=1,
  155. padding_mode="zeros",
  156. scale=1.0,
  157. zero_point=0,
  158. dtype=torch.quint8,
  159. ):
  160. r"""
  161. Applies a 1D convolution over a quantized 1D input composed of several input
  162. planes.
  163. See :class:`~torch.ao.nn.quantized.Conv1d` for details and output shape.
  164. Args:
  165. input: quantized input tensor of shape :math:`(\text{minibatch} , \text{in\_channels} , iW)`
  166. weight: quantized filters of shape :math:`(\text{out\_channels} , \frac{\text{in\_channels}}{\text{groups}} , iW)`
  167. bias: **non-quantized** bias tensor of shape :math:`(\text{out\_channels})`. The tensor type must be `torch.float`.
  168. stride: the stride of the convolving kernel. Can be a single number or a
  169. tuple `(sW,)`. Default: 1
  170. padding: implicit paddings on both sides of the input. Can be a
  171. single number or a tuple `(padW,)`. Default: 0
  172. dilation: the spacing between kernel elements. Can be a single number or
  173. a tuple `(dW,)`. Default: 1
  174. groups: split input into groups, :math:`\text{in\_channels}` should be divisible by the
  175. number of groups. Default: 1
  176. padding_mode: the padding mode to use. Only "zeros" is supported for quantized convolution at the moment. Default: "zeros"
  177. scale: quantization scale for the output. Default: 1.0
  178. zero_point: quantization zero_point for the output. Default: 0
  179. dtype: quantization data type to use. Default: ``torch.quint8``
  180. Examples::
  181. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_QENGINE)
  182. >>> from torch.ao.nn.quantized import functional as qF
  183. >>> filters = torch.randn(33, 16, 3, dtype=torch.float)
  184. >>> inputs = torch.randn(20, 16, 50, dtype=torch.float)
  185. >>> bias = torch.randn(33, dtype=torch.float)
  186. >>>
  187. >>> scale, zero_point = 1.0, 0
  188. >>> dtype_inputs = torch.quint8
  189. >>> dtype_filters = torch.qint8
  190. >>>
  191. >>> q_filters = torch.quantize_per_tensor(filters, scale, zero_point, dtype_filters)
  192. >>> q_inputs = torch.quantize_per_tensor(inputs, scale, zero_point, dtype_inputs)
  193. >>> qF.conv1d(q_inputs, q_filters, bias, padding=1, scale=scale, zero_point=zero_point)
  194. """ # noqa: E501
  195. if padding_mode != "zeros":
  196. raise NotImplementedError("Only zero-padding is supported!")
  197. if input.dtype != torch.quint8:
  198. raise NotImplementedError(
  199. "Only torch.quint8 is supported for activation tensor!"
  200. )
  201. if weight.dtype != torch.qint8:
  202. raise NotImplementedError("Only torch.qint8 is supported for weight tensor!")
  203. if input.ndim != 3:
  204. raise ValueError("Input shape must be `(N, C, L)`!")
  205. stride = _pair_from_first(stride)
  206. padding = _pair_from_first(padding)
  207. dilation = _pair_from_first(dilation)
  208. packed_params = torch.ops.quantized.conv1d_prepack(
  209. weight, bias, stride, padding, dilation, groups
  210. )
  211. return torch.ops.quantized.conv1d(input, packed_params, scale, zero_point)
  212. def conv2d(
  213. input,
  214. weight,
  215. bias,
  216. stride=1,
  217. padding=0,
  218. dilation=1,
  219. groups=1,
  220. padding_mode="zeros",
  221. scale=1.0,
  222. zero_point=0,
  223. dtype=torch.quint8,
  224. ):
  225. r"""
  226. Applies a 2D convolution over a quantized 2D input composed of several input
  227. planes.
  228. See :class:`~torch.ao.nn.quantized.Conv2d` for details and output shape.
  229. Args:
  230. input: quantized input tensor of shape :math:`(\text{minibatch} , \text{in\_channels} , iH , iW)`
  231. weight: quantized filters of shape :math:`(\text{out\_channels} , \frac{\text{in\_channels}}{\text{groups}} , kH , kW)`
  232. bias: **non-quantized** bias tensor of shape :math:`(\text{out\_channels})`. The tensor type must be `torch.float`.
  233. stride: the stride of the convolving kernel. Can be a single number or a
  234. tuple `(sH, sW)`. Default: 1
  235. padding: implicit paddings on both sides of the input. Can be a
  236. single number or a tuple `(padH, padW)`. Default: 0
  237. dilation: the spacing between kernel elements. Can be a single number or
  238. a tuple `(dH, dW)`. Default: 1
  239. groups: split input into groups, :math:`\text{in\_channels}` should be divisible by the
  240. number of groups. Default: 1
  241. padding_mode: the padding mode to use. Only "zeros" is supported for quantized convolution at the moment. Default: "zeros"
  242. scale: quantization scale for the output. Default: 1.0
  243. zero_point: quantization zero_point for the output. Default: 0
  244. dtype: quantization data type to use. Default: ``torch.quint8``
  245. Examples::
  246. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_QENGINE)
  247. >>> from torch.ao.nn.quantized import functional as qF
  248. >>> filters = torch.randn(8, 4, 3, 3, dtype=torch.float)
  249. >>> inputs = torch.randn(1, 4, 5, 5, dtype=torch.float)
  250. >>> bias = torch.randn(8, dtype=torch.float)
  251. >>>
  252. >>> scale, zero_point = 1.0, 0
  253. >>> dtype_inputs = torch.quint8
  254. >>> dtype_filters = torch.qint8
  255. >>>
  256. >>> q_filters = torch.quantize_per_tensor(filters, scale, zero_point, dtype_filters)
  257. >>> q_inputs = torch.quantize_per_tensor(inputs, scale, zero_point, dtype_inputs)
  258. >>> qF.conv2d(q_inputs, q_filters, bias, padding=1, scale=scale, zero_point=zero_point)
  259. """ # noqa: E501
  260. if padding_mode != "zeros":
  261. raise NotImplementedError("Only zero-padding is supported!")
  262. if input.dtype != torch.quint8:
  263. raise NotImplementedError(
  264. "Only torch.quint8 is supported for activation tensor!"
  265. )
  266. if weight.dtype != torch.qint8:
  267. raise NotImplementedError("Only torch.qint8 is supported for weight tensor!")
  268. if input.ndim != 4:
  269. raise ValueError("Input shape must be `(N, C, H, W)`!")
  270. stride = _pair(stride)
  271. padding = _pair(padding)
  272. dilation = _pair(dilation)
  273. packed_params = torch.ops.quantized.conv2d_prepack(
  274. weight, bias, stride, padding, dilation, groups
  275. )
  276. return torch.ops.quantized.conv2d(input, packed_params, scale, zero_point)
  277. def conv3d(
  278. input,
  279. weight,
  280. bias,
  281. stride=1,
  282. padding=0,
  283. dilation=1,
  284. groups=1,
  285. padding_mode="zeros",
  286. scale=1.0,
  287. zero_point=0,
  288. dtype=torch.quint8,
  289. ):
  290. r"""
  291. Applies a 3D convolution over a quantized 3D input composed of several input
  292. planes.
  293. See :class:`~torch.ao.nn.quantized.Conv3d` for details and output shape.
  294. Args:
  295. input: quantized input tensor of shape
  296. :math:`(\text{minibatch} , \text{in\_channels} , iD , iH , iW)`
  297. weight: quantized filters of shape
  298. :math:`(\text{out\_channels} , \frac{\text{in\_channels}}{\text{groups}} , kD , kH , kW)`
  299. bias: **non-quantized** bias tensor of shape
  300. :math:`(\text{out\_channels})`. The tensor type must be `torch.float`.
  301. stride: the stride of the convolving kernel. Can be a single number or a
  302. tuple `(sD, sH, sW)`. Default: 1
  303. padding: implicit paddings on both sides of the input. Can be a
  304. single number or a tuple `(padD, padH, padW)`. Default: 0
  305. dilation: the spacing between kernel elements. Can be a single number or
  306. a tuple `(dD, dH, dW)`. Default: 1
  307. groups: split input into groups, :math:`\text{in\_channels}` should be
  308. divisible by the number of groups. Default: 1
  309. padding_mode: the padding mode to use. Only "zeros" is supported for
  310. quantized convolution at the moment. Default: "zeros"
  311. scale: quantization scale for the output. Default: 1.0
  312. zero_point: quantization zero_point for the output. Default: 0
  313. dtype: quantization data type to use. Default: ``torch.quint8``
  314. Examples::
  315. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_QENGINE)
  316. >>> from torch.ao.nn.quantized import functional as qF
  317. >>> filters = torch.randn(8, 4, 3, 3, 3, dtype=torch.float)
  318. >>> inputs = torch.randn(1, 4, 5, 5, 5, dtype=torch.float)
  319. >>> bias = torch.randn(8, dtype=torch.float)
  320. >>>
  321. >>> scale, zero_point = 1.0, 0
  322. >>> dtype_inputs = torch.quint8
  323. >>> dtype_filters = torch.qint8
  324. >>>
  325. >>> q_filters = torch.quantize_per_tensor(filters, scale, zero_point, dtype_filters)
  326. >>> q_inputs = torch.quantize_per_tensor(inputs, scale, zero_point, dtype_inputs)
  327. >>> qF.conv3d(q_inputs, q_filters, bias, padding=1, scale=scale, zero_point=zero_point)
  328. """ # noqa: E501
  329. if padding_mode != "zeros":
  330. raise NotImplementedError("Only zero-padding is supported!")
  331. if input.dtype != torch.quint8:
  332. raise NotImplementedError(
  333. "Only torch.quint8 is supported for activation tensor!"
  334. )
  335. if weight.dtype != torch.qint8:
  336. raise NotImplementedError("Only torch.qint8 is supported for weight tensor!")
  337. if input.ndim != 5:
  338. raise ValueError("Input shape must be `(N, C, D, H, W)`!")
  339. stride = _triple(stride)
  340. padding = _triple(padding)
  341. dilation = _triple(dilation)
  342. packed_params = torch.ops.quantized.conv3d_prepack(
  343. weight, bias, stride, padding, dilation, groups
  344. )
  345. return torch.ops.quantized.conv3d(input, packed_params, scale, zero_point)
  346. def interpolate(
  347. input, size=None, scale_factor=None, mode="nearest", align_corners=None
  348. ):
  349. r"""Down/up samples the input to either the given :attr:`size` or the given
  350. :attr:`scale_factor`
  351. See :func:`torch.nn.functional.interpolate` for implementation details.
  352. The input dimensions are interpreted in the form:
  353. `mini-batch x channels x [optional depth] x [optional height] x width`.
  354. .. note:: The input quantization parameters propagate to the output.
  355. .. note:: Only 2D/3D input is supported for quantized inputs
  356. .. note:: Only the following modes are supported for the quantized inputs:
  357. - `bilinear`
  358. - `nearest`
  359. Args:
  360. input (Tensor): the input tensor
  361. size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]):
  362. output spatial size.
  363. scale_factor (float or Tuple[float]): multiplier for spatial size. Has to match input size if it is a tuple.
  364. mode (str): algorithm used for upsampling:
  365. ``'nearest'`` | ``'bilinear'``
  366. align_corners (bool, optional): Geometrically, we consider the pixels of the
  367. input and output as squares rather than points.
  368. If set to ``True``, the input and output tensors are aligned by the
  369. center points of their corner pixels, preserving the values at the corner pixels.
  370. If set to ``False``, the input and output tensors are aligned by the corner
  371. points of their corner pixels, and the interpolation uses edge value padding
  372. for out-of-boundary values, making this operation *independent* of input size
  373. when :attr:`scale_factor` is kept the same. This only has an effect when :attr:`mode`
  374. is ``'bilinear'``.
  375. Default: ``False``
  376. """
  377. if not input.is_quantized:
  378. raise ValueError("Input to 'quantized.interpolate' must be quantized!")
  379. return torch.nn.functional.interpolate(
  380. input, size, scale_factor, mode, align_corners
  381. )
  382. def linear(
  383. input: Tensor,
  384. weight: Tensor,
  385. bias: Optional[Tensor] = None,
  386. scale: Optional[float] = None,
  387. zero_point: Optional[int] = None,
  388. ) -> Tensor:
  389. r"""
  390. Applies a linear transformation to the incoming quantized data:
  391. :math:`y = xA^T + b`.
  392. See :class:`~torch.ao.nn.quantized.Linear`
  393. .. note::
  394. Current implementation packs weights on every call, which has penalty on performance.
  395. If you want to avoid the overhead, use :class:`~torch.ao.nn.quantized.Linear`.
  396. Args:
  397. input (Tensor): Quantized input of type `torch.quint8`
  398. weight (Tensor): Quantized weight of type `torch.qint8`
  399. bias (Tensor): None or fp32 bias of type `torch.float`
  400. scale (double): output scale. If None, derived from the input scale
  401. zero_point (long): output zero point. If None, derived from the input zero_point
  402. Shape:
  403. - Input: :math:`(N, *, in\_features)` where `*` means any number of
  404. additional dimensions
  405. - Weight: :math:`(out\_features, in\_features)`
  406. - Bias: :math:`(out\_features)`
  407. - Output: :math:`(N, *, out\_features)`
  408. """
  409. if scale is None:
  410. scale = input.q_scale()
  411. if zero_point is None:
  412. zero_point = input.q_zero_point()
  413. _packed_params = torch.ops.quantized.linear_prepack(weight, bias)
  414. return torch.ops.quantized.linear(input, _packed_params, scale, zero_point)
  415. def max_pool1d(
  416. input,
  417. kernel_size,
  418. stride=None,
  419. padding=0,
  420. dilation=1,
  421. ceil_mode=False,
  422. return_indices=False,
  423. ):
  424. r"""Applies a 1D max pooling over a quantized input signal composed of
  425. several quantized input planes.
  426. .. note:: The input quantization parameters are propagated to the output.
  427. See :class:`~torch.ao.nn.quantized.MaxPool1d` for details.
  428. """
  429. if return_indices:
  430. raise NotImplementedError("return_indices is not yet implemented!")
  431. if stride is None:
  432. stride = torch.jit.annotate(list[int], [])
  433. return torch.nn.functional.max_pool1d(
  434. input,
  435. kernel_size,
  436. stride,
  437. padding,
  438. dilation,
  439. ceil_mode=ceil_mode,
  440. return_indices=return_indices,
  441. )
  442. def max_pool2d(
  443. input,
  444. kernel_size,
  445. stride=None,
  446. padding=0,
  447. dilation=1,
  448. ceil_mode=False,
  449. return_indices=False,
  450. ):
  451. r"""Applies a 2D max pooling over a quantized input signal composed of
  452. several quantized input planes.
  453. .. note:: The input quantization parameters are propagated to the output.
  454. See :class:`~torch.ao.nn.quantized.MaxPool2d` for details.
  455. """
  456. if return_indices:
  457. raise NotImplementedError("return_indices is not yet implemented!")
  458. if stride is None:
  459. stride = torch.jit.annotate(list[int], [])
  460. return torch.nn.functional.max_pool2d(
  461. input,
  462. kernel_size,
  463. stride,
  464. padding,
  465. dilation,
  466. ceil_mode=ceil_mode,
  467. return_indices=return_indices,
  468. )
  469. def celu(input: Tensor, scale: float, zero_point: int, alpha: float = 1.0) -> Tensor:
  470. r"""celu(input, scale, zero_point, alpha=1.) -> Tensor
  471. Applies the quantized CELU function element-wise.
  472. .. math::
  473. \text{CELU}(x) = \max(0,x) + \min(0, \alpha * (\exp(x / \alpha) - 1))
  474. Args:
  475. input: quantized input
  476. alpha: the :math:`\alpha` value for the CELU formulation. Default: 1.0
  477. """
  478. if not input.is_quantized:
  479. raise ValueError("Input to 'quantized.celu' must be quantized!")
  480. return torch.ops.quantized.celu(input, scale, zero_point, alpha)
  481. def leaky_relu(
  482. input: Tensor,
  483. negative_slope: float = 0.01,
  484. inplace: bool = False,
  485. scale: Optional[float] = None,
  486. zero_point: Optional[int] = None,
  487. ):
  488. r"""
  489. Quantized version of the.
  490. leaky_relu(input, negative_slope=0.01, inplace=False, scale, zero_point) -> Tensor
  491. Applies element-wise,
  492. :math:`\text{LeakyReLU}(x) = \max(0, x) + \text{negative\_slope} * \min(0, x)`
  493. Args:
  494. input: Quantized input
  495. negative_slope: The slope of the negative input
  496. inplace: Inplace modification of the input tensor
  497. scale, zero_point: Scale and zero point of the output tensor.
  498. See :class:`~torch.nn.LeakyReLU` for more details.
  499. """
  500. if scale is not None and zero_point is not None:
  501. assert not inplace, "Cannot rescale with `inplace`"
  502. output = torch._empty_affine_quantized(
  503. input.shape, scale=scale, zero_point=int(zero_point), dtype=input.dtype
  504. )
  505. torch._C._nn.leaky_relu(input, negative_slope, out=output)
  506. return output
  507. if inplace:
  508. result = torch._C._nn.leaky_relu_(input, negative_slope)
  509. else:
  510. result = torch._C._nn.leaky_relu(input, negative_slope)
  511. return result
  512. def hardtanh(
  513. input: Tensor, min_val: float = -1.0, max_val: float = 1.0, inplace: bool = False
  514. ) -> Tensor:
  515. r"""This is the quantized version of :func:`~torch.nn.functional.hardtanh`."""
  516. if not input.is_quantized:
  517. raise ValueError("Input to 'quantized.hardtanh' must be quantized!")
  518. if inplace:
  519. return torch._C._nn.hardtanh_(input, min_val, max_val)
  520. return torch._C._nn.hardtanh(input, min_val, max_val)
  521. def hardswish(input: Tensor, scale: float, zero_point: int) -> Tensor:
  522. r"""This is the quantized version of :func:`~torch.nn.functional.hardswish`.
  523. Args:
  524. input: quantized input
  525. scale: quantization scale of the output tensor
  526. zero_point: quantization zero point of the output tensor
  527. """
  528. if not input.is_quantized:
  529. raise ValueError("Input to 'quantized.hardswish' must be quantized!")
  530. return torch._ops.ops.quantized.hardswish(input, scale, zero_point)
  531. def threshold(input: Tensor, threshold: float, value: float) -> Tensor:
  532. r"""Applies the quantized version of the threshold function element-wise:
  533. .. math::
  534. x = \begin{cases}
  535. x & \text{if~} x > \text{threshold} \\
  536. \text{value} & \text{otherwise}
  537. \end{cases}
  538. See :class:`~torch.nn.Threshold` for more details.
  539. """
  540. if not input.is_quantized:
  541. raise ValueError("Input to 'quantized.threshold' must be quantized!")
  542. if threshold is None:
  543. raise ValueError("Input to 'threshold' must be specified!")
  544. if value is None:
  545. raise ValueError("Input to 'value' must be specified!")
  546. return torch._ops.ops.quantized.threshold(input, threshold, value)
  547. def elu(input: Tensor, scale: float, zero_point: int, alpha: float = 1.0) -> Tensor:
  548. r"""This is the quantized version of :func:`~torch.nn.functional.elu`.
  549. Args:
  550. input: quantized input
  551. scale: quantization scale of the output tensor
  552. zero_point: quantization zero point of the output tensor
  553. alpha: the alpha constant
  554. """
  555. if not input.is_quantized:
  556. raise ValueError("Input to 'quantized.elu' must be quantized!")
  557. return torch.ops.quantized.elu(input, scale, zero_point, alpha)
  558. def hardsigmoid(input: Tensor, inplace: bool = False) -> Tensor:
  559. r"""This is the quantized version of :func:`~torch.nn.functional.hardsigmoid`."""
  560. if not input.is_quantized:
  561. raise ValueError("Input to 'quantized.hardsigmoid' must be quantized!")
  562. if inplace:
  563. return torch._C._nn.hardsigmoid_(input) # type: ignore[attr-defined]
  564. return torch._C._nn.hardsigmoid(input)
  565. def clamp(input: Tensor, min_: float, max_: float) -> Tensor:
  566. r"""float(input, min\_, max\_) -> Tensor
  567. Applies the clamp function element-wise.
  568. See :class:`~torch.ao.nn.quantized.clamp` for more details.
  569. Args:
  570. input: quantized input
  571. min_: minimum value for clamping
  572. max_: maximum value for clamping
  573. """
  574. if not input.is_quantized:
  575. raise ValueError("Input to 'quantized.clamp' must be quantized!")
  576. return torch.clamp(input, min_, max_)
  577. def upsample(input, size=None, scale_factor=None, mode="nearest", align_corners=None):
  578. r"""Upsamples the input to either the given :attr:`size` or the given
  579. :attr:`scale_factor`
  580. .. warning::
  581. This function is deprecated in favor of
  582. :func:`torch.ao.nn.quantized.functional.interpolate`.
  583. This is equivalent with ``nn.quantized.functional.interpolate(...)``.
  584. See :func:`torch.nn.functional.interpolate` for implementation details.
  585. The input dimensions are interpreted in the form:
  586. `mini-batch x channels x [optional depth] x [optional height] x width`.
  587. .. note:: The input quantization parameters propagate to the output.
  588. .. note:: Only 2D input is supported for quantized inputs
  589. .. note:: Only the following modes are supported for the quantized inputs:
  590. - `bilinear`
  591. - `nearest`
  592. Args:
  593. input (Tensor): quantized input tensor
  594. size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]):
  595. output spatial size.
  596. scale_factor (float or Tuple[float]): multiplier for spatial size. Has to be an integer.
  597. mode (str): algorithm used for upsampling:
  598. ``'nearest'`` | ``'bilinear'``
  599. align_corners (bool, optional): Geometrically, we consider the pixels of the
  600. input and output as squares rather than points.
  601. If set to ``True``, the input and output tensors are aligned by the
  602. center points of their corner pixels, preserving the values at the corner pixels.
  603. If set to ``False``, the input and output tensors are aligned by the corner
  604. points of their corner pixels, and the interpolation uses edge value padding
  605. for out-of-boundary values, making this operation *independent* of input size
  606. when :attr:`scale_factor` is kept the same. This only has an effect when :attr:`mode`
  607. is ``'bilinear'``.
  608. Default: ``False``
  609. .. warning::
  610. With ``align_corners = True``, the linearly interpolating modes
  611. (`bilinear`) don't proportionally align the
  612. output and input pixels, and thus the output values can depend on the
  613. input size. This was the default behavior for these modes up to version
  614. 0.3.1. Since then, the default behavior is ``align_corners = False``.
  615. See :class:`~torch.nn.Upsample` for concrete examples on how this
  616. affects the outputs.
  617. """
  618. warnings.warn(
  619. "nn.quantized.functional.upsample is deprecated. Use nn.quantized.functional.interpolate instead."
  620. )
  621. return interpolate(input, size, scale_factor, mode, align_corners)
  622. def upsample_bilinear(input, size=None, scale_factor=None):
  623. r"""Upsamples the input, using bilinear upsampling.
  624. .. warning::
  625. This function is deprecated in favor of
  626. :func:`torch.ao.nn.quantized.functional.interpolate`.
  627. This is equivalent with
  628. ``nn.quantized.functional.interpolate(..., mode='bilinear', align_corners=True)``.
  629. .. note:: The input quantization parameters propagate to the output.
  630. .. note:: Only 2D inputs are supported
  631. Args:
  632. input (Tensor): quantized input
  633. size (int or Tuple[int, int]): output spatial size.
  634. scale_factor (int or Tuple[int, int]): multiplier for spatial size
  635. """
  636. # DeprecationWarning is ignored by default
  637. warnings.warn(
  638. "nn.quantized.functional.upsample_bilinear is deprecated. Use nn.quantized.functional.interpolate instead."
  639. )
  640. return interpolate(input, size, scale_factor, mode="bilinear", align_corners=True)
  641. def upsample_nearest(input, size=None, scale_factor=None):
  642. r"""Upsamples the input, using nearest neighbours' pixel values.
  643. .. warning::
  644. This function is deprecated in favor of
  645. :func:`torch.ao.nn.quantized.functional.interpolate`.
  646. This is equivalent with ``nn.quantized.functional.interpolate(..., mode='nearest')``.
  647. .. note:: The input quantization parameters propagate to the output.
  648. .. note:: Only 2D inputs are supported
  649. Args:
  650. input (Tensor): quantized input
  651. size (int or Tuple[int, int] or Tuple[int, int, int]): output spatial
  652. size.
  653. scale_factor (int): multiplier for spatial size. Has to be an integer.
  654. """
  655. # DeprecationWarning is ignored by default
  656. warnings.warn(
  657. "nn.quantized.functional.upsample_nearest is deprecated. Use nn.quantized.functional.interpolate instead."
  658. )
  659. return interpolate(input, size, scale_factor, mode="nearest")