|
| 1 | +# make a langchain graph instance with the model |
| 2 | +from typing_extensions import TypedDict, List, Annotated, Literal |
| 3 | +from dataset_and_llm import llm |
| 4 | +from prompts import make_prompt, FLOPCounts |
| 5 | +from io_cost import get_query_cost |
| 6 | +import operator |
| 7 | +from langgraph.graph import StateGraph, END |
| 8 | +from langgraph.checkpoint.sqlite import SqliteSaver |
| 9 | +from langchain.schema import AIMessage |
| 10 | +from configuration import Configuration |
| 11 | +import sqlite3 |
| 12 | + |
| 13 | +class BaselineQueryState(TypedDict, total=False): |
| 14 | + source_code: str |
| 15 | + combined_name: str |
| 16 | + kernel_name: str |
| 17 | + exec_args: str |
| 18 | + grid_size: str |
| 19 | + block_size: str |
| 20 | + total_num_threads: str |
| 21 | + compile_commands: str |
| 22 | + |
| 23 | + empirical_sp_flop_count: float |
| 24 | + empirical_dp_flop_count: float |
| 25 | + |
| 26 | + prompt_type: Literal["simple", "full"] |
| 27 | + |
| 28 | + raw_flop_counts: Annotated[List[AIMessage], operator.add] |
| 29 | + |
| 30 | + predicted_sp_flop_count: int |
| 31 | + predicted_dp_flop_count: int |
| 32 | + predicted_sp_flop_count_explanation: str |
| 33 | + predicted_dp_flop_count_explanation: str |
| 34 | + |
| 35 | + input_tokens: Annotated[List[int], operator.add] |
| 36 | + output_tokens: Annotated[List[int], operator.add] |
| 37 | + total_cost: Annotated[List[float], operator.add] |
| 38 | + |
| 39 | + total_query_time: Annotated[List[float], operator.add] |
| 40 | + error: Annotated[List[str], operator.add] |
| 41 | + |
| 42 | +# Calculate the total number of threads from the gridSz and the blockSz |
| 43 | +# grid size is a string of format "(x, y, z)" |
| 44 | +# block size is a string of format "(x, y, z)" |
| 45 | +def calc_total_threads(gridSz:str, blockSz:str): |
| 46 | + gridSz = eval(gridSz) |
| 47 | + blockSz = eval(blockSz) |
| 48 | + total_threads = gridSz[0] * gridSz[1] * gridSz[2] * blockSz[0] * blockSz[1] * blockSz[2] |
| 49 | + return str(total_threads) |
| 50 | + |
| 51 | +def get_input_problem(state: BaselineQueryState, config): |
| 52 | + verbose = config.get("configurable", {}).get("verbose_printing", False) |
| 53 | + |
| 54 | + row = config.get("configurable", {}).get("input_problem_row", None) |
| 55 | + |
| 56 | + prompt_type = config.get("configurable", {}).get("prompt_type", "simple") |
| 57 | + |
| 58 | + combined_name = row['combined_name'] |
| 59 | + |
| 60 | + assert row is not None, f"Target problem '{combined_name}' not found in the dataset." |
| 61 | + |
| 62 | + if verbose: |
| 63 | + print("---------- BEGIN STEP 0: GET INPUT PROBLEM ----------", flush=True) |
| 64 | + |
| 65 | + to_return = {'source_code' : row['source_code'], |
| 66 | + 'combined_name' : combined_name, |
| 67 | + 'kernel_name' : row['Kernel Name'], |
| 68 | + 'exec_args' : row['exeArgs'], |
| 69 | + 'grid_size' : row['Grid Size'], |
| 70 | + 'block_size' : row['Block Size'], |
| 71 | + 'total_num_threads' : calc_total_threads(row['Grid Size'], row['Block Size']), |
| 72 | + 'compile_commands' : row['compile_commands'], |
| 73 | + # these "true" values do not get passed to the LLMs |
| 74 | + # they are used to calculate how close the LLM prediction is to the ground-truth |
| 75 | + 'empirical_sp_flop_count' : row['SP_FLOP'], |
| 76 | + 'empirical_dp_flop_count' : row['DP_FLOP'], |
| 77 | + 'prompt_type' : prompt_type |
| 78 | + } |
| 79 | + |
| 80 | + if verbose: |
| 81 | + for k, v in to_return.items(): |
| 82 | + if k != "source_code": |
| 83 | + print(f"\t{k}: {v}", flush=True) |
| 84 | + print("---------- END STEP 0: GET INPUT PROBLEM ----------", flush=True) |
| 85 | + |
| 86 | + return to_return |
| 87 | + |
| 88 | + |
| 89 | +def query_for_flop_count(state: BaselineQueryState, config): |
| 90 | + verbose = config.get("configurable", {}).get("verbose_printing", False) |
| 91 | + |
| 92 | + configured_llm = llm.with_config(configurable=config.get("configurable", {})).with_structured_output(FLOPCounts, include_raw=True) |
| 93 | + |
| 94 | + prompt = make_prompt(state['prompt_type']) |
| 95 | + |
| 96 | + chain = prompt | configured_llm |
| 97 | + |
| 98 | + if verbose: |
| 99 | + print("---------- BEGIN STEP 1: QUERY FOR FLOP COUNT ----------", flush=True) |
| 100 | + print(f"\tQuerying for FLOP count of kernel: {state['combined_name']}", flush=True) |
| 101 | + |
| 102 | + result = chain.invoke({ |
| 103 | + "source_code": state['source_code'], |
| 104 | + "kernel_name": state['kernel_name'], |
| 105 | + "exec_args": state['exec_args'], |
| 106 | + "grid_size": state['grid_size'], |
| 107 | + "block_size": state['block_size'], |
| 108 | + "total_num_threads": state['total_num_threads'], |
| 109 | + "compile_commands": state['compile_commands'] |
| 110 | + }) |
| 111 | + |
| 112 | + parsed_result = result['parsed'] |
| 113 | + |
| 114 | + if verbose: |
| 115 | + result['raw'].pretty_print() |
| 116 | + # check if the sp_flop_count attributes are present and not None |
| 117 | + if parsed_result.sp_flop_count is not None and parsed_result.dp_flop_count is not None: |
| 118 | + print(f"\tGot an LLM response!: \n\tSP_FLOP:[{parsed_result.sp_flop_count}], \n\tDP_FLOP:[{parsed_result.dp_flop_count}]\n", flush=True) |
| 119 | + |
| 120 | + query_cost = get_query_cost(result['raw'], verbose) |
| 121 | + |
| 122 | + return query_cost | {'predicted_sp_flop_count': parsed_result.sp_flop_count, |
| 123 | + 'predicted_dp_flop_count': parsed_result.dp_flop_count, |
| 124 | + 'predicted_sp_flop_count_explanation': parsed_result.sp_flop_explanation, |
| 125 | + 'predicted_dp_flop_count_explanation': parsed_result.dp_flop_explanation, |
| 126 | + 'raw_flop_counts': [result['raw']] |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | +def make_graph(sqlite_db_path: str): |
| 131 | + # now let's set up the StateGraph to represent the agent |
| 132 | + workflow = StateGraph(BaselineQueryState, context_schema=Configuration) |
| 133 | + workflow.add_node("get_input_problem_0", get_input_problem) |
| 134 | + workflow.add_node("query_for_flop_count_1", query_for_flop_count) |
| 135 | + |
| 136 | + workflow.add_edge("get_input_problem_0", "query_for_flop_count_1") |
| 137 | + workflow.add_edge("query_for_flop_count_1", END) |
| 138 | + |
| 139 | + workflow.set_entry_point("get_input_problem_0") |
| 140 | + |
| 141 | + # let's also add a checkpointer to save intermediate results |
| 142 | + # sqlite_db_path: path to sqlite database used by SqliteSaver to persist graph checkpoints |
| 143 | + conn = sqlite3.connect(sqlite_db_path, check_same_thread=False) |
| 144 | + checkpointer = SqliteSaver(conn) |
| 145 | + graph = workflow.compile(checkpointer=checkpointer) |
| 146 | + |
| 147 | + return graph |
| 148 | + |
| 149 | + |
| 150 | + |
0 commit comments