Spaces:
Running
Running
File size: 4,848 Bytes
67c46fd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
import os
import json
import torch
import logging
import concurrent.futures
import librosa
import torch.distributed as dist
from funasr_detach.register import tables
@tables.register("index_ds_classes", "IndexDSJsonlRankSplit")
class IndexDSJsonlRankSplit(torch.utils.data.Dataset):
def __init__(self, path):
super().__init__()
contents = []
with open(path, encoding="utf-8") as fin:
for line in fin:
data = json.loads(line.strip())
if "text" in data: # for sft
self.contents.append(data["text"])
if "source" in data: # for speech lab pretrain
prompt = data["prompt"]
source = data["source"]
target = data["target"]
source_len = data["source_len"]
target_len = data["target_len"]
contents.append(
{
"source": source,
"prompt": prompt,
"target": target,
"source_len": source_len,
"target_len": target_len,
}
)
self.contents = []
total_num = len(contents)
try:
rank = dist.get_rank()
world_size = dist.get_world_size()
except:
rank = 0
world_size = 1
logging.warning("distributed is not initialized, only single shard")
num_per_rank = total_num // world_size
# rank = 0
# import ipdb; ipdb.set_trace()
self.contents = contents[rank * num_per_rank : (rank + 1) * num_per_rank]
logging.info(
"in rank: {}, num of samplers: {}, total_num of samplers across ranks: {}".format(
rank, len(self.contents), len(contents)
)
)
def __len__(self):
return len(self.contents)
def __getitem__(self, index):
try:
data = self.contents[index]
except:
print(index)
return data
def get_source_len(self, data_dict):
return data_dict["source_len"]
def get_target_len(self, data_dict):
return data_dict["target_len"] if "target_len" in data_dict else 0
@tables.register("index_ds_classes", "IndexDSJsonl")
@tables.register("index_ds_classes", "IndexDSJsonlRankFull")
class IndexDSJsonlRankFull(torch.utils.data.Dataset):
def __init__(self, path: str, **kwargs):
super().__init__()
if isinstance(path, (list, tuple)): # wav.scp, text.txt/text.trans
from funasr_detach.datasets.audio_datasets.scp2jsonl import (
gen_jsonl_from_wav_text_list,
)
jsonl_outdir = os.path.dirname(path[0])
jsonl_name = (
"datalist_train.jsonl"
if kwargs.get("is_training", True)
else "datalist_val.jsonl"
)
jsonl_file_out = os.path.join(jsonl_outdir, jsonl_name)
if not os.path.exists(jsonl_file_out):
print(f"datalist is: {path}, generate jsonl from it")
gen_jsonl_from_wav_text_list(
path, jsonl_file_out=jsonl_file_out, **kwargs
)
path = jsonl_file_out
contents = []
with open(path, encoding="utf-8") as fin:
for line in fin:
data = json.loads(line.strip())
if "text" in data: # for sft
self.contents.append(data["text"])
if "source" in data: # for speech lab pretrain
prompt = data.get("prompt", "<ASR>")
source = data["source"]
target = data["target"]
source_len = data.get("source_len", 1)
target_len = data.get("target_len", 0)
contents.append(
{
"source": source,
"prompt": prompt,
"target": target,
"source_len": source_len,
"target_len": target_len,
}
)
self.contents = contents
logging.info(
"total_num of samplers across ranks: {}".format(len(self.contents))
)
def __len__(self):
return len(self.contents)
def __getitem__(self, index):
try:
data = self.contents[index]
except:
print(index)
return data
def get_source_len(self, data_dict):
return data_dict.get("source_len", 1)
def get_target_len(self, data_dict):
return data_dict.get("target_len", 0)
|