# ################################
# Model: wav2vec2 + DNN + CTC/Attention
# Augmentation: SpecAugment
# Authors: Titouan Parcollet 2021
# ################################

sample_rate: 16000
wav2vec2_hub: LeBenchmark/wav2vec2-FR-7K-large

# BPE parameters
token_type: unigram  # ["unigram", "bpe", "char"]
character_coverage: 1.0

# Model parameters
activation: !name:torch.nn.LeakyReLU
dnn_layers: 2
dnn_neurons: 1024
emb_size: 128
dec_neurons: 1024

# Outputs
output_neurons: 76  # BPE size, index(blank/eos/bos) = 0

# Decoding parameters
# Be sure that the bos and eos index match with the BPEs ones
blank_index: 0
bos_index: 1
eos_index: 2
min_decode_ratio: 0.0
max_decode_ratio: 1.0
beam_size: 80
eos_threshold: 1.5
using_max_attn_shift: True
max_attn_shift: 140
ctc_weight_decode: 0.0
temperature: 1.50

enc: !new:speechbrain.nnet.containers.Sequential
    input_shape: [null, null, 1024]
    linear1: !name:speechbrain.nnet.linear.Linear
        n_neurons: 1024
        bias: True
    bn1: !name:speechbrain.nnet.normalization.BatchNorm1d
    activation: !new:torch.nn.LeakyReLU
    drop: !new:torch.nn.Dropout
        p: 0.15
    linear2: !name:speechbrain.nnet.linear.Linear
        n_neurons: 1024
        bias: True
    bn2: !name:speechbrain.nnet.normalization.BatchNorm1d
    activation2: !new:torch.nn.LeakyReLU
    drop2: !new:torch.nn.Dropout
        p: 0.15
    linear3: !name:speechbrain.nnet.linear.Linear
        n_neurons: 1024
        bias: True
    bn3: !name:speechbrain.nnet.normalization.BatchNorm1d
    activation3: !new:torch.nn.LeakyReLU

wav2vec2: !new:speechbrain.lobes.models.huggingface_wav2vec.HuggingFaceWav2Vec2
    source: !ref <wav2vec2_hub>
    output_norm: True
    freeze: True
    pretrain: False
    save_path: model_checkpoints

ctc_lin: !new:speechbrain.nnet.linear.Linear
    input_size: !ref <dnn_neurons>
    n_neurons: !ref <output_neurons>

log_softmax: !new:speechbrain.nnet.activations.Softmax
    apply_log: True

ctc_cost: !name:speechbrain.nnet.losses.ctc_loss
    blank_index: !ref <blank_index>

asr_model: !new:torch.nn.ModuleList
    - [!ref <enc>, !ref <ctc_lin>]

tokenizer: !new:sentencepiece.SentencePieceProcessor

encoder: !new:speechbrain.nnet.containers.LengthsCapableSequential
    wav2vec2: !ref <wav2vec2>
    enc: !ref <enc>
    ctc_lin: !ref <ctc_lin>

decoding_function: !name:speechbrain.decoders.ctc_greedy_decode
    blank_id: !ref <blank_index>

modules:
    encoder: !ref <encoder>

pretrainer: !new:speechbrain.utils.parameter_transfer.Pretrainer
    loadables:
        wav2vec2: !ref <wav2vec2>
        asr: !ref <asr_model>
        tokenizer: !ref <tokenizer>