Skip to content

Commit

Permalink
Code update
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 600584870
  • Loading branch information
Forgotten authored and The swirl_dynamics Authors committed Jan 22, 2024
1 parent 9db72b1 commit 9c958e7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
4 changes: 1 addition & 3 deletions swirl_dynamics/projects/debiasing/rectified_flow/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from ml_collections import config_flags
import optax
from orbax import checkpoint
from swirl_dynamics.lib.diffusion import unets
from swirl_dynamics.projects.debiasing.rectified_flow import data_utils
from swirl_dynamics.projects.debiasing.rectified_flow import models
from swirl_dynamics.projects.debiasing.rectified_flow import trainers
Expand Down Expand Up @@ -106,7 +105,7 @@ def main(argv):
)

# Setting up the neural network for the flow model.
flow_model = unets.PreconditionedDenoiser(
flow_model = models.RescaledUnet(
out_channels=config.out_channels,
num_channels=config.num_channels,
downsample_ratio=config.downsample_ratio,
Expand All @@ -117,7 +116,6 @@ def main(argv):
use_attention=config.use_attention,
use_position_encoding=config.use_position_encoding,
num_heads=config.num_heads,
sigma_data=1.0, # standard deviation of the entire dataset.
)

model = models.ReFlowModel(
Expand Down
40 changes: 40 additions & 0 deletions swirl_dynamics/projects/debiasing/rectified_flow/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import flax.linen as nn
import jax
import jax.numpy as jnp
from swirl_dynamics.lib.diffusion import unets
from swirl_dynamics.templates import models
from swirl_dynamics.templates import trainers

Expand Down Expand Up @@ -218,3 +219,42 @@ def _flow(
)

return _flow


class RescaledUnet(unets.UNet):
"""Rescaled flow model.
Attributes:
time_rescale: Factor for rescaling the time, which normally is in [0, 1] and
the input to the UNet which is a noise level, which has a much wider range
of values.
"""

time_rescale: float = 1000.0

@nn.compact
def __call__(
self,
x: Array,
sigma: Array,
cond: dict[str, Array] | None = None,
*,
is_training: bool,
) -> Array:
"""Runs rescaled Unet with noise input."""
if sigma.ndim < 1:
sigma = jnp.broadcast_to(sigma, (x.shape[0],))

if sigma.ndim != 1 or x.shape[0] != sigma.shape[0]:
raise ValueError(
"sigma must be 1D and have the same leading (batch) dimension as x"
f" ({x.shape[0]})!"
)

time = sigma * self.time_rescale

f_x = super().__call__(
x, time, cond, is_training=is_training
)

return f_x

0 comments on commit 9c958e7

Please sign in to comment.