onejev 0.1.1__py3-none-any.whl
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.
- onejev-0.1.1.dist-info/METADATA +199 -0
- onejev-0.1.1.dist-info/RECORD +20 -0
- onejev-0.1.1.dist-info/WHEEL +5 -0
- onejev-0.1.1.dist-info/entry_points.txt +2 -0
- onejev-0.1.1.dist-info/licenses/LICENSE +202 -0
- onejev-0.1.1.dist-info/top_level.txt +1 -0
- qev/__init__.py +4 -0
- qev/answers.py +74 -0
- qev/calibrate.py +114 -0
- qev/cli.py +209 -0
- qev/client.py +114 -0
- qev/engine.py +443 -0
- qev/fp8.py +134 -0
- qev/llamacpp.py +291 -0
- qev/media.py +157 -0
- qev/mm_engine.py +765 -0
- qev/playground/index.html +91 -0
- qev/prompt.py +157 -0
- qev/schema.py +118 -0
- qev/server.py +94 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: onejev
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Open System One decision model: typed, calibrated decisions behind a TypeSafe-compatible API
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: transformers>=5.0
|
|
10
|
+
Requires-Dist: pillow>=10
|
|
11
|
+
Requires-Dist: jinja2>=3.1
|
|
12
|
+
Requires-Dist: pydantic>=2.0
|
|
13
|
+
Requires-Dist: fastapi>=0.110
|
|
14
|
+
Requires-Dist: uvicorn>=0.30
|
|
15
|
+
Provides-Extra: torch
|
|
16
|
+
Requires-Dist: torch>=2.6; extra == "torch"
|
|
17
|
+
Requires-Dist: torchvision>=0.21; extra == "torch"
|
|
18
|
+
Requires-Dist: accelerate>=1.0; extra == "torch"
|
|
19
|
+
Requires-Dist: safetensors>=0.4; extra == "torch"
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
22
|
+
Requires-Dist: httpx>=0.27; extra == "dev"
|
|
23
|
+
Requires-Dist: typesafe-sdk>=0.6; extra == "dev"
|
|
24
|
+
Provides-Extra: train
|
|
25
|
+
Requires-Dist: torch>=2.6; extra == "train"
|
|
26
|
+
Requires-Dist: torchvision>=0.21; extra == "train"
|
|
27
|
+
Requires-Dist: accelerate>=1.0; extra == "train"
|
|
28
|
+
Requires-Dist: safetensors>=0.4; extra == "train"
|
|
29
|
+
Requires-Dist: peft>=0.13; extra == "train"
|
|
30
|
+
Requires-Dist: pyyaml>=6; extra == "train"
|
|
31
|
+
Requires-Dist: pyarrow>=14; extra == "train"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
<p align="center">
|
|
35
|
+
<picture>
|
|
36
|
+
<source media="(prefers-color-scheme: dark)" srcset="assets/banner-dark.svg">
|
|
37
|
+
<img alt="OneJev, a Multimodal System One Decision Model" src="assets/banner.svg" width="100%">
|
|
38
|
+
</picture>
|
|
39
|
+
</p>
|
|
40
|
+
|
|
41
|
+
<p align="center"><b>English</b> · <a href="assets/README_zh.md">简体中文</a> · <a href="assets/README_ja.md">日本語</a></p>
|
|
42
|
+
|
|
43
|
+
<p align="center">
|
|
44
|
+
<a href="https://huggingface.co/collections/OmniJev/onejev"><img alt="Hugging Face" src="https://img.shields.io/badge/Hugging_Face-OneJev-FFD21E?style=flat-square&logo=huggingface&logoColor=black"></a>
|
|
45
|
+
<a href="https://huggingface.co/spaces/BradNLP/OneJev"><img alt="Demo" src="https://img.shields.io/badge/Demo-Try_it-D45BB6?style=flat-square&logo=gradio&logoColor=white"></a>
|
|
46
|
+
<a href="https://github.com/OmniJev/OneJev"><img alt="GitHub" src="https://img.shields.io/badge/GitHub-OmniJev%2FOneJev-181717?style=flat-square&logo=github&logoColor=white"></a>
|
|
47
|
+
<a href="https://omnijev.github.io/OneJev/"><img alt="Website" src="https://img.shields.io/badge/Website-OneJev-0A84FF?style=flat-square&logo=googlechrome&logoColor=white"></a>
|
|
48
|
+
<a href="LICENSE"><img alt="License" src="https://img.shields.io/badge/License-Apache_2.0-D22128?style=flat-square&logo=apache&logoColor=white"></a>
|
|
49
|
+
</p>
|
|
50
|
+
<p align="center">
|
|
51
|
+
<a href="https://huggingface.co/datasets/OmniJev/OneJev-Data"><img alt="Data" src="https://img.shields.io/badge/Data-OneJev--Data-FF9D00?style=flat-square&logo=huggingface&logoColor=white"></a>
|
|
52
|
+
<a href="examples/request.json"><img alt="API" src="https://img.shields.io/badge/API-System_One-009688?style=flat-square&logo=fastapi&logoColor=white"></a>
|
|
53
|
+
<a href="https://www.python.org"><img alt="Python" src="https://img.shields.io/badge/Python-3.10%2B-3776AB?style=flat-square&logo=python&logoColor=white"></a>
|
|
54
|
+
<a href="https://pytorch.org"><img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-2.6%2B-EE4C2C?style=flat-square&logo=pytorch&logoColor=white"></a>
|
|
55
|
+
<a href="https://github.com/huggingface/transformers"><img alt="Transformers" src="https://img.shields.io/badge/Transformers-5%2B-FFD21E?style=flat-square&logo=huggingface&logoColor=black"></a>
|
|
56
|
+
</p>
|
|
57
|
+
|
|
58
|
+
OneJev is a multimodal System One decision model. It returns calibrated probabilities for typed questions about
|
|
59
|
+
screenshots, photos, videos and text in a single forward pass. Available in 0.8B, 4B, 9B and 27B.
|
|
60
|
+
|
|
61
|
+
## Results
|
|
62
|
+
|
|
63
|
+
<p align="center">
|
|
64
|
+
<picture>
|
|
65
|
+
<source media="(prefers-color-scheme: dark)" srcset="assets/results-dark.svg">
|
|
66
|
+
<img alt="Block bar charts: the four OneJev sizes against Jev 1.13, Jev-Omni 12B and Qwen3.8-27B thinking on the OneJev test set, DecisionBench hard, TypeSafe and MMStar" src="assets/results.svg" width="100%">
|
|
67
|
+
</picture>
|
|
68
|
+
</p>
|
|
69
|
+
|
|
70
|
+
<p align="center">
|
|
71
|
+
<picture>
|
|
72
|
+
<source media="(prefers-color-scheme: dark)" srcset="assets/table-dark.svg">
|
|
73
|
+
<img alt="Results table, best score in each row in magenta, second best in light pink" src="assets/table.svg" width="100%">
|
|
74
|
+
</picture>
|
|
75
|
+
</p>
|
|
76
|
+
|
|
77
|
+
Accuracy (%). The OneJev test set is held out from OneJev training. Jev 1.13 uses published text-only scores;
|
|
78
|
+
Jev-Omni and Qwen3.8-27B thinking were evaluated by us.
|
|
79
|
+
|
|
80
|
+
<p align="center">
|
|
81
|
+
<picture>
|
|
82
|
+
<source media="(prefers-color-scheme: dark)" srcset="assets/speed-dark.svg">
|
|
83
|
+
<img alt="Latency on one H200 for one 1280x720 screenshot: OneJev-0.8B 31 ms for 1 question, 51 ms for 10, 5.1 ms per question; OneJev-4B 64 ms for 1 question, 104 ms for 10, 10.4 ms per question; OneJev-9B 81 ms for 1 question, 131 ms for 10, 13.1 ms per question; OneJev-27B 189 ms for 1 question, 324 ms for 10, 32.4 ms per question" src="assets/speed.svg" width="100%">
|
|
84
|
+
</picture>
|
|
85
|
+
</p>
|
|
86
|
+
|
|
87
|
+
## Quick start
|
|
88
|
+
|
|
89
|
+
Choose one backend, then run the Python example below.
|
|
90
|
+
|
|
91
|
+
### Option A: PyTorch
|
|
92
|
+
|
|
93
|
+
For NVIDIA GPUs. Supports text, images and video.
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
pip install "qev[torch] @ git+https://github.com/OmniJev/OneJev.git"
|
|
97
|
+
qev serve --model OmniJev/OneJev-4B --port 8000
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Option B: llama.cpp
|
|
101
|
+
|
|
102
|
+
For GGUF models. Supports text and images; use PyTorch for video. Install
|
|
103
|
+
[llama.cpp](https://github.com/ggml-org/llama.cpp/blob/master/docs/install.md) first (`brew install llama.cpp` on macOS).
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
pip install git+https://github.com/OmniJev/OneJev.git
|
|
107
|
+
qev serve --gguf mradermacher/OneJev-4B-GGUF:Q8_0 --port 8000
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Send a request
|
|
111
|
+
|
|
112
|
+
Both backends serve the same API at `http://localhost:8000`. In another terminal, run this example with your own
|
|
113
|
+
`screenshot.png`:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from qev import Client, Choice, Noul, Score
|
|
117
|
+
from qev.media import data_uri
|
|
118
|
+
|
|
119
|
+
r = Client("http://localhost:8000").system_one(
|
|
120
|
+
state={"task": "Pay the open invoice from ACME", "screen": "<image:1>"},
|
|
121
|
+
media=[{"type": "image", "data": data_uri("screenshot.png")}],
|
|
122
|
+
questions={"done": Noul("The invoice has been paid"),
|
|
123
|
+
"next": Choice("What should the agent do next?",
|
|
124
|
+
{"click": "click an element", "type": "type text", "scroll": "scroll", "stop": "stop"}),
|
|
125
|
+
"progress": Score("How far along is the task?", ["not started", "halfway", "almost done", "done"])},
|
|
126
|
+
)
|
|
127
|
+
r.answers["done"].noul # probability of yes
|
|
128
|
+
r.answers["next"].probabilities # one probability per option
|
|
129
|
+
r.answers["progress"].score # expected level
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The API is compatible with TypeSafe System One. More examples: [video](examples/video.py),
|
|
133
|
+
[curl](examples/curl.sh), [official TypeSafe SDK](examples/official_sdk.py).
|
|
134
|
+
|
|
135
|
+
## Training
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
git clone https://github.com/OmniJev/OneJev.git
|
|
139
|
+
cd OneJev
|
|
140
|
+
pip install -e ".[train]"
|
|
141
|
+
|
|
142
|
+
# Choose A or B. A is enabled below.
|
|
143
|
+
# A. Demo: 100 examples with images (23 MB)
|
|
144
|
+
hf download OmniJev/OneJev-Data sample/sample-100.parquet --repo-type dataset --local-dir data/onejev
|
|
145
|
+
python -m train.unpack data/onejev --sample
|
|
146
|
+
|
|
147
|
+
# B. Full dataset: 94,707 examples (17.7 GB). Uncomment these two lines instead of A.
|
|
148
|
+
# hf download OmniJev/OneJev-Data --repo-type dataset --include "data/*.parquet" --local-dir data/onejev
|
|
149
|
+
# python -m train.unpack data/onejev
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Either option creates `data/onejev/train.jsonl` and extracts images and video frames into `data/onejev/media/`.
|
|
153
|
+
|
|
154
|
+
[OneJev-Data](https://huggingface.co/datasets/OmniJev/OneJev-Data) releases 94,707 of the original 99,193
|
|
155
|
+
training questions; the remaining sources do not permit redistribution.
|
|
156
|
+
|
|
157
|
+
### Fine-tune
|
|
158
|
+
|
|
159
|
+
The four models fine-tune Qwen3.5-0.8B, Qwen3.5-4B, Qwen3.5-9B and Qwen3.8-27B for one epoch with the vision tower
|
|
160
|
+
frozen. The configs use a learning rate of `5e-6`, a 16,384-token limit, and cross-entropy plus Brier loss over answer
|
|
161
|
+
probabilities. To train the 4B model on four GPUs:
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
torchrun --nproc-per-node 4 -m train.sft --config train/configs/onejev_4b_full.yaml
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Configs: [0.8B](train/configs/onejev_08b_full.yaml) · [4B](train/configs/onejev_4b_full.yaml) ·
|
|
168
|
+
[9B](train/configs/onejev_9b_full.yaml) · [27B](train/configs/onejev_27b_full.yaml).
|
|
169
|
+
Set `--nproc-per-node` to your GPU count; the 9B and 27B configs enable FSDP to shard model and optimizer state.
|
|
170
|
+
For your own data, change `train` and `media_root` in the config.
|
|
171
|
+
|
|
172
|
+
The final 4B checkpoint is saved to `train/runs/onejev_4b/final`. Start it with:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
qev serve --model train/runs/onejev_4b/final --port 8000
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Citation
|
|
179
|
+
|
|
180
|
+
```bibtex
|
|
181
|
+
@misc{onejev2026,
|
|
182
|
+
title = {{OneJev}: A Multimodal System One Decision Model},
|
|
183
|
+
author = {{OmniJev Team}},
|
|
184
|
+
year = {2026},
|
|
185
|
+
howpublished = {\url{https://github.com/OmniJev/OneJev}}
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Friendly Links
|
|
190
|
+
|
|
191
|
+
- [LINUX DO](https://linux.do)
|
|
192
|
+
- [Jev](https://typesafe.ai)
|
|
193
|
+
- [Awesome JEV](https://github.com/OmniJev/awesome-jev-gallery)
|
|
194
|
+
- [Awesome JEV Website](https://omnijev.github.io/awesome-jev-gallery/)
|
|
195
|
+
- [PlayJev](https://github.com/OmniJev/PlayJev)
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
199
|
+
[Apache 2.0](LICENSE)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
onejev-0.1.1.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
2
|
+
qev/__init__.py,sha256=bpWa-RBdgR_BEz727x0GV1w2botOMfHqWUlWjKMkNtA,145
|
|
3
|
+
qev/answers.py,sha256=CbBtuiWMgKapr9qlqs_Q91I_nSoIentkAsoDVT6m-6Y,2542
|
|
4
|
+
qev/calibrate.py,sha256=P6YBOoHH55pwKvzcwOvQib7nBHWQwYzERwiFD1rtoe4,4245
|
|
5
|
+
qev/cli.py,sha256=Iy963SM978vcqwUuqqaR3QPxwvDhqpV7JW1qurg4Avs,10152
|
|
6
|
+
qev/client.py,sha256=VgygD37P_ucuZJCv3WmjTPT1Wm4XZaBJPUvuv4trQng,4417
|
|
7
|
+
qev/engine.py,sha256=-WZcaHFIGNvqjtwegNCw2Gc3U33WBv9Tz_N6r7hNa_Q,22720
|
|
8
|
+
qev/fp8.py,sha256=5mZjUUcSdHnbuB9FxGmhOwkHif855gpyh38rbQ4njek,5996
|
|
9
|
+
qev/llamacpp.py,sha256=eYK4B_HO_WI00mDn6urlv91egEQXJR8DJa35AeFDidk,14574
|
|
10
|
+
qev/media.py,sha256=H27HGwOkL39IRhJgWA2KrMH6b_ZKPGhMsjux1dE6qMA,6676
|
|
11
|
+
qev/mm_engine.py,sha256=KwOykO6TKMql4jvqjZK229j_9hFSMFu-DkRa1NHnoTI,38656
|
|
12
|
+
qev/prompt.py,sha256=F9-uCeTf9Hy4zoDhcuuhFvGIuCtwwnd70U_ZuKcWgN4,6421
|
|
13
|
+
qev/schema.py,sha256=vOS301OsEVqeUK7yhJdd3u2aO7RS4_2SilVmaHBbwvM,3050
|
|
14
|
+
qev/server.py,sha256=Ud2q_rxncoauv_Nw7gdAuu-fkCd4syu42hsGH_qWTdI,3954
|
|
15
|
+
qev/playground/index.html,sha256=dV8iRSoS5WzjOvC8KEF1OcwG9oOHN6RpAqGy7W-5xGE,6483
|
|
16
|
+
onejev-0.1.1.dist-info/METADATA,sha256=S5YLIB9p0S8hkw4F9wm1hOw13tIOeVY9c3VkMKlrbUk,8941
|
|
17
|
+
onejev-0.1.1.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
18
|
+
onejev-0.1.1.dist-info/entry_points.txt,sha256=nu0tCD0-9sRclFCluddGkyPu1kZzYo5e1VGvRP-nqEg,37
|
|
19
|
+
onejev-0.1.1.dist-info/top_level.txt,sha256=VwtV-FGA2ITJe4PdPSpCWK5c6h6d6VYl1DnfVrH2XTE,4
|
|
20
|
+
onejev-0.1.1.dist-info/RECORD,,
|
|
@@ -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.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
qev
|
qev/__init__.py
ADDED
qev/answers.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Answer assembly: probabilities to typed answers, with TypeSafe's confidence formulas.
|
|
2
|
+
|
|
3
|
+
The two confidence functions are ported from typesafe-ai/system-one-adapter-python
|
|
4
|
+
(MIT License, Copyright (c) 2026 TypeSafe AI) and verified against Jev's published answers
|
|
5
|
+
(mean absolute error 0.013 for Choice, 0.005 for Score, within two-decimal rounding).
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Any, Sequence
|
|
10
|
+
|
|
11
|
+
from .schema import ChoiceAnswer, NoulAnswer, ScoreAnswer
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _normalize(probs: Sequence[float]) -> list[float]:
|
|
15
|
+
total = float(sum(probs))
|
|
16
|
+
if total <= 0:
|
|
17
|
+
return [1.0 / len(probs)] * len(probs)
|
|
18
|
+
return [float(p) / total for p in probs]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def choice_confidence(probs: Sequence[float]) -> float:
|
|
22
|
+
"""Peak probability rescaled from uniform (0) to certainty (1)."""
|
|
23
|
+
if len(probs) == 1:
|
|
24
|
+
return 1.0
|
|
25
|
+
p = _normalize(probs)
|
|
26
|
+
uniform = 1.0 / len(p)
|
|
27
|
+
return (max(p) - uniform) / (1.0 - uniform)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def score_confidence(probs: Sequence[float]) -> float:
|
|
31
|
+
"""One minus the probability-weighted mean absolute deviation from the modal level,
|
|
32
|
+
normalised by the mean absolute deviation of the uniform distribution over the levels."""
|
|
33
|
+
if len(probs) == 1:
|
|
34
|
+
return 1.0
|
|
35
|
+
p = _normalize(probs)
|
|
36
|
+
k = len(p)
|
|
37
|
+
mode = max(range(k), key=p.__getitem__)
|
|
38
|
+
distance = sum(pi * abs(i - mode) for i, pi in enumerate(p))
|
|
39
|
+
center = (k - 1) / 2
|
|
40
|
+
uniform_mad = sum(abs(i - center) for i in range(k)) / k
|
|
41
|
+
return max(0.0, 1.0 - distance / uniform_mad)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def expected_score(probs: Sequence[float]) -> float:
|
|
45
|
+
p = _normalize(probs)
|
|
46
|
+
return float(sum(i * pi for i, pi in enumerate(p)))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _round(x: float, nd: int = 6) -> float:
|
|
50
|
+
return float(round(x, nd))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def make_noul(p_yes: float) -> NoulAnswer:
|
|
54
|
+
return NoulAnswer(noul=_round(min(1.0, max(0.0, float(p_yes)))))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def make_choice(options: Sequence[str], probs: Sequence[float]) -> ChoiceAnswer:
|
|
58
|
+
p = _normalize(probs)
|
|
59
|
+
best = max(range(len(p)), key=p.__getitem__)
|
|
60
|
+
return ChoiceAnswer(
|
|
61
|
+
choice=options[best],
|
|
62
|
+
probabilities={o: _round(pi) for o, pi in zip(options, p)},
|
|
63
|
+
confidence=_round(choice_confidence(p)),
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def make_score(levels: Sequence[Any], probs: Sequence[float]) -> ScoreAnswer:
|
|
68
|
+
p = _normalize(probs)
|
|
69
|
+
return ScoreAnswer(
|
|
70
|
+
score=_round(expected_score(p)),
|
|
71
|
+
legend={str(i): lvl for i, lvl in enumerate(levels)},
|
|
72
|
+
probabilities={str(i): _round(pi) for i, pi in enumerate(p)},
|
|
73
|
+
confidence=_round(score_confidence(p)),
|
|
74
|
+
)
|
qev/calibrate.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Calibration: temperatures per (primitive, option count), fitting, and probability metrics."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import logging
|
|
6
|
+
import math
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Sequence
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class Calibration:
|
|
14
|
+
"""Temperature table. Keys are "kind" or "kind:K". Missing keys fall back to the primitive-level
|
|
15
|
+
temperature, then to 1.0."""
|
|
16
|
+
|
|
17
|
+
temperatures: dict[str, float] = field(default_factory=dict)
|
|
18
|
+
source: str = "none"
|
|
19
|
+
|
|
20
|
+
def temperature(self, kind: str, k: int) -> float:
|
|
21
|
+
return self.temperatures.get(f"{kind}:{k}", self.temperatures.get(kind, 1.0))
|
|
22
|
+
|
|
23
|
+
@classmethod
|
|
24
|
+
def load(cls, path: str | Path) -> "Calibration":
|
|
25
|
+
data = json.loads(Path(path).read_text())
|
|
26
|
+
return cls(temperatures={k: float(v) for k, v in data.get("temperatures", {}).items()}, source=str(path))
|
|
27
|
+
|
|
28
|
+
def save(self, path: str | Path) -> None:
|
|
29
|
+
from .prompt import PROMPT_VERSION
|
|
30
|
+
|
|
31
|
+
Path(path).write_text(json.dumps({"temperatures": self.temperatures, "source": self.source,
|
|
32
|
+
"prompt_version": PROMPT_VERSION}, indent=2))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def softmax(logits: Sequence[float], temperature: float = 1.0) -> list[float]:
|
|
36
|
+
z = [float(x) / temperature for x in logits]
|
|
37
|
+
m = max(z)
|
|
38
|
+
w = [math.exp(x - m) for x in z]
|
|
39
|
+
s = sum(w)
|
|
40
|
+
return [x / s for x in w]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def fit_temperature(logits: Sequence[Sequence[float]], targets: Sequence[Sequence[float]],
|
|
44
|
+
lo: float = 0.05, hi: float = 50.0, iters: int = 60) -> float:
|
|
45
|
+
"""Fit one scalar temperature by minimising cross-entropy against (possibly soft) targets.
|
|
46
|
+
Golden-section search on log T; no torch dependency so it runs anywhere."""
|
|
47
|
+
def nll(t: float) -> float:
|
|
48
|
+
total = 0.0
|
|
49
|
+
for z, y in zip(logits, targets):
|
|
50
|
+
p = softmax(z, t)
|
|
51
|
+
total -= sum(yi * math.log(max(pi, 1e-12)) for yi, pi in zip(y, p))
|
|
52
|
+
return total / max(len(logits), 1)
|
|
53
|
+
|
|
54
|
+
a, b = math.log(lo), math.log(hi)
|
|
55
|
+
gr = (math.sqrt(5) - 1) / 2
|
|
56
|
+
c, d = b - gr * (b - a), a + gr * (b - a)
|
|
57
|
+
fc, fd = nll(math.exp(c)), nll(math.exp(d))
|
|
58
|
+
for _ in range(iters):
|
|
59
|
+
if fc < fd:
|
|
60
|
+
b, d, fd = d, c, fc
|
|
61
|
+
c = b - gr * (b - a)
|
|
62
|
+
fc = nll(math.exp(c))
|
|
63
|
+
else:
|
|
64
|
+
a, c, fc = c, d, fd
|
|
65
|
+
d = a + gr * (b - a)
|
|
66
|
+
fd = nll(math.exp(d))
|
|
67
|
+
t = math.exp((a + b) / 2)
|
|
68
|
+
if t >= hi * 0.99 or t <= lo * 1.01:
|
|
69
|
+
logging.getLogger("qev.calibrate").warning(
|
|
70
|
+
"temperature fit hit the %s bound (T=%.4f) on %d rows; the slot logits likely carry no signal for this group",
|
|
71
|
+
"upper" if t >= hi * 0.99 else "lower", t, len(logits))
|
|
72
|
+
return t
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def ece(confidences: Sequence[float], correct: Sequence[float], n_bins: int = 15) -> float:
|
|
76
|
+
n = len(confidences)
|
|
77
|
+
if n == 0:
|
|
78
|
+
return 0.0
|
|
79
|
+
total = 0.0
|
|
80
|
+
for b in range(n_bins):
|
|
81
|
+
lo, hi = b / n_bins, (b + 1) / n_bins
|
|
82
|
+
idx = [i for i, c in enumerate(confidences) if (lo < c <= hi) or (b == 0 and c <= lo)]
|
|
83
|
+
if idx:
|
|
84
|
+
avg_conf = sum(confidences[i] for i in idx) / len(idx)
|
|
85
|
+
avg_acc = sum(correct[i] for i in idx) / len(idx)
|
|
86
|
+
total += len(idx) / n * abs(avg_conf - avg_acc)
|
|
87
|
+
return total
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def adaptive_ece(confidences: Sequence[float], correct: Sequence[float], n_bins: int = 15) -> float:
|
|
91
|
+
n = len(confidences)
|
|
92
|
+
if n == 0:
|
|
93
|
+
return 0.0
|
|
94
|
+
order = sorted(range(n), key=lambda i: confidences[i])
|
|
95
|
+
total = 0.0
|
|
96
|
+
size = max(1, n // n_bins)
|
|
97
|
+
for start in range(0, n, size):
|
|
98
|
+
idx = order[start:start + size]
|
|
99
|
+
avg_conf = sum(confidences[i] for i in idx) / len(idx)
|
|
100
|
+
avg_acc = sum(correct[i] for i in idx) / len(idx)
|
|
101
|
+
total += len(idx) / n * abs(avg_conf - avg_acc)
|
|
102
|
+
return total
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def brier(probs: Sequence[float], target: Sequence[float]) -> float:
|
|
106
|
+
return sum((p - t) ** 2 for p, t in zip(probs, target))
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def nll(probs: Sequence[float], target: Sequence[float]) -> float:
|
|
110
|
+
return -sum(t * math.log(max(p, 1e-12)) for p, t in zip(probs, target))
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def tvd(p: Sequence[float], q: Sequence[float]) -> float:
|
|
114
|
+
return 0.5 * sum(abs(a - b) for a, b in zip(p, q))
|