Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use nnx.Sequential over ModuleList #5

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions jflux/autoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,13 @@ def __init__(
curr_res = resolution
in_ch_mult = (1,) + tuple(ch_mult)
self.in_ch_mult = in_ch_mult
# FIXME: Use nnx.Sequential instead
self.down = nnx.ModuleList()
block_in = self.ch
for i_level in range(self.num_resolutions):
# FIXME: Use nnx.Sequential instead
block = nnx.ModuleList()
blocks = []
block_in = ch * in_ch_mult[i_level]
block_out = ch * ch_mult[i_level]
for _ in range(self.num_res_blocks):
block.append(
blocks.append(
ResnetBlock(
in_channels=block_in,
out_channels=block_out,
Expand All @@ -359,17 +356,17 @@ def __init__(
)
)
block_in = block_out
down = nnx.Module()
down.block = block
if i_level != self.num_resolutions - 1:
down.downsample = Downsample(
in_channels=block_in,
rngs=rngs,
dtype=self.dtype,
param_dtype=self.param_dtype,
blocks.append(
Downsample(
in_channels=block_in,
rngs=rngs,
dtype=self.dtype,
param_dtype=self.param_dtype,
)
)
curr_res = curr_res // 2
self.down.append(down)
self.down = nnx.Sequential(*blocks)

# middle
self.middle = nnx.Sequential(
Expand Down Expand Up @@ -520,14 +517,12 @@ def __init__(
)

# upsampling
# FIXME: Use nnx.Sequential instead
self.up = nnx.ModuleList()
for i_level in reversed(range(self.num_resolutions)):
# FIXME: Use nnx.Sequential instead
block = nnx.ModuleList()
blocks = []
block_out = ch * ch_mult[i_level]
for _ in range(self.num_res_blocks + 1):
block.append(
blocks.append(
ResnetBlock(
in_channels=block_in,
out_channels=block_out,
Expand All @@ -537,17 +532,20 @@ def __init__(
)
)
block_in = block_out
up = nnx.Module()
up.block = block

upsample_module = [*blocks]
if i_level != 0:
up.upsample = Upsample(
in_channels=block_in,
rngs=rngs,
dtype=self.dtype,
param_dtype=self.param_dtype,
upsample_module.append(
Upsample(
in_channels=block_in,
rngs=rngs,
dtype=self.dtype,
param_dtype=self.param_dtype,
)
)
curr_res = curr_res * 2
self.up.insert(0, up) # prepend to get consistent order

self.up = nnx.Sequential(*upsample_module)

# end
self.norm_out = nnx.GroupNorm(
Expand Down