pydanttention
Transformer model attention in Pydantic.
Adapted from the source by Theia Vogel (MIT licensed, included here as vogel_manual_transformer.py):
In turn using model ops from picoGPT (MIT license)
Motivation
Rewriting AI model source code as Pydantic data models is an interesting exercise. I'd note the following benefits.
-
All operations can be subclassed from an arbitrary
Operationmodel (see.models.ops.base), i.e. they all expect their first argument to be a numpy arrayx. This naturally allows you to factor your code around a category of 'operations'. -
Since all functions get turned into a class (a Pydantic data model with type-annotated fields for input state rather than funcdef kw/args), and classes are conventionally named in
PascalCasewhereas functions (like all other Python variables) are conventionally named insnake_case, you can easily observe from case alone where significant operations are called, as well as where the data model is referenced (byself.{field}) making these 2 types of data access distinct from the intermediate variables. This gives a better sense at a glance of data flow through your program. -
State can be configured at runtime but also given defaults at import time through use of fields in the data model. The original source code hardcoded values in the config as module globals (similarly to using class variables), it was not possible to configure component parts at runtime. This was appropriate to author an expository demo, but made it difficult to approach as a reader wishing to modify and experiment (likewise code is easier to test if easier to configure at runtime).
-
Clear and consolidated declarations of input data (i.e. not scattered across many sites of declaration) without losing the ability to decompose into structured components. The original code used primitive types (lists of dictionaries) for the attention blocks, which became model field defaults in a self-contained module (see
.models.config). Since Pydantic allows you to load ("validate") typed data models from these primitive types, we could supply the original dictionary primitive toAttentionBlock.model_validateand it'd still work (but doing so is actually more verbose than just constructing the model class directly).
Metadata
Release files for pydanttention 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydanttention-0.1.2.tar.gz | 10.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydanttention-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.8 kB
Release files / pydanttention-0.1.2.tar.gz
| Download URL | pydanttention-0.1.2.tar.gz |
|---|---|
| Size | 10.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1275c31aaa5a912c2d489a8077f8832787013cc27751e841db90864107cb56a7
|
|
BLAKE2b-256 checksum How to use checksums |
52c48b0d5efa6089770f22ef4613ec26d6ddfaca1a77571e0279e0bf30c334ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.9.1 CPython/3.10.6
|
Release files / pydanttention-0.1.2-py3-none-any.whl
| Download URL | pydanttention-0.1.2-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8668d6ca0ba4cdb7ea71744b81ff15b1c054054f5f862f7a3a30557226c9714d
|
|
BLAKE2b-256 checksum How to use checksums |
7ffa62986ff64578eca0bb9957cff487efc348baebefcd3bb1f867e3d2a1670f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.9.1 CPython/3.10.6
|