CoolFace
Apppublic

declare-lab/tango2

sourceHugging Faceupdated 2y agoView on Hugging Face
92likes
resnet_flax.py125 linesDownload Raw Back to models
1# Copyright 2023 The HuggingFace Team. All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14import flax.linen as nn15import jax16import jax.numpy as jnp17 18 19class FlaxUpsample2D(nn.Module):20    out_channels: int21    dtype: jnp.dtype = jnp.float3222 23    def setup(self):24        self.conv = nn.Conv(25            self.out_channels,26            kernel_size=(3, 3),27            strides=(1, 1),28            padding=((1, 1), (1, 1)),29            dtype=self.dtype,30        )31 32    def __call__(self, hidden_states):33        batch, height, width, channels = hidden_states.shape34        hidden_states = jax.image.resize(35            hidden_states,36            shape=(batch, height * 2, width * 2, channels),37            method="nearest",38        )39        hidden_states = self.conv(hidden_states)40        return hidden_states41 42 43class FlaxDownsample2D(nn.Module):44    out_channels: int45    dtype: jnp.dtype = jnp.float3246 47    def setup(self):48        self.conv = nn.Conv(49            self.out_channels,50            kernel_size=(3, 3),51            strides=(2, 2),52            padding=((1, 1), (1, 1)),  # padding="VALID",53            dtype=self.dtype,54        )55 56    def __call__(self, hidden_states):57        # pad = ((0, 0), (0, 1), (0, 1), (0, 0))  # pad height and width dim58        # hidden_states = jnp.pad(hidden_states, pad_width=pad)59        hidden_states = self.conv(hidden_states)60        return hidden_states61 62 63class FlaxResnetBlock2D(nn.Module):64    in_channels: int65    out_channels: int = None66    dropout_prob: float = 0.067    use_nin_shortcut: bool = None68    dtype: jnp.dtype = jnp.float3269 70    def setup(self):71        out_channels = self.in_channels if self.out_channels is None else self.out_channels72 73        self.norm1 = nn.GroupNorm(num_groups=32, epsilon=1e-5)74        self.conv1 = nn.Conv(75            out_channels,76            kernel_size=(3, 3),77            strides=(1, 1),78            padding=((1, 1), (1, 1)),79            dtype=self.dtype,80        )81 82        self.time_emb_proj = nn.Dense(out_channels, dtype=self.dtype)83 84        self.norm2 = nn.GroupNorm(num_groups=32, epsilon=1e-5)85        self.dropout = nn.Dropout(self.dropout_prob)86        self.conv2 = nn.Conv(87            out_channels,88            kernel_size=(3, 3),89            strides=(1, 1),90            padding=((1, 1), (1, 1)),91            dtype=self.dtype,92        )93 94        use_nin_shortcut = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut95 96        self.conv_shortcut = None97        if use_nin_shortcut:98            self.conv_shortcut = nn.Conv(99                out_channels,100                kernel_size=(1, 1),101                strides=(1, 1),102                padding="VALID",103                dtype=self.dtype,104            )105 106    def __call__(self, hidden_states, temb, deterministic=True):107        residual = hidden_states108        hidden_states = self.norm1(hidden_states)109        hidden_states = nn.swish(hidden_states)110        hidden_states = self.conv1(hidden_states)111 112        temb = self.time_emb_proj(nn.swish(temb))113        temb = jnp.expand_dims(jnp.expand_dims(temb, 1), 1)114        hidden_states = hidden_states + temb115 116        hidden_states = self.norm2(hidden_states)117        hidden_states = nn.swish(hidden_states)118        hidden_states = self.dropout(hidden_states, deterministic)119        hidden_states = self.conv2(hidden_states)120 121        if self.conv_shortcut is not None:122            residual = self.conv_shortcut(residual)123 124        return hidden_states + residual125