class_docstr: Head selection implemented in S2TTransformer
class_docstr_tok: ["Head","selection","implemented","in","S2TTransformer"]
code: def build_encoder(cls, args):
      if (
          safe_hasattr(args, "encoder_attn_head_select")
          and args.encoder_attn_head_select
      ):
          encoder = HeadSelectionS2TTransformerEncoder(args)
      else:
          encoder = S2TTransformerEncoder(args)
      pretraining_path = getattr(
          args, "load_pretrained_encoder_from", None
      )
      if pretraining_path is not None:
          if not Path(pretraining_path).exists():
              logger.warning(
                  f"skipped pretraining because {pretraining_path} does not exist"
              )
          else:
              encoder = (
                  checkpoint_utils.load_pretrained_component_from_model(
                      component=encoder, checkpoint=pretraining_path
                  )
              )
              logger.info(
                  f"loaded pretrained encoder from: {pretraining_path}"
              )
      return encoder
  
imports: import logging
  from typing import Dict, List, Optional
  from pathlib import Path
  import torch.nn
  from torch import Tensor
  from fairseq import checkpoint_utils
  from fairseq.models import register_model, register_model_architecture
  from fairseq.utils import safe_hasattr
  from fairseq.models.speech_to_text.s2t_transformer import S2TTransformerModel, S2TTransformerEncoder, TransformerDecoderScriptable
  from fairseq.models.speech_to_text.s2t_transformer import base_architecture
  from ..modules.attn_head_selector import AttnHeadSelector
  from ..modules.head_selection_transformer_layer import HeadSelectionTransformerEncoderLayer
  from .head_selection_transformer import HeadSelectionTransformerDecoder
label_desc: Use image node_id 3 for calling the HeadSelectionS2TTransformerModel obj's underlying member method code with example usage: obj.build_encoder(cls, args) and returns: encoder