MCPcopy Index your code
hub / github.com/ml-explore/mlx-examples / EncoderDecoderBlock2D

Class EncoderDecoderBlock2D

stable_diffusion/stable_diffusion/vae.py:45–90  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43
44
45class EncoderDecoderBlock2D(nn.Module):
46 def __init__(
47 self,
48 in_channels: int,
49 out_channels: int,
50 num_layers: int = 1,
51 resnet_groups: int = 32,
52 add_downsample=True,
53 add_upsample=True,
54 ):
55 super().__init__()
56
57 # Add the resnet blocks
58 self.resnets = [
59 ResnetBlock2D(
60 in_channels=in_channels if i == 0 else out_channels,
61 out_channels=out_channels,
62 groups=resnet_groups,
63 )
64 for i in range(num_layers)
65 ]
66
67 # Add an optional downsampling layer
68 if add_downsample:
69 self.downsample = nn.Conv2d(
70 out_channels, out_channels, kernel_size=3, stride=2, padding=0
71 )
72
73 # or upsampling layer
74 if add_upsample:
75 self.upsample = nn.Conv2d(
76 out_channels, out_channels, kernel_size=3, stride=1, padding=1
77 )
78
79 def __call__(self, x):
80 for resnet in self.resnets:
81 x = resnet(x)
82
83 if "downsample" in self:
84 x = mx.pad(x, [(0, 0), (0, 1), (0, 1), (0, 0)])
85 x = self.downsample(x)
86
87 if "upsample" in self:
88 x = self.upsample(upsample_nearest(x))
89
90 return x
91
92
93class Encoder(nn.Module):

Callers 2

__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected