MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / Downsample2D

Class Downsample2D

src/diffusers/models/downsampling.py:70–154  ·  view source on GitHub ↗

A 2D downsampling layer with an optional convolution. Parameters: channels (`int`): number of channels in the inputs and outputs. use_conv (`bool`, default `False`): option to use a convolution. out_channels (`int`, optional): number o

Source from the content-addressed store, hash-verified

68
69
70class Downsample2D(nn.Module):
71 """A 2D downsampling layer with an optional convolution.
72
73 Parameters:
74 channels (`int`):
75 number of channels in the inputs and outputs.
76 use_conv (`bool`, default `False`):
77 option to use a convolution.
78 out_channels (`int`, optional):
79 number of output channels. Defaults to `channels`.
80 padding (`int`, default `1`):
81 padding for the convolution.
82 name (`str`, default `conv`):
83 name of the downsampling 2D layer.
84 """
85
86 def __init__(
87 self,
88 channels: int,
89 use_conv: bool = False,
90 out_channels: Optional[int] = None,
91 padding: int = 1,
92 name: str = "conv",
93 kernel_size=3,
94 norm_type=None,
95 eps=None,
96 elementwise_affine=None,
97 bias=True,
98 ):
99 super().__init__()
100 self.channels = channels
101 self.out_channels = out_channels or channels
102 self.use_conv = use_conv
103 self.padding = padding
104 stride = 2
105 self.name = name
106 conv_cls = nn.Conv2d if USE_PEFT_BACKEND else LoRACompatibleConv
107
108 if norm_type == "ln_norm":
109 self.norm = nn.LayerNorm(channels, eps, elementwise_affine)
110 elif norm_type == "rms_norm":
111 self.norm = RMSNorm(channels, eps, elementwise_affine)
112 elif norm_type is None:
113 self.norm = None
114 else:
115 raise ValueError(f"unknown norm_type: {norm_type}")
116
117 if use_conv:
118 conv = conv_cls(
119 self.channels, self.out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias
120 )
121 else:
122 assert self.channels == self.out_channels
123 conv = nn.AvgPool2d(kernel_size=stride, stride=stride)
124
125 # TODO(Suraj, Patrick) - clean up after weight dicts are correctly renamed
126 if name == "conv":
127 self.Conv2d_0 = conv

Callers 15

__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls