Conv2d.patch 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. --- /usr/local/lib/python3.5/dist-packages/torch/nn/modules/conv.py
  2. +++ /usr/local/lib/python3.5/dist-packages/torch/nn/modules/conv.py
  3. @@ -1,5 +1,5 @@
  4. class Conv2d(_ConvNd):
  5. - __doc__ = r"""Applies a 2D convolution over an input signal composed of several input
  6. + r"""Applies a 2D convolution over an input signal composed of several input
  7. planes.
  8. In the simplest case, the output value of the layer with input size
  9. @@ -15,21 +15,29 @@
  10. :math:`N` is a batch size, :math:`C` denotes a number of channels,
  11. :math:`H` is a height of input planes in pixels, and :math:`W` is
  12. width in pixels.
  13. - """ + r"""
  14. -
  15. - This module supports :ref:`TensorFloat32<tf32_on_ampere>`.
  16. * :attr:`stride` controls the stride for the cross-correlation, a single
  17. number or a tuple.
  18. - * :attr:`padding` controls the amount of implicit padding on both
  19. + * :attr:`padding` controls the amount of implicit zero-paddings on both
  20. sides for :attr:`padding` number of points for each dimension.
  21. * :attr:`dilation` controls the spacing between the kernel points; also
  22. known as the ¨¤ trous algorithm. It is harder to describe, but this `link`_
  23. has a nice visualization of what :attr:`dilation` does.
  24. - {groups_note}
  25. + * :attr:`groups` controls the connections between inputs and outputs.
  26. + :attr:`in_channels` and :attr:`out_channels` must both be divisible by
  27. + :attr:`groups`. For example,
  28. +
  29. + * At groups=1, all inputs are convolved to all outputs.
  30. + * At groups=2, the operation becomes equivalent to having two conv
  31. + layers side by side, each seeing half the input channels,
  32. + and producing half the output channels, and both subsequently
  33. + concatenated.
  34. + * At groups= :attr:`in_channels`, each input channel is convolved with
  35. + its own set of filters, of size:
  36. + :math:`\left\lfloor\frac{C_\text{out}}{C_\text{in}}\right\rfloor`.
  37. The parameters :attr:`kernel_size`, :attr:`stride`, :attr:`padding`, :attr:`dilation` can either be:
  38. @@ -37,27 +45,34 @@
  39. - a ``tuple`` of two ints -- in which case, the first `int` is used for the height dimension,
  40. and the second `int` for the width dimension
  41. - Note:
  42. - {depthwise_separable_note}
  43. + .. note::
  44. - Note:
  45. - {cudnn_reproducibility_note}
  46. + Depending of the size of your kernel, several (of the last)
  47. + columns of the input might be lost, because it is a valid `cross-correlation`_,
  48. + and not a full `cross-correlation`_.
  49. + It is up to the user to add proper padding.
  50. +
  51. + .. note::
  52. +
  53. + When `groups == in_channels` and `out_channels == K * in_channels`,
  54. + where `K` is a positive integer, this operation is also termed in
  55. + literature as depthwise convolution.
  56. +
  57. + In other words, for an input of size :math:`(N, C_{in}, H_{in}, W_{in})`,
  58. + a depthwise convolution with a depthwise multiplier `K`, can be constructed by arguments
  59. + :math:`(in\_channels=C_{in}, out\_channels=C_{in} \times K, ..., groups=C_{in})`.
  60. +
  61. + .. include:: cudnn_deterministic.rst
  62. Args:
  63. in_channels (int): Number of channels in the input image
  64. out_channels (int): Number of channels produced by the convolution
  65. kernel_size (int or tuple): Size of the convolving kernel
  66. stride (int or tuple, optional): Stride of the convolution. Default: 1
  67. - padding (int or tuple, optional): Zero-padding added to both sides of
  68. - the input. Default: 0
  69. - padding_mode (string, optional): ``'zeros'``, ``'reflect'``,
  70. - ``'replicate'`` or ``'circular'``. Default: ``'zeros'``
  71. + padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0
  72. dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
  73. - groups (int, optional): Number of blocked connections from input
  74. - channels to output channels. Default: 1
  75. - bias (bool, optional): If ``True``, adds a learnable bias to the
  76. - output. Default: ``True``
  77. - """.format(**reproducibility_notes, **convolution_notes) + r"""
  78. + groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
  79. + bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
  80. Shape:
  81. - Input: :math:`(N, C_{in}, H_{in}, W_{in})`
  82. @@ -73,18 +88,16 @@
  83. Attributes:
  84. weight (Tensor): the learnable weights of the module of shape
  85. - :math:`(\text{out\_channels}, \frac{\text{in\_channels}}{\text{groups}},`
  86. - :math:`\text{kernel\_size[0]}, \text{kernel\_size[1]})`.
  87. - The values of these weights are sampled from
  88. - :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  89. - :math:`k = \frac{groups}{C_\text{in} * \prod_{i=0}^{1}\text{kernel\_size}[i]}`
  90. - bias (Tensor): the learnable bias of the module of shape
  91. - (out_channels). If :attr:`bias` is ``True``,
  92. - then the values of these weights are
  93. - sampled from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  94. - :math:`k = \frac{groups}{C_\text{in} * \prod_{i=0}^{1}\text{kernel\_size}[i]}`
  95. + (out_channels, in_channels, kernel_size[0], kernel_size[1]).
  96. + The values of these weights are sampled from
  97. + :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  98. + :math:`k = \frac{1}{C_\text{in} * \prod_{i=0}^{1}\text{kernel\_size}[i]}`
  99. + bias (Tensor): the learnable bias of the module of shape (out_channels). If :attr:`bias` is ``True``,
  100. + then the values of these weights are
  101. + sampled from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
  102. + :math:`k = \frac{1}{C_\text{in} * \prod_{i=0}^{1}\text{kernel\_size}[i]}`
  103. - Examples:
  104. + Examples::
  105. >>> # With square kernels and equal stride
  106. >>> m = nn.Conv2d(16, 33, 3, stride=2)
  107. @@ -101,35 +114,18 @@
  108. .. _link:
  109. https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
  110. """
  111. + def __init__(self, in_channels, out_channels, kernel_size, stride=1,
  112. + padding=0, dilation=1, groups=1, bias=True):
  113. + kernel_size = _pair(kernel_size)
  114. + stride = _pair(stride)
  115. + padding = _pair(padding)
  116. + dilation = _pair(dilation)
  117. + super(Conv2d, self).__init__(
  118. + in_channels, out_channels, kernel_size, stride, padding, dilation,
  119. + False, _pair(0), groups, bias)
  120. - def __init__(
  121. - self,
  122. - in_channels: int,
  123. - out_channels: int,
  124. - kernel_size: _size_2_t,
  125. - stride: _size_2_t = 1,
  126. - padding: _size_2_t = 0,
  127. - dilation: _size_2_t = 1,
  128. - groups: int = 1,
  129. - bias: bool = True,
  130. - padding_mode: str = 'zeros' # TODO: refine this type
  131. - ):
  132. - kernel_size_ = _pair(kernel_size)
  133. - stride_ = _pair(stride)
  134. - padding_ = _pair(padding)
  135. - dilation_ = _pair(dilation)
  136. - super(Conv2d, self).__init__(
  137. - in_channels, out_channels, kernel_size_, stride_, padding_, dilation_,
  138. - False, _pair(0), groups, bias, padding_mode)
  139. -
  140. - def _conv_forward(self, input: Tensor, weight: Tensor, bias: Optional[Tensor]):
  141. - if self.padding_mode != 'zeros':
  142. - return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode),
  143. - weight, bias, self.stride,
  144. - _pair(0), self.dilation, self.groups)
  145. - return F.conv2d(input, weight, bias, self.stride,
  146. + @weak_script_method
  147. + def forward(self, input):
  148. + return F.conv2d(input, self.weight, self.bias, self.stride,
  149. self.padding, self.dilation, self.groups)
  150. - def forward(self, input: Tensor) -> Tensor:
  151. - return self._conv_forward(input, self.weight, self.bias)
  152. -