Skip to content

Commit d2ac1d0

Browse files
committed
first commit
0 parents  commit d2ac1d0

39 files changed

Lines changed: 4235 additions & 0 deletions

‎.coverage‎

52 KB
Binary file not shown.

‎.github/workflows/ci.yml‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: CI/CD Pipeline
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
permissions:
10+
contents: read
11+
12+
env:
13+
UV_SYSTEM_PYTHON: 1
14+
15+
jobs:
16+
check:
17+
name: Code Quality & Formatting
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
- name: Set up Python
24+
uses: actions/setup-python@v5
25+
with:
26+
python-version: "3.12"
27+
28+
- name: Install uv
29+
uses: astral-sh/setup-uv@v5
30+
31+
- name: Run Ruff Check
32+
run: uv run ruff check .
33+
34+
- name: Run Ruff Format Check
35+
run: uv run ruff format --check .
36+
37+
test:
38+
name: Test Suite
39+
runs-on: ubuntu-latest
40+
steps:
41+
- name: Checkout repository
42+
uses: actions/checkout@v4
43+
44+
- name: Set up Python
45+
uses: actions/setup-python@v5
46+
with:
47+
python-version: "3.12"
48+
49+
- name: Install uv
50+
uses: astral-sh/setup-uv@v5
51+
52+
- name: Install dependencies
53+
run: uv sync --all-extras
54+
55+
- name: Run Tests
56+
run: uv run pytest
57+
58+
- name: Run Benchmarks
59+
run: uv run python benches/mlp_benchmark.py --fast -o benchmark_results.json
60+
61+
- name: Run Memory Profiler
62+
run: uv run memray run -o memray_results.bin benches/profile_mlp.py
63+
64+
- name: Generate HTML Flamegraph
65+
run: uv run memray flamegraph -f -o memray_report.html memray_results.bin
66+
67+
- name: Generate JSON Stats
68+
run: uv run memray stats --json -f -o memray_report.json memray_results.bin
69+
70+
- name: Upload Memory Profile Reports
71+
uses: actions/upload-artifact@v4
72+
with:
73+
name: python-memory-reports
74+
path: |
75+
memray_report.html
76+
memray_report.json

‎.gitignore‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Python-generated files
2+
__pycache__/
3+
*.py[oc]
4+
build/
5+
dist/
6+
wheels/
7+
*.egg-info
8+
9+
# Virtual environments
10+
.venv
11+
*_cache
12+
htmlcov
13+
14+
benchmark_report.html
15+
benchmark_results.json
16+
problem.txt
17+
source.txt

‎.python-version‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.14

‎ARCHITECTURE.md‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Architecture & Design (Python)
2+
3+
This document describes the high-level architecture of the `gate_learner` Python package.
4+
5+
## Module Overview
6+
7+
The project is structured as a decoupled library with a Typer CLI wrapper. This separation ensures that the core mathematical engine remains independent of I/O, CLI parsing, and plotting logic.
8+
9+
```text
10+
+-----------------------------------------------------------------------+
11+
| CLI Module |
12+
| (gate_learner/cli.py, main) |
13+
+-----------------------------------+-----------------------------------+
14+
|
15+
v
16+
+-----------------------------------+-----------------------------------+
17+
| Dataset Generator |
18+
| (gate_learner/dataset.py) |
19+
+-----------------------------------+-----------------------------------+
20+
|
21+
v
22+
+-----------------------------------+-----------------------------------+
23+
| Core ML Engine |
24+
| (gate_learner/core.py) |
25+
| +---------------------------------------------------------------+ |
26+
| | MultilayerPerceptron | |
27+
| | - Forward Pass (Sigmoid / ReLU Activation) | |
28+
| | - Gradient Accumulation (BCE Loss) | |
29+
| +-------------------------------+-------------------------------+ |
30+
| | |
31+
| v |
32+
| +---------------------------------------------------------------+ |
33+
| | AdamOptimizer | |
34+
| | - Parameter Update (L2 Regularisation & Bias Correction) | |
35+
| +-------------------------------+-------------------------------+ |
36+
+-----------------------------------+-----------------------------------+
37+
|
38+
+-----------------------+
39+
| |
40+
v v
41+
+-----------------------------------+---+ +---+--------------------+
42+
| Storage Module | | Plot Module (Feature) |
43+
| (gate_learner/storage.py) | | (gate_learner/plot.py) |
44+
+---------------------------------------+ +------------------------+
45+
```
46+
47+
### 1. Core ML Engine (`core.py`)
48+
Contains the mathematical representation of the neural network.
49+
- **`Layer`**: Represents a single layer containing weights, biases, and an activation function.
50+
- **`MultilayerPerceptron`**: Manages a sequence of layers. It holds pre-allocated internal buffers for activations, deltas, and gradients to avoid list allocations during the training loop.
51+
- **`AdamOptimizer`**: Implements the Adam optimization algorithm. It maintains its own state buffers (first and second moments) matching the network's architecture.
52+
53+
### 2. Dataset Generator (`dataset.py`)
54+
Generates truth tables for $N$-input OR and XOR gates. It includes safety checks to prevent memory exhaustion by limiting $N < 20$ (since dataset size scales exponentially as $2^N$).
55+
56+
### 3. Storage Module (`storage.py`)
57+
Handles JSON serialisation and deserialisation of the trained model weights and training history.
58+
59+
### 4. Plot Module (`plot.py`)
60+
Gated under optional `matplotlib` import. It renders a dual-axis PNG chart showing the training loss and accuracy curves over epochs.
61+
62+
### 5. CLI Module (`cli.py` & `main.py`)
63+
Parses command-line arguments using `typer`, coordinates the training loop, manages early stopping, and handles output generation.
64+
65+
## Key Architectural Decisions
66+
67+
### Memory Management & Allocation
68+
To achieve high performance in pure Python, list allocations are avoided inside the training loop.
69+
- The `MultilayerPerceptron` allocates its activation, delta, and gradient buffers once during initialization via `ensure_buffers()`.
70+
- During the forward and backward passes, these buffers are mutated in-place.
71+
72+
### Concurrency & Serialization
73+
The `MultilayerPerceptron` and `Layer` classes are designed to be fully picklable (serialisable). This allows the model to be safely transferred across processes using Python's `multiprocessing` module for parallel hyperparameter grid search.

‎CHANGELOG.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [0.1.0] - 2026-06-25
9+
10+
### Added
11+
- Initial release of the `gate_learner` neural network engine in Python.
12+
- Custom Multilayer Perceptron (MLP) implementation with support for arbitrary hidden layer architectures.
13+
- Sigmoid and ReLU activation functions with Xavier/Glorot and He Uniform weight initialisation.
14+
- Adam Optimizer with L2 regularisation, momentum, and bias correction.
15+
- Pre-allocated internal buffers for activations, deltas, and gradients to avoid repetitive memory allocations during training.
16+
- Dynamic dataset generator for $N$-input OR and XOR logic gates.
17+
- Early stopping mechanisms based on convergence (Loss < 0.01) and stalling (Loss change < 1e-6 over 50 epochs).
18+
- JSON serialisation and deserialisation for models and training histories.
19+
- Dual-axis plotting of training loss and accuracy curves (gated under the `visualise` extra feature using `matplotlib`).
20+
- Comprehensive unit tests, integration tests, and performance benchmarks using `pytest` and `pytest-benchmark`.

‎CODE_OF_CONDUCT.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8+
9+
## Our Standards
10+
11+
Examples of behaviour that contributes to a positive environment for our community include:
12+
13+
* Demonstrating empathy and kindness toward other people
14+
* Being respectful of differing opinions, viewpoints, and experiences
15+
* Giving and gracefully accepting constructive feedback
16+
* Accepting responsibility and apologising to those affected by our mistakes, and learning from the experience
17+
* Focusing on what is best for the overall community, not just the individual
18+
19+
Examples of unacceptable behaviour include:
20+
21+
* The use of sexualised language or imagery, and unwelcome sexual attention or advances
22+
* Trolling, insulting or derogatory comments, and personal or political attacks
23+
* Public or private harassment
24+
* Publishing others' private information, such as a physical or email address, without their explicit permission
25+
* Other conduct which could reasonably be considered inappropriate in a professional setting
26+
27+
## Enforcement Responsibilities
28+
29+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behaviour and will take appropriate and fair corrective action in response to any behaviour that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
32+
33+
## Scope
34+
35+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
36+
37+
## Enforcement
38+
39+
Instances of abusive, harassing, or otherwise unacceptable behaviour may be reported to the community leaders at **dzulkiflianwar2@gmail.com**. All complaints will be reviewed and investigated promptly and fairly.
40+
41+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.

‎CONTRIBUTING.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributing to Gate Learner
2+
3+
Thank you for your interest in contributing to `gate_learner`! We welcome contributions from everyone.
4+
5+
## Development Setup
6+
7+
Ensure you have Python 3.12+ and `uv` installed.
8+
9+
1. Clone the repository:
10+
```bash
11+
git clone https://github.com/ios-community/gate-learner.git
12+
cd gate-learner
13+
```
14+
15+
2. Set up the virtual environment and install dependencies:
16+
```bash
17+
uv sync --all-extras
18+
```
19+
20+
3. Run the test suite to verify your setup:
21+
```bash
22+
uv run pytest
23+
```
24+
25+
## Code Style & Quality
26+
27+
We enforce strict code quality standards using `ruff`. Before submitting a pull request, please ensure your code is formatted and free of lint errors:
28+
29+
```bash
30+
# Run linter checks
31+
uv run ruff check .
32+
33+
# Format code
34+
uv run ruff format .
35+
```
36+
37+
## Documentation
38+
39+
All public APIs must be fully documented using the **Google Style Python Docstrings** format. Code examples in docstrings should be written as runnable doctests and will be validated during testing.
40+
41+
## Submitting a Pull Request
42+
43+
1. Fork the repository and create your branch from `main`.
44+
2. Ensure all tests, doctests, and linter checks pass.
45+
3. Commit your changes with clear, descriptive commit messages.
46+
4. Submit a pull request to the `main` branch.

0 commit comments

Comments
 (0)