-
Notifications
You must be signed in to change notification settings - Fork 9
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
Barebones python/S3 implementation #10
Open
revbucket
wants to merge
9
commits into
allenai:main
Choose a base branch
from
revbucket:s3_python
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 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
77e5160
Barebones python/S3 implementation
ab02649
Bash script written
8aaf3c9
Added i) directory support, ii) FP rate args, iii) No-Save option
10b192a
Made changes requseted in PR
a6b63fd
Added --no-progress flag
a7b7ccf
Added options for no-progress (cleaner signature) | Updated README.md…
2cc9cac
Cleaned up readme duplicates
f035820
Merged main
5be38a8
Bash script fixed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
""" Quick'n'dirty mapping of bff for python before I can make it pure-rust | ||
How this works: (using ray) | ||
- map to DOWNLOAD all files and store on some local location | ||
- use this local mapping to feed to bff.rs | ||
- map to UPLOAD all files to some output location | ||
*Yes I know this is clunky and requires 2x dataset size in local storage | ||
|
||
ASSUMES .jsonl.gz files, with 'text' field and FLAT file structure on s3 | ||
i.e. | ||
s3://bucket/input_dir/ | ||
file_00.jsonl.gz | ||
file_01.jsonl.gz | ||
. | ||
. | ||
. | ||
file_N.jsonl.gz | ||
# AND NO SUBDIRECTORIES! | ||
|
||
TODOS: | ||
- Add default tokenizer/counter to get good filter/ngram sizes automatically | ||
- Make this pure rust | ||
+ Only need |num_threads| * |file_size| *2 + |filter_size| memory | ||
+ Sadly I code rust like a preschooler, so I'm working on this =( | ||
- Add a progress bar vs printing filenames w/in rust code | ||
""" | ||
|
||
|
||
import argparse | ||
import ray | ||
import boto3 | ||
from pathlib import Path | ||
import os | ||
from tqdm.auto import tqdm | ||
import glob | ||
import subprocess | ||
|
||
|
||
RUST_CMD = os.path.join(os.path.dirname(__file__), 'target/release/bff') | ||
|
||
# ================================================= | ||
# = Ray/S3 Utils = | ||
# ================================================= | ||
|
||
def to_iterator(obj_ids, batch_size=100): | ||
while obj_ids: | ||
done, obj_ids = ray.wait(obj_ids, num_returns=min(batch_size, len(obj_ids))) | ||
for d in done: | ||
yield ray.get(d) | ||
|
||
|
||
def block_until_complete(ret): | ||
"""Use this when we want to efficiently wait until an iterable | ||
of ray object ids finishes | ||
""" | ||
|
||
for _ in tqdm(to_iterator(ret), total=len(ret)): | ||
pass | ||
ret = ray.get(ret) | ||
|
||
|
||
def copy_to_s3(local_obj, remote_dir, disable_tqdm=True): | ||
if remote_dir == None: | ||
return | ||
remote_obj = os.path.join(remote_dir, os.path.basename(local_obj)).replace("s3://", "") | ||
bucket, key = remote_obj.split("/", 1) | ||
s3 = boto3.client("s3") | ||
local_obj_size = os.path.getsize(local_obj) | ||
with tqdm(total=local_obj_size, unit="B", unit_scale=True, desc=local_obj, disable=disable_tqdm) as pbar: | ||
s3.upload_file(Bucket=bucket, Filename=local_obj, Key=key, Callback=lambda b: pbar.update(b)) | ||
|
||
@ray.remote | ||
def RAY_copy_to_s3(local_obj, remote_dir, disable_tqdm=True): | ||
copy_to_s3(local_obj, remote_dir, disable_tqdm=disable_tqdm) | ||
|
||
|
||
def copy_from_s3(remote_obj, local_dir, disable_tqdm=True): | ||
bucket, key = remote_obj.replace("s3://", "").split("/", 1) | ||
s3 = boto3.client("s3") | ||
remote_obj_size = s3.head_object(Bucket=bucket, Key=key)["ContentLength"] | ||
target = os.path.join(local_dir, os.path.basename(key)) | ||
with tqdm(total=remote_obj_size, unit="B", unit_scale=True, desc=remote_obj, disable=disable_tqdm) as pbar: | ||
s3.download_file(Bucket=bucket, Key=key, Filename=target, Callback=lambda b: pbar.update(b)) | ||
|
||
@ray.remote | ||
def RAY_copy_from_s3(remote_obj, local_dir, disable_tqdm=True): | ||
copy_from_s3(remote_obj, local_dir, disable_tqdm=disable_tqdm) | ||
|
||
|
||
def list_s3_keys(prefix, suffix='.jsonl.gz'): | ||
""" Lists full s3 uri's of all keys that match both the prefix and suffix. | ||
if Suffix==None => no filtering on suffx | ||
""" | ||
s3 = boto3.resource("s3") | ||
bucket_name, path_within_bucket = prefix.replace("s3://", "").split("/", 1) | ||
bucket = s3.Bucket(bucket_name) | ||
|
||
s3_uris = [] | ||
for x in bucket.objects.filter(Prefix=path_within_bucket): | ||
if suffix == None or x.key.endswith(suffix): | ||
s3_uris.append(os.path.join('s3://%s' % bucket_name, x.key)) | ||
return s3_uris | ||
|
||
|
||
|
||
# ================================================ | ||
# = Main block = | ||
# ================================================ | ||
|
||
|
||
def run_bff(tmp_dir, input_dir, output_dir, filter_size, expected_ngram_count): | ||
# Subprocess DIY python<->rust bindings. Might be better to use maturin, but this is a bit simpler | ||
cmd = '%s --bloom-filter-file %s --bloom-filter-size %s --expected-ngram-count %s --output-directory %s %s' % ( | ||
RUST_CMD, | ||
os.path.join(tmp_dir, 'filter.bff'), | ||
filter_size, | ||
expected_ngram_count, | ||
os.path.join(output_dir, ''), | ||
os.path.join(input_dir, '*') | ||
) | ||
return_code = subprocess.run(cmd, shell=True).returncode | ||
assert return_code == 0 | ||
|
||
|
||
def main(s3_input_dir, s3_output_dir, filter_size, | ||
expected_ngram_count, tmp_storage_dir, suffix='.jsonl.gz'): | ||
# Step 0: initialize ray and args and local storage directories | ||
ray.init(ignore_reinit_error=True) | ||
|
||
input_dir = os.path.join(tmp_storage_dir, 'input') | ||
output_dir = os.path.join(tmp_storage_dir, 'output') | ||
for p in input_dir, output_dir: | ||
Path(p).mkdir(parents=True, exist_ok=True) | ||
|
||
# step 1: collect and download s3 files to disk | ||
print("Collecting and downloading s3 files...") | ||
s3_uris = list_s3_keys(s3_input_dir, suffix=suffix)[:10] | ||
download_refs = [RAY_copy_from_s3.remote(uri, input_dir) for uri in s3_uris] | ||
block_until_complete(download_refs) | ||
|
||
# Step 2: Run BFF | ||
print("Running BFF on %s local files..." % len(s3_uris)) | ||
run_bff(tmp_storage_dir, input_dir, output_dir, filter_size, expected_ngram_count) | ||
|
||
|
||
# Step 3: upload output files to S3 | ||
print("Uploading filtered files...") | ||
output_files = glob.glob(os.path.join(output_dir, '*%s' % suffix if suffix != None else '*')) | ||
upload_refs = [RAY_copy_to_s3.remote(f, s3_output_dir) for f in output_files] | ||
block_until_complete(upload_refs) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
|
||
# S3 adapter args | ||
parser.add_argument('--s3-input-dir', type=str) | ||
parser.add_argument('--s3-output-dir', type=str) | ||
|
||
# Args passed to rust function | ||
parser.add_argument('--filter-size', type=int) | ||
parser.add_argument('--expected-ngram-count', type=int) | ||
parser.add_argument('--tmp-storage-dir', type=str, default='/tmp/bff') | ||
|
||
args = parser.parse_args() | ||
main(s3_input_dir=args.s3_input_dir, | ||
s3_output_dir=args.s3_output_dir, | ||
filter_size=args.filter_size, | ||
expected_ngram_count=args.expected_ngram_count, | ||
tmp_storage_dir=args.tmp_storage_dir) |
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 |
---|---|---|
|
@@ -175,12 +175,11 @@ impl BloomFilter { | |
hash_builder_seeds.push(seeds); | ||
} | ||
|
||
let mut bits = Vec::new(); | ||
let number_of_u32 = size_in_bytes / size_of::<AtomicU32>(); | ||
bits.reserve_exact(number_of_u32); | ||
for _ in 0..number_of_u32 { | ||
bits.push(AtomicU32::new(0)); | ||
} | ||
let mut bits = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We definitely want this change! |
||
(0..number_of_u32).map(|_| AtomicU32::default()).collect() | ||
}; | ||
|
||
|
||
Self { | ||
bits, | ||
|
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.
This is fine, if you want to put it into a
scripts/
directory, but I feel like this is not really necessary. You could replace the entire thing with a few lines of bash: