Commit ·
4ef8025
1
Parent(s): d492aea
Update multi_threaded_processed.py
Browse files18 unmodified lines1920212223242526277 unmodified lines3536373839402 unmodified lines43444546474812 unmodified lines61626364656616 unmodified lines838485868788899091929332 unmodified lines12612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017118 unmodified lines# In the multiprocessing case, the below leads to each process creating the same directory# random = random.Random(42) # make it reproducibleNUM_THREADS = 16NUM_PROC = 64DEBUG_SIZE = 4096CWD = os.getcwd()7 unmodified linescompleted = run_in_shell("git fetch --depth 2 origin " + commit, cwd=cwd)# If it requires authenticationif completed.returncode != 0:return ("", "")# Optionally do git diff at the same time (Saving code needs to be added)# git_diff = run_in_shell(f"git diff {commit}^ {commit}", cwd=cwd).stdout.decode(errors='ignore')2 unmodified linescompleted = run_in_shell("git checkout FETCH_HEAD^ -- " + old_file, cwd=cwd)# If there's only a new file, but no old fileif completed.returncode != 0:return (new_contents, "")old_contents = run_in_shell("cat " + old_file, cwd=cwd).stdout.decode(errors='ignore')return (new_contents, old_contents)12 unmodified linestry:new_contents, old_contents = get_file_contents(commit_id, old_file, new_file, repo, cwd=random_dir)except Exception as e:# Break in case of many repos that all lead us nowhereif i > 10:break16 unmodified linesreturn {k: [dic[k] for dic in results] for k in results[0]}if __name__ == "__main__":# git clone bigcode/github-commitsds = datasets.load_dataset("./github-commits", use_auth_token=True)["train"]### OPTIONAL FILTERING ###"""java = [".java"]javascript = [".js",32 unmodified linesextensions = json.load(f)suffices = [suffix for suffices in extensions.values() for suffix in suffices]def filter_extension(ex):splits = ex["new_file"].split(".")if len(splits) == 1: return Falsereturn "." + splits[-1] in sufficesdef filter_extension_python(ex):splits = ex["new_file"].split(".")if len(splits) == 1: return Falsesplits = ex["new_file"].split(".")return "." + splits[-1] in pythondef filter_update(ex):return ex["message"] != "Update " + ex["old_file"]filter_msg = ["initial commit", "please\n", "please"]def filter_misc(ex):return ex["message"] not in filter_msg# Removes ~10Mds = ds.filter(filter_extension, num_proc=NUM_PROC)print("After Extension filter", len(ds))# Removes ~1Mds = ds.filter(filter_update, num_proc=NUM_PROC)print("After Update filter", len(ds))ds = ds.filter(filter_extension_python, num_proc=NUM_PROC)print("After Python filter", len(ds))ds = ds.filter(filter_misc, num_proc=NUM_PROC)print("After Misc filter", len(ds))ds = ds.select(range(DEBUG_SIZE))"""### END FILTERING ###def run_multi_processing_threading():ds.map(get_diff_multi_threaded_processed, num_proc=NUM_PROC, batch_size=NUM_THREADS, batched=True).to_json("mpt.jsonl")NUM_TRIALS = 1print(f"Timing multithreading + multiprocessing using {NUM_THREADS} threads and {NUM_PROC} processes")time = timeit.timeit(stmt=run_multi_processing_threading, number=NUM_TRIALS)print("Time:", time)with open("mpt.txt", "w") as f:f.write(str(time))#run_multi_processing_threading()18 unmodified lines1920212223242526277 unmodified lines353637383940412 unmodified lines4445464748495012 unmodified lines6364656667686916 unmodified lines868788899091929394959632 unmodified lines12913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417518 unmodified lines# In the multiprocessing case, the below leads to each process creating the same directory# random = random.Random(42) # make it reproducibleNUM_THREADS = 64NUM_PROC = 64# DEBUG_SIZE = 1024CWD = os.getcwd()7 unmodified linescompleted = run_in_shell("git fetch --depth 2 origin " + commit, cwd=cwd)# If it requires authenticationif completed.returncode != 0:#print("ERRORC1", completed)return ("", "")# Optionally do git diff at the same time (Saving code needs to be added)# git_diff = run_in_shell(f"git diff {commit}^ {commit}", cwd=cwd).stdout.decode(errors='ignore')2 unmodified linescompleted = run_in_shell("git checkout FETCH_HEAD^ -- " + old_file, cwd=cwd)# If there's only a new file, but no old fileif completed.returncode != 0:#print("ERRORC2", completed)return (new_contents, "")old_contents = run_in_shell("cat " + old_file, cwd=cwd).stdout.decode(errors='ignore')return (new_contents, old_contents)12 unmodified linestry:new_contents, old_contents = get_file_contents(commit_id, old_file, new_file, repo, cwd=random_dir)except Exception as e:#print("ERROR", commit_id, old_file, new_file, repo, str(random_dir), e)# Break in case of many repos that all lead us nowhereif i > 10:break16 unmodified linesreturn {k: [dic[k] for dic in results] for k in results[0]}if __name__ == "__main__":# git clone https://huggingface.co/datasets/bigcode/github-commitsds = datasets.load_dataset("./github-commits", use_auth_token=True)["train"]### OPTIONAL FILTERING ####"""java = [".java"]javascript = [".js",32 unmodified linesextensions = json.load(f)suffices = [suffix for suffices in extensions.values() for suffix in suffices]def filter_extension(ex):return ex["new_file"].endswith(suffices)def filter_extension_python(ex):return ex["new_file"].endswith(python)def filter_update(ex):return ex["message"] != "Update " + ex["old_file"]filter_msg = ["initial commit", "please\n", "please", "lalala"]def filter_misc(ex):return ex["message"] not in filter_msg# Removes ~10Mds = ds.filter(filter_extension, num_proc=NUM_PROC)print("After Extension filter", len(ds))# Removes ~1Mds = ds.filter(filter_update, num_proc=NUM_PROC)print("After Update filter", len(ds))#ds = ds.filter(filter_extension_python, num_proc=NUM_PROC)#print("After Python filter", len(ds))ds = ds.filter(filter_misc, num_proc=NUM_PROC)print("After Misc filter", len(ds))#ds = ds.select(range(DEBUG_SIZE))START = 0 # Modify for each instance (0 - 7)samples_per_instance = 64 * 64 * 64 * 32 # 8_388_608select_start = START * samples_per_instanceselect_end = START * samples_per_instance + samples_per_instanceds = ds.select(range(select_start, select_end))print(f"Going from {select_start} till {select_end}")#"""### END FILTERING ###def run_multi_processing_threading():ds.map(get_diff_multi_threaded_processed, num_proc=NUM_PROC, batch_size=NUM_THREADS, batched=True).to_json("mpt.jsonl")#NUM_TRIALS = 1#print(f"Timing multithreading + multiprocessing using {NUM_THREADS} threads and {NUM_PROC} processes")#time = timeit.timeit(stmt=run_multi_processing_threading, number=NUM_TRIALS)#print("Time:", time)#with open("mpt.txt", "w") as f:# f.write(str(time))run_multi_processing_threading()
multi_threaded_processed.py+31-27