Fix quantizer with balancing_heuristic

This commit is contained in:
James Betker 2021-09-29 14:46:05 -06:00
parent 4914c526dc
commit f84ccbdfb2

View File

@ -48,7 +48,7 @@ class Quantize(nn.Module):
self.register_buffer("embed_avg", embed.clone()) self.register_buffer("embed_avg", embed.clone())
def forward(self, input): def forward(self, input):
if self.codes_full: if self.balancing_heuristic and self.codes_full:
h = torch.histc(self.codes, bins=self.n_embed, min=0, max=self.n_embed) / len(self.codes) h = torch.histc(self.codes, bins=self.n_embed, min=0, max=self.n_embed) / len(self.codes)
mask = torch.logical_or(h > .9, h < .01).unsqueeze(1) mask = torch.logical_or(h > .9, h < .01).unsqueeze(1)
ep = self.embed.permute(1,0) ep = self.embed.permute(1,0)
@ -73,13 +73,14 @@ class Quantize(nn.Module):
embed_ind = embed_ind.view(*input.shape[:-1]) embed_ind = embed_ind.view(*input.shape[:-1])
quantize = self.embed_code(embed_ind) quantize = self.embed_code(embed_ind)
if self.codes is None: if self.balancing_heuristic:
self.codes = embed_ind.flatten() if self.codes is None:
else: self.codes = embed_ind.flatten()
self.codes = torch.cat([self.codes, embed_ind.flatten()]) else:
if len(self.codes) > self.max_codes: self.codes = torch.cat([self.codes, embed_ind.flatten()])
self.codes = self.codes[-self.max_codes:] if len(self.codes) > self.max_codes:
self.codes_full = True self.codes = self.codes[-self.max_codes:]
self.codes_full = True
if self.training: if self.training:
embed_onehot_sum = embed_onehot.sum(0) embed_onehot_sum = embed_onehot.sum(0)