File size: 2,225 Bytes
e12111a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Training state and checkpoints.

https://github.com/google/flax/blob/main/examples/imagenet/train.py
"""
from __future__ import annotations

from typing import Callable

import chex
import flax.linen as nn
import jax
import jax.numpy as jnp
from absl import logging
from flax.training import dynamic_scale as dynamic_scale_lib
from omegaconf import DictConfig

from imgx.train_state import TrainState as BaseTrainState
from imgx.train_state import init_optimizer


class TrainState(BaseTrainState):
    """Train state.

    If using nn.BatchNorm, batch_stats needs to be tracked.
    https://flax.readthedocs.io/en/latest/guides/batch_norm.html
    https://github.com/google/flax/blob/main/examples/imagenet/train.py
    """

    loss_count_hist: jnp.ndarray  # mutable
    loss_sq_hist: jnp.ndarray  # mutable


def create_train_state(
    key: jax.Array,
    batch: dict[str, jnp.ndarray],
    model: nn.Module,
    config: DictConfig,
    initialized: Callable[[jax.Array, chex.ArrayTree, nn.Module], chex.ArrayTree],
) -> TrainState:
    """Create initial training state.

    Args:
        key: random key.
        batch: batch data for determining input shapes.
        model: model.
        config: entire configuration.
        initialized: function to get initialized model parameters.

    Returns:
        initial training state.
    """
    dynamic_scale = None
    platform = jax.local_devices()[0].platform
    if config.half_precision and platform == "gpu":
        dynamic_scale = dynamic_scale_lib.DynamicScale()

    params = initialized(key, batch, model)

    # count params
    params_count = sum(x.size for x in jax.tree_util.tree_leaves(params))
    logging.info(f"The model has {params_count:,} parameters.")

    # diffusion related
    num_timesteps = config.task.diffusion.num_timesteps
    loss_count_hist = jnp.zeros((num_timesteps,), dtype=jnp.int32)
    loss_sq_hist = jnp.zeros((num_timesteps,), dtype=jnp.float32)

    tx = init_optimizer(config=config)
    state = TrainState.create(
        apply_fn=model.apply,
        params=params,
        tx=tx,
        dynamic_scale=dynamic_scale,
        loss_count_hist=loss_count_hist,
        loss_sq_hist=loss_sq_hist,
    )
    return state