Files
pytorch/tools/testing/target_determination/heuristics/llm.py
Xuehai Pan b6bdb67f82 [BE][Easy] use pathlib.Path instead of dirname / ".." / pardir (#129374)
Changes by apply order:

1. Replace all `".."` and `os.pardir` usage with `os.path.dirname(...)`.
2. Replace nested `os.path.dirname(os.path.dirname(...))` call with `str(Path(...).parent.parent)`.
3. Reorder `.absolute()` ~/ `.resolve()`~ and `.parent`: always resolve the path first.

    `.parent{...}.absolute()` -> `.absolute().parent{...}`

4. Replace chained `.parent x N` with `.parents[${N - 1}]`: the code is easier to read (see 5.)

    `.parent.parent.parent.parent` -> `.parents[3]`

5. ~Replace `.parents[${N - 1}]` with `.parents[${N} - 1]`: the code is easier to read and does not introduce any runtime overhead.~

    ~`.parents[3]` -> `.parents[4 - 1]`~

6. ~Replace `.parents[2 - 1]` with `.parent.parent`: because the code is shorter and easier to read.~

Pull Request resolved: https://github.com/pytorch/pytorch/pull/129374
Approved by: https://github.com/justinchuby, https://github.com/malfet
2024-12-29 17:23:13 +00:00

56 lines
1.8 KiB
Python

from __future__ import annotations
import json
import os
import re
from collections import defaultdict
from pathlib import Path
from typing import Any
from tools.stats.import_test_stats import ADDITIONAL_CI_FILES_FOLDER
from tools.testing.target_determination.heuristics.interface import (
HeuristicInterface,
TestPrioritizations,
)
from tools.testing.target_determination.heuristics.utils import normalize_ratings
from tools.testing.test_run import TestRun
REPO_ROOT = Path(__file__).resolve().parents[4]
class LLM(HeuristicInterface):
def __init__(self, **kwargs: dict[str, Any]) -> None:
super().__init__(**kwargs)
def get_prediction_confidence(self, tests: list[str]) -> TestPrioritizations:
critical_tests = self.get_mappings()
filter_valid_tests = {
TestRun(test): score
for test, score in critical_tests.items()
if test in tests
}
normalized_scores = normalize_ratings(filter_valid_tests, 0.25)
return TestPrioritizations(tests, normalized_scores)
def get_mappings(self) -> dict[str, float]:
path = (
REPO_ROOT
/ ADDITIONAL_CI_FILES_FOLDER
/ "llm_results/mappings/indexer-files-gitdiff-output.json"
)
if not os.path.exists(path):
print(f"could not find path {path}")
return {}
with open(path) as f:
# Group by file
r = defaultdict(list)
for key, value in json.load(f).items():
re_match = re.match("(.*).py", key)
if re_match:
file = re_match.group(1)
r[file].append(value)
# Average the scores for each file
r = {file: sum(scores) / len(scores) for file, scores in r.items()}
return r