Implement the time embedding module used in U-Net architectures for diffusion models. In denoising diffusion probabilistic models (DDPMs), the U-Net must be conditioned on the current noise level, which is represented by a discrete timestep. The time embedding transforms scalar timestep values into rich vector representations that the network can use.
The time embedding consists of two stages:
Stage 1 - Sinusoidal Embedding: Convert each scalar timestep into a fixed-size vector using sinusoidal functions with geometrically spaced frequencies. Given an embedding dimension embed_dim (must be even), compute half_dim = embed_dim // 2 frequency components. The frequencies are computed as:
freq_i = exp(-ln(max_period) * i / half_dim) for i = 0, 1, ..., half_dim - 1
For each timestep t, compute args = t * freq_i, then concatenate [sin(args), cos(args)] to form a vector of size embed_dim.
Stage 2 - MLP Projection: Pass the sinusoidal embedding through a two-layer MLP with SiLU activation (also known as Swish) between the layers. SiLU is defined as silu(x) = x * sigmoid(x) where sigmoid(x) = 1 / (1 + exp(-x)).
The MLP consists of: Linear(W1, b1) -> SiLU -> Linear(W2, b2)
Your function should accept a batch of timesteps and return the corresponding batch of time embeddings. Use only NumPy.