xiph / LPCNet

Efficient neural speech synthesis
BSD 3-Clause "New" or "Revised" License
1.12k stars 295 forks source link

If I want use GRU instead of CuDNNGRU,How can I modify the reshape code of A in Sparsify and SparsifyGRUB #169

Open qipengh opened 2 years ago

qipengh commented 2 years ago

For some reason, I can't call CuDNNGRU, I can only call GRU. This will runs the network by small operators . In addition to modifying the interface, are there any changes to be made to the CuDNNGRU strange weight ordering part? For example, is the following my modification correct? thanks

diff --git a/training_tf2/lpcnet.py b/training_tf2/lpcnet.py
index ffb2390..a154903 100644
--- a/training_tf2/lpcnet.py
+++ b/training_tf2/lpcnet.py
@@ -100,7 +100,7 @@ class Sparsify(Callback):
                 A = p[:, k*N:(k+1)*N]
                 A = A - np.diag(np.diag(A))
                 #This is needed because of the CuDNNGRU strange weight ordering
-                A = np.transpose(A, (1, 0))
+                # A = np.transpose(A, (1, 0))
                 L=np.reshape(A, (N//4, 4, N//8, 8))
                 S=np.sum(L*L, axis=-1)
                 S=np.sum(S, axis=1)
@@ -111,7 +111,7 @@ class Sparsify(Callback):
                 mask = np.repeat(mask, 8, axis=1)
                 mask = np.minimum(1, mask + np.diag(np.ones((N,))))
                 #This is needed because of the CuDNNGRU strange weight ordering
-                mask = np.transpose(mask, (1, 0))
+                # mask = np.transpose(mask, (1, 0))
                 p[:, k*N:(k+1)*N] = p[:, k*N:(k+1)*N]*mask
                 #print(thresh, np.mean(mask))
             if self.quantize and ((self.batch > self.t_start and (self.batch-self.t_start) % self.interval == 0) or self.batch >= self.t_end):
@@ -155,8 +155,8 @@ class SparsifyGRUB(Callback):
                     density = 1 - (1-self.final_density[k])*(1 - r*r*r)
                 A = p[:, k*M:(k+1)*M]
                 #This is needed because of the CuDNNGRU strange weight ordering
-                A = np.reshape(A, (M, N))
-                A = np.transpose(A, (1, 0))
+                # A = np.reshape(A, (M, N))
+                # A = np.transpose(A, (1, 0))
                 N2 = self.grua_units
                 A2 = A[:N2, :]
                 L=np.reshape(A2, (N2//4, 4, M//8, 8))
@@ -169,8 +169,8 @@ class SparsifyGRUB(Callback):
                 mask = np.repeat(mask, 8, axis=1)
                 A = np.concatenate([A2*mask, A[N2:,:]], axis=0)
                 #This is needed because of the CuDNNGRU strange weight ordering
-                A = np.transpose(A, (1, 0))
-                A = np.reshape(A, (N, M))
+                # A = np.transpose(A, (1, 0))
+                # A = np.reshape(A, (N, M))
                 p[:, k*M:(k+1)*M] = A
                 #print(thresh, np.mean(mask))
             if self.quantize and ((self.batch > self.t_start and (self.batch-self.t_start) % self.interval == 0) or self.batch >= self.t_end):
@@ -266,15 +266,19 @@ def new_lpcnet_model(rnn_units1=384, rnn_units2=16, nb_used_features=20, batch_s
     quant = quant_regularizer if quantize else None

     if training:
-        rnn = CuDNNGRU(rnn_units1, return_sequences=True, return_state=True, name='gru_a', stateful=True,
+        # rnn = CuDNNGRU(rnn_units1, return_sequences=True, return_state=True, name='gru_a', stateful=True,
+        #       recurrent_constraint = constraint, recurrent_regularizer=quant)
+        # rnn2 = CuDNNGRU(rnn_units2, return_sequences=True, return_state=True, name='gru_b', stateful=True,
+        #        kernel_constraint=constraint, recurrent_constraint = constraint, kernel_regularizer=quant, recurrent_regularizer=quant)
+        rnn = GRU(rnn_units1, return_sequences=True, return_state=True, recurrent_activation="tanh", reset_after='true', name='gru_a',
               recurrent_constraint = constraint, recurrent_regularizer=quant)
-        rnn2 = CuDNNGRU(rnn_units2, return_sequences=True, return_state=True, name='gru_b', stateful=True,
+        rnn2 = GRU(rnn_units2, return_sequences=True, return_state=True, recurrent_activation="tanh", reset_after='true', name='gru_b',
                kernel_constraint=constraint, recurrent_constraint = constraint, kernel_regularizer=quant, recurrent_regularizer=quant)
     else:
         rnn = GRU(rnn_units1, return_sequences=True, return_state=True, recurrent_activation="sigmoid", reset_after='true', name='gru_a', stateful=True,