Metadata-Version: 2.4
Name: eml-cost-torch
Version: 0.5.2
Summary: In-development PyTorch diagnostics for Pfaffian layer profiles, EML-graph curvature, differential-Galois bounds, and Schwarz 2F1 detection.
Author: Monogate Research
License-Expression: Apache-2.0
Keywords: pytorch,profiler,neural-network,pfaffian,complexity
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: eml-cost>=0.20.1
Requires-Dist: sympy>=1.12
Provides-Extra: torch
Requires-Dist: torch>=2.0; extra == "torch"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: mypy>=1.5; extra == "dev"
Requires-Dist: torch>=2.0; extra == "dev"
Dynamic: license-file

# eml-cost-torch

In-development PyTorch diagnostics: per-layer Pfaffian profile of a
`torch.nn.Module`, EML-graph curvature helpers, differential-Galois bounds,
and Schwarz 2F1 detection.

## Status

In development. The package reports diagnostics and empirical summaries; it
does not claim formal verification of a model.

## Install

Base install is lightweight and does not install PyTorch:

```bash
pip install eml-cost-torch
```

Install the PyTorch-backed APIs with:

```bash
pip install 'eml-cost-torch[torch]'
```

## Quick start

```python
import torch.nn as nn
from eml_cost_torch import profile

model = nn.Sequential(
    nn.Linear(128, 64),
    nn.ReLU(),
    nn.Linear(64, 32),
    nn.GELU(),
    nn.Linear(32, 10),
)

p = profile(model)

print(p.total_layers)                  # 5
print(p.total_pfaffian_depth)          # 0 — all r=0 (ReLU=0, GELU=non-EML, Linear=0)
print(p.transcendental_layer_count)    # 0
print(p.non_eml_layer_count)           # 1 — GELU uses erf
print(p.estimated_pfaffian_width)      # 0 — no softmax/attention

for layer in p.layers:
    print(f"  {layer.name:8s}  {layer.activation:30s}  r={layer.pfaffian_r}")
```

## What it does

Walks the module graph statically (does NOT execute the model), classifies
each layer against an internal registry of activation/operator types, and
returns a structured profile.

Registry covers ~50 standard `torch.nn` modules:
- Linear / Conv / Norm: `r=0` (polynomial)
- ReLU family / Hard sigmoid / Hard swish: `r=0`
- Sigmoid, Tanh, Softplus, ELU, SiLU/Swish: `r=1`
- Mish: `r=3`
- GELU: flagged `is_pfaffian_not_eml=True` (uses erf, outside EML class)
- Softmax / MultiheadAttention: contributes to `estimated_pfaffian_width`

## Why this matters for architecture search

Across the internal test set, NN training cost appeared to correlate with
Pfaffian width. This profile gives a static diagnostic input you can use as
a search heuristic before training.

## Library API

```python
from eml_cost_torch import profile, ModelProfile, LayerProfile

p: ModelProfile = profile(model)

# Aggregate fields
p.layers                       # list[LayerProfile]
p.total_layers                 # int
p.total_pfaffian_depth         # sum of r over all layers
p.total_eml_depth              # sum of depth
p.transcendental_layer_count   # count of layers with r >= 1
p.non_eml_layer_count          # count of layers using non-EML primitives (e.g., GELU)
p.estimated_pfaffian_width     # parallel-chain count (softmax + attention)
p.total_params                 # parameter count

# Per-layer fields
layer.name                  # named_modules path
layer.cls_name              # Python class name
layer.activation            # friendly description
layer.pfaffian_r            # chain order
layer.eml_depth             # routing depth
layer.is_pfaffian_not_eml   # True for GELU and similar
layer.n_params              # parameters at this layer
```

## Curvature diagnostic (eml-graph)

Closed-form Gaussian + mean curvature of the eml surface
`z = exp(x) - ln(y + 1)`. Use it as a diagnostic on training inputs
or intermediate activations to see where in input space the eml
geometry is steeply curved — typically a fingerprint of regions
where finite-precision evaluation drift is largest.

```python
import torch
from eml_cost_torch import gaussian_K, mean_H, curvature_summary

x = torch.randn(1000) * 0.5
y = torch.randn(1000) * 0.5 + 1.0   # keep y > -1 to stay off the branch cut
s = curvature_summary(x, y)

s.K_min, s.K_max                    # range of Gaussian curvature
s.K_log10_dynamic_range             # orders of magnitude spanned
s.K_median, s.K_mean                # central tendency
s.H_min, s.H_max, s.H_median        # mean curvature stats
s.n_finite, s.n_nonfinite           # samples that landed on y = -1 etc.
```

`gaussian_K` and `mean_H` are autograd-friendly torch ops; they
preserve the input tensor's dtype and device. Both K and H are
real-valued; inputs with `y == -1` produce non-finite outputs (the
ln branch point) and are filtered out by `curvature_summary`.

Closed forms and the supporting derivation live in
`monogate-research/exploration/C247_omega_differential_geometry/`.
This is intentionally a **diagnostic only**, not a learning-rate
scheduler — the C-247b prototype showed that Adam absorbs the same
signal through its second-moment estimate, so explicit curvature
scaling does not improve training.

## License

Apache-2.0. See LICENSE.
