(
self,
batch: dict,
device: torch.device,
)
| 66 | self.interpolate = False |
| 67 | |
| 68 | def forward( |
| 69 | self, |
| 70 | batch: dict, |
| 71 | device: torch.device, |
| 72 | ) -> Dict[str, Optional[torch.Tensor]]: |
| 73 | |
| 74 | audio_token = batch['acoustic_token'].to(device) |
| 75 | audio_token_len = batch['acoustic_token_len'].to(device) |
| 76 | audio_token = audio_token.view(audio_token.size(0),-1,self.num_codebooks) |
| 77 | if "semantic_token" not in batch: |
| 78 | token = audio_token[:,:,0] |
| 79 | token_len = (audio_token_len/self.num_codebooks).long() |
| 80 | |
| 81 | else: |
| 82 | token = batch['semantic_token'].to(device) |
| 83 | token_len = batch['semantic_token_len'].to(device) |
| 84 | |
| 85 | with torch.no_grad(): |
| 86 | feat = self.quantizer.embed(audio_token) |
| 87 | feat_len = (audio_token_len/self.num_codebooks).long() |
| 88 | |
| 89 | token = self.input_embedding(token) |
| 90 | h, h_lengths = self.encoder(token, token_len) |
| 91 | h, h_lengths = self.length_regulator(h, feat_len) |
| 92 | |
| 93 | # get conditions |
| 94 | if self.cond: |
| 95 | conds = torch.zeros(feat.shape, device=token.device) |
| 96 | for i, j in enumerate(feat_len): |
| 97 | if random.random() < 0.5: |
| 98 | continue |
| 99 | index = random.randint(0, int(0.3 * j)) |
| 100 | conds[i, :index] = feat[i, :index] |
| 101 | conds = conds.transpose(1, 2) |
| 102 | else: |
| 103 | conds = None |
| 104 | |
| 105 | mask = (~make_pad_mask(feat_len)).to(h) |
| 106 | |
| 107 | loss, _ = self.decoder.compute_loss( |
| 108 | feat, |
| 109 | mask.unsqueeze(1), |
| 110 | h.transpose(1, 2).contiguous(), |
| 111 | None, |
| 112 | cond=conds |
| 113 | ) |
| 114 | |
| 115 | return {'loss': loss} |
| 116 | |
| 117 | @torch.inference_mode() |
| 118 | def inference(self, |
nothing calls this directly
no test coverage detected