From 33d3d2084403fd34b79c835d2f2fe04f6cd8f738 Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期三, 13 九月 2023 09:33:54 +0800
Subject: [PATCH] Merge branch 'main' of github.com:alibaba-damo-academy/FunASR add

---
 funasr/export/models/predictor/cif.py |  209 +++++++++++++++++++++++++++++++++------------------
 1 files changed, 135 insertions(+), 74 deletions(-)

diff --git a/funasr/export/models/predictor/cif.py b/funasr/export/models/predictor/cif.py
index 034e233..03c4433 100644
--- a/funasr/export/models/predictor/cif.py
+++ b/funasr/export/models/predictor/cif.py
@@ -1,9 +1,8 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
+
 import torch
 from torch import nn
-import logging
-import numpy as np
 
 
 def sequence_mask(lengths, maxlen=None, dtype=torch.float32, device=None):
@@ -37,6 +36,17 @@
 	def forward(self, hidden: torch.Tensor,
 	            mask: torch.Tensor,
 	            ):
+		alphas, token_num = self.forward_cnn(hidden, mask)
+		mask = mask.transpose(-1, -2).float()
+		mask = mask.squeeze(-1)
+		hidden, alphas, token_num = self.tail_process_fn(hidden, alphas, mask=mask)
+		acoustic_embeds, cif_peak = cif(hidden, alphas, self.threshold)
+		
+		return acoustic_embeds, token_num, alphas, cif_peak
+	
+	def forward_cnn(self, hidden: torch.Tensor,
+	            mask: torch.Tensor,
+	            ):
 		h = hidden
 		context = h.transpose(1, 2)
 		queries = self.pad(context)
@@ -48,14 +58,10 @@
 		alphas = torch.nn.functional.relu(alphas * self.smooth_factor - self.noise_threshold)
 		mask = mask.transpose(-1, -2).float()
 		alphas = alphas * mask
-		
 		alphas = alphas.squeeze(-1)
-		
 		token_num = alphas.sum(-1)
-		
-		acoustic_embeds, cif_peak = cif(hidden, alphas, self.threshold)
-		
-		return acoustic_embeds, token_num, alphas, cif_peak
+
+		return alphas, token_num
 	
 	def tail_process_fn(self, hidden, alphas, token_num=None, mask=None):
 		b, t, d = hidden.size()
@@ -63,18 +69,21 @@
 		
 		zeros_t = torch.zeros((b, 1), dtype=torch.float32, device=alphas.device)
 		ones_t = torch.ones_like(zeros_t)
+
 		mask_1 = torch.cat([mask, zeros_t], dim=1)
 		mask_2 = torch.cat([ones_t, mask], dim=1)
 		mask = mask_2 - mask_1
 		tail_threshold = mask * tail_threshold
-		alphas = torch.cat([alphas, tail_threshold], dim=1)
-		
+		alphas = torch.cat([alphas, zeros_t], dim=1)
+		alphas = torch.add(alphas, tail_threshold)
+
 		zeros = torch.zeros((b, 1, d), dtype=hidden.dtype).to(hidden.device)
 		hidden = torch.cat([hidden, zeros], dim=1)
 		token_num = alphas.sum(dim=-1)
 		token_num_floor = torch.floor(token_num)
 		
 		return hidden, alphas, token_num_floor
+
 
 # @torch.jit.script
 # def cif(hidden, alphas, threshold: float):
@@ -113,70 +122,14 @@
 # 	fires = torch.stack(list_fires, 1)
 # 	frames = torch.stack(list_frames, 1)
 # 	list_ls = []
-# 	len_labels = torch.round(alphas.sum(-1)).int()
-# 	max_label_len = len_labels.max().item()
-# 	# print("type: {}".format(type(max_label_len)))
+# 	len_labels = torch.floor(alphas.sum(-1)).int()
+# 	max_label_len = len_labels.max()
 # 	for b in range(batch_size):
 # 		fire = fires[b, :]
 # 		l = torch.index_select(frames[b, :, :], 0, torch.nonzero(fire >= threshold).squeeze())
-# 		pad_l = torch.zeros([int(max_label_len - l.size(0)), int(hidden_size)], dtype=l.dtype, device=hidden.device)
+# 		pad_l = torch.zeros([int(max_label_len - l.size(0)), int(hidden_size)], device=hidden.device)
 # 		list_ls.append(torch.cat([l, pad_l], 0))
 # 	return torch.stack(list_ls, 0), fires
-
-# @torch.jit.script
-# def cif(hidden, alphas, threshold: float):
-# 	batch_size, len_time, hidden_size = hidden.size()
-# 	threshold = torch.tensor([threshold], dtype=alphas.dtype).to(alphas.device)
-#
-# 	# loop varss
-# 	integrate = torch.zeros([batch_size], dtype=alphas.dtype, device=hidden.device)
-# 	frame = torch.zeros([batch_size, hidden_size], dtype=hidden.dtype, device=hidden.device)
-# 	# intermediate vars along time
-# 	list_fires = []
-# 	list_frames = []
-#
-# 	for t in range(len_time):
-# 		alpha = alphas[:, t]
-# 		distribution_completion = torch.ones([batch_size], dtype=alphas.dtype, device=hidden.device) - integrate
-#
-# 		integrate += alpha
-# 		list_fires.append(integrate)
-#
-# 		fire_place = integrate >= threshold
-# 		integrate = torch.where(fire_place,
-# 		                        integrate - torch.ones([batch_size], dtype=alphas.dtype, device=hidden.device),
-# 		                        integrate)
-# 		cur = torch.where(fire_place,
-# 		                  distribution_completion,
-# 		                  alpha)
-# 		remainds = alpha - cur
-#
-# 		frame += cur[:, None] * hidden[:, t, :]
-# 		list_frames.append(frame)
-# 		frame = torch.where(fire_place[:, None].repeat(1, hidden_size),
-# 		                    remainds[:, None] * hidden[:, t, :],
-# 		                    frame)
-#
-# 	fires = torch.stack(list_fires, 1)
-# 	frames = torch.stack(list_frames, 1)
-# 	len_labels = torch.floor(torch.sum(alphas, dim=1)).int()
-# 	max_label_len = torch.max(len_labels)
-# 	pad_num = max_label_len - len_labels
-# 	pad_num_max = torch.max(pad_num).item()
-# 	frames_pad_tensor = torch.zeros([int(batch_size), int(pad_num_max), int(hidden_size)], dtype=frames.dtype,
-# 	                                device=frames.device)
-# 	fires_pad_tensor = torch.ones([int(batch_size), int(pad_num_max)], dtype=fires.dtype, device=fires.device)
-# 	fires_pad_tensor_mask = sequence_mask_scripts(pad_num, maxlen=int(pad_num_max))
-# 	fires_pad_tensor *= fires_pad_tensor_mask
-# 	frames_pad = torch.cat([frames, frames_pad_tensor], dim=1)
-# 	fires_pad = torch.cat([fires, fires_pad_tensor], dim=1)
-# 	index_bool = fires_pad >= threshold
-# 	frames_fire = frames_pad[index_bool]
-# 	frames_fire = torch.reshape(frames_fire, (int(batch_size), -1, int(hidden_size)))
-# 	frames_fire_mask = sequence_mask_scripts(len_labels, maxlen=int(max_label_len))
-# 	frames_fire *= frames_fire_mask[:, :, None]
-#
-# 	return frames_fire, fires
 
 
 @torch.jit.script
@@ -215,15 +168,11 @@
 	
 	fires = torch.stack(list_fires, 1)
 	frames = torch.stack(list_frames, 1)
-	# list_ls = []
-	len_labels = torch.round(alphas.sum(-1)).type(torch.int32)
-	# max_label_len = int(torch.max(len_labels).item())
-	# print("type: {}".format(type(max_label_len)))
+
 	fire_idxs = fires >= threshold
 	frame_fires = torch.zeros_like(hidden)
 	max_label_len = frames[0, fire_idxs[0]].size(0)
 	for b in range(batch_size):
-		# fire = fires[b, :]
 		frame_fire = frames[b, fire_idxs[b]]
 		frame_len = frame_fire.size(0)
 		frame_fires[b, :frame_len, :] = frame_fire
@@ -232,3 +181,115 @@
 			max_label_len = frame_len
 	frame_fires = frame_fires[:, :max_label_len, :]
 	return frame_fires, fires
+
+
+class CifPredictorV3(nn.Module):
+	def __init__(self, model):
+		super().__init__()
+		
+		self.pad = model.pad
+		self.cif_conv1d = model.cif_conv1d
+		self.cif_output = model.cif_output
+		self.threshold = model.threshold
+		self.smooth_factor = model.smooth_factor
+		self.noise_threshold = model.noise_threshold
+		self.tail_threshold = model.tail_threshold
+
+		self.upsample_times = model.upsample_times
+		self.upsample_cnn = model.upsample_cnn
+		self.blstm = model.blstm
+		self.cif_output2 = model.cif_output2
+		self.smooth_factor2 = model.smooth_factor2
+		self.noise_threshold2 = model.noise_threshold2
+	
+	def forward(self, hidden: torch.Tensor,
+	            mask: torch.Tensor,
+	            ):
+		h = hidden
+		context = h.transpose(1, 2)
+		queries = self.pad(context)
+		output = torch.relu(self.cif_conv1d(queries))
+		output = output.transpose(1, 2)
+		
+		output = self.cif_output(output)
+		alphas = torch.sigmoid(output)
+		alphas = torch.nn.functional.relu(alphas * self.smooth_factor - self.noise_threshold)
+		mask = mask.transpose(-1, -2).float()
+		alphas = alphas * mask
+		alphas = alphas.squeeze(-1)
+		token_num = alphas.sum(-1)
+		
+		mask = mask.squeeze(-1)
+		hidden, alphas, token_num = self.tail_process_fn(hidden, alphas, mask=mask)
+		acoustic_embeds, cif_peak = cif(hidden, alphas, self.threshold)
+		
+		return acoustic_embeds, token_num, alphas, cif_peak
+	
+	def get_upsample_timestmap(self, hidden, mask=None, token_num=None):
+		h = hidden
+		b = hidden.shape[0]
+		context = h.transpose(1, 2)
+
+		# generate alphas2
+		_output = context
+		output2 = self.upsample_cnn(_output)
+		output2 = output2.transpose(1, 2)
+		output2, (_, _) = self.blstm(output2)
+		alphas2 = torch.sigmoid(self.cif_output2(output2))
+		alphas2 = torch.nn.functional.relu(alphas2 * self.smooth_factor2 - self.noise_threshold2)
+		
+		mask = mask.repeat(1, self.upsample_times, 1).transpose(-1, -2).reshape(alphas2.shape[0], -1)
+		mask = mask.unsqueeze(-1)
+		alphas2 = alphas2 * mask
+		alphas2 = alphas2.squeeze(-1)
+		_token_num = alphas2.sum(-1)
+		alphas2 *= (token_num / _token_num)[:, None].repeat(1, alphas2.size(1))
+		# upsampled alphas and cif_peak
+		us_alphas = alphas2
+		us_cif_peak = cif_wo_hidden(us_alphas, self.threshold - 1e-4)
+		return us_alphas, us_cif_peak
+
+	def tail_process_fn(self, hidden, alphas, token_num=None, mask=None):
+		b, t, d = hidden.size()
+		tail_threshold = self.tail_threshold
+		
+		zeros_t = torch.zeros((b, 1), dtype=torch.float32, device=alphas.device)
+		ones_t = torch.ones_like(zeros_t)
+
+		mask_1 = torch.cat([mask, zeros_t], dim=1)
+		mask_2 = torch.cat([ones_t, mask], dim=1)
+		mask = mask_2 - mask_1
+		tail_threshold = mask * tail_threshold
+		alphas = torch.cat([alphas, zeros_t], dim=1)
+		alphas = torch.add(alphas, tail_threshold)
+
+		zeros = torch.zeros((b, 1, d), dtype=hidden.dtype).to(hidden.device)
+		hidden = torch.cat([hidden, zeros], dim=1)
+		token_num = alphas.sum(dim=-1)
+		token_num_floor = torch.floor(token_num)
+		
+		return hidden, alphas, token_num_floor
+
+
+@torch.jit.script
+def cif_wo_hidden(alphas, threshold: float):
+    batch_size, len_time = alphas.size()
+
+    # loop varss
+    integrate = torch.zeros([batch_size], dtype=alphas.dtype, device=alphas.device)
+    # intermediate vars along time
+    list_fires = []
+
+    for t in range(len_time):
+        alpha = alphas[:, t]
+
+        integrate += alpha
+        list_fires.append(integrate)
+
+        fire_place = integrate >= threshold
+        integrate = torch.where(fire_place,
+                                integrate - torch.ones([batch_size], device=alphas.device)*threshold,
+                                integrate)
+
+    fires = torch.stack(list_fires, 1)
+    return fires

--
Gitblit v1.9.1