docstr: Merges all shard files into shard 0, then removes shard suffix.
docstr_tok: ["Merges","all","shard","files","into","shard","0",",","then","removes","shard","suffix","."]
code: def merge_shards(self) -> None:
      """Merges all shard files into shard 0, then removes shard suffix."""
  
      shard_id = self.data_parallel_rank
      num_shards = self.data_parallel_world_size
  
      if self.data_parallel_world_size > 1:
  
          def merge_shards_with_root(fname: str) -> None:
              fname = os.path.join(
                  self.cfg.decoding.results_path, fname
              )
              logger.info("Merging %s on shard %d", fname, shard_id)
              base_fpath = Path(f"{fname}.0")
              with open(base_fpath, "a") as out_file:
                  for s in range(1, num_shards):
                      shard_fpath = Path(f"{fname}.{s}")
                      with open(shard_fpath, "r") as in_file:
                          for line in in_file:
                              out_file.write(line)
                      shard_fpath.unlink()
              shutil.move(f"{fname}.0", fname)
  
          dist.barrier()  # ensure all shards finished writing
          if shard_id == (0 % num_shards):
              merge_shards_with_root("hypo.word")
          if shard_id == (1 % num_shards):
              merge_shards_with_root("hypo.units")
          if shard_id == (2 % num_shards):
              merge_shards_with_root("ref.word")
          if shard_id == (3 % num_shards):
              merge_shards_with_root("ref.units")
          dist.barrier()
  
imports: import ast
  import hashlib
  import logging
  import os
  import shutil
  import sys
  import re
  from dataclasses import dataclass, field, is_dataclass
  from pathlib import Path
  from typing import Any, Dict, List, Optional, Tuple, Union
  import editdistance
  import torch
  import torch.distributed
  from examples.speech_recognition.new.decoders.decoder_config import DecoderConfig, FlashlightDecoderConfig
  from examples.speech_recognition.new.decoders.decoder import Decoder
  from fairseq import checkpoint_utils, distributed_utils, progress_bar, tasks, utils
  from fairseq.data.data_utils import post_process
  from fairseq.dataclass.configs import CheckpointConfig, CommonConfig, CommonEvalConfig, DatasetConfig, DistributedTrainingConfig, FairseqDataclass
  from fairseq.logging.meters import StopwatchMeter, TimeMeter
  from fairseq.logging.progress_bar import BaseProgressBar
  from fairseq.models.fairseq_model import FairseqModel
  from omegaconf import OmegaConf
  import hydra
  from hydra.core.config_store import ConfigStore
label_desc: Use image node_id 8 for calling the InferenceProcessor obj's underlying member method code with example usage: obj.merge_shards() without return types