Commit ·
10de591
1
Parent(s): f0abe0a
Update multi_threaded_processed.py
Browse files35 unmodified lines363738394041422 unmodified lines4546474849505152535455565758591 unmodified line616263646566674 unmodified lines727374757677787980819 unmodified lines919293949596979810 unmodified lines1091101111121131141151164 unmodified lines1211221231241251261271281291301311321331341351361371381391401411421431446 unmodified lines1511521531541551561571 unmodified line15916016116216316416516616716816917017117217317417535 unmodified lines# 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')completed = run_in_shell("git checkout FETCH_HEAD -- " + new_file, cwd=cwd)2 unmodified lines# 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)def get_diff(ex):commit_id = ex["commit"]repos = list(set(ex["repos"].split(",")))old_file = ex["old_file"]new_file = ex["new_file"]for i, repo in enumerate(repos):repo = "https://xxx:xxx@github.com/" + repo + ".git"# Create a random directory to store the repo1 unmodified line# Can take very long when running many processesrun_in_shell("mkdir " + random_dir, timeout=300)try: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 nowhere4 unmodified linesrun_in_shell("rm -rf " + random_dir) # clean up againex["new_contents"] = new_contentsex["old_contents"] = old_contentsreturn ex# If no repo workedex["new_contents"] = ""ex["old_contents"] = ""return exdef get_diff_multi_threaded_processed(batch):9 unmodified lines### OPTIONAL FILTERING ####"""java = tuple([".java"])javascript = tuple([".js","._js",".bones",10 unmodified lines".ssjs",".xsjs",".xsjslib"])python = tuple([".py",".bzl",".gyp",4 unmodified lines".pyw",".tac",".wsgi",".xpy",])import jsonwith open("programming-languages.json", "r") as f:extensions = json.load(f)suffices = tuple([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_msg6 unmodified lines#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_6081 unmodified lineselect_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(f"diffs_{select_start}_{select_end}.jsonl", num_proc=NUM_PROC)#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()35 unmodified lines363738394041422 unmodified lines454647484950515253545556575859606162631 unmodified line656667686970714 unmodified lines76777879808182838485868788899 unmodified lines9910010110210310410510610 unmodified lines1171181191201211221231244 unmodified lines1291301311321331341351361371381391401411421431441451461471481491501511526 unmodified lines1591601611621631641651 unmodified line16716816917017117217317417517617717817918018118218318418518635 unmodified lines# If it requires authenticationif completed.returncode != 0:#print("ERRORC1", completed)return ("", "", completed.returncode, completed.stderr.decode(errors='ignore'))# 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')completed = run_in_shell("git checkout FETCH_HEAD -- " + new_file, cwd=cwd)2 unmodified lines# If there's only a new file, but no old fileif completed.returncode != 0:#print("ERRORC2", completed)return (new_contents, "", completed.returncode, completed.stderr.decode(errors='ignore'))old_contents = run_in_shell("cat " + old_file, cwd=cwd).stdout.decode(errors='ignore')return (new_contents, old_contents, completed.returncode, completed.stderr.decode(errors='ignore'))def get_diff(ex):commit_id = ex["commit"]repos = list(set(ex["repos"].split(",")))old_file = ex["old_file"]new_file = ex["new_file"]# Initializereturncode = 0stderr = "unknown"for i, repo in enumerate(repos):repo = "https://xxx:xxx@github.com/" + repo + ".git"# Create a random directory to store the repo1 unmodified line# Can take very long when running many processesrun_in_shell("mkdir " + random_dir, timeout=300)try:new_contents, old_contents, returncode, stderr = 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 nowhere4 unmodified linesrun_in_shell("rm -rf " + random_dir) # clean up againex["new_contents"] = new_contentsex["old_contents"] = old_contentsex["returncode"] = returncodeex["stderr"] = stderrreturn ex# If no repo workedex["new_contents"] = ""ex["old_contents"] = ""ex["returncode"] = returncodeex["stderr"] = stderrreturn exdef get_diff_multi_threaded_processed(batch):9 unmodified lines### OPTIONAL FILTERING ####"""java = [".java"]javascript = [".js","._js",".bones",10 unmodified lines".ssjs",".xsjs",".xsjslib"]python = [".py",".bzl",".gyp",4 unmodified lines".pyw",".tac",".wsgi",".xpy"]import jsonwith open("programming-languages.json", "r") as f:extensions = json.load(f)suffices = tuple([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_msg6 unmodified lines#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_6081 unmodified lineselect_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(f"diffs_{select_start}_{select_end}.jsonl", num_proc=NUM_PROC)# Benchmarking#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))# Runningrun_multi_processing_threading()
multi_threaded_processed.py+30-19