Rbaerk commited on
Commit
5b79575
·
verified ·
1 Parent(s): 71dfe4d

Match published source formatting

Browse files
Files changed (2) hide show
  1. encoder_blocks.py +19 -19
  2. quantizer.py +11 -11
encoder_blocks.py CHANGED
@@ -3,19 +3,19 @@
3
 
4
  Copyright (2024) Bytedance Ltd. and/or its affiliates
5
 
6
- Licensed under the Apache License, Version 2.0 (the "License");
7
- you may not use this file except in compliance with the License.
8
- You may obtain a copy of the License at
9
 
10
- http://www.apache.org/licenses/LICENSE-2.0
11
 
12
- Unless required by applicable law or agreed to in writing, software
13
- distributed under the License is distributed on an "AS IS" BASIS,
14
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- See the License for the specific language governing permissions and
16
- limitations under the License.
17
 
18
- Reference:
19
  https://github.com/mlfoundations/open_clip/blob/main/src/open_clip/transformer.py
20
  https://github.com/baofff/U-ViT/blob/main/libs/timm.py
21
  """
@@ -74,11 +74,11 @@ class TiTokEncoder(nn.Module):
74
  def __init__(self, config):
75
  super().__init__()
76
  self.config = config
77
- self.width_size = config.dataset.preprocessing.width_size
78
- self.height_size = config.dataset.preprocessing.height_size
79
  self.patch_size = config.model.vq_model.vit_enc_patch_size
80
- self.grid_size_w = self.width_size // self.patch_size
81
- self.grid_size_h = self.height_size // self.patch_size
82
  self.model_size = config.model.vq_model.vit_enc_model_size
83
  self.num_latent_tokens = config.model.vq_model.num_latent_tokens
84
  self.token_size = config.model.vq_model.token_size
@@ -103,11 +103,11 @@ class TiTokEncoder(nn.Module):
103
  "base": 12,
104
  "large": 16,
105
  }[self.model_size]
106
-
107
  self.patch_embed = nn.Conv2d(
108
  in_channels=3, out_channels=self.width,
109
  kernel_size=self.patch_size, stride=self.patch_size,padding = (4,2), bias=True)
110
-
111
  scale = self.width ** -0.5
112
  self.class_embedding = nn.Parameter(scale * torch.randn(1, self.width))
113
  self.positional_embedding = nn.Parameter(
@@ -132,7 +132,7 @@ class TiTokEncoder(nn.Module):
132
  # class embeddings and positional embeddings
133
  x = torch.cat([_expand_token(self.class_embedding, x.shape[0]).to(x.dtype), x], dim=1)
134
  x = x + self.positional_embedding.to(x.dtype) # shape = [*, grid ** 2 + 1, width]
135
-
136
 
137
  latent_tokens = _expand_token(latent_tokens, x.shape[0]).to(x.dtype)
138
  latent_tokens = latent_tokens + self.latent_token_positional_embedding.to(x.dtype)
@@ -149,7 +149,7 @@ class TiTokEncoder(nn.Module):
149
  x = torch.utils.checkpoint.checkpoint(create_custom_forward(self.transformer[i]), x,use_reentrant=False)
150
 
151
  x = x.permute(1, 0, 2) # LND -> NLD
152
-
153
  latent_tokens = x[:, 1+self.grid_size_h*self.grid_size_w:]
154
  latent_tokens = self.ln_post(latent_tokens)
155
  # fake 2D shape
@@ -173,7 +173,7 @@ class HW_encoder_2(nn.Module):
173
  # # self.conv4 = nn.Conv2d(in_channels , in_channels//4 , kernel_size=3, padding=1)
174
 
175
  # def pixel_shuffle(self, x, scale_factor=0.5):
176
- # n, c, h, w = x.size()
177
  # new_h = int(h * scale_factor)
178
  # new_w = int(w * scale_factor)
179
  # x = x.view(n, int(c / (scale_factor ** 2)), new_h, new_w)
 
3
 
4
  Copyright (2024) Bytedance Ltd. and/or its affiliates
5
 
6
+ Licensed under the Apache License, Version 2.0 (the "License");
7
+ you may not use this file except in compliance with the License.
8
+ You may obtain a copy of the License at
9
 
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
 
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
 
18
+ Reference:
19
  https://github.com/mlfoundations/open_clip/blob/main/src/open_clip/transformer.py
20
  https://github.com/baofff/U-ViT/blob/main/libs/timm.py
21
  """
 
74
  def __init__(self, config):
75
  super().__init__()
76
  self.config = config
77
+ self.width_size = config.dataset.preprocessing.width_size
78
+ self.height_size = config.dataset.preprocessing.height_size
79
  self.patch_size = config.model.vq_model.vit_enc_patch_size
80
+ self.grid_size_w = self.width_size // self.patch_size
81
+ self.grid_size_h = self.height_size // self.patch_size
82
  self.model_size = config.model.vq_model.vit_enc_model_size
83
  self.num_latent_tokens = config.model.vq_model.num_latent_tokens
84
  self.token_size = config.model.vq_model.token_size
 
103
  "base": 12,
104
  "large": 16,
105
  }[self.model_size]
106
+
107
  self.patch_embed = nn.Conv2d(
108
  in_channels=3, out_channels=self.width,
109
  kernel_size=self.patch_size, stride=self.patch_size,padding = (4,2), bias=True)
110
+
111
  scale = self.width ** -0.5
112
  self.class_embedding = nn.Parameter(scale * torch.randn(1, self.width))
113
  self.positional_embedding = nn.Parameter(
 
132
  # class embeddings and positional embeddings
133
  x = torch.cat([_expand_token(self.class_embedding, x.shape[0]).to(x.dtype), x], dim=1)
134
  x = x + self.positional_embedding.to(x.dtype) # shape = [*, grid ** 2 + 1, width]
135
+
136
 
137
  latent_tokens = _expand_token(latent_tokens, x.shape[0]).to(x.dtype)
138
  latent_tokens = latent_tokens + self.latent_token_positional_embedding.to(x.dtype)
 
149
  x = torch.utils.checkpoint.checkpoint(create_custom_forward(self.transformer[i]), x,use_reentrant=False)
150
 
151
  x = x.permute(1, 0, 2) # LND -> NLD
152
+
153
  latent_tokens = x[:, 1+self.grid_size_h*self.grid_size_w:]
154
  latent_tokens = self.ln_post(latent_tokens)
155
  # fake 2D shape
 
173
  # # self.conv4 = nn.Conv2d(in_channels , in_channels//4 , kernel_size=3, padding=1)
174
 
175
  # def pixel_shuffle(self, x, scale_factor=0.5):
176
+ # n, c, h, w = x.size()
177
  # new_h = int(h * scale_factor)
178
  # new_w = int(w * scale_factor)
179
  # x = x.view(n, int(c / (scale_factor ** 2)), new_h, new_w)
quantizer.py CHANGED
@@ -2,19 +2,19 @@
2
 
3
  Copyright (2024) Bytedance Ltd. and/or its affiliates
4
 
5
- Licensed under the Apache License, Version 2.0 (the "License");
6
- you may not use this file except in compliance with the License.
7
- You may obtain a copy of the License at
8
 
9
- http://www.apache.org/licenses/LICENSE-2.0
10
 
11
- Unless required by applicable law or agreed to in writing, software
12
- distributed under the License is distributed on an "AS IS" BASIS,
13
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- See the License for the specific language governing permissions and
15
  limitations under the License.
16
 
17
- Reference:
18
  https://github.com/CompVis/taming-transformers/blob/master/taming/modules/vqvae/quantize.py
19
  https://github.com/google-research/magvit/blob/main/videogvt/models/vqvae.py
20
  https://github.com/CompVis/latent-diffusion/blob/main/ldm/modules/distributions/distributions.py
@@ -92,7 +92,7 @@ class VectorQuantizer(torch.nn.Module):
92
  all_unnormed_z_flattened = gather(unnormed_z_flattened).detach()
93
  if all_d.shape[0] != all_unnormed_z_flattened.shape[0]:
94
  raise ValueError(
95
- "all_d and all_unnormed_z_flattened have different length" +
96
  f"{all_d.shape}, {all_unnormed_z_flattened.shape}")
97
  indices = torch.argmin(all_d, dim=0)
98
  random_feat = all_unnormed_z_flattened[indices]
@@ -128,7 +128,7 @@ class VectorQuantizer(torch.nn.Module):
128
  if self.use_l2_norm:
129
  z_quantized = torch.nn.functional.normalize(z_quantized, dim=-1)
130
  return z_quantized
131
-
132
 
133
  class DiagonalGaussianDistribution(object):
134
  @autocast(enabled=False)
 
2
 
3
  Copyright (2024) Bytedance Ltd. and/or its affiliates
4
 
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
 
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
 
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
  limitations under the License.
16
 
17
+ Reference:
18
  https://github.com/CompVis/taming-transformers/blob/master/taming/modules/vqvae/quantize.py
19
  https://github.com/google-research/magvit/blob/main/videogvt/models/vqvae.py
20
  https://github.com/CompVis/latent-diffusion/blob/main/ldm/modules/distributions/distributions.py
 
92
  all_unnormed_z_flattened = gather(unnormed_z_flattened).detach()
93
  if all_d.shape[0] != all_unnormed_z_flattened.shape[0]:
94
  raise ValueError(
95
+ "all_d and all_unnormed_z_flattened have different length" +
96
  f"{all_d.shape}, {all_unnormed_z_flattened.shape}")
97
  indices = torch.argmin(all_d, dim=0)
98
  random_feat = all_unnormed_z_flattened[indices]
 
128
  if self.use_l2_norm:
129
  z_quantized = torch.nn.functional.normalize(z_quantized, dim=-1)
130
  return z_quantized
131
+
132
 
133
  class DiagonalGaussianDistribution(object):
134
  @autocast(enabled=False)