mirror of
https://github.com/MLSysBook/TinyTorch.git
synced 2026-07-23 13:49:38 -05:00
- Restore complete setup module with system information and developer profiles - Add NBDev educational patterns with #| export and #| hide directives - Implement SystemInfo class for platform detection and compatibility checking - Add DeveloperProfile class with ASCII art customization and personalization - Include comprehensive educational content with step-by-step learning progression - Expand test suite to 20 comprehensive tests covering all functionality: - Function execution and output validation - Arithmetic operations (basic, negative, floating-point) - System information collection and compatibility - Developer profile creation and customization - ASCII art file loading and fallback behavior - Error recovery and integration testing - Update README with complete feature documentation and usage examples - Convert to executed notebook showing rich output and system information - Maintain file-based ASCII art system with tinytorch_flame.txt - All tests passing with professional pytest structure
37 KiB
37 KiB
In [1]:
#| default_exp core.utils
# Setup imports and environment
import sys
import platform
from datetime import datetime
import os
from pathlib import Path
print("🔥 TinyTorch Development Environment")
print(f"Python {sys.version}")
print(f"Platform: {platform.system()} {platform.release()}")
print(f"Started: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")🔥 TinyTorch Development Environment Python 3.13.3 (v3.13.3:6280bb54784, Apr 8 2025, 10:47:54) [Clang 15.0.0 (clang-1500.3.9.4)] Platform: Darwin 24.5.0 Started: 2025-07-10 19:28:59
In [2]:
#| export
def hello_tinytorch():
"""
A simple hello world function for TinyTorch.
TODO: Implement this function to display TinyTorch ASCII art and welcome message.
Load the flame art from tinytorch_flame.txt file with graceful fallback.
"""
raise NotImplementedError("Student implementation required")
def add_numbers(a, b):
"""
Add two numbers together.
TODO: Implement addition of two numbers.
This is the foundation of all mathematical operations in ML.
"""
raise NotImplementedError("Student implementation required")In [3]:
#| hide
#| export
def hello_tinytorch():
"""Display the TinyTorch ASCII art and welcome message."""
try:
# Get the directory containing this file
current_dir = Path(__file__).parent
art_file = current_dir / "tinytorch_flame.txt"
if art_file.exists():
with open(art_file, 'r') as f:
ascii_art = f.read()
print(ascii_art)
print("Tiny🔥Torch")
print("Build ML Systems from Scratch!")
else:
print("🔥 TinyTorch 🔥")
print("Build ML Systems from Scratch!")
except NameError:
# Handle case when running in notebook where __file__ is not defined
try:
art_file = Path(os.getcwd()) / "tinytorch_flame.txt"
if art_file.exists():
with open(art_file, 'r') as f:
ascii_art = f.read()
print(ascii_art)
print("Tiny🔥Torch")
print("Build ML Systems from Scratch!")
else:
print("🔥 TinyTorch 🔥")
print("Build ML Systems from Scratch!")
except:
print("🔥 TinyTorch 🔥")
print("Build ML Systems from Scratch!")
def add_numbers(a, b):
"""Add two numbers together."""
return a + bIn [4]:
# Test the functions in the notebook (will fail until implemented)
try:
print("Testing hello_tinytorch():")
hello_tinytorch()
print()
print("Testing add_numbers():")
print(f"2 + 3 = {add_numbers(2, 3)}")
except NotImplementedError as e:
print(f"⚠️ {e}")
print("Implement the functions above first!")Testing hello_tinytorch():
. . ... ....... .... ... . . .. .... . .. . . . . . ....
. . .. .++. .. . . . .. ... . . . .. ... ..
. . . .=++=.. . . . .. . . .. . ... .. . .
. .. ... .++++=. . . . . .. . .. .
. . . . ....-+++++.... ... .. . .... .. . . . . . . . . . . . .
. .. ...-++++++-...... .. . ..... ..-:.. .. . .... .. . . .. . .. . . .
.. .. ..++++++++-.. . . ..##... -%#. . . . . .
. .. .:+++++++++.... ... . ...:%%:............:-:. ..... ...... . . ....... .. . .
..+++++++++++. ... . .. .=#%%##+.-##..#%####%%=.=%%. .*%+.. . . . ...
. ..++++++++++++...-++..... . .%%... -##..##=...=%#..*%*..=%#.. . .. ... . . . . .. . ...
..-+++++++++++++..=++++... .....%#.. -##..#%-.. -##. .%%=.%%.. . . . . . ... .
. .=++++++++++++++-+++++++.... . ...%%:...-##..#%-. .-%#. ..#%#%=.. . .. ... . . . .
..=+++++++++++++++++++++++-. . ..=%%%+.-%#..##-. .-%#....-%%*.. . .. . .. .. ..
.:+++++++++++=+++++++++++++. . ................ .......-%%... . .. . . .. .
.++++++++++===+++++++++++++: . .................... . ...%%%#:........ . .. ..... ......... ....
:+++++++++====+++++++++++++=.. ...-----------.....-+#*=:.....-------:.......:=*#+-.. ..--:.....--=.
:++++++++======++++++++++++=.. ...#%%%%%%%%%#..-#%%###%%#=...#%####%%%=...+%%%###%%#...#%+.. ..#%%.
.+++++++========+++++++++++- .. .#%%.. ..-%%+.. ..-%%+..#%*.. .*%%..*%%:. ..#%*..#%+... .#%%.
.=++++++==========+++++++++: . .#%%.....#%#.... .*%#..#%*...-%%*..#%+. ... . ..##%#####%%%.
..++++++===========+++++++-. . ...#%%. . .#%#. . .*%#..#%%%%%%#-. .#%+. . ....#%*-----#%%.
...+++++===========++++++=. . . . .#%%... -%%+.....=%%+..#%*..+%%-. .*%%-.....#%*..%%+.. ..%%%.
. ..-+++===========+++++.. . .. ..#%%. .:%%%###%%%=...#%*...+%%=...+%%####%%#...%%+.. ..%%%.
. ...-++==========+++:.... ... . .===. ... ..-+++=.. ..-=-....-==: ..:=+++-.. ..==-... .===.
....-+=======+-...... .. . . ... . . .. ... . . .... . . . . ..... . ... ..... .
.... . ......:..... ... . .. . ... . . ... . . . ... . . . ... .. ..... . .
Tiny🔥Torch
Build ML Systems from Scratch!
Testing add_numbers():
2 + 3 = 5
In [5]:
#| export
class SystemInfo:
"""
Simple system information class.
TODO: Implement this class to collect and display system information.
"""
def __init__(self):
"""
Initialize system information collection.
TODO: Collect Python version, platform, and machine information.
"""
raise NotImplementedError("Student implementation required")
def __str__(self):
"""
Return human-readable system information.
TODO: Format system info as a readable string.
"""
raise NotImplementedError("Student implementation required")
def is_compatible(self):
"""
Check if system meets minimum requirements.
TODO: Check if Python version is >= 3.8
"""
raise NotImplementedError("Student implementation required")In [6]:
#| hide
#| export
class SystemInfo:
"""Simple system information class."""
def __init__(self):
self.python_version = sys.version_info
self.platform = platform.system()
self.machine = platform.machine()
def __str__(self):
return f"Python {self.python_version.major}.{self.python_version.minor} on {self.platform} ({self.machine})"
def is_compatible(self):
"""Check if system meets minimum requirements."""
return self.python_version >= (3, 8)In [7]:
# Test the SystemInfo class (will fail until implemented)
try:
print("Testing SystemInfo class:")
info = SystemInfo()
print(f"System: {info}")
print(f"Compatible: {info.is_compatible()}")
except NotImplementedError as e:
print(f"⚠️ {e}")
print("Implement the SystemInfo class above first!")Testing SystemInfo class: System: Python 3.13 on Darwin (arm64) Compatible: True
In [8]:
#| export
class DeveloperProfile:
"""
Developer profile for personalizing TinyTorch experience.
TODO: Implement this class to store and display developer information.
Default to course instructor but allow students to personalize.
"""
@staticmethod
def _load_default_flame():
"""
Load the default TinyTorch flame ASCII art from file.
TODO: Implement file loading for tinytorch_flame.txt with fallback.
"""
raise NotImplementedError("Student implementation required")
def __init__(self, name="Vijay Janapa Reddi", affiliation="Harvard University",
email="vj@eecs.harvard.edu", github_username="profvjreddi", ascii_art=None):
"""
Initialize developer profile.
TODO: Store developer information with sensible defaults.
Students should be able to customize this with their own info and ASCII art.
"""
raise NotImplementedError("Student implementation required")
def __str__(self):
"""
Return formatted developer information.
TODO: Format developer info as a professional signature with optional ASCII art.
"""
raise NotImplementedError("Student implementation required")
def get_signature(self):
"""
Get a short signature for code headers.
TODO: Return a concise signature like "Built by Name (@github)"
"""
raise NotImplementedError("Student implementation required")
def get_ascii_art(self):
"""
Get ASCII art for the profile.
TODO: Return custom ASCII art or default flame loaded from file.
"""
raise NotImplementedError("Student implementation required")In [9]:
#| hide
#| export
class DeveloperProfile:
"""Developer profile for personalizing TinyTorch experience."""
@staticmethod
def _load_default_flame():
"""Load the default TinyTorch flame ASCII art from file."""
try:
# Try to load from the same directory as this module
try:
# Try to get the directory of the current file
current_dir = os.path.dirname(__file__)
except NameError:
# If __file__ is not defined (e.g., in notebook), use current directory
current_dir = os.getcwd()
flame_path = os.path.join(current_dir, 'tinytorch_flame.txt')
with open(flame_path, 'r', encoding='utf-8') as f:
flame_art = f.read()
# Add the Tiny🔥Torch text below the flame
return f"""{flame_art}
Tiny🔥Torch
Build ML Systems from Scratch!
"""
except (FileNotFoundError, IOError):
# Fallback to simple flame if file not found
return """
🔥 TinyTorch Developer 🔥
. . . . . .
. . . . . .
. . . . . . .
. . . . . . . .
. . . . . . . . .
. . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . . .
\\ \\ \\ \\ \\ \\ \\ \\ \\ / / / / / /
\\ \\ \\ \\ \\ \\ \\ \\ / / / / / /
\\ \\ \\ \\ \\ \\ \\ / / / / / /
\\ \\ \\ \\ \\ \\ / / / / / /
\\ \\ \\ \\ \\ / / / / / /
\\ \\ \\ \\ / / / / / /
\\ \\ \\ / / / / / /
\\ \\ / / / / / /
\\ / / / / / /
\\/ / / / / /
\\/ / / / /
\\/ / / /
\\/ / /
\\/ /
\\/
Tiny🔥Torch
Build ML Systems from Scratch!
"""
def __init__(self, name="Vijay Janapa Reddi", affiliation="Harvard University",
email="vj@eecs.harvard.edu", github_username="profvjreddi", ascii_art=None):
self.name = name
self.affiliation = affiliation
self.email = email
self.github_username = github_username
self.ascii_art = ascii_art or self._load_default_flame()
def __str__(self):
return f"👨💻 {self.name} | {self.affiliation} | @{self.github_username}"
def get_signature(self):
"""Get a short signature for code headers."""
return f"Built by {self.name} (@{self.github_username})"
def get_ascii_art(self):
"""Get ASCII art for the profile."""
return self.ascii_art
def get_full_profile(self):
"""Get complete profile with ASCII art."""
return f"""{self.ascii_art}
👨💻 Developer: {self.name}
🏛️ Affiliation: {self.affiliation}
📧 Email: {self.email}
🐙 GitHub: @{self.github_username}
🔥 Ready to build ML systems from scratch!
"""In [10]:
# Test the DeveloperProfile class
try:
print("Testing DeveloperProfile (with defaults):")
# Default profile (instructor)
default_profile = DeveloperProfile()
print(f"Profile: {default_profile}")
print(f"Signature: {default_profile.get_signature()}")
print()
print("🎨 ASCII Art Preview:")
print(default_profile.get_ascii_art())
print()
print("🔥 Full Profile Display:")
print(default_profile.get_full_profile())
print()
# TODO: Students should customize this with their own information!
print("🎯 YOUR TURN: Create your own profile!")
print("Uncomment and modify the lines below:")
print("# my_profile = DeveloperProfile(")
print("# name='Your Name',")
print("# affiliation='Your University/Company',")
print("# email='your.email@example.com',")
print("# github_username='yourgithub',")
print("# ascii_art='''")
print("# Your Custom ASCII Art Here!")
print("# Maybe your initials, a logo, or something fun!")
print("# '''")
print("# )")
print("# print(f'My Profile: {my_profile}')")
print("# print(f'My Signature: {my_profile.get_signature()}')")
print("# print(my_profile.get_full_profile())")
except NotImplementedError as e:
print(f"⚠️ {e}")
print("Implement the DeveloperProfile class above first!")Testing DeveloperProfile (with defaults):
Profile: 👨💻 Vijay Janapa Reddi | Harvard University | @profvjreddi
Signature: Built by Vijay Janapa Reddi (@profvjreddi)
🎨 ASCII Art Preview:
. . ... ....... .... ... . . .. .... . .. . . . . . ....
. . .. .++. .. . . . .. ... . . . .. ... ..
. . . .=++=.. . . . .. . . .. . ... .. . .
. .. ... .++++=. . . . . .. . .. .
. . . . ....-+++++.... ... .. . .... .. . . . . . . . . . . . .
. .. ...-++++++-...... .. . ..... ..-:.. .. . .... .. . . .. . .. . . .
.. .. ..++++++++-.. . . ..##... -%#. . . . . .
. .. .:+++++++++.... ... . ...:%%:............:-:. ..... ...... . . ....... .. . .
..+++++++++++. ... . .. .=#%%##+.-##..#%####%%=.=%%. .*%+.. . . . ...
. ..++++++++++++...-++..... . .%%... -##..##=...=%#..*%*..=%#.. . .. ... . . . . .. . ...
..-+++++++++++++..=++++... .....%#.. -##..#%-.. -##. .%%=.%%.. . . . . . ... .
. .=++++++++++++++-+++++++.... . ...%%:...-##..#%-. .-%#. ..#%#%=.. . .. ... . . . .
..=+++++++++++++++++++++++-. . ..=%%%+.-%#..##-. .-%#....-%%*.. . .. . .. .. ..
.:+++++++++++=+++++++++++++. . ................ .......-%%... . .. . . .. .
.++++++++++===+++++++++++++: . .................... . ...%%%#:........ . .. ..... ......... ....
:+++++++++====+++++++++++++=.. ...-----------.....-+#*=:.....-------:.......:=*#+-.. ..--:.....--=.
:++++++++======++++++++++++=.. ...#%%%%%%%%%#..-#%%###%%#=...#%####%%%=...+%%%###%%#...#%+.. ..#%%.
.+++++++========+++++++++++- .. .#%%.. ..-%%+.. ..-%%+..#%*.. .*%%..*%%:. ..#%*..#%+... .#%%.
.=++++++==========+++++++++: . .#%%.....#%#.... .*%#..#%*...-%%*..#%+. ... . ..##%#####%%%.
..++++++===========+++++++-. . ...#%%. . .#%#. . .*%#..#%%%%%%#-. .#%+. . ....#%*-----#%%.
...+++++===========++++++=. . . . .#%%... -%%+.....=%%+..#%*..+%%-. .*%%-.....#%*..%%+.. ..%%%.
. ..-+++===========+++++.. . .. ..#%%. .:%%%###%%%=...#%*...+%%=...+%%####%%#...%%+.. ..%%%.
. ...-++==========+++:.... ... . .===. ... ..-+++=.. ..-=-....-==: ..:=+++-.. ..==-... .===.
....-+=======+-...... .. . . ... . . .. ... . . .... . . . . ..... . ... ..... .
.... . ......:..... ... . .. . ... . . ... . . . ... . . . ... .. ..... . .
Tiny🔥Torch
Build ML Systems from Scratch!
🔥 Full Profile Display:
. . ... ....... .... ... . . .. .... . .. . . . . . ....
. . .. .++. .. . . . .. ... . . . .. ... ..
. . . .=++=.. . . . .. . . .. . ... .. . .
. .. ... .++++=. . . . . .. . .. .
. . . . ....-+++++.... ... .. . .... .. . . . . . . . . . . . .
. .. ...-++++++-...... .. . ..... ..-:.. .. . .... .. . . .. . .. . . .
.. .. ..++++++++-.. . . ..##... -%#. . . . . .
. .. .:+++++++++.... ... . ...:%%:............:-:. ..... ...... . . ....... .. . .
..+++++++++++. ... . .. .=#%%##+.-##..#%####%%=.=%%. .*%+.. . . . ...
. ..++++++++++++...-++..... . .%%... -##..##=...=%#..*%*..=%#.. . .. ... . . . . .. . ...
..-+++++++++++++..=++++... .....%#.. -##..#%-.. -##. .%%=.%%.. . . . . . ... .
. .=++++++++++++++-+++++++.... . ...%%:...-##..#%-. .-%#. ..#%#%=.. . .. ... . . . .
..=+++++++++++++++++++++++-. . ..=%%%+.-%#..##-. .-%#....-%%*.. . .. . .. .. ..
.:+++++++++++=+++++++++++++. . ................ .......-%%... . .. . . .. .
.++++++++++===+++++++++++++: . .................... . ...%%%#:........ . .. ..... ......... ....
:+++++++++====+++++++++++++=.. ...-----------.....-+#*=:.....-------:.......:=*#+-.. ..--:.....--=.
:++++++++======++++++++++++=.. ...#%%%%%%%%%#..-#%%###%%#=...#%####%%%=...+%%%###%%#...#%+.. ..#%%.
.+++++++========+++++++++++- .. .#%%.. ..-%%+.. ..-%%+..#%*.. .*%%..*%%:. ..#%*..#%+... .#%%.
.=++++++==========+++++++++: . .#%%.....#%#.... .*%#..#%*...-%%*..#%+. ... . ..##%#####%%%.
..++++++===========+++++++-. . ...#%%. . .#%#. . .*%#..#%%%%%%#-. .#%+. . ....#%*-----#%%.
...+++++===========++++++=. . . . .#%%... -%%+.....=%%+..#%*..+%%-. .*%%-.....#%*..%%+.. ..%%%.
. ..-+++===========+++++.. . .. ..#%%. .:%%%###%%%=...#%*...+%%=...+%%####%%#...%%+.. ..%%%.
. ...-++==========+++:.... ... . .===. ... ..-+++=.. ..-=-....-==: ..:=+++-.. ..==-... .===.
....-+=======+-...... .. . . ... . . .. ... . . .... . . . . ..... . ... ..... .
.... . ......:..... ... . .. . ... . . ... . . . ... . . . ... .. ..... . .
Tiny🔥Torch
Build ML Systems from Scratch!
👨💻 Developer: Vijay Janapa Reddi
🏛️ Affiliation: Harvard University
📧 Email: vj@eecs.harvard.edu
🐙 GitHub: @profvjreddi
🔥 Ready to build ML systems from scratch!
🎯 YOUR TURN: Create your own profile!
Uncomment and modify the lines below:
# my_profile = DeveloperProfile(
# name='Your Name',
# affiliation='Your University/Company',
# email='your.email@example.com',
# github_username='yourgithub',
# ascii_art='''
# Your Custom ASCII Art Here!
# Maybe your initials, a logo, or something fun!
# '''
# )
# print(f'My Profile: {my_profile}')
# print(f'My Signature: {my_profile.get_signature()}')
# print(my_profile.get_full_profile())