File size: 3,001 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
"""Test basic functions for model."""


from functools import partial

import chex
import jax
import jax.numpy as jnp
from absl.testing import parameterized
from chex._src import fake

from imgx.model.basic import MLP, InstanceNorm, sinusoidal_positional_embedding


# Set `FLAGS.chex_n_cpu_devices` CPU devices for all tests.
def setUpModule() -> None:  # pylint: disable=invalid-name
    """Fake multi-devices."""
    fake.set_n_cpu_devices(2)


class TestInstanceNorm(chex.TestCase):
    """Test the function sinusoidal_positional_embedding."""

    @chex.all_variants()
    @parameterized.named_parameters(
        (
            "1d",
            (2,),
        ),
        (
            "2d",
            (2, 3),
        ),
    )
    def test_shapes(
        self,
        in_shape: tuple[int, ...],
    ) -> None:
        """Test output shapes under different device condition.

        Args:
            in_shape: input shape.
        """
        rng = {"params": jax.random.PRNGKey(0)}
        norm = InstanceNorm()
        x = jax.random.uniform(
            jax.random.PRNGKey(0),
            shape=in_shape,
        )
        out, _ = self.variant(norm.init_with_output)(rng, x)
        chex.assert_shape(out, in_shape)


class TestSinusoidalPositionalEmbedding(chex.TestCase):
    """Test the function sinusoidal_positional_embedding."""

    @chex.all_variants()
    @parameterized.named_parameters(
        ("1d case 1", (2,), 4, 5),
        (
            "1d case 2",
            (2,),
            8,
            10000,
        ),
        (
            "2d",
            (2, 3),
            8,
            10000,
        ),
    )
    def test_shapes(self, in_shape: tuple[int, ...], dim: int, max_period: int) -> None:
        """Test output shapes under different device condition.

        Args:
            in_shape: input shape.
            dim: embedding dimension, assume to be evenly divided by two.
            max_period: controls the minimum frequency of the embeddings.
        """
        rng = jax.random.PRNGKey(0)
        x = jax.random.uniform(
            rng,
            shape=in_shape,
        )
        out = self.variant(
            partial(sinusoidal_positional_embedding, dim=dim, max_period=max_period)
        )(x)
        chex.assert_shape(out, (*in_shape, dim))


class TestMLP(chex.TestCase):
    """Test MLP."""

    emb_size: int = 4
    output_size: int = 8

    @chex.all_variants()
    @parameterized.product(
        in_shape=[(3, 4, 5), (3, 4), (3,)],
        remat=[True, False],
    )
    def test_shapes(
        self,
        in_shape: tuple[int, ...],
        remat: bool,
    ) -> None:
        """Test output shapes."""
        rng = {"params": jax.random.PRNGKey(0)}
        mlp = MLP(
            emb_size=self.emb_size,
            output_size=self.output_size,
            remat=remat,
        )
        out, _ = self.variant(mlp.init_with_output)(rng, jnp.ones(in_shape))
        chex.assert_shape(out, (*in_shape[:-1], self.output_size))