-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: sallyom <[email protected]>
- Loading branch information
Showing
6 changed files
with
457 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ click = "*" | |
[dev-packages] | ||
|
||
[requires] | ||
python_version = "3.12" | ||
python_version = "3.11" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .components import run_mt_bench_op, load_mt_bench_results_op, parse_output_to_dict | ||
#from . import faked | ||
|
||
__all__ = ["run_mt_bench_op", "load_mt_bench_results_op", "parse_output_to_dict"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# type: ignore | ||
# pylint: disable=no-value-for-parameter,import-outside-toplevel,import-error | ||
from typing import List, NamedTuple | ||
from kfp import dsl | ||
from kfp.dsl import component, Input, Output, Artifact, Model, importer | ||
|
||
INSTRUCTLAB_IMAGE = "registry.redhat.io/rhelai1/instructlab-nvidia-rhel9:1.1" | ||
TOOLBOX_IMAGE = "registry.access.redhat.com/ubi9/toolbox" | ||
#MODELS_INFO = "kfp-models" | ||
#JUDGE_SECRET = "judge-server" | ||
|
||
@component(base_image=TOOLBOX_IMAGE) | ||
def parse_output_to_dict(output: str): | ||
lines = output.strip().split('\n') | ||
report = {} | ||
|
||
# Iterate through each line and extract key-value pairs | ||
for line in lines: | ||
line = line.strip() | ||
if line.startswith("## MODEL"): | ||
report["model"] = lines[lines.index(line) + 1].strip() | ||
elif line.startswith("### AVERAGE"): | ||
average_line = lines[lines.index(line) + 1].strip() | ||
report["average"] = float(average_line.split()[0]) | ||
report["average_details"] = average_line | ||
elif line.startswith("### TURN ONE"): | ||
report["turn_one"] = float(lines[lines.index(line) + 1].strip()) | ||
elif line.startswith("### TURN TWO"): | ||
report["turn_two"] = float(lines[lines.index(line) + 1].strip()) | ||
elif line.startswith("### ERROR RATE"): | ||
report["error_rate"] = float(lines[lines.index(line) + 1].strip()) | ||
|
||
return report | ||
|
||
|
||
@component(base_image=INSTRUCTLAB_IMAGE) | ||
def run_mt_bench_op( | ||
mt_bench_output: Output[Artifact], | ||
models_path_prefix: str, | ||
judge_model_path: str, | ||
models_list: List[int], | ||
) -> NamedTuple("outputs", best_model=str, best_score=float) : | ||
import json | ||
import os | ||
import subprocess | ||
import typing | ||
|
||
Outputs = NamedTuple("outputs", best_model=str, best_score=float) | ||
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True" | ||
|
||
# ilab CLI does not support external models | ||
#judge_model_name = os.getenv("JUDGE_MODEL_NAME") | ||
#judge_endpoint = os.getenv("JUDGE_ENDPOINT") | ||
#judge_api_key = os.getenv("JUDGE_API_KEY", "") | ||
|
||
#os.environ["ILAB_MODELS_DIR"] = "{models_path_prefix}" | ||
|
||
scores = {} | ||
all_mt_bench_data = [] | ||
for model_name in models_list: | ||
model_path = f"{models_path_prefix}/{model_name}" | ||
|
||
#ilab model evaluate --benchmark mt_bench | ||
# --model $ILAB_MODELS_DIR/granite-7b-lab-Q4_K_M.gguf | ||
# --judge-model $ILAB_MODELS_DIR/granite-7b-lab-Q4_K_M.gguf | ||
|
||
command = ["ilab", "model", "evaluate", "--benchmark", "mt_bench", "--model", "{models_path_prefix}/{model_name}", "--judge-model", "{judge_model_path}"] | ||
result = subprocess.run(command, capture_output=True, text=True) | ||
|
||
print(result.stdout) | ||
print(result.stderr) | ||
if result.returncode == 0: | ||
print("MT_BENCH executed successfully!") | ||
else: | ||
print("MT_BENCH failed with return code:", result.returncode) | ||
|
||
output_str = result.stdout | ||
output_dict = parse_output_to_dict(output_str) | ||
overall_score = output_dict["average"] | ||
mt_bench_data = json.dumps(output_dict, indent=4) | ||
|
||
all_mt_bench_data.append(mt_bench_data) | ||
scores[model_path] = overall_score | ||
|
||
with open(mt_bench_output.path, 'w') as f: | ||
json.dump(all_mt_bench_data, f, indent=4) | ||
|
||
best_model = max(scores, key=scores.get) | ||
best_score = scores[best_model] | ||
return Outputs(best_model, best_score) | ||
|
||
@component(base_image=TOOLBOX_IMAGE) | ||
def load_mt_bench_results_op(mt_bench_output: Input[Artifact]) -> list: | ||
import json | ||
|
||
mt_bench_score_list = [] | ||
with open(mt_bench_output.path, 'r') as f: | ||
mt_bench_score_list = json.load(f) | ||
|
||
print("MT_BENCH Evaluation Data:") | ||
for mt_bench_score in mt_bench_score_list: | ||
print(json.dumps(mt_bench_score, indent=4)) | ||
|
||
return mt_bench_score_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.