File size: 3,104 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""Basic functions and modules."""
from __future__ import annotations

from typing import Callable

import flax.linen as nn
import jax
import jax.numpy as jnp


class Identity(nn.Module):
    """Identity module."""

    dtype: jnp.dtype = jnp.float32

    @nn.compact
    def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
        """Forward pass.

        Args:
            x: input.

        Returns:
            input.
        """
        return x


class InstanceNorm(nn.Module):
    """Instance norm.

    The norm is calculated on axes excluding batch and features.
    """

    dtype: jnp.dtype = jnp.float32

    @nn.compact
    def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
        """Forward pass.

        Args:
            x: input with batch axis, (batch, ..., channel).

        Returns:
            Normalised input.
        """
        reduction_axes = tuple(range(x.ndim)[slice(1, -1)])
        return nn.LayerNorm(
            reduction_axes=reduction_axes,
        )(x)


def sinusoidal_positional_embedding(
    x: jnp.ndarray,
    dim: int,
    max_period: int = 10000,
    dtype: jnp.dtype = jnp.float32,
) -> jnp.ndarray:
    """Create sinusoidal timestep embeddings.

    Half defined by sin, half by cos.
    For position x, the embeddings are (for i = 0,...,half_dim-1)
        sin(x / (max_period ** (i/half_dim)))
        cos(x / (max_period ** (i/half_dim)))

    Args:
        x: (..., ), with values in [0, 1].
        dim: embedding dimension, assume to be evenly divided by two.
        max_period: controls the minimum frequency of the embeddings.
        dtype: dtype of the embeddings.

    Returns:
        Embedding of size (..., dim).
    """
    ndim_x = len(x.shape)
    if dim % 2 != 0:
        raise ValueError(f"dim must be evenly divided by two, got {dim}.")
    half_dim = dim // 2
    # (half_dim,)
    freq = jnp.arange(0, half_dim, dtype=dtype)
    freq = jnp.exp(-jnp.log(max_period) * freq / half_dim)
    # (..., half_dim)
    freq = jnp.expand_dims(freq, axis=tuple(range(ndim_x)))
    args = x[..., None] * max_period * freq
    # (..., dim)
    return jnp.concatenate([jnp.cos(args), jnp.sin(args)], axis=-1)


class MLP(nn.Module):
    """Two-layer MLP."""

    emb_size: int
    output_size: int
    activation: Callable[[jnp.ndarray], jnp.ndarray] = jax.nn.gelu
    kernel_init: Callable[
        [jax.Array, jnp.shape, jnp.dtype], jnp.ndarray
    ] = nn.initializers.lecun_normal()
    remat: bool = True
    dtype: jnp.dtype = jnp.float32

    @nn.compact
    def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
        """Forward pass.

        Args:
            x: shape (..., in_size)

        Returns:
            shape (..., out_size)
        """
        dense_cls = nn.remat(nn.Dense) if self.remat else nn.Dense
        x = dense_cls(
            self.emb_size,
            kernel_init=self.kernel_init,
            dtype=self.dtype,
        )(x)
        x = self.activation(x)
        x = dense_cls(
            self.output_size,
            kernel_init=self.kernel_init,
            dtype=self.dtype,
        )(x)
        return x