{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":84795,"databundleVersionId":10462807},{"sourceType":"modelInstanceVersion","sourceId":11371,"databundleVersionId":7771674,"modelInstanceId":5171}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"\n![](https://opengraph.githubassets.com/589594f788a4448dfe547f5999a901ce44b94d851559d036df16766e02a2ef62/swe-bench/SWE-bench)\n\n# Data Overview\n\ndata/data.parquet The train set metadata, which includes a limited to a handful of examples. You are encouraged to source additional codebases for training your models. Most of the metadata provided here is only available for the train set.\n\n- instance_id - A unique string identifier for the instance (aka GitHub issue).\n- repo - The relevant GitHub repository. Also served by the evaluation API.\n- problem_statement - Text describing the issue. Also served by the evaluation API.\n- patch - Only provided for the train set. The patch resolving the issue.\n- test_patch - Only provided for the train set. The patch resolving the issue.\n- pull_number - The PR number of the pull request resolving the issue.\n- base_commit - The commit used as the basis for the provided copy of the repo.\n- issue_numbers - The original ID number of the issue.\n- [PASS_TO_PASS/FAIL_TO_PASS] - Lists of the unit tests to run for this issue.\n\n### Imports:","metadata":{}},{"cell_type":"code","source":"import kaggle_evaluation.konwinski_prize_inference_server\nimport zipfile\nimport pandas as pd\nfrom datasets import load_dataset\nimport os\nimport shutil\nimport io\nfrom wordcloud import WordCloud\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:37:37.568254Z","iopub.execute_input":"2024-12-20T01:37:37.56942Z","iopub.status.idle":"2024-12-20T01:37:53.054347Z","shell.execute_reply.started":"2024-12-20T01:37:37.569371Z","shell.execute_reply":"2024-12-20T01:37:53.052927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rc = {\n    \"axes.facecolor\": \"#F8F8F8\",\n    \"figure.facecolor\": \"#F8F8F8\",\n    \"axes.edgecolor\": \"#000000\",\n    \"grid.color\": \"#EBEBE7\" + \"30\",\n    \"font.family\": \"serif\",\n    \"axes.labelcolor\": \"#000000\",\n    \"xtick.color\": \"#000000\",\n    \"ytick.color\": \"#000000\",\n    \"grid.alpha\": 0.4,\n}\n\nsns.set(rc=rc)\npalette = ['#302c36', '#037d97', '#E4591E', '#C09741',\n           '#EC5B6D', '#90A6B1', '#6ca957', '#D8E3E2']\n\nfrom colorama import Style, Fore\nblk = Style.BRIGHT + Fore.BLACK\nmgt = Style.BRIGHT + Fore.MAGENTA\nred = Style.BRIGHT + Fore.RED\nblu = Style.BRIGHT + Fore.BLUE\nres = Style.RESET_ALL\n\nplt.style.use('seaborn-v0_8-pastel')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:37:53.056489Z","iopub.execute_input":"2024-12-20T01:37:53.05714Z","iopub.status.idle":"2024-12-20T01:37:53.066756Z","shell.execute_reply.started":"2024-12-20T01:37:53.057059Z","shell.execute_reply":"2024-12-20T01:37:53.065499Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Competition Dataset","metadata":{}},{"cell_type":"code","source":"import zipfile\nkonwinski= zipfile.ZipFile('../input/konwinski-prize/data.a_zip')\nkonwinski.extractall()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:37:53.068159Z","iopub.execute_input":"2024-12-20T01:37:53.068591Z","iopub.status.idle":"2024-12-20T01:37:56.419538Z","shell.execute_reply.started":"2024-12-20T01:37:53.068556Z","shell.execute_reply":"2024-12-20T01:37:56.418432Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"zf = zipfile.ZipFile('../input/konwinski-prize/data.a_zip') \n\ntrain_data = pd.read_parquet(\"data/data.parquet\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:37:56.422007Z","iopub.execute_input":"2024-12-20T01:37:56.422379Z","iopub.status.idle":"2024-12-20T01:37:56.624097Z","shell.execute_reply.started":"2024-12-20T01:37:56.422348Z","shell.execute_reply":"2024-12-20T01:37:56.62281Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:37:56.625502Z","iopub.execute_input":"2024-12-20T01:37:56.625822Z","iopub.status.idle":"2024-12-20T01:37:56.674668Z","shell.execute_reply.started":"2024-12-20T01:37:56.625792Z","shell.execute_reply":"2024-12-20T01:37:56.672981Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Origional Dataset:","metadata":{}},{"cell_type":"code","source":"swebench = load_dataset('princeton-nlp/SWE-bench', split='test')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:37:56.676747Z","iopub.execute_input":"2024-12-20T01:37:56.677316Z","iopub.status.idle":"2024-12-20T01:38:02.626096Z","shell.execute_reply.started":"2024-12-20T01:37:56.677254Z","shell.execute_reply":"2024-12-20T01:38:02.624956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"swebench","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:02.627455Z","iopub.execute_input":"2024-12-20T01:38:02.627793Z","iopub.status.idle":"2024-12-20T01:38:02.635403Z","shell.execute_reply.started":"2024-12-20T01:38:02.627761Z","shell.execute_reply":"2024-12-20T01:38:02.634036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert the dataset to a pandas DataFrame\nswebench = swebench.to_pandas()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:02.636847Z","iopub.execute_input":"2024-12-20T01:38:02.63725Z","iopub.status.idle":"2024-12-20T01:38:02.715142Z","shell.execute_reply.started":"2024-12-20T01:38:02.637218Z","shell.execute_reply":"2024-12-20T01:38:02.713585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"swebench.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:02.716612Z","iopub.execute_input":"2024-12-20T01:38:02.717104Z","iopub.status.idle":"2024-12-20T01:38:02.740606Z","shell.execute_reply.started":"2024-12-20T01:38:02.717029Z","shell.execute_reply":"2024-12-20T01:38:02.739049Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Some Data Visualizations","metadata":{}},{"cell_type":"code","source":"text = ' '.join(swebench['problem_statement'])\nwordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)\nplt.figure(figsize=(14, 5))\nplt.imshow(wordcloud, interpolation='bilinear')\nplt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:02.744845Z","iopub.execute_input":"2024-12-20T01:38:02.745723Z","iopub.status.idle":"2024-12-20T01:38:06.082717Z","shell.execute_reply.started":"2024-12-20T01:38:02.745684Z","shell.execute_reply":"2024-12-20T01:38:06.081572Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"swebench['statement_length'] = swebench['problem_statement'].apply(len)\nplt.figure(figsize=(14,5))\nsns.histplot(x='statement_length', data=swebench, bins=30,kde=True)\nplt.title('Statement Length')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:06.08401Z","iopub.execute_input":"2024-12-20T01:38:06.084357Z","iopub.status.idle":"2024-12-20T01:38:06.847931Z","shell.execute_reply.started":"2024-12-20T01:38:06.084326Z","shell.execute_reply":"2024-12-20T01:38:06.846487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"swebench['patch_length'] = swebench['patch'].apply(len)\nplt.figure(figsize=(14,5))\nsns.histplot(x='patch_length', data=swebench, bins=30,kde=True)\nplt.title('Patch Length')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:06.849853Z","iopub.execute_input":"2024-12-20T01:38:06.850266Z","iopub.status.idle":"2024-12-20T01:38:07.300471Z","shell.execute_reply.started":"2024-12-20T01:38:06.850231Z","shell.execute_reply":"2024-12-20T01:38:07.299098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(14,5))\nsns.scatterplot(data=swebench, x=\"patch_length\", y=\"statement_length\")\nplt.title('Statement Length vs Patch Length')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:07.302318Z","iopub.execute_input":"2024-12-20T01:38:07.302757Z","iopub.status.idle":"2024-12-20T01:38:07.816225Z","shell.execute_reply.started":"2024-12-20T01:38:07.302713Z","shell.execute_reply":"2024-12-20T01:38:07.814961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"swebench.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:07.818234Z","iopub.execute_input":"2024-12-20T01:38:07.818751Z","iopub.status.idle":"2024-12-20T01:38:07.847617Z","shell.execute_reply.started":"2024-12-20T01:38:07.818701Z","shell.execute_reply":"2024-12-20T01:38:07.846001Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Pre-processing","metadata":{}},{"cell_type":"code","source":"df = pd.DataFrame({'Question': swebench['problem_statement'], 'Answer': swebench['patch']})\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:07.849507Z","iopub.execute_input":"2024-12-20T01:38:07.85004Z","iopub.status.idle":"2024-12-20T01:38:07.981943Z","shell.execute_reply.started":"2024-12-20T01:38:07.849988Z","shell.execute_reply":"2024-12-20T01:38:07.980309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"QNA_dataset = []\n    \nfor index, row in df.iterrows():\n    question, answer = row['Question'], row['Answer']\n    template = (f\"Question:\\n{question}\\n\\nAnswer:\\n{answer}\")\n    QNA_dataset.append(template)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:07.983455Z","iopub.execute_input":"2024-12-20T01:38:07.983851Z","iopub.status.idle":"2024-12-20T01:38:08.117476Z","shell.execute_reply.started":"2024-12-20T01:38:07.983815Z","shell.execute_reply":"2024-12-20T01:38:08.116294Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Gemma Fine Tuning","metadata":{}},{"cell_type":"code","source":"# Install Keras 3 last. See https://keras.io/getting_started/ for more details.\n!pip install -q -U keras-nlp\n!pip install -q -U keras>=3\n\nimport os\n\nos.environ[\"KERAS_BACKEND\"] = \"jax\"  # Or \"torch\" or \"tensorflow\".\n# Avoid memory fragmentation on JAX backend.\nos.environ[\"XLA_PYTHON_CLIENT_MEM_FRACTION\"]=\"1.00\"\n\nimport keras\nimport keras_nlp","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:08.11909Z","iopub.execute_input":"2024-12-20T01:38:08.119508Z","iopub.status.idle":"2024-12-20T01:38:50.793376Z","shell.execute_reply.started":"2024-12-20T01:38:08.119452Z","shell.execute_reply":"2024-12-20T01:38:50.791842Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Import Model","metadata":{}},{"cell_type":"code","source":"gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset(\"gemma_2b_en\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:38:50.795115Z","iopub.execute_input":"2024-12-20T01:38:50.796165Z","iopub.status.idle":"2024-12-20T01:40:14.52575Z","shell.execute_reply.started":"2024-12-20T01:38:50.796111Z","shell.execute_reply":"2024-12-20T01:40:14.524324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gemma_lm.backbone.enable_lora(rank=64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:40:14.527396Z","iopub.execute_input":"2024-12-20T01:40:14.527751Z","iopub.status.idle":"2024-12-20T01:40:15.103137Z","shell.execute_reply.started":"2024-12-20T01:40:14.52771Z","shell.execute_reply":"2024-12-20T01:40:15.099782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gemma_lm.preprocessor.sequence_length = 512\n\noptimizer = keras.optimizers.AdamW(\n    learning_rate=5e-5,\n    weight_decay=0.01,\n    beta_1=0.9,          \n    beta_2=0.999        \n    )\n\noptimizer.exclude_from_weight_decay(var_names=[\"bias\", \"scale\"])\n\ngemma_lm.compile(\n    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    optimizer=optimizer,\n    weighted_metrics=[keras.metrics.SparseCategoricalAccuracy()],\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T01:40:15.104917Z","iopub.execute_input":"2024-12-20T01:40:15.105385Z","iopub.status.idle":"2024-12-20T01:40:15.120628Z","shell.execute_reply.started":"2024-12-20T01:40:15.105334Z","shell.execute_reply":"2024-12-20T01:40:15.118984Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Fine Tuning","metadata":{}},{"cell_type":"code","source":"%%time\n\ngemma_lm.fit(QNA_dataset, epochs=1, batch_size=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T22:57:30.194634Z","iopub.execute_input":"2024-12-19T22:57:30.194902Z","iopub.status.idle":"2024-12-20T00:00:45.036143Z","shell.execute_reply.started":"2024-12-19T22:57:30.194867Z","shell.execute_reply":"2024-12-20T00:00:45.035238Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"I have trained model for only 1 epoch since the dataset is very big and it takes several hours to fine tune the model on GPU.\n\n# Submission","metadata":{}},{"cell_type":"code","source":"instance_count = None\n\ndef get_number_of_instances(num_instances: int) -> None:\n    \"\"\" The very first message from the gateway will be the total number of instances to be served.\n    You don't need to edit this function.\n    \"\"\"\n    global instance_count\n    instance_count = num_instances","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T00:29:06.647742Z","iopub.execute_input":"2024-12-20T00:29:06.648084Z","iopub.status.idle":"2024-12-20T00:29:06.652817Z","shell.execute_reply.started":"2024-12-20T00:29:06.648055Z","shell.execute_reply":"2024-12-20T00:29:06.65167Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Custom `predict` function","metadata":{}},{"cell_type":"code","source":"first_prediction = True\n\n\ndef predict(problem_statement: str, repo_archive: io.BytesIO) -> str:\n    \"\"\"Inference function to generate a patch for a GitHub issue.\n\n    Args:\n        problem_statement (str): The text of the GitHub issue.\n        repo_archive (io.BytesIO): A BytesIO buffer containing a .tar archive of the codebase.\n\n    Returns:\n        str: The generated patch as a string.\n    \"\"\"\n    global first_prediction\n    if not first_prediction:\n        return None  # Skip the first issue.\n\n    # Unpack the repository archive\n    with open(\"repo_archive.tar\", \"wb\") as f:\n        f.write(repo_archive.read())\n    repo_path = \"repo\"\n    if os.path.exists(repo_path):\n        shutil.rmtree(repo_path)\n    shutil.unpack_archive(\"repo_archive.tar\", extract_dir=repo_path)\n    os.remove(\"repo_archive.tar\")\n    first_prediction = False\n\n    # Generate a patch\n    input_text = f\"Problem:\\n{problem_statement}\\n\\nPatch:\"\n    try:\n        # Modify the generate call to remove unsupported arguments\n        generated_patch = gemma_lm.generate([input_text])[0]\n        return generated_patch\n    except Exception as e:\n        print(f\"Error during generation: {e}\")\n        return \"Error generating patch.\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T00:30:49.322558Z","iopub.execute_input":"2024-12-20T00:30:49.323461Z","iopub.status.idle":"2024-12-20T00:30:49.330322Z","shell.execute_reply.started":"2024-12-20T00:30:49.323424Z","shell.execute_reply":"2024-12-20T00:30:49.329366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inference_server = kaggle_evaluation.konwinski_prize_inference_server.KPrizeInferenceServer(\n    get_number_of_instances,   \n    predict\n)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        data_paths=(\n            '/kaggle/input/konwinski-prize/',  # Path to the entire competition dataset\n            '/kaggle/tmp/konwinski-prize/',   # Path to a scratch directory for unpacking data.a_zip.\n        )\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T00:30:52.94338Z","iopub.execute_input":"2024-12-20T00:30:52.943742Z","iopub.status.idle":"2024-12-20T00:31:23.704925Z","shell.execute_reply.started":"2024-12-20T00:30:52.94371Z","shell.execute_reply":"2024-12-20T00:31:23.703815Z"}},"outputs":[],"execution_count":null}]}