Skip to content

Commit 1fc31e0

Browse files
committed
add freeze, unfreeze methods with experimental tag
Signed-off-by: nithinraok <nithinrao.koluguri@gmail.com>
1 parent e6df25c commit 1fc31e0

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

nemo/collections/asr/modules/transformer_encoder.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import torch.nn as nn
1919
from torch.nn.attention.flex_attention import create_block_mask, flex_attention
2020

21+
from nemo.utils.decorators import experimental
22+
2123
flex_attention_compiled = torch.compile(flex_attention, dynamic=True)
2224

2325

@@ -151,6 +153,7 @@ def forward(self, x, block_mask=None):
151153
return x
152154

153155

156+
@experimental
154157
class TransformerEncoder(nn.Module):
155158
"""Pre-norm Transformer encoder for ASR.
156159
@@ -239,3 +242,14 @@ def forward(self, audio_signal, length):
239242
x = self.final_norm(x)
240243
x = x.transpose(1, 2) # (B, T, D) -> (B, D, T)
241244
return x, length
245+
246+
def freeze(self):
247+
for p in self.parameters():
248+
p.requires_grad = False
249+
self.eval()
250+
251+
def unfreeze(self, partial: bool = False):
252+
for p in self.parameters():
253+
p.requires_grad = True
254+
if not partial:
255+
self.train()

0 commit comments

Comments
 (0)