mirror of
https://github.com/MLSysBook/TinyTorch.git
synced 2026-06-02 07:26:25 -05:00
🎯 Student Version (Visible): - All functions raise NotImplementedError('Student implementation required') - Clear TODO instructions with specific guidance - Test cells handle NotImplementedError gracefully - Students must implement everything from scratch 🔧 Instructor Version (Hidden with #|hide): - Complete working implementations - Same function signatures as student version - Production-ready code for package export 📚 Added MODULE_GENERATION_GUIDE.md: - Clear instructions for creating student/instructor modules - NBDev workflow documentation - Module structure patterns and templates - Generation commands and checklist 🔄 Generation Flow: 1. Write .py with student stubs + hidden instructor solutions 2. Convert to notebook: jupytext --to notebook module_dev.py 3. Export package: python bin/tito.py sync --module name 4. Package gets instructor solutions, students see exercises Perfect foundation for all TinyTorch modules
362 lines
11 KiB
Plaintext
362 lines
11 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ec75e599",
|
|
"metadata": {
|
|
"cell_marker": "\"\"\""
|
|
},
|
|
"source": [
|
|
"# Module 0: Setup - Tiny🔥Torch Development Workflow\n",
|
|
"\n",
|
|
"Welcome to TinyTorch! This module teaches you the development workflow you'll use throughout the course.\n",
|
|
"\n",
|
|
"## Learning Goals\n",
|
|
"- Understand the nbdev notebook-to-Python workflow\n",
|
|
"- Write your first TinyTorch code\n",
|
|
"- Run tests and use the CLI tools\n",
|
|
"- Get comfortable with the development rhythm\n",
|
|
"\n",
|
|
"## The TinyTorch Development Cycle\n",
|
|
"\n",
|
|
"1. **Write code** in this notebook using `#| export` \n",
|
|
"2. **Export code** with `python bin/tito.py sync --module setup`\n",
|
|
"3. **Run tests** with `python bin/tito.py test --module setup`\n",
|
|
"4. **Check progress** with `python bin/tito.py info`\n",
|
|
"\n",
|
|
"Let's get started!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "bce81028",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#| default_exp core.utils\n",
|
|
"\n",
|
|
"# Setup imports and environment\n",
|
|
"import sys\n",
|
|
"import platform\n",
|
|
"from datetime import datetime\n",
|
|
"\n",
|
|
"print(\"🔥 TinyTorch Development Environment\")\n",
|
|
"print(f\"Python {sys.version}\")\n",
|
|
"print(f\"Platform: {platform.system()} {platform.release()}\")\n",
|
|
"print(f\"Started: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "698330d0",
|
|
"metadata": {
|
|
"cell_marker": "\"\"\"",
|
|
"lines_to_next_cell": 1
|
|
},
|
|
"source": [
|
|
"## Step 1: Understanding the Module → Package Structure\n",
|
|
"\n",
|
|
"**🎓 Teaching vs. 🔧 Building**: This course has two sides:\n",
|
|
"- **Teaching side**: You work in `modules/setup/setup_dev.ipynb` (learning-focused)\n",
|
|
"- **Building side**: Your code exports to `tinytorch/core/utils.py` (production package)\n",
|
|
"\n",
|
|
"**Key Concept**: The `#| default_exp core.utils` directive at the top tells nbdev to export all `#| export` cells to `tinytorch/core/utils.py`.\n",
|
|
"\n",
|
|
"This separation allows us to:\n",
|
|
"- Organize learning by **concepts** (modules) \n",
|
|
"- Organize code by **function** (package structure)\n",
|
|
"- Build a real ML framework while learning systematically\n",
|
|
"\n",
|
|
"Let's write a simple \"Hello World\" function with the `#| export` directive:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "dc32cd6d",
|
|
"metadata": {
|
|
"lines_to_next_cell": 1
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#| export\n",
|
|
"def hello_tinytorch():\n",
|
|
" \"\"\"\n",
|
|
" A simple hello world function for TinyTorch.\n",
|
|
" \n",
|
|
" TODO: Implement this function to return a welcoming message about TinyTorch.\n",
|
|
" Make it encouraging and mention what students will build.\n",
|
|
" \"\"\"\n",
|
|
" raise NotImplementedError(\"Student implementation required\")\n",
|
|
"\n",
|
|
"def add_numbers(a, b):\n",
|
|
" \"\"\"\n",
|
|
" Add two numbers together.\n",
|
|
" \n",
|
|
" TODO: Implement addition of two numbers.\n",
|
|
" This is the foundation of all mathematical operations in ML.\n",
|
|
" \"\"\"\n",
|
|
" raise NotImplementedError(\"Student implementation required\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0a0a5958",
|
|
"metadata": {
|
|
"lines_to_next_cell": 1
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#| hide\n",
|
|
"#| export\n",
|
|
"def hello_tinytorch():\n",
|
|
" \"\"\"A simple hello world function for TinyTorch.\"\"\"\n",
|
|
" return \"🔥 Welcome to TinyTorch! Ready to build ML systems from scratch? Let's go! 🔥\"\n",
|
|
"\n",
|
|
"def add_numbers(a, b):\n",
|
|
" \"\"\"Add two numbers together.\"\"\"\n",
|
|
" return a + b"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "12ceeb04",
|
|
"metadata": {
|
|
"cell_marker": "\"\"\""
|
|
},
|
|
"source": [
|
|
"### 🧪 Test Your Implementation\n",
|
|
"\n",
|
|
"Once you implement the functions above, run this cell to test them:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "6a9e762c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Test the functions in the notebook (will fail until implemented)\n",
|
|
"try:\n",
|
|
" print(\"Testing hello_tinytorch():\")\n",
|
|
" print(hello_tinytorch())\n",
|
|
" print()\n",
|
|
" print(\"Testing add_numbers():\")\n",
|
|
" print(f\"2 + 3 = {add_numbers(2, 3)}\")\n",
|
|
"except NotImplementedError as e:\n",
|
|
" print(f\"⚠️ {e}\")\n",
|
|
" print(\"Implement the functions above first!\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "4b3bad41",
|
|
"metadata": {
|
|
"cell_marker": "\"\"\"",
|
|
"lines_to_next_cell": 1
|
|
},
|
|
"source": [
|
|
"## Step 2: A Simple Class\n",
|
|
"\n",
|
|
"Let's create a simple class that will help us understand system information. This is still basic, but shows how to structure classes in TinyTorch."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c0aa67a6",
|
|
"metadata": {
|
|
"lines_to_next_cell": 1
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#| export\n",
|
|
"class SystemInfo:\n",
|
|
" \"\"\"\n",
|
|
" Simple system information class.\n",
|
|
" \n",
|
|
" TODO: Implement this class to collect and display system information.\n",
|
|
" \"\"\"\n",
|
|
" \n",
|
|
" def __init__(self):\n",
|
|
" \"\"\"\n",
|
|
" Initialize system information collection.\n",
|
|
" \n",
|
|
" TODO: Collect Python version, platform, and machine information.\n",
|
|
" \"\"\"\n",
|
|
" raise NotImplementedError(\"Student implementation required\")\n",
|
|
" \n",
|
|
" def __str__(self):\n",
|
|
" \"\"\"\n",
|
|
" Return human-readable system information.\n",
|
|
" \n",
|
|
" TODO: Format system info as a readable string.\n",
|
|
" \"\"\"\n",
|
|
" raise NotImplementedError(\"Student implementation required\")\n",
|
|
" \n",
|
|
" def is_compatible(self):\n",
|
|
" \"\"\"\n",
|
|
" Check if system meets minimum requirements.\n",
|
|
" \n",
|
|
" TODO: Check if Python version is >= 3.8\n",
|
|
" \"\"\"\n",
|
|
" raise NotImplementedError(\"Student implementation required\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "54a1a2d9",
|
|
"metadata": {
|
|
"lines_to_next_cell": 1
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#| hide\n",
|
|
"#| export\n",
|
|
"class SystemInfo:\n",
|
|
" \"\"\"Simple system information class.\"\"\"\n",
|
|
" \n",
|
|
" def __init__(self):\n",
|
|
" self.python_version = sys.version_info\n",
|
|
" self.platform = platform.system()\n",
|
|
" self.machine = platform.machine()\n",
|
|
" \n",
|
|
" def __str__(self):\n",
|
|
" return f\"Python {self.python_version.major}.{self.python_version.minor} on {self.platform} ({self.machine})\"\n",
|
|
" \n",
|
|
" def is_compatible(self):\n",
|
|
" \"\"\"Check if system meets minimum requirements.\"\"\"\n",
|
|
" return self.python_version >= (3, 8)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "902d46d4",
|
|
"metadata": {
|
|
"cell_marker": "\"\"\""
|
|
},
|
|
"source": [
|
|
"### 🧪 Test Your SystemInfo Class\n",
|
|
"\n",
|
|
"Once you implement the SystemInfo class above, run this cell to test it:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "bfaab1ad",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Test the SystemInfo class (will fail until implemented)\n",
|
|
"try:\n",
|
|
" print(\"Testing SystemInfo class:\")\n",
|
|
" info = SystemInfo()\n",
|
|
" print(f\"System: {info}\")\n",
|
|
" print(f\"Compatible: {info.is_compatible()}\")\n",
|
|
"except NotImplementedError as e:\n",
|
|
" print(f\"⚠️ {e}\")\n",
|
|
" print(\"Implement the SystemInfo class above first!\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "98f8308e",
|
|
"metadata": {
|
|
"cell_marker": "\"\"\""
|
|
},
|
|
"source": [
|
|
"## Step 3: Try the Export Process\n",
|
|
"\n",
|
|
"Now let's export our code! In your terminal, run:\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"python bin/tito.py sync --module setup\n",
|
|
"```\n",
|
|
"\n",
|
|
"This will export the code marked with `#| export` to `tinytorch/core/utils.py`.\n",
|
|
"\n",
|
|
"**What happens during export:**\n",
|
|
"1. nbdev scans this notebook for `#| export` cells\n",
|
|
"2. Extracts the Python code \n",
|
|
"3. Writes it to `tinytorch/core/utils.py` (because of `#| default_exp core.utils`)\n",
|
|
"4. Handles imports and dependencies automatically\n",
|
|
"\n",
|
|
"**🔍 Verification**: After export, check `tinytorch/core/utils.py` - you'll see your functions there with auto-generated headers pointing back to this notebook!\n",
|
|
"\n",
|
|
"**Note**: The export process will use the instructor solutions (from `#|hide` cells) so the package will have working implementations even if you haven't completed the exercises yet."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5c8763c2",
|
|
"metadata": {
|
|
"cell_marker": "\"\"\""
|
|
},
|
|
"source": [
|
|
"## Step 4: Run Tests\n",
|
|
"\n",
|
|
"After exporting, run the tests:\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"python bin/tito.py test --module setup\n",
|
|
"```\n",
|
|
"\n",
|
|
"This will run all tests for the setup module and verify your implementation works correctly.\n",
|
|
"\n",
|
|
"## Step 5: Check Your Progress\n",
|
|
"\n",
|
|
"See your overall progress:\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"python bin/tito.py info\n",
|
|
"```\n",
|
|
"\n",
|
|
"This shows which modules are complete and which are pending."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "7600dbe5",
|
|
"metadata": {
|
|
"cell_marker": "\"\"\""
|
|
},
|
|
"source": [
|
|
"## 🎉 Congratulations!\n",
|
|
"\n",
|
|
"You've learned the TinyTorch development workflow:\n",
|
|
"\n",
|
|
"1. ✅ Write code in notebooks with `#| export`\n",
|
|
"2. ✅ Export with `tito sync --module setup` \n",
|
|
"3. ✅ Test with `tito test --module setup`\n",
|
|
"4. ✅ Check progress with `tito info`\n",
|
|
"\n",
|
|
"**This is the rhythm you'll use for every module in TinyTorch.**\n",
|
|
"\n",
|
|
"### Next Steps\n",
|
|
"\n",
|
|
"Ready for the real work? Head to **Module 1: Tensor** where you'll build the core data structures that power everything else in TinyTorch.\n",
|
|
"\n",
|
|
"**Development Tips:**\n",
|
|
"- Always test your code in the notebook first\n",
|
|
"- Export frequently to catch issues early \n",
|
|
"- Read error messages carefully - they're designed to help\n",
|
|
"- When stuck, check if your code exports cleanly first\n",
|
|
"\n",
|
|
"Happy building! 🔥"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"jupytext": {
|
|
"main_language": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|