This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
torch.nn.functional.kl_div(student_log_prob, teacher_prob, reduction='batchmean') * (self.temperature**2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
features_pen = features.float().pow(2).mean() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
optimizer = torch.optim.Adam(self.parameters(), lr=self.lr) | |
def lr_lambda(current_epoch): | |
if current_epoch < self.num_lr_warm_up_epoch: | |
return float(current_epoch+1) / float(max(1, self.num_lr_warm_up_epoch)) | |
else: | |
return max( 0.0, float(self.max_epoch - current_epoch) / float(max(1, self.max_epoch - self.num_lr_warm_up_epoch))) | |
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
step = num_trans_layer_student_init_model // num_trans_layer_student_model student_init_model_selected_transformer_layers = [i for i in range(0, num_trans_layer_student_init_model, step)] | |
student_model_trans_layer_prefix = "encoder.layers." | |
student_model_transformer_layers = [i for i in range(num_trans_layer_student_model)] | |
for student_layer_i, init_layer_i in zip(student_model_transformer_layers, student_init_model_selected_transformer_layers): | |
for transformer_part in transformer_parts: | |
layer_name = student_model_trans_layer_prefix + str(student_layer_i) + transformer_part | |
param = student_init_model_state[student_init_model_trans_layer_prefix + str(init_layer_i) + transformer_part] | |
student_model_state[layer_name].copy_(param) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prepare_for_inference_after_quantization(self): | |
dequantizer = torch.nn.quantized.DeQuantize() | |
for trans_layer in self.encoder.layers: | |
trans_layer.self_attn.q_proj_bias = trans_layer.self_attn.q_proj.bias() | |
trans_layer.self_attn.k_proj_bias = trans_layer.self_attn.k_proj.bias() | |
trans_layer.self_attn.v_proj_bias = trans_layer.self_attn.v_proj.bias() | |
trans_layer.self_attn.in_proj_bias = torch.cat((trans_layer.self_attn.q_proj_bias, trans_layer.self_attn.k_proj_bias, trans_layer.self_attn.v_proj_bias)) | |
trans_layer.self_attn.out_proj_bias = trans_layer.self_attn.out_proj.bias() | |
trans_layer.self_attn.out_proj_weight = dequantizer(trans_layer.self_attn.out_proj.weight()) | |
trans_layer.self_attn.q_proj_weight = dequantizer(trans_layer.self_attn.q_proj.weight()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
quantized_model = torch.quantization.quantize_dynamic(pt_wav2vec2, {torch.nn.Linear}, dtype=torch.qint8, inplace=True) | |
quantized_model.prepare_for_inference_after_quantization() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
decoder_out = decoder.decode(emissions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wav2letter.criterion import CpuViterbiPath, get_data_ptr_as_bytes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decode(self, emissions): | |
B, T, N = emissions.size() | |
hypos = list() | |
if self.asg_transitions is None: | |
transitions = torch.FloatTensor(N, N).zero_() | |
else: | |
transitions = torch.FloatTensor(self.asg_transitions).view(N, N) | |
viterbi_path = torch.IntTensor(B, T) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ray | |
ray.init() |