-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use accurate available CPU count STT-wide #2253
Open
wasertech
wants to merge
16
commits into
coqui-ai:main
Choose a base branch
from
wasertech:fix_mp_cpucount
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
57fe9e2
fix cpu count
NanoNabla ad600fb
Add generate_scorer_batch for batch creating LMs
HarikalarKutusu fd7f3ba
Merge branch 'coqui-ai:main' into main
HarikalarKutusu 5e60418
Fixes for batch LM
HarikalarKutusu 3dc2efc
Add some enhancements
HarikalarKutusu b9da8e6
Merge branch 'coqui-ai:main' into main
HarikalarKutusu 22c8cd5
Reimpl özden batch lm gen
wasertech e6af942
Better output managment
wasertech 155090b
Added units
wasertech 38b1643
improve rendering date
wasertech 2792c00
rm unused code in test
wasertech 4ad8527
Merge branch 'batch-lm-gen-fix' into fix_mp_cpucount
wasertech 559acb7
use accurate cpu count stt-wide
wasertech 2b112c0
fix the universe and everything
wasertech ff2e131
rm unused import
wasertech 51388ee
rm unused import II
wasertech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/sh | ||
|
||
# This test optimizes the scorer for testing purposes | ||
|
||
set -xe | ||
|
||
lm_path="./data/lm" | ||
sources_lm_filepath="./data/smoke_test/vocab.txt" | ||
|
||
# Force only one visible device because we have a single-sample dataset | ||
# and when trying to run on multiple devices (like GPUs), this will break | ||
|
||
python data/lm/generate_lm_batch.py \ | ||
--input_txt "${sources_lm_filepath}" \ | ||
--output_dir "${lm_path}" \ | ||
--top_k_list 30000 \ | ||
--arpa_order_list "4" \ | ||
--max_arpa_memory "85%" \ | ||
--arpa_prune_list "0|0|2" \ | ||
--binary_a_bits 255 \ | ||
--binary_q_bits 8 \ | ||
--binary_type trie \ | ||
--kenlm_bins /code/kenlm/build/bin/ \ | ||
-j 1 |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
import argparse | ||
import gzip | ||
import io | ||
import os | ||
import re | ||
import subprocess | ||
import logging | ||
from collections import Counter | ||
import datetime, time | ||
from pathlib import Path | ||
|
||
import concurrent.futures | ||
from concurrent.futures import wait | ||
|
||
import progressbar | ||
from clearml import Task | ||
|
||
from generate_lm import build_lm, convert_and_filter_topk | ||
from coqui_stt_training.util import cpu | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
wxh = os.get_terminal_size() | ||
|
||
LINE = "-" * wxh.lines | ||
|
||
|
||
def generate_batch_lm( | ||
parser_batch, arpa_order, top_k, arpa_prune, i, total_runs, output_dir | ||
): | ||
results = [] | ||
Path(output_dir).mkdir(parents=True, exist_ok=True) | ||
# Create a child parser and add single elements | ||
parser_single = argparse.ArgumentParser( | ||
parents=[parser_batch], | ||
add_help=False, | ||
) | ||
parser_single.add_argument("--arpa_order", type=int, default=arpa_order) | ||
parser_single.add_argument("--top_k", type=int, default=top_k) | ||
parser_single.add_argument("--arpa_prune", type=str, default=arpa_prune) | ||
args_single = parser_single.parse_args() | ||
args_single.output_dir = output_dir | ||
_start_time = ( | ||
time.perf_counter() | ||
) # We use time.perf_counter() to acurately mesure delta of t; not datetime obj nor standard time.time() | ||
# logging.info("-" * 3 * 10) | ||
results.append( | ||
f"{datetime.datetime.now():%Y-%m-%d %H:%M} RUNNING {i}/{total_runs} FOR {arpa_order=} {top_k=} {arpa_prune=}" | ||
) | ||
# logging.info("-" * 3 * 10) | ||
# call with these arguments | ||
data_lower, vocab_str = convert_and_filter_topk(args_single) | ||
build_lm(args_single, data_lower, vocab_str) | ||
parser_single = None | ||
os.remove(os.path.join(output_dir, "lm.arpa")) | ||
os.remove(os.path.join(output_dir, "lm_filtered.arpa")) | ||
os.remove(os.path.join(output_dir, "lower.txt.gz")) | ||
results.append( | ||
f"LM generation {i} took: {time.perf_counter() - _start_time} seconds" | ||
) | ||
return results | ||
|
||
|
||
def parse_args(): | ||
n = int(cpu.available_count()) | ||
parser_batch = argparse.ArgumentParser( | ||
description="Generate lm.binary and top-k vocab for Coqui STT in batch for multiple arpa_order, top_k and arpa_prune values." | ||
) | ||
parser_batch.add_argument( | ||
"--input_txt", | ||
help="Path to a file.txt or file.txt.gz with sample sentences", | ||
type=str, | ||
required=True, | ||
) | ||
parser_batch.add_argument( | ||
"--output_dir", help="Directory path for the output", type=str, required=True | ||
) | ||
# parser.add_argument( | ||
# "--top_k", | ||
# help="Use top_k most frequent words for the vocab.txt file. These will be used to filter the ARPA file.", | ||
# type=int, | ||
# required=False, | ||
# ) | ||
parser_batch.add_argument( | ||
"--kenlm_bins", | ||
help="File path to the KENLM binaries lmplz, filter and build_binary", | ||
type=str, | ||
required=True, | ||
) | ||
# parser.add_argument( | ||
# "--arpa_order", | ||
# help="Order of k-grams in ARPA-file generation", | ||
# type=int, | ||
# required=False, | ||
# ) | ||
parser_batch.add_argument( | ||
"--max_arpa_memory", | ||
help="Maximum allowed memory usage for ARPA-file generation", | ||
type=str, | ||
required=True, | ||
) | ||
# parser.add_argument( | ||
# "--arpa_prune", | ||
# help="ARPA pruning parameters. Separate values with '|'", | ||
# type=str, | ||
# required=True, | ||
# ) | ||
parser_batch.add_argument( | ||
"--binary_a_bits", | ||
help="Build binary quantization value a in bits", | ||
type=int, | ||
required=True, | ||
) | ||
parser_batch.add_argument( | ||
"--binary_q_bits", | ||
help="Build binary quantization value q in bits", | ||
type=int, | ||
required=True, | ||
) | ||
parser_batch.add_argument( | ||
"--binary_type", | ||
help="Build binary data structure type", | ||
type=str, | ||
required=True, | ||
) | ||
parser_batch.add_argument( | ||
"--discount_fallback", | ||
help="To try when such message is returned by kenlm: 'Could not calculate Kneser-Ney discounts [...] rerun with --discount_fallback'", | ||
action="store_true", | ||
) | ||
parser_batch.add_argument( | ||
"--clearml_project", | ||
required=False, | ||
default="STT/wav2vec2 decoding", | ||
) | ||
parser_batch.add_argument( | ||
"--clearml_task", | ||
required=False, | ||
default="LM generation", | ||
) | ||
|
||
# | ||
# The following are added for batch processing instead of single ones commented out above | ||
# | ||
|
||
parser_batch.add_argument( | ||
"--arpa_order_list", | ||
help="List of arpa_order values. Separate values with '-' (e.g. '3-4-5').", | ||
type=str, | ||
required=True, | ||
) | ||
parser_batch.add_argument( | ||
"--top_k_list", | ||
help="A list of top_k values. Separate values with '-' (e.g. '20000-50000').", | ||
type=str, | ||
required=True, | ||
) | ||
parser_batch.add_argument( | ||
"--arpa_prune_list", | ||
help="ARPA pruning parameters. Separate values with '|', groups with '-' (e.g. '0|0|1-0|0|2')", | ||
type=str, | ||
required=True, | ||
) | ||
parser_batch.add_argument( | ||
"-j", | ||
"--n_proc", | ||
help=f"Maximum allowed processes. (default: {n})", | ||
type=int, | ||
default=n, | ||
) | ||
|
||
return parser_batch | ||
|
||
|
||
def main(): | ||
|
||
args_batch = parse_args() | ||
args_parsed_batch = args_batch.parse_args() | ||
|
||
try: | ||
task = Task.init( | ||
project_name=args_parsed_batch.clearml_project, | ||
task_name=args_parsed_batch.clearml_task, | ||
) | ||
except Exception: | ||
pass | ||
|
||
arpa_order_list = [] | ||
top_k_list = [] | ||
for x in args_parsed_batch.arpa_order_list.split("-"): | ||
if x.isnumeric(): | ||
arpa_order_list.append(int(float(x))) | ||
for x in args_parsed_batch.top_k_list.split("-"): | ||
if x.isnumeric(): | ||
top_k_list.append(int(float(x))) | ||
arpa_prune_list = args_parsed_batch.arpa_prune_list.split("-") | ||
|
||
i = 1 | ||
total_runs = len(arpa_order_list) * len(top_k_list) * len(arpa_prune_list) | ||
start_time = time.perf_counter() | ||
|
||
assert int(args_parsed_batch.n_proc) <= int( | ||
total_runs | ||
), f"Maximum number of proc exceded given {total_runs} task(s).\n[{args_parsed_batch.n_proc=} <= {total_runs=}]\nSet the -j|--n_proc argument to a value equal or lower than {total_runs}." | ||
|
||
n = int(args_parsed_batch.n_proc) | ||
|
||
with concurrent.futures.ThreadPoolExecutor(max_workers=n) as executor: | ||
futures = [] | ||
try: | ||
for i, arpa_order in enumerate(arpa_order_list, start=1): | ||
for top_k in top_k_list: | ||
for arpa_prune in arpa_prune_list: | ||
output_dir = os.path.join( | ||
args_parsed_batch.output_dir, | ||
f"{arpa_order}-{top_k}-{arpa_prune}", | ||
) | ||
future = executor.submit( | ||
generate_batch_lm, | ||
args_batch, | ||
arpa_order, | ||
top_k, | ||
arpa_prune, | ||
i, | ||
total_runs, | ||
output_dir, | ||
) | ||
futures.append(future) | ||
i += 1 | ||
f = wait(futures) | ||
print(LINE) | ||
for d in f.done: | ||
for r in d.result(): | ||
print(r) | ||
print(LINE) | ||
except KeyboardInterrupt: | ||
print("Caught KeyboardInterrupt, terminating workers") | ||
executor.terminate() | ||
executor.join() | ||
|
||
try: | ||
task.upload_artifact( | ||
name="lm.binary", | ||
artifact_object=os.path.join(args_parsed_batch.output_dir, "lm.binary"), | ||
) | ||
except Exception: | ||
pass | ||
|
||
# Delete intermediate files | ||
# os.remove(os.path.join(args_batch.output_dir, "lower.txt.gz")) | ||
|
||
logging.info( | ||
f"Took {time.perf_counter() - start_time} seconds to generate {total_runs} language {'models' if total_runs > 1 else 'model'}." | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except KeyboardInterrupt: | ||
exit(1) |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
catch interrupt and raise it so
main()
can exit with code 1