id
int64
0
190k
prompt
stringlengths
21
13.4M
docstring
stringlengths
1
12k
17,101
def check_id(data, task_id): assert data[task_id]["task_id"] == f"HumanEval/{task_id}" def fix(data): check_id(data, 116) data[116]["contract"] = ( '\n assert isinstance(arr, list), "invalid inputs" # $_CONTRACT_$' + '\n assert all(isinstance(x, int) and x >= 0 for x in arr), "invalid inputs" # $_CONTRACT_$\n' ) data[116]["plus_input"] = [ l for l in data[116]["plus_input"] if isinstance(l[0], list) and all(isinstance(x, int) and x >= 0 for x in l[0]) ] return data
null
17,102
import json import os import pickle from os import PathLike from typing import List import matplotlib.pyplot as plt import numpy as np from tqdm import tqdm from evalplus.data import get_human_eval_plus from evalplus.eval import estimate_pass_at_k def passk_rel_drop(task2bvs_old, task2bvs_new): # old_rate: # dim0: problems # dim1: each experiment (model@temperature) # dim2: pass/fail booleans for each sample # this fn computes the relative drop in pass@k averaged over experiments passk_old = {} passk_new = {} # sample size => k => List[pass@k] for exp_i in range(len(task2bvs_old[0])): ntotal = [] npass_old = [] npass_new = [] nsamples = None for task_i in range(len(task2bvs_old)): bv_old = task2bvs_old[task_i][exp_i] bv_new = task2bvs_new[task_i][exp_i] ntotal.append(len(bv_old)) npass_old.append(bv_old.sum()) npass_new.append(bv_new.sum()) if nsamples is None: nsamples = len(bv_old) assert len(bv_old) == len(bv_new) == nsamples d_old = passk_old.setdefault(nsamples, {}) d_new = passk_new.setdefault(nsamples, {}) for k in [1, 10, 100]: if nsamples >= k: pass_at_k_old = estimate_pass_at_k(ntotal, npass_old, k).mean() * 100 pass_at_k_new = estimate_pass_at_k(ntotal, npass_new, k).mean() * 100 d_old.setdefault(k, []).append(pass_at_k_old) d_new.setdefault(k, []).append(pass_at_k_new) for nsamples in passk_old: print("=====================================") print(f"{nsamples = }") do = passk_old[nsamples] dn = passk_new[nsamples] drops = [] for k in [1, 10, 100]: if k in do: pko = np.array(do[k]) pkn = np.array(dn[k]) drop = 100 * (pko - pkn) / pko drops.append(drop) print( f"pass@{k}: \t{pko.mean():.1f}% -> {pkn.mean():.1f}% (drop {drop.mean():.1f}%)" ) drops = np.array(drops) print(f"+++ {drops.mean() = :.1f}%") print(f"+++ {drops.max() = :.1f}%") print("=====================================")
null
17,103
import json import os import pickle from os import PathLike from typing import List import matplotlib.pyplot as plt import numpy as np from tqdm import tqdm from evalplus.data import get_human_eval_plus from evalplus.eval import estimate_pass_at_k SUCCESS = "success" def get_data(paths: List[PathLike]): task2bvs_old = None task2bvs_new = None for path in tqdm(paths): # each experiment res = json.load(open(path, "r"))["eval"] ntask = len(res) assert ntask == 164 if task2bvs_old is None and task2bvs_new is None: task2bvs_old = [[] for _ in range(ntask)] task2bvs_new = [[] for _ in range(ntask)] # i-th => task-i pass rate for an experiment for i, v in enumerate(res.values()): # each task base = v["base"] plus = v["plus"] bbv = np.array([s == SUCCESS for s, _ in base]) pbv = np.array([s == SUCCESS for s, _ in plus]) & bbv assert bbv.mean() >= pbv.mean() task2bvs_old[i].append(bbv) task2bvs_new[i].append(pbv) assert len(task2bvs_old) == len(task2bvs_new) return task2bvs_old, task2bvs_new
null
17,104
import argparse import json import os from typing import Any, Dict, List from rich.progress import track from evalplus.eval.utils import swallow_io from evalplus.evaluate import evaluate from tools.tsr.utils import ( clean, execute_cmd, get_cmd_output, get_problems, get_task_ids, to_path, ) def get_problems(dataset: str): return globals()[f"{dataset}_problems"] def get_task_ids(dataset: str) -> List[str]: problems = globals()[f"{dataset}_problems"] return list(problems.keys()) def to_path(task_id: str) -> str: return task_id.replace("/", "_") def clean(file_path: str): if os.path.exists(file_path): os.remove(file_path) def execute_cmd(cmd: list): os.system(" ".join(cmd)) def get_cmd_output(cmd_list: list) -> str: return subprocess.run(cmd_list, stdout=subprocess.PIPE, check=True).stdout.decode() def prepare_mutants(mutation_dir: str, dataset: str): pwd = os.getcwd() task_ids = get_task_ids(dataset) problems = get_problems(dataset) os.makedirs(mutation_dir, exist_ok=True) for task_id in track(task_ids, "Generating mutants"): task_dir = os.path.join(mutation_dir, to_path(task_id)) os.makedirs(task_dir, exist_ok=True) if any(map(lambda filename: filename.startswith("m"), os.listdir(task_dir))): # already have mutants continue # Make groundtruth groundtruth_code = ( problems[task_id]["prompt"] + problems[task_id]["canonical_solution"] ) with open(os.path.join(task_dir, "gt.py"), "w") as f: f.write(groundtruth_code) # Make dummy pytest with open(os.path.join(task_dir, "test_dummy.py"), "w") as f: f.write("def test_dummy():\n pass") # Use mutmut to generate mutants os.chdir(task_dir) clean(".mutmut-cache") execute_cmd(["mutmut run", "--paths-to-mutate=gt.py", "1>/dev/null"]) try: # Collect metainfo total_mutants = int( get_cmd_output(["mutmut", "results"]).split("\n")[-2].split("-")[-1] ) except: total_mutants = 0 # Dump mutants for i in range(1, total_mutants + 1): execute_cmd(["cp", "gt.py", "gt_copy.py"]) execute_cmd(["mutmut", "apply", str(i)]) execute_cmd(["mv", "gt.py", f"m{i}.py"]) execute_cmd(["mv", "gt_copy.py", "gt.py"]) # Remove gt and dummy pytest execute_cmd(["rm", "gt.py"]) execute_cmd(["rm", "test_dummy.py"]) clean(".mutmut-cache") os.chdir(pwd)
null
17,105
import argparse import json import os from typing import Any, Dict, List from rich.progress import track from evalplus.eval.utils import swallow_io from evalplus.evaluate import evaluate from tools.tsr.utils import ( clean, execute_cmd, get_cmd_output, get_problems, get_task_ids, to_path, ) def mutants_eval(mutation_dir: str, dataset: str): args = argparse.Namespace( dataset=dataset, samples=mutation_dir, base_only=False, parallel=None, i_just_wanna_run=False, test_details=True, min_time_limit=0.2, gt_time_limit_factor=4.0, mini=False, ) print("Evaluating mutants... ", end="", flush=True) with swallow_io(): evaluate(args) print("Done")
null
17,106
import argparse import json import os import pickle from concurrent.futures import ProcessPoolExecutor, as_completed from copy import deepcopy from typing import Any, Dict, List, Optional, Tuple from rich.progress import track from evalplus.data import write_jsonl from tools.tsr.coverage_init import collect_coverage_info from tools.tsr.mutation_init import collect_mutation_info from tools.tsr.sample_init import collect_sample_info from tools.tsr.utils import get_problems, get_task_ids, to_path def get_problems(dataset: str): def get_task_ids(dataset: str) -> List[str]: def global_util_init(dataset: str): global problems global task_ids global problem_count problems = get_problems(dataset) task_ids = get_task_ids(dataset) problem_count = len(problems)
null
17,107
import argparse import json import os import pickle from concurrent.futures import ProcessPoolExecutor, as_completed from copy import deepcopy from typing import Any, Dict, List, Optional, Tuple from rich.progress import track from evalplus.data import write_jsonl from tools.tsr.coverage_init import collect_coverage_info from tools.tsr.mutation_init import collect_mutation_info from tools.tsr.sample_init import collect_sample_info from tools.tsr.utils import get_problems, get_task_ids, to_path def merge_set_cover(*args) -> Dict[str, List[str]]: merged_set_cover = {task_id: [] for task_id in task_ids} for set_cover_dict in args: for task_id, plus_tests in set_cover_dict.items(): for plus_test in plus_tests: if plus_test not in merged_set_cover[task_id]: merged_set_cover[task_id].append(plus_test) return merged_set_cover
null
17,108
import argparse import json import os import pickle from concurrent.futures import ProcessPoolExecutor, as_completed from copy import deepcopy from typing import Any, Dict, List, Optional, Tuple from rich.progress import track from evalplus.data import write_jsonl from tools.tsr.coverage_init import collect_coverage_info from tools.tsr.mutation_init import collect_mutation_info from tools.tsr.sample_init import collect_sample_info from tools.tsr.utils import get_problems, get_task_ids, to_path def parallel_greedy_cover( info_dict: Optional[Dict[str, Dict[str, List[Any]]]], exclude_model: str, type: str, **kwargs, ) -> Dict[str, List[str]]: plus_tests = {task_id: [] for task_id in task_ids} with ProcessPoolExecutor(max_workers=32) as executor: futures = [] for task_id in task_ids: if type == "sample": path_task_id = to_path(task_id) sample_dir = kwargs["sample_dir"] with open(os.path.join(sample_dir, f"{path_task_id}.pkl"), "rb") as f: td = pickle.load(f) args = (task_id, td, exclude_model) else: args = (task_id, info_dict[task_id], exclude_model) futures.append(executor.submit(greedy_cover, *args)) for future in track(as_completed(futures), f"min set cover :: {type}"): task_id, min_cover = future.result() plus_tests[task_id] = min_cover return plus_tests def collect_coverage_info(coverage_dir: str, dataset: str) -> Dict[str, Dict[str, Any]]: os.makedirs(coverage_dir, exist_ok=True) problems = get_problems(dataset) task_ids = get_task_ids(dataset) coverage_info = {task_id: {} for task_id in task_ids} for task_id in track(task_ids, description="Testing gt coverage..."): coverage_cache_path = os.path.join(coverage_dir, f"{to_path(task_id)}.pkl") if os.path.isfile(coverage_cache_path): with open(coverage_cache_path, "rb") as f: coverage_info[task_id] = pickle.load(f) continue groundtruth_code = ( problems[task_id]["prompt"] + problems[task_id]["canonical_solution"] ) plus_tests = problems[task_id]["plus_input"] entry_point = problems[task_id]["entry_point"] for i, plus_test in enumerate(plus_tests): per, branch, branch_covered = test_code_coverage( to_path(task_id), groundtruth_code, [plus_test], entry_point ) test_id = f"plus_{i}" coverage_info[task_id].setdefault(test_id, []).extend( [(br, "gt") for br in branch_covered] ) with open(coverage_cache_path, "wb") as f: pickle.dump(coverage_info[task_id], f) return coverage_info def get_coverage_set_cover( coverage_dir: str, exclude_model: str, dataset: str ) -> Dict[str, List[str]]: coverage_info_dict = collect_coverage_info(coverage_dir, dataset) return parallel_greedy_cover(coverage_info_dict, exclude_model, "coverage")
null
17,109
import argparse import json import os import pickle from concurrent.futures import ProcessPoolExecutor, as_completed from copy import deepcopy from typing import Any, Dict, List, Optional, Tuple from rich.progress import track from evalplus.data import write_jsonl from tools.tsr.coverage_init import collect_coverage_info from tools.tsr.mutation_init import collect_mutation_info from tools.tsr.sample_init import collect_sample_info from tools.tsr.utils import get_problems, get_task_ids, to_path def parallel_greedy_cover( info_dict: Optional[Dict[str, Dict[str, List[Any]]]], exclude_model: str, type: str, **kwargs, ) -> Dict[str, List[str]]: plus_tests = {task_id: [] for task_id in task_ids} with ProcessPoolExecutor(max_workers=32) as executor: futures = [] for task_id in task_ids: if type == "sample": path_task_id = to_path(task_id) sample_dir = kwargs["sample_dir"] with open(os.path.join(sample_dir, f"{path_task_id}.pkl"), "rb") as f: td = pickle.load(f) args = (task_id, td, exclude_model) else: args = (task_id, info_dict[task_id], exclude_model) futures.append(executor.submit(greedy_cover, *args)) for future in track(as_completed(futures), f"min set cover :: {type}"): task_id, min_cover = future.result() plus_tests[task_id] = min_cover return plus_tests def collect_mutation_info( eval_path: str, dataset: str ) -> Dict[str, Dict[str, List[Any]]]: mutation_info = {task_id: {} for task_id in get_task_ids(dataset)} assert os.path.isfile( eval_path ), f"mutation testing result file {eval_path} missing!" eval_res = json.load(open(eval_path, "r"))["eval"] for task_id, v in eval_res.items(): for i_code, (status, res_list) in enumerate(v["plus"]): if status == "success": continue for i_test, res in enumerate(res_list): test_id = f"plus_{i_test}" if res == False: mutation_info[task_id].setdefault(test_id, []).append( ("mutant", i_code) ) return mutation_info def get_mutation_set_cover( mutation_dir: str, exclude_model: str, dataset: str ) -> Dict[str, List[str]]: mutation_info_dict = collect_mutation_info( os.path.join(mutation_dir, "eval_results.json"), dataset ) return parallel_greedy_cover(mutation_info_dict, exclude_model, "mutation")
null
17,110
import argparse import json import os import pickle from concurrent.futures import ProcessPoolExecutor, as_completed from copy import deepcopy from typing import Any, Dict, List, Optional, Tuple from rich.progress import track from evalplus.data import write_jsonl from tools.tsr.coverage_init import collect_coverage_info from tools.tsr.mutation_init import collect_mutation_info from tools.tsr.sample_init import collect_sample_info from tools.tsr.utils import get_problems, get_task_ids, to_path def parallel_greedy_cover( info_dict: Optional[Dict[str, Dict[str, List[Any]]]], exclude_model: str, type: str, **kwargs, ) -> Dict[str, List[str]]: def collect_sample_info(sample_dir: str, sample_eval_dir: str, dataset: str): def get_sample_set_cover( sample_dir: str, sample_eval_dir: str, exclude_model: str, dataset: str ) -> Dict[str, List[str]]: collect_sample_info(sample_dir, sample_eval_dir, dataset) return parallel_greedy_cover(None, exclude_model, "sample", sample_dir=sample_dir)
null
17,111
import argparse import json import os import pickle from concurrent.futures import ProcessPoolExecutor, as_completed from copy import deepcopy from typing import Any, Dict, List, Optional, Tuple from rich.progress import track from evalplus.data import write_jsonl from tools.tsr.coverage_init import collect_coverage_info from tools.tsr.mutation_init import collect_mutation_info from tools.tsr.sample_init import collect_sample_info from tools.tsr.utils import get_problems, get_task_ids, to_path def compute_avg_test(set_cover_info: Dict[str, List[str]]) -> float: sum_tests = sum( len(problems[task_id]["base_input"]) + len(set_cover_info[task_id]) for task_id in task_ids ) return sum_tests / problem_count def gen_report(set_cover_info: Dict[str, List[str]], sample_eval_dir: str, model: str): tsr_dict = {"ntests": compute_avg_test(set_cover_info), "pass@1": 0} model_path = os.path.join(sample_eval_dir, f"{model}_temp_0.0", "eval_results.json") with open(model_path, "r") as f: mdict = json.load(f) correct_cnt = 0 for task_id in task_ids: legacy_task_id = task_id if legacy_task_id not in mdict["eval"]: legacy_task_id = legacy_task_id.replace("/", "_") if mdict["eval"][legacy_task_id]["base"][0][0] != "success": continue correct = True for plus_id in set_cover_info[task_id]: index = int(plus_id.split("_")[-1]) if mdict["eval"][legacy_task_id]["plus"][0][1][index] == False: correct = False break if correct: correct_cnt += 1 tsr_dict["pass@1"] = correct_cnt / problem_count return tsr_dict
null
17,112
import argparse import json import os import pickle from concurrent.futures import ProcessPoolExecutor, as_completed from copy import deepcopy from typing import Any, Dict, List, Optional, Tuple from rich.progress import track from evalplus.data import write_jsonl from tools.tsr.coverage_init import collect_coverage_info from tools.tsr.mutation_init import collect_mutation_info from tools.tsr.sample_init import collect_sample_info from tools.tsr.utils import get_problems, get_task_ids, to_path def dump_humaneval_plus_mini(set_cover_info: Dict[str, List[str]], mini_path: str): new_problems = [] for task_id in task_ids: otask = problems[task_id] task = { "task_id": task_id, "prompt": otask["prompt"], "contract": otask["contract"], "canonical_solution": otask["canonical_solution"], "entry_point": otask["entry_point"], "base_input": otask["base_input"], "plus_input": [], "atol": otask["atol"], } for plus_test in set_cover_info[task_id]: index = int(plus_test.split("_")[-1]) task["plus_input"].append(otask["plus_input"][index]) new_problems.append(deepcopy(task)) write_jsonl(os.path.join(mini_path, "HumanEvalPlus-Mini.jsonl"), new_problems)
null
17,113
import argparse import json import os import numpy as np from termcolor import cprint from evalplus.eval import estimate_pass_at_k def analyze_resfile(resfile): before_summary = {} after_summary = {} res = json.load(open(resfile))["eval"] total = [] before_pass = [] after_pass = [] for v in res.values(): total.append(v["nfiles"]) bc = sum([r[0] == SUCCESS for r in v["base"]]) before_pass.append(bc) if v["plus"]: after_pass.append( sum( [ v["plus"][i][0] == v["base"][i][0] == SUCCESS for i in range(len(v["plus"])) ] ) ) total = np.array(total) before_pass = np.array(before_pass) after_pass = np.array(after_pass) for k in [1, 10, 100]: if total.min() >= k: pass_at_k = estimate_pass_at_k(total, before_pass, k).mean() before_summary[f"pass@{k}"] = pass_at_k * 100 # scale to % for k in [1, 10, 100]: if total.min() >= k: pass_at_k = estimate_pass_at_k(total, after_pass, k).mean() after_summary[f"pass@{k}"] = pass_at_k * 100 return before_summary, after_summary
null
17,114
import argparse import json import os import numpy as np from termcolor import cprint from evalplus.eval import estimate_pass_at_k def align_ampersands(str1, str2): """ This function takes two strings containing various "&" characters and transforms them so that the indices of "&" are aligned. This is useful for formatting LaTeX tables. Args: str1 (str): First input string containing "&" characters. str2 (str): Second input string containing "&" characters. Returns: Tuple[str, str]: Two transformed strings with aligned "&" indices. """ # Find indices of "&" characters in both strings amp_idx1 = [i for i, char in enumerate(str1) if char == "&"] amp_idx2 = [i for i, char in enumerate(str2) if char == "&"] assert len(amp_idx1) == len(amp_idx2) # Replace "&" characters in the strings with "\&" at the aligned indices acc1, acc2 = 0, 0 for i, j in zip(amp_idx1, amp_idx2): diff = (j + acc2) - (i + acc1) if diff > 0: str1 = str1[: i + acc1] + " " * diff + str1[i + acc1 :] acc1 += diff elif diff < 0: str2 = str2[: j + acc2] + " " * (-diff) + str2[j + acc2 :] acc2 -= diff return str1, str2 def texprint(before_summary, after_summary, bfgreedy, afgreedy): TEXTTEMPS = [r"\temptwo{}", r"\tempfour{}", r"\tempsix{}", r"\tempeight{}"] def aplus(s) -> str: return r"\aplus{" + s + r"}" def make_line(summary, amax, ap=False): pkvals = [f"{v[amax[i]]:.1f}" for i, v in enumerate(summary.values())] if ap: pkvals = [aplus(v) for v in pkvals] return ( " & ".join(pkvals) + " & " + " & ".join([TEXTTEMPS[i] for i in amax]) + r" \\" ) print("======== LaTeX Table Ingredent ========") argmax = [np.argmax(v) for v in before_summary.values()] text1 = "base & " if bfgreedy is not None: text1 += f"{bfgreedy:.1f} & " argmax = [np.argmax(v) for v in after_summary.values()] text1 += make_line(before_summary, argmax) text2 = "\\aplus{+extra} & " if afgreedy is not None: text2 += aplus(f"{afgreedy:.1f}") + " & " text2 += make_line(after_summary, argmax, ap=True) text1, text2 = align_ampersands(text1, text2) cprint(text1, "green") cprint(text2, "green")
null
17,115
import argparse import json import os import numpy as np from termcolor import cprint from evalplus.eval import estimate_pass_at_k TEMPS = [0.2, 0.4, 0.6, 0.8] def rich_print(before_summary, after_summary, bfgreedy, afgreedy): from rich.console import Console from rich.table import Table console = Console() table = Table(show_header=True, header_style="bold magenta", title="pass@k results") before_row = [] after_row = [] table.add_column(" ", style="dim", no_wrap=True) if bfgreedy is not None: assert afgreedy is not None table.add_column("Greedy pass@1", justify="right", style="bold green") before_row.append(f"{bfgreedy:.1f}") after_row.append(f"{afgreedy:.1f}") for k in before_summary: table.add_column(k, justify="right", style="bold magenta") table.add_column("Tbest.", justify="right") amax_before = np.argmax(before_summary[k]) amax_after = np.argmax(after_summary[k]) before_row.append(f"{before_summary[k][amax_before]:.1f}") before_row.append(f"{TEMPS[amax_before]}") after_row.append(f"{after_summary[k][amax_after]:.1f}") after_row.append(f"{TEMPS[amax_after]}") table.add_row("Before", *before_row) table.add_row("After", *after_row) console.print(table)
null
17,116
import json import os from evalplus.data import get_human_eval_plus from evalplus.gen.util import trusted_exec def execute(code, input_list) -> bool: try: trusted_exec(code, [input_list], entry_point) except Exception as e: assert str(e) == "invalid inputs" return False return True
null
17,117
import json import os from evalplus.data import get_human_eval_plus from evalplus.gen.util import trusted_exec def write(new_input_dict): with open(new_input_path, "a") as f: f.write(json.dumps(new_input_dict) + "\n")
null
17,118
import ast import re import traceback from typing import List, Optional def syntax_check(code, verbose=False): try: ast.parse(code) return True except (SyntaxError, MemoryError): if verbose: traceback.print_exc() return False def remove_unindented_lines(code, protect_before, execeptions, trim_tails): lines = code.splitlines() cut_idx = [] cut_enabled = False for i, line in enumerate(lines): if not cut_enabled and line.startswith(protect_before): cut_enabled = True continue if line.strip() == "": continue if any(line.startswith(e) for e in execeptions): continue lspace = len(line) - len(line.lstrip()) if lspace == 0: cut_idx.append(i) if any(line.rstrip().startswith(t) for t in trim_tails): # cut off everything behind cut_idx.extend(list(range(i, len(lines)))) break return "\n".join([line for i, line in enumerate(lines) if i not in cut_idx]) def to_four_space_indents(old_code): new_code = "" for line in old_code.splitlines(): lspace = len(line) - len(line.lstrip()) if lspace == 3: new_code += " " new_code += line + "\n" return new_code def sanitize( old_code: str, entry_point: str, rm_prefix_lines: Optional[str] = None, eofs: List = None, ): new_code = old_code if rm_prefix_lines is not None: new_code = "\n".join( [ line for line in old_code.splitlines() if not line.startswith(rm_prefix_lines) ] ) new_code = "\n" + new_code def_left = "def " + entry_point # basic handling of chat output new_code = new_code.replace("\n```python\n", "\n```\n") for chunk in new_code.split("\n```\n"): if def_left in chunk: new_code = chunk break chunks = [chunk for chunk in re.split(f"{def_left}\s*\(", new_code)] # TODO: having return does not mean this is complete bodies = [chunk for chunk in chunks[1:] if " return " in chunk.split("\ndef")[0]] def_left = def_left + "(" new_code = def_left + def_left.join(bodies) if len(bodies) > 0 else "" # fn + impl new_code = to_four_space_indents(new_code) for eof in eofs or []: new_code = new_code.split(eof)[0] # remove lines starting from the first unindented line after def_left new_code = remove_unindented_lines( new_code, protect_before=def_left, execeptions=["def ", "import ", "from "], trim_tails=['"""', "if", "print"], ) new_code = chunks[0] + new_code # cut all functions that are not syntactically correct && not the entry point parts = new_code.split("\ndef ") includes = [parts[0]] for fn in new_code.split("\ndef ")[1:]: if ( fn.strip().startswith(entry_point + " ") or fn.strip().startswith(entry_point + "(") or syntax_check("\ndef " + fn) ): includes.append(fn) new_code = "\ndef ".join(includes) return new_code.strip()
null
17,119
import gzip import json import os from os import PathLike from typing import Dict, Iterable import tempdir import wget from appdirs import user_cache_dir CACHE_DIR = user_cache_dir("evalplus") def get_dataset_metadata(name, version, mini): assert name in ["HumanEvalPlus", "MbppPlus"], f"Unknown/unsupported dataset: {name}" extra = "-Mini" if mini else "" url = f"https://github.com/ganler/release/releases/download/humanevalplus/{name}{extra}-{version}.jsonl.gz" cache_path = os.path.join(CACHE_DIR, f"{name}{extra}-{version}.jsonl") return url, cache_path
null
17,120
import gzip import json import os from os import PathLike from typing import Dict, Iterable import tempdir import wget from appdirs import user_cache_dir CACHE_DIR = user_cache_dir("evalplus") def make_cache(gzip_url, cache_path): # Check if human eval file exists in CACHE_DIR if not os.path.exists(cache_path): # Install HumanEval dataset and parse as jsonl print(f"Downloading dataset from {gzip_url}") with tempdir.TempDir() as tmpdir: plus_gz_path = os.path.join(tmpdir, f"data.jsonl.gz") wget.download(gzip_url, plus_gz_path) with gzip.open(plus_gz_path, "rb") as f: plus = f.read().decode("utf-8") # create CACHE_DIR if not exists if not os.path.exists(CACHE_DIR): os.makedirs(CACHE_DIR) # Write the original human eval file to CACHE_DIR with open(cache_path, "w") as f: f.write(plus)
null
17,121
import gzip import json import os from os import PathLike from typing import Dict, Iterable import tempdir import wget from appdirs import user_cache_dir The provided code snippet includes necessary dependencies for implementing the `write_jsonl` function. Write a Python function `def write_jsonl( filename: str, data: Iterable[Dict], append: bool = False, drop_builtin: bool = True )` to solve the following problem: Writes an iterable of dictionaries to jsonl Here is the function: def write_jsonl( filename: str, data: Iterable[Dict], append: bool = False, drop_builtin: bool = True ): """ Writes an iterable of dictionaries to jsonl """ if append: mode = "ab" else: mode = "wb" filename = os.path.expanduser(filename) if filename.endswith(".gz"): with open(filename, mode) as fp: with gzip.GzipFile(fileobj=fp, mode="wb") as gzfp: for x in data: if drop_builtin: x = {k: v for k, v in x.items() if not k.startswith("_")} gzfp.write((json.dumps(x) + "\n").encode("utf-8")) else: with open(filename, mode) as fp: for x in data: if drop_builtin: x = {k: v for k, v in x.items() if not k.startswith("_")} fp.write((json.dumps(x) + "\n").encode("utf-8"))
Writes an iterable of dictionaries to jsonl
17,122
import gzip import json import os from os import PathLike from typing import Dict, Iterable import tempdir import wget from appdirs import user_cache_dir def stream_jsonl(filename: str) -> Iterable[Dict]: """ Parses each jsonl line and yields it as a dictionary """ if filename.endswith(".gz"): with open(filename, "rb") as gzfp: with gzip.open(gzfp, "rt") as fp: for line in fp: if any(not x.isspace() for x in line): yield json.loads(line) else: with open(filename, "r") as fp: for line in fp: if any(not x.isspace() for x in line): yield json.loads(line) The provided code snippet includes necessary dependencies for implementing the `load_solutions` function. Write a Python function `def load_solutions(sample_path: PathLike) -> Iterable[Dict]` to solve the following problem: We accept two formats of inputs. + `sample.jsonl` which is the format from HumanEval, i.e., {task_id, completion}. + A folder which contains sub-folders named after the task_id. Each sub-folder contains samples named in `[?].py` where `?` is the solution id starting with 0. Different from `sample.jsonl`, the solutions must be complete (with prompt prefix). Here is the function: def load_solutions(sample_path: PathLike) -> Iterable[Dict]: """We accept two formats of inputs. + `sample.jsonl` which is the format from HumanEval, i.e., {task_id, completion}. + A folder which contains sub-folders named after the task_id. Each sub-folder contains samples named in `[?].py` where `?` is the solution id starting with 0. Different from `sample.jsonl`, the solutions must be complete (with prompt prefix). """ # if it is a file if os.path.isfile(sample_path): for i, sample in enumerate(stream_jsonl(sample_path)): sample["_identifier"] = sample["task_id"] + "_" + str(i) yield sample else: # if it is a folder for task_id in os.listdir(sample_path): task_path = os.path.join(sample_path, task_id) if not os.path.isdir(task_path): continue for solution_id in os.listdir(task_path): solution_path = os.path.join(task_path, solution_id) if os.path.isfile(solution_path) and solution_path.endswith(".py"): with open(solution_path, "r") as f: completion = f.read() yield { "_identifier": solution_path, "_path": solution_path, "task_id": task_id.replace("_", "/"), "solution": completion, }
We accept two formats of inputs. + `sample.jsonl` which is the format from HumanEval, i.e., {task_id, completion}. + A folder which contains sub-folders named after the task_id. Each sub-folder contains samples named in `[?].py` where `?` is the solution id starting with 0. Different from `sample.jsonl`, the solutions must be complete (with prompt prefix).
17,123
import gzip import json import os from os import PathLike from typing import Dict, Iterable import tempdir import wget from appdirs import user_cache_dir def write_directory(directory: PathLike, data: Iterable[Dict]): os.makedirs(directory, exist_ok=True) counters = {} for sample in data: assert "solution" in sample, "Samples must come with `solution` field!" task_id = sample["task_id"].replace("/", "_") task_dir = os.path.join(directory, task_id) os.makedirs(task_dir, exist_ok=True) if task_id not in counters: counters[task_id] = 0 sample_id = counters[task_id] with open(os.path.join(task_dir, f"{sample_id}.py"), "w") as f: f.write(sample["solution"]) counters[task_id] += 1
null
17,124
import gzip import json import os from os import PathLike from typing import Dict, Iterable import tempdir import wget from appdirs import user_cache_dir def completeness_check(name, plus): for task_id, task in plus.items(): for key in [ "prompt", "contract", "canonical_solution", "base_input", "plus_input", "atol", ]: assert key in task, f"{key} not found in {name} #{task_id}!"
null
17,125
import gzip import json import os from os import PathLike from typing import Dict, Iterable import tempdir import wget from appdirs import user_cache_dir def to_raw(string): return string.encode("unicode-escape").decode().replace("\\\\", "\\")
null
17,126
import hashlib import json import os from typing import Dict from evalplus.data.utils import ( CACHE_DIR, completeness_check, get_dataset_metadata, make_cache, stream_jsonl, ) def _ready_human_eval_plus_path(mini=False) -> str: if HUMANEVAL_OVERRIDE_PATH: return HUMANEVAL_OVERRIDE_PATH url, plus_path = get_dataset_metadata("HumanEvalPlus", HUMANEVAL_PLUS_VERSION, mini) make_cache(url, plus_path) return plus_path The provided code snippet includes necessary dependencies for implementing the `get_human_eval_plus_hash` function. Write a Python function `def get_human_eval_plus_hash() -> str` to solve the following problem: Get the hash of HumanEvalPlus. Returns: str: The hash of HumanEvalPlus Here is the function: def get_human_eval_plus_hash() -> str: """Get the hash of HumanEvalPlus. Returns: str: The hash of HumanEvalPlus """ plus_path = _ready_human_eval_plus_path() with open(plus_path, "rb") as f: plus = f.read() return hashlib.md5(plus).hexdigest()
Get the hash of HumanEvalPlus. Returns: str: The hash of HumanEvalPlus
17,127
import hashlib import json import os from typing import Dict from evalplus.data.utils import ( CACHE_DIR, completeness_check, get_dataset_metadata, make_cache, stream_jsonl, ) def _ready_human_eval_plus_path(mini=False) -> str: if HUMANEVAL_OVERRIDE_PATH: return HUMANEVAL_OVERRIDE_PATH url, plus_path = get_dataset_metadata("HumanEvalPlus", HUMANEVAL_PLUS_VERSION, mini) make_cache(url, plus_path) return plus_path The provided code snippet includes necessary dependencies for implementing the `get_human_eval_plus` function. Write a Python function `def get_human_eval_plus(err_incomplete=True, mini=False) -> Dict[str, Dict]` to solve the following problem: Get HumanEvalPlus locally. Args: err_incomplete (bool, optional): Whether to raise error if HumanEvalPlus is not complete. Defaults to True. mini (bool, optional): Whether to use the mini version of HumanEvalPlus. Defaults to False. Returns: List[Dict[str, str]]: List of dicts with keys "task_id", "prompt", "contract", "canonical_solution", "base_input" Notes: "task_id" is the identifier string for the task "prompt" is the function signature with docstring "contract" is the assertions for the function's input (validity) "canonical_solution" is the ground-truth implementation for diff-testing "base_input" is the test inputs from original HumanEval "plus_input" is the test inputs brought by EvalPlus "atol" is the absolute tolerance for diff-testing Here is the function: def get_human_eval_plus(err_incomplete=True, mini=False) -> Dict[str, Dict]: """Get HumanEvalPlus locally. Args: err_incomplete (bool, optional): Whether to raise error if HumanEvalPlus is not complete. Defaults to True. mini (bool, optional): Whether to use the mini version of HumanEvalPlus. Defaults to False. Returns: List[Dict[str, str]]: List of dicts with keys "task_id", "prompt", "contract", "canonical_solution", "base_input" Notes: "task_id" is the identifier string for the task "prompt" is the function signature with docstring "contract" is the assertions for the function's input (validity) "canonical_solution" is the ground-truth implementation for diff-testing "base_input" is the test inputs from original HumanEval "plus_input" is the test inputs brought by EvalPlus "atol" is the absolute tolerance for diff-testing """ plus_path = _ready_human_eval_plus_path(mini=mini) plus = {task["task_id"]: task for task in stream_jsonl(plus_path)} if err_incomplete: completeness_check("HumanEval+", plus) return plus
Get HumanEvalPlus locally. Args: err_incomplete (bool, optional): Whether to raise error if HumanEvalPlus is not complete. Defaults to True. mini (bool, optional): Whether to use the mini version of HumanEvalPlus. Defaults to False. Returns: List[Dict[str, str]]: List of dicts with keys "task_id", "prompt", "contract", "canonical_solution", "base_input" Notes: "task_id" is the identifier string for the task "prompt" is the function signature with docstring "contract" is the assertions for the function's input (validity) "canonical_solution" is the ground-truth implementation for diff-testing "base_input" is the test inputs from original HumanEval "plus_input" is the test inputs brought by EvalPlus "atol" is the absolute tolerance for diff-testing
17,128
import hashlib import json import os from typing import Dict from evalplus.data.utils import ( CACHE_DIR, completeness_check, get_dataset_metadata, make_cache, stream_jsonl, ) The provided code snippet includes necessary dependencies for implementing the `get_human_eval` function. Write a Python function `def get_human_eval() -> Dict[str, Dict]` to solve the following problem: Get HumanEval from OpenAI's github repo and return as a list of parsed dicts. Returns: List[Dict[str, str]]: List of dicts with keys "prompt", "test", "entry_point" Notes: "task_id" is the identifier string for the task. "prompt" is the prompt to be used for the task (function signature with docstrings). "test" is test-cases wrapped in a `check` function. "entry_point" is the name of the function. Here is the function: def get_human_eval() -> Dict[str, Dict]: """Get HumanEval from OpenAI's github repo and return as a list of parsed dicts. Returns: List[Dict[str, str]]: List of dicts with keys "prompt", "test", "entry_point" Notes: "task_id" is the identifier string for the task. "prompt" is the prompt to be used for the task (function signature with docstrings). "test" is test-cases wrapped in a `check` function. "entry_point" is the name of the function. """ # Check if human eval file exists in CACHE_DIR human_eval_path = os.path.join(CACHE_DIR, "HumanEval.jsonl") make_cache( "https://github.com/openai/human-eval/raw/master/data/HumanEval.jsonl.gz", human_eval_path, ) human_eval = open(human_eval_path, "r").read().split("\n") human_eval = [json.loads(line) for line in human_eval if line] # Handle 115_max_fill.py to make its docstring well-formed human_eval[115]["prompt"] = "import math\n" + human_eval[115]["prompt"].replace( "import math\n", "" ) return {task["task_id"]: task for task in human_eval}
Get HumanEval from OpenAI's github repo and return as a list of parsed dicts. Returns: List[Dict[str, str]]: List of dicts with keys "prompt", "test", "entry_point" Notes: "task_id" is the identifier string for the task. "prompt" is the prompt to be used for the task (function signature with docstrings). "test" is test-cases wrapped in a `check` function. "entry_point" is the name of the function.
17,129
import hashlib import json import os from typing import Dict import wget from evalplus.data.utils import ( CACHE_DIR, completeness_check, get_dataset_metadata, make_cache, stream_jsonl, ) def mbpp_serialize_inputs(task_id: str, inputs: list) -> list: task_id = int(task_id.split("/")[-1]) if task_id == 115: return [[[list(item) for item in inp[0]]] for inp in inputs] elif task_id == 124: return [(str(inp[0]), str(inp[1])) for inp in inputs] elif task_id == 252: return [[str(inp[0])] for inp in inputs] return inputs
null
17,130
import hashlib import json import os from typing import Dict import wget from evalplus.data.utils import ( CACHE_DIR, completeness_check, get_dataset_metadata, make_cache, stream_jsonl, ) The provided code snippet includes necessary dependencies for implementing the `get_mbpp` function. Write a Python function `def get_mbpp() -> Dict[str, Dict]` to solve the following problem: Get sanitized MBPP from Google's Github repo. Here is the function: def get_mbpp() -> Dict[str, Dict]: """Get sanitized MBPP from Google's Github repo.""" mbpp_path = os.path.join(CACHE_DIR, "sanitized-mbpp.json") if not os.path.exists(mbpp_path): os.makedirs(CACHE_DIR, exist_ok=True) # Install MBPP-sanitized from scratch print("Downloading original MBPP dataset...") wget.download( "https://github.com/google-research/google-research/raw/master/mbpp/sanitized-mbpp.json", mbpp_path, ) with open(mbpp_path, "r") as f: mbpp = json.load(f) return {str(task["task_id"]): task for task in mbpp}
Get sanitized MBPP from Google's Github repo.
17,131
import hashlib import json import os from typing import Dict import wget from evalplus.data.utils import ( CACHE_DIR, completeness_check, get_dataset_metadata, make_cache, stream_jsonl, ) def _ready_mbpp_plus_path(mini=False) -> str: assert mini is False, "Mini version of MBPP+ is not available yet." if MBPP_OVERRIDE_PATH: return MBPP_OVERRIDE_PATH url, plus_path = get_dataset_metadata("MbppPlus", MBPP_PLUS_VERSION, mini) make_cache(url, plus_path) return plus_path def mbpp_deserialize_inputs(task_id: str, inputs: list) -> list: task_id = int(task_id.split("/")[-1]) if task_id in [ 2, 116, 132, 143, 222, 261, 273, 394, 399, 421, 424, 429, 470, 560, 579, 596, 616, 630, 726, 740, 744, 809, ]: modified_inputs = [[tuple(lst) for lst in inp] for inp in inputs] elif task_id in [ 63, 64, 70, 94, 120, 237, 272, 299, 400, 409, 417, 438, 473, 614, 780, ]: modified_inputs = [ [[tuple(lst) for lst in lst_lst] for lst_lst in inp] for inp in inputs ] elif task_id in [75, 413, 444, 753]: modified_inputs = [ [[tuple(lst) for lst in inp[0]]] + [inp[1]] for inp in inputs ] elif task_id == 106 or task_id == 750: modified_inputs = [[inp[0]] + [tuple(inp[1])] for inp in inputs] elif task_id == 115: modified_inputs = [ [ [ set(item) if isinstance(item, list) and len(item) else {} for item in inp[0] ] ] for inp in inputs ] elif task_id == 124: modified_inputs = [(float(inp[0]), complex(inp[1])) for inp in inputs] elif task_id in [250, 405, 446, 617, 720, 763, 808]: modified_inputs = [[tuple(inp[0])] + [inp[1]] for inp in inputs] elif task_id in [259, 401, 445]: modified_inputs = [ [[tuple(lst) for lst in lst_lst] for lst_lst in inp] for inp in inputs ] modified_inputs = [[tuple(lst) for lst in inp] for inp in modified_inputs] elif task_id == 278: modified_inputs = [ [[tuple(item) if isinstance(item, list) else item for item in inp[0]]] for inp in inputs ] modified_inputs = [[tuple(lst) for lst in inp] for inp in modified_inputs] elif task_id == 307: modified_inputs = [[tuple(inp[0])] + [inp[1], inp[2]] for inp in inputs] elif task_id == 722: modified_inputs = [ [{key: tuple(value) for key, value in inp[0].items()}] + inp[1:] for inp in inputs ] elif task_id == 252: modified_inputs = [[complex(inp[0])] for inp in inputs] elif task_id in [580, 615, 791]: def turn_all_list_into_tuple(inp): if isinstance(inp, list): return tuple([turn_all_list_into_tuple(item) for item in inp]) return inp modified_inputs = [turn_all_list_into_tuple(inp) for inp in inputs] else: modified_inputs = inputs return modified_inputs def get_mbpp_plus(err_incomplete=True, mini=False) -> Dict[str, Dict]: plus_path = _ready_mbpp_plus_path() plus = {task["task_id"]: task for task in stream_jsonl(plus_path)} for task_id, task in plus.items(): task["base_input"] = mbpp_deserialize_inputs(task_id, task["base_input"]) task["plus_input"] = mbpp_deserialize_inputs(task_id, task["plus_input"]) if err_incomplete: completeness_check("MBPP+", plus) return plus
null
17,132
import hashlib import json import os from typing import Dict import wget from evalplus.data.utils import ( CACHE_DIR, completeness_check, get_dataset_metadata, make_cache, stream_jsonl, ) def _ready_mbpp_plus_path(mini=False) -> str: assert mini is False, "Mini version of MBPP+ is not available yet." if MBPP_OVERRIDE_PATH: return MBPP_OVERRIDE_PATH url, plus_path = get_dataset_metadata("MbppPlus", MBPP_PLUS_VERSION, mini) make_cache(url, plus_path) return plus_path The provided code snippet includes necessary dependencies for implementing the `get_mbpp_plus_hash` function. Write a Python function `def get_mbpp_plus_hash() -> str` to solve the following problem: Get the hash of MbppPlus. Returns: str: The hash of MbppPlus Here is the function: def get_mbpp_plus_hash() -> str: """Get the hash of MbppPlus. Returns: str: The hash of MbppPlus """ plus_path = _ready_mbpp_plus_path() with open(plus_path, "rb") as f: plus = f.read() return hashlib.md5(plus).hexdigest()
Get the hash of MbppPlus. Returns: str: The hash of MbppPlus
17,133
import math import multiprocessing import time from typing import Any, List, Union from evalplus.data import get_human_eval_plus from evalplus.eval.utils import ( TimeoutException, create_tempdir, reliability_guard, swallow_io, time_limit, ) MAX_WARMUP_LIMIT = 5 RUN_REPEAT = 25 def execute_for_runtime( code: str, inputs: List, warmups: List, entry_point: str ) -> Union[str, float]: def unsafe_execute(): with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil rmtree = shutil.rmtree rmdir = os.rmdir chdir = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # load functions exec_globals = {} exec(code, exec_globals) fn = exec_globals[entry_point] try: # warmup calls for warmup in warmups: with swallow_io(): fn(*warmup) start_time = time.time() # real call with swallow_io(): with time_limit(3): fn(*inputs) duration = time.time() - start_time result.append(duration) except TimeoutException: result.append("timed out") except BaseException as e: result.append("thrown exception") # Needed for cleaning up. shutil.rmtree = rmtree os.rmdir = rmdir os.chdir = chdir manager = multiprocessing.Manager() result = manager.list() p = multiprocessing.Process(target=unsafe_execute) p.start() p.join(timeout=3 + 1) if p.is_alive(): p.kill() return result[0] def test_solution_runtime( dataset: str = "humaneval", task_id: str = "HumanEval/0", impl: str = "canonical", inputs: Union[str, List[List[Any]]] = "base_input", ): if "humaneval" in dataset: problems, problem = get_human_eval_plus(), None for p in problems: if p["task_id"] == task_id: problem = p assert problem != None, f"invalid {task_id = }" entry_point = problem["entry_point"] impl = problem["prompt"] + ( impl if impl != "canonical" else problem["canonical_solution"] ) if inputs == "base_input": inputs = problem["base_input"] results = [1000, 1000] for input_list in inputs: # choose warmup input warmups = [] for base_input_list in problem["base_input"]: if ( hash(str(base_input_list)) != hash(str(input_list)) and len(warmups) < MAX_WARMUP_LIMIT ): warmups.append(base_input_list) runtime_list = [ execute_for_runtime(impl, input_list, warmups, entry_point) for _ in range(RUN_REPEAT) ] if any(type(x) != float for x in runtime_list): print(f"{task_id = } incorrect") return None, None avg_runtime = sum(runtime_list) / len(runtime_list) sd = math.sqrt( sum((runtime - avg_runtime) ** 2 for runtime in runtime_list) / (RUN_REPEAT - 1) ) if sd < results[1]: results[0] = avg_runtime results[1] = sd return results
null
17,134
import argparse import importlib import inspect import multiprocessing import os import sys from io import StringIO from typing import Any, Callable, List, Union import coverage from evalplus.data import get_human_eval_plus from evalplus.data.utils import to_raw from evalplus.eval.utils import reliability_guard, swallow_io, time_limit def test_code_coverage( code: str, inputs: List[List[Any]], entry_point: str, mode="branch" ): def safety_test(code: str, inputs: List[List[Any]], entry_point: str): for input_list in inputs: code += f"{entry_point}({construct_inputs_sig(input_list)})\n" reliability_guard() try: with swallow_io(): with time_limit(1): exec(code, {}) except: sys.exit(1) p = multiprocessing.Process(target=safety_test, args=(code, inputs, entry_point)) p.start() p.join() safe = p.exitcode == 0 if p.is_alive(): p.terminate() p.kill() if not safe: print("Potentially dangerous code, refuse coverage test.") return None with open("tmp_src.py", "w") as f: f.write(code) import tmp_src importlib.reload(tmp_src) func = getattr(tmp_src, f"{entry_point}", None) assert func != None, f"{entry_point = } not exist" cov = coverage.Coverage(branch=True) cov.start() with swallow_io(): for input_list in inputs: func(*input_list) cov.stop() with Capturing() as outputs: cov.lcov_report(outfile="-") ret = parse_lcov(outputs, func, mode) os.remove("tmp_src.py") return ret The provided code snippet includes necessary dependencies for implementing the `test_solution_coverage` function. Write a Python function `def test_solution_coverage( dataset: str = "HumanEvalPlus", task_id: str = "HumanEval/0", impl: str = "canonical", inputs: Union[str, List[List[Any]]] = "base_input", mode: str = "branch", )` to solve the following problem: Parameters: * dataset: {None, "HumanEval", "HumanEvalPlus"} * task_id: ralated to dataset * impl: {"canonical", source code} * inputs: {"base_inputs", list} * mode: {"branch"}, will support "line" for coverage-guided LLM test generation Here is the function: def test_solution_coverage( dataset: str = "HumanEvalPlus", task_id: str = "HumanEval/0", impl: str = "canonical", inputs: Union[str, List[List[Any]]] = "base_input", mode: str = "branch", ): """ Parameters: * dataset: {None, "HumanEval", "HumanEvalPlus"} * task_id: ralated to dataset * impl: {"canonical", source code} * inputs: {"base_inputs", list} * mode: {"branch"}, will support "line" for coverage-guided LLM test generation """ if "HumanEval" in dataset: problems, problem = get_human_eval_plus(), None for p in problems: if p["task_id"] == task_id: problem = p assert problem != None, f"invalid {task_id = }" entry_point = problem["entry_point"] code = problem["prompt"] + ( impl if impl != "canonical" else problem["canonical_solution"] ) if inputs == "base_input": inputs = problem["base_input"] else: raise NotImplementedError return test_code_coverage(code, inputs, entry_point, mode)
Parameters: * dataset: {None, "HumanEval", "HumanEvalPlus"} * task_id: ralated to dataset * impl: {"canonical", source code} * inputs: {"base_inputs", list} * mode: {"branch"}, will support "line" for coverage-guided LLM test generation
17,135
import signal import time from typing import Dict import openai from openai.types.chat import ChatCompletion def make_request( client: openai.Client, message: str, model: str, max_tokens: int = 512, temperature: float = 1, n: int = 1, **kwargs ) -> ChatCompletion: def handler(signum, frame): def make_auto_request(*args, **kwargs) -> ChatCompletion: ret = None while ret is None: try: signal.signal(signal.SIGALRM, handler) signal.alarm(100) ret = make_request(*args, **kwargs) signal.alarm(0) except openai.RateLimitError: print("Rate limit exceeded. Waiting...") signal.alarm(0) time.sleep(5) except openai.APIConnectionError: print("API connection error. Waiting...") signal.alarm(0) time.sleep(5) except openai.APIError as e: print(e) signal.alarm(0) except Exception as e: print("Unknown error. Waiting...") print(e) signal.alarm(0) time.sleep(1) return ret
null
17,136
import argparse import json import os from evalplus.data.mbpp import mbpp_serialize_inputs from evalplus.gen.chatgpt_gen import ChatGPTGen from evalplus.gen.type_mut import TypedMutGen class SetEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, set): return list(obj) return json.JSONEncoder.default(self, obj) def insert_contract_into_code(entry_point, code, contract): lines = code.split("\n") index = lines.index( next(line for line in lines if line.startswith(f"def {entry_point}")) ) lines.insert(index + 1, contract) return "\n".join(lines) def input_generation(args, problems): with open(args.output, "w") as file: for problem in problems.values(): new_input = {} task_id = problem["task_id"] print(f"generating inputs for {task_id} ...") # by default we do not include constraints in the prompt (code) code = problem["prompt"] + problem["canonical_solution"] # but we use c_code to include contract which checks input validity at execution time if args.dataset == "humaneval": c_code = ( problem["prompt"] + problem["contract"] + problem["canonical_solution"] ) elif args.dataset == "mbpp": c_code = problem["prompt"] + insert_contract_into_code( entry_point=problem["entry_point"], code=problem["canonical_solution"], contract=problem["contract"], ) # first generate chatgpt input_gen = ChatGPTGen( problem["base_input"], problem["entry_point"], c_code, code ).generate(args.chatgpt_len) # generate mutation next if input_gen is None or len(input_gen) == 0: new_input["task_id"] = task_id new_input["inputs"] = {} file.write(json.dumps(new_input, cls=SetEncoder) + "\n") continue input_gen.extend( TypedMutGen(input_gen, problem["entry_point"], c_code).generate( args.mut_len ) ) print(f"generated {len(input_gen)} inputs") new_input["task_id"] = task_id if args.dataset == "mbpp": new_input["inputs"] = mbpp_serialize_inputs(task_id, input_gen) new_input["inputs"] = input_gen file.write(json.dumps(new_input, cls=SetEncoder) + "\n")
null
17,137
import argparse import json import multiprocessing import os import pickle import threading import time from collections import Counter, defaultdict from concurrent.futures import ProcessPoolExecutor, as_completed from datetime import datetime from typing import Any, Dict, List, Optional, Tuple, Union from warnings import warn import numpy as np from termcolor import cprint from tqdm import tqdm from evalplus.data import ( get_human_eval_plus, get_human_eval_plus_hash, get_mbpp_plus, get_mbpp_plus_hash, load_solutions, ) from evalplus.data.utils import CACHE_DIR from evalplus.eval import ( SUCCESS, compatible_eval_result, estimate_pass_at_k, untrusted_check, ) from evalplus.eval._special_oracle import MBPP_OUTPUT_NOT_NONE_TASKS from evalplus.gen.util import trusted_exec def get_groundtruth(problems, hashcode, tasks_only_output_not_none): cache_file = os.path.join(CACHE_DIR, f"{hashcode}.pkl") if os.path.exists(cache_file): print(f"Load from ground-truth from {cache_file}") with open(cache_file, "rb") as f: return pickle.load(f) os.makedirs(CACHE_DIR, exist_ok=True) print("Computing expected output...") tbegin = time.time() expected_output = {} for task_id, problem in problems.items(): oracle = {} oracle["base"], oracle["base_time"] = trusted_exec( problem["prompt"] + problem["canonical_solution"], problem["base_input"], problem["entry_point"], record_time=True, output_not_none=problem["entry_point"] in tasks_only_output_not_none, ) oracle["plus"], oracle["plus_time"] = trusted_exec( problem["prompt"] + problem["canonical_solution"], problem["plus_input"], problem["entry_point"], record_time=True, output_not_none=problem["entry_point"] in tasks_only_output_not_none, ) expected_output[task_id] = oracle print(f"Expected outputs computed in {time.time() - tbegin:.2f}s") with open(cache_file, "wb") as f: pickle.dump(expected_output, f) return expected_output def check_correctness( dataset: str, completion_id: int, problem: Dict[str, Any], solution: str, expected_output: Dict[str, List], base_only=False, fast_check=False, identifier=None, min_time_limit: float = 0.1, gt_time_limit_factor: float = 2.0, ) -> Dict[str, Union[int, Optional[Result]]]: ret = { "completion_id": completion_id, "task_id": problem["task_id"], "_identifier": identifier, } ret["base"] = untrusted_check( dataset, solution, problem["base_input"], problem["entry_point"], expected=expected_output["base"], atol=problem["atol"], ref_time=expected_output["base_time"], fast_check=fast_check, min_time_limit=min_time_limit, gt_time_limit_factor=gt_time_limit_factor, ) if not base_only: ret["plus"] = untrusted_check( dataset, solution, problem["plus_input"], problem["entry_point"], expected=expected_output["plus"], atol=problem["atol"], ref_time=expected_output["plus_time"], fast_check=fast_check, min_time_limit=min_time_limit, gt_time_limit_factor=gt_time_limit_factor, ) return ret def evaluate(flags): if flags.parallel is None: n_workers = max(1, multiprocessing.cpu_count() // 2) else: n_workers = flags.parallel if os.path.isdir(flags.samples): result_path = os.path.join(flags.samples, "eval_results.json") else: assert flags.samples.endswith(".jsonl") result_path = flags.samples.replace(".jsonl", "_eval_results.json") if os.path.isfile(result_path) and not flags.i_just_wanna_run: print(f"Load from previous results from {result_path}") with open(result_path, "r") as f: results = json.load(f) results = compatible_eval_result(results) else: if flags.dataset == "humaneval": problems = get_human_eval_plus(mini=flags.mini) dataset_hash = get_human_eval_plus_hash() expected_output = get_groundtruth(problems, dataset_hash, []) elif flags.dataset == "mbpp": problems = get_mbpp_plus(mini=flags.mini) dataset_hash = get_mbpp_plus_hash() expected_output = get_groundtruth( problems, dataset_hash, MBPP_OUTPUT_NOT_NONE_TASKS, ) results = { "date": datetime.now().strftime("%Y-%m-%d %H:%M"), "hash": dataset_hash, "eval": {}, } with ProcessPoolExecutor(max_workers=n_workers) as executor: futures = [] completion_id = Counter() n_samples = 0 eval_results = defaultdict(list) remainings = set() print("Reading samples...") for sample in tqdm(load_solutions(flags.samples)): task_id = sample["task_id"] solution = ( sample["solution"] if "solution" in sample else problems[task_id]["prompt"] + sample["completion"] ) remainings.add(sample["_identifier"]) args = ( flags.dataset, completion_id[task_id], problems[task_id], solution, expected_output[task_id], flags.base_only, not flags.test_details, # fast_check sample["_identifier"], flags.min_time_limit, flags.gt_time_limit_factor, ) futures.append(executor.submit(check_correctness, *args)) completion_id[task_id] += 1 n_samples += 1 assert n_samples == len(remainings), "Missing problems in unfinished" assert len(completion_id) == len(problems), "Missing problems in samples" def stucking_checker(): while remainings: last_size = len(remainings) time.sleep(20) if last_size != len(remainings) or len(remainings) == 0: continue # Potential stucking warn("No samples had finished testing in the last 20s") warn(f"{len(remainings)} samples to be tested: {remainings}") threading.Thread(target=stucking_checker).start() for future in tqdm(as_completed(futures), total=n_samples): result = future.result() remainings.remove(result["_identifier"]) eval_results[result["task_id"]].append(result) # sort the results for each problem by completion_id for task_id, task_results in eval_results.items(): task_results.sort(key=lambda x: x["completion_id"]) results["eval"][task_id] = { "nfiles": len(task_results), "base": [x["base"] for x in task_results], "plus": [x["plus"] for x in task_results] if not flags.base_only else [], } if os.path.isfile(result_path) and flags.i_just_wanna_run: decision = "" # while decision.lower() not in ["y", "n"]: # print(f"{result_path} already exists. Press [Y/N] to overwrite or exit...") # decision = input() # if decision.lower() == "y": # mv the file to a backup new_path = result_path + ".bak" while os.path.isfile(new_path): new_path += ".bak" os.rename(result_path, new_path) print(f"Backup {result_path} to {new_path}") if not os.path.isfile(result_path): with open(result_path, "w") as f: json.dump(results, f) # Calculate pass@k. total = np.array([r["nfiles"] for r in results["eval"].values()]) base_correct = [] new_correct = [] for res in results["eval"].values(): bc = sum([r[0] == SUCCESS for r in res["base"]]) base_correct.append(bc) if res["plus"]: new_correct.append( sum( [ res["plus"][i][0] == res["base"][i][0] == SUCCESS for i in range(len(res["plus"])) ] ) ) base_correct = np.array(base_correct) pass_at_k = { f"pass@{k}": estimate_pass_at_k(total, base_correct, k).mean() for k in [1, 10, 100] if total.min() >= k } cprint(f"{flags.dataset} (base tests)", "red") for k, v in pass_at_k.items(): cprint(f"{k}:\t{v:.3f}", "red") if new_correct: cprint(f"{flags.dataset}+ (base + extra tests)", "green") pass_at_k = { f"pass@{k}": estimate_pass_at_k(total, np.array(new_correct), k).mean() for k in [1, 10, 100] if (total >= k).all() } for k, v in pass_at_k.items(): cprint(f"{k}:\t{v:.3f}", "green")
null
17,138
import contextlib import faulthandler import io import os import platform import signal import tempfile from typing import Optional class WriteOnlyStringIO(io.StringIO): """StringIO that throws an exception when it's read from""" def read(self, *args, **kwargs): raise IOError def readline(self, *args, **kwargs): raise IOError def readlines(self, *args, **kwargs): raise IOError def readable(self, *args, **kwargs): """Returns True if the IO object can be read.""" return False class redirect_stdin(contextlib._RedirectStream): # type: ignore _stream = "stdin" def swallow_io(): stream = WriteOnlyStringIO() with contextlib.redirect_stdout(stream): with contextlib.redirect_stderr(stream): with redirect_stdin(stream): yield
null
17,139
import contextlib import faulthandler import io import os import platform import signal import tempfile from typing import Optional class TimeoutException(Exception): pass def time_limit(seconds: float): def signal_handler(signum, frame): raise TimeoutException("Timed out!") signal.setitimer(signal.ITIMER_REAL, seconds) signal.signal(signal.SIGALRM, signal_handler) try: yield finally: signal.setitimer(signal.ITIMER_REAL, 0)
null
17,140
import contextlib import faulthandler import io import os import platform import signal import tempfile from typing import Optional def chdir(root): if root == ".": yield return cwd = os.getcwd() os.chdir(root) try: yield except BaseException as exc: raise exc finally: os.chdir(cwd) def create_tempdir(): with tempfile.TemporaryDirectory() as dirname: with chdir(dirname): yield dirname
null
17,141
import contextlib import faulthandler import io import os import platform import signal import tempfile from typing import Optional def chdir(root): if root == ".": yield return cwd = os.getcwd() os.chdir(root) try: yield except BaseException as exc: raise exc finally: os.chdir(cwd) The provided code snippet includes necessary dependencies for implementing the `reliability_guard` function. Write a Python function `def reliability_guard(maximum_memory_bytes: Optional[int] = None)` to solve the following problem: This disables various destructive functions and prevents the generated code from interfering with the test (e.g. fork bomb, killing other processes, removing filesystem files, etc.) WARNING This function is NOT a security sandbox. Untrusted code, including, model- generated code, should not be blindly executed outside of one. See the Codex paper for more information about OpenAI's code sandbox, and proceed with caution. Here is the function: def reliability_guard(maximum_memory_bytes: Optional[int] = None): """ This disables various destructive functions and prevents the generated code from interfering with the test (e.g. fork bomb, killing other processes, removing filesystem files, etc.) WARNING This function is NOT a security sandbox. Untrusted code, including, model- generated code, should not be blindly executed outside of one. See the Codex paper for more information about OpenAI's code sandbox, and proceed with caution. """ if maximum_memory_bytes is not None: import resource resource.setrlimit( resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit( resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit( resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins builtins.exit = None builtins.quit = None import os os.environ["OMP_NUM_THREADS"] = "1" os.kill = None os.system = None os.putenv = None os.remove = None os.removedirs = None os.rmdir = None os.fchdir = None os.setuid = None os.fork = None os.forkpty = None os.killpg = None os.rename = None os.renames = None os.truncate = None os.replace = None os.unlink = None os.fchmod = None os.fchown = None os.chmod = None os.chown = None os.chroot = None os.fchdir = None os.lchflags = None os.lchmod = None os.lchown = None os.getcwd = None os.chdir = None builtins.open = None import shutil shutil.rmtree = None shutil.move = None shutil.chown = None import subprocess subprocess.Popen = None # type: ignore __builtins__["help"] = None import sys sys.modules["ipdb"] = None sys.modules["joblib"] = None sys.modules["resource"] = None sys.modules["psutil"] = None sys.modules["tkinter"] = None
This disables various destructive functions and prevents the generated code from interfering with the test (e.g. fork bomb, killing other processes, removing filesystem files, etc.) WARNING This function is NOT a security sandbox. Untrusted code, including, model- generated code, should not be blindly executed outside of one. See the Codex paper for more information about OpenAI's code sandbox, and proceed with caution.
17,142
import math The provided code snippet includes necessary dependencies for implementing the `_poly` function. Write a Python function `def _poly(xs: list, x: float)` to solve the following problem: Evaluates polynomial with coefficients xs at point x. return xs[0] + xs[1] * x + xs[1] * x^2 + .... xs[n] * x^n Here is the function: def _poly(xs: list, x: float): """ Evaluates polynomial with coefficients xs at point x. return xs[0] + xs[1] * x + xs[1] * x^2 + .... xs[n] * x^n """ return sum([coeff * math.pow(x, i) for i, coeff in enumerate(xs)])
Evaluates polynomial with coefficients xs at point x. return xs[0] + xs[1] * x + xs[1] * x^2 + .... xs[n] * x^n
17,143
import ast import gradio as gr import os import re import json import logging import torch from datetime import datetime from threading import Thread from typing import Optional from transformers import TextIteratorStreamer from functools import partial from huggingface_hub import CommitScheduler from uuid import uuid4 from pathlib import Path from code_interpreter.JupyterClient import JupyterNotebook import warnings from code_interpreter.OpenCodeInterpreter import OpenCodeInterpreter JSON_DATASET_DIR = Path("json_dataset") JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True) logging.basicConfig(level=logging.INFO) class StreamingOpenCodeInterpreter(OpenCodeInterpreter): def generate( self, prompt: str = "", max_new_tokens = 1024, do_sample: bool = False, top_p: float = 0.95, top_k: int = 50, ) -> str: def save_json(dialog, mode, json_file_path, dialog_id) -> None: def convert_history(gradio_history: list[list], interpreter_history: list[dict]): def update_uuid(dialog_info): def is_valid_python_code(code): def has_input_function_calls(code): class JupyterNotebook: def __init__(self): def clean_output(self, outputs): def add_and_run(self, code_string): def run_code_in_thread(): def close(self): def __deepcopy__(self, memo): def gradio_launch(model_path: str, MAX_TRY: int = 3): with gr.Blocks() as demo: chatbot = gr.Chatbot(height=600, label="OpenCodeInterpreter", avatar_images=["assets/user.pic.jpg", "assets/assistant.pic.jpg"], show_copy_button=True) with gr.Group(): with gr.Row(): msg = gr.Textbox( container=False, show_label=False, label="Message", placeholder="Type a message...", scale=7, autofocus=True ) sub = gr.Button( "Submit", variant="primary", scale=1, min_width=150 ) # stop = gr.Button( # "Stop", # variant="stop", # visible=False, # scale=1, # min_width=150 # ) with gr.Row(): # retry = gr.Button("🔄 Retry", variant="secondary") # undo = gr.Button("↩️ Undo", variant="secondary") clear = gr.Button("🗑️ Clear", variant="secondary") session_state = gr.State([]) jupyter_state = gr.State(JupyterNotebook()) dialog_info = gr.State(["", 0]) demo.load(update_uuid, dialog_info, dialog_info) def bot(user_message, history, jupyter_state, dialog_info, interpreter): logging.info(f"user message: {user_message}") interpreter.dialog = convert_history(gradio_history=history, interpreter_history=interpreter.dialog) history.append([user_message, None]) interpreter.dialog.append({"role": "user", "content": user_message}) # setup HAS_CODE = False # For now prompt = interpreter.dialog_to_prompt(dialog=interpreter.dialog) _ = interpreter.generate(prompt) history[-1][1] = "" generated_text = "" for character in interpreter.streamer: history[-1][1] += character history[-1][1] = history[-1][1].replace("<|EOT|>","") generated_text += character yield history, history, jupyter_state, dialog_info if is_valid_python_code(history[-1][1].strip()): history[-1][1] = f"```python\n{history[-1][1].strip()}\n```" generated_text = history[-1][1] HAS_CODE, generated_code_block = interpreter.extract_code_blocks( generated_text ) interpreter.dialog.append( { "role": "assistant", "content": generated_text.replace("<unk>_", "") .replace("<unk>", "") .replace("<|EOT|>", ""), } ) logging.info(f"saving current dialog to file {dialog_info[0]}.json...") logging.info(f"current dialog: {interpreter.dialog}") save_json(interpreter.dialog, mode="openci_only", json_file_path=JSON_DATASET_DIR/f"{dialog_info[0]}.json", dialog_id=dialog_info[0]) attempt = 1 while HAS_CODE: if attempt > MAX_TRY: break # if no code then doesn't have to execute it generated_text = "" # clear generated text yield history, history, jupyter_state, dialog_info # replace unknown thing to none '' generated_code_block = generated_code_block.replace( "<unk>_", "" ).replace("<unk>", "") if has_input_function_calls(generated_code_block): code_block_output = "Please directly assign the value of inputs instead of using input() function in your code." else: ( code_block_output, error_flag, ) = interpreter.execute_code_and_return_output( f"{generated_code_block}", jupyter_state ) if error_flag == "Timeout": logging.info(f"{dialog_info[0]}: Restart jupyter kernel due to timeout") jupyter_state = JupyterNotebook() code_block_output = interpreter.clean_code_output(code_block_output) if code_block_output.strip(): code_block_output = "Execution result: \n" + code_block_output else: code_block_output = "Code is executed, but result is empty. Please make sure that you include test case in your code." history.append([code_block_output, ""]) interpreter.dialog.append({"role": "user", "content": code_block_output}) yield history, history, jupyter_state, dialog_info prompt = interpreter.dialog_to_prompt(dialog=interpreter.dialog) logging.info(f"generating answer for dialog {dialog_info[0]}") _ = interpreter.generate(prompt) for character in interpreter.streamer: history[-1][1] += character history[-1][1] = history[-1][1].replace("<|EOT|>","") generated_text += character yield history, history, jupyter_state, dialog_info logging.info(f"finish generating answer for dialog {dialog_info[0]}") HAS_CODE, generated_code_block = interpreter.extract_code_blocks( history[-1][1] ) interpreter.dialog.append( { "role": "assistant", "content": generated_text.replace("<unk>_", "") .replace("<unk>", "") .replace("<|EOT|>", ""), } ) attempt += 1 logging.info(f"saving current dialog to file {dialog_info[0]}.json...") logging.info(f"current dialog: {interpreter.dialog}") save_json(interpreter.dialog, mode="openci_only", json_file_path=JSON_DATASET_DIR/f"{dialog_info[0]}.json", dialog_id=dialog_info[0]) if generated_text.endswith("<|EOT|>"): continue return history, history, jupyter_state, dialog_info def reset_textbox(): return gr.update(value="") def clear_history(history, jupyter_state, dialog_info, interpreter): interpreter.dialog = [] jupyter_state.close() return [], [], JupyterNotebook(), update_uuid(dialog_info) interpreter = StreamingOpenCodeInterpreter(model_path=model_path) sub.click(partial(bot, interpreter=interpreter), [msg, session_state, jupyter_state, dialog_info], [chatbot, session_state, jupyter_state, dialog_info]) sub.click(reset_textbox, [], [msg]) clear.click(partial(clear_history, interpreter=interpreter), [session_state, jupyter_state, dialog_info], [chatbot, session_state, jupyter_state, dialog_info], queue=False) demo.queue(max_size=20) demo.launch(share=True)
null
17,144
import re import os SITE_PKG_ERROR_PREFIX = f'File {PYTHON_PREFIX}/lib/python3.10/' def get_error_header(traceback_str): lines = traceback_str.split('\n') for line in lines: if 'Error:' in line: return line return '' # Return None if no error message is found def clean_error_msg(error_str:str =''): filtered_error_msg = error_str.__str__().split('An error occurred while executing the following cell')[-1].split("\n------------------\n")[-1] raw_error_msg = "".join(filtered_error_msg) # Remove escape sequences for colored text ansi_escape = re.compile(r'\x1b\[[0-?]*[ -/]*[@-~]') error_msg = ansi_escape.sub('', raw_error_msg) error_str_out = '' error_msg_only_cell = error_msg.split(SITE_PKG_ERROR_PREFIX) error_str_out += f'{error_msg_only_cell[0]}\n' error_header = get_error_header(error_msg_only_cell[-1]) if error_header not in error_str_out: error_str_out += get_error_header(error_msg_only_cell[-1]) return error_str_out
null
17,145
import glob import json import subprocess import os import multiprocessing import re import argparse zeros_pattern = r"^0+\s" OPT = ["O0", "O1", "O2", "O3"] def write_to_file(file_path, data): with multiprocessing.Lock(): with open(file_path, "a") as f: json.dump(data, f) f.write("\n") def compile_and_write(input_file, output_file): base_output_file = input_file.replace(".c", "") asm_all = {} input_text = open(input_file).read() # Read input file if "/* Variables and functions */" in input_text: # Exclude macro and types input_text = input_text.split("/* Variables and functions */")[-1] input_text = "\n\n".join(input_text.split("\n\n")[1:]) # Exclude variables ##### begin of remove __attribute__ input_text = input_text.replace("__attribute__((used)) ", "") ##### end of remove __attribute__ try: for opt_state in OPT: obj_output = base_output_file + "_" + opt_state + ".o" asm_output = base_output_file + "_" + opt_state + ".s" # Compile the C program to object file subprocess.run( ["gcc", "-c", "-o", obj_output, input_file, "-" + opt_state], check=True, ) # Generate assembly code from object file using objdump subprocess.run( f"objdump -d {obj_output} > {asm_output}", shell=True, # Use shell to handle redirection check=True, ) with open(asm_output) as f: asm = f.read() ##### start of clean up asm_clean = "" asm = asm.split("Disassembly of section .text:")[-1].strip() for tmp in asm.split("\n"): tmp_asm = tmp.split("\t")[-1] # remove the binary code tmp_asm = tmp_asm.split("#")[0].strip() # remove the comments asm_clean += tmp_asm + "\n" if len(asm_clean.split("\n")) < 4: raise ValueError("compile fails") asm = asm_clean ##### end of clean up ##### start of filter digits and attribute asm = re.sub(zeros_pattern, "", asm) asm = asm.replace("__attribute__((used)) ", "") ##### end of filter digits asm_all["opt-state-" + opt_state] = asm # Remove the object file if os.path.exists(obj_output): os.remove(obj_output) except Exception as e: print(f"Error in file {input_file}: {e}") return finally: # Remove the assembly output files for opt_state in OPT: asm_output = base_output_file + "_" + opt_state + ".s" if os.path.exists(asm_output): os.remove(asm_output) sample = { "name": input_file, "input": input_text, # Use the processed input text "input_ori": open(input_file).read(), "output": asm_all, # Use the asm_all } # Write to file write_to_file(output_file, sample)
null
17,146
import glob import json import subprocess import os import multiprocessing import re import argparse def parse_args(): parser = argparse.ArgumentParser( description="Compile C files and generate JSONL output." ) parser.add_argument( "--root", required=True, help="Root directory where AnghaBench files are located.", ) parser.add_argument("--output", required=True, help="Path to JSONL output file.") args = parser.parse_args() return args
null
17,147
import subprocess import asyncio from transformers import AutoTokenizer import os import json from loguru import logger import traceback from argparse import ArgumentParser from pathlib import Path import sys from tqdm import tqdm from server.text_generation import TextGenerationServer, TextGenerationClient import multiprocessing def parse_args() -> ArgumentParser: parser = ArgumentParser() parser.add_argument("--model_path", type=str) parser.add_argument("--dtype", type=str, default="bfloat16") parser.add_argument("--port", type=int, default=8080) parser.add_argument("--max_input_len", type=int, default=8192) parser.add_argument("--max_total_tokens", type=int, default=8800) parser.add_argument("--max_batch_prefill_tokens", type=int, default=72000) parser.add_argument("--num_shards", type=int, default=4) parser.add_argument("--max_new_tokens", type=int, default=512) parser.add_argument("--repeat", type=int, default=1) parser.add_argument("--testset_path", type=str) parser.add_argument("--num_workers", type=int, default=16) return parser.parse_args()
null
17,148
import subprocess import asyncio from transformers import AutoTokenizer import os import json from loguru import logger import traceback from argparse import ArgumentParser from pathlib import Path import sys from tqdm import tqdm from server.text_generation import TextGenerationServer, TextGenerationClient import multiprocessing logger.add(sys.stdout, colorize=False, format="{time} {level} {message}") def decompile_pass_rate(testsets, gen_results_repeat, opts, args): all_stats = [] for gen_index, gen_results in enumerate(gen_results_repeat): with multiprocessing.Pool(args.num_workers) as pool: tasks = [ { "c_func": testset["c_func"], "c_test": testset["c_test"], "c_func_decompile": output[0], } for testset, output in zip(testsets, gen_results) ] eval_results = list(tqdm(pool.imap(evaluate_func, tasks), total=len(tasks))) stats = {opt: {"compile": 0, "run": 0, "total": 0} for opt in opts} for idx, (testset, output, flag) in enumerate( tqdm( zip(testsets, gen_results, eval_results), total=len(testsets), desc="Evaluating", ) ): c_func_decompile = output[0] c_func = testset["c_func"] c_test = testset["c_test"] flag_compile, flag_run = flag[0], flag[1] opt = testset["type"] stats[opt]["total"] += 1 if flag_compile: stats[opt]["compile"] += 1 if flag_run: stats[opt]["run"] += 1 all_stats.append(stats) # average avg_stats = {opt: {"compile": 0, "run": 0, "total": 0} for opt in opts} for stats in all_stats: for opt in opts: avg_stats[opt]["compile"] += stats[opt]["compile"] avg_stats[opt]["run"] += stats[opt]["run"] avg_stats[opt]["total"] += stats[opt]["total"] for opt in opts: avg_stats[opt]["compile"] /= len(gen_results_repeat) avg_stats[opt]["run"] /= len(gen_results_repeat) avg_stats[opt]["total"] /= len(gen_results_repeat) for opt, data in avg_stats.items(): compile_rate = data["compile"] / data["total"] if data["total"] > 0 else 0 run_rate = data["run"] / data["total"] if data["total"] > 0 else 0 print( f"Optimization {opt}: Compile Rate: {compile_rate:.4f}, Run Rate: {run_rate:.4f}" ) return 0 class TextGenerationServer: def __init__( self, model_id: str, port: int, dtype: str, max_input_len: int, max_total_tokens: int, max_batch_prefill_tokens: int, num_shards: int, ): master_port = random.randint(10_000, 20_000) args = [ "text-generation-launcher", "--model-id", model_id, "--port", str(port), "--master-port", str(master_port), ] args.extend(["--num-shard", str(num_shards)]) args.extend(["--dtype", dtype]) args.extend(["--max-input-length", str(max_input_len)]) args.extend(["--max-total-tokens", str(max_total_tokens)]) args.extend(["--max-batch-prefill-tokens", str(max_batch_prefill_tokens)]) logger.info(" ".join(args)) self.launcher = subprocess.Popen(args, stdout=subprocess.DEVNULL) logger.info("Waiting for text generation server to start...") def webserver_ready(): try: socket.create_connection(("127.0.0.1", 8080), timeout=1).close() return True except (socket.timeout, ConnectionRefusedError): return False while not webserver_ready(): time.sleep(10) logger.info("Text generation webserver ready") def __del__(self): self.launcher.terminate() self.launcher.wait() class TextGenerationClient: def __init__(self, port, stop_sequences: List[str]): self.client = AsyncClient(f"http://127.0.0.1:{port}", timeout=9999) self.stop_sequences = stop_sequences async def generate( self, input: str, max_new_tokens: int, do_sample: bool, pbar: tqdm, **kwargs, ) -> str: try: if do_sample: top_p = kwargs.get("top_p", 0.95) temperature = kwargs.get("temperature", 0.8) output = await self.client.generate( input, max_new_tokens=max_new_tokens, stop_sequences=self.stop_sequences, do_sample=do_sample, temperature=temperature, top_p=top_p, ) else: output = await self.client.generate( input, max_new_tokens=max_new_tokens, stop_sequences=self.stop_sequences, do_sample=do_sample, ) generated_text = output.generated_text for stop_sequence in self.stop_sequences: generated_text = generated_text.replace(stop_sequence, "") except Exception as e: generated_text = "" logger.error(e) pbar.update() return generated_text async def generate_code_results( self, inputs: List[str], max_new_tokens: int, num_outputs: int, task_size: int = 50, **kwargs, ) -> np.array: with tqdm( total=len(inputs * num_outputs), desc="Fetching code generation results" ) as pbar: results = [] max_new_tokens = max_new_tokens if max_new_tokens > 0 else 32 do_sample = num_outputs > 1 requests = [input for input in inputs for _ in range(num_outputs)] for i in range(0, len(requests), task_size): tasks = [] for input in requests[i : i + task_size]: task = asyncio.ensure_future( self.generate(input, max_new_tokens, do_sample, pbar, **kwargs) ) tasks.append(task) for result in await asyncio.gather(*tasks): results.append(result) results = np.array(results).reshape(len(inputs), num_outputs) return results def run_eval_pipeline(args: ArgumentParser) -> int: model_path = Path(args.model_path) if not model_path.exists() or not model_path.is_dir(): logger.error(f"Invalid model {model_path}") return -1 try: testsets = json.load(open(args.testset_path, "r")) logger.info(f"Loaded testset with {len(testsets)} cases") tokenizer = AutoTokenizer.from_pretrained(model_path) stop_sequences = [tokenizer.eos_token] opts = { "O0": "# This is the assembly code with O0 optimization:\n", "O1": "# This is the assembly code with O1 optimization:\n", "O2": "# This is the assembly code with O2 optimization:\n", "O3": "# This is the assembly code with O3 optimization:\n", } after = "\n# What is the source code?\n" inputs = [] for testset in testsets: input_asm_prompt = testset["input_asm_prompt"] opt = testset["type"] prompt = opts[opt] + input_asm_prompt + after inputs.append(prompt) text_gen_server = TextGenerationServer( str(model_path), args.port, args.dtype, args.max_input_len, args.max_total_tokens, args.max_batch_prefill_tokens, args.num_shards, ) text_gen_client = TextGenerationClient( port=args.port, stop_sequences=stop_sequences ) gen_results_repeat = [] logger.info(f"The exp will loop for {args.repeat} times....") for i in range(args.repeat): logger.info(f"The {i+1} loop...") loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) gen_results = loop.run_until_complete( text_gen_client.generate_code_results( inputs, args.max_new_tokens, num_outputs=1 ) ) gen_results_repeat.append(gen_results) except Exception as e: logger.error(e) traceback.print_exc() return -1 ret = decompile_pass_rate(testsets, gen_results_repeat, opts, args) return ret
null
17,149
import subprocess from transformers import AutoTokenizer, AutoModelForCausalLM import argparse import os import torch import re import json from tqdm import tqdm, trange os.environ["TOKENIZERS_PARALLELISM"] = "false" with open(args.data_path,'r') as f: data_all = json.load(f) with open('results.txt','a') as f: for opt_state in num_compile.keys(): f.write('model:{},opt:{},compile rate:{:.4f},run_rate:{:.4f}\n'.format(args.model_path,opt_state,num_compile[opt_state]/NUM,num_run[opt_state]/NUM)) def evaluate_func(c_func,c_test,c_func_decompile): flag_compile = 0 flag_run = 0 c_include = '' for line in c_func.split('\n'): if '#include' in line: c_include += line+'\n' c_func = c_func.replace(line, '') for line in c_test.split('\n'): if '#include' in line: c_include += line+'\n' c_test = c_test.replace(line, '') c_combine = c_include + '\n' + c_func_decompile + '\n' + c_test c_onlyfunc = c_include + '\n' + c_func_decompile # Define the C file and executable names c_file = 'combine.c' executable = 'combine' if os.path.exists(executable): os.remove(executable) c_file_onlyfunc = 'onlyfunc.c' executable_onlyfunc = 'onlyfunc' if os.path.exists(executable): os.remove(executable_onlyfunc) with open(c_file,'w') as f: f.write(c_combine) with open(c_file_onlyfunc,'w') as f: f.write(c_onlyfunc) # Compile the C program to an assembly compile_command = f'gcc -S {c_file_onlyfunc} -o {executable_onlyfunc} -lm' try: subprocess.run(compile_command, shell=True, check=True) flag_compile = 1 except: return flag_compile, flag_run # Compile the C program to an executable compile_command = f'gcc {c_file} -o {executable} -lm' try: subprocess.run(compile_command, shell=True, check=True) flag_compile = 1 except: return flag_compile, flag_run # Run the compiled executable run_command = f'./{executable}' try: process = subprocess.run(run_command, shell=True, check=True,capture_output=True, timeout=5) flag_run = 1 except subprocess.CalledProcessError as e: pass except Exception as e: pass return flag_compile, flag_run
null
17,150
import glob import platform import subprocess import os from os import path from setuptools import find_packages, setup import torch from torch.utils.cpp_extension import CUDA_HOME, CUDNN_HOME, CppExtension, CUDAExtension def fetch_requirements(): with open("requirements.txt") as f: reqs = f.read().strip().split("\n") return reqs
null
17,151
import glob import platform import subprocess import os from os import path from setuptools import find_packages, setup import torch from torch.utils.cpp_extension import CUDA_HOME, CUDNN_HOME, CppExtension, CUDAExtension def get_version(): this_dir = path.dirname(path.abspath(__file__)) if os.getenv("BUILD_VERSION"): # In CI version = os.getenv("BUILD_VERSION") else: version_file_path = path.join(this_dir, "version.txt") version = open(version_file_path, "r").readlines()[0].strip() # The following is used to build release packages. # Users should never use it. suffix = os.getenv("SFAST_VERSION_SUFFIX", "") version = version + suffix if os.getenv("BUILD_NIGHTLY", "0") == "1": from datetime import datetime date_str = datetime.today().strftime("%y%m%d") version = version + ".dev" + date_str init_py_path = path.join(this_dir, "src", "sfast", "__init__.py") init_py = open(init_py_path, "r").readlines() new_init_py = [l for l in init_py if not l.startswith("__version__")] new_init_py.append('__version__ = "{}"\n'.format(version)) with open(init_py_path, "w") as f: f.write("".join(new_init_py)) return version
null
17,152
import glob import platform import subprocess import os from os import path from setuptools import find_packages, setup import torch from torch.utils.cpp_extension import CUDA_HOME, CUDNN_HOME, CppExtension, CUDAExtension torch_ver = [int(x) for x in torch.__version__.split(".")[:2]] assert torch_ver >= [1, 8], "Requires PyTorch >= 1.8" def get_cuda_version(cuda_dir) -> int: nvcc_bin = "nvcc" if cuda_dir is None else cuda_dir + "/bin/nvcc" raw_output = subprocess.check_output([nvcc_bin, "-V"], universal_newlines=True) output = raw_output.split() release_idx = output.index("release") + 1 release = output[release_idx].split(".") bare_metal_major = int(release[0]) bare_metal_minor = int(release[1][0]) assert bare_metal_minor < 100 return bare_metal_major * 100 + bare_metal_minor def get_extensions(): this_dir = path.dirname(path.abspath(__file__)) extensions_dir = path.join(this_dir, "src", "sfast", "csrc") include_dirs = [extensions_dir] sources = glob.glob(path.join(extensions_dir, "**", "*.cpp"), recursive=True) # common code between cuda and rocm platforms, for hipify version [1,0,0] and later. source_cuda = glob.glob(path.join(extensions_dir, "**", "*.cu"), recursive=True) source_cuda_rt = glob.glob(path.join(extensions_dir, "**", "*.cc"), recursive=True) extension = CppExtension extra_compile_args = {"cxx": []} library_dirs = [] libraries = [] define_macros = [] # if (torch.cuda.is_available() # and ((CUDA_HOME is not None) or is_rocm_pytorch)): # Skip the above useless check as we will always compile with CUDA support, # and the CI might be running on CPU-only machines. if platform.system() != "Darwin" and os.getenv("WITH_CUDA", "1") != "0": assert CUDA_HOME is not None, "Cannot find CUDA installation. If you want to compile without CUDA, set `WITH_CUDA=0`." cutlass_root = os.path.join(this_dir, "third_party", "cutlass") cutlass_include = os.path.join(cutlass_root, "include") if not os.path.exists(cutlass_root) or not os.path.exists( cutlass_include): raise RuntimeError("Cannot find cutlass. Please run " "`git submodule update --init --recursive`.") include_dirs.append(cutlass_include) cutlass_tools_util_include = os.path.join(cutlass_root, "tools", "util", "include") include_dirs.append(cutlass_tools_util_include) cutlass_examples_dual_gemm = os.path.join(cutlass_root, "examples", "45_dual_gemm") include_dirs.append(cutlass_examples_dual_gemm) extension = CUDAExtension sources += source_cuda sources += source_cuda_rt # from torch.utils.cpp_extension import ROCM_HOME # is_rocm_pytorch = (True if ((torch.version.hip is not None) and # (ROCM_HOME is not None)) else False) # if is_rocm_pytorch: # assert torch_ver >= [1, 8], "ROCM support requires PyTorch >= 1.8!" define_macros += [("WITH_CUDA", None)] extra_compile_args["nvcc"] = [ "--use_fast_math", "-U__CUDA_NO_HALF_OPERATORS__", "-U__CUDA_NO_HALF_CONVERSIONS__", "--extended-lambda", "-D_ENABLE_EXTENDED_ALIGNED_STORAGE", "-std=c++17", "--ptxas-options=-O2", "--ptxas-options=-allow-expensive-optimizations=true", ] cuda_version = get_cuda_version(CUDA_HOME) if cuda_version >= 1102: extra_compile_args["nvcc"] += [ "--threads", "2", "--ptxas-options=-v", ] if platform.system() == "Windows": extra_compile_args["nvcc"] += [ "-Xcompiler", "/Zc:lambda", "-Xcompiler", "/Zc:preprocessor", "-Xcompiler", "/Zc:__cplusplus", # cannot call non-constexpr function "cutlass::const_min" ] nvcc_flags_env = os.getenv("NVCC_FLAGS", "") if nvcc_flags_env != "": extra_compile_args["nvcc"].extend(nvcc_flags_env.split(" ")) if torch_ver < [1, 7]: # supported by https://github.com/pytorch/pytorch/pull/43931 CC = os.environ.get("CC", None) if CC is not None: extra_compile_args["nvcc"].append("-ccbin={}".format(CC)) if CUDNN_HOME is None: try: # Try to use the bundled version of CUDNN with PyTorch installation. # This is also used in CI. from nvidia import cudnn except ImportError: cudnn = None if cudnn is not None: cudnn_dir = os.path.dirname(cudnn.__file__) print("Using CUDNN from {}".format(cudnn_dir)) include_dirs.append(os.path.join(cudnn_dir, "include")) # Hope PyTorch knows how to link it correctly. # We only need headers because PyTorch should have # linked the actual library file. (But why not work on Windows?) # Make Windows CI happy (unresolved external symbol) # Why Linux does not need this? if platform.system() == "Windows": library_dirs.append(os.path.join(cudnn_dir, "lib")) library_dirs.append(os.path.join(cudnn_dir, "lib", "x64")) try: from nvidia import cublas except ImportError: cublas = None if cublas is not None: cublas_dir = os.path.dirname(cublas.__file__) print("Using CUBLAS from {}".format(cublas_dir)) include_dirs.append(os.path.join(cublas_dir, "include")) # Hope PyTorch knows how to link it correctly. # We only need headers because PyTorch should have # linked the actual library file. (But why not work on Windows?) # Make Windows CI happy (unresolved external symbol) # Why Linux does not need this? if platform.system() == "Windows": library_dirs.append(os.path.join(cublas_dir, "lib")) library_dirs.append(os.path.join(cublas_dir, "lib", "x64")) if platform.system() == "Windows": libraries.append("cudnn") libraries.append("cublas") libraries.append("cublasLt") else: print("Compiling without CUDA support") ext_modules = [ extension( "sfast._C", sorted(sources), include_dirs=[os.path.abspath(p) for p in include_dirs], define_macros=define_macros, extra_compile_args=extra_compile_args, library_dirs=[os.path.abspath(p) for p in library_dirs], libraries=libraries, ) ] return ext_modules
null
17,153
import logging import functools import threading import dataclasses import torch from sfast.utils.copy import (tree_copy_, tree_copy, shadow_copy, can_be_perfectly_copied) from sfast.hooks.module_jit_hook import (apply_to_all_modules, apply_to_module) def get_requires_grad_from_tensors(x): if isinstance(x, torch.Tensor): return x.requires_grad elif isinstance(x, (list, tuple)): for y in x: requires_grad = get_requires_grad_from_tensors(y) if requires_grad: return True return False elif dataclasses.is_dataclass(x): for k in dataclasses.fields(x): requires_grad = get_requires_grad_from_tensors(getattr(x, k)) if requires_grad: return True return False elif isinstance(x, dict): for v in x.values(): requires_grad = get_requires_grad_from_tensors(v) if requires_grad: return True return False else: return False
null
17,154
import logging import functools import threading import dataclasses import torch from sfast.utils.copy import (tree_copy_, tree_copy, shadow_copy, can_be_perfectly_copied) from sfast.hooks.module_jit_hook import (apply_to_all_modules, apply_to_module) def can_be_perfectly_copied(obj): if obj is None: return True # micro optimization: bool obj is an instance of int elif isinstance(obj, (torch.Tensor, float, int, str, bytes)): return True elif isinstance(obj, (list, tuple)): return all(can_be_perfectly_copied(x) for x in obj) elif dataclasses.is_dataclass(obj): return all( can_be_perfectly_copied(getattr(obj, field.name)) for field in dataclasses.fields(obj)) elif isinstance(obj, dict): return all(can_be_perfectly_copied(v) for v in obj.values()) else: return False def can_io_obj_be_perfectly_graphed(obj): return can_be_perfectly_copied(obj)
null
17,155
import logging import functools import threading import dataclasses import torch from sfast.utils.copy import (tree_copy_, tree_copy, shadow_copy, can_be_perfectly_copied) from sfast.hooks.module_jit_hook import (apply_to_all_modules, apply_to_module) class AutoGraphCraphCompiler: def __init__(self, **kwargs): self.kwargs = kwargs self._is_compiling = threading.local() def is_compiling(self): return getattr(self._is_compiling, 'value', False) def get_inputs_key(self, func, inputs, kwargs): if not can_io_obj_be_perfectly_graphed((inputs, kwargs)): return None return (hash_arg(inputs), hash_arg(kwargs)) def get_outputs_key(self, func, outputs): if not can_io_obj_be_perfectly_graphed(outputs): return None return hash_arg(outputs) def compile(self, func, inputs, kwargs): self._is_compiling.value = True try: graphed = simple_make_graphed_callable(func, inputs, kwargs, **self.kwargs) wrapped = func.forward if isinstance(func, torch.nn.Module) else func def functionalized(*args, **kwargs): return graphed(*args, **kwargs) if isinstance(func, torch.nn.Module): functionalized.__self__ = func elif hasattr(func, '__self__'): functionalized.__self__ = func.__self__ return functionalized finally: self._is_compiling.value = False def apply_to_all_modules(m, compiler, filter_func=None): if filter_func is None: filter_func = lambda stack: True return patch_module(m, filter_func, lambda m: apply_to_module(m, compiler)) def apply_to_module(m, compiler): ModuleJITHook(m, compiler) return m def apply_auto_graph_compiler_to_all_modules(m, filter_func=None, recursive=True, **kwargs): if recursive: return apply_to_all_modules(m, AutoGraphCraphCompiler(**kwargs), filter_func=filter_func) else: return apply_to_module(m, AutoGraphCraphCompiler(**kwargs))
null
17,156
from typing import Optional import torch from xformers.ops import (memory_efficient_attention, AttentionOp) from xformers import ops from sfast.utils.custom_python_operator import register_custom_python_operator STR_OP_MAP = {v: k for k, v in OP_STR_MAP.items()} def xformers_memory_efficient_attention_torch_op( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_bias: Optional[torch.Tensor] = None, p: float = 0.0, scale: Optional[float] = None, op: Optional[str] = None): if op is not None: op = STR_OP_MAP[op] hidden_states = memory_efficient_attention(query, key, value, attn_bias, p, scale, op=op) return hidden_states
null
17,157
from sfast.utils.patch import patch_module def patch_module(m, filter_func, patch_func, stack=None, inplace=False): if stack is None: stack = [(None, m)] if filter_func(stack): if inplace: patch_func(m) else: m = patch_func(m) for name, child in m.named_children(): stack.append((name, child)) if filter_func(stack): if inplace: patch_func(child) else: setattr(m, name, patch_func(child)) else: patch_module(child, filter_func, patch_func, stack, inplace) stack.pop() return m class TritonConv2D(nn.Module): def __init__(self, module): super().__init__() self.module = module self.training = module.training def forward(self, x, *args, **kwargs): weight = self.module.weight x = TTO.contiguous(x, memory_format=suggest_memory_format(weight)) return self.module(x, *args, **kwargs) def patch_conv2d(m): from torch.nn import Conv2d from .native import TritonConv2D return patch_module(m, lambda stack: isinstance(stack[-1][1], Conv2d), TritonConv2D)
null
17,158
from sfast.utils.patch import patch_module def patch_module(m, filter_func, patch_func, stack=None, inplace=False): if stack is None: stack = [(None, m)] if filter_func(stack): if inplace: patch_func(m) else: m = patch_func(m) for name, child in m.named_children(): stack.append((name, child)) if filter_func(stack): if inplace: patch_func(child) else: setattr(m, name, patch_func(child)) else: patch_module(child, filter_func, patch_func, stack, inplace) stack.pop() return m class TritonLinear(nn.Module): def __init__(self, module): super().__init__() self.module = module self.training = module.training def forward(self, x, *args, **kwargs): weight = self.module.weight x = TTO.contiguous(x, memory_format=suggest_memory_format(weight)) return self.module(x, *args, **kwargs) # if xformers is None: # return self.module(x, *args, **kwargs) # else: # grad_mode = torch.is_grad_enabled() # bias = self.module.bias # return _fused_linear_triton.apply( # x, weight, bias, 0, grad_mode and weight.requires_grad, # False if bias is None else grad_mode and bias.requires_grad, # False) def patch_linear(m): from torch.nn import Linear from .native import TritonLinear return patch_module(m, lambda stack: isinstance(stack[-1][1], Linear), TritonLinear)
null
17,159
from sfast.utils.patch import patch_module def patch_module(m, filter_func, patch_func, stack=None, inplace=False): class TritonGroupNorm(nn.Module): def __init__(self, module): def forward(self, x, *args, **kwargs): def patch_group_norm(m): from torch.nn import GroupNorm from .native import TritonGroupNorm return patch_module(m, lambda stack: isinstance(stack[-1][1], GroupNorm), TritonGroupNorm)
null
17,160
from sfast.utils.patch import patch_module def patch_module(m, filter_func, patch_func, stack=None, inplace=False): if stack is None: stack = [(None, m)] if filter_func(stack): if inplace: patch_func(m) else: m = patch_func(m) for name, child in m.named_children(): stack.append((name, child)) if filter_func(stack): if inplace: patch_func(child) else: setattr(m, name, patch_func(child)) else: patch_module(child, filter_func, patch_func, stack, inplace) stack.pop() return m class TritonGroupNormSiLU(nn.Module): def __init__(self, module): super().__init__() self.module = module self.training = module.training def forward(self, x, *args, **kwargs): # x = TTO.contiguous(x) # return self.module(x, *args, **kwargs) module = self.module # x = TTO.contiguous(x) return TTO.group_norm_silu(x, module.num_groups, module.weight, module.bias, module.eps) def patch_group_norm_silu(m): from torch.nn import (Sequential, GroupNorm, SiLU) from .native import TritonGroupNormSiLU def filter_func(stack): seq = stack[-1][1] if not isinstance(seq, Sequential): return False return len(seq) >= 2 and isinstance(seq[0], GroupNorm) and isinstance( seq[1], SiLU) def patch_func(module): return Sequential(TritonGroupNormSiLU(module[0]), *module[2:]) return patch_module(m, filter_func, patch_func)
null
17,161
from sfast.utils.patch import patch_module def patch_module(m, filter_func, patch_func, stack=None, inplace=False): if stack is None: stack = [(None, m)] if filter_func(stack): if inplace: patch_func(m) else: m = patch_func(m) for name, child in m.named_children(): stack.append((name, child)) if filter_func(stack): if inplace: patch_func(child) else: setattr(m, name, patch_func(child)) else: patch_module(child, filter_func, patch_func, stack, inplace) stack.pop() return m class TritonLoRACompatibleConv(nn.Module): def __init__(self, module): super().__init__() self.module = module self.training = module.training def set_lora_layer(self, lora_layer): if hasattr(self.module, 'set_lora_layer'): self.module.set_lora_layer(lora_layer) def forward(self, x, *args, **kwargs): weight = self.module.weight x = TTO.contiguous(x, memory_format=suggest_memory_format(weight)) return self.module(x, *args, **kwargs) def patch_lora_compatible_conv(m): from torch.nn import Conv2d # from diffusers.models.lora import LoRACompatibleConv from .diffusers import TritonLoRACompatibleConv return patch_module(m, lambda stack: isinstance(stack[-1][1], Conv2d), TritonLoRACompatibleConv)
null
17,162
from sfast.utils.patch import patch_module def patch_module(m, filter_func, patch_func, stack=None, inplace=False): if stack is None: stack = [(None, m)] if filter_func(stack): if inplace: patch_func(m) else: m = patch_func(m) for name, child in m.named_children(): stack.append((name, child)) if filter_func(stack): if inplace: patch_func(child) else: setattr(m, name, patch_func(child)) else: patch_module(child, filter_func, patch_func, stack, inplace) stack.pop() return m class TritonLoRACompatibleLinear(nn.Module): def __init__(self, module): super().__init__() self.module = module self.training = module.training def set_lora_layer(self, lora_layer): if hasattr(self.module, 'set_lora_layer'): self.module.set_lora_layer(lora_layer) def forward(self, x, *args, **kwargs): weight = self.module.weight x = TTO.contiguous(x, memory_format=suggest_memory_format(weight)) return self.module(x, *args, **kwargs) # if xformers is None: # return self.module(x, *args, **kwargs) # else: # grad_mode = torch.is_grad_enabled() # bias = self.module.bias # return _fused_linear_triton.apply( # x, weight, bias, 0, grad_mode and weight.requires_grad, # False if bias is None else grad_mode and bias.requires_grad, # False) def patch_lora_compatible_linear(m): from torch.nn import Linear # from diffusers.models.lora import LoRACompatibleLinear from .diffusers import TritonLoRACompatibleLinear return patch_module(m, lambda stack: isinstance(stack[-1][1], Linear), TritonLoRACompatibleLinear)
null
17,163
import torch import sfast from sfast.utils.custom_python_operator import register_custom_python_operator from .ops.copy import copy from .ops.group_norm import (group_norm_forward, group_norm_silu_forward) from .ops.layer_norm import LayerNorm as TritonLayerNorm from .ops.conv import conv_forward aten = torch.ops.aten contiguous = construct_triton_contiguous_torch_op() 'BLOCK_M': 'BATCH_STRIDE_INP_IS_1': 'STRIDE_INP_0_IS_1': 'BATCH_STRIDE_OUT_IS_1': 'STRIDE_OUT_0_IS_1': })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_M'] // 32)), })''') })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_M'] * kwargs['BLOCK_N'] // 32)), })''') })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_M'] * kwargs['BLOCK_N'] * kwargs['BLOCK_K'] // 32)), })''') def copy(dst, src): dst_device = dst.device src_device = src.device assert dst_device.type == 'cuda' assert dst_device == src_device dst_shape = dst.shape src_shape = src.shape assert dst_shape == src_shape dst_strides = dst.stride() src_strides = src.stride() ndim = dst.ndim if ndim in (1, 2): if dst.ndim == 1: dst = dst[None, :] src = src[None, :] bsz, sz0 = dst_shape bsd, sd0 = dst_strides bss, ss0 = src_strides def grid(meta): return (triton.cdiv(sz0, meta['BLOCK_M']), bsz) copy_2d_kernel[grid]( dst, src, bsz, sz0, bss, ss0, bsd, sd0, ) elif ndim == 3: bs, sz0, sz1 = dst_shape bsd, sd0, sd1 = dst_strides bss, ss0, ss1 = src_strides def grid(meta): return (triton.cdiv(sz0, meta['BLOCK_M']) * triton.cdiv(sz1, meta['BLOCK_N']), bs) copy_3d_kernel[grid]( dst, src, bs, sz0, sz1, bss, ss0, ss1, bsd, sd0, sd1, ) elif ndim == 4: bs, sz0, sz1, sz2 = dst_shape bsd, sd0, sd1, sd2 = dst_strides bss, ss0, ss1, ss2 = src_strides def grid(meta): return (triton.cdiv(sz0, meta['BLOCK_M']) * triton.cdiv(sz1, meta['BLOCK_N']) * triton.cdiv(sz2, meta['BLOCK_K']), bs) copy_4d_kernel[grid]( dst, src, bs, sz0, sz1, sz2, bss, ss0, ss1, ss2, bsd, sd0, sd1, sd2, ) else: raise NotImplementedError return dst def construct_triton_contiguous_torch_op(): class TritonContiguous(torch.autograd.Function): @staticmethod def forward(ctx, x, memory_format=torch.contiguous_format): if x.device.type != 'cuda' or x.ndim > 4 or x.is_contiguous( memory_format=memory_format): return aten.contiguous(x, memory_format=memory_format) else: dst = torch.empty_like(x, memory_format=memory_format) return copy(dst, x) @staticmethod def backward(ctx, grad_output): return grad_output, None def contiguous(x, memory_format=torch.contiguous_format): return TritonContiguous.apply(x, memory_format) return contiguous
null
17,164
import torch import sfast from sfast.utils.custom_python_operator import register_custom_python_operator from .ops.copy import copy from .ops.group_norm import (group_norm_forward, group_norm_silu_forward) from .ops.layer_norm import LayerNorm as TritonLayerNorm from .ops.conv import conv_forward aten = torch.ops.aten clone = constuct_triton_clone_torch_op() 'BLOCK_M': 'BATCH_STRIDE_INP_IS_1': 'STRIDE_INP_0_IS_1': 'BATCH_STRIDE_OUT_IS_1': 'STRIDE_OUT_0_IS_1': })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_M'] // 32)), })''') })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_M'] * kwargs['BLOCK_N'] // 32)), })''') })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_M'] * kwargs['BLOCK_N'] * kwargs['BLOCK_K'] // 32)), })''') def copy(dst, src): dst_device = dst.device src_device = src.device assert dst_device.type == 'cuda' assert dst_device == src_device dst_shape = dst.shape src_shape = src.shape assert dst_shape == src_shape dst_strides = dst.stride() src_strides = src.stride() ndim = dst.ndim if ndim in (1, 2): if dst.ndim == 1: dst = dst[None, :] src = src[None, :] bsz, sz0 = dst_shape bsd, sd0 = dst_strides bss, ss0 = src_strides def grid(meta): return (triton.cdiv(sz0, meta['BLOCK_M']), bsz) copy_2d_kernel[grid]( dst, src, bsz, sz0, bss, ss0, bsd, sd0, ) elif ndim == 3: bs, sz0, sz1 = dst_shape bsd, sd0, sd1 = dst_strides bss, ss0, ss1 = src_strides def grid(meta): return (triton.cdiv(sz0, meta['BLOCK_M']) * triton.cdiv(sz1, meta['BLOCK_N']), bs) copy_3d_kernel[grid]( dst, src, bs, sz0, sz1, bss, ss0, ss1, bsd, sd0, sd1, ) elif ndim == 4: bs, sz0, sz1, sz2 = dst_shape bsd, sd0, sd1, sd2 = dst_strides bss, ss0, ss1, ss2 = src_strides def grid(meta): return (triton.cdiv(sz0, meta['BLOCK_M']) * triton.cdiv(sz1, meta['BLOCK_N']) * triton.cdiv(sz2, meta['BLOCK_K']), bs) copy_4d_kernel[grid]( dst, src, bs, sz0, sz1, sz2, bss, ss0, ss1, ss2, bsd, sd0, sd1, sd2, ) else: raise NotImplementedError return dst def constuct_triton_clone_torch_op(): class TritonClone(torch.autograd.Function): @staticmethod def forward(ctx, x, memory_format=torch.preserve_format): if x.device.type != 'cuda' or x.ndim > 4 or x.is_contiguous( memory_format=memory_format): return aten.clone(x, memory_format=memory_format) else: dst = torch.empty_like(x, memory_format=memory_format) return copy(dst, x) @staticmethod def backward(ctx, grad_output): return grad_output, None def clone(x, memory_format=torch.preserve_format): return TritonClone.apply(x, memory_format) return clone
null
17,165
import torch import sfast from sfast.utils.custom_python_operator import register_custom_python_operator from .ops.copy import copy from .ops.group_norm import (group_norm_forward, group_norm_silu_forward) from .ops.layer_norm import LayerNorm as TritonLayerNorm from .ops.conv import conv_forward aten = torch.ops.aten reshape = construct_triton_reshape_torch_op() 'BLOCK_M': 'BATCH_STRIDE_INP_IS_1': 'STRIDE_INP_0_IS_1': 'BATCH_STRIDE_OUT_IS_1': 'STRIDE_OUT_0_IS_1': })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_M'] // 32)), })''') })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_M'] * kwargs['BLOCK_N'] // 32)), })''') })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_M'] * kwargs['BLOCK_N'] * kwargs['BLOCK_K'] // 32)), })''') def copy(dst, src): dst_device = dst.device src_device = src.device assert dst_device.type == 'cuda' assert dst_device == src_device dst_shape = dst.shape src_shape = src.shape assert dst_shape == src_shape dst_strides = dst.stride() src_strides = src.stride() ndim = dst.ndim if ndim in (1, 2): if dst.ndim == 1: dst = dst[None, :] src = src[None, :] bsz, sz0 = dst_shape bsd, sd0 = dst_strides bss, ss0 = src_strides def grid(meta): copy_2d_kernel[grid]( dst, src, bsz, sz0, bss, ss0, bsd, sd0, ) elif ndim == 3: bs, sz0, sz1 = dst_shape bsd, sd0, sd1 = dst_strides bss, ss0, ss1 = src_strides def grid(meta): copy_3d_kernel[grid]( dst, src, bs, sz0, sz1, bss, ss0, ss1, bsd, sd0, sd1, ) elif ndim == 4: bs, sz0, sz1, sz2 = dst_shape bsd, sd0, sd1, sd2 = dst_strides bss, ss0, ss1, ss2 = src_strides def grid(meta): copy_4d_kernel[grid]( dst, src, bs, sz0, sz1, sz2, bss, ss0, ss1, ss2, bsd, sd0, sd1, sd2, ) else: raise NotImplementedError return dst def construct_triton_reshape_torch_op(): class TritonReshape(torch.autograd.Function): @staticmethod def forward(ctx, x, shape): ctx.shape = x.shape if x.device.type != 'cuda' or x.ndim > 4 or sfast._C._compute_stride( x.shape, x.stride(), shape) is not None: return aten.reshape(x, shape) else: dst = torch.empty_like(x, memory_format=torch.contiguous_format) copy(dst, x) return aten.reshape(dst, shape) @staticmethod def backward(ctx, grad_output): if grad_output.device.type != 'cuda' or grad_output.ndim > 4 or sfast._C._compute_stride( grad_output.shape, grad_output.stride(), ctx.shape) is not None: return grad_output.reshape(ctx.shape), None else: dst = torch.empty_like(grad_output, memory_format=torch.contiguous_format) copy(dst, grad_output) return dst.reshape(ctx.shape), None def reshape(x, shape): return TritonReshape.apply(x, shape) return reshape
null
17,166
import torch import sfast from sfast.utils.custom_python_operator import register_custom_python_operator from .ops.copy import copy from .ops.group_norm import (group_norm_forward, group_norm_silu_forward) from .ops.layer_norm import LayerNorm as TritonLayerNorm from .ops.conv import conv_forward aten = torch.ops.aten contiguous = construct_triton_contiguous_torch_op() group_norm = construct_triton_group_norm_torch_op() })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['ROW_SIZE'] * kwargs['BLOCK_SIZE'] // 128)), 'C_G': lambda kwargs: kwargs['C'] // kwargs['groups'], })''') })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['ROW_SIZE'] * kwargs['BLOCK_SIZE'] // 128)), 'C_G': lambda kwargs: kwargs['C'] // kwargs['groups'], })''') })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_SIZE'] // 128)), })''') group_norm_forward = create_group_norm_forward() def construct_triton_group_norm_torch_op(): class TritonGroupNorm(torch.autograd.Function): @staticmethod def forward(ctx, input, num_groups, weight=None, bias=None, eps=1e-05): device_type = input.device.type if device_type != 'cuda' or input.ndim > 4: input = input.contiguous() if weight is not None: weight = weight.contiguous() if bias is not None: bias = bias.contiguous() N, C = input.shape[:2] HxW = input.numel() // (N * C) output, mean, rstd = aten.native_group_norm( input, weight, bias, N, C, HxW, num_groups, eps) else: needs_backward = any(x is not None and x.requires_grad for x in [input, weight, bias]) output, mean, rstd = group_norm_forward( input, num_groups, weight, bias, eps, output_mean=needs_backward, output_rstd=needs_backward) ctx.save_for_backward(input, weight, bias, mean, rstd) ctx.num_groups = num_groups return output @staticmethod def backward(ctx, grad_output): input, weight, bias, mean, rstd = ctx.saved_tensors grad_input_mask = (ctx.needs_input_grad[0], ctx.needs_input_grad[2], ctx.needs_input_grad[3]) N, C = input.shape[:2] HxW = input.numel() // (N * C) grad_output = grad_output.contiguous() input = input.contiguous() mean = mean.contiguous() rstd = rstd.contiguous() weight = weight.contiguous() if weight is not None else None grad_inputs = aten.native_group_norm_backward( grad_output, input, mean, rstd, weight, N, C, HxW, ctx.num_groups, grad_input_mask) grad_input, grad_weight, grad_bias = grad_inputs return grad_input, None, grad_weight, grad_bias, None def group_norm(input, num_groups, weight=None, bias=None, eps=1e-05): return TritonGroupNorm.apply(input, num_groups, weight, bias, eps) return group_norm
null
17,167
import torch import sfast from sfast.utils.custom_python_operator import register_custom_python_operator from .ops.copy import copy from .ops.group_norm import (group_norm_forward, group_norm_silu_forward) from .ops.layer_norm import LayerNorm as TritonLayerNorm from .ops.conv import conv_forward aten = torch.ops.aten contiguous = construct_triton_contiguous_torch_op() group_norm_silu = construct_triton_group_norm_silu_torch_op() })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['ROW_SIZE'] * kwargs['BLOCK_SIZE'] // 128)), 'C_G': lambda kwargs: kwargs['C'] // kwargs['groups'], })''') })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['ROW_SIZE'] * kwargs['BLOCK_SIZE'] // 128)), 'C_G': lambda kwargs: kwargs['C'] // kwargs['groups'], })''') })''') 'num_warps': lambda kwargs: max(1, min(16, kwargs['BLOCK_SIZE'] // 128)), })''') group_norm_silu_forward = create_group_norm_forward(act=activation.silu) def construct_triton_group_norm_silu_torch_op(): class TritonGroupNormSiLU(torch.autograd.Function): @staticmethod def forward(ctx, input, num_groups, weight=None, bias=None, eps=1e-05): device_type = input.device.type if device_type != 'cuda' or input.ndim > 4: input = input.contiguous() if weight is not None: weight = weight.contiguous() if bias is not None: bias = bias.contiguous() N, C = input.shape[:2] HxW = input.numel() // (N * C) output, mean, rstd = aten.native_group_norm( input, weight, bias, N, C, HxW, num_groups, eps) output = aten.silu(output) else: needs_backward = any(x is not None and x.requires_grad for x in [input, weight, bias]) output, mean, rstd = group_norm_silu_forward( input, num_groups, weight, bias, eps, output_mean=needs_backward, output_rstd=needs_backward) ctx.save_for_backward(input, weight, bias, mean, rstd) ctx.num_groups = num_groups return output @staticmethod def backward(ctx, grad_output): input, weight, bias, mean, rstd = ctx.saved_tensors grad_input_mask = (ctx.needs_input_grad[0], ctx.needs_input_grad[2], ctx.needs_input_grad[3]) N, C = input.shape[:2] HxW = input.numel() // (N * C) grad_output = grad_output.contiguous() input = input.contiguous() mean = mean.contiguous() rstd = rstd.contiguous() weight = weight.contiguous() if weight is not None else None repeats = input.shape[1] // ctx.num_groups normed = input.sub( mean.repeat_interleave(repeats, 1)[..., None, None]).mul_( rstd.repeat_interleave(repeats, 1)[..., None, None]) grad_normed = aten.silu_backward(grad_output, normed) grad_inputs = aten.native_group_norm_backward( grad_normed, input, mean, rstd, weight, N, C, HxW, ctx.num_groups, grad_input_mask) grad_input, grad_weight, grad_bias = grad_inputs return grad_input, None, grad_weight, grad_bias, None def group_norm_silu(input, num_groups, weight=None, bias=None, eps=1e-05): return TritonGroupNormSiLU.apply(input, num_groups, weight, bias, eps) return group_norm_silu
null
17,168
import torch import sfast from sfast.utils.custom_python_operator import register_custom_python_operator from .ops.copy import copy from .ops.group_norm import (group_norm_forward, group_norm_silu_forward) from .ops.layer_norm import LayerNorm as TritonLayerNorm from .ops.conv import conv_forward aten = torch.ops.aten layer_norm = construct_triton_layer_norm_torch_op() def construct_triton_layer_norm_torch_op(): def layer_norm(input, normalized_shape, weight=None, bias=None, eps=1e-05): if input.device.type != 'cuda' or not input.is_contiguous(): return aten.layer_norm(input, normalized_shape, weight, bias, eps) return TritonLayerNorm.apply(input, normalized_shape, weight, bias, eps) return layer_norm
null
17,169
import torch import sfast from sfast.utils.custom_python_operator import register_custom_python_operator from .ops.copy import copy from .ops.group_norm import (group_norm_forward, group_norm_silu_forward) from .ops.layer_norm import LayerNorm as TritonLayerNorm from .ops.conv import conv_forward aten = torch.ops.aten _convolution = construct__convolution_torch_op() conv_forward = _conv.forward def construct__convolution_torch_op(): class Triton_Convolution(torch.autograd.Function): @staticmethod def forward(ctx, input, weight, bias, stride, padding, dilation, transposed, output_padding, groups, benchmark, deterministic, cudnn_enabled, allow_tf32): if groups != 1 or transposed or deterministic or not allow_tf32: output = aten._convolution(input, weight, bias, stride, padding, dilation, transposed, output_padding, groups, benchmark, deterministic, cudnn_enabled, allow_tf32) else: output = conv_forward(input, weight, bias, stride, padding, dilation, transposed, output_padding, groups) return output @staticmethod def backward(ctx, grad_output): return None, None, None, None, None, None, None, None, None, None, None, None, None def _convolution(input, weight, bias, stride, padding, dilation, transposed, output_padding, groups, benchmark, deterministic, cudnn_enabled, allow_tf32): return Triton_Convolution.apply(input, weight, bias, stride, padding, dilation, transposed, output_padding, groups, benchmark, deterministic, cudnn_enabled, allow_tf32) return _convolution
null
17,170
import torch import triton import triton.language as tl from sfast.utils.copy_func import copy_func from . import activation from .utils import welford_combine def welford_combine(mean_1, m2_1, weight_1, mean_2, m2_2, weight_2): delta = mean_2 - mean_1 new_weight = weight_1 + weight_2 # w2_over_w = weight_2 / new_weight w2_over_w = tl.where(new_weight == 0.0, 0.0, weight_2 / new_weight) return ( mean_1 + delta * w2_over_w, m2_1 + m2_2 + delta * delta * weight_1 * w2_over_w, new_weight, ) def group_norm_4d_channels_last_forward_collect_stats_kernel_stage_1( input_ptr, N, C, HxW, groups, cluster_size, cluster_num, cluster_mean_ptr, cluster_m2_ptr, cluster_weight_ptr, C_G, ROW_SIZE: tl.constexpr, BLOCK_SIZE: tl.constexpr, ): group = tl.program_id(0) cluster = tl.program_id(1) pid_batch = tl.program_id(2) offset = pid_batch * C * HxW + group * C_G X = input_ptr + offset _mean = tl.zeros((BLOCK_SIZE, ROW_SIZE), dtype=tl.float32) _m2 = tl.zeros((BLOCK_SIZE, ROW_SIZE), dtype=tl.float32) _weight = tl.zeros((BLOCK_SIZE, ROW_SIZE), dtype=tl.float32) row = tl.arange(0, ROW_SIZE) start = cluster * cluster_size end = start + cluster_size end = min(end, HxW) for off in range(start, end, BLOCK_SIZE): r = off + tl.arange(0, BLOCK_SIZE) m2_ = tl.zeros((BLOCK_SIZE, ROW_SIZE), dtype=tl.float32) mask = (r < end)[:, None] & (row[None, :] < C_G) weight_ = mask.to(tl.float32) x = tl.load(X + (r * C)[:, None] + row[None, :], mask=mask).to(tl.float32) _mean, _m2, _weight = welford_combine(_mean, _m2, _weight, x, m2_, weight_) _mean = tl.view(_mean, (BLOCK_SIZE * ROW_SIZE, )) _m2 = tl.view(_m2, (BLOCK_SIZE * ROW_SIZE, )) _weight = tl.view(_weight, (BLOCK_SIZE * ROW_SIZE, )) mean, m2, weight = tl.reduce((_mean, _m2, _weight), 0, welford_combine) # var = m2 / weight # rstd = 1. / tl.sqrt(var + eps) offset = pid_batch * groups * cluster_num + group * cluster_num + cluster tl.store(cluster_mean_ptr + offset, mean) tl.store(cluster_m2_ptr + offset, m2) tl.store(cluster_weight_ptr + offset, weight)
null
17,171
import torch import triton import triton.language as tl from sfast.utils.copy_func import copy_func from . import activation from .utils import welford_combine def welford_combine(mean_1, m2_1, weight_1, mean_2, m2_2, weight_2): def group_norm_4d_channels_last_forward_collect_stats_kernel_stage_2( cluster_mean_ptr, cluster_m2_ptr, cluster_weight_ptr, N, groups, cluster_num, eps, mean_ptr, rstd_ptr, BLOCK_SIZE: tl.constexpr, ): group = tl.program_id(0) pid_batch = tl.program_id(1) block = tl.arange(0, BLOCK_SIZE) mask = block < cluster_num offset = pid_batch * groups * cluster_num + group * cluster_num + block cluster_mean = tl.load(cluster_mean_ptr + offset, mask=mask) cluster_m2 = tl.load(cluster_m2_ptr + offset, mask=mask) cluster_weight = tl.load(cluster_weight_ptr + offset, mask=mask) mean, m2, weight = tl.reduce((cluster_mean, cluster_m2, cluster_weight), 0, welford_combine) var = m2 / weight rstd = 1. / tl.sqrt(var + eps) offset = pid_batch * groups + group tl.store(mean_ptr + offset, mean) tl.store(rstd_ptr + offset, rstd)
null
17,172
import torch try: from torch._prims_common import suggest_memory_format except ImportError: from sfast.utils.memory_format import suggest_memory_format import triton import triton.language as tl from sfast.utils.copy_func import copy_func from . import activation from .utils import welford_combine def group_norm_4d_forward_kernel( input_ptr, gamma_ptr, beta_ptr, N, C, HxW, groups, eps, output_ptr, mean_ptr, rstd_ptr, C_G, GROUP_SIZE, BLOCK_SIZE: tl.constexpr, ): group = tl.program_id(0) pid_batch = tl.program_id(1) offset = pid_batch * C * HxW + group * GROUP_SIZE X = input_ptr + offset Y = output_ptr + offset _mean = tl.zeros((BLOCK_SIZE, ), dtype=tl.float32) _m2 = tl.zeros((BLOCK_SIZE, ), dtype=tl.float32) _weight = tl.zeros((BLOCK_SIZE, ), dtype=tl.float32) for off in range(0, GROUP_SIZE, BLOCK_SIZE): r = off + tl.arange(0, BLOCK_SIZE) x = tl.load(X + r, mask=r < GROUP_SIZE).to(tl.float32) m2_ = tl.zeros((BLOCK_SIZE, ), dtype=tl.float32) weight_ = (r < GROUP_SIZE).to(tl.float32) _mean, _m2, _weight = welford_combine(_mean, _m2, _weight, x, m2_, weight_) mean, m2, weight = tl.reduce((_mean, _m2, _weight), 0, welford_combine) var = m2 / weight rstd = 1. / tl.sqrt(var + eps) if mean_ptr is not None: tl.store(mean_ptr + pid_batch * groups + group, mean) if rstd_ptr is not None: tl.store(rstd_ptr + pid_batch * groups + group, rstd) if gamma_ptr is None and beta_ptr is None: for c in range(0, C_G): a = rstd b = -a * mean for off in range(0, HxW, BLOCK_SIZE): r = off + tl.arange(0, BLOCK_SIZE) x = tl.load(X + c * HxW + r, mask=r < HxW).to(tl.float32) x = a * x + b x = act(x) tl.store(Y + c * HxW + r, x, mask=r < HxW) else: for c in range(0, C_G): if gamma_ptr is None: gamma = 1. else: gamma = tl.load(gamma_ptr + group * C_G + c).to(tl.float32) if beta_ptr is None: beta = 0. else: beta = tl.load(beta_ptr + group * C_G + c).to(tl.float32) a = rstd * gamma b = beta - a * mean for off in range(0, HxW, BLOCK_SIZE): r = off + tl.arange(0, BLOCK_SIZE) x = tl.load(X + c * HxW + r, mask=r < HxW).to(tl.float32) x = a * x + b x = act(x) tl.store(Y + c * HxW + r, x, mask=r < HxW) def create_group_norm_4d_forward_kernel(act=activation.identity): kernel = group_norm_4d_forward_kernel kernel = copy_func(kernel, globals={ **globals(), **{ 'act': act } }, name=f'{kernel.__name__}_{act.__name__}') kernel = triton.heuristics({ 'BLOCK_SIZE': lambda kwargs: min(4096, triton.next_power_of_2(kwargs['HxW'])), })(triton.heuristics({ 'num_warps': lambda kwargs: max( 1, min(16, triton.next_power_of_2(kwargs['HxW'] // 128))), 'C_G': lambda kwargs: kwargs['C'] // kwargs['groups'], 'GROUP_SIZE': lambda kwargs: kwargs['C'] // kwargs['groups'] * kwargs['HxW'], })(triton.jit(kernel))) return kernel 'ROW_SIZE': lambda kwargs: triton.next_power_of_2(kwargs['C'] // kwargs['groups']), 'BLOCK_SIZE': lambda kwargs: max( 1, min(triton.next_power_of_2(kwargs['HxW']), 4096 // (triton.next_power_of_2(kwargs['C'] // kwargs['groups'])) )), def group_norm_4d_channels_last_forward_collect_stats_kernel( input_ptr, N, C, HxW, groups, eps, mean_ptr, rstd_ptr, C_G, ROW_SIZE: tl.constexpr, BLOCK_SIZE: tl.constexpr, ): group = tl.program_id(0) pid_batch = tl.program_id(1) offset = pid_batch * C * HxW + group * C_G X = input_ptr + offset _mean = tl.zeros((BLOCK_SIZE, ROW_SIZE), dtype=tl.float32) _m2 = tl.zeros((BLOCK_SIZE, ROW_SIZE), dtype=tl.float32) _weight = tl.zeros((BLOCK_SIZE, ROW_SIZE), dtype=tl.float32) row = tl.arange(0, ROW_SIZE) for off in range(0, HxW, BLOCK_SIZE): r = off + tl.arange(0, BLOCK_SIZE) m2_ = tl.zeros((BLOCK_SIZE, ROW_SIZE), dtype=tl.float32) mask = (r < HxW)[:, None] & (row[None, :] < C_G) weight_ = mask.to(tl.float32) x = tl.load(X + (r * C)[:, None] + row[None, :], mask=mask).to(tl.float32) _mean, _m2, _weight = welford_combine(_mean, _m2, _weight, x, m2_, weight_) _mean = tl.view(_mean, (BLOCK_SIZE * ROW_SIZE, )) _m2 = tl.view(_m2, (BLOCK_SIZE * ROW_SIZE, )) _weight = tl.view(_weight, (BLOCK_SIZE * ROW_SIZE, )) mean, m2, weight = tl.reduce((_mean, _m2, _weight), 0, welford_combine) var = m2 / weight rstd = 1. / tl.sqrt(var + eps) offset = pid_batch * groups + group tl.store(mean_ptr + offset, mean) tl.store(rstd_ptr + offset, rstd) 'ROW_SIZE': lambda kwargs: triton.next_power_of_2(kwargs['C'] // kwargs['groups']), 'BLOCK_SIZE': lambda kwargs: max( 1, min(triton.next_power_of_2(kwargs['cluster_size']), 4096 // (triton.next_power_of_2(kwargs['C'] // kwargs['groups'])) )), def group_norm_4d_channels_last_forward_apply_kernel( input_ptr, gamma_ptr, beta_ptr, mean_ptr, rstd_ptr, N, C, HxW, groups, eps, output_ptr, C_G, ROW_SIZE: tl.constexpr, BLOCK_SIZE: tl.constexpr, ): hw = tl.program_id(0) * BLOCK_SIZE pid_batch = tl.program_id(1) offset = pid_batch * C * HxW X = input_ptr + offset Y = output_ptr + offset group_row = tl.arange(0, ROW_SIZE) group_row = group_row // C_G group_mask = group_row < groups mean = tl.load(mean_ptr + pid_batch * groups + group_row, mask=group_mask) rstd = tl.load(rstd_ptr + pid_batch * groups + group_row, mask=group_mask) row = tl.arange(0, ROW_SIZE) mask = row < C if gamma_ptr is None: gamma = tl.full((ROW_SIZE, ), 1., dtype=mean.dtype) else: gamma = tl.load(gamma_ptr + row, mask=mask) if beta_ptr is None: beta = tl.zeros((ROW_SIZE, ), dtype=mean.dtype) else: beta = tl.load(beta_ptr + row, mask=mask) a = rstd * gamma b = beta - a * mean a = a[None, :] b = b[None, :] r = hw + tl.arange(0, BLOCK_SIZE) x = tl.load(X + (r * C)[:, None] + row[None, :], mask=(r < HxW)[:, None] & mask[None, :]) x = a * x + b x = act(x) tl.store(Y + (r * C)[:, None] + row[None, :], x, mask=(r < HxW)[:, None] & mask[None, :]) def create_group_norm_4d_channels_last_forward_apply_kernel( act=activation.identity): kernel = group_norm_4d_channels_last_forward_apply_kernel kernel = copy_func(kernel, globals={ **globals(), **{ 'act': act } }, name=f'{kernel.__name__}_{act.__name__}') kernel = triton.heuristics({ 'ROW_SIZE': lambda kwargs: triton.next_power_of_2(kwargs['C']), 'BLOCK_SIZE': lambda kwargs: max( 1, min(triton.next_power_of_2(kwargs['HxW']), 4096 // triton. next_power_of_2(kwargs['C']))), })(triton.heuristics({ 'num_warps': lambda kwargs: max( 1, min(16, kwargs['ROW_SIZE'] * kwargs['BLOCK_SIZE'] // 128)), 'C_G': lambda kwargs: kwargs['C'] // kwargs['groups'], })(triton.jit(kernel))) return kernel group_norm_forward = create_group_norm_forward() def suggest_memory_format(x: TensorLikeType) -> torch.memory_format: if x.layout != torch.strided: return torch.contiguous_format if are_strides_like_channels_last(x.shape, x.stride()): return torch.channels_last if x.ndim == 4 else torch.channels_last_3d return torch.contiguous_format def create_group_norm_forward(act=activation.identity): group_norm_4d_forward_kernel = create_group_norm_4d_forward_kernel(act=act) group_norm_4d_channels_last_forward_apply_kernel = create_group_norm_4d_channels_last_forward_apply_kernel( act=act) def group_norm_forward(input, num_groups, weight=None, bias=None, eps=1e-05, output_mean=True, output_rstd=True): assert input.device.type == 'cuda' assert 2 <= input.ndim <= 4 dim_padding = 0 while input.ndim < 4: input = input.unsqueeze(-1) dim_padding += 1 shape = input.shape N, C, H, W = shape assert C % num_groups == 0 assert weight is None or weight.shape == (C, ) assert bias is None or bias.shape == (C, ) if weight is not None: assert weight.device.type == 'cuda' weight = weight.contiguous() if bias is not None: assert bias.device.type == 'cuda' bias = bias.contiguous() memory_format = suggest_memory_format(input) if memory_format == torch.channels_last: input = input.contiguous(memory_format=torch.channels_last) # cluster_num = 32 # cluster_size = (H * W + cluster_num - 1) // cluster_num # mean_cluster = torch.empty((N, num_groups, cluster_num), # dtype=torch.float32, # device=input.device) # m2_cluster = torch.empty((N, num_groups, cluster_num), # dtype=torch.float32, # device=input.device) # weight_cluster = torch.empty((N, num_groups, cluster_num), # dtype=torch.float32, # device=input.device) # def grid(meta): # return (num_groups, cluster_num, N) # group_norm_4d_channels_last_forward_collect_stats_kernel_stage_1[ # grid](input, N, C, H * W, num_groups, cluster_size, # cluster_num, mean_cluster, m2_cluster, weight_cluster) mean = torch.empty(( N, num_groups, ), dtype=input.dtype, device=input.device) rstd = torch.empty(( N, num_groups, ), dtype=input.dtype, device=input.device) # def grid(meta): # return (num_groups, N) # group_norm_4d_channels_last_forward_collect_stats_kernel_stage_2[ # grid](mean_cluster, m2_cluster, weight_cluster, N, num_groups, # cluster_num, eps, mean, rstd) # del mean_cluster, m2_cluster, weight_cluster def grid(meta): return (num_groups, N) group_norm_4d_channels_last_forward_collect_stats_kernel[grid]( input, N, C, H * W, num_groups, eps, mean, rstd) output = torch.empty_like(input) def grid(meta): return (triton.cdiv(H * W, meta['BLOCK_SIZE']), N) group_norm_4d_channels_last_forward_apply_kernel[grid]( input, weight, bias, mean, rstd, N, C, H * W, num_groups, eps, output) if not output_mean: mean = None if not output_rstd: rstd = None else: mean = torch.empty( ( N, num_groups, ), dtype=input.dtype, device=input.device) if output_mean else None rstd = torch.empty( ( N, num_groups, ), dtype=input.dtype, device=input.device) if output_rstd else None input = input.contiguous() output = torch.empty_like(input) def grid(meta): return (num_groups, N) group_norm_4d_forward_kernel[grid](input, weight, bias, N, C, H * W, num_groups, eps, output, mean, rstd) while dim_padding > 0: output = output.squeeze(-1) dim_padding -= 1 return output, mean, rstd return group_norm_forward
null
17,173
import torch try: from torch._prims_common import suggest_memory_format except ImportError: from sfast.utils.memory_format import suggest_memory_format import triton import triton.language as tl from sfast.utils.copy_func import copy_func from . import activation from .utils import welford_combine group_norm_forward = create_group_norm_forward() def test_group_norm(): x = torch.randn(2, 320, 32, 32).cuda().half() groups = 32 weight = torch.randn(x.shape[1]).cuda().half() beta = torch.randn(x.shape[1]).cuda().half() torch.cuda.synchronize() begin_time = time.time() y = F.group_norm(x, groups, weight, beta, 1e-5) torch.cuda.synchronize() print('torch time: ', time.time() - begin_time) torch.cuda.synchronize() begin_time = time.time() y_triton = group_norm_forward(x, groups, weight, beta, 1e-5)[0] torch.cuda.synchronize() print('triton time: ', time.time() - begin_time) torch.testing.assert_close(y_triton, y, rtol=1e-2, atol=1e-2) x_channel_last = x.contiguous(memory_format=torch.channels_last) torch.cuda.synchronize() begin_time = time.time() y_channel_last = F.group_norm(x_channel_last, groups, weight, beta, 1e-5) y_channel_last = y_channel_last.contiguous( memory_format=torch.channels_last) torch.cuda.synchronize() print('torch channels last time: ', time.time() - begin_time) torch.cuda.synchronize() begin_time = time.time() y_triton_channel_last = group_norm_forward(x_channel_last, groups, weight, beta, 1e-5)[0] torch.cuda.synchronize() print('triton channels last time: ', time.time() - begin_time) torch.testing.assert_close(y_triton_channel_last, y_channel_last, rtol=1e-2, atol=1e-2)
null
17,174
import functools import operator import torch import triton import triton.language as tl from .utils import welford_combine def welford_combine(mean_1, m2_1, weight_1, mean_2, m2_2, weight_2): delta = mean_2 - mean_1 new_weight = weight_1 + weight_2 # w2_over_w = weight_2 / new_weight w2_over_w = tl.where(new_weight == 0.0, 0.0, weight_2 / new_weight) return ( mean_1 + delta * w2_over_w, m2_1 + m2_2 + delta * delta * weight_1 * w2_over_w, new_weight, ) def _layer_norm_fwd_fused( X, # pointer to the input Y, # pointer to the output W, # pointer to the weights B, # pointer to the biases Mean, # pointer to the mean Rstd, # pointer to the 1/std stride: tl. constexpr, # how much to increase the pointer when moving by 1 row N: tl.constexpr, # number of columns in X eps, # epsilon to avoid division by zero BLOCK_SIZE: tl.constexpr, ): # Map the program id to the row of X and Y it should compute. row = tl.program_id(0) Y += row * stride X += row * stride if BLOCK_SIZE >= N: cols = tl.arange(0, BLOCK_SIZE) x = tl.load(X + cols, mask=cols < N).to(tl.float32) m2_ = tl.zeros((BLOCK_SIZE, ), dtype=tl.float32) weight_ = (cols < N).to(tl.float32) _mean, _m2, _weight = x, m2_, weight_ else: # Compute mean _mean = tl.zeros((BLOCK_SIZE, ), dtype=tl.float32) _m2 = tl.zeros((BLOCK_SIZE, ), dtype=tl.float32) _weight = tl.zeros((BLOCK_SIZE, ), dtype=tl.float32) for off in range(0, N, BLOCK_SIZE): cols = off + tl.arange(0, BLOCK_SIZE) x = tl.load(X + cols, mask=cols < N).to(tl.float32) m2_ = tl.zeros((BLOCK_SIZE, ), dtype=tl.float32) weight_ = (cols < N).to(tl.float32) if off == 0: _mean, _m2, _weight = x, m2_, weight_ else: _mean, _m2, _weight = welford_combine(_mean, _m2, _weight, x, m2_, weight_) mean, m2, weight = tl.reduce((_mean, _m2, _weight), 0, welford_combine) var = m2 / weight rstd = 1 / tl.sqrt(var + eps) mean = mean.to(x.dtype) rstd = rstd.to(x.dtype) # Write mean / rstd if Mean is not None: tl.store(Mean + row, mean) if Rstd is not None: tl.store(Rstd + row, rstd) # Normalize and apply linear transformation if BLOCK_SIZE >= N: cols = tl.arange(0, BLOCK_SIZE) mask = cols < N if W is None: w = tl.full((BLOCK_SIZE, ), 1.0, dtype=x.dtype) else: w = tl.load(W + cols, mask=mask) if B is None: b = tl.zeros((BLOCK_SIZE, ), dtype=x.dtype) else: b = tl.load(B + cols, mask=mask) # x = tl.load(X + cols, mask=mask).to(tl.float32) x_hat = (x - mean) * rstd y = x_hat * w + b # Write output tl.store(Y + cols, y, mask=mask) else: for off in range(0, N, BLOCK_SIZE): cols = off + tl.arange(0, BLOCK_SIZE) mask = cols < N if W is None: w = tl.full((BLOCK_SIZE, ), 1.0, dtype=x.dtype) else: w = tl.load(W + cols, mask=mask) if B is None: b = tl.zeros((BLOCK_SIZE, ), dtype=x.dtype) else: b = tl.load(B + cols, mask=mask) x = tl.load(X + cols, mask=mask) x_hat = (x - mean) * rstd y = x_hat * w + b # Write output tl.store(Y + cols, y, mask=mask)
null
17,175
import functools import operator import torch import triton import triton.language as tl from .utils import welford_combine def _layer_norm_bwd_dx_fused( DX, # pointer to the input gradient DY, # pointer to the output gradient DW, # pointer to the partial sum of weights gradient DB, # pointer to the partial sum of biases gradient X, # pointer to the input W, # pointer to the weights B, # pointer to the biases Mean, # pointer to the mean Rstd, # pointer to the 1/std Lock, # pointer to the lock stride: tl. constexpr, # how much to increase the pointer when moving by 1 row N: tl.constexpr, # number of columns in X eps, # epsilon to avoid division by zero GROUP_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr): # Map the program id to the elements of X, DX, and DY it should compute. row = tl.program_id(0) cols = tl.arange(0, BLOCK_SIZE_N) mask = cols < N X += row * stride DY += row * stride DX += row * stride # Offset locks and weights/biases gradient pointer for parallel reduction lock_id = row % GROUP_SIZE_M Lock += lock_id Count = Lock + GROUP_SIZE_M DW = DW + lock_id * N + cols DB = DB + lock_id * N + cols # Load data to SRAM x = tl.load(X + cols, mask=mask, other=0).to(tl.float32) dy = tl.load(DY + cols, mask=mask, other=0).to(tl.float32) w = tl.load(W + cols, mask=mask).to(tl.float32) mean = tl.load(Mean + row) rstd = tl.load(Rstd + row) # Compute dx xhat = (x - mean) * rstd wdy = w * dy xhat = tl.where(mask, xhat, 0.) wdy = tl.where(mask, wdy, 0.) c1 = tl.sum(xhat * wdy, axis=0) / N c2 = tl.sum(wdy, axis=0) / N dx = (wdy - (xhat * c1 + c2)) * rstd # Write dx tl.store(DX + cols, dx, mask=mask) # Accumulate partial sums for dw/db partial_dw = (dy * xhat).to(w.dtype) partial_db = (dy).to(w.dtype) while tl.atomic_cas(Lock, 0, 1) == 1: pass count = tl.load(Count) # First store doesn't accumulate if count == 0: tl.atomic_xchg(Count, 1) else: partial_dw += tl.load(DW, mask=mask) partial_db += tl.load(DB, mask=mask) tl.store(DW, partial_dw, mask=mask) tl.store(DB, partial_db, mask=mask) # Release the lock tl.atomic_xchg(Lock, 0)
null
17,176
import functools import operator import torch import triton import triton.language as tl from .utils import welford_combine def _layer_norm_bwd_dwdb( DW, # pointer to the partial sum of weights gradient DB, # pointer to the partial sum of biases gradient FINAL_DW, # pointer to the weights gradient FINAL_DB, # pointer to the biases gradient M, # GROUP_SIZE_M N, # number of columns BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr): # Map the program id to the elements of DW and DB it should compute. pid = tl.program_id(0) cols = pid * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N) dw = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) db = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) # Iterate through the rows of DW and DB to sum the partial sums. for i in range(0, M, BLOCK_SIZE_M): rows = i + tl.arange(0, BLOCK_SIZE_M) mask = (rows[:, None] < M) & (cols[None, :] < N) offs = rows[:, None] * N + cols[None, :] dw += tl.load(DW + offs, mask=mask, other=0.) db += tl.load(DB + offs, mask=mask, other=0.) # Write the final sum to the output. sum_dw = tl.sum(dw, axis=0) sum_db = tl.sum(db, axis=0) tl.store(FINAL_DW + cols, sum_dw, mask=cols < N) tl.store(FINAL_DB + cols, sum_db, mask=cols < N)
null
17,177
import functools import operator import torch import triton import triton.language as tl from .utils import welford_combine layer_norm = LayerNorm.apply def test_layer_norm(M, N, dtype, eps=1e-5, device='cuda'): # create data x_shape = (M, N) w_shape = (x_shape[-1], ) weight = torch.rand(w_shape, dtype=dtype, device='cuda', requires_grad=True) bias = torch.rand(w_shape, dtype=dtype, device='cuda', requires_grad=True) x = -2.3 + 0.5 * torch.randn(x_shape, dtype=dtype, device='cuda') dy = .1 * torch.randn_like(x) x.requires_grad_(True) # forward pass y_tri = layer_norm(x, w_shape, weight, bias, eps) y_ref = torch.nn.functional.layer_norm(x, w_shape, weight, bias, eps).to(dtype) # backward pass (triton) y_tri.backward(dy, retain_graph=True) dx_tri, dw_tri, db_tri = [_.grad.clone() for _ in [x, weight, bias]] x.grad, weight.grad, bias.grad = None, None, None # backward pass (torch) y_ref.backward(dy, retain_graph=True) dx_ref, dw_ref, db_ref = [_.grad.clone() for _ in [x, weight, bias]] # compare assert torch.allclose(y_tri, y_ref, atol=1e-2, rtol=0) assert torch.allclose(dx_tri, dx_ref, atol=1e-2, rtol=0) assert torch.allclose(db_tri, db_ref, atol=1e-2, rtol=0) assert torch.allclose(dw_tri, dw_ref, atol=1e-2, rtol=0)
null
17,178
import functools import operator import torch import triton import triton.language as tl from .utils import welford_combine layer_norm = LayerNorm.apply def bench_layer_norm(M, N, dtype, provider, mode='backward', eps=1e-5, device='cuda'): # create data x_shape = (M, N) w_shape = (x_shape[-1], ) weight = torch.rand(w_shape, dtype=dtype, device='cuda', requires_grad=True) bias = torch.rand(w_shape, dtype=dtype, device='cuda', requires_grad=True) x = -2.3 + 0.5 * torch.randn(x_shape, dtype=dtype, device='cuda') dy = .1 * torch.randn_like(x) x.requires_grad_(True) quantiles = [0.5, 0.2, 0.8] # utility functions if provider == 'triton': def y_fwd(): return layer_norm(x, w_shape, weight, bias, eps) # noqa: F811, E704 if provider == 'torch': def y_fwd(): return torch.nn.functional.layer_norm(x, w_shape, weight, bias, eps) # noqa: F811, E704 if provider == 'apex': apex_layer_norm = apex.normalization.FusedLayerNorm(w_shape).to( x.device).to(x.dtype) def y_fwd(): return apex_layer_norm(x) # noqa: F811, E704 # forward pass if mode == 'forward': def gbps(ms): return 2 * x.numel() * x.element_size() / ms * 1e-6 ms, min_ms, max_ms = triton.testing.do_bench(y_fwd, quantiles=quantiles, rep=500) # backward pass if mode == 'backward': def gbps(ms): return 3 * x.numel() * x.element_size( ) / ms * 1e-6 # noqa: F811, E704 y = y_fwd() ms, min_ms, max_ms = triton.testing.do_bench( lambda: y.backward(dy, retain_graph=True), quantiles=quantiles, grad_to_none=[x], rep=500) return gbps(ms), gbps(max_ms), gbps(min_ms)
null
17,179
import heapq import torch import triton import triton.language as tl def estimate_conv_time( # backend, device, num_warps, num_stages, x, BATCH, IN_C, IN_H, IN_W, KERNEL_N, KERNEL_H, KERNEL_W, OUT_H, OUT_W, BLOCK_M, BLOCK_K, BLOCK_N, debug=False, **kwargs, ): def early_config_prune(configs, named_args): def conv_heuristics(): configs = [ triton.Config({ "BLOCK_M": 128, "BLOCK_N": 128, "BLOCK_K": 32 }, num_stages=2, num_warps=8), triton.Config({ "BLOCK_M": 256, "BLOCK_N": 64, "BLOCK_K": 32 }, num_stages=2, num_warps=8), triton.Config({ "BLOCK_M": 256, "BLOCK_N": 32, "BLOCK_K": 32 }, num_stages=4, num_warps=4), triton.Config({ "BLOCK_M": 256, "BLOCK_N": 32, "BLOCK_K": 64 }, num_stages=4, num_warps=4), triton.Config({ "BLOCK_M": 256, "BLOCK_N": 16, "BLOCK_K": 32 }, num_stages=4, num_warps=2), triton.Config({ "BLOCK_M": 64, "BLOCK_N": 128, "BLOCK_K": 32 }, num_stages=4, num_warps=8), triton.Config({ "BLOCK_M": 128, "BLOCK_N": 64, "BLOCK_K": 32 }, num_stages=4, num_warps=4), triton.Config({ "BLOCK_M": 64, "BLOCK_N": 64, "BLOCK_K": 32 }, num_stages=4, num_warps=4), triton.Config({ "BLOCK_M": 128, "BLOCK_N": 16, "BLOCK_K": 32 }, num_stages=4, num_warps=4), triton.Config({ "BLOCK_M": 128, "BLOCK_N": 128, "BLOCK_K": 128 }, num_stages=3, num_warps=8), triton.Config({ "BLOCK_M": 256, "BLOCK_N": 64, "BLOCK_K": 128 }, num_stages=3, num_warps=8), triton.Config({ "BLOCK_M": 256, "BLOCK_N": 32, "BLOCK_K": 128 }, num_stages=4, num_warps=4), triton.Config({ "BLOCK_M": 64, "BLOCK_N": 128, "BLOCK_K": 128 }, num_stages=4, num_warps=4), triton.Config({ "BLOCK_M": 128, "BLOCK_N": 64, "BLOCK_K": 128 }, num_stages=4, num_warps=4), triton.Config({ "BLOCK_M": 128, "BLOCK_N": 32, "BLOCK_K": 64 }, num_stages=4, num_warps=2), triton.Config({ "BLOCK_M": 64, "BLOCK_N": 64, "BLOCK_K": 64 }, num_stages=4, num_warps=2), # triton.Config( # {"BLOCK_M": 128, "BLOCK_N": 16, "BLOCK_K": 64}, num_stages=4, num_warps=2 # ), ] key = [ "BATCH", "IN_C", "IN_H", "IN_W", "KERNEL_N", "KERNEL_H", "KERNEL_W", "OUT_H", "OUT_W", # parameters of conv "stride_h", "stride_w", "padding_h", "padding_w", "dilation_h", "dilation_w", "output_padding_h", "output_padding_w", "groups", ] prune_configs_by = { "early_config_prune": early_config_prune, "perf_model": estimate_conv_time, "top_k": 10, } return triton.autotune(configs, key, prune_configs_by=prune_configs_by)
null
17,180
import heapq import torch import triton import triton.language as tl def _unpack(idx, order, shape): if torch.is_tensor(idx): _12 = torch.div(idx, shape[order[0]], rounding_mode="trunc") _0 = idx % shape[order[0]] _2 = torch.div(_12, shape[order[1]], rounding_mode="trunc") _1 = _12 % shape[order[1]] else: _12 = idx // shape[order[0]] _0 = idx % shape[order[0]] _2 = _12 // shape[order[1]] _1 = _12 % shape[order[1]] return _0, _1, _2
null
17,181
import heapq import torch import triton import triton.language as tl The provided code snippet includes necessary dependencies for implementing the `_kernel_delta_x_hwc` function. Write a Python function `def _kernel_delta_x_hwc( x, w, bias, y, # stride of tensor stride_xn, stride_xc, stride_xh, stride_xw, stride_wn, stride_wc, stride_wh, stride_ww, stride_yn, stride_yc, stride_yh, stride_yw, # pointer inc for x delta_xh_ptr, delta_xw_ptr, delta_xc_ptr, # Tensor dimensions BATCH, IN_C, IN_H, IN_W, KERNEL_N, KERNEL_H, KERNEL_W, OUT_H, OUT_W, # parameters of conv stride_h, stride_w, padding_h, padding_w, dilation_h, dilation_w, output_padding_h, output_padding_w, groups, # Metaparameters ACC_TYPE: tl.constexpr, CONV1X1_NHWC: tl.constexpr, # blocks in different dimension BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, # reduction tiling parameter for matmul BLOCK_K: tl.constexpr, # Super-blocking for better L2 peformance GROUP_H: tl.constexpr, WITH_BIAS: tl.constexpr, )` to solve the following problem: each program instance computes a [BLOCK_BATCH, BLOCK_N, BLOCK_H, BLOCK_W] block of y Here is the function: def _kernel_delta_x_hwc( x, w, bias, y, # stride of tensor stride_xn, stride_xc, stride_xh, stride_xw, stride_wn, stride_wc, stride_wh, stride_ww, stride_yn, stride_yc, stride_yh, stride_yw, # pointer inc for x delta_xh_ptr, delta_xw_ptr, delta_xc_ptr, # Tensor dimensions BATCH, IN_C, IN_H, IN_W, KERNEL_N, KERNEL_H, KERNEL_W, OUT_H, OUT_W, # parameters of conv stride_h, stride_w, padding_h, padding_w, dilation_h, dilation_w, output_padding_h, output_padding_w, groups, # Metaparameters ACC_TYPE: tl.constexpr, CONV1X1_NHWC: tl.constexpr, # blocks in different dimension BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, # reduction tiling parameter for matmul BLOCK_K: tl.constexpr, # Super-blocking for better L2 peformance GROUP_H: tl.constexpr, WITH_BIAS: tl.constexpr, ): """ each program instance computes a [BLOCK_BATCH, BLOCK_N, BLOCK_H, BLOCK_W] block of y """ # ----------------------------------------------------------- # Map program ids `pid` to the block of y it should compute. pid_nhw = tl.program_id(0) pid_k = tl.program_id(1) # offset for output y off_y_k = pid_k * BLOCK_N + tl.arange(0, BLOCK_N) off_y_nhw = pid_nhw * BLOCK_M + tl.arange(0, BLOCK_M) off_y_n = off_y_nhw // (OUT_H * OUT_W) off_y_hw = off_y_nhw % (OUT_H * OUT_W) off_y_h = off_y_hw // OUT_W + output_padding_h off_y_w = off_y_hw % OUT_W + output_padding_w # offset for the initial ptr for x off_x_n = off_y_n off_x_h = off_y_h * stride_h - padding_h off_x_w = off_y_w * stride_w - padding_w off_x_nhw = off_x_n * stride_xn + off_x_h * stride_xh + off_x_w * stride_xw off_x_crs = tl.arange(0, BLOCK_K) CRS = IN_C * KERNEL_H * KERNEL_W # load inc ptr of x, upade x_ptrs if not CONV1X1_NHWC: delta_xh_ptrs = delta_xh_ptr + off_x_crs delta_xw_ptrs = delta_xw_ptr + off_x_crs delta_xc_ptrs = delta_xc_ptr + off_x_crs delta_xh = tl.load(delta_xh_ptrs, mask=off_x_crs < CRS, other=0) delta_xw = tl.load(delta_xw_ptrs, mask=off_x_crs < CRS, other=0) delta_xc = tl.load(delta_xc_ptrs, mask=off_x_crs < CRS, other=0) off_x_crs_unpacked = (delta_xh * stride_xh + delta_xw * stride_xw + delta_xc * stride_xc) x_ptrs = x + off_x_nhw[:, None] + off_x_crs_unpacked[None, :] else: x_ptrs = x + off_x_nhw[:, None] + off_x_crs[None, :] delta_xh = 0 delta_xw = 0 mask_x = ((off_x_n < BATCH)[:, None] & (off_x_crs < CRS)[None, :] & (off_x_h[:, None] + delta_xh[None, :] >= 0) & (off_x_h[:, None] + delta_xh[None, :] < IN_H) & (off_x_w[:, None] + delta_xw[None, :] >= 0) & (off_x_w[:, None] + delta_xw[None, :] < IN_W)) # offset for the inital ptr for w off_w_crs = tl.arange(0, BLOCK_K) off_w_k = off_y_k w_ptrs = w + off_w_crs[:, None] + off_w_k[None, :] * stride_wn mask_w = (off_x_crs < CRS)[:, None] & (off_w_k < KERNEL_N)[None, :] # ------ load x ------ matrix_x = tl.load(x_ptrs, mask=mask_x, other=0.0) # ------ load w ------ matrix_w = tl.load(w_ptrs, mask=mask_w, other=0.0) # ----------------------------------------------------------- # allocate accumulator acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=ACC_TYPE) for crs in range(0, CRS, BLOCK_K): # ------ matrix multiplication ------ acc += tl.dot(matrix_x, matrix_w, out_dtype=ACC_TYPE) # ------ update ptrs ------ w_ptrs += BLOCK_K # load inc ptr of x, upade x_ptrs off_x_crs = crs + BLOCK_K + tl.arange(0, BLOCK_K) if not CONV1X1_NHWC: delta_xh_ptrs += BLOCK_K delta_xw_ptrs += BLOCK_K delta_xc_ptrs += BLOCK_K delta_xh = tl.load(delta_xh_ptrs, mask=off_x_crs < CRS, other=0) delta_xw = tl.load(delta_xw_ptrs, mask=off_x_crs < CRS, other=0) delta_xc = tl.load(delta_xc_ptrs, mask=off_x_crs < CRS, other=0) off_x_crs_unpacked = (delta_xh * stride_xh + delta_xw * stride_xw + delta_xc * stride_xc) x_ptrs = x + off_x_nhw[:, None] + off_x_crs_unpacked[None, :] else: x_ptrs += BLOCK_K mask_x = ((off_x_n < BATCH)[:, None] & (off_x_crs < CRS)[None, :] & (off_x_h[:, None] + delta_xh[None, :] >= 0) & (off_x_h[:, None] + delta_xh[None, :] < IN_H) & (off_x_w[:, None] + delta_xw[None, :] >= 0) & (off_x_w[:, None] + delta_xw[None, :] < IN_W)) mask_w = (off_x_crs < CRS)[:, None] & (off_w_k < KERNEL_N)[None, :] # ------ prefetch ------ # ------ load x ------ matrix_x = tl.load(x_ptrs, mask=mask_x, other=0.0) # ------ load w ------ matrix_w = tl.load(w_ptrs, mask=mask_w, other=0.0) if WITH_BIAS: acc += tl.load(bias + off_y_k)[None, :] acc = acc.to(y.dtype.element_ty) # rematerialize -- this saves some registers # offset for output y off_y_k = pid_k * BLOCK_N + tl.arange(0, BLOCK_N) off_y_nhw = pid_nhw * BLOCK_M + tl.arange(0, BLOCK_M) off_y_n = off_y_nhw // (OUT_H * OUT_W) off_y_hw = off_y_nhw % (OUT_H * OUT_W) # consider output padding off_y_h = off_y_hw // OUT_W + output_padding_h off_y_w = off_y_hw % OUT_W + output_padding_w # y ptrs in the block of [BLOCK_M, BLOCK_N] y_ptrs = (y + off_y_n[:, None] * stride_yn + off_y_h[:, None] * stride_yh + off_y_w[:, None] * stride_yw + off_y_k[None, :] * stride_yc) # out-of-bounds check mask_y = ((off_y_n < BATCH)[:, None] & (off_y_h < OUT_H + output_padding_h)[:, None] & (off_y_w < OUT_W + output_padding_w)[:, None] & (off_y_k < KERNEL_N)[None, :]) tl.store(y_ptrs, acc, mask=mask_y) return
each program instance computes a [BLOCK_BATCH, BLOCK_N, BLOCK_H, BLOCK_W] block of y
17,182
import heapq import torch import triton import triton.language as tl The provided code snippet includes necessary dependencies for implementing the `_kernel_delta_x` function. Write a Python function `def _kernel_delta_x( x, w, bias, y, # stride of tensor stride_xn, stride_xc, stride_xh, stride_xw, stride_wn, stride_wc, stride_wh, stride_ww, stride_yn, stride_yc, stride_yh, stride_yw, # pointer inc for x delta_x_ptr, # Tensor dimensions BATCH, IN_C, IN_H, IN_W, KERNEL_N, KERNEL_H, KERNEL_W, OUT_H, OUT_W, # parameters of conv stride_h, stride_w, padding_h, padding_w, dilation_h, dilation_w, output_padding_h, output_padding_w, groups, # Metaparameters ACC_TYPE: tl.constexpr, CONV1X1_NHWC: tl.constexpr, # blocks in different dimension BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, # reduction tiling parameter for matmul BLOCK_K: tl.constexpr, # Super-blocking for better L2 peformance GROUP_H: tl.constexpr, WITH_BIAS: tl.constexpr, )` to solve the following problem: each program instance computes a [BLOCK_BATCH, BLOCK_N, BLOCK_H, BLOCK_W] block of y Here is the function: def _kernel_delta_x( x, w, bias, y, # stride of tensor stride_xn, stride_xc, stride_xh, stride_xw, stride_wn, stride_wc, stride_wh, stride_ww, stride_yn, stride_yc, stride_yh, stride_yw, # pointer inc for x delta_x_ptr, # Tensor dimensions BATCH, IN_C, IN_H, IN_W, KERNEL_N, KERNEL_H, KERNEL_W, OUT_H, OUT_W, # parameters of conv stride_h, stride_w, padding_h, padding_w, dilation_h, dilation_w, output_padding_h, output_padding_w, groups, # Metaparameters ACC_TYPE: tl.constexpr, CONV1X1_NHWC: tl.constexpr, # blocks in different dimension BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, # reduction tiling parameter for matmul BLOCK_K: tl.constexpr, # Super-blocking for better L2 peformance GROUP_H: tl.constexpr, WITH_BIAS: tl.constexpr, ): """ each program instance computes a [BLOCK_BATCH, BLOCK_N, BLOCK_H, BLOCK_W] block of y """ # ----------------------------------------------------------- # Map program ids `pid` to the block of y it should compute. pid_nhw = tl.program_id(0) pid_k = tl.program_id(1) # offset for output y off_y_k = pid_k * BLOCK_N + tl.arange(0, BLOCK_N) off_y_nhw = pid_nhw * BLOCK_M + tl.arange(0, BLOCK_M) off_y_n = off_y_nhw // (OUT_H * OUT_W) off_y_hw = off_y_nhw % (OUT_H * OUT_W) off_y_h = off_y_hw // OUT_W + output_padding_h off_y_w = off_y_hw % OUT_W + output_padding_w # offset for the initial ptr for x off_x_n = off_y_n off_x_h = off_y_h * stride_h - padding_h off_x_w = off_y_w * stride_w - padding_w off_x_nhw = off_x_n * stride_xn + off_x_h * stride_xh + off_x_w * stride_xw off_x_crs = tl.arange(0, BLOCK_K) CRS = IN_C * KERNEL_H * KERNEL_W # load inc ptr of x, upade x_ptrs if not CONV1X1_NHWC: delta_x_ptrs = delta_x_ptr + off_x_crs off_x_crs_unpacked = tl.load(delta_x_ptrs, mask=off_x_crs < CRS) x_ptrs = x + off_x_nhw[:, None] + off_x_crs_unpacked[None, :] else: x_ptrs = x + off_x_nhw[:, None] + off_x_crs[None, :] mask_x = ((off_x_n < BATCH) & (off_x_h >= 0) & (off_x_h < IN_H) & (off_x_w >= 0) & (off_x_w < IN_W))[:, None] & (off_x_crs < CRS)[None, :] # offset for the inital ptr for w off_w_crs = tl.arange(0, BLOCK_K) off_w_k = off_y_k w_ptrs = w + off_w_crs[:, None] + off_w_k[None, :] * stride_wn mask_w = (off_x_crs < CRS)[:, None] & (off_w_k < KERNEL_N)[None, :] # ------ load x ------ matrix_x = tl.load(x_ptrs, mask=mask_x, other=0.0) # ------ load w ------ matrix_w = tl.load(w_ptrs, mask=mask_w, other=0.0) # ----------------------------------------------------------- # allocate accumulator acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=ACC_TYPE) for crs in range(0, CRS, BLOCK_K): # ------ matrix multiplication ------ acc += tl.dot(matrix_x, matrix_w, out_dtype=ACC_TYPE) # ------ update ptrs ------ w_ptrs += BLOCK_K # load inc ptr of x, upade x_ptrs if not CONV1X1_NHWC: delta_x_ptrs += BLOCK_K off_x_crs = crs + BLOCK_K + tl.arange(0, BLOCK_K) off_x_crs_unpacked = tl.load(delta_x_ptrs, mask=off_x_crs < CRS, other=0) x_ptrs = x + off_x_nhw[:, None] + off_x_crs_unpacked[None, :] else: off_x_crs = crs + BLOCK_K + tl.arange(0, BLOCK_K) x_ptrs += BLOCK_K mask_x = ((off_x_n < BATCH) & (off_x_h >= 0) & (off_x_h < IN_H) & (off_x_w >= 0) & (off_x_w < IN_W))[:, None] & (off_x_crs < CRS)[None, :] mask_w = (off_x_crs < CRS)[:, None] & (off_w_k < KERNEL_N)[None, :] # ------ prefetch ------ # ------ load x ------ matrix_x = tl.load(x_ptrs, mask=mask_x, other=0.0) # ------ load w ------ matrix_w = tl.load(w_ptrs, mask=mask_w, other=0.0) if WITH_BIAS: acc += tl.load(bias + off_y_k)[None, :] acc = acc.to(y.dtype.element_ty) # rematerialize -- this saves some registers # offset for output y off_y_k = pid_k * BLOCK_N + tl.arange(0, BLOCK_N) off_y_nhw = pid_nhw * BLOCK_M + tl.arange(0, BLOCK_M) off_y_n = off_y_nhw // (OUT_H * OUT_W) off_y_hw = off_y_nhw % (OUT_H * OUT_W) # consider output padding off_y_h = off_y_hw // OUT_W + output_padding_h off_y_w = off_y_hw % OUT_W + output_padding_w # y ptrs in the block of [BLOCK_M, BLOCK_N] y_ptrs = (y + off_y_n[:, None] * stride_yn + off_y_h[:, None] * stride_yh + off_y_w[:, None] * stride_yw + off_y_k[None, :] * stride_yc) # out-of-bounds check mask_y = ((off_y_n < BATCH)[:, None] & (off_y_h < OUT_H + output_padding_h)[:, None] & (off_y_w < OUT_W + output_padding_w)[:, None] & (off_y_k < KERNEL_N)[None, :]) tl.store(y_ptrs, acc, mask=mask_y) return
each program instance computes a [BLOCK_BATCH, BLOCK_N, BLOCK_H, BLOCK_W] block of y
17,183
import triton import triton.language as tl def silu(x): return x * tl.sigmoid(x.to(tl.float32)).to(x.dtype)
null
17,184
import triton import triton.language as tl def relu(x): return tl.max(x, 0.0)
null
17,185
import triton import triton.language as tl def gelu(x): return 0.5 * x * (1.0 + tl.tanh(0.7978845608028654 * (x + 0.044715 * x * x * x)))
null
17,186
import torch import triton import triton.language as tl from itertools import product def copy(dst, src): def test_transpose(x): print('--------------------------------') print('Input Shape: ', x.shape) print('Input Bytes: ', x.numel() * x.element_size()) begin = time.time() transposed = x.transpose(-1, -2) out = torch.empty_like(transposed).contiguous() torch.cuda.synchronize() out = out.copy_(transposed) torch.cuda.synchronize() print(torch.allclose(out, transposed)) end = time.time() print('PyTorch Time: ', end - begin) del out transposed = x.transpose(-1, -2) out = torch.empty_like(transposed).contiguous() torch.cuda.synchronize() begin = time.time() out = copy(out, transposed) torch.cuda.synchronize() print(torch.allclose(out, transposed)) end = time.time() print('Triton Time: ', end - begin)
null
17,187
import functools import torch from torch._dynamo.backends.registry import register_backend from torch._dynamo.backends.common import aot_autograd, fake_tensor_unsupported from functorch.compile import make_boxed_compiler from sfast.jit.trace_helper import trace_with_kwargs def sfast_jit_script(gm, example_inputs, *, ts_compiler=None, **kwargs): ts = torch.jit.script(gm, **kwargs) if ts_compiler is not None: ts = ts_compiler(ts, example_inputs) return ts
null
17,188
import functools import torch from torch._dynamo.backends.registry import register_backend from torch._dynamo.backends.common import aot_autograd, fake_tensor_unsupported from functorch.compile import make_boxed_compiler from sfast.jit.trace_helper import trace_with_kwargs def gen_jit_aot_compiler(compiler, ts_compiler): def sfast_jit_script_aot_autograd(gm, example_inputs, *, fw_ts_compiler=None, bw_ts_compiler=None, **kwargs): fw_compiler = gen_jit_aot_compiler( torch.jit.script, fw_ts_compiler, **kwargs) bw_compiler = gen_jit_aot_compiler( torch.jit.script, bw_ts_compiler, **kwargs) return aot_autograd(fw_compiler=fw_compiler, bw_compiler=bw_compiler)(gm, example_inputs)
null
17,189
import functools import torch from torch._dynamo.backends.registry import register_backend from torch._dynamo.backends.common import aot_autograd, fake_tensor_unsupported from functorch.compile import make_boxed_compiler from sfast.jit.trace_helper import trace_with_kwargs def gen_jit_aot_compiler(compiler, ts_compiler): wrapped = functools.partial(compiler, ts_compiler=ts_compiler) return make_boxed_compiler(wrapped) def sfast_jit_trace(gm, example_inputs, *, ts_compiler=None, **kwargs): # If check_trace is True, the tracer will run with the example_inputs after tracing. # This will cause the GraphFunction executor to generate some cached optimized graphs first. # (torch/csrc/jit/api/function_impl.h: get_executor()) # But we might modify its graph later, so we don't want to cache it. # So we set check_trace to False. ts, call_helper = trace_with_kwargs(gm, example_inputs, **kwargs) if ts_compiler is not None: ts = ts_compiler(ts, call_helper(ts).convert_inputs(example_inputs)) return call_helper(ts) def sfast_jit_trace_aot_autograd(gm, example_inputs, *, fw_ts_compiler=None, bw_ts_compiler=None, **kwargs): fw_compiler = gen_jit_aot_compiler( sfast_jit_trace, fw_ts_compiler, **kwargs) bw_compiler = gen_jit_aot_compiler( sfast_jit_trace, bw_ts_compiler, **kwargs) return aot_autograd(fw_compiler=fw_compiler, bw_compiler=bw_compiler)(gm, example_inputs)
null
17,190
import functools from .backends.registry import _lazy_import def _lazy_import(): from .. import backends from torch._dynamo.utils import import_submodule import_submodule(backends)
null
17,191
import logging import inspect import functools import threading import torch from sfast.utils import flat_tensors from sfast.utils.copy import tree_copy from sfast.hooks.module_jit_hook import (apply_to_all_modules, apply_to_module) from .utils import better_trace def can_io_obj_be_perfectly_traced(obj): return flat_tensors.can_be_perfectly_flattened(obj)
null
17,192
import logging import torch def jit_pass_optimize_contiguous(graph): if hasattr(torch.ops.sfast_triton, 'contiguous'): torch._C._jit_pass_custom_pattern_based_rewrite_graph( ''' graph(%1, %2): %x = aten::contiguous(%1, %2) return (%x)''', ''' graph(%1, %2): %x = sfast_triton::contiguous(%1, %2) return (%x)''', graph)
null
17,193
import io import functools import cProfile import pstats def with_cProfile(*amount, out_func=None, file=None): def _with_cProfile(func): @functools.wraps(func) def wrapper(*args, **kwargs): pr = cProfile.Profile() try: retval = pr.runcall(func, *args, **kwargs) return retval finally: if out_func is None: stats_stream = io.StringIO() stats = pstats.Stats(pr, stream=stats_stream).sort_stats( pstats.SortKey.CUMULATIVE) stats.print_stats(*amount) msg = stats_stream.getvalue() if file is None: print(msg) else: print(msg, file=file) else: out_func(pr) return wrapper return _with_cProfile
null
17,194
import torch def device_has_tensor_core(): if torch.cuda.is_available(): major, minor = torch.cuda.get_device_capability() return major >= 7 return False
null
17,195
import torch def device_has_capability(major, minor): if torch.cuda.is_available(): major_, minor_ = torch.cuda.get_device_capability() return (major_, minor_) >= (major, minor) return False
null
17,196
import torch import sfast registered_custom_python_operator_names = set() def register_custom_python_operator(schema, callable): name = torch._C.parse_schema(schema).name if name in registered_custom_python_operator_names: return sfast._C._jit_register_custom_python_operator(schema, callable) registered_custom_python_operator_names.add(name)
null
17,197
import contextlib import packaging.version from functorch.compile import (aot_function, aot_module) import torch def no_fake_tensor(): if packaging.version.parse( torch.__version__) >= packaging.version.parse("2.0.0"): from torch._functorch import config use_fake_tensor = config.use_fake_tensor config.use_fake_tensor = False try: yield finally: config.use_fake_tensor = use_fake_tensor else: yield
null
17,198
import contextlib import packaging.version from functorch.compile import (aot_function, aot_module) import torch def get_compiler_fn(title=None): def aot_printer(fn): if isinstance(fn, torch.nn.Module): return aot_module(fn, fw_compiler=get_compiler_fn("Forward Code:"), bw_compiler=get_compiler_fn("Backward Code:")) else: return aot_function(fn, fw_compiler=get_compiler_fn("Forward Code:"), bw_compiler=get_compiler_fn("Backward Code:"))
null
17,199
import logging import torch from torch.utils._python_dispatch import TorchDispatchMode def with_dispatch_mode(dispatch_mode): def decorator(func): def wrapper(*args, **kwargs): with dispatch_mode(): return func(*args, **kwargs) return wrapper return decorator
null
17,200
from __future__ import print_function import base64 import os import sys def print_osc(terminal): if terminal.startswith('screen') or terminal.startswith('tmux'): print_partial("\033Ptmux;\033\033]") else: print_partial("\033]") def print_st(terminal): if terminal.startswith('screen') or terminal.startswith('tmux'): print_partial("\a\033\\") else: print_partial("\a") def print_partial(msg): print(msg, end='') def print_image(image_file_name=None, data=None, width=None, height=None): terminal = os.environ.get('TERM', '') print_osc(terminal) print_partial('1337;File=') args = [] if image_file_name: b64_file_name = base64.b64encode(image_file_name.encode('ascii')).decode('ascii') args.append('name=' + b64_file_name) with open(image_file_name, "rb") as image_file: b64_data = base64.b64encode(image_file.read()).decode('ascii') elif data: b64_data = base64.b64encode(data).decode('ascii') else: raise ValueError("Expected image_file_name or data") args.append('size=' + str(len(b64_data))) if width is not None: args.append('width=' + str(width)) if height is not None: args.append('height=' + str(height)) args.append("inline=1") print_partial(';'.join(args)) print_partial(":") print_partial(b64_data) print_st(terminal)
null