{ "cells": [ { "cell_type": "markdown", "id": "local-0", "metadata": {}, "source": [ "# 2026-2 RAG · RTX 5070 Ti 후속 평가\n", "\n", "1차 제출과 같은 공식 BM25 후보를 사용해 BGE 가중치의 효과를 분리합니다.\n", "같은 파인튜닝 BGE 순위에서 1차 답변 프롬프트와 근거 중심 프롬프트를 비교합니다.\n", "정답은 순위나 답변을 만드는 데 사용하지 않으며, 마지막 채점에서만 사용합니다.\n", "\n", "[전처리 코퍼스·PID 매핑·BM25 색인](https://huggingface.co/datasets/EllaNAM/rag-assignment-processed-bm25/tree/45666fd968034ff46d875613d08f5bd070462e96) · `45666fd968034ff46d875613d08f5bd070462e96`\n", "\n", "완성된 Windows 전처리 코퍼스와 BM25 색인에서 동일한 비교를 실행합니다.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "local-1", "metadata": { "execution": { "iopub.execute_input": "2026-12-04T13:23:42.996796Z", "iopub.status.busy": "2026-12-04T13:23:42.996796Z", "iopub.status.idle": "2026-12-04T13:23:49.051742Z", "shell.execute_reply": "2026-12-04T13:23:49.051742Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\hjn40\\Documents\\Codex\\2026-12-04\\zip-2026-2-rag-windows-pc\\work\\submission\\Source Code\\.venv-win\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "NVIDIA GeForce RTX 5070 Ti 12.8\n" ] } ], "source": [ "from pathlib import Path\n", "import os\n", "import sys\n", "\n", "PROJECT = Path.cwd()\n", "if not (PROJECT / 'src').is_dir():\n", " PROJECT = PROJECT / 'Source Code'\n", "sys.path.insert(0, str(PROJECT))\n", "sys.path.insert(0, str(PROJECT / 'src'))\n", "ROOT = Path(os.environ.get('RAG_WORK_ROOT', 'D:/project/rag-assignment-2026-2'))\n", "os.environ.setdefault('HF_HUB_CACHE', str(ROOT / 'hf' / 'hub'))\n", "os.environ.setdefault('HF_HUB_DISABLE_IMPLICIT_TOKEN', '1')\n", "os.environ.setdefault('JAX_PLATFORMS', 'cpu')\n", "\n", "import torch\n", "from local_followup import prepare, rank, generate, compare\n", "\n", "LIMIT = None # Dev 1,000문항. 실행 점검은 8 등으로 설정합니다.\n", "USE_PROCESSED = True # True면 완성된 로컬 전처리 색인을 별도 평가합니다.\n", "ARTIFACT_REPO = 'EllaNAM/rag-assignment-processed-bm25'\n", "ARTIFACT_REVISION = '45666fd968034ff46d875613d08f5bd070462e96'\n", "probe = torch.ones((16, 16), device='cuda', dtype=torch.float16)\n", "assert (probe @ probe)[0, 0].item() == 16\n", "print(torch.cuda.get_device_name(), torch.version.cuda)\n", "del probe\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "local-2", "metadata": { "execution": { "iopub.execute_input": "2026-12-04T13:23:49.053750Z", "iopub.status.busy": "2026-12-04T13:23:49.053750Z", "iopub.status.idle": "2026-12-04T13:23:49.068256Z", "shell.execute_reply": "2026-12-04T13:23:49.068256Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\r", "Load D:\\project\\rag-assignment-2026-2\\data\\dev_qa.jsonl...: 0it [00:00, ?it/s]" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\r", "Load D:\\project\\rag-assignment-2026-2\\data\\dev_qa.jsonl...: 1000it [00:00, 286378.81it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "1000 D:\\project\\rag-assignment-2026-2\\dev-followup\\processed\\ad2292d5950a\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "processed_root = ROOT / 'local-colab-artifacts'\n", "processed_files = ('processed_corpus.jsonl', 'pid_remap.json', 'bm25/sparse_index.json',\n", " 'bm25/data.csc.index.npy', 'bm25/indices.csc.index.npy',\n", " 'bm25/indptr.csc.index.npy', 'bm25/params.index.json', 'bm25/vocab.index.json')\n", "if USE_PROCESSED and not all((processed_root / name).is_file() for name in processed_files):\n", " if ARTIFACT_REVISION is None:\n", " raise RuntimeError('전처리 데이터의 검증된 공개 커밋을 매개변수에 입력하세요.')\n", " from huggingface_hub import snapshot_download\n", " snapshot_download(\n", " ARTIFACT_REPO, repo_type='dataset', revision=ARTIFACT_REVISION,\n", " local_dir=processed_root, token=False,\n", " allow_patterns=['processed_corpus.jsonl', 'pid_remap.json', 'bm25/*', 'build-info.json'],\n", " )\n", "qa, settings, output = prepare(ROOT, LIMIT, USE_PROCESSED)\n", "output.mkdir(parents=True, exist_ok=True)\n", "print(len(qa), output)\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "local-3", "metadata": { "execution": { "iopub.execute_input": "2026-12-04T13:23:49.068256Z", "iopub.status.busy": "2026-12-04T13:23:49.068256Z", "iopub.status.idle": "2026-12-04T13:25:56.589111Z", "shell.execute_reply": "2026-12-04T13:25:56.589111Z" } }, "outputs": [], "source": [ "# MiniLM 후보는 한 번만 계산해 두 BGE 모델에 공통으로 사용합니다.\n", "ranking = rank(qa, settings, output)\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "local-4", "metadata": { "execution": { "iopub.execute_input": "2026-12-04T13:25:56.589111Z", "iopub.status.busy": "2026-12-04T13:25:56.589111Z", "iopub.status.idle": "2026-12-04T13:33:10.928009Z", "shell.execute_reply": "2026-12-04T13:33:10.928009Z" } }, "outputs": [], "source": [ "# 리랭커가 GPU에서 내려간 상태에서 Qwen을 한 번만 올립니다.\n", "generate(qa, settings, output, ranking)\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "local-5", "metadata": { "execution": { "iopub.execute_input": "2026-12-04T13:33:10.930096Z", "iopub.status.busy": "2026-12-04T13:33:10.929098Z", "iopub.status.idle": "2026-12-04T13:33:34.054170Z", "shell.execute_reply": "2026-12-04T13:33:34.054170Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
questionsrecall@100recall@5span_ememf1generation_seconds
experiment
first-prompt10000.8940.7750.4850.3830.506548213.922352
evidence-prompt10000.8940.7750.4490.3830.485398205.577156
\n", "
" ], "text/plain": [ " questions recall@100 recall@5 span_em em f1 \\\n", "experiment \n", "first-prompt 1000 0.894 0.775 0.485 0.383 0.506548 \n", "evidence-prompt 1000 0.894 0.775 0.449 0.383 0.485398 \n", "\n", " generation_seconds \n", "experiment \n", "first-prompt 213.922352 \n", "evidence-prompt 205.577156 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "answer_string_in_top5 775\n", "em_first_misses_with_string 394\n", "em_recovered_with_string 51\n", "em_regressed_with_string 50\n", "span_em_first_misses_with_string 293\n", "span_em_recovered_with_string 31\n", "span_em_regressed_with_string 66\n", "first_span_match_only 101\n", "first_raw_span_lost_after_postprocessing 0\n", "dtype: int64" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "90" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 공식 채점과 문항별 결합으로 지표 및 답변 실패 전이를 계산합니다.\n", "result = compare(qa, settings, output, ranking)\n", "import pandas as pd\n", "display(pd.DataFrame(result['runs']).set_index('experiment'))\n", "display(pd.Series(result['paired']))\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.13" } }, "nbformat": 4, "nbformat_minor": 5 }