Spaces:
Runtime error
Runtime error
Rex Cheng commited on
Commit ·
164c335
1
Parent(s): 627e0b8
speed up inference
Browse filesFiles changed (4)
47 unmodified lines4849505152535447 unmodified linessynchformer_ckpt=model.synchformer_ckpt,enable_conditions=True,mode=model.mode,bigvgan_vocoder_ckpt=model.bigvgan_16k_path)feature_utils = feature_utils.to(device, dtype).eval()return net, feature_utils, seq_cfg47 unmodified lines484950515253545547 unmodified linessynchformer_ckpt=model.synchformer_ckpt,enable_conditions=True,mode=model.mode,bigvgan_vocoder_ckpt=model.bigvgan_16k_path,need_vae_encoder=False)feature_utils = feature_utils.to(device, dtype).eval()return net, feature_utils, seq_cfg
75 unmodified lines76777879808182838485868788899091929394959697989910010110210310480 unmodified lines18518618718818919019119219319419519619719875 unmodified lines}def generate(clip_video: Optional[torch.Tensor],sync_video: Optional[torch.Tensor],text: Optional[list[str]],*,negative_text: Optional[list[str]] = None,feature_utils: FeaturesUtils,net: MMAudio,fm: FlowMatching,rng: torch.Generator,cfg_strength: float):device = feature_utils.devicedtype = feature_utils.dtypebs = len(text)if clip_video is not None:clip_video = clip_video.to(device, dtype, non_blocking=True)clip_features = feature_utils.encode_video_with_clip(clip_video, batch_size=bs)else:clip_features = net.get_empty_clip_sequence(bs)if sync_video is not None:sync_video = sync_video.to(device, dtype, non_blocking=True)sync_features = feature_utils.encode_video_with_sync(sync_video, batch_size=bs)else:sync_features = net.get_empty_sync_sequence(bs)80 unmodified linesdata_chunk = reader.pop_chunks()clip_chunk = data_chunk[0]sync_chunk = data_chunk[1]print('clip', clip_chunk.shape, clip_chunk.dtype, clip_chunk.max())print('sync', sync_chunk.shape, sync_chunk.dtype, sync_chunk.max())assert clip_chunk is not Noneassert sync_chunk is not Nonefor i in range(reader.num_out_streams):print(reader.get_out_stream_info(i))clip_frames = clip_transform(clip_chunk)sync_frames = sync_transform(sync_chunk)75 unmodified lines76777879808182838485868788899091929394959697989910010110210310410510610710810911011111280 unmodified lines19319419519619719819920020175 unmodified lines}def generate(clip_video: Optional[torch.Tensor],sync_video: Optional[torch.Tensor],text: Optional[list[str]],*,negative_text: Optional[list[str]] = None,feature_utils: FeaturesUtils,net: MMAudio,fm: FlowMatching,rng: torch.Generator,cfg_strength: float,clip_batch_size_multiplier: int = 40,sync_batch_size_multiplier: int = 40,) -> torch.Tensor:device = feature_utils.devicedtype = feature_utils.dtypebs = len(text)if clip_video is not None:clip_video = clip_video.to(device, dtype, non_blocking=True)clip_features = feature_utils.encode_video_with_clip(clip_video,batch_size=bs *clip_batch_size_multiplier)else:clip_features = net.get_empty_clip_sequence(bs)if sync_video is not None:sync_video = sync_video.to(device, dtype, non_blocking=True)sync_features = feature_utils.encode_video_with_sync(sync_video,batch_size=bs *sync_batch_size_multiplier)else:sync_features = net.get_empty_sync_sequence(bs)80 unmodified linesdata_chunk = reader.pop_chunks()clip_chunk = data_chunk[0]sync_chunk = data_chunk[1]assert clip_chunk is not Noneassert sync_chunk is not Noneclip_frames = clip_transform(clip_chunk)sync_frames = sync_transform(sync_chunk)
14 unmodified lines1516171819202113 unmodified lines35363738394014 unmodified lines*,vae_ckpt_path,vocoder_ckpt_path: Optional[str] = None,mode: Literal['16k', '44k']):super().__init__()self.vae: VAE = get_my_vae(mode).eval()vae_state_dict = torch.load(vae_ckpt_path, weights_only=True, map_location='cpu')13 unmodified linesfor param in self.parameters():param.requires_grad = False@torch.inference_mode()def encode(self, x: torch.Tensor) -> DiagonalGaussianDistribution:return self.vae.encode(x)14 unmodified lines151617181920212213 unmodified lines36373839404142434414 unmodified lines*,vae_ckpt_path,vocoder_ckpt_path: Optional[str] = None,mode: Literal['16k', '44k'],need_vae_encoder: bool = True):super().__init__()self.vae: VAE = get_my_vae(mode).eval()vae_state_dict = torch.load(vae_ckpt_path, weights_only=True, map_location='cpu')13 unmodified linesfor param in self.parameters():param.requires_grad = Falseif not need_vae_encoder:del self.vae.encoder@torch.inference_mode()def encode(self, x: torch.Tensor) -> DiagonalGaussianDistribution:return self.vae.encode(x)
40 unmodified lines41424344454617 unmodified lines6465666768697071727374757677787980818238 unmodified lines12112212312412512612712812940 unmodified linessynchformer_ckpt: Optional[str] = None,enable_conditions: bool = True,mode=Literal['16k', '44k'],):super().__init__()17 unmodified linesif tod_vae_ckpt is not None:self.tod = AutoEncoderModule(vae_ckpt_path=tod_vae_ckpt,vocoder_ckpt_path=bigvgan_vocoder_ckpt,mode=mode)else:self.tod = Noneself.mel_converter = MelConverter()def compile(self):if self.clip_model is not None:self.encode_video_with_clip = torch.compile(self.encode_video_with_clip)self.clip_model.encode_image = torch.compile(self.clip_model.encode_image)self.clip_model.encode_text = torch.compile(self.clip_model.encode_text)if self.synchformer is not None:self.synchformer = torch.compile(self.synchformer)self.tod.encode = torch.compile(self.tod.encode)self.decode = torch.compile(self.decode)self.vocode = torch.compile(self.vocode)38 unmodified linesoutputs = []if batch_size < 0:batch_size = bfor i in range(0, b, batch_size):outputs.append(self.synchformer(x[i:i + batch_size]))x = torch.cat(outputs, dim=0).flatten(start_dim=1, end_dim=2)return x@torch.inference_mode()40 unmodified lines4142434445464717 unmodified lines65666768697071727374757677787980818238 unmodified lines12112212312412512612712812913013140 unmodified linessynchformer_ckpt: Optional[str] = None,enable_conditions: bool = True,mode=Literal['16k', '44k'],need_vae_encoder: bool = True,):super().__init__()17 unmodified linesif tod_vae_ckpt is not None:self.tod = AutoEncoderModule(vae_ckpt_path=tod_vae_ckpt,vocoder_ckpt_path=bigvgan_vocoder_ckpt,mode=mode,need_vae_encoder=need_vae_encoder)else:self.tod = Noneself.mel_converter = MelConverter()def compile(self):if self.clip_model is not None:self.clip_model.encode_image = torch.compile(self.clip_model.encode_image)self.clip_model.encode_text = torch.compile(self.clip_model.encode_text)if self.synchformer is not None:self.synchformer = torch.compile(self.synchformer)self.decode = torch.compile(self.decode)self.vocode = torch.compile(self.vocode)38 unmodified linesoutputs = []if batch_size < 0:batch_size = bx = rearrange(x, 'b s t c h w -> (b s) 1 t c h w')for i in range(0, b * num_segments, batch_size):outputs.append(self.synchformer(x[i:i + batch_size]))x = torch.cat(outputs, dim=0)x = rearrange(x, '(b s) 1 t d -> b (s t) d', b=b)return x@torch.inference_mode()
app.py+2-1
mmaudio/eval_utils.py+20-17
mmaudio/ext/autoencoder/autoencoder.py+5-1
mmaudio/model/utils/features_utils.py+7-5