cida-plugin 1.0.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.
- cida_plugin-1.0.0/LICENSE +201 -0
- cida_plugin-1.0.0/PKG-INFO +167 -0
- cida_plugin-1.0.0/README.md +133 -0
- cida_plugin-1.0.0/cida_plugin/__init__.py +62 -0
- cida_plugin-1.0.0/cida_plugin/agent.py +198 -0
- cida_plugin-1.0.0/cida_plugin/config.py +228 -0
- cida_plugin-1.0.0/cida_plugin/consensus.py +178 -0
- cida_plugin-1.0.0/cida_plugin/core.py +451 -0
- cida_plugin-1.0.0/cida_plugin/deliberation.py +376 -0
- cida_plugin-1.0.0/cida_plugin/diagnostics.py +124 -0
- cida_plugin-1.0.0/cida_plugin/hf.py +194 -0
- cida_plugin-1.0.0/cida_plugin/liquid_dynamics.py +291 -0
- cida_plugin-1.0.0/cida_plugin/losses.py +174 -0
- cida_plugin-1.0.0/cida_plugin/translator.py +74 -0
- cida_plugin-1.0.0/cida_plugin/ttt.py +348 -0
- cida_plugin-1.0.0/cida_plugin/vision_backbone.py +43 -0
- cida_plugin-1.0.0/cida_plugin.egg-info/PKG-INFO +167 -0
- cida_plugin-1.0.0/cida_plugin.egg-info/SOURCES.txt +26 -0
- cida_plugin-1.0.0/cida_plugin.egg-info/dependency_links.txt +1 -0
- cida_plugin-1.0.0/cida_plugin.egg-info/requires.txt +11 -0
- cida_plugin-1.0.0/cida_plugin.egg-info/top_level.txt +1 -0
- cida_plugin-1.0.0/pyproject.toml +61 -0
- cida_plugin-1.0.0/setup.cfg +4 -0
- cida_plugin-1.0.0/tests/test_hf_wrapper.py +133 -0
- cida_plugin-1.0.0/tests/test_liquid_ode.py +168 -0
- cida_plugin-1.0.0/tests/test_plugin.py +25 -0
- cida_plugin-1.0.0/tests/test_role_priors.py +155 -0
- cida_plugin-1.0.0/tests/test_ttt.py +360 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cida-plugin
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Universal Evidence-Grounded Multi-Agent Deliberation Layer for any encoder
|
|
5
|
+
Author-email: Kairat Zhaksylykov <zhaksylykov.k06@gmail.com>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/Kairatzh/CIDA-plugin
|
|
8
|
+
Project-URL: Repository, https://github.com/Kairatzh/CIDA-plugin.git
|
|
9
|
+
Project-URL: Documentation, https://github.com/Kairatzh/CIDA-plugin#readme
|
|
10
|
+
Project-URL: Issues, https://github.com/Kairatzh/CIDA-plugin/issues
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
19
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: numpy>=1.24
|
|
24
|
+
Requires-Dist: torch>=2.1
|
|
25
|
+
Requires-Dist: transformers>=4.0
|
|
26
|
+
Requires-Dist: huggingface_hub>=0.14.0
|
|
27
|
+
Requires-Dist: torchdiffeq>=0.2.3
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest-cov>=5.0; extra == "dev"
|
|
31
|
+
Requires-Dist: ruff>=0.3.0; extra == "dev"
|
|
32
|
+
Requires-Dist: black>=24.0.0; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# CIDA-Plugin v3: Universal Evidence-Grounded Multi-Agent Deliberation Layer
|
|
36
|
+
|
|
37
|
+
> *"What if a neural network could argue with itself — and reach a better answer?"*
|
|
38
|
+
|
|
39
|
+
**CIDA-Plugin** is a drop-in architectural layer that can be added on top of **any** pre-trained Transformer encoder (BERT, DistilBERT, RoBERTa, etc.) or Vision Backbone (ResNet, DenseNet, etc.). Instead of a simple Linear Head, CIDA-Plugin introduces a **Multi-Agent Deliberation Protocol**.
|
|
40
|
+
|
|
41
|
+
It forces the model to form independent perspectives (agents), exchange arguments, and reach a consensus weighted by each agent's uncertainty.
|
|
42
|
+
|
|
43
|
+
**Result:** Massive reductions in Expected Calibration Error (ECE), robust uncertainty estimation, and better-reasoned predictions without relying on post-hoc calibration methods like Temperature Scaling.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## ⚡ What's New in v3 (Simplified Architecture)
|
|
48
|
+
We transitioned from a highly complex, statistical embedding-based formulation (v2) to a streamlined, theoretically grounded Bayesian-inspired architecture (v3):
|
|
49
|
+
|
|
50
|
+
* **Bayesian Role Priors**: Removed the hyperparameter-heavy `debate_loss`, role-specific serialization losses, and learnable `RoleEmbeddings`. Instead, agents are assigned fixed, mathematically guaranteed prior beliefs (Prosecutor, Defender, Skeptic, Integrator). This enforces structural disagreement at all times, preventing agent representation collapse.
|
|
51
|
+
* **Weighted Mean Consensus**: Replaced the Product of Experts (PoE) aggregator, which incorrectly assumed agent independence and amplified shared bias (causing overconfidence). We now aggregate beliefs via a Weighted Mean, which is mathematically sound for correlated variables.
|
|
52
|
+
* **Disagreement-as-Uncertainty**: Replaced the circular and unstable `ReliabilityTracker` with an observable, non-learned uncertainty quantification based on the variance (standard deviation) between expert beliefs: $U = f(\text{std}(b))$.
|
|
53
|
+
* **3-Component Loss System**: Reduced the loss system from 11 components to 3 core components: Task Loss (CE/BCE), Calibration Loss (Brier Score), and Anti-Collapse Loss (used only as a safety net).
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## ⚡ Comparison: Legacy vs. v3 Simplified
|
|
58
|
+
|
|
59
|
+
| Feature | Legacy CIDA (v2/Omega) | CIDA v3 (Simplified) |
|
|
60
|
+
| :--- | :--- | :--- |
|
|
61
|
+
| **Agent Diversity** | Additive embeddings + `debate_loss` | Fixed Role Priors + `anti_collapse_loss` |
|
|
62
|
+
| **Consensus Mech** | Product of Experts (PoE) | Weighted Mean (Correlation-Aware) |
|
|
63
|
+
| **Reliability/Uncertainty** | Learned EMA Reliability Tracker | Observed Disagreement ($std(b)$) |
|
|
64
|
+
| **Loss System** | 11 components (hard to tune) | 3 components (extremely stable) |
|
|
65
|
+
| **Hyperparameters** | 11 (lambda schedules, temp, etc.) | 2 (lambda_cal, lambda_ac) |
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## 📦 Installation
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install .
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## ⚡ Quickstart
|
|
78
|
+
|
|
79
|
+
CIDA-Plugin is designed to be as easy to use as a standard Hugging Face model.
|
|
80
|
+
|
|
81
|
+
### 1. Training with any Encoder
|
|
82
|
+
```python
|
|
83
|
+
import torch
|
|
84
|
+
from transformers import AutoModel
|
|
85
|
+
from cida_plugin import CIDAPlugin, CIDAPluginConfig, CIDALoss
|
|
86
|
+
|
|
87
|
+
# 1. Load any frozen encoder
|
|
88
|
+
encoder = AutoModel.from_pretrained("distilbert-base-uncased")
|
|
89
|
+
d_model = encoder.config.hidden_size
|
|
90
|
+
|
|
91
|
+
# 2. Initialize the plugin config
|
|
92
|
+
config = CIDAPluginConfig(
|
|
93
|
+
d_input=d_model, # Match encoder output dimension
|
|
94
|
+
d_hidden=128, # Internal plugin dimension
|
|
95
|
+
num_classes=2,
|
|
96
|
+
max_rounds=3, # Deliberation rounds
|
|
97
|
+
early_stop_threshold=0.90
|
|
98
|
+
)
|
|
99
|
+
plugin = CIDAPlugin(config)
|
|
100
|
+
loss_fn = CIDALoss(lambda_cal=0.4, lambda_ac=0.2)
|
|
101
|
+
|
|
102
|
+
# 3. Forward pass
|
|
103
|
+
input_ids = torch.randint(0, 1000, (4, 128))
|
|
104
|
+
out = encoder(input_ids)
|
|
105
|
+
pooled = out.last_hidden_state[:, 0, :]
|
|
106
|
+
|
|
107
|
+
# The plugin takes the pooled representation and deliberates
|
|
108
|
+
plugin_out = plugin(pooled, seq_output=out.last_hidden_state)
|
|
109
|
+
|
|
110
|
+
logits = plugin_out["p_final"] # (Batch, Num_Classes)
|
|
111
|
+
loss, loss_components = loss_fn(logits, targets, plugin_out["b_all"])
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 2. Saving and Loading (Hugging Face style)
|
|
115
|
+
```python
|
|
116
|
+
# Save to disk
|
|
117
|
+
plugin.save_pretrained("./my-cida-plugin")
|
|
118
|
+
|
|
119
|
+
# Load from disk
|
|
120
|
+
loaded_plugin = CIDAPlugin.from_pretrained("./my-cida-plugin")
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## 🛠️ Architecture Overview
|
|
126
|
+
|
|
127
|
+
The plugin takes the output of your encoder and processes it through the following steps:
|
|
128
|
+
|
|
129
|
+
1. **Input Projection:** Maps the arbitrary `d_input` of the encoder to the internal `d_hidden` of the agents.
|
|
130
|
+
2. **Agent Initialization:** Creates $M$ distinct agents initialized with the pooled representation.
|
|
131
|
+
3. **Deliberation Loop ($R$ rounds):**
|
|
132
|
+
- **Evidence Extraction:** Agents attend to the input sequence to gather distinct evidence.
|
|
133
|
+
- **Message Formulation:** Agents compress their beliefs and evidence into theses.
|
|
134
|
+
- **Cross-Attention Communication:** Agents listen to others, explicitly weighting disagreement.
|
|
135
|
+
- **Gated Update:** Agents update their internal states.
|
|
136
|
+
- **Role Prior Blending:** Enforces structural roles (Prosecutor, Defender, Skeptic, Integrator) on updated beliefs.
|
|
137
|
+
4. **Consensus Aggregation:** A final Weighted Mean consensus calculation.
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## 🧪 Liquid Dynamics & TTT (v5 Additions)
|
|
142
|
+
* **Liquid Neural ODE**: Set `use_liquid_dynamics=True` to replace the discrete iteration loop with continuous-time deliberation solver ($ds/dt = -s/\tau(x) + F(s,r,e)$).
|
|
143
|
+
* **Test-Time Training (TTT)**: Set `use_ttt=True` to allow agents to adapt their weights to a specific input using self-supervised masked state reconstruction steps before answering.
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## 🎮 Interactive Demo (Hugging Face Spaces)
|
|
148
|
+
|
|
149
|
+
See how 4 agents deliberate before answering — with agent vote charts and uncertainty gauges.
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
# Train demo checkpoints (~5 min on CPU)
|
|
153
|
+
python demo/train_demo.py
|
|
154
|
+
|
|
155
|
+
# Launch Gradio locally
|
|
156
|
+
python demo/app.py
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Deploy to [Hugging Face Spaces](https://huggingface.co/spaces): create a Gradio Space pointing to the `demo/` folder (see `demo/README.md`).
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## ⚡ Running Tests
|
|
164
|
+
To verify the installation and execution:
|
|
165
|
+
```bash
|
|
166
|
+
pytest tests/ -v
|
|
167
|
+
```
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# CIDA-Plugin v3: Universal Evidence-Grounded Multi-Agent Deliberation Layer
|
|
2
|
+
|
|
3
|
+
> *"What if a neural network could argue with itself — and reach a better answer?"*
|
|
4
|
+
|
|
5
|
+
**CIDA-Plugin** is a drop-in architectural layer that can be added on top of **any** pre-trained Transformer encoder (BERT, DistilBERT, RoBERTa, etc.) or Vision Backbone (ResNet, DenseNet, etc.). Instead of a simple Linear Head, CIDA-Plugin introduces a **Multi-Agent Deliberation Protocol**.
|
|
6
|
+
|
|
7
|
+
It forces the model to form independent perspectives (agents), exchange arguments, and reach a consensus weighted by each agent's uncertainty.
|
|
8
|
+
|
|
9
|
+
**Result:** Massive reductions in Expected Calibration Error (ECE), robust uncertainty estimation, and better-reasoned predictions without relying on post-hoc calibration methods like Temperature Scaling.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## ⚡ What's New in v3 (Simplified Architecture)
|
|
14
|
+
We transitioned from a highly complex, statistical embedding-based formulation (v2) to a streamlined, theoretically grounded Bayesian-inspired architecture (v3):
|
|
15
|
+
|
|
16
|
+
* **Bayesian Role Priors**: Removed the hyperparameter-heavy `debate_loss`, role-specific serialization losses, and learnable `RoleEmbeddings`. Instead, agents are assigned fixed, mathematically guaranteed prior beliefs (Prosecutor, Defender, Skeptic, Integrator). This enforces structural disagreement at all times, preventing agent representation collapse.
|
|
17
|
+
* **Weighted Mean Consensus**: Replaced the Product of Experts (PoE) aggregator, which incorrectly assumed agent independence and amplified shared bias (causing overconfidence). We now aggregate beliefs via a Weighted Mean, which is mathematically sound for correlated variables.
|
|
18
|
+
* **Disagreement-as-Uncertainty**: Replaced the circular and unstable `ReliabilityTracker` with an observable, non-learned uncertainty quantification based on the variance (standard deviation) between expert beliefs: $U = f(\text{std}(b))$.
|
|
19
|
+
* **3-Component Loss System**: Reduced the loss system from 11 components to 3 core components: Task Loss (CE/BCE), Calibration Loss (Brier Score), and Anti-Collapse Loss (used only as a safety net).
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## ⚡ Comparison: Legacy vs. v3 Simplified
|
|
24
|
+
|
|
25
|
+
| Feature | Legacy CIDA (v2/Omega) | CIDA v3 (Simplified) |
|
|
26
|
+
| :--- | :--- | :--- |
|
|
27
|
+
| **Agent Diversity** | Additive embeddings + `debate_loss` | Fixed Role Priors + `anti_collapse_loss` |
|
|
28
|
+
| **Consensus Mech** | Product of Experts (PoE) | Weighted Mean (Correlation-Aware) |
|
|
29
|
+
| **Reliability/Uncertainty** | Learned EMA Reliability Tracker | Observed Disagreement ($std(b)$) |
|
|
30
|
+
| **Loss System** | 11 components (hard to tune) | 3 components (extremely stable) |
|
|
31
|
+
| **Hyperparameters** | 11 (lambda schedules, temp, etc.) | 2 (lambda_cal, lambda_ac) |
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 📦 Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install .
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## ⚡ Quickstart
|
|
44
|
+
|
|
45
|
+
CIDA-Plugin is designed to be as easy to use as a standard Hugging Face model.
|
|
46
|
+
|
|
47
|
+
### 1. Training with any Encoder
|
|
48
|
+
```python
|
|
49
|
+
import torch
|
|
50
|
+
from transformers import AutoModel
|
|
51
|
+
from cida_plugin import CIDAPlugin, CIDAPluginConfig, CIDALoss
|
|
52
|
+
|
|
53
|
+
# 1. Load any frozen encoder
|
|
54
|
+
encoder = AutoModel.from_pretrained("distilbert-base-uncased")
|
|
55
|
+
d_model = encoder.config.hidden_size
|
|
56
|
+
|
|
57
|
+
# 2. Initialize the plugin config
|
|
58
|
+
config = CIDAPluginConfig(
|
|
59
|
+
d_input=d_model, # Match encoder output dimension
|
|
60
|
+
d_hidden=128, # Internal plugin dimension
|
|
61
|
+
num_classes=2,
|
|
62
|
+
max_rounds=3, # Deliberation rounds
|
|
63
|
+
early_stop_threshold=0.90
|
|
64
|
+
)
|
|
65
|
+
plugin = CIDAPlugin(config)
|
|
66
|
+
loss_fn = CIDALoss(lambda_cal=0.4, lambda_ac=0.2)
|
|
67
|
+
|
|
68
|
+
# 3. Forward pass
|
|
69
|
+
input_ids = torch.randint(0, 1000, (4, 128))
|
|
70
|
+
out = encoder(input_ids)
|
|
71
|
+
pooled = out.last_hidden_state[:, 0, :]
|
|
72
|
+
|
|
73
|
+
# The plugin takes the pooled representation and deliberates
|
|
74
|
+
plugin_out = plugin(pooled, seq_output=out.last_hidden_state)
|
|
75
|
+
|
|
76
|
+
logits = plugin_out["p_final"] # (Batch, Num_Classes)
|
|
77
|
+
loss, loss_components = loss_fn(logits, targets, plugin_out["b_all"])
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 2. Saving and Loading (Hugging Face style)
|
|
81
|
+
```python
|
|
82
|
+
# Save to disk
|
|
83
|
+
plugin.save_pretrained("./my-cida-plugin")
|
|
84
|
+
|
|
85
|
+
# Load from disk
|
|
86
|
+
loaded_plugin = CIDAPlugin.from_pretrained("./my-cida-plugin")
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## 🛠️ Architecture Overview
|
|
92
|
+
|
|
93
|
+
The plugin takes the output of your encoder and processes it through the following steps:
|
|
94
|
+
|
|
95
|
+
1. **Input Projection:** Maps the arbitrary `d_input` of the encoder to the internal `d_hidden` of the agents.
|
|
96
|
+
2. **Agent Initialization:** Creates $M$ distinct agents initialized with the pooled representation.
|
|
97
|
+
3. **Deliberation Loop ($R$ rounds):**
|
|
98
|
+
- **Evidence Extraction:** Agents attend to the input sequence to gather distinct evidence.
|
|
99
|
+
- **Message Formulation:** Agents compress their beliefs and evidence into theses.
|
|
100
|
+
- **Cross-Attention Communication:** Agents listen to others, explicitly weighting disagreement.
|
|
101
|
+
- **Gated Update:** Agents update their internal states.
|
|
102
|
+
- **Role Prior Blending:** Enforces structural roles (Prosecutor, Defender, Skeptic, Integrator) on updated beliefs.
|
|
103
|
+
4. **Consensus Aggregation:** A final Weighted Mean consensus calculation.
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## 🧪 Liquid Dynamics & TTT (v5 Additions)
|
|
108
|
+
* **Liquid Neural ODE**: Set `use_liquid_dynamics=True` to replace the discrete iteration loop with continuous-time deliberation solver ($ds/dt = -s/\tau(x) + F(s,r,e)$).
|
|
109
|
+
* **Test-Time Training (TTT)**: Set `use_ttt=True` to allow agents to adapt their weights to a specific input using self-supervised masked state reconstruction steps before answering.
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## 🎮 Interactive Demo (Hugging Face Spaces)
|
|
114
|
+
|
|
115
|
+
See how 4 agents deliberate before answering — with agent vote charts and uncertainty gauges.
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# Train demo checkpoints (~5 min on CPU)
|
|
119
|
+
python demo/train_demo.py
|
|
120
|
+
|
|
121
|
+
# Launch Gradio locally
|
|
122
|
+
python demo/app.py
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Deploy to [Hugging Face Spaces](https://huggingface.co/spaces): create a Gradio Space pointing to the `demo/` folder (see `demo/README.md`).
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## ⚡ Running Tests
|
|
130
|
+
To verify the installation and execution:
|
|
131
|
+
```bash
|
|
132
|
+
pytest tests/ -v
|
|
133
|
+
```
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CIDA-Plugin: Universal Evidence-Grounded Multi-Agent Deliberation Layer (v3).
|
|
3
|
+
|
|
4
|
+
Использование (минимальное):
|
|
5
|
+
from cida_plugin import CIDAPlugin, CIDAPluginConfig
|
|
6
|
+
|
|
7
|
+
cfg = CIDAPluginConfig(d_input=768, num_classes=2)
|
|
8
|
+
plugin = CIDAPlugin(cfg)
|
|
9
|
+
out = plugin(pooled_output) # pooled_output: (B, 768)
|
|
10
|
+
logits = out["p_final"] # (B, 2)
|
|
11
|
+
|
|
12
|
+
Использование (с seq_output для evidence pointers):
|
|
13
|
+
out = plugin(pooled_output, seq_output=hidden_states, mask=attention_mask)
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from .config import CIDAPluginConfig
|
|
17
|
+
from .core import CIDAPlugin
|
|
18
|
+
from .agent import (
|
|
19
|
+
AgentState,
|
|
20
|
+
apply_role_prior,
|
|
21
|
+
compute_role_orthogonality_loss,
|
|
22
|
+
RoleNames,
|
|
23
|
+
)
|
|
24
|
+
from .deliberation import (
|
|
25
|
+
AgentEvidenceExtractor,
|
|
26
|
+
MessageFormulator,
|
|
27
|
+
CounterargumentCommunication,
|
|
28
|
+
AgentUpdater,
|
|
29
|
+
)
|
|
30
|
+
from .consensus import ConsensusAggregator, HaltingPredictor
|
|
31
|
+
from .losses import CIDALoss
|
|
32
|
+
from .diagnostics import DebateDiagnostics
|
|
33
|
+
from .ttt import TestTimeTrainer
|
|
34
|
+
from .liquid_dynamics import LiquidDeliberationSolver
|
|
35
|
+
from .hf import wrap_hf_model, HFModelWithCIDA
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
# Главный интерфейс
|
|
39
|
+
"CIDAPlugin",
|
|
40
|
+
"CIDAPluginConfig",
|
|
41
|
+
"wrap_hf_model",
|
|
42
|
+
"HFModelWithCIDA",
|
|
43
|
+
# Потери и диагностика
|
|
44
|
+
"CIDALoss",
|
|
45
|
+
"DebateDiagnostics",
|
|
46
|
+
# [v5] Test-Time Training
|
|
47
|
+
"TestTimeTrainer",
|
|
48
|
+
# [v5] Liquid Neural ODE Dynamics
|
|
49
|
+
"LiquidDeliberationSolver",
|
|
50
|
+
# Компоненты (для кастомных архитектур)
|
|
51
|
+
"AgentState",
|
|
52
|
+
"apply_role_prior",
|
|
53
|
+
"compute_role_orthogonality_loss",
|
|
54
|
+
"RoleNames",
|
|
55
|
+
"AgentEvidenceExtractor",
|
|
56
|
+
"MessageFormulator",
|
|
57
|
+
"CounterargumentCommunication",
|
|
58
|
+
"AgentUpdater",
|
|
59
|
+
"ConsensusAggregator",
|
|
60
|
+
"HaltingPredictor",
|
|
61
|
+
]
|
|
62
|
+
|