conv.py 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. # -*- coding: utf-8 -*-
  2. import math
  3. import warnings
  4. import torch
  5. from torch import Tensor
  6. from torch.nn.parameter import Parameter, UninitializedParameter
  7. from .. import functional as F
  8. from .. import init
  9. from .lazy import LazyModuleMixin
  10. from .module import Module
  11. from .utils import _single, _pair, _triple, _reverse_repeat_tuple
  12. from torch._torch_docs import reproducibility_notes
  13. from ..common_types import _size_1_t, _size_2_t, _size_3_t
  14. from typing import Optional, List, Tuple, Union
  15. __all__ = ['Conv1d', 'Conv2d', 'Conv3d', 'ConvTranspose1d', 'ConvTranspose2d', 'ConvTranspose3d',
  16. 'LazyConv1d', 'LazyConv2d', 'LazyConv3d', 'LazyConvTranspose1d', 'LazyConvTranspose2d',
  17. 'LazyConvTranspose3d']
  18. convolution_notes = \
  19. {"groups_note": r"""* :attr:`groups` controls the connections between inputs and outputs.
  20. :attr:`in_channels` and :attr:`out_channels` must both be divisible by
  21. :attr:`groups`. For example,
  22. * At groups=1, all inputs are convolved to all outputs.
  23. * At groups=2, the operation becomes equivalent to having two conv
  24. layers side by side, each seeing half the input channels
  25. and producing half the output channels, and both subsequently
  26. concatenated.
  27. * At groups= :attr:`in_channels`, each input channel is convolved with
  28. its own set of filters (of size
  29. :math:`\frac{\text{out\_channels}}{\text{in\_channels}}`).""",
  30. "depthwise_separable_note": r"""When `groups == in_channels` and `out_channels == K * in_channels`,
  31. where `K` is a positive integer, this operation is also known as a "depthwise convolution".
  32. In other words, for an input of size :math:`(N, C_{in}, L_{in})`,
  33. a depthwise convolution with a depthwise multiplier `K` can be performed with the arguments
  34. :math:`(C_\text{in}=C_\text{in}, C_\text{out}=C_\text{in} \times \text{K}, ..., \text{groups}=C_\text{in})`."""} # noqa: B950
  35. class _ConvNd(Module):
  36. __constants__ = ['stride', 'padding', 'dilation', 'groups',
  37. 'padding_mode', 'output_padding', 'in_channels',
  38. 'out_channels', 'kernel_size']
  39. __annotations__ = {'bias': Optional[torch.Tensor]}
  40. def _conv_forward(self, input: Tensor, weight: Tensor, bias: Optional[Tensor]) -> Tensor:
  41. ...
  42. in_channels: int
  43. _reversed_padding_repeated_twice: List[int]
  44. out_channels: int
  45. kernel_size: Tuple[int, ...]
  46. stride: Tuple[int, ...]
  47. padding: Union[str, Tuple[int, ...]]
  48. dilation: Tuple[int, ...]
  49. transposed: bool
  50. output_padding: Tuple[int, ...]
  51. groups: int
  52. padding_mode: str
  53. weight: Tensor
  54. bias: Optional[Tensor]
  55. def __init__(self,
  56. in_channels: int,
  57. out_channels: int,
  58. kernel_size: Tuple[int, ...],
  59. stride: Tuple[int, ...],
  60. padding: Tuple[int, ...],
  61. dilation: Tuple[int, ...],
  62. transposed: bool,
  63. output_padding: Tuple[int, ...],
  64. groups: int,
  65. bias: bool,
  66. padding_mode: str,
  67. device=None,
  68. dtype=None) -> None:
  69. factory_kwargs = {'device': device, 'dtype': dtype}
  70. super().__init__()
  71. if groups <= 0:
  72. raise ValueError('groups must be a positive integer')
  73. if in_channels % groups != 0:
  74. raise ValueError('in_channels must be divisible by groups')
  75. if out_channels % groups != 0:
  76. raise ValueError('out_channels must be divisible by groups')
  77. valid_padding_strings = {'same', 'valid'}
  78. if isinstance(padding, str):
  79. if padding not in valid_padding_strings:
  80. raise ValueError(
  81. "Invalid padding string {!r}, should be one of {}".format(
  82. padding, valid_padding_strings))
  83. if padding == 'same' and any(s != 1 for s in stride):
  84. raise ValueError("padding='same' is not supported for strided convolutions")
  85. valid_padding_modes = {'zeros', 'reflect', 'replicate', 'circular'}
  86. if padding_mode not in valid_padding_modes:
  87. raise ValueError("padding_mode must be one of {}, but got padding_mode='{}'".format(
  88. valid_padding_modes, padding_mode))
  89. self.in_channels = in_channels
  90. self.out_channels = out_channels
  91. self.kernel_size = kernel_size
  92. self.stride = stride
  93. self.padding = padding
  94. self.dilation = dilation
  95. self.transposed = transposed
  96. self.output_padding = output_padding
  97. self.groups = groups
  98. self.padding_mode = padding_mode
  99. # `_reversed_padding_repeated_twice` is the padding to be passed to
  100. # `F.pad` if needed (e.g., for non-zero padding types that are
  101. # implemented as two ops: padding + conv). `F.pad` accepts paddings in
  102. # reverse order than the dimension.
  103. if isinstance(self.padding, str):
  104. self._reversed_padding_repeated_twice = [0, 0] * len(kernel_size)
  105. if padding == 'same':
  106. for d, k, i in zip(dilation, kernel_size,
  107. range(len(kernel_size) - 1, -1, -1)):
  108. total_padding = d * (k - 1)
  109. left_pad = total_padding // 2
  110. self._reversed_padding_repeated_twice[2 * i] = left_pad
  111. self._reversed_padding_repeated_twice[2 * i + 1] = (
  112. total_padding - left_pad)
  113. else:
  114. self._reversed_padding_repeated_twice = _reverse_repeat_tuple(self.padding, 2)
  115. if transposed:
  116. self.weight = Parameter(torch.empty(
  117. (in_channels, out_channels // groups, *kernel_size), **factory_kwargs))
  118. else:
  119. self.weight = Parameter(torch.empty(
  120. (out_channels, in_channels // groups, *kernel_size), **factory_kwargs))
  121. if bias:
  122. self.bias = Parameter(torch.empty(out_channels, **factory_kwargs))
  123. else:
  124. self.register_parameter('bias', None)
  125. self.reset_parameters()
  126. def reset_parameters(self) -> None:
  127. # Setting a=sqrt(5) in kaiming_uniform is the same as initializing with
  128. # uniform(-1/sqrt(k), 1/sqrt(k)), where k = weight.size(1) * prod(*kernel_size)
  129. # For more details see: https://github.com/pytorch/pytorch/issues/15314#issuecomment-477448573
  130. init.kaiming_uniform_(self.weight, a=math.sqrt(5))
  131. if self.bias is not None:
  132. fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
  133. if fan_in != 0:
  134. bound = 1 / math.sqrt(fan_in)
  135. init.uniform_(self.bias, -bound, bound)
  136. def extra_repr(self):
  137. s = ('{in_channels}, {out_channels}, kernel_size={kernel_size}'
  138. ', stride={stride}')
  139. if self.padding != (0,) * len(self.padding):
  140. s += ', padding={padding}'
  141. if self.dilation != (1,) * len(self.dilation):
  142. s += ', dilation={dilation}'
  143. if self.output_padding != (0,) * len(self.output_padding):
  144. s += ', output_padding={output_padding}'
  145. if self.groups != 1:
  146. s += ', groups={groups}'
  147. if self.bias is None:
  148. s += ', bias=False'
  149. if self.padding_mode != 'zeros':
  150. s += ', padding_mode={padding_mode}'
  151. return s.format(**self.__dict__)
  152. def __setstate__(self, state):
  153. super().__setstate__(state)
  154. if not hasattr(self, 'padding_mode'):
  155. self.padding_mode = 'zeros'
  156. class Conv1d(_ConvNd):
  157. __doc__ = r"""Applies a 1D convolution over an input signal composed of several input
  158. planes.
  159. In the simplest case, the output value of the layer with input size
  160. :math:`(N, C_{\text{in}}, L)` and output :math:`(N, C_{\text{out}}, L_{\text{out}})` can be
  161. precisely described as:
  162. .. math::
  163. \text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) +
  164. \sum_{k = 0}^{C_{in} - 1} \text{weight}(C_{\text{out}_j}, k)
  165. \star \text{input}(N_i, k)
  166. where :math:`\star` is the valid `cross-correlation`_ operator,
  167. :math:`N` is a batch size, :math:`C` denotes a number of channels,
  168. :math:`L` is a length of signal sequence.
  169. """ + r"""
  170. This module supports :ref:`TensorFloat32<tf32_on_ampere>`.
  171. On certain ROCm devices, when using float16 inputs this module will use :ref:`different precision<fp16_on_mi200>` for backward.
  172. * :attr:`stride` controls the stride for the cross-correlation, a single
  173. number or a one-element tuple.
  174. * :attr:`padding` controls the amount of padding applied to the input. It
  175. can be either a string {{'valid', 'same'}} or a tuple of ints giving the
  176. amount of implicit padding applied on both sides.
  177. * :attr:`dilation` controls the spacing between the kernel points; also
  178. known as the à trous algorithm. It is harder to describe, but this `link`_
  179. has a nice visualization of what :attr:`dilation` does.
  180. {groups_note}
  181. Note:
  182. {depthwise_separable_note}
  183. Note:
  184. {cudnn_reproducibility_note}
  185. Note:
  186. ``padding='valid'`` is the same as no padding. ``padding='same'`` pads
  187. the input so the output has the shape as the input. However, this mode
  188. doesn't support any stride values other than 1.
  189. Note:
  190. This module supports complex data types i.e. ``complex32, complex64, complex128``.
  191. Args:
  192. in_channels (int): Number of channels in the input image
  193. out_channels (int): Number of channels produced by the convolution
  194. kernel_size (int or tuple): Size of the convolving kernel
  195. stride (int or tuple, optional): Stride of the convolution. Default: 1
  196. padding (int, tuple or str, optional): Padding added to both sides of
  197. the input. Default: 0
  198. padding_mode (str, optional): ``'zeros'``, ``'reflect'``,
  199. ``'replicate'`` or ``'circular'``. Default: ``'zeros'``
  200. dilation (int or tuple, optional): Spacing between kernel
  201. elements. Default: 1
  202. groups (int, optional): Number of blocked connections from input
  203. channels to output channels. Default: 1
  204. bias (bool, optional): If ``True``, adds a learnable bias to the
  205. output. Default: ``True``
  206. """.format(**reproducibility_notes, **convolution_notes) + r"""
  207. Shape:
  208. - Input: :math:`(N, C_{in}, L_{in})` or :math:`(C_{in}, L_{in})`
  209. - Output: :math:`(N, C_{out}, L_{out})` or :math:`(C_{out}, L_{out})`, where
  210. .. math::
  211. L_{out} = \left\lfloor\frac{L_{in} + 2 \times \text{padding} - \text{dilation}
  212. \times (\text{kernel\_size} - 1) - 1}{\text{stride}} + 1\right\rfloor
  213. Attributes:
  214. weight (Tensor): the learnable weights of the module of shape
  215. :math:`(\text{out\_channels},
  216. \frac{\text{in\_channels}}{\text{groups}}, \text{kernel\_size})`.
  217. The values of these weights are sampled from
  218. :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  219. :math:`k = \frac{groups}{C_\text{in} * \text{kernel\_size}}`
  220. bias (Tensor): the learnable bias of the module of shape
  221. (out_channels). If :attr:`bias` is ``True``, then the values of these weights are
  222. sampled from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  223. :math:`k = \frac{groups}{C_\text{in} * \text{kernel\_size}}`
  224. Examples::
  225. >>> m = nn.Conv1d(16, 33, 3, stride=2)
  226. >>> input = torch.randn(20, 16, 50)
  227. >>> output = m(input)
  228. .. _cross-correlation:
  229. https://en.wikipedia.org/wiki/Cross-correlation
  230. .. _link:
  231. https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
  232. """
  233. def __init__(
  234. self,
  235. in_channels: int,
  236. out_channels: int,
  237. kernel_size: _size_1_t,
  238. stride: _size_1_t = 1,
  239. padding: Union[str, _size_1_t] = 0,
  240. dilation: _size_1_t = 1,
  241. groups: int = 1,
  242. bias: bool = True,
  243. padding_mode: str = 'zeros', # TODO: refine this type
  244. device=None,
  245. dtype=None
  246. ) -> None:
  247. factory_kwargs = {'device': device, 'dtype': dtype}
  248. # we create new variables below to make mypy happy since kernel_size has
  249. # type Union[int, Tuple[int]] and kernel_size_ has type Tuple[int]
  250. kernel_size_ = _single(kernel_size)
  251. stride_ = _single(stride)
  252. padding_ = padding if isinstance(padding, str) else _single(padding)
  253. dilation_ = _single(dilation)
  254. super().__init__(
  255. in_channels, out_channels, kernel_size_, stride_, padding_, dilation_,
  256. False, _single(0), groups, bias, padding_mode, **factory_kwargs)
  257. def _conv_forward(self, input: Tensor, weight: Tensor, bias: Optional[Tensor]):
  258. if self.padding_mode != 'zeros':
  259. return F.conv1d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode),
  260. weight, bias, self.stride,
  261. _single(0), self.dilation, self.groups)
  262. return F.conv1d(input, weight, bias, self.stride,
  263. self.padding, self.dilation, self.groups)
  264. def forward(self, input: Tensor) -> Tensor:
  265. return self._conv_forward(input, self.weight, self.bias)
  266. class Conv2d(_ConvNd):
  267. __doc__ = r"""Applies a 2D convolution over an input signal composed of several input
  268. planes.
  269. In the simplest case, the output value of the layer with input size
  270. :math:`(N, C_{\text{in}}, H, W)` and output :math:`(N, C_{\text{out}}, H_{\text{out}}, W_{\text{out}})`
  271. can be precisely described as:
  272. .. math::
  273. \text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) +
  274. \sum_{k = 0}^{C_{\text{in}} - 1} \text{weight}(C_{\text{out}_j}, k) \star \text{input}(N_i, k)
  275. where :math:`\star` is the valid 2D `cross-correlation`_ operator,
  276. :math:`N` is a batch size, :math:`C` denotes a number of channels,
  277. :math:`H` is a height of input planes in pixels, and :math:`W` is
  278. width in pixels.
  279. """ + r"""
  280. This module supports :ref:`TensorFloat32<tf32_on_ampere>`.
  281. On certain ROCm devices, when using float16 inputs this module will use :ref:`different precision<fp16_on_mi200>` for backward.
  282. * :attr:`stride` controls the stride for the cross-correlation, a single
  283. number or a tuple.
  284. * :attr:`padding` controls the amount of padding applied to the input. It
  285. can be either a string {{'valid', 'same'}} or an int / a tuple of ints giving the
  286. amount of implicit padding applied on both sides.
  287. * :attr:`dilation` controls the spacing between the kernel points; also
  288. known as the à trous algorithm. It is harder to describe, but this `link`_
  289. has a nice visualization of what :attr:`dilation` does.
  290. {groups_note}
  291. The parameters :attr:`kernel_size`, :attr:`stride`, :attr:`padding`, :attr:`dilation` can either be:
  292. - a single ``int`` -- in which case the same value is used for the height and width dimension
  293. - a ``tuple`` of two ints -- in which case, the first `int` is used for the height dimension,
  294. and the second `int` for the width dimension
  295. Note:
  296. {depthwise_separable_note}
  297. Note:
  298. {cudnn_reproducibility_note}
  299. Note:
  300. ``padding='valid'`` is the same as no padding. ``padding='same'`` pads
  301. the input so the output has the shape as the input. However, this mode
  302. doesn't support any stride values other than 1.
  303. Note:
  304. This module supports complex data types i.e. ``complex32, complex64, complex128``.
  305. Args:
  306. in_channels (int): Number of channels in the input image
  307. out_channels (int): Number of channels produced by the convolution
  308. kernel_size (int or tuple): Size of the convolving kernel
  309. stride (int or tuple, optional): Stride of the convolution. Default: 1
  310. padding (int, tuple or str, optional): Padding added to all four sides of
  311. the input. Default: 0
  312. padding_mode (str, optional): ``'zeros'``, ``'reflect'``,
  313. ``'replicate'`` or ``'circular'``. Default: ``'zeros'``
  314. dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
  315. groups (int, optional): Number of blocked connections from input
  316. channels to output channels. Default: 1
  317. bias (bool, optional): If ``True``, adds a learnable bias to the
  318. output. Default: ``True``
  319. """.format(**reproducibility_notes, **convolution_notes) + r"""
  320. Shape:
  321. - Input: :math:`(N, C_{in}, H_{in}, W_{in})` or :math:`(C_{in}, H_{in}, W_{in})`
  322. - Output: :math:`(N, C_{out}, H_{out}, W_{out})` or :math:`(C_{out}, H_{out}, W_{out})`, where
  323. .. math::
  324. H_{out} = \left\lfloor\frac{H_{in} + 2 \times \text{padding}[0] - \text{dilation}[0]
  325. \times (\text{kernel\_size}[0] - 1) - 1}{\text{stride}[0]} + 1\right\rfloor
  326. .. math::
  327. W_{out} = \left\lfloor\frac{W_{in} + 2 \times \text{padding}[1] - \text{dilation}[1]
  328. \times (\text{kernel\_size}[1] - 1) - 1}{\text{stride}[1]} + 1\right\rfloor
  329. Attributes:
  330. weight (Tensor): the learnable weights of the module of shape
  331. :math:`(\text{out\_channels}, \frac{\text{in\_channels}}{\text{groups}},`
  332. :math:`\text{kernel\_size[0]}, \text{kernel\_size[1]})`.
  333. The values of these weights are sampled from
  334. :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  335. :math:`k = \frac{groups}{C_\text{in} * \prod_{i=0}^{1}\text{kernel\_size}[i]}`
  336. bias (Tensor): the learnable bias of the module of shape
  337. (out_channels). If :attr:`bias` is ``True``,
  338. then the values of these weights are
  339. sampled from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  340. :math:`k = \frac{groups}{C_\text{in} * \prod_{i=0}^{1}\text{kernel\_size}[i]}`
  341. Examples:
  342. >>> # With square kernels and equal stride
  343. >>> m = nn.Conv2d(16, 33, 3, stride=2)
  344. >>> # non-square kernels and unequal stride and with padding
  345. >>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
  346. >>> # non-square kernels and unequal stride and with padding and dilation
  347. >>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
  348. >>> input = torch.randn(20, 16, 50, 100)
  349. >>> output = m(input)
  350. .. _cross-correlation:
  351. https://en.wikipedia.org/wiki/Cross-correlation
  352. .. _link:
  353. https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
  354. """
  355. def __init__(
  356. self,
  357. in_channels: int,
  358. out_channels: int,
  359. kernel_size: _size_2_t,
  360. stride: _size_2_t = 1,
  361. padding: Union[str, _size_2_t] = 0,
  362. dilation: _size_2_t = 1,
  363. groups: int = 1,
  364. bias: bool = True,
  365. padding_mode: str = 'zeros', # TODO: refine this type
  366. device=None,
  367. dtype=None
  368. ) -> None:
  369. factory_kwargs = {'device': device, 'dtype': dtype}
  370. kernel_size_ = _pair(kernel_size)
  371. stride_ = _pair(stride)
  372. padding_ = padding if isinstance(padding, str) else _pair(padding)
  373. dilation_ = _pair(dilation)
  374. super().__init__(
  375. in_channels, out_channels, kernel_size_, stride_, padding_, dilation_,
  376. False, _pair(0), groups, bias, padding_mode, **factory_kwargs)
  377. def _conv_forward(self, input: Tensor, weight: Tensor, bias: Optional[Tensor]):
  378. if self.padding_mode != 'zeros':
  379. return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode),
  380. weight, bias, self.stride,
  381. _pair(0), self.dilation, self.groups)
  382. return F.conv2d(input, weight, bias, self.stride,
  383. self.padding, self.dilation, self.groups)
  384. def forward(self, input: Tensor) -> Tensor:
  385. return self._conv_forward(input, self.weight, self.bias)
  386. class Conv3d(_ConvNd):
  387. __doc__ = r"""Applies a 3D convolution over an input signal composed of several input
  388. planes.
  389. In the simplest case, the output value of the layer with input size :math:`(N, C_{in}, D, H, W)`
  390. and output :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})` can be precisely described as:
  391. .. math::
  392. out(N_i, C_{out_j}) = bias(C_{out_j}) +
  393. \sum_{k = 0}^{C_{in} - 1} weight(C_{out_j}, k) \star input(N_i, k)
  394. where :math:`\star` is the valid 3D `cross-correlation`_ operator
  395. """ + r"""
  396. This module supports :ref:`TensorFloat32<tf32_on_ampere>`.
  397. On certain ROCm devices, when using float16 inputs this module will use :ref:`different precision<fp16_on_mi200>` for backward.
  398. * :attr:`stride` controls the stride for the cross-correlation.
  399. * :attr:`padding` controls the amount of padding applied to the input. It
  400. can be either a string {{'valid', 'same'}} or a tuple of ints giving the
  401. amount of implicit padding applied on both sides.
  402. * :attr:`dilation` controls the spacing between the kernel points; also known as the à trous algorithm.
  403. It is harder to describe, but this `link`_ has a nice visualization of what :attr:`dilation` does.
  404. {groups_note}
  405. The parameters :attr:`kernel_size`, :attr:`stride`, :attr:`padding`, :attr:`dilation` can either be:
  406. - a single ``int`` -- in which case the same value is used for the depth, height and width dimension
  407. - a ``tuple`` of three ints -- in which case, the first `int` is used for the depth dimension,
  408. the second `int` for the height dimension and the third `int` for the width dimension
  409. Note:
  410. {depthwise_separable_note}
  411. Note:
  412. {cudnn_reproducibility_note}
  413. Note:
  414. ``padding='valid'`` is the same as no padding. ``padding='same'`` pads
  415. the input so the output has the shape as the input. However, this mode
  416. doesn't support any stride values other than 1.
  417. Note:
  418. This module supports complex data types i.e. ``complex32, complex64, complex128``.
  419. Args:
  420. in_channels (int): Number of channels in the input image
  421. out_channels (int): Number of channels produced by the convolution
  422. kernel_size (int or tuple): Size of the convolving kernel
  423. stride (int or tuple, optional): Stride of the convolution. Default: 1
  424. padding (int, tuple or str, optional): Padding added to all six sides of
  425. the input. Default: 0
  426. padding_mode (str, optional): ``'zeros'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'zeros'``
  427. dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
  428. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
  429. bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
  430. """.format(**reproducibility_notes, **convolution_notes) + r"""
  431. Shape:
  432. - Input: :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})` or :math:`(C_{in}, D_{in}, H_{in}, W_{in})`
  433. - Output: :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})` or :math:`(C_{out}, D_{out}, H_{out}, W_{out})`,
  434. where
  435. .. math::
  436. D_{out} = \left\lfloor\frac{D_{in} + 2 \times \text{padding}[0] - \text{dilation}[0]
  437. \times (\text{kernel\_size}[0] - 1) - 1}{\text{stride}[0]} + 1\right\rfloor
  438. .. math::
  439. H_{out} = \left\lfloor\frac{H_{in} + 2 \times \text{padding}[1] - \text{dilation}[1]
  440. \times (\text{kernel\_size}[1] - 1) - 1}{\text{stride}[1]} + 1\right\rfloor
  441. .. math::
  442. W_{out} = \left\lfloor\frac{W_{in} + 2 \times \text{padding}[2] - \text{dilation}[2]
  443. \times (\text{kernel\_size}[2] - 1) - 1}{\text{stride}[2]} + 1\right\rfloor
  444. Attributes:
  445. weight (Tensor): the learnable weights of the module of shape
  446. :math:`(\text{out\_channels}, \frac{\text{in\_channels}}{\text{groups}},`
  447. :math:`\text{kernel\_size[0]}, \text{kernel\_size[1]}, \text{kernel\_size[2]})`.
  448. The values of these weights are sampled from
  449. :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  450. :math:`k = \frac{groups}{C_\text{in} * \prod_{i=0}^{2}\text{kernel\_size}[i]}`
  451. bias (Tensor): the learnable bias of the module of shape (out_channels). If :attr:`bias` is ``True``,
  452. then the values of these weights are
  453. sampled from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  454. :math:`k = \frac{groups}{C_\text{in} * \prod_{i=0}^{2}\text{kernel\_size}[i]}`
  455. Examples::
  456. >>> # With square kernels and equal stride
  457. >>> m = nn.Conv3d(16, 33, 3, stride=2)
  458. >>> # non-square kernels and unequal stride and with padding
  459. >>> m = nn.Conv3d(16, 33, (3, 5, 2), stride=(2, 1, 1), padding=(4, 2, 0))
  460. >>> input = torch.randn(20, 16, 10, 50, 100)
  461. >>> output = m(input)
  462. .. _cross-correlation:
  463. https://en.wikipedia.org/wiki/Cross-correlation
  464. .. _link:
  465. https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
  466. """
  467. def __init__(
  468. self,
  469. in_channels: int,
  470. out_channels: int,
  471. kernel_size: _size_3_t,
  472. stride: _size_3_t = 1,
  473. padding: Union[str, _size_3_t] = 0,
  474. dilation: _size_3_t = 1,
  475. groups: int = 1,
  476. bias: bool = True,
  477. padding_mode: str = 'zeros',
  478. device=None,
  479. dtype=None
  480. ) -> None:
  481. factory_kwargs = {'device': device, 'dtype': dtype}
  482. kernel_size_ = _triple(kernel_size)
  483. stride_ = _triple(stride)
  484. padding_ = padding if isinstance(padding, str) else _triple(padding)
  485. dilation_ = _triple(dilation)
  486. super().__init__(
  487. in_channels, out_channels, kernel_size_, stride_, padding_, dilation_,
  488. False, _triple(0), groups, bias, padding_mode, **factory_kwargs)
  489. def _conv_forward(self, input: Tensor, weight: Tensor, bias: Optional[Tensor]):
  490. if self.padding_mode != "zeros":
  491. return F.conv3d(
  492. F.pad(
  493. input, self._reversed_padding_repeated_twice, mode=self.padding_mode
  494. ),
  495. weight,
  496. bias,
  497. self.stride,
  498. _triple(0),
  499. self.dilation,
  500. self.groups,
  501. )
  502. return F.conv3d(
  503. input, weight, bias, self.stride, self.padding, self.dilation, self.groups
  504. )
  505. def forward(self, input: Tensor) -> Tensor:
  506. return self._conv_forward(input, self.weight, self.bias)
  507. class _ConvTransposeNd(_ConvNd):
  508. def __init__(self, in_channels, out_channels, kernel_size, stride,
  509. padding, dilation, transposed, output_padding,
  510. groups, bias, padding_mode, device=None, dtype=None) -> None:
  511. if padding_mode != 'zeros':
  512. raise ValueError('Only "zeros" padding mode is supported for {}'.format(self.__class__.__name__))
  513. factory_kwargs = {'device': device, 'dtype': dtype}
  514. super().__init__(
  515. in_channels, out_channels, kernel_size, stride,
  516. padding, dilation, transposed, output_padding,
  517. groups, bias, padding_mode, **factory_kwargs)
  518. # dilation being an optional parameter is for backwards
  519. # compatibility
  520. def _output_padding(self, input: Tensor, output_size: Optional[List[int]],
  521. stride: List[int], padding: List[int], kernel_size: List[int],
  522. num_spatial_dims: int, dilation: Optional[List[int]] = None) -> List[int]:
  523. if output_size is None:
  524. ret = _single(self.output_padding) # converting to list if was not already
  525. else:
  526. has_batch_dim = input.dim() == num_spatial_dims + 2
  527. num_non_spatial_dims = 2 if has_batch_dim else 1
  528. if len(output_size) == num_non_spatial_dims + num_spatial_dims:
  529. output_size = output_size[num_non_spatial_dims:]
  530. if len(output_size) != num_spatial_dims:
  531. raise ValueError(
  532. "ConvTranspose{}D: for {}D input, output_size must have {} or {} elements (got {})"
  533. .format(num_spatial_dims, input.dim(), num_spatial_dims,
  534. num_non_spatial_dims + num_spatial_dims, len(output_size)))
  535. min_sizes = torch.jit.annotate(List[int], [])
  536. max_sizes = torch.jit.annotate(List[int], [])
  537. for d in range(num_spatial_dims):
  538. dim_size = ((input.size(d + num_non_spatial_dims) - 1) * stride[d] -
  539. 2 * padding[d] +
  540. (dilation[d] if dilation is not None else 1) * (kernel_size[d] - 1) + 1)
  541. min_sizes.append(dim_size)
  542. max_sizes.append(min_sizes[d] + stride[d] - 1)
  543. for i in range(len(output_size)):
  544. size = output_size[i]
  545. min_size = min_sizes[i]
  546. max_size = max_sizes[i]
  547. if size < min_size or size > max_size:
  548. raise ValueError((
  549. "requested an output size of {}, but valid sizes range "
  550. "from {} to {} (for an input of {})").format(
  551. output_size, min_sizes, max_sizes, input.size()[2:]))
  552. res = torch.jit.annotate(List[int], [])
  553. for d in range(num_spatial_dims):
  554. res.append(output_size[d] - min_sizes[d])
  555. ret = res
  556. return ret
  557. class ConvTranspose1d(_ConvTransposeNd):
  558. __doc__ = r"""Applies a 1D transposed convolution operator over an input image
  559. composed of several input planes.
  560. This module can be seen as the gradient of Conv1d with respect to its input.
  561. It is also known as a fractionally-strided convolution or
  562. a deconvolution (although it is not an actual deconvolution operation as it does
  563. not compute a true inverse of convolution). For more information, see the visualizations
  564. `here`_ and the `Deconvolutional Networks`_ paper.
  565. This module supports :ref:`TensorFloat32<tf32_on_ampere>`.
  566. On certain ROCm devices, when using float16 inputs this module will use :ref:`different precision<fp16_on_mi200>` for backward.
  567. * :attr:`stride` controls the stride for the cross-correlation.
  568. * :attr:`padding` controls the amount of implicit zero padding on both
  569. sides for ``dilation * (kernel_size - 1) - padding`` number of points. See note
  570. below for details.
  571. * :attr:`output_padding` controls the additional size added to one side
  572. of the output shape. See note below for details.
  573. * :attr:`dilation` controls the spacing between the kernel points; also known as the à trous algorithm.
  574. It is harder to describe, but the link `here`_ has a nice visualization of what :attr:`dilation` does.
  575. {groups_note}
  576. Note:
  577. The :attr:`padding` argument effectively adds ``dilation * (kernel_size - 1) - padding``
  578. amount of zero padding to both sizes of the input. This is set so that
  579. when a :class:`~torch.nn.Conv1d` and a :class:`~torch.nn.ConvTranspose1d`
  580. are initialized with same parameters, they are inverses of each other in
  581. regard to the input and output shapes. However, when ``stride > 1``,
  582. :class:`~torch.nn.Conv1d` maps multiple input shapes to the same output
  583. shape. :attr:`output_padding` is provided to resolve this ambiguity by
  584. effectively increasing the calculated output shape on one side. Note
  585. that :attr:`output_padding` is only used to find output shape, but does
  586. not actually add zero-padding to output.
  587. Note:
  588. In some circumstances when using the CUDA backend with CuDNN, this operator
  589. may select a nondeterministic algorithm to increase performance. If this is
  590. undesirable, you can try to make the operation deterministic (potentially at
  591. a performance cost) by setting ``torch.backends.cudnn.deterministic =
  592. True``.
  593. Please see the notes on :doc:`/notes/randomness` for background.
  594. Args:
  595. in_channels (int): Number of channels in the input image
  596. out_channels (int): Number of channels produced by the convolution
  597. kernel_size (int or tuple): Size of the convolving kernel
  598. stride (int or tuple, optional): Stride of the convolution. Default: 1
  599. padding (int or tuple, optional): ``dilation * (kernel_size - 1) - padding`` zero-padding
  600. will be added to both sides of the input. Default: 0
  601. output_padding (int or tuple, optional): Additional size added to one side
  602. of the output shape. Default: 0
  603. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
  604. bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
  605. dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
  606. """.format(**reproducibility_notes, **convolution_notes) + r"""
  607. Shape:
  608. - Input: :math:`(N, C_{in}, L_{in})` or :math:`(C_{in}, L_{in})`
  609. - Output: :math:`(N, C_{out}, L_{out})` or :math:`(C_{out}, L_{out})`, where
  610. .. math::
  611. L_{out} = (L_{in} - 1) \times \text{stride} - 2 \times \text{padding} + \text{dilation}
  612. \times (\text{kernel\_size} - 1) + \text{output\_padding} + 1
  613. Attributes:
  614. weight (Tensor): the learnable weights of the module of shape
  615. :math:`(\text{in\_channels}, \frac{\text{out\_channels}}{\text{groups}},`
  616. :math:`\text{kernel\_size})`.
  617. The values of these weights are sampled from
  618. :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  619. :math:`k = \frac{groups}{C_\text{out} * \text{kernel\_size}}`
  620. bias (Tensor): the learnable bias of the module of shape (out_channels).
  621. If :attr:`bias` is ``True``, then the values of these weights are
  622. sampled from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  623. :math:`k = \frac{groups}{C_\text{out} * \text{kernel\_size}}`
  624. .. _`here`:
  625. https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
  626. .. _`Deconvolutional Networks`:
  627. https://www.matthewzeiler.com/mattzeiler/deconvolutionalnetworks.pdf
  628. """
  629. def __init__(
  630. self,
  631. in_channels: int,
  632. out_channels: int,
  633. kernel_size: _size_1_t,
  634. stride: _size_1_t = 1,
  635. padding: _size_1_t = 0,
  636. output_padding: _size_1_t = 0,
  637. groups: int = 1,
  638. bias: bool = True,
  639. dilation: _size_1_t = 1,
  640. padding_mode: str = 'zeros',
  641. device=None,
  642. dtype=None
  643. ) -> None:
  644. factory_kwargs = {'device': device, 'dtype': dtype}
  645. kernel_size = _single(kernel_size)
  646. stride = _single(stride)
  647. padding = _single(padding)
  648. dilation = _single(dilation)
  649. output_padding = _single(output_padding)
  650. super().__init__(
  651. in_channels, out_channels, kernel_size, stride, padding, dilation,
  652. True, output_padding, groups, bias, padding_mode, **factory_kwargs)
  653. def forward(self, input: Tensor, output_size: Optional[List[int]] = None) -> Tensor:
  654. if self.padding_mode != 'zeros':
  655. raise ValueError('Only `zeros` padding mode is supported for ConvTranspose1d')
  656. assert isinstance(self.padding, tuple)
  657. # One cannot replace List by Tuple or Sequence in "_output_padding" because
  658. # TorchScript does not support `Sequence[T]` or `Tuple[T, ...]`.
  659. num_spatial_dims = 1
  660. output_padding = self._output_padding(
  661. input, output_size, self.stride, self.padding, self.kernel_size, # type: ignore[arg-type]
  662. num_spatial_dims, self.dilation) # type: ignore[arg-type]
  663. return F.conv_transpose1d(
  664. input, self.weight, self.bias, self.stride, self.padding,
  665. output_padding, self.groups, self.dilation)
  666. class ConvTranspose2d(_ConvTransposeNd):
  667. __doc__ = r"""Applies a 2D transposed convolution operator over an input image
  668. composed of several input planes.
  669. This module can be seen as the gradient of Conv2d with respect to its input.
  670. It is also known as a fractionally-strided convolution or
  671. a deconvolution (although it is not an actual deconvolution operation as it does
  672. not compute a true inverse of convolution). For more information, see the visualizations
  673. `here`_ and the `Deconvolutional Networks`_ paper.
  674. This module supports :ref:`TensorFloat32<tf32_on_ampere>`.
  675. On certain ROCm devices, when using float16 inputs this module will use :ref:`different precision<fp16_on_mi200>` for backward.
  676. * :attr:`stride` controls the stride for the cross-correlation.
  677. * :attr:`padding` controls the amount of implicit zero padding on both
  678. sides for ``dilation * (kernel_size - 1) - padding`` number of points. See note
  679. below for details.
  680. * :attr:`output_padding` controls the additional size added to one side
  681. of the output shape. See note below for details.
  682. * :attr:`dilation` controls the spacing between the kernel points; also known as the à trous algorithm.
  683. It is harder to describe, but the link `here`_ has a nice visualization of what :attr:`dilation` does.
  684. {groups_note}
  685. The parameters :attr:`kernel_size`, :attr:`stride`, :attr:`padding`, :attr:`output_padding`
  686. can either be:
  687. - a single ``int`` -- in which case the same value is used for the height and width dimensions
  688. - a ``tuple`` of two ints -- in which case, the first `int` is used for the height dimension,
  689. and the second `int` for the width dimension
  690. Note:
  691. The :attr:`padding` argument effectively adds ``dilation * (kernel_size - 1) - padding``
  692. amount of zero padding to both sizes of the input. This is set so that
  693. when a :class:`~torch.nn.Conv2d` and a :class:`~torch.nn.ConvTranspose2d`
  694. are initialized with same parameters, they are inverses of each other in
  695. regard to the input and output shapes. However, when ``stride > 1``,
  696. :class:`~torch.nn.Conv2d` maps multiple input shapes to the same output
  697. shape. :attr:`output_padding` is provided to resolve this ambiguity by
  698. effectively increasing the calculated output shape on one side. Note
  699. that :attr:`output_padding` is only used to find output shape, but does
  700. not actually add zero-padding to output.
  701. Note:
  702. {cudnn_reproducibility_note}
  703. Args:
  704. in_channels (int): Number of channels in the input image
  705. out_channels (int): Number of channels produced by the convolution
  706. kernel_size (int or tuple): Size of the convolving kernel
  707. stride (int or tuple, optional): Stride of the convolution. Default: 1
  708. padding (int or tuple, optional): ``dilation * (kernel_size - 1) - padding`` zero-padding
  709. will be added to both sides of each dimension in the input. Default: 0
  710. output_padding (int or tuple, optional): Additional size added to one side
  711. of each dimension in the output shape. Default: 0
  712. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
  713. bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
  714. dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
  715. """.format(**reproducibility_notes, **convolution_notes) + r"""
  716. Shape:
  717. - Input: :math:`(N, C_{in}, H_{in}, W_{in})` or :math:`(C_{in}, H_{in}, W_{in})`
  718. - Output: :math:`(N, C_{out}, H_{out}, W_{out})` or :math:`(C_{out}, H_{out}, W_{out})`, where
  719. .. math::
  720. H_{out} = (H_{in} - 1) \times \text{stride}[0] - 2 \times \text{padding}[0] + \text{dilation}[0]
  721. \times (\text{kernel\_size}[0] - 1) + \text{output\_padding}[0] + 1
  722. .. math::
  723. W_{out} = (W_{in} - 1) \times \text{stride}[1] - 2 \times \text{padding}[1] + \text{dilation}[1]
  724. \times (\text{kernel\_size}[1] - 1) + \text{output\_padding}[1] + 1
  725. Attributes:
  726. weight (Tensor): the learnable weights of the module of shape
  727. :math:`(\text{in\_channels}, \frac{\text{out\_channels}}{\text{groups}},`
  728. :math:`\text{kernel\_size[0]}, \text{kernel\_size[1]})`.
  729. The values of these weights are sampled from
  730. :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  731. :math:`k = \frac{groups}{C_\text{out} * \prod_{i=0}^{1}\text{kernel\_size}[i]}`
  732. bias (Tensor): the learnable bias of the module of shape (out_channels)
  733. If :attr:`bias` is ``True``, then the values of these weights are
  734. sampled from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  735. :math:`k = \frac{groups}{C_\text{out} * \prod_{i=0}^{1}\text{kernel\_size}[i]}`
  736. Examples::
  737. >>> # With square kernels and equal stride
  738. >>> m = nn.ConvTranspose2d(16, 33, 3, stride=2)
  739. >>> # non-square kernels and unequal stride and with padding
  740. >>> m = nn.ConvTranspose2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
  741. >>> input = torch.randn(20, 16, 50, 100)
  742. >>> output = m(input)
  743. >>> # exact output size can be also specified as an argument
  744. >>> input = torch.randn(1, 16, 12, 12)
  745. >>> downsample = nn.Conv2d(16, 16, 3, stride=2, padding=1)
  746. >>> upsample = nn.ConvTranspose2d(16, 16, 3, stride=2, padding=1)
  747. >>> h = downsample(input)
  748. >>> h.size()
  749. torch.Size([1, 16, 6, 6])
  750. >>> output = upsample(h, output_size=input.size())
  751. >>> output.size()
  752. torch.Size([1, 16, 12, 12])
  753. .. _`here`:
  754. https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
  755. .. _`Deconvolutional Networks`:
  756. https://www.matthewzeiler.com/mattzeiler/deconvolutionalnetworks.pdf
  757. """
  758. def __init__(
  759. self,
  760. in_channels: int,
  761. out_channels: int,
  762. kernel_size: _size_2_t,
  763. stride: _size_2_t = 1,
  764. padding: _size_2_t = 0,
  765. output_padding: _size_2_t = 0,
  766. groups: int = 1,
  767. bias: bool = True,
  768. dilation: _size_2_t = 1,
  769. padding_mode: str = 'zeros',
  770. device=None,
  771. dtype=None
  772. ) -> None:
  773. factory_kwargs = {'device': device, 'dtype': dtype}
  774. kernel_size = _pair(kernel_size)
  775. stride = _pair(stride)
  776. padding = _pair(padding)
  777. dilation = _pair(dilation)
  778. output_padding = _pair(output_padding)
  779. super().__init__(
  780. in_channels, out_channels, kernel_size, stride, padding, dilation,
  781. True, output_padding, groups, bias, padding_mode, **factory_kwargs)
  782. def forward(self, input: Tensor, output_size: Optional[List[int]] = None) -> Tensor:
  783. if self.padding_mode != 'zeros':
  784. raise ValueError('Only `zeros` padding mode is supported for ConvTranspose2d')
  785. assert isinstance(self.padding, tuple)
  786. # One cannot replace List by Tuple or Sequence in "_output_padding" because
  787. # TorchScript does not support `Sequence[T]` or `Tuple[T, ...]`.
  788. num_spatial_dims = 2
  789. output_padding = self._output_padding(
  790. input, output_size, self.stride, self.padding, self.kernel_size, # type: ignore[arg-type]
  791. num_spatial_dims, self.dilation) # type: ignore[arg-type]
  792. return F.conv_transpose2d(
  793. input, self.weight, self.bias, self.stride, self.padding,
  794. output_padding, self.groups, self.dilation)
  795. class ConvTranspose3d(_ConvTransposeNd):
  796. __doc__ = r"""Applies a 3D transposed convolution operator over an input image composed of several input
  797. planes.
  798. The transposed convolution operator multiplies each input value element-wise by a learnable kernel,
  799. and sums over the outputs from all input feature planes.
  800. This module can be seen as the gradient of Conv3d with respect to its input.
  801. It is also known as a fractionally-strided convolution or
  802. a deconvolution (although it is not an actual deconvolution operation as it does
  803. not compute a true inverse of convolution). For more information, see the visualizations
  804. `here`_ and the `Deconvolutional Networks`_ paper.
  805. This module supports :ref:`TensorFloat32<tf32_on_ampere>`.
  806. On certain ROCm devices, when using float16 inputs this module will use :ref:`different precision<fp16_on_mi200>` for backward.
  807. * :attr:`stride` controls the stride for the cross-correlation.
  808. * :attr:`padding` controls the amount of implicit zero padding on both
  809. sides for ``dilation * (kernel_size - 1) - padding`` number of points. See note
  810. below for details.
  811. * :attr:`output_padding` controls the additional size added to one side
  812. of the output shape. See note below for details.
  813. * :attr:`dilation` controls the spacing between the kernel points; also known as the à trous algorithm.
  814. It is harder to describe, but the link `here`_ has a nice visualization of what :attr:`dilation` does.
  815. {groups_note}
  816. The parameters :attr:`kernel_size`, :attr:`stride`, :attr:`padding`, :attr:`output_padding`
  817. can either be:
  818. - a single ``int`` -- in which case the same value is used for the depth, height and width dimensions
  819. - a ``tuple`` of three ints -- in which case, the first `int` is used for the depth dimension,
  820. the second `int` for the height dimension and the third `int` for the width dimension
  821. Note:
  822. The :attr:`padding` argument effectively adds ``dilation * (kernel_size - 1) - padding``
  823. amount of zero padding to both sizes of the input. This is set so that
  824. when a :class:`~torch.nn.Conv3d` and a :class:`~torch.nn.ConvTranspose3d`
  825. are initialized with same parameters, they are inverses of each other in
  826. regard to the input and output shapes. However, when ``stride > 1``,
  827. :class:`~torch.nn.Conv3d` maps multiple input shapes to the same output
  828. shape. :attr:`output_padding` is provided to resolve this ambiguity by
  829. effectively increasing the calculated output shape on one side. Note
  830. that :attr:`output_padding` is only used to find output shape, but does
  831. not actually add zero-padding to output.
  832. Note:
  833. {cudnn_reproducibility_note}
  834. Args:
  835. in_channels (int): Number of channels in the input image
  836. out_channels (int): Number of channels produced by the convolution
  837. kernel_size (int or tuple): Size of the convolving kernel
  838. stride (int or tuple, optional): Stride of the convolution. Default: 1
  839. padding (int or tuple, optional): ``dilation * (kernel_size - 1) - padding`` zero-padding
  840. will be added to both sides of each dimension in the input. Default: 0
  841. output_padding (int or tuple, optional): Additional size added to one side
  842. of each dimension in the output shape. Default: 0
  843. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
  844. bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
  845. dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
  846. """.format(**reproducibility_notes, **convolution_notes) + r"""
  847. Shape:
  848. - Input: :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})` or :math:`(C_{in}, D_{in}, H_{in}, W_{in})`
  849. - Output: :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})` or
  850. :math:`(C_{out}, D_{out}, H_{out}, W_{out})`, where
  851. .. math::
  852. D_{out} = (D_{in} - 1) \times \text{stride}[0] - 2 \times \text{padding}[0] + \text{dilation}[0]
  853. \times (\text{kernel\_size}[0] - 1) + \text{output\_padding}[0] + 1
  854. .. math::
  855. H_{out} = (H_{in} - 1) \times \text{stride}[1] - 2 \times \text{padding}[1] + \text{dilation}[1]
  856. \times (\text{kernel\_size}[1] - 1) + \text{output\_padding}[1] + 1
  857. .. math::
  858. W_{out} = (W_{in} - 1) \times \text{stride}[2] - 2 \times \text{padding}[2] + \text{dilation}[2]
  859. \times (\text{kernel\_size}[2] - 1) + \text{output\_padding}[2] + 1
  860. Attributes:
  861. weight (Tensor): the learnable weights of the module of shape
  862. :math:`(\text{in\_channels}, \frac{\text{out\_channels}}{\text{groups}},`
  863. :math:`\text{kernel\_size[0]}, \text{kernel\_size[1]}, \text{kernel\_size[2]})`.
  864. The values of these weights are sampled from
  865. :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  866. :math:`k = \frac{groups}{C_\text{out} * \prod_{i=0}^{2}\text{kernel\_size}[i]}`
  867. bias (Tensor): the learnable bias of the module of shape (out_channels)
  868. If :attr:`bias` is ``True``, then the values of these weights are
  869. sampled from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  870. :math:`k = \frac{groups}{C_\text{out} * \prod_{i=0}^{2}\text{kernel\_size}[i]}`
  871. Examples::
  872. >>> # With square kernels and equal stride
  873. >>> m = nn.ConvTranspose3d(16, 33, 3, stride=2)
  874. >>> # non-square kernels and unequal stride and with padding
  875. >>> m = nn.ConvTranspose3d(16, 33, (3, 5, 2), stride=(2, 1, 1), padding=(0, 4, 2))
  876. >>> input = torch.randn(20, 16, 10, 50, 100)
  877. >>> output = m(input)
  878. .. _`here`:
  879. https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
  880. .. _`Deconvolutional Networks`:
  881. https://www.matthewzeiler.com/mattzeiler/deconvolutionalnetworks.pdf
  882. """
  883. def __init__(
  884. self,
  885. in_channels: int,
  886. out_channels: int,
  887. kernel_size: _size_3_t,
  888. stride: _size_3_t = 1,
  889. padding: _size_3_t = 0,
  890. output_padding: _size_3_t = 0,
  891. groups: int = 1,
  892. bias: bool = True,
  893. dilation: _size_3_t = 1,
  894. padding_mode: str = 'zeros',
  895. device=None,
  896. dtype=None
  897. ) -> None:
  898. factory_kwargs = {'device': device, 'dtype': dtype}
  899. kernel_size = _triple(kernel_size)
  900. stride = _triple(stride)
  901. padding = _triple(padding)
  902. dilation = _triple(dilation)
  903. output_padding = _triple(output_padding)
  904. super().__init__(
  905. in_channels, out_channels, kernel_size, stride, padding, dilation,
  906. True, output_padding, groups, bias, padding_mode, **factory_kwargs)
  907. def forward(self, input: Tensor, output_size: Optional[List[int]] = None) -> Tensor:
  908. if self.padding_mode != 'zeros':
  909. raise ValueError('Only `zeros` padding mode is supported for ConvTranspose3d')
  910. assert isinstance(self.padding, tuple)
  911. # One cannot replace List by Tuple or Sequence in "_output_padding" because
  912. # TorchScript does not support `Sequence[T]` or `Tuple[T, ...]`.
  913. num_spatial_dims = 3
  914. output_padding = self._output_padding(
  915. input, output_size, self.stride, self.padding, self.kernel_size, # type: ignore[arg-type]
  916. num_spatial_dims, self.dilation) # type: ignore[arg-type]
  917. return F.conv_transpose3d(
  918. input, self.weight, self.bias, self.stride, self.padding,
  919. output_padding, self.groups, self.dilation)
  920. # TODO: Deprecate and remove the following alias `_ConvTransposeMixin`.
  921. #
  922. # `_ConvTransposeMixin` was a mixin that was removed. It is meant to be used
  923. # with `_ConvNd` to construct actual module classes that implements conv
  924. # transpose ops:
  925. #
  926. # class MyConvTranspose(_ConvNd, _ConvTransposeMixin):
  927. # ...
  928. #
  929. # In PyTorch, it has been replaced by `_ConvTransposeNd`, which is a proper
  930. # subclass of `_ConvNd`. However, some user code in the wild still (incorrectly)
  931. # use the internal class `_ConvTransposeMixin`. Hence, we provide this alias
  932. # for BC, because it is cheap and easy for us to do so, even though that
  933. # `_ConvTransposeNd` is really not a mixin anymore (but multiple inheritance as
  934. # above would still work).
  935. class _ConvTransposeMixin(_ConvTransposeNd):
  936. def __init__(self, *args, **kwargs):
  937. warnings.warn(
  938. "_ConvTransposeMixin is a deprecated internal class. "
  939. "Please consider using public APIs.")
  940. super().__init__(*args, **kwargs)
  941. # TODO: Conv2dLocal
  942. # TODO: Conv2dMap
  943. # TODO: ConvTranspose2dMap
  944. class _LazyConvXdMixin(LazyModuleMixin):
  945. groups: int
  946. transposed: bool
  947. in_channels: int
  948. out_channels: int
  949. kernel_size: Tuple[int, ...]
  950. weight: UninitializedParameter
  951. bias: UninitializedParameter
  952. def reset_parameters(self) -> None:
  953. # has_uninitialized_params is defined in parent class and it is using a protocol on self
  954. if not self.has_uninitialized_params() and self.in_channels != 0: # type: ignore[misc]
  955. # "type:ignore[..]" is required because mypy thinks that "reset_parameters" is undefined
  956. # in super class. Turns out that it is defined in _ConvND which is inherited by any class
  957. # that also inherits _LazyConvXdMixin
  958. super().reset_parameters() # type: ignore[misc]
  959. # Signature of "initialize_parameters" is incompatible with the definition in supertype LazyModuleMixin
  960. def initialize_parameters(self, input) -> None: # type: ignore[override]
  961. # defined by parent class but using a protocol
  962. if self.has_uninitialized_params(): # type: ignore[misc]
  963. self.in_channels = self._get_in_channels(input)
  964. if self.in_channels % self.groups != 0:
  965. raise ValueError('in_channels must be divisible by groups')
  966. assert isinstance(self.weight, UninitializedParameter)
  967. if self.transposed:
  968. self.weight.materialize((
  969. self.in_channels, self.out_channels // self.groups, *self.kernel_size))
  970. else:
  971. self.weight.materialize((
  972. self.out_channels, self.in_channels // self.groups, *self.kernel_size))
  973. if self.bias is not None:
  974. assert isinstance(self.bias, UninitializedParameter)
  975. self.bias.materialize((self.out_channels,))
  976. self.reset_parameters()
  977. # Function to extract in_channels from first input.
  978. def _get_in_channels(self, input: Tensor) -> int:
  979. num_spatial_dims = self._get_num_spatial_dims()
  980. num_dims_no_batch = num_spatial_dims + 1 # +1 for channels dim
  981. num_dims_batch = num_dims_no_batch + 1
  982. if input.dim() not in (num_dims_no_batch, num_dims_batch):
  983. raise RuntimeError("Expected {}D (unbatched) or {}D (batched) input to {}, but "
  984. "got input of size: {}".format(num_dims_no_batch, num_dims_batch,
  985. self.__class__.__name__, input.shape))
  986. return input.shape[1] if input.dim() == num_dims_batch else input.shape[0]
  987. # Function to return the number of spatial dims expected for inputs to the module.
  988. # This is expected to be implemented by subclasses.
  989. def _get_num_spatial_dims(self) -> int:
  990. raise NotImplementedError()
  991. # LazyConv1d defines weight as a Tensor but derived class defines it as UnitializeParameter
  992. class LazyConv1d(_LazyConvXdMixin, Conv1d): # type: ignore[misc]
  993. r"""A :class:`torch.nn.Conv1d` module with lazy initialization of
  994. the ``in_channels`` argument of the :class:`Conv1d` that is inferred from
  995. the ``input.size(1)``.
  996. The attributes that will be lazily initialized are `weight` and `bias`.
  997. Check the :class:`torch.nn.modules.lazy.LazyModuleMixin` for further documentation
  998. on lazy modules and their limitations.
  999. Args:
  1000. out_channels (int): Number of channels produced by the convolution
  1001. kernel_size (int or tuple): Size of the convolving kernel
  1002. stride (int or tuple, optional): Stride of the convolution. Default: 1
  1003. padding (int or tuple, optional): Zero-padding added to both sides of
  1004. the input. Default: 0
  1005. padding_mode (str, optional): ``'zeros'``, ``'reflect'``,
  1006. ``'replicate'`` or ``'circular'``. Default: ``'zeros'``
  1007. dilation (int or tuple, optional): Spacing between kernel
  1008. elements. Default: 1
  1009. groups (int, optional): Number of blocked connections from input
  1010. channels to output channels. Default: 1
  1011. bias (bool, optional): If ``True``, adds a learnable bias to the
  1012. output. Default: ``True``
  1013. .. seealso:: :class:`torch.nn.Conv1d` and :class:`torch.nn.modules.lazy.LazyModuleMixin`
  1014. """
  1015. # super class define this variable as None. "type: ignore[..] is required
  1016. # since we are redefining the variable.
  1017. cls_to_become = Conv1d # type: ignore[assignment]
  1018. def __init__(
  1019. self,
  1020. out_channels: int,
  1021. kernel_size: _size_1_t,
  1022. stride: _size_1_t = 1,
  1023. padding: _size_1_t = 0,
  1024. dilation: _size_1_t = 1,
  1025. groups: int = 1,
  1026. bias: bool = True,
  1027. padding_mode: str = 'zeros',
  1028. device=None,
  1029. dtype=None
  1030. ) -> None:
  1031. factory_kwargs = {'device': device, 'dtype': dtype}
  1032. super().__init__(
  1033. 0,
  1034. 0,
  1035. kernel_size,
  1036. stride,
  1037. padding,
  1038. dilation,
  1039. groups,
  1040. # bias is hardcoded to False to avoid creating tensor
  1041. # that will soon be overwritten.
  1042. False,
  1043. padding_mode,
  1044. **factory_kwargs
  1045. )
  1046. self.weight = UninitializedParameter(**factory_kwargs)
  1047. self.out_channels = out_channels
  1048. if bias:
  1049. self.bias = UninitializedParameter(**factory_kwargs)
  1050. def _get_num_spatial_dims(self) -> int:
  1051. return 1
  1052. # LazyConv2d defines weight as a Tensor but derived class defines it as UnitializeParameter
  1053. class LazyConv2d(_LazyConvXdMixin, Conv2d): # type: ignore[misc]
  1054. r"""A :class:`torch.nn.Conv2d` module with lazy initialization of
  1055. the ``in_channels`` argument of the :class:`Conv2d` that is inferred from
  1056. the ``input.size(1)``.
  1057. The attributes that will be lazily initialized are `weight` and `bias`.
  1058. Check the :class:`torch.nn.modules.lazy.LazyModuleMixin` for further documentation
  1059. on lazy modules and their limitations.
  1060. Args:
  1061. out_channels (int): Number of channels produced by the convolution
  1062. kernel_size (int or tuple): Size of the convolving kernel
  1063. stride (int or tuple, optional): Stride of the convolution. Default: 1
  1064. padding (int or tuple, optional): Zero-padding added to both sides of
  1065. the input. Default: 0
  1066. padding_mode (str, optional): ``'zeros'``, ``'reflect'``,
  1067. ``'replicate'`` or ``'circular'``. Default: ``'zeros'``
  1068. dilation (int or tuple, optional): Spacing between kernel
  1069. elements. Default: 1
  1070. groups (int, optional): Number of blocked connections from input
  1071. channels to output channels. Default: 1
  1072. bias (bool, optional): If ``True``, adds a learnable bias to the
  1073. output. Default: ``True``
  1074. .. seealso:: :class:`torch.nn.Conv2d` and :class:`torch.nn.modules.lazy.LazyModuleMixin`
  1075. """
  1076. # super class define this variable as None. "type: ignore[..] is required
  1077. # since we are redefining the variable.
  1078. cls_to_become = Conv2d # type: ignore[assignment]
  1079. def __init__(
  1080. self,
  1081. out_channels: int,
  1082. kernel_size: _size_2_t,
  1083. stride: _size_2_t = 1,
  1084. padding: _size_2_t = 0,
  1085. dilation: _size_2_t = 1,
  1086. groups: int = 1,
  1087. bias: bool = True,
  1088. padding_mode: str = 'zeros', # TODO: refine this type
  1089. device=None,
  1090. dtype=None
  1091. ) -> None:
  1092. factory_kwargs = {'device': device, 'dtype': dtype}
  1093. super().__init__(
  1094. 0,
  1095. 0,
  1096. kernel_size,
  1097. stride,
  1098. padding,
  1099. dilation,
  1100. groups,
  1101. # bias is hardcoded to False to avoid creating tensor
  1102. # that will soon be overwritten.
  1103. False,
  1104. padding_mode,
  1105. **factory_kwargs
  1106. )
  1107. self.weight = UninitializedParameter(**factory_kwargs)
  1108. self.out_channels = out_channels
  1109. if bias:
  1110. self.bias = UninitializedParameter(**factory_kwargs)
  1111. def _get_num_spatial_dims(self) -> int:
  1112. return 2
  1113. # LazyConv3d defines weight as a Tensor but derived class defines it as UnitializeParameter
  1114. class LazyConv3d(_LazyConvXdMixin, Conv3d): # type: ignore[misc]
  1115. r"""A :class:`torch.nn.Conv3d` module with lazy initialization of
  1116. the ``in_channels`` argument of the :class:`Conv3d` that is inferred from
  1117. the ``input.size(1)``.
  1118. The attributes that will be lazily initialized are `weight` and `bias`.
  1119. Check the :class:`torch.nn.modules.lazy.LazyModuleMixin` for further documentation
  1120. on lazy modules and their limitations.
  1121. Args:
  1122. out_channels (int): Number of channels produced by the convolution
  1123. kernel_size (int or tuple): Size of the convolving kernel
  1124. stride (int or tuple, optional): Stride of the convolution. Default: 1
  1125. padding (int or tuple, optional): Zero-padding added to both sides of
  1126. the input. Default: 0
  1127. padding_mode (str, optional): ``'zeros'``, ``'reflect'``,
  1128. ``'replicate'`` or ``'circular'``. Default: ``'zeros'``
  1129. dilation (int or tuple, optional): Spacing between kernel
  1130. elements. Default: 1
  1131. groups (int, optional): Number of blocked connections from input
  1132. channels to output channels. Default: 1
  1133. bias (bool, optional): If ``True``, adds a learnable bias to the
  1134. output. Default: ``True``
  1135. .. seealso:: :class:`torch.nn.Conv3d` and :class:`torch.nn.modules.lazy.LazyModuleMixin`
  1136. """
  1137. # super class define this variable as None. "type: ignore[..] is required
  1138. # since we are redefining the variable.
  1139. cls_to_become = Conv3d # type: ignore[assignment]
  1140. def __init__(
  1141. self,
  1142. out_channels: int,
  1143. kernel_size: _size_3_t,
  1144. stride: _size_3_t = 1,
  1145. padding: _size_3_t = 0,
  1146. dilation: _size_3_t = 1,
  1147. groups: int = 1,
  1148. bias: bool = True,
  1149. padding_mode: str = 'zeros',
  1150. device=None,
  1151. dtype=None
  1152. ) -> None:
  1153. factory_kwargs = {'device': device, 'dtype': dtype}
  1154. super().__init__(
  1155. 0,
  1156. 0,
  1157. kernel_size,
  1158. stride,
  1159. padding,
  1160. dilation,
  1161. groups,
  1162. # bias is hardcoded to False to avoid creating tensor
  1163. # that will soon be overwritten.
  1164. False,
  1165. padding_mode,
  1166. **factory_kwargs
  1167. )
  1168. self.weight = UninitializedParameter(**factory_kwargs)
  1169. self.out_channels = out_channels
  1170. if bias:
  1171. self.bias = UninitializedParameter(**factory_kwargs)
  1172. def _get_num_spatial_dims(self) -> int:
  1173. return 3
  1174. # LazyConvTranspose1d defines weight as a Tensor but derived class defines it as UnitializeParameter
  1175. class LazyConvTranspose1d(_LazyConvXdMixin, ConvTranspose1d): # type: ignore[misc]
  1176. r"""A :class:`torch.nn.ConvTranspose1d` module with lazy initialization of
  1177. the ``in_channels`` argument of the :class:`ConvTranspose1d` that is inferred from
  1178. the ``input.size(1)``.
  1179. The attributes that will be lazily initialized are `weight` and `bias`.
  1180. Check the :class:`torch.nn.modules.lazy.LazyModuleMixin` for further documentation
  1181. on lazy modules and their limitations.
  1182. Args:
  1183. out_channels (int): Number of channels produced by the convolution
  1184. kernel_size (int or tuple): Size of the convolving kernel
  1185. stride (int or tuple, optional): Stride of the convolution. Default: 1
  1186. padding (int or tuple, optional): ``dilation * (kernel_size - 1) - padding`` zero-padding
  1187. will be added to both sides of the input. Default: 0
  1188. output_padding (int or tuple, optional): Additional size added to one side
  1189. of the output shape. Default: 0
  1190. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
  1191. bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
  1192. dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
  1193. .. seealso:: :class:`torch.nn.ConvTranspose1d` and :class:`torch.nn.modules.lazy.LazyModuleMixin`
  1194. """
  1195. # super class define this variable as None. "type: ignore[..] is required
  1196. # since we are redefining the variable.
  1197. cls_to_become = ConvTranspose1d # type: ignore[assignment]
  1198. def __init__(
  1199. self,
  1200. out_channels: int,
  1201. kernel_size: _size_1_t,
  1202. stride: _size_1_t = 1,
  1203. padding: _size_1_t = 0,
  1204. output_padding: _size_1_t = 0,
  1205. groups: int = 1,
  1206. bias: bool = True,
  1207. dilation: _size_1_t = 1,
  1208. padding_mode: str = 'zeros',
  1209. device=None,
  1210. dtype=None
  1211. ) -> None:
  1212. factory_kwargs = {'device': device, 'dtype': dtype}
  1213. super().__init__(
  1214. 0,
  1215. 0,
  1216. kernel_size,
  1217. stride,
  1218. padding,
  1219. output_padding,
  1220. groups,
  1221. # bias is hardcoded to False to avoid creating tensor
  1222. # that will soon be overwritten.
  1223. False,
  1224. dilation,
  1225. padding_mode,
  1226. **factory_kwargs
  1227. )
  1228. self.weight = UninitializedParameter(**factory_kwargs)
  1229. self.out_channels = out_channels
  1230. if bias:
  1231. self.bias = UninitializedParameter(**factory_kwargs)
  1232. def _get_num_spatial_dims(self) -> int:
  1233. return 1
  1234. # LazyConvTranspose2d defines weight as a Tensor but derived class defines it as UnitializeParameter
  1235. class LazyConvTranspose2d(_LazyConvXdMixin, ConvTranspose2d): # type: ignore[misc]
  1236. r"""A :class:`torch.nn.ConvTranspose2d` module with lazy initialization of
  1237. the ``in_channels`` argument of the :class:`ConvTranspose2d` that is inferred from
  1238. the ``input.size(1)``.
  1239. The attributes that will be lazily initialized are `weight` and `bias`.
  1240. Check the :class:`torch.nn.modules.lazy.LazyModuleMixin` for further documentation
  1241. on lazy modules and their limitations.
  1242. Args:
  1243. out_channels (int): Number of channels produced by the convolution
  1244. kernel_size (int or tuple): Size of the convolving kernel
  1245. stride (int or tuple, optional): Stride of the convolution. Default: 1
  1246. padding (int or tuple, optional): ``dilation * (kernel_size - 1) - padding`` zero-padding
  1247. will be added to both sides of each dimension in the input. Default: 0
  1248. output_padding (int or tuple, optional): Additional size added to one side
  1249. of each dimension in the output shape. Default: 0
  1250. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
  1251. bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
  1252. dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
  1253. .. seealso:: :class:`torch.nn.ConvTranspose2d` and :class:`torch.nn.modules.lazy.LazyModuleMixin`
  1254. """
  1255. # super class define this variable as None. "type: ignore[..] is required
  1256. # since we are redefining the variable.
  1257. cls_to_become = ConvTranspose2d # type: ignore[assignment]
  1258. def __init__(
  1259. self,
  1260. out_channels: int,
  1261. kernel_size: _size_2_t,
  1262. stride: _size_2_t = 1,
  1263. padding: _size_2_t = 0,
  1264. output_padding: _size_2_t = 0,
  1265. groups: int = 1,
  1266. bias: bool = True,
  1267. dilation: int = 1,
  1268. padding_mode: str = 'zeros',
  1269. device=None,
  1270. dtype=None
  1271. ) -> None:
  1272. factory_kwargs = {'device': device, 'dtype': dtype}
  1273. super().__init__(
  1274. 0,
  1275. 0,
  1276. kernel_size,
  1277. stride,
  1278. padding,
  1279. output_padding,
  1280. groups,
  1281. # bias is hardcoded to False to avoid creating tensor
  1282. # that will soon be overwritten.
  1283. False,
  1284. dilation,
  1285. padding_mode,
  1286. **factory_kwargs
  1287. )
  1288. self.weight = UninitializedParameter(**factory_kwargs)
  1289. self.out_channels = out_channels
  1290. if bias:
  1291. self.bias = UninitializedParameter(**factory_kwargs)
  1292. def _get_num_spatial_dims(self) -> int:
  1293. return 2
  1294. # LazyConvTranspose3d defines weight as a Tensor but derived class defines it as UnitializeParameter
  1295. class LazyConvTranspose3d(_LazyConvXdMixin, ConvTranspose3d): # type: ignore[misc]
  1296. r"""A :class:`torch.nn.ConvTranspose3d` module with lazy initialization of
  1297. the ``in_channels`` argument of the :class:`ConvTranspose3d` that is inferred from
  1298. the ``input.size(1)``.
  1299. The attributes that will be lazily initialized are `weight` and `bias`.
  1300. Check the :class:`torch.nn.modules.lazy.LazyModuleMixin` for further documentation
  1301. on lazy modules and their limitations.
  1302. Args:
  1303. out_channels (int): Number of channels produced by the convolution
  1304. kernel_size (int or tuple): Size of the convolving kernel
  1305. stride (int or tuple, optional): Stride of the convolution. Default: 1
  1306. padding (int or tuple, optional): ``dilation * (kernel_size - 1) - padding`` zero-padding
  1307. will be added to both sides of each dimension in the input. Default: 0
  1308. output_padding (int or tuple, optional): Additional size added to one side
  1309. of each dimension in the output shape. Default: 0
  1310. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
  1311. bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
  1312. dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
  1313. .. seealso:: :class:`torch.nn.ConvTranspose3d` and :class:`torch.nn.modules.lazy.LazyModuleMixin`
  1314. """
  1315. # super class define this variable as None. "type: ignore[..] is required
  1316. # since we are redefining the variable.
  1317. cls_to_become = ConvTranspose3d # type: ignore[assignment]
  1318. def __init__(
  1319. self,
  1320. out_channels: int,
  1321. kernel_size: _size_3_t,
  1322. stride: _size_3_t = 1,
  1323. padding: _size_3_t = 0,
  1324. output_padding: _size_3_t = 0,
  1325. groups: int = 1,
  1326. bias: bool = True,
  1327. dilation: _size_3_t = 1,
  1328. padding_mode: str = 'zeros',
  1329. device=None,
  1330. dtype=None
  1331. ) -> None:
  1332. factory_kwargs = {'device': device, 'dtype': dtype}
  1333. super().__init__(
  1334. 0,
  1335. 0,
  1336. kernel_size,
  1337. stride,
  1338. padding,
  1339. output_padding,
  1340. groups,
  1341. # bias is hardcoded to False to avoid creating tensor
  1342. # that will soon be overwritten.
  1343. False,
  1344. dilation,
  1345. padding_mode,
  1346. **factory_kwargs
  1347. )
  1348. self.weight = UninitializedParameter(**factory_kwargs)
  1349. self.out_channels = out_channels
  1350. if bias:
  1351. self.bias = UninitializedParameter(**factory_kwargs)
  1352. def _get_num_spatial_dims(self) -> int:
  1353. return 3