brier 0.1.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.
Files changed (64) hide show
  1. brier-0.1.0/.gitignore +24 -0
  2. brier-0.1.0/CHANGELOG.md +49 -0
  3. brier-0.1.0/LICENSE +202 -0
  4. brier-0.1.0/NOTICE +4 -0
  5. brier-0.1.0/PKG-INFO +211 -0
  6. brier-0.1.0/README.md +174 -0
  7. brier-0.1.0/pyproject.toml +101 -0
  8. brier-0.1.0/src/brier/__init__.py +8 -0
  9. brier-0.1.0/src/brier/__main__.py +73 -0
  10. brier-0.1.0/src/brier/_math.py +57 -0
  11. brier-0.1.0/src/brier/_version.py +8 -0
  12. brier-0.1.0/src/brier/artifacts.py +519 -0
  13. brier-0.1.0/src/brier/backends/__init__.py +1 -0
  14. brier-0.1.0/src/brier/backends/base.py +50 -0
  15. brier-0.1.0/src/brier/backends/fake.py +125 -0
  16. brier-0.1.0/src/brier/backends/hf.py +264 -0
  17. brier-0.1.0/src/brier/bench/__init__.py +1 -0
  18. brier-0.1.0/src/brier/bench/__main__.py +104 -0
  19. brier-0.1.0/src/brier/bench/features.py +97 -0
  20. brier-0.1.0/src/brier/bench/run.py +335 -0
  21. brier-0.1.0/src/brier/bench/tasks.py +166 -0
  22. brier-0.1.0/src/brier/calibrate/__init__.py +1 -0
  23. brier-0.1.0/src/brier/calibrate/temperature.py +160 -0
  24. brier-0.1.0/src/brier/check.py +287 -0
  25. brier-0.1.0/src/brier/debias.py +113 -0
  26. brier-0.1.0/src/brier/decider.py +441 -0
  27. brier-0.1.0/src/brier/decision.py +97 -0
  28. brier-0.1.0/src/brier/errors.py +29 -0
  29. brier-0.1.0/src/brier/heads/__init__.py +1 -0
  30. brier-0.1.0/src/brier/heads/_features.py +59 -0
  31. brier-0.1.0/src/brier/heads/fitted.py +130 -0
  32. brier-0.1.0/src/brier/heads/lda.py +105 -0
  33. brier-0.1.0/src/brier/heads/ridge.py +89 -0
  34. brier-0.1.0/src/brier/heads/select.py +215 -0
  35. brier-0.1.0/src/brier/metrics.py +244 -0
  36. brier-0.1.0/src/brier/prompts.py +134 -0
  37. brier-0.1.0/src/brier/py.typed +0 -0
  38. brier-0.1.0/src/brier/questions.py +125 -0
  39. brier-0.1.0/src/brier/readout.py +55 -0
  40. brier-0.1.0/tests/integration/README.md +2 -0
  41. brier-0.1.0/tests/integration/test_hf_backend.py +376 -0
  42. brier-0.1.0/tests/unit/test_artifacts.py +394 -0
  43. brier-0.1.0/tests/unit/test_artifacts_hostile.py +338 -0
  44. brier-0.1.0/tests/unit/test_bench.py +286 -0
  45. brier-0.1.0/tests/unit/test_bench_l2.py +176 -0
  46. brier-0.1.0/tests/unit/test_check.py +318 -0
  47. brier-0.1.0/tests/unit/test_decider.py +187 -0
  48. brier-0.1.0/tests/unit/test_decider_l1.py +145 -0
  49. brier-0.1.0/tests/unit/test_decider_l2.py +412 -0
  50. brier-0.1.0/tests/unit/test_decider_save_load.py +126 -0
  51. brier-0.1.0/tests/unit/test_decision.py +96 -0
  52. brier-0.1.0/tests/unit/test_fake_backend.py +142 -0
  53. brier-0.1.0/tests/unit/test_heads.py +232 -0
  54. brier-0.1.0/tests/unit/test_hf_lazy_import.py +13 -0
  55. brier-0.1.0/tests/unit/test_hf_template.py +83 -0
  56. brier-0.1.0/tests/unit/test_math.py +86 -0
  57. brier-0.1.0/tests/unit/test_metrics.py +223 -0
  58. brier-0.1.0/tests/unit/test_package.py +6 -0
  59. brier-0.1.0/tests/unit/test_prompts.py +151 -0
  60. brier-0.1.0/tests/unit/test_questions.py +149 -0
  61. brier-0.1.0/tests/unit/test_readme.py +35 -0
  62. brier-0.1.0/tests/unit/test_readout_debias.py +217 -0
  63. brier-0.1.0/tests/unit/test_select.py +372 -0
  64. brier-0.1.0/tests/unit/test_temperature.py +131 -0
brier-0.1.0/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ build/
5
+ dist/
6
+ *.egg-info/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ .coverage
11
+ coverage.xml
12
+ htmlcov/
13
+ .hypothesis/
14
+ # local config and secrets
15
+ .env
16
+ .env.*
17
+ .claude/settings.local.json
18
+ # benchmark outputs, caches, model files
19
+ results/
20
+ cache/
21
+ *.safetensors
22
+ *.bin
23
+ *.pt
24
+ site/
@@ -0,0 +1,49 @@
1
+ # Changelog
2
+
3
+ All notable changes are documented here. Format: [Keep a Changelog](https://keepachangelog.com/en/1.1.0/);
4
+ versioning: [Semantic Versioning](https://semver.org/).
5
+
6
+ ## [Unreleased]
7
+
8
+ ## [0.1.0] - 2026-10-03
9
+
10
+ First public release.
11
+
12
+ ### Added
13
+ - **Typed questions** `Choice`, `Noul` (yes/no) and `Score`, answered as probability
14
+ distributions read from next-token log-probabilities (no generation, no fine-tuning). Every
15
+ `Decision` records the level that produced it.
16
+ - **Correction levels** in `Decider` (`decide`, `decide_batch`):
17
+ - `raw`: label-token readout (Score falls back to lettered options when digits are not single
18
+ tokens);
19
+ - `L0`, no labels: cyclic option rotations with a geometric-mean combine, plus an optional
20
+ batch prior from unlabelled states (`fit_prior`; ADR-0005);
21
+ - `L1`: temperature scaling (`fit_temperature`, at least 50 labels);
22
+ - `L2`: hidden-state heads (`fit_head`, at least 60 labels and 5 per answer): ridge and
23
+ Ledoit–Wolf shrinkage LDA, layer / solver / alpha chosen by stratified 5-fold out-of-fold
24
+ NLL; L2 temperature with Platt (1999) targets for two classes and plain temperature
25
+ scaling beyond (METHODS.md).
26
+ - **Calibration artifacts**: `Decider.save` / `Decider.load` (`artifact.json` + `arrays.npz`,
27
+ never model weights), loaded as untrusted input (SHA-256, size caps, no pickle) and bound to
28
+ the model id, revision, dtype and prompt templates (ADR-0003, 0006, 0007).
29
+ - **`HFBackend`** for Hugging Face causal LMs: safetensors only, `trust_remote_code` never set,
30
+ special-token-safe prompts, batched readout and hidden states. Chat models are prompted through
31
+ their chat template, base models through a plain-text prompt (ADR-0008).
32
+ - **`brier check <model-id>`**: a conformance report before calibrating (label tokens, batching,
33
+ hidden states, a 14-item sanity task, supported levels); `--json`.
34
+ - **Benchmark** `python -m brier.bench run` (banking20 from BANKING77, CC-BY-4.0, pinned and
35
+ SHA-256 verified): all levels, bootstrap and paired CIs, flip rate, L2 label curves, per-item
36
+ outputs without state text. Results in `docs/results.md`.
37
+ - **Model compatibility**: integration suite and `brier check` pass on 15 models from Qwen3,
38
+ SmolLM2/3, TinyLlama, OLMo-2, Gemma 3, Granite-MoE, LFM2, DeepSeek-R1-Distill, Falcon3,
39
+ Phi-4-mini, OLMoE and Mistral, including two mixtures of experts and two base models
40
+ (`docs/COMPATIBILITY.md`).
41
+ - Documentation: README quickstart (run in CI), API reference on GitHub Pages, SPEC, METHODS,
42
+ ARCHITECTURE, EVALUATION, THREAT_MODEL, related work and eight ADRs.
43
+
44
+ ### Security
45
+ - GitHub Actions and pre-commit hooks pinned to full commit SHAs; CodeQL, `pip-audit` and
46
+ Dependabot in CI.
47
+
48
+ [Unreleased]: https://github.com/mohdUwaish59/brier/compare/v0.1.0...HEAD
49
+ [0.1.0]: https://github.com/mohdUwaish59/brier/releases/tag/v0.1.0
brier-0.1.0/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
brier-0.1.0/NOTICE ADDED
@@ -0,0 +1,4 @@
1
+ brier
2
+ Copyright 2026 Mohd Uwaish
3
+
4
+ This product includes software developed as part of the brier project.
brier-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,211 @@
1
+ Metadata-Version: 2.5
2
+ Name: brier
3
+ Version: 0.1.0
4
+ Summary: Calibrated typed decisions (choice / yes-no / score) from open LLMs, without generation or fine-tuning.
5
+ Project-URL: Homepage, https://github.com/mohdUwaish59/brier
6
+ Project-URL: Documentation, https://mohduwaish59.github.io/brier/
7
+ Project-URL: Issues, https://github.com/mohdUwaish59/brier/issues
8
+ Project-URL: Changelog, https://github.com/mohdUwaish59/brier/blob/main/CHANGELOG.md
9
+ Author: Mohd Uwaish
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ License-File: NOTICE
13
+ Keywords: calibration,classification,decision,jev,llm,logits,probability
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Intended Audience :: Science/Research
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3 :: Only
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Programming Language :: Python :: 3.13
24
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
25
+ Classifier: Typing :: Typed
26
+ Requires-Python: >=3.10
27
+ Requires-Dist: numpy>=1.24
28
+ Provides-Extra: bench
29
+ Requires-Dist: safetensors>=0.4; extra == 'bench'
30
+ Requires-Dist: torch>=2.2; extra == 'bench'
31
+ Requires-Dist: transformers>=5.0; extra == 'bench'
32
+ Provides-Extra: hf
33
+ Requires-Dist: safetensors>=0.4; extra == 'hf'
34
+ Requires-Dist: torch>=2.2; extra == 'hf'
35
+ Requires-Dist: transformers>=5.0; extra == 'hf'
36
+ Description-Content-Type: text/markdown
37
+
38
+ # brier
39
+
40
+ [![CI](https://github.com/mohdUwaish59/brier/actions/workflows/ci.yml/badge.svg)](https://github.com/mohdUwaish59/brier/actions/workflows/ci.yml)
41
+ [![PyPI](https://img.shields.io/pypi/v/brier)](https://pypi.org/project/brier/)
42
+ [![Python](https://img.shields.io/pypi/pyversions/brier)](https://pypi.org/project/brier/)
43
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](https://github.com/mohdUwaish59/brier/blob/main/LICENSE)
44
+ [![Docs](https://img.shields.io/badge/docs-API%20reference-informational)](https://mohduwaish59.github.io/brier/)
45
+
46
+ > Turn an open LLM into a calibrated decision model: choice, yes/no and score questions
47
+ > answered with real probabilities, read from the model in a forward pass, without generation
48
+ > or fine-tuning.
49
+
50
+ **Status: alpha (0.1).** The API may still change before 1.0. See the
51
+ [roadmap](https://github.com/mohdUwaish59/brier/blob/main/docs/ROADMAP.md).
52
+
53
+ **Try it:** [tutorial notebook](https://colab.research.google.com/github/mohdUwaish59/brier/blob/main/notebooks/tutorial_ticket_router.ipynb):
54
+ a calibrated ticket router from an open model in about 15 minutes, on a free Colab GPU.
55
+
56
+ ## Why
57
+
58
+ Reading an LLM's answer from generated text is slow and needs parsing. Raw next-token
59
+ probabilities skip that, but they are biased by option order and badly calibrated. brier reads
60
+ the answer distribution directly and corrects it in levels. Every decision records the level
61
+ that produced it.
62
+
63
+ | Level | Labels needed | What it fixes | Forward passes |
64
+ |---|---|---|---|
65
+ | `raw` | 0 | baseline readout of the label tokens | 1 |
66
+ | `L0` | 0 (optional unlabelled pool) | option-order bias, label prior bias | K for a K-option Choice, else 1 |
67
+ | `L1` | ≥ 50 | over/under-confidence (one temperature) | as L0 |
68
+ | `L2` | ≥ 60, ≥ 5 per answer | accuracy and calibration via a hidden-state head | 1 |
69
+
70
+ ## Results
71
+
72
+ banking20 (20 BANKING77 intents), Qwen3-1.7B, 2,603 test items, 95 % bootstrap CIs.
73
+ L1 and L2 use 300 labels. Full tables, protocol and caveats:
74
+ [`docs/results.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/results.md).
75
+
76
+ | | raw | L0 | L1 | L2 |
77
+ |---|---|---|---|---|
78
+ | Accuracy ↑ | 0.626 | 0.668 | 0.668 | **0.807** [0.793, 0.823] |
79
+ | ECE ↓ | 0.357 | 0.299 | 0.094 | **0.035** [0.028, 0.050] |
80
+ | NLL ↓ | 6.541 | 3.845 | 1.204 | **0.727** [0.672, 0.781] |
81
+
82
+ Across six model families (same task and labels; point estimates):
83
+
84
+ | Model | Accuracy: raw → L0 → L2 | ECE: raw → L1 → L2 | Order flips: raw → L0 |
85
+ |---|---|---|---|
86
+ | Qwen3-1.7B | 0.626 → 0.668 → **0.807** | 0.357 → 0.094 → **0.035** | 0.364 → 0.201 |
87
+ | Falcon3-1B-Base (no chat template) | 0.155 → 0.601 → **0.804** | 0.119 → 0.162 → **0.072** | 0.990 → 0.371 |
88
+ | LFM2-1.2B (hybrid convolution) | 0.200 → 0.617 → **0.802** | 0.336 → 0.064 → **0.036** | 0.972 → 0.415 |
89
+ | SmolLM3-3B | 0.660 → 0.746 → **0.834** | 0.190 → **0.030** → 0.039 | 0.279 → 0.123 |
90
+ | Phi-4-mini (3.8B) | 0.711 → 0.786 → **0.859** | 0.186 → **0.022** → 0.057 | 0.275 → 0.099 |
91
+ | OLMoE-1B-7B (mixture of experts) | 0.257 → 0.686 → **0.819** | 0.068 → 0.098 → **0.084** | 0.945 → 0.316 |
92
+
93
+ Zero labels (L0) raise accuracy and cut order flips on every model; with 300 labels (L2) every
94
+ model lands at 0.80–0.86 accuracy. One task so far: evidence that the method works, not a
95
+ general claim. Details and caveats in
96
+ [`docs/results.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/results.md).
97
+
98
+ ## Install
99
+
100
+ ```bash
101
+ pip install "brier[hf]"
102
+ ```
103
+
104
+ Python ≥ 3.10. The core needs only numpy; the `hf` extra adds torch and transformers.
105
+
106
+ ## Quickstart
107
+
108
+ ```python
109
+ from brier import Choice, Decider, Noul
110
+ from brier.backends.hf import HFBackend
111
+
112
+ d = Decider(HFBackend("Qwen/Qwen3-1.7B", revision="<commit-sha>"))
113
+ route = Choice("Which team?", ["billing", "technical", "sales"], name="route")
114
+ refund = Noul("Is this a refund request?", name="refund")
115
+
116
+ res = d.decide("My card was charged twice, please fix it now!", [route, refund], level="L0")
117
+ print(res["route"].answer, res["route"].probs) # top option and all option probabilities
118
+ print(res["refund"].p_yes, res["refund"].level) # probability of yes, and "L0"
119
+ ```
120
+
121
+ Calibrate with your own data, save the calibration, and reuse it:
122
+
123
+ ```python
124
+ d.fit_prior(unlabelled_states, [route]) # L0 prior, no labels
125
+ d.fit_temperature(labelled_states, route, route_labels) # L1, >= 50 labels
126
+ d.fit_head(labelled_states, route, route_labels) # L2, >= 60 labels, >= 5 per answer
127
+ d.decide(state, [route], level="L2")
128
+
129
+ d.save("calibration/") # JSON + .npz, never model weights
130
+ d = Decider.load("calibration/", HFBackend("Qwen/Qwen3-1.7B", revision="<commit-sha>"))
131
+ ```
132
+
133
+ `load` refuses a calibration made for another model, revision, precision (`dtype`) or prompt
134
+ template. Pin `revision` to a commit SHA: a model name alone does not pin weights.
135
+
136
+ Questions:
137
+ - **`Choice(text, options, name=...)`**: one of K options.
138
+ - **`Noul(text, name=...)`**: yes/no; `p_yes` is the probability of yes.
139
+ - **`Score(text, levels, name=...)`**: an integer rating `1..levels`; `expected` is the mean.
140
+
141
+ ## Does it work with my model?
142
+
143
+ Hugging Face causal LMs with safetensors weights: chat models through their chat template, base
144
+ models through a plain-text prompt. Check yours before calibrating:
145
+
146
+ ```text
147
+ $ brier check Qwen/Qwen3-0.6B --revision c1899de289a04d12100db370d81485cdf75e47ca
148
+
149
+ PASS revision c1899de289a04d12100db370d81485cdf75e47ca
150
+ PASS prompt_format the model's chat template
151
+ PASS labels.choice A-Z: single, distinct tokens
152
+ PASS labels.noul Yes, No: single, distinct tokens
153
+ PASS labels.score 10 levels as lettered options (digit labels are not single tokens)
154
+ PASS batch_consistency max |p_batched - p_single| = 5.3e-06 (limit 2e-02)
155
+ PASS hidden_states 7 candidate layers (12-24 of 28), d = 1024
156
+ PASS sanity accuracy raw 79% / L0 79% on 14 items, order flips raw 50% / L0 17%, ...
157
+
158
+ Supported levels: raw, L0, L1, L2
159
+ ```
160
+
161
+ Tested so far (15 models): Qwen3, SmolLM2 / SmolLM3, TinyLlama, OLMo-2, Gemma 3, Granite-MoE,
162
+ LFM2, DeepSeek-R1-Distill, Falcon3, Phi-4-mini, OLMoE and Mistral, including two mixtures of
163
+ experts and two base models. Details and known quirks:
164
+ [`docs/COMPATIBILITY.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/COMPATIBILITY.md).
165
+
166
+ ## Documentation
167
+
168
+ - [API reference](https://mohduwaish59.github.io/brier/)
169
+ - [`SPEC.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/SPEC.md): behaviour and limits
170
+ - [`METHODS.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/METHODS.md): the maths of every level, with references
171
+ - [`EVALUATION.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/EVALUATION.md) and
172
+ [`results.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/results.md): benchmark protocol and results
173
+ - [`related_work.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/related_work.md): where the idea and the methods come from
174
+
175
+ ## Limitations
176
+
177
+ - One benchmark task so far (banking20); more tasks are welcome.
178
+ - `L0` costs K forward passes for a K-option question; there is no prefix caching yet.
179
+ - Hugging Face `transformers` only (no vLLM backend yet); models need safetensors weights and
180
+ the standard decoder layout.
181
+ - Decisions on untrusted text can be steered by prompt injection (see Security).
182
+
183
+ ## Contributing
184
+
185
+ Contributions are welcome: new models in the compatibility table (run `brier check` and open a
186
+ PR), benchmark tasks, backends and docs. Start with
187
+ [`CONTRIBUTING.md`](https://github.com/mohdUwaish59/brier/blob/main/CONTRIBUTING.md) and the
188
+ issues labelled *good first issue*.
189
+
190
+ ## Security
191
+
192
+ Decisions on untrusted text can be manipulated by prompt injection. Don't use them as the only
193
+ gate for destructive actions. brier never runs model code (`trust_remote_code` is never set),
194
+ loads weights from safetensors only, and validates calibration files as untrusted input. Report
195
+ vulnerabilities privately; see
196
+ [SECURITY.md](https://github.com/mohdUwaish59/brier/blob/main/SECURITY.md) and
197
+ [`THREAT_MODEL.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/THREAT_MODEL.md).
198
+
199
+ ## Related work
200
+
201
+ brier follows the idea of TypeSafe AI's Jev and of
202
+ [AnyJev](https://github.com/nokia-applied-research/AnyJev) (Nokia Applied Research): typed
203
+ questions answered from next-token probabilities, corrected in levels. It is an independent
204
+ implementation, not affiliated with either. The correction methods come from published work
205
+ (Zheng et al. 2024; Zhou et al. 2024; Guo et al. 2017; Platt 1999; Ledoit & Wolf 2004).
206
+ [`related_work.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/related_work.md) sets
207
+ out what brier shares with these projects and where it differs.
208
+
209
+ ## License
210
+
211
+ Apache-2.0
brier-0.1.0/README.md ADDED
@@ -0,0 +1,174 @@
1
+ # brier
2
+
3
+ [![CI](https://github.com/mohdUwaish59/brier/actions/workflows/ci.yml/badge.svg)](https://github.com/mohdUwaish59/brier/actions/workflows/ci.yml)
4
+ [![PyPI](https://img.shields.io/pypi/v/brier)](https://pypi.org/project/brier/)
5
+ [![Python](https://img.shields.io/pypi/pyversions/brier)](https://pypi.org/project/brier/)
6
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](https://github.com/mohdUwaish59/brier/blob/main/LICENSE)
7
+ [![Docs](https://img.shields.io/badge/docs-API%20reference-informational)](https://mohduwaish59.github.io/brier/)
8
+
9
+ > Turn an open LLM into a calibrated decision model: choice, yes/no and score questions
10
+ > answered with real probabilities, read from the model in a forward pass, without generation
11
+ > or fine-tuning.
12
+
13
+ **Status: alpha (0.1).** The API may still change before 1.0. See the
14
+ [roadmap](https://github.com/mohdUwaish59/brier/blob/main/docs/ROADMAP.md).
15
+
16
+ **Try it:** [tutorial notebook](https://colab.research.google.com/github/mohdUwaish59/brier/blob/main/notebooks/tutorial_ticket_router.ipynb):
17
+ a calibrated ticket router from an open model in about 15 minutes, on a free Colab GPU.
18
+
19
+ ## Why
20
+
21
+ Reading an LLM's answer from generated text is slow and needs parsing. Raw next-token
22
+ probabilities skip that, but they are biased by option order and badly calibrated. brier reads
23
+ the answer distribution directly and corrects it in levels. Every decision records the level
24
+ that produced it.
25
+
26
+ | Level | Labels needed | What it fixes | Forward passes |
27
+ |---|---|---|---|
28
+ | `raw` | 0 | baseline readout of the label tokens | 1 |
29
+ | `L0` | 0 (optional unlabelled pool) | option-order bias, label prior bias | K for a K-option Choice, else 1 |
30
+ | `L1` | ≥ 50 | over/under-confidence (one temperature) | as L0 |
31
+ | `L2` | ≥ 60, ≥ 5 per answer | accuracy and calibration via a hidden-state head | 1 |
32
+
33
+ ## Results
34
+
35
+ banking20 (20 BANKING77 intents), Qwen3-1.7B, 2,603 test items, 95 % bootstrap CIs.
36
+ L1 and L2 use 300 labels. Full tables, protocol and caveats:
37
+ [`docs/results.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/results.md).
38
+
39
+ | | raw | L0 | L1 | L2 |
40
+ |---|---|---|---|---|
41
+ | Accuracy ↑ | 0.626 | 0.668 | 0.668 | **0.807** [0.793, 0.823] |
42
+ | ECE ↓ | 0.357 | 0.299 | 0.094 | **0.035** [0.028, 0.050] |
43
+ | NLL ↓ | 6.541 | 3.845 | 1.204 | **0.727** [0.672, 0.781] |
44
+
45
+ Across six model families (same task and labels; point estimates):
46
+
47
+ | Model | Accuracy: raw → L0 → L2 | ECE: raw → L1 → L2 | Order flips: raw → L0 |
48
+ |---|---|---|---|
49
+ | Qwen3-1.7B | 0.626 → 0.668 → **0.807** | 0.357 → 0.094 → **0.035** | 0.364 → 0.201 |
50
+ | Falcon3-1B-Base (no chat template) | 0.155 → 0.601 → **0.804** | 0.119 → 0.162 → **0.072** | 0.990 → 0.371 |
51
+ | LFM2-1.2B (hybrid convolution) | 0.200 → 0.617 → **0.802** | 0.336 → 0.064 → **0.036** | 0.972 → 0.415 |
52
+ | SmolLM3-3B | 0.660 → 0.746 → **0.834** | 0.190 → **0.030** → 0.039 | 0.279 → 0.123 |
53
+ | Phi-4-mini (3.8B) | 0.711 → 0.786 → **0.859** | 0.186 → **0.022** → 0.057 | 0.275 → 0.099 |
54
+ | OLMoE-1B-7B (mixture of experts) | 0.257 → 0.686 → **0.819** | 0.068 → 0.098 → **0.084** | 0.945 → 0.316 |
55
+
56
+ Zero labels (L0) raise accuracy and cut order flips on every model; with 300 labels (L2) every
57
+ model lands at 0.80–0.86 accuracy. One task so far: evidence that the method works, not a
58
+ general claim. Details and caveats in
59
+ [`docs/results.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/results.md).
60
+
61
+ ## Install
62
+
63
+ ```bash
64
+ pip install "brier[hf]"
65
+ ```
66
+
67
+ Python ≥ 3.10. The core needs only numpy; the `hf` extra adds torch and transformers.
68
+
69
+ ## Quickstart
70
+
71
+ ```python
72
+ from brier import Choice, Decider, Noul
73
+ from brier.backends.hf import HFBackend
74
+
75
+ d = Decider(HFBackend("Qwen/Qwen3-1.7B", revision="<commit-sha>"))
76
+ route = Choice("Which team?", ["billing", "technical", "sales"], name="route")
77
+ refund = Noul("Is this a refund request?", name="refund")
78
+
79
+ res = d.decide("My card was charged twice, please fix it now!", [route, refund], level="L0")
80
+ print(res["route"].answer, res["route"].probs) # top option and all option probabilities
81
+ print(res["refund"].p_yes, res["refund"].level) # probability of yes, and "L0"
82
+ ```
83
+
84
+ Calibrate with your own data, save the calibration, and reuse it:
85
+
86
+ ```python
87
+ d.fit_prior(unlabelled_states, [route]) # L0 prior, no labels
88
+ d.fit_temperature(labelled_states, route, route_labels) # L1, >= 50 labels
89
+ d.fit_head(labelled_states, route, route_labels) # L2, >= 60 labels, >= 5 per answer
90
+ d.decide(state, [route], level="L2")
91
+
92
+ d.save("calibration/") # JSON + .npz, never model weights
93
+ d = Decider.load("calibration/", HFBackend("Qwen/Qwen3-1.7B", revision="<commit-sha>"))
94
+ ```
95
+
96
+ `load` refuses a calibration made for another model, revision, precision (`dtype`) or prompt
97
+ template. Pin `revision` to a commit SHA: a model name alone does not pin weights.
98
+
99
+ Questions:
100
+ - **`Choice(text, options, name=...)`**: one of K options.
101
+ - **`Noul(text, name=...)`**: yes/no; `p_yes` is the probability of yes.
102
+ - **`Score(text, levels, name=...)`**: an integer rating `1..levels`; `expected` is the mean.
103
+
104
+ ## Does it work with my model?
105
+
106
+ Hugging Face causal LMs with safetensors weights: chat models through their chat template, base
107
+ models through a plain-text prompt. Check yours before calibrating:
108
+
109
+ ```text
110
+ $ brier check Qwen/Qwen3-0.6B --revision c1899de289a04d12100db370d81485cdf75e47ca
111
+
112
+ PASS revision c1899de289a04d12100db370d81485cdf75e47ca
113
+ PASS prompt_format the model's chat template
114
+ PASS labels.choice A-Z: single, distinct tokens
115
+ PASS labels.noul Yes, No: single, distinct tokens
116
+ PASS labels.score 10 levels as lettered options (digit labels are not single tokens)
117
+ PASS batch_consistency max |p_batched - p_single| = 5.3e-06 (limit 2e-02)
118
+ PASS hidden_states 7 candidate layers (12-24 of 28), d = 1024
119
+ PASS sanity accuracy raw 79% / L0 79% on 14 items, order flips raw 50% / L0 17%, ...
120
+
121
+ Supported levels: raw, L0, L1, L2
122
+ ```
123
+
124
+ Tested so far (15 models): Qwen3, SmolLM2 / SmolLM3, TinyLlama, OLMo-2, Gemma 3, Granite-MoE,
125
+ LFM2, DeepSeek-R1-Distill, Falcon3, Phi-4-mini, OLMoE and Mistral, including two mixtures of
126
+ experts and two base models. Details and known quirks:
127
+ [`docs/COMPATIBILITY.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/COMPATIBILITY.md).
128
+
129
+ ## Documentation
130
+
131
+ - [API reference](https://mohduwaish59.github.io/brier/)
132
+ - [`SPEC.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/SPEC.md): behaviour and limits
133
+ - [`METHODS.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/METHODS.md): the maths of every level, with references
134
+ - [`EVALUATION.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/EVALUATION.md) and
135
+ [`results.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/results.md): benchmark protocol and results
136
+ - [`related_work.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/related_work.md): where the idea and the methods come from
137
+
138
+ ## Limitations
139
+
140
+ - One benchmark task so far (banking20); more tasks are welcome.
141
+ - `L0` costs K forward passes for a K-option question; there is no prefix caching yet.
142
+ - Hugging Face `transformers` only (no vLLM backend yet); models need safetensors weights and
143
+ the standard decoder layout.
144
+ - Decisions on untrusted text can be steered by prompt injection (see Security).
145
+
146
+ ## Contributing
147
+
148
+ Contributions are welcome: new models in the compatibility table (run `brier check` and open a
149
+ PR), benchmark tasks, backends and docs. Start with
150
+ [`CONTRIBUTING.md`](https://github.com/mohdUwaish59/brier/blob/main/CONTRIBUTING.md) and the
151
+ issues labelled *good first issue*.
152
+
153
+ ## Security
154
+
155
+ Decisions on untrusted text can be manipulated by prompt injection. Don't use them as the only
156
+ gate for destructive actions. brier never runs model code (`trust_remote_code` is never set),
157
+ loads weights from safetensors only, and validates calibration files as untrusted input. Report
158
+ vulnerabilities privately; see
159
+ [SECURITY.md](https://github.com/mohdUwaish59/brier/blob/main/SECURITY.md) and
160
+ [`THREAT_MODEL.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/THREAT_MODEL.md).
161
+
162
+ ## Related work
163
+
164
+ brier follows the idea of TypeSafe AI's Jev and of
165
+ [AnyJev](https://github.com/nokia-applied-research/AnyJev) (Nokia Applied Research): typed
166
+ questions answered from next-token probabilities, corrected in levels. It is an independent
167
+ implementation, not affiliated with either. The correction methods come from published work
168
+ (Zheng et al. 2024; Zhou et al. 2024; Guo et al. 2017; Platt 1999; Ledoit & Wolf 2004).
169
+ [`related_work.md`](https://github.com/mohdUwaish59/brier/blob/main/docs/related_work.md) sets
170
+ out what brier shares with these projects and where it differs.
171
+
172
+ ## License
173
+
174
+ Apache-2.0