Skip to content
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

Fix util for randomisation of train file #427

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/train_from_scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from farm.modeling.tokenization import Tokenizer
from farm.data_handler.data_silo import StreamingDataSilo, DataSilo
from farm.data_handler.processor import BertStyleLMProcessor
from farm.data_handler.utils import randomize_and_split_file
from farm.data_handler.utils import split_file
from farm.modeling.adaptive_model import AdaptiveModel
from farm.modeling.language_model import LanguageModel
from farm.modeling.optimization import initialize_optimizer
Expand Down Expand Up @@ -53,7 +53,7 @@ def train_from_scratch():

# Option B) (recommended when using StreamingDataSilo):
# split and shuffle that file to have random order within and across epochs
randomize_and_split_file(data_dir / "train.txt", output_dir=Path("data/split_files"), docs_per_file=1000)
split_file(data_dir / "train.txt", output_dir=Path("data/split_files"), docs_per_file=1000)
train_filename = Path("data/split_files")

dev_filename = "dev.txt"
Expand Down
4 changes: 2 additions & 2 deletions examples/train_from_scratch_with_sagemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from farm.modeling.language_model import LanguageModel
from farm.modeling.optimization import initialize_optimizer
from farm.modeling.prediction_head import BertLMHead, NextSentenceHead
from farm.data_handler.utils import randomize_and_split_file
from farm.data_handler.utils import split_file
from farm.train import Trainer
from farm.utils import set_all_seeds, StdoutLogger, initialize_device_settings
import argparse
Expand Down Expand Up @@ -64,7 +64,7 @@ def train_from_scratch(args):

# Split and shuffle training data
if args["local_rank"] in [-1, 0]:
randomize_and_split_file(data_dir / args["train_file"], output_dir=data_dir / "split_files")
split_file(data_dir / args["train_file"], output_dir=data_dir / "split_files")
# let other processes wait for splitted files from rank 0
torch.distributed.barrier()

Expand Down
4 changes: 1 addition & 3 deletions farm/data_handler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ def filter_elements_per_worker(gen):
return iter(lambda: list(islice(iterable, n)), [])


def randomize_and_split_file(filepath, output_dir, docs_per_file=1_000, delimiter="", encoding="utf-8"):
def split_file(filepath, output_dir, docs_per_file=1_000, delimiter="", encoding="utf-8"):
total_lines = sum(1 for line in open(filepath, encoding=encoding))
output_file_number = 1
doc_count = 0
Expand All @@ -782,7 +782,6 @@ def randomize_and_split_file(filepath, output_dir, docs_per_file=1_000, delimite
filename = output_dir / f"part_{output_file_number}"
os.makedirs(os.path.dirname(filename), exist_ok=True)
write_file = stack.enter_context(open(filename, 'w+', buffering=10 * 1024 * 1024))
random.shuffle(lines_to_write)
write_file.writelines(lines_to_write)
write_file.close()
output_file_number += 1
Expand All @@ -792,7 +791,6 @@ def randomize_and_split_file(filepath, output_dir, docs_per_file=1_000, delimite
filename = output_dir / f"part_{output_file_number}"
os.makedirs(os.path.dirname(filename), exist_ok=True)
write_file = stack.enter_context(open(filename, 'w+', buffering=10 * 1024 * 1024))
random.shuffle(lines_to_write)
write_file.writelines(lines_to_write)
write_file.close()

Expand Down