fabricpc 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fabricpc-0.4.0/LICENSE +21 -0
- fabricpc-0.4.0/PKG-INFO +184 -0
- fabricpc-0.4.0/README.md +121 -0
- fabricpc-0.4.0/fabricpc/__init__.py +84 -0
- fabricpc-0.4.0/fabricpc/core/__init__.py +111 -0
- fabricpc-0.4.0/fabricpc/core/_frozen.py +59 -0
- fabricpc-0.4.0/fabricpc/core/activations.py +390 -0
- fabricpc-0.4.0/fabricpc/core/energy.py +520 -0
- fabricpc-0.4.0/fabricpc/core/inference.py +389 -0
- fabricpc-0.4.0/fabricpc/core/initializers.py +482 -0
- fabricpc-0.4.0/fabricpc/core/learning.py +60 -0
- fabricpc-0.4.0/fabricpc/core/mupc.py +421 -0
- fabricpc-0.4.0/fabricpc/core/positional.py +49 -0
- fabricpc-0.4.0/fabricpc/core/scaling.py +111 -0
- fabricpc-0.4.0/fabricpc/core/state_ops.py +54 -0
- fabricpc-0.4.0/fabricpc/core/topology.py +84 -0
- fabricpc-0.4.0/fabricpc/core/types.py +217 -0
- fabricpc-0.4.0/fabricpc/experiments/__init__.py +33 -0
- fabricpc-0.4.0/fabricpc/experiments/ab_experiment.py +648 -0
- fabricpc-0.4.0/fabricpc/experiments/statistics.py +135 -0
- fabricpc-0.4.0/fabricpc/graph_assembly/__init__.py +5 -0
- fabricpc-0.4.0/fabricpc/graph_assembly/graph_construction.py +260 -0
- fabricpc-0.4.0/fabricpc/graph_initialization/__init__.py +24 -0
- fabricpc-0.4.0/fabricpc/graph_initialization/params_initializer.py +63 -0
- fabricpc-0.4.0/fabricpc/graph_initialization/state_initializer.py +386 -0
- fabricpc-0.4.0/fabricpc/jax_config.py +122 -0
- fabricpc-0.4.0/fabricpc/models/__init__.py +5 -0
- fabricpc-0.4.0/fabricpc/models/transformer.py +160 -0
- fabricpc-0.4.0/fabricpc/nodes/__init__.py +58 -0
- fabricpc-0.4.0/fabricpc/nodes/base.py +746 -0
- fabricpc-0.4.0/fabricpc/nodes/convolutional.py +241 -0
- fabricpc-0.4.0/fabricpc/nodes/identity.py +163 -0
- fabricpc-0.4.0/fabricpc/nodes/linear.py +228 -0
- fabricpc-0.4.0/fabricpc/nodes/linear_explicit_grad.py +174 -0
- fabricpc-0.4.0/fabricpc/nodes/linear_residual.py +202 -0
- fabricpc-0.4.0/fabricpc/nodes/pooling.py +428 -0
- fabricpc-0.4.0/fabricpc/nodes/skip_connection.py +144 -0
- fabricpc-0.4.0/fabricpc/nodes/storkey_hopfield.py +421 -0
- fabricpc-0.4.0/fabricpc/nodes/transformer.py +512 -0
- fabricpc-0.4.0/fabricpc/nodes/transformer_v2.py +422 -0
- fabricpc-0.4.0/fabricpc/training/__init__.py +66 -0
- fabricpc-0.4.0/fabricpc/training/multi_gpu.py +43 -0
- fabricpc-0.4.0/fabricpc/training/natural_gradients.py +117 -0
- fabricpc-0.4.0/fabricpc/training/optimizers.py +13 -0
- fabricpc-0.4.0/fabricpc/training/train.py +845 -0
- fabricpc-0.4.0/fabricpc/training/train_autoregressive.py +758 -0
- fabricpc-0.4.0/fabricpc/training/train_backprop.py +714 -0
- fabricpc-0.4.0/fabricpc/tuning/__init__.py +3 -0
- fabricpc-0.4.0/fabricpc/tuning/bayesian_tuner.py +418 -0
- fabricpc-0.4.0/fabricpc/utils/__init__.py +24 -0
- fabricpc-0.4.0/fabricpc/utils/dashboarding/__init__.py +80 -0
- fabricpc-0.4.0/fabricpc/utils/dashboarding/_aim_available.py +68 -0
- fabricpc-0.4.0/fabricpc/utils/dashboarding/callbacks.py +171 -0
- fabricpc-0.4.0/fabricpc/utils/dashboarding/extractors.py +287 -0
- fabricpc-0.4.0/fabricpc/utils/dashboarding/inference_tracking.py +312 -0
- fabricpc-0.4.0/fabricpc/utils/dashboarding/trackers.py +544 -0
- fabricpc-0.4.0/fabricpc/utils/data/__init__.py +25 -0
- fabricpc-0.4.0/fabricpc/utils/data/data_utils.py +45 -0
- fabricpc-0.4.0/fabricpc/utils/data/dataloader.py +657 -0
- fabricpc-0.4.0/fabricpc/utils/helpers.py +19 -0
- fabricpc-0.4.0/fabricpc.egg-info/PKG-INFO +184 -0
- fabricpc-0.4.0/fabricpc.egg-info/SOURCES.txt +92 -0
- fabricpc-0.4.0/fabricpc.egg-info/dependency_links.txt +1 -0
- fabricpc-0.4.0/fabricpc.egg-info/requires.txt +53 -0
- fabricpc-0.4.0/fabricpc.egg-info/top_level.txt +1 -0
- fabricpc-0.4.0/pyproject.toml +165 -0
- fabricpc-0.4.0/setup.cfg +4 -0
- fabricpc-0.4.0/tests/test_auto_node_grad.py +371 -0
- fabricpc-0.4.0/tests/test_bayesian_tuner.py +208 -0
- fabricpc-0.4.0/tests/test_conv_pool_integration.py +179 -0
- fabricpc-0.4.0/tests/test_convolutional.py +567 -0
- fabricpc-0.4.0/tests/test_dashboarding_extractors.py +56 -0
- fabricpc-0.4.0/tests/test_doc_snippets.py +258 -0
- fabricpc-0.4.0/tests/test_energy.py +177 -0
- fabricpc-0.4.0/tests/test_experiments.py +371 -0
- fabricpc-0.4.0/tests/test_external_custom_node.py +419 -0
- fabricpc-0.4.0/tests/test_fabricpc.py +567 -0
- fabricpc-0.4.0/tests/test_immutable_config.py +347 -0
- fabricpc-0.4.0/tests/test_inference_order.py +335 -0
- fabricpc-0.4.0/tests/test_initializers.py +206 -0
- fabricpc-0.4.0/tests/test_jax_config.py +201 -0
- fabricpc-0.4.0/tests/test_multi_gpu.py +352 -0
- fabricpc-0.4.0/tests/test_mupc.py +1107 -0
- fabricpc-0.4.0/tests/test_ndim_shapes.py +210 -0
- fabricpc-0.4.0/tests/test_optimizers.py +113 -0
- fabricpc-0.4.0/tests/test_pooling.py +553 -0
- fabricpc-0.4.0/tests/test_rope.py +145 -0
- fabricpc-0.4.0/tests/test_state_initializer.py +337 -0
- fabricpc-0.4.0/tests/test_storkey_hopfield.py +344 -0
- fabricpc-0.4.0/tests/test_token_loaders.py +116 -0
- fabricpc-0.4.0/tests/test_train_backprop.py +281 -0
- fabricpc-0.4.0/tests/test_transformer_mupc.py +234 -0
- fabricpc-0.4.0/tests/test_transformer_nodes.py +481 -0
- fabricpc-0.4.0/tests/test_transformer_v2_mupc.py +71 -0
fabricpc-0.4.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matthew Behrend
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
fabricpc-0.4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fabricpc
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: A flexible, performant predictive coding library using JAX
|
|
5
|
+
Author-email: SingularityNET Foundation <info@singularitynet.io>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/trueagi-io/FabricPC
|
|
8
|
+
Project-URL: Repository, https://github.com/trueagi-io/FabricPC
|
|
9
|
+
Project-URL: Documentation, https://github.com/trueagi-io/FabricPC/blob/main/docs/user_guides/00_index.md
|
|
10
|
+
Project-URL: Changelog, https://github.com/trueagi-io/FabricPC/blob/main/CHANGELOG.md
|
|
11
|
+
Project-URL: Issues, https://github.com/trueagi-io/FabricPC/issues
|
|
12
|
+
Keywords: predictive-coding,jax,neural-networks,machine-learning
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: jax>=0.7.0
|
|
24
|
+
Requires-Dist: optax>=0.1.7
|
|
25
|
+
Requires-Dist: orbax-checkpoint>=0.4.0
|
|
26
|
+
Requires-Dist: flax>=0.7.5
|
|
27
|
+
Requires-Dist: chex>=0.1.84
|
|
28
|
+
Requires-Dist: jaxtyping>=0.2.23
|
|
29
|
+
Requires-Dist: numpy>=1.24.0
|
|
30
|
+
Requires-Dist: tqdm>=4.65.0
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
33
|
+
Requires-Dist: hypothesis>=6.0.0; extra == "dev"
|
|
34
|
+
Requires-Dist: black[colorama]==26.1.0; extra == "dev"
|
|
35
|
+
Requires-Dist: ruff==0.15.19; extra == "dev"
|
|
36
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
37
|
+
Requires-Dist: pre-commit>=3.0.0; extra == "dev"
|
|
38
|
+
Requires-Dist: build>=1.0.0; extra == "dev"
|
|
39
|
+
Requires-Dist: twine>=5.0.0; extra == "dev"
|
|
40
|
+
Provides-Extra: tfds
|
|
41
|
+
Requires-Dist: tensorflow-datasets>=4.9.0; extra == "tfds"
|
|
42
|
+
Requires-Dist: tensorflow-cpu>=2.15.0; (platform_system == "Linux" and platform_machine == "x86_64") and extra == "tfds"
|
|
43
|
+
Requires-Dist: tensorflow>=2.15.0; (platform_system != "Linux" or platform_machine != "x86_64") and extra == "tfds"
|
|
44
|
+
Requires-Dist: importlib_resources; extra == "tfds"
|
|
45
|
+
Requires-Dist: tokenizers>=0.15.0; extra == "tfds"
|
|
46
|
+
Provides-Extra: experiments
|
|
47
|
+
Requires-Dist: scipy>=1.10.0; extra == "experiments"
|
|
48
|
+
Requires-Dist: optuna>=3.0.0; extra == "experiments"
|
|
49
|
+
Provides-Extra: viz
|
|
50
|
+
Requires-Dist: plotly>=5.0.0; extra == "viz"
|
|
51
|
+
Requires-Dist: kaleido>=0.2.1; extra == "viz"
|
|
52
|
+
Requires-Dist: pandas>=2.0.0; extra == "viz"
|
|
53
|
+
Requires-Dist: aim>=3.0.0; (python_version < "3.13" and platform_system != "Windows") and extra == "viz"
|
|
54
|
+
Provides-Extra: cpu
|
|
55
|
+
Requires-Dist: jax[cpu]; extra == "cpu"
|
|
56
|
+
Provides-Extra: cuda12
|
|
57
|
+
Requires-Dist: jax[cuda12]; extra == "cuda12"
|
|
58
|
+
Provides-Extra: cuda13
|
|
59
|
+
Requires-Dist: jax[cuda13]; extra == "cuda13"
|
|
60
|
+
Provides-Extra: all
|
|
61
|
+
Requires-Dist: fabricpc[experiments,tfds,viz]; extra == "all"
|
|
62
|
+
Dynamic: license-file
|
|
63
|
+
|
|
64
|
+
# FabricPC
|
|
65
|
+
|
|
66
|
+
**State-of-the-art predictive coding, made easy.**
|
|
67
|
+
|
|
68
|
+
FabricPC is an easy-to-use, high-performance open-source Python library for building and training predictive coding networks. It is designed to get researchers from idea to running experiment as fast as possible, eliminating algorithm boilerplate. A single directed edge between nodes is all that's needed to define a connection. Local derivatives are built in, following graph topology. The framework handles inference and learning dynamics automatically for whatever you write in a node's `forward()` method.
|
|
69
|
+
|
|
70
|
+
Built on JAX for GPU and multi-GPU acceleration with local (node-level) automatic differentiation.
|
|
71
|
+
|
|
72
|
+
## What It Does
|
|
73
|
+
|
|
74
|
+
FabricPC supports arbitrary graph topologies: feedforward, recurrent, skip connections, and cyclic architectures. Heterogeneous components such as linear, convolutional, and pooling nodes, transformer blocks, and Storkey-Hopfield associative memory coexist within the same energy-minimization graph. The same graph topology can be trained by predictive coding (`train_pcn`) or by backpropagation (`train_backprop`), so controlled PC-vs-backprop comparisons reuse one model definition instead of two. See `examples/PC_backprop_compare.py`.
|
|
75
|
+
|
|
76
|
+
Internally, everything is organized around three abstractions: nodes (state and computation), edges (connections between nodes), and updates (inference and learning algorithms).
|
|
77
|
+
|
|
78
|
+
## Installation
|
|
79
|
+
|
|
80
|
+
Python 3.11–3.13. Install into a virtual environment, not the system Python. Create and activate the environment, then one command installs FabricPC, its optional dependencies, and a version-matched JAX backend — pick the line for your hardware:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
python3 -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
84
|
+
|
|
85
|
+
pip install -U "fabricpc[all,cuda13]" # GPU, CUDA 13 (NVIDIA driver ≥580)
|
|
86
|
+
pip install -U "fabricpc[all,cuda12]" # GPU, CUDA 12
|
|
87
|
+
pip install -U "fabricpc[all]" # CPU only
|
|
88
|
+
pip install fabricpc # core library only, CPU
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`nvidia-smi` reports the CUDA version your driver supports.
|
|
92
|
+
|
|
93
|
+
**Platform:** GPU acceleration requires **Linux** (x86_64 or aarch64) — JAX publishes CUDA wheels for Linux only. On native Windows or macOS, install CPU-only; for GPU on Windows use WSL2 (JAX marks WSL2 GPU support experimental). The optional Aim experiment tracker in `[viz]`/`[all]` is Linux/macOS only and supports Python ≤3.12; on Windows or Python 3.13 it is skipped automatically and everything else installs normally.
|
|
94
|
+
|
|
95
|
+
See the [installation guide](https://github.com/trueagi-io/FabricPC/blob/main/docs/user_guides/01_installation.md) for details.
|
|
96
|
+
|
|
97
|
+
### From source (contributors)
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
git clone https://github.com/trueagi-io/FabricPC.git
|
|
101
|
+
cd FabricPC
|
|
102
|
+
python3 -m venv .venv && source .venv/bin/activate
|
|
103
|
+
pip install -U -e ".[all,dev]" # add a backend extra for GPU: ".[all,dev,cuda12]"
|
|
104
|
+
|
|
105
|
+
# Install pre-commit hooks for code quality
|
|
106
|
+
pre-commit install
|
|
107
|
+
|
|
108
|
+
# Run an example
|
|
109
|
+
python examples/mnist_demo.py
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Build a Model
|
|
113
|
+
|
|
114
|
+
Define the graph. Initialize the parameters. Start experimenting.
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
import jax
|
|
118
|
+
from fabricpc.nodes import Linear
|
|
119
|
+
from fabricpc.core.topology import Edge
|
|
120
|
+
from fabricpc.graph_assembly import TaskMap, graph
|
|
121
|
+
from fabricpc.graph_initialization import initialize_params
|
|
122
|
+
from fabricpc.core.inference import InferenceSGD
|
|
123
|
+
from fabricpc import setup_jax
|
|
124
|
+
|
|
125
|
+
setup_jax()
|
|
126
|
+
|
|
127
|
+
layer1 = Linear(shape=(784,), name="input")
|
|
128
|
+
layer2 = Linear(shape=(256,), name="hidden")
|
|
129
|
+
layer3 = Linear(shape=(10,), name="output")
|
|
130
|
+
|
|
131
|
+
structure = graph(
|
|
132
|
+
nodes=[layer1, layer2, layer3],
|
|
133
|
+
edges=[Edge(source=layer1, target=layer2.slot("in")),
|
|
134
|
+
Edge(source=layer2, target=layer3.slot("in"))],
|
|
135
|
+
task_map=TaskMap(x=layer1, y=layer3),
|
|
136
|
+
inference=InferenceSGD(eta_infer=0.05, infer_steps=20),
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
rng_key = jax.random.PRNGKey(0)
|
|
140
|
+
params = initialize_params(structure, rng_key)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Demos
|
|
144
|
+
|
|
145
|
+
The [`examples`](https://github.com/trueagi-io/FabricPC/tree/main/examples) folder includes working demonstrations across image classification, sequence modeling, depth scaling (`examples/scaling/`), associative memory, and architectural probes. Start with [`mnist_demo.py`](https://github.com/trueagi-io/FabricPC/blob/main/examples/mnist_demo.py) (over 98% accuracy on MNIST) and explore from there:
|
|
146
|
+
|
|
147
|
+
- [`mnist_conv_demo.py`](https://github.com/trueagi-io/FabricPC/blob/main/examples/mnist_conv_demo.py) — convolutional MNIST classifier with `ConvNode` and `MaxPool`
|
|
148
|
+
- [`resnet18_cifar10_demo.py`](https://github.com/trueagi-io/FabricPC/blob/main/examples/resnet18_cifar10_demo.py) — ResNet-18 as a PC graph, with global average pooling
|
|
149
|
+
- [`transformer_v2_demo.py`](https://github.com/trueagi-io/FabricPC/blob/main/examples/transformer_v2_demo.py) — character- or BPE-level language modeling with text generation
|
|
150
|
+
- [`transformer_tuning.py`](https://github.com/trueagi-io/FabricPC/blob/main/examples/transformer_tuning.py) — two-phase hyperparameter search minimizing validation perplexity
|
|
151
|
+
|
|
152
|
+
## Documentation
|
|
153
|
+
|
|
154
|
+
User guides, API reference, and tutorials live in [`docs/user_guides`](https://github.com/trueagi-io/FabricPC/blob/main/docs/user_guides/00_index.md). Development plans and technical design documents are in [`docs/dev_plans`](https://github.com/trueagi-io/FabricPC/tree/main/docs/dev_plans).
|
|
155
|
+
|
|
156
|
+
## Extending FabricPC
|
|
157
|
+
|
|
158
|
+
### Custom Nodes
|
|
159
|
+
|
|
160
|
+
Create custom node types by subclassing `NodeBase`. Implement the `get_slots()`, `initialize_params()`, and `forward()` methods. Nodes have a single output. Slots define incoming connections and are referenced in edges when building the graph.
|
|
161
|
+
|
|
162
|
+
See [`docs/user_guides/06_custom_nodes.md`](https://github.com/trueagi-io/FabricPC/blob/main/docs/user_guides/06_custom_nodes.md) for the node contract and a Conv2D teaching example (the production node is `fabricpc.nodes.ConvNode`).
|
|
163
|
+
|
|
164
|
+
## Contributing
|
|
165
|
+
|
|
166
|
+
Contributions are welcome! Please open issues or pull requests on the GitHub repository.
|
|
167
|
+
- Develop on a branch using the convention `username/your_feature_name`.
|
|
168
|
+
- Demos must match baseline results, or explain any divergence.
|
|
169
|
+
- The test suite must pass.
|
|
170
|
+
- Write unit tests and docstrings for new code.
|
|
171
|
+
- Use the pre-commit hooks for PEP8 style and code quality.
|
|
172
|
+
- Rebase before opening PR.
|
|
173
|
+
|
|
174
|
+
This is a research-first project.
|
|
175
|
+
- APIs may change frequently until the v1.0 release.
|
|
176
|
+
- Any breaking changes are documented in the changelog.
|
|
177
|
+
|
|
178
|
+
## Team
|
|
179
|
+
|
|
180
|
+
FabricPC is actively maintained by SingularityNET as part of the Artificial Superintelligence Alliance. Project lead: Dr. Matthew Behrend.
|
|
181
|
+
|
|
182
|
+
## License
|
|
183
|
+
|
|
184
|
+
This project is licensed under the [MIT License](https://github.com/trueagi-io/FabricPC/blob/main/LICENSE).
|
fabricpc-0.4.0/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# FabricPC
|
|
2
|
+
|
|
3
|
+
**State-of-the-art predictive coding, made easy.**
|
|
4
|
+
|
|
5
|
+
FabricPC is an easy-to-use, high-performance open-source Python library for building and training predictive coding networks. It is designed to get researchers from idea to running experiment as fast as possible, eliminating algorithm boilerplate. A single directed edge between nodes is all that's needed to define a connection. Local derivatives are built in, following graph topology. The framework handles inference and learning dynamics automatically for whatever you write in a node's `forward()` method.
|
|
6
|
+
|
|
7
|
+
Built on JAX for GPU and multi-GPU acceleration with local (node-level) automatic differentiation.
|
|
8
|
+
|
|
9
|
+
## What It Does
|
|
10
|
+
|
|
11
|
+
FabricPC supports arbitrary graph topologies: feedforward, recurrent, skip connections, and cyclic architectures. Heterogeneous components such as linear, convolutional, and pooling nodes, transformer blocks, and Storkey-Hopfield associative memory coexist within the same energy-minimization graph. The same graph topology can be trained by predictive coding (`train_pcn`) or by backpropagation (`train_backprop`), so controlled PC-vs-backprop comparisons reuse one model definition instead of two. See `examples/PC_backprop_compare.py`.
|
|
12
|
+
|
|
13
|
+
Internally, everything is organized around three abstractions: nodes (state and computation), edges (connections between nodes), and updates (inference and learning algorithms).
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
Python 3.11–3.13. Install into a virtual environment, not the system Python. Create and activate the environment, then one command installs FabricPC, its optional dependencies, and a version-matched JAX backend — pick the line for your hardware:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
python3 -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
21
|
+
|
|
22
|
+
pip install -U "fabricpc[all,cuda13]" # GPU, CUDA 13 (NVIDIA driver ≥580)
|
|
23
|
+
pip install -U "fabricpc[all,cuda12]" # GPU, CUDA 12
|
|
24
|
+
pip install -U "fabricpc[all]" # CPU only
|
|
25
|
+
pip install fabricpc # core library only, CPU
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`nvidia-smi` reports the CUDA version your driver supports.
|
|
29
|
+
|
|
30
|
+
**Platform:** GPU acceleration requires **Linux** (x86_64 or aarch64) — JAX publishes CUDA wheels for Linux only. On native Windows or macOS, install CPU-only; for GPU on Windows use WSL2 (JAX marks WSL2 GPU support experimental). The optional Aim experiment tracker in `[viz]`/`[all]` is Linux/macOS only and supports Python ≤3.12; on Windows or Python 3.13 it is skipped automatically and everything else installs normally.
|
|
31
|
+
|
|
32
|
+
See the [installation guide](https://github.com/trueagi-io/FabricPC/blob/main/docs/user_guides/01_installation.md) for details.
|
|
33
|
+
|
|
34
|
+
### From source (contributors)
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
git clone https://github.com/trueagi-io/FabricPC.git
|
|
38
|
+
cd FabricPC
|
|
39
|
+
python3 -m venv .venv && source .venv/bin/activate
|
|
40
|
+
pip install -U -e ".[all,dev]" # add a backend extra for GPU: ".[all,dev,cuda12]"
|
|
41
|
+
|
|
42
|
+
# Install pre-commit hooks for code quality
|
|
43
|
+
pre-commit install
|
|
44
|
+
|
|
45
|
+
# Run an example
|
|
46
|
+
python examples/mnist_demo.py
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Build a Model
|
|
50
|
+
|
|
51
|
+
Define the graph. Initialize the parameters. Start experimenting.
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
import jax
|
|
55
|
+
from fabricpc.nodes import Linear
|
|
56
|
+
from fabricpc.core.topology import Edge
|
|
57
|
+
from fabricpc.graph_assembly import TaskMap, graph
|
|
58
|
+
from fabricpc.graph_initialization import initialize_params
|
|
59
|
+
from fabricpc.core.inference import InferenceSGD
|
|
60
|
+
from fabricpc import setup_jax
|
|
61
|
+
|
|
62
|
+
setup_jax()
|
|
63
|
+
|
|
64
|
+
layer1 = Linear(shape=(784,), name="input")
|
|
65
|
+
layer2 = Linear(shape=(256,), name="hidden")
|
|
66
|
+
layer3 = Linear(shape=(10,), name="output")
|
|
67
|
+
|
|
68
|
+
structure = graph(
|
|
69
|
+
nodes=[layer1, layer2, layer3],
|
|
70
|
+
edges=[Edge(source=layer1, target=layer2.slot("in")),
|
|
71
|
+
Edge(source=layer2, target=layer3.slot("in"))],
|
|
72
|
+
task_map=TaskMap(x=layer1, y=layer3),
|
|
73
|
+
inference=InferenceSGD(eta_infer=0.05, infer_steps=20),
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
rng_key = jax.random.PRNGKey(0)
|
|
77
|
+
params = initialize_params(structure, rng_key)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Demos
|
|
81
|
+
|
|
82
|
+
The [`examples`](https://github.com/trueagi-io/FabricPC/tree/main/examples) folder includes working demonstrations across image classification, sequence modeling, depth scaling (`examples/scaling/`), associative memory, and architectural probes. Start with [`mnist_demo.py`](https://github.com/trueagi-io/FabricPC/blob/main/examples/mnist_demo.py) (over 98% accuracy on MNIST) and explore from there:
|
|
83
|
+
|
|
84
|
+
- [`mnist_conv_demo.py`](https://github.com/trueagi-io/FabricPC/blob/main/examples/mnist_conv_demo.py) — convolutional MNIST classifier with `ConvNode` and `MaxPool`
|
|
85
|
+
- [`resnet18_cifar10_demo.py`](https://github.com/trueagi-io/FabricPC/blob/main/examples/resnet18_cifar10_demo.py) — ResNet-18 as a PC graph, with global average pooling
|
|
86
|
+
- [`transformer_v2_demo.py`](https://github.com/trueagi-io/FabricPC/blob/main/examples/transformer_v2_demo.py) — character- or BPE-level language modeling with text generation
|
|
87
|
+
- [`transformer_tuning.py`](https://github.com/trueagi-io/FabricPC/blob/main/examples/transformer_tuning.py) — two-phase hyperparameter search minimizing validation perplexity
|
|
88
|
+
|
|
89
|
+
## Documentation
|
|
90
|
+
|
|
91
|
+
User guides, API reference, and tutorials live in [`docs/user_guides`](https://github.com/trueagi-io/FabricPC/blob/main/docs/user_guides/00_index.md). Development plans and technical design documents are in [`docs/dev_plans`](https://github.com/trueagi-io/FabricPC/tree/main/docs/dev_plans).
|
|
92
|
+
|
|
93
|
+
## Extending FabricPC
|
|
94
|
+
|
|
95
|
+
### Custom Nodes
|
|
96
|
+
|
|
97
|
+
Create custom node types by subclassing `NodeBase`. Implement the `get_slots()`, `initialize_params()`, and `forward()` methods. Nodes have a single output. Slots define incoming connections and are referenced in edges when building the graph.
|
|
98
|
+
|
|
99
|
+
See [`docs/user_guides/06_custom_nodes.md`](https://github.com/trueagi-io/FabricPC/blob/main/docs/user_guides/06_custom_nodes.md) for the node contract and a Conv2D teaching example (the production node is `fabricpc.nodes.ConvNode`).
|
|
100
|
+
|
|
101
|
+
## Contributing
|
|
102
|
+
|
|
103
|
+
Contributions are welcome! Please open issues or pull requests on the GitHub repository.
|
|
104
|
+
- Develop on a branch using the convention `username/your_feature_name`.
|
|
105
|
+
- Demos must match baseline results, or explain any divergence.
|
|
106
|
+
- The test suite must pass.
|
|
107
|
+
- Write unit tests and docstrings for new code.
|
|
108
|
+
- Use the pre-commit hooks for PEP8 style and code quality.
|
|
109
|
+
- Rebase before opening PR.
|
|
110
|
+
|
|
111
|
+
This is a research-first project.
|
|
112
|
+
- APIs may change frequently until the v1.0 release.
|
|
113
|
+
- Any breaking changes are documented in the changelog.
|
|
114
|
+
|
|
115
|
+
## Team
|
|
116
|
+
|
|
117
|
+
FabricPC is actively maintained by SingularityNET as part of the Artificial Superintelligence Alliance. Project lead: Dr. Matthew Behrend.
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
This project is licensed under the [MIT License](https://github.com/trueagi-io/FabricPC/blob/main/LICENSE).
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""
|
|
2
|
+
FabricPC-JAX: Predictive Coding Networks in JAX
|
|
3
|
+
================================================
|
|
4
|
+
|
|
5
|
+
A functional, high-performance implementation of predictive coding networks
|
|
6
|
+
using JAX for automatic differentiation, JIT compilation, and multi-device parallelism.
|
|
7
|
+
|
|
8
|
+
Key Features:
|
|
9
|
+
- Functional programming paradigm (immutable data structures)
|
|
10
|
+
- JIT-compiled inference and training loops
|
|
11
|
+
- Multi-GPU/TPU support with pmap
|
|
12
|
+
- XLA optimization for maximum performance
|
|
13
|
+
|
|
14
|
+
Example:
|
|
15
|
+
>>> from fabricpc.nodes import Linear
|
|
16
|
+
>>> from fabricpc.core.topology import Edge
|
|
17
|
+
>>> from fabricpc.graph_assembly import TaskMap, graph
|
|
18
|
+
>>> from fabricpc.graph_initialization import initialize_params
|
|
19
|
+
>>> from fabricpc.training import train_pcn, evaluate_pcn
|
|
20
|
+
>>>
|
|
21
|
+
>>> # Define nodes
|
|
22
|
+
>>> input_node = Linear(shape=(784,), name="input")
|
|
23
|
+
>>> hidden = Linear(shape=(128,), name="hidden")
|
|
24
|
+
>>> output = Linear(shape=(10,), name="output")
|
|
25
|
+
>>>
|
|
26
|
+
>>> # Build graph
|
|
27
|
+
>>> structure = graph(
|
|
28
|
+
... nodes=[input_node, hidden, output],
|
|
29
|
+
... edges=[
|
|
30
|
+
... Edge(source=input_node, target=hidden.slot("in")),
|
|
31
|
+
... Edge(source=hidden, target=output.slot("in")),
|
|
32
|
+
... ],
|
|
33
|
+
... task_map=TaskMap(x=input_node, y=output),
|
|
34
|
+
... inference=InferenceSGD(eta_infer=0.05, infer_steps=10),
|
|
35
|
+
... )
|
|
36
|
+
>>> params = initialize_params(structure, rng_key)
|
|
37
|
+
>>> trained_params, history, _ = train_pcn(params, structure, train_loader, config)
|
|
38
|
+
>>> metrics = evaluate_pcn(trained_params, structure, test_loader, config)
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
from importlib.metadata import version
|
|
42
|
+
|
|
43
|
+
__version__ = version("fabricpc")
|
|
44
|
+
|
|
45
|
+
# Submodules (for advanced use)
|
|
46
|
+
from fabricpc import (
|
|
47
|
+
core,
|
|
48
|
+
graph_initialization,
|
|
49
|
+
nodes,
|
|
50
|
+
training,
|
|
51
|
+
utils,
|
|
52
|
+
graph_assembly,
|
|
53
|
+
models,
|
|
54
|
+
experiments,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
# Core API - what most users need
|
|
58
|
+
from fabricpc.graph_initialization import initialize_params
|
|
59
|
+
from fabricpc.training import train_pcn, evaluate_pcn
|
|
60
|
+
from fabricpc.jax_config import setup_jax
|
|
61
|
+
|
|
62
|
+
# Types - for type hints
|
|
63
|
+
from fabricpc.core.types import GraphParams, GraphState, GraphStructure
|
|
64
|
+
|
|
65
|
+
__all__ = [
|
|
66
|
+
# Core API (common use)
|
|
67
|
+
"initialize_params",
|
|
68
|
+
"train_pcn",
|
|
69
|
+
"evaluate_pcn",
|
|
70
|
+
"setup_jax",
|
|
71
|
+
# Types (for type hints)
|
|
72
|
+
"GraphParams",
|
|
73
|
+
"GraphState",
|
|
74
|
+
"GraphStructure",
|
|
75
|
+
# Submodules (advanced use)
|
|
76
|
+
"core",
|
|
77
|
+
"graph_assembly",
|
|
78
|
+
"graph_initialization",
|
|
79
|
+
"models",
|
|
80
|
+
"nodes",
|
|
81
|
+
"training",
|
|
82
|
+
"utils",
|
|
83
|
+
"experiments",
|
|
84
|
+
]
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""Core JAX predictive coding components."""
|
|
2
|
+
|
|
3
|
+
# Type definitions
|
|
4
|
+
from fabricpc.core.types import (
|
|
5
|
+
GraphParams,
|
|
6
|
+
GraphState,
|
|
7
|
+
GraphStructure,
|
|
8
|
+
NodeInfo,
|
|
9
|
+
EdgeInfo,
|
|
10
|
+
SlotInfo,
|
|
11
|
+
NodeParams,
|
|
12
|
+
NodeState,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
# Activation functions
|
|
16
|
+
from fabricpc.core.activations import (
|
|
17
|
+
ActivationBase,
|
|
18
|
+
IdentityActivation,
|
|
19
|
+
SigmoidActivation,
|
|
20
|
+
TanhActivation,
|
|
21
|
+
ReLUActivation,
|
|
22
|
+
LeakyReLUActivation,
|
|
23
|
+
GeluActivation,
|
|
24
|
+
SoftmaxActivation,
|
|
25
|
+
HardTanhActivation,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
# Energy functions
|
|
29
|
+
from fabricpc.core.energy import (
|
|
30
|
+
EnergyFunctional,
|
|
31
|
+
GaussianEnergy,
|
|
32
|
+
BernoulliEnergy,
|
|
33
|
+
CrossEntropyEnergy,
|
|
34
|
+
LaplacianEnergy,
|
|
35
|
+
HuberEnergy,
|
|
36
|
+
KLDivergenceEnergy,
|
|
37
|
+
compute_energy,
|
|
38
|
+
compute_energy_gradient,
|
|
39
|
+
get_energy_and_gradient,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# Inference functions and classes
|
|
43
|
+
from fabricpc.core.inference import (
|
|
44
|
+
InferenceBase,
|
|
45
|
+
InferenceSGD,
|
|
46
|
+
InferenceSGDNormClip,
|
|
47
|
+
gather_inputs,
|
|
48
|
+
run_inference,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# Initializers
|
|
52
|
+
from fabricpc.core.initializers import (
|
|
53
|
+
InitializerBase,
|
|
54
|
+
ZerosInitializer,
|
|
55
|
+
OnesInitializer,
|
|
56
|
+
NormalInitializer,
|
|
57
|
+
UniformInitializer,
|
|
58
|
+
XavierInitializer,
|
|
59
|
+
KaimingInitializer,
|
|
60
|
+
MuPCInitializer,
|
|
61
|
+
initialize,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
__all__ = [
|
|
65
|
+
# Types
|
|
66
|
+
"GraphParams",
|
|
67
|
+
"GraphState",
|
|
68
|
+
"GraphStructure",
|
|
69
|
+
"NodeInfo",
|
|
70
|
+
"EdgeInfo",
|
|
71
|
+
"SlotInfo",
|
|
72
|
+
"NodeParams",
|
|
73
|
+
"NodeState",
|
|
74
|
+
# Activation functions
|
|
75
|
+
"ActivationBase",
|
|
76
|
+
"IdentityActivation",
|
|
77
|
+
"SigmoidActivation",
|
|
78
|
+
"TanhActivation",
|
|
79
|
+
"ReLUActivation",
|
|
80
|
+
"LeakyReLUActivation",
|
|
81
|
+
"GeluActivation",
|
|
82
|
+
"SoftmaxActivation",
|
|
83
|
+
"HardTanhActivation",
|
|
84
|
+
# Energy functions
|
|
85
|
+
"EnergyFunctional",
|
|
86
|
+
"GaussianEnergy",
|
|
87
|
+
"BernoulliEnergy",
|
|
88
|
+
"CrossEntropyEnergy",
|
|
89
|
+
"LaplacianEnergy",
|
|
90
|
+
"HuberEnergy",
|
|
91
|
+
"KLDivergenceEnergy",
|
|
92
|
+
"compute_energy",
|
|
93
|
+
"compute_energy_gradient",
|
|
94
|
+
"get_energy_and_gradient",
|
|
95
|
+
# Inference
|
|
96
|
+
"InferenceBase",
|
|
97
|
+
"InferenceSGD",
|
|
98
|
+
"InferenceSGDNormClip",
|
|
99
|
+
"gather_inputs",
|
|
100
|
+
"run_inference",
|
|
101
|
+
# Initializers
|
|
102
|
+
"InitializerBase",
|
|
103
|
+
"ZerosInitializer",
|
|
104
|
+
"OnesInitializer",
|
|
105
|
+
"NormalInitializer",
|
|
106
|
+
"UniformInitializer",
|
|
107
|
+
"XavierInitializer",
|
|
108
|
+
"KaimingInitializer",
|
|
109
|
+
"MuPCInitializer",
|
|
110
|
+
"initialize",
|
|
111
|
+
]
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Shared immutability mixin for stateless config value objects.
|
|
2
|
+
|
|
3
|
+
``ActivationBase``, ``EnergyFunctional``, and ``InitializerBase`` place a single
|
|
4
|
+
default instance directly in node ``__init__`` signatures. A signature default
|
|
5
|
+
is evaluated once at import and shared by every defaulted call, so that instance
|
|
6
|
+
is only safe if it cannot be mutated. ``FrozenConfig`` is the single source of
|
|
7
|
+
that freeze for all three families.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import types
|
|
11
|
+
|
|
12
|
+
# Immutable scalar types accepted as config values. Tuples are accepted too,
|
|
13
|
+
# recursively. A future structured value (e.g. a per-channel alpha) is a tuple,
|
|
14
|
+
# not a list or an array.
|
|
15
|
+
_IMMUTABLE_SCALARS = (bool, int, float, str, bytes, type(None))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _validate_immutable(value, key, owner):
|
|
19
|
+
"""Reject any config value that is not an immutable scalar or a tuple of them."""
|
|
20
|
+
if isinstance(value, _IMMUTABLE_SCALARS):
|
|
21
|
+
return
|
|
22
|
+
if isinstance(value, tuple):
|
|
23
|
+
for item in value:
|
|
24
|
+
_validate_immutable(item, key, owner)
|
|
25
|
+
return
|
|
26
|
+
raise TypeError(
|
|
27
|
+
f"{owner} config value {key!r} must be an immutable scalar "
|
|
28
|
+
f"(int, float, str, bool, bytes, None) or a tuple of those; "
|
|
29
|
+
f"got {type(value).__name__}"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class FrozenConfig:
|
|
34
|
+
"""Freezes an instance after construction.
|
|
35
|
+
|
|
36
|
+
Once ``__init__`` has run, attributes cannot be set or deleted, and
|
|
37
|
+
``config`` is a read-only mapping whose keys cannot be added, removed, or
|
|
38
|
+
reassigned. Config values are validated at construction: only immutable
|
|
39
|
+
scalars (int, float, str, bool, bytes, None) and tuples of those are
|
|
40
|
+
accepted, so the whole object is immutable, not just its top level.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(self, **config):
|
|
44
|
+
for key, value in config.items():
|
|
45
|
+
_validate_immutable(value, key, type(self).__name__)
|
|
46
|
+
# object.__setattr__ bypasses the freeze below to set these two fields
|
|
47
|
+
# once; every later assignment goes through __setattr__ and is rejected.
|
|
48
|
+
object.__setattr__(self, "config", types.MappingProxyType(config))
|
|
49
|
+
object.__setattr__(self, "_frozen", True)
|
|
50
|
+
|
|
51
|
+
def __setattr__(self, name, value):
|
|
52
|
+
if getattr(self, "_frozen", False):
|
|
53
|
+
raise AttributeError(
|
|
54
|
+
f"{type(self).__name__} is immutable; cannot set {name!r}"
|
|
55
|
+
)
|
|
56
|
+
object.__setattr__(self, name, value)
|
|
57
|
+
|
|
58
|
+
def __delattr__(self, name):
|
|
59
|
+
raise AttributeError(f"{type(self).__name__} is immutable")
|