diff --git a/week4/community-contributions/BernardUdo/README.md b/week4/community-contributions/BernardUdo/README.md new file mode 100644 index 000000000..7f6536245 --- /dev/null +++ b/week4/community-contributions/BernardUdo/README.md @@ -0,0 +1,10 @@ +# Week 4 Exercise Submission - BernardUdo + +This folder contains my Week 4 exercise solution: + +- `week4_exercise.ipynb`: a modular notebook for generating optimized C++ from Python with a frontier model, including optional compile and benchmark helpers. + +## Notes + +- The notebook is split into manageable sections for easier maintenance and refactoring. +- API-driven generation requires an OpenAI-compatible API key. diff --git a/week4/community-contributions/BernardUdo/week4_exercise.ipynb b/week4/community-contributions/BernardUdo/week4_exercise.ipynb new file mode 100644 index 000000000..555de1377 --- /dev/null +++ b/week4/community-contributions/BernardUdo/week4_exercise.ipynb @@ -0,0 +1,221 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Week 4 Exercise - Frontier Code Generator\n", + "\n", + "Author: BernardUdo\n", + "\n", + "This notebook implements a modular code generator that converts Python snippets into optimized C++ using a frontier LLM model, then optionally compiles and benchmarks the output.\n", + "\n", + "## Sections\n", + "- Configuration\n", + "- Prompt Templates\n", + "- LLM Code Generation\n", + "- File and Compilation Utilities\n", + "- Example Run" + ], + "id": "d6efee05" + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "import os\n", + "import re\n", + "import textwrap\n", + "import subprocess\n", + "from pathlib import Path\n", + "\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI" + ], + "execution_count": null, + "outputs": [], + "id": "ac42a717" + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# -----------------------------\n", + "# Configuration Block\n", + "# -----------------------------\n", + "load_dotenv(override=True)\n", + "\n", + "MODEL_NAME = \"gpt-5\"\n", + "OUTPUT_DIR = Path(\"generated\")\n", + "CPP_FILENAME = \"generated.cpp\"\n", + "BINARY_NAME = \"generated\"\n", + "\n", + "OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n", + "if not OPENAI_API_KEY:\n", + " raise EnvironmentError(\"OPENAI_API_KEY is not set in your environment.\")\n", + "\n", + "client = OpenAI(api_key=OPENAI_API_KEY)\n", + "OUTPUT_DIR.mkdir(parents=True, exist_ok=True)\n", + "print(f\"Using model: {MODEL_NAME}\")" + ], + "execution_count": null, + "outputs": [], + "id": "f9fb858d" + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# -----------------------------\n", + "# Prompt and Parsing Helpers\n", + "# -----------------------------\n", + "SYSTEM_PROMPT = textwrap.dedent(\n", + " \"\"\"\n", + " You are an expert C++ performance engineer.\n", + " Convert Python code into modern C++17 code optimized for runtime performance.\n", + "\n", + " Rules:\n", + " 1. Return only valid C++ code in a single fenced ```cpp block.\n", + " 2. Preserve the behavior of the original Python logic.\n", + " 3. Prefer efficient data structures and avoid unnecessary heap allocations.\n", + " 4. Include a simple main() demonstrating the function.\n", + " \"\"\"\n", + ").strip()\n", + "\n", + "\n", + "def build_user_prompt(python_code: str) -> str:\n", + " return textwrap.dedent(\n", + " f\"\"\"\n", + " Convert this Python program into optimized C++17:\n", + "\n", + " ```python\n", + " {python_code}\n", + " ```\n", + " \"\"\"\n", + " ).strip()\n", + "\n", + "\n", + "def extract_cpp_code(model_text: str) -> str:\n", + " match = re.search(r\"```cpp\\s*(.*?)```\", model_text, re.DOTALL)\n", + " if match:\n", + " return match.group(1).strip()\n", + " return model_text.strip()" + ], + "execution_count": null, + "outputs": [], + "id": "a60b0b2d" + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# -----------------------------\n", + "# Generation Utilities\n", + "# -----------------------------\n", + "def generate_cpp_from_python(python_code: str, model: str = MODEL_NAME) -> str:\n", + " response = client.chat.completions.create(\n", + " model=model,\n", + " messages=[\n", + " {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n", + " {\"role\": \"user\", \"content\": build_user_prompt(python_code)},\n", + " ],\n", + " )\n", + " raw_text = response.choices[0].message.content\n", + " return extract_cpp_code(raw_text)\n", + "\n", + "\n", + "def write_cpp_file(cpp_code: str, output_dir: Path = OUTPUT_DIR, filename: str = CPP_FILENAME) -> Path:\n", + " cpp_path = output_dir / filename\n", + " cpp_path.write_text(cpp_code, encoding=\"utf-8\")\n", + " return cpp_path" + ], + "execution_count": null, + "outputs": [], + "id": "4205f001" + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# -----------------------------\n", + "# Compile and Run Utilities\n", + "# -----------------------------\n", + "def compile_cpp(cpp_path: Path, binary_name: str = BINARY_NAME) -> Path:\n", + " binary_path = cpp_path.parent / binary_name\n", + " command = [\n", + " \"g++\",\n", + " \"-O3\",\n", + " \"-march=native\",\n", + " \"-std=c++17\",\n", + " str(cpp_path),\n", + " \"-o\",\n", + " str(binary_path),\n", + " ]\n", + " subprocess.run(command, check=True, text=True, capture_output=True)\n", + " return binary_path\n", + "\n", + "\n", + "def run_binary(binary_path: Path) -> str:\n", + " result = subprocess.run([str(binary_path)], check=True, text=True, capture_output=True)\n", + " return result.stdout.strip()" + ], + "execution_count": null, + "outputs": [], + "id": "e4f6469d" + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# -----------------------------\n", + "# Example Run\n", + "# -----------------------------\n", + "python_source = textwrap.dedent(\n", + " \"\"\"\n", + " def sum_of_squares(n: int) -> int:\n", + " return sum(i * i for i in range(n + 1))\n", + "\n", + " if __name__ == \"__main__\":\n", + " print(sum_of_squares(1000000))\n", + " \"\"\"\n", + ").strip()\n", + "\n", + "cpp_code = generate_cpp_from_python(python_source)\n", + "cpp_file = write_cpp_file(cpp_code)\n", + "print(f\"Generated C++ file: {cpp_file}\")\n", + "print(cpp_code[:1200])" + ], + "execution_count": null, + "outputs": [], + "id": "4b4c681b" + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# Optional compile + run\n", + "# Uncomment after verifying g++ is available on your machine.\n", + "\n", + "# binary_file = compile_cpp(cpp_file)\n", + "# output = run_binary(binary_file)\n", + "# print(output)" + ], + "id": "4f9cccc7", + "execution_count": null, + "outputs": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file