cotlab 0.8.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.
- cotlab-0.8.0/.env.example +1 -0
- cotlab-0.8.0/.github/workflows/docs.yml +42 -0
- cotlab-0.8.0/.github/workflows/lint.yml +35 -0
- cotlab-0.8.0/.github/workflows/tests.yml +364 -0
- cotlab-0.8.0/.gitignore +64 -0
- cotlab-0.8.0/.pre-commit-config.yaml +18 -0
- cotlab-0.8.0/CLAUDE.md +75 -0
- cotlab-0.8.0/Dockerfile.rocm +18 -0
- cotlab-0.8.0/LICENSE +21 -0
- cotlab-0.8.0/PKG-INFO +166 -0
- cotlab-0.8.0/README.md +117 -0
- cotlab-0.8.0/conf/backend/transformers.yaml +14 -0
- cotlab-0.8.0/conf/backend/vllm.yaml +11 -0
- cotlab-0.8.0/conf/config.yaml +21 -0
- cotlab-0.8.0/conf/dataset/afrimedqa.yaml +6 -0
- cotlab-0.8.0/conf/dataset/cardiology.yaml +5 -0
- cotlab-0.8.0/conf/dataset/histopathology.yaml +8 -0
- cotlab-0.8.0/conf/dataset/m_arc.yaml +6 -0
- cotlab-0.8.0/conf/dataset/medbullets.yaml +6 -0
- cotlab-0.8.0/conf/dataset/medmcqa.yaml +7 -0
- cotlab-0.8.0/conf/dataset/medqa.yaml +6 -0
- cotlab-0.8.0/conf/dataset/medxpertqa.yaml +6 -0
- cotlab-0.8.0/conf/dataset/mmlu_medical.yaml +5 -0
- cotlab-0.8.0/conf/dataset/movie_ood.yaml +7 -0
- cotlab-0.8.0/conf/dataset/neurology.yaml +5 -0
- cotlab-0.8.0/conf/dataset/oncology.yaml +5 -0
- cotlab-0.8.0/conf/dataset/patching_pairs.yaml +5 -0
- cotlab-0.8.0/conf/dataset/pediatrics.yaml +7 -0
- cotlab-0.8.0/conf/dataset/plab.yaml +5 -0
- cotlab-0.8.0/conf/dataset/probing_diagnosis.yaml +6 -0
- cotlab-0.8.0/conf/dataset/pubhealthbench.yaml +6 -0
- cotlab-0.8.0/conf/dataset/pubmedqa.yaml +5 -0
- cotlab-0.8.0/conf/dataset/radiology.yaml +5 -0
- cotlab-0.8.0/conf/dataset/synthetic.yaml +6 -0
- cotlab-0.8.0/conf/dataset/tcga.yaml +7 -0
- cotlab-0.8.0/conf/experiment/activation_compare.yaml +14 -0
- cotlab-0.8.0/conf/experiment/activation_patching.yaml +21 -0
- cotlab-0.8.0/conf/experiment/attention_analysis.yaml +28 -0
- cotlab-0.8.0/conf/experiment/classification.yaml +6 -0
- cotlab-0.8.0/conf/experiment/composite_shift_detector.yaml +20 -0
- cotlab-0.8.0/conf/experiment/cot_ablation.yaml +8 -0
- cotlab-0.8.0/conf/experiment/cot_faithfulness.yaml +16 -0
- cotlab-0.8.0/conf/experiment/cot_heads.yaml +7 -0
- cotlab-0.8.0/conf/experiment/full_layer_cot.yaml +7 -0
- cotlab-0.8.0/conf/experiment/full_layer_patching.yaml +8 -0
- cotlab-0.8.0/conf/experiment/h_neuron_analysis.yaml +19 -0
- cotlab-0.8.0/conf/experiment/logit_lens.yaml +24 -0
- cotlab-0.8.0/conf/experiment/multi_head_cot.yaml +8 -0
- cotlab-0.8.0/conf/experiment/multi_head_patching.yaml +8 -0
- cotlab-0.8.0/conf/experiment/probing_classifier.yaml +13 -0
- cotlab-0.8.0/conf/experiment/residual_norm_ood.yaml +17 -0
- cotlab-0.8.0/conf/experiment/sae_feature_analysis.yaml +25 -0
- cotlab-0.8.0/conf/experiment/steering_vectors.yaml +9 -0
- cotlab-0.8.0/conf/experiment/sycophancy_heads.yaml +8 -0
- cotlab-0.8.0/conf/experiment/token_group_contrast.yaml +22 -0
- cotlab-0.8.0/conf/prompt/adversarial.yaml +6 -0
- cotlab-0.8.0/conf/prompt/arrogance.yaml +9 -0
- cotlab-0.8.0/conf/prompt/cardiology.yaml +8 -0
- cotlab-0.8.0/conf/prompt/chain_of_thought.yaml +10 -0
- cotlab-0.8.0/conf/prompt/contrarian.yaml +5 -0
- cotlab-0.8.0/conf/prompt/contrarian_matched.yaml +5 -0
- cotlab-0.8.0/conf/prompt/cot_matched.yaml +5 -0
- cotlab-0.8.0/conf/prompt/direct_answer.yaml +10 -0
- cotlab-0.8.0/conf/prompt/direct_matched.yaml +5 -0
- cotlab-0.8.0/conf/prompt/expert_persona.yaml +6 -0
- cotlab-0.8.0/conf/prompt/few_shot.yaml +6 -0
- cotlab-0.8.0/conf/prompt/histopathology.yaml +10 -0
- cotlab-0.8.0/conf/prompt/mcq.yaml +8 -0
- cotlab-0.8.0/conf/prompt/neurology.yaml +8 -0
- cotlab-0.8.0/conf/prompt/no_instruction.yaml +5 -0
- cotlab-0.8.0/conf/prompt/oncology.yaml +8 -0
- cotlab-0.8.0/conf/prompt/plab.yaml +8 -0
- cotlab-0.8.0/conf/prompt/pubhealthbench.yaml +6 -0
- cotlab-0.8.0/conf/prompt/pubmedqa.yaml +8 -0
- cotlab-0.8.0/conf/prompt/radiology.yaml +8 -0
- cotlab-0.8.0/conf/prompt/simple.yaml +7 -0
- cotlab-0.8.0/conf/prompt/socratic.yaml +5 -0
- cotlab-0.8.0/conf/prompt/sycophantic.yaml +6 -0
- cotlab-0.8.0/conf/prompt/tcga.yaml +7 -0
- cotlab-0.8.0/conf/prompt/uncertainty.yaml +8 -0
- cotlab-0.8.0/data/datasets.yaml +2 -0
- cotlab-0.8.0/data/movie_ood.jsonl +399 -0
- cotlab-0.8.0/docker-compose.rocm.yml +32 -0
- cotlab-0.8.0/docs/api/core.md +17 -0
- cotlab-0.8.0/docs/api/experiments.md +12 -0
- cotlab-0.8.0/docs/api/prompts.md +23 -0
- cotlab-0.8.0/docs/getting-started/installation.md +119 -0
- cotlab-0.8.0/docs/getting-started/quickstart.md +65 -0
- cotlab-0.8.0/docs/guide/configuration.md +61 -0
- cotlab-0.8.0/docs/guide/experiments.md +31 -0
- cotlab-0.8.0/docs/guide/models.md +71 -0
- cotlab-0.8.0/docs/guide/prompts.md +29 -0
- cotlab-0.8.0/docs/index.md +62 -0
- cotlab-0.8.0/docs/rocm-setup.md +67 -0
- cotlab-0.8.0/mkdocs.yml +61 -0
- cotlab-0.8.0/notebooks/cotlab_tutorial.ipynb +424 -0
- cotlab-0.8.0/pyproject.toml +96 -0
- cotlab-0.8.0/scripts/analyze_json_compliance.py +231 -0
- cotlab-0.8.0/scripts/build_movie_ood_dataset.py +373 -0
- cotlab-0.8.0/scripts/compare_json_vs_freetext.py +245 -0
- cotlab-0.8.0/scripts/cotlab-rocm.sh +55 -0
- cotlab-0.8.0/src/cotlab/__init__.py +3 -0
- cotlab-0.8.0/src/cotlab/analyse_experiments.py +392 -0
- cotlab-0.8.0/src/cotlab/analysis/__init__.py +11 -0
- cotlab-0.8.0/src/cotlab/analysis/cot_parser.py +243 -0
- cotlab-0.8.0/src/cotlab/analysis/faithfulness_metrics.py +192 -0
- cotlab-0.8.0/src/cotlab/backends/__init__.py +16 -0
- cotlab-0.8.0/src/cotlab/backends/base.py +78 -0
- cotlab-0.8.0/src/cotlab/backends/transformers_backend.py +335 -0
- cotlab-0.8.0/src/cotlab/backends/vllm_backend.py +227 -0
- cotlab-0.8.0/src/cotlab/cli.py +83 -0
- cotlab-0.8.0/src/cotlab/core/__init__.py +34 -0
- cotlab-0.8.0/src/cotlab/core/base.py +749 -0
- cotlab-0.8.0/src/cotlab/core/config.py +90 -0
- cotlab-0.8.0/src/cotlab/core/registry.py +68 -0
- cotlab-0.8.0/src/cotlab/datasets/__init__.py +45 -0
- cotlab-0.8.0/src/cotlab/datasets/loaders.py +1889 -0
- cotlab-0.8.0/src/cotlab/experiment/__init__.py +315 -0
- cotlab-0.8.0/src/cotlab/experiments/__init__.py +43 -0
- cotlab-0.8.0/src/cotlab/experiments/activation_compare.py +290 -0
- cotlab-0.8.0/src/cotlab/experiments/activation_patching.py +1050 -0
- cotlab-0.8.0/src/cotlab/experiments/attention_analysis.py +885 -0
- cotlab-0.8.0/src/cotlab/experiments/classification.py +235 -0
- cotlab-0.8.0/src/cotlab/experiments/composite_shift_detector.py +524 -0
- cotlab-0.8.0/src/cotlab/experiments/cot_ablation.py +277 -0
- cotlab-0.8.0/src/cotlab/experiments/cot_faithfulness.py +187 -0
- cotlab-0.8.0/src/cotlab/experiments/cot_heads.py +208 -0
- cotlab-0.8.0/src/cotlab/experiments/full_layer_cot.py +232 -0
- cotlab-0.8.0/src/cotlab/experiments/full_layer_patching.py +225 -0
- cotlab-0.8.0/src/cotlab/experiments/h_neuron_analysis.py +712 -0
- cotlab-0.8.0/src/cotlab/experiments/logit_lens.py +439 -0
- cotlab-0.8.0/src/cotlab/experiments/multi_head_cot.py +220 -0
- cotlab-0.8.0/src/cotlab/experiments/multi_head_patching.py +229 -0
- cotlab-0.8.0/src/cotlab/experiments/probing_classifier.py +402 -0
- cotlab-0.8.0/src/cotlab/experiments/residual_norm_ood.py +413 -0
- cotlab-0.8.0/src/cotlab/experiments/sae_feature_analysis.py +673 -0
- cotlab-0.8.0/src/cotlab/experiments/steering_vectors.py +223 -0
- cotlab-0.8.0/src/cotlab/experiments/sycophancy_heads.py +224 -0
- cotlab-0.8.0/src/cotlab/logging/__init__.py +5 -0
- cotlab-0.8.0/src/cotlab/logging/json_logger.py +161 -0
- cotlab-0.8.0/src/cotlab/main.py +317 -0
- cotlab-0.8.0/src/cotlab/patching/__init__.py +24 -0
- cotlab-0.8.0/src/cotlab/patching/cache.py +141 -0
- cotlab-0.8.0/src/cotlab/patching/hooks.py +558 -0
- cotlab-0.8.0/src/cotlab/patching/interventions.py +86 -0
- cotlab-0.8.0/src/cotlab/patching/patcher.py +439 -0
- cotlab-0.8.0/src/cotlab/patching/sae.py +181 -0
- cotlab-0.8.0/src/cotlab/prompts/__init__.py +43 -0
- cotlab-0.8.0/src/cotlab/prompts/cardiology.py +378 -0
- cotlab-0.8.0/src/cotlab/prompts/histopathology.py +265 -0
- cotlab-0.8.0/src/cotlab/prompts/length_matched_strategies.py +157 -0
- cotlab-0.8.0/src/cotlab/prompts/mcq.py +193 -0
- cotlab-0.8.0/src/cotlab/prompts/neurology.py +353 -0
- cotlab-0.8.0/src/cotlab/prompts/oncology.py +367 -0
- cotlab-0.8.0/src/cotlab/prompts/plab.py +162 -0
- cotlab-0.8.0/src/cotlab/prompts/pubhealthbench.py +82 -0
- cotlab-0.8.0/src/cotlab/prompts/pubmedqa.py +173 -0
- cotlab-0.8.0/src/cotlab/prompts/radiology.py +414 -0
- cotlab-0.8.0/src/cotlab/prompts/strategies.py +939 -0
- cotlab-0.8.0/src/cotlab/prompts/tcga.py +168 -0
- cotlab-0.8.0/src/cotlab/runner.py +204 -0
- cotlab-0.8.0/tests/conftest.py +40 -0
- cotlab-0.8.0/tests/test_analysis.py +96 -0
- cotlab-0.8.0/tests/test_backends.py +30 -0
- cotlab-0.8.0/tests/test_core.py +81 -0
- cotlab-0.8.0/tests/test_datasets.py +608 -0
- cotlab-0.8.0/tests/test_experiment_docs.py +90 -0
- cotlab-0.8.0/tests/test_experiments.py +442 -0
- cotlab-0.8.0/tests/test_patching.py +535 -0
- cotlab-0.8.0/tests/test_prompts.py +485 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
HF_TOKEN=your_huggingface_token_here
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
name: Deploy Documentation
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
paths:
|
|
8
|
+
- 'docs/**'
|
|
9
|
+
- 'mkdocs.yml'
|
|
10
|
+
- '.github/workflows/docs.yml'
|
|
11
|
+
|
|
12
|
+
permissions:
|
|
13
|
+
contents: write
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
deploy:
|
|
17
|
+
runs-on: ubuntu-latest
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@v4
|
|
20
|
+
|
|
21
|
+
- name: Setup micromamba
|
|
22
|
+
uses: mamba-org/setup-micromamba@v2
|
|
23
|
+
with:
|
|
24
|
+
micromamba-version: "2.0.5-0"
|
|
25
|
+
environment-name: cotlab
|
|
26
|
+
create-args: >-
|
|
27
|
+
python=3.11
|
|
28
|
+
pip
|
|
29
|
+
init-shell: bash
|
|
30
|
+
|
|
31
|
+
- name: Install dependencies
|
|
32
|
+
run: |
|
|
33
|
+
pip install mkdocs mkdocs-material "mkdocstrings[python]"
|
|
34
|
+
shell: micromamba-shell {0}
|
|
35
|
+
|
|
36
|
+
- name: Install CoTLab package
|
|
37
|
+
run: pip install -e .
|
|
38
|
+
shell: micromamba-shell {0}
|
|
39
|
+
|
|
40
|
+
- name: Deploy to GitHub Pages
|
|
41
|
+
run: mkdocs gh-deploy --force
|
|
42
|
+
shell: micromamba-shell {0}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: Lint
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
schedule:
|
|
9
|
+
- cron: "0 6 * * 1" # Every Monday at 06:00 UTC
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
lint:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v6
|
|
16
|
+
|
|
17
|
+
- name: Set up Python
|
|
18
|
+
uses: actions/setup-python@v6
|
|
19
|
+
with:
|
|
20
|
+
python-version: "3.11"
|
|
21
|
+
|
|
22
|
+
- name: Install uv
|
|
23
|
+
uses: astral-sh/setup-uv@v7
|
|
24
|
+
with:
|
|
25
|
+
enable-cache: true
|
|
26
|
+
cache-dependency-glob: "pyproject.toml"
|
|
27
|
+
|
|
28
|
+
- name: Install dependencies
|
|
29
|
+
run: uv pip install --system -e ".[dev]"
|
|
30
|
+
|
|
31
|
+
- name: Check formatting
|
|
32
|
+
run: ruff format --check src/ tests/
|
|
33
|
+
|
|
34
|
+
- name: Lint
|
|
35
|
+
run: ruff check src/ tests/
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
schedule:
|
|
9
|
+
- cron: "0 6 * * 1" # Every Monday at 06:00 UTC
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
unit-tests:
|
|
13
|
+
runs-on: ${{ matrix.os }}
|
|
14
|
+
strategy:
|
|
15
|
+
matrix:
|
|
16
|
+
os: [ubuntu-latest, macos-latest]
|
|
17
|
+
python-version: ["3.11", "3.12", "3.13"]
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v6
|
|
21
|
+
|
|
22
|
+
- name: Free disk space (Linux)
|
|
23
|
+
if: runner.os == 'Linux'
|
|
24
|
+
run: |
|
|
25
|
+
sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /opt/hostedtoolcache/CodeQL
|
|
26
|
+
|
|
27
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
28
|
+
uses: actions/setup-python@v6
|
|
29
|
+
with:
|
|
30
|
+
python-version: ${{ matrix.python-version }}
|
|
31
|
+
|
|
32
|
+
- name: Install uv
|
|
33
|
+
uses: astral-sh/setup-uv@v7
|
|
34
|
+
with:
|
|
35
|
+
enable-cache: true
|
|
36
|
+
cache-dependency-glob: "pyproject.toml"
|
|
37
|
+
|
|
38
|
+
- name: Install dependencies
|
|
39
|
+
run: uv pip install --system -e ".[dev]"
|
|
40
|
+
|
|
41
|
+
- name: Run unit tests
|
|
42
|
+
run: python -m pytest tests/ -v --tb=short
|
|
43
|
+
|
|
44
|
+
vllm-metal:
|
|
45
|
+
runs-on: macos-latest
|
|
46
|
+
continue-on-error: true
|
|
47
|
+
steps:
|
|
48
|
+
- uses: actions/checkout@v6
|
|
49
|
+
|
|
50
|
+
- name: Print Apple Silicon info
|
|
51
|
+
run: |
|
|
52
|
+
sysctl -n machdep.cpu.brand_string
|
|
53
|
+
uname -m
|
|
54
|
+
|
|
55
|
+
- name: Set up Python
|
|
56
|
+
uses: actions/setup-python@v6
|
|
57
|
+
with:
|
|
58
|
+
python-version: "3.12"
|
|
59
|
+
|
|
60
|
+
- name: Install uv
|
|
61
|
+
uses: astral-sh/setup-uv@v7
|
|
62
|
+
with:
|
|
63
|
+
enable-cache: true
|
|
64
|
+
cache-dependency-glob: "pyproject.toml"
|
|
65
|
+
|
|
66
|
+
- name: Install CoTLab
|
|
67
|
+
run: uv pip install --system -e ".[dev]"
|
|
68
|
+
|
|
69
|
+
- name: Install vLLM 0.13.0 from source
|
|
70
|
+
run: |
|
|
71
|
+
cd /tmp
|
|
72
|
+
curl -OL https://github.com/vllm-project/vllm/releases/download/v0.13.0/vllm-0.13.0.tar.gz
|
|
73
|
+
tar xf vllm-0.13.0.tar.gz && cd vllm-0.13.0
|
|
74
|
+
pip install -r requirements/cpu.txt
|
|
75
|
+
pip install .
|
|
76
|
+
|
|
77
|
+
- name: Pin transformers for vLLM
|
|
78
|
+
run: pip install "transformers>=4.56.0,<5"
|
|
79
|
+
|
|
80
|
+
- name: Install vllm-metal plugin
|
|
81
|
+
run: pip install vllm-metal
|
|
82
|
+
|
|
83
|
+
- name: Re-pin transformers (final)
|
|
84
|
+
run: pip install "transformers>=4.56.0,<5"
|
|
85
|
+
|
|
86
|
+
- name: Show versions
|
|
87
|
+
run: |
|
|
88
|
+
python - <<'PY'
|
|
89
|
+
import transformers, vllm
|
|
90
|
+
print("transformers:", transformers.__version__)
|
|
91
|
+
print("vllm:", vllm.__version__)
|
|
92
|
+
PY
|
|
93
|
+
|
|
94
|
+
- name: Test vllm-metal installation
|
|
95
|
+
run: |
|
|
96
|
+
python -c "
|
|
97
|
+
from vllm import LLM
|
|
98
|
+
import vllm_metal
|
|
99
|
+
print(f'vLLM-Metal ready!')
|
|
100
|
+
print(f'vllm-metal version: {vllm_metal.__version__}')
|
|
101
|
+
"
|
|
102
|
+
|
|
103
|
+
- name: Test VLLMBackend platform detection
|
|
104
|
+
run: |
|
|
105
|
+
python -c "
|
|
106
|
+
from cotlab.backends.vllm_backend import VLLMBackend, _detect_platform, _is_apple_silicon
|
|
107
|
+
assert _is_apple_silicon(), 'Should detect Apple Silicon'
|
|
108
|
+
assert _detect_platform() == 'metal', 'Should detect metal platform'
|
|
109
|
+
backend = VLLMBackend()
|
|
110
|
+
assert backend.platform == 'metal', 'Backend should use metal platform'
|
|
111
|
+
print('VLLMBackend metal platform detection works!')
|
|
112
|
+
"
|
|
113
|
+
|
|
114
|
+
integration:
|
|
115
|
+
runs-on: ${{ matrix.os }}
|
|
116
|
+
env:
|
|
117
|
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
|
118
|
+
HUGGING_FACE_HUB_TOKEN: ${{ secrets.HF_TOKEN }}
|
|
119
|
+
strategy:
|
|
120
|
+
matrix:
|
|
121
|
+
os: [ubuntu-latest, macos-latest]
|
|
122
|
+
steps:
|
|
123
|
+
- uses: actions/checkout@v6
|
|
124
|
+
|
|
125
|
+
- name: Free disk space (Linux)
|
|
126
|
+
if: runner.os == 'Linux'
|
|
127
|
+
run: |
|
|
128
|
+
sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /opt/hostedtoolcache/CodeQL
|
|
129
|
+
|
|
130
|
+
- name: Set up Python
|
|
131
|
+
uses: actions/setup-python@v6
|
|
132
|
+
with:
|
|
133
|
+
python-version: "3.11"
|
|
134
|
+
|
|
135
|
+
- name: Install uv
|
|
136
|
+
uses: astral-sh/setup-uv@v7
|
|
137
|
+
with:
|
|
138
|
+
enable-cache: true
|
|
139
|
+
cache-dependency-glob: "pyproject.toml"
|
|
140
|
+
|
|
141
|
+
- name: Install dependencies
|
|
142
|
+
run: uv pip install --system -e ".[dev]"
|
|
143
|
+
|
|
144
|
+
- name: Check Hugging Face token
|
|
145
|
+
run: |
|
|
146
|
+
if [ -z "${HF_TOKEN}" ]; then
|
|
147
|
+
echo "HF_TOKEN secret is required for private dataset access."
|
|
148
|
+
exit 1
|
|
149
|
+
fi
|
|
150
|
+
|
|
151
|
+
- name: Authenticate Hugging Face
|
|
152
|
+
run: |
|
|
153
|
+
python - <<'PY'
|
|
154
|
+
import os
|
|
155
|
+
from huggingface_hub import login
|
|
156
|
+
|
|
157
|
+
login(token=os.environ["HF_TOKEN"], add_to_git_credential=False)
|
|
158
|
+
print("Authenticated to Hugging Face Hub.")
|
|
159
|
+
PY
|
|
160
|
+
|
|
161
|
+
- name: Test framework imports
|
|
162
|
+
run: |
|
|
163
|
+
python -c "
|
|
164
|
+
from cotlab import __version__
|
|
165
|
+
from cotlab.backends import TransformersBackend
|
|
166
|
+
from cotlab.experiments import (
|
|
167
|
+
CoTFaithfulnessExperiment,
|
|
168
|
+
ClassificationExperiment,
|
|
169
|
+
CoTAblationExperiment,
|
|
170
|
+
ActivationPatchingExperiment,
|
|
171
|
+
ActivationCompareExperiment,
|
|
172
|
+
SycophancyHeadsExperiment,
|
|
173
|
+
MultiHeadPatchingExperiment,
|
|
174
|
+
FullLayerPatchingExperiment,
|
|
175
|
+
SteeringVectorsExperiment,
|
|
176
|
+
CoTHeadsExperiment,
|
|
177
|
+
LogitLensExperiment,
|
|
178
|
+
MultiHeadCoTExperiment,
|
|
179
|
+
ResidualNormOODExperiment,
|
|
180
|
+
CompositeShiftDetectorExperiment,
|
|
181
|
+
)
|
|
182
|
+
from cotlab.prompts import ChainOfThoughtStrategy, RadiologyPromptStrategy, CardiologyPromptStrategy, NeurologyPromptStrategy, OncologyPromptStrategy
|
|
183
|
+
from cotlab.datasets import RadiologyDataset, SyntheticMedicalDataset, CardiologyDataset, NeurologyDataset, OncologyDataset
|
|
184
|
+
from cotlab.analysis import CoTParser, FaithfulnessMetrics
|
|
185
|
+
from cotlab.patching import ActivationPatcher
|
|
186
|
+
|
|
187
|
+
print(f'CoTLab v{__version__} - All 15 experiments available!')
|
|
188
|
+
"
|
|
189
|
+
|
|
190
|
+
- name: Test prompt strategies
|
|
191
|
+
run: |
|
|
192
|
+
python -c "
|
|
193
|
+
from cotlab.prompts import create_prompt_strategy
|
|
194
|
+
|
|
195
|
+
strategies = [
|
|
196
|
+
'simple', 'chain_of_thought', 'direct_answer', 'arrogance', 'no_instruction',
|
|
197
|
+
'adversarial', 'uncertainty', 'socratic', 'contrarian', 'expert_persona',
|
|
198
|
+
'sycophantic', 'few_shot'
|
|
199
|
+
]
|
|
200
|
+
for name in strategies:
|
|
201
|
+
s = create_prompt_strategy(name)
|
|
202
|
+
prompt = s.build_prompt({'question': 'Test question?'})
|
|
203
|
+
print(f'{name}: {len(prompt)} chars')
|
|
204
|
+
|
|
205
|
+
print(f'All {len(strategies)} prompt strategies work!')
|
|
206
|
+
"
|
|
207
|
+
|
|
208
|
+
- name: Test dataset loading
|
|
209
|
+
run: |
|
|
210
|
+
python -c "
|
|
211
|
+
from cotlab.datasets import SyntheticMedicalDataset, RadiologyDataset, CardiologyDataset, NeurologyDataset, OncologyDataset, PubHealthBenchDataset, MARCDataset, MedBulletsDataset, PLABDataset
|
|
212
|
+
|
|
213
|
+
for Dataset in [SyntheticMedicalDataset, RadiologyDataset, CardiologyDataset, NeurologyDataset, OncologyDataset, PubHealthBenchDataset, MARCDataset, MedBulletsDataset, PLABDataset]:
|
|
214
|
+
dataset = Dataset()
|
|
215
|
+
print(f'{Dataset.__name__}: {len(dataset)} samples')
|
|
216
|
+
|
|
217
|
+
print('All 9 datasets load correctly!')
|
|
218
|
+
"
|
|
219
|
+
|
|
220
|
+
- name: Test CoT parser
|
|
221
|
+
run: |
|
|
222
|
+
python -c "
|
|
223
|
+
from cotlab.analysis import CoTParser
|
|
224
|
+
|
|
225
|
+
parser = CoTParser()
|
|
226
|
+
cot = '''
|
|
227
|
+
1. Patient has fever
|
|
228
|
+
2. Symptoms suggest infection
|
|
229
|
+
Therefore, likely viral URI.
|
|
230
|
+
'''
|
|
231
|
+
|
|
232
|
+
result = parser.analyze(cot)
|
|
233
|
+
print(f'Steps: {result[\"num_steps\"]}')
|
|
234
|
+
print(f'Conclusion: {result[\"conclusion\"]}')
|
|
235
|
+
print('CoT parser works!')
|
|
236
|
+
"
|
|
237
|
+
|
|
238
|
+
- name: Test new prompt features (answer_first, few_shot, contrarian)
|
|
239
|
+
run: |
|
|
240
|
+
python -c "
|
|
241
|
+
from cotlab.prompts import RadiologyPromptStrategy, CardiologyPromptStrategy
|
|
242
|
+
|
|
243
|
+
answer_first = RadiologyPromptStrategy(answer_first=True)
|
|
244
|
+
af_prompt = answer_first.build_prompt({'report': 'Test'})
|
|
245
|
+
assert 'Initial' in af_prompt, 'answer_first prompt should contain Initial Assessment'
|
|
246
|
+
print('answer_first mode works')
|
|
247
|
+
|
|
248
|
+
with_examples = RadiologyPromptStrategy(few_shot=True)
|
|
249
|
+
without_examples = RadiologyPromptStrategy(few_shot=False)
|
|
250
|
+
text_with = with_examples.build_prompt({'report': 'Test'})
|
|
251
|
+
text_without = without_examples.build_prompt({'report': 'Test'})
|
|
252
|
+
assert 'Example 1:' in text_with, 'Should have examples when few_shot=true'
|
|
253
|
+
assert 'Example 1:' not in text_without, 'Should not have examples when few_shot=false'
|
|
254
|
+
assert len(text_without) < len(text_with), 'Prompt without examples should be shorter'
|
|
255
|
+
print('few_shot parameter works')
|
|
256
|
+
|
|
257
|
+
standard = RadiologyPromptStrategy(contrarian=False)
|
|
258
|
+
contrarian = RadiologyPromptStrategy(contrarian=True)
|
|
259
|
+
std_prompt = standard.build_prompt({'report': 'Test'})
|
|
260
|
+
con_prompt = contrarian.build_prompt({'report': 'Test'})
|
|
261
|
+
assert std_prompt != con_prompt, 'Contrarian prompt should differ from standard'
|
|
262
|
+
print('contrarian mode works')
|
|
263
|
+
|
|
264
|
+
for Strategy in [RadiologyPromptStrategy, CardiologyPromptStrategy]:
|
|
265
|
+
s = Strategy(answer_first=True, few_shot=False)
|
|
266
|
+
prompt = s.build_prompt({'report': 'Test'})
|
|
267
|
+
assert 'Initial' in prompt and 'Example 1:' not in prompt
|
|
268
|
+
print('Parameter combinations work across specialties')
|
|
269
|
+
"
|
|
270
|
+
|
|
271
|
+
- name: Test histopathology dataset and prompt
|
|
272
|
+
run: |
|
|
273
|
+
python -c "
|
|
274
|
+
from cotlab.datasets import HistopathologyDataset
|
|
275
|
+
from cotlab.prompts import HistopathologyPromptStrategy
|
|
276
|
+
|
|
277
|
+
print('Testing HistopathologyPromptStrategy...')
|
|
278
|
+
|
|
279
|
+
p = HistopathologyPromptStrategy()
|
|
280
|
+
prompt = p.build_prompt({'text': 'Test histopathology report'})
|
|
281
|
+
assert 'quality' in prompt.lower(), 'Prompt should mention quality'
|
|
282
|
+
assert 'Example 1' in prompt, 'Should have few-shot examples by default'
|
|
283
|
+
print('Standard prompt works')
|
|
284
|
+
|
|
285
|
+
p_no_fs = HistopathologyPromptStrategy(few_shot=False)
|
|
286
|
+
prompt_no_fs = p_no_fs.build_prompt({'text': 'Test'})
|
|
287
|
+
assert 'Example 1' not in prompt_no_fs, 'Should not have examples when few_shot=false'
|
|
288
|
+
print('few_shot=False works')
|
|
289
|
+
|
|
290
|
+
p_con = HistopathologyPromptStrategy(contrarian=True)
|
|
291
|
+
prompt_con = p_con.build_prompt({'text': 'Test'})
|
|
292
|
+
assert 'skeptical' in prompt_con.lower() or 'critical' in prompt_con.lower(), 'Contrarian should be skeptical'
|
|
293
|
+
assert 'skeptical' in p_con.get_system_message().lower(), 'System message should be skeptical'
|
|
294
|
+
print('contrarian mode works')
|
|
295
|
+
|
|
296
|
+
p_af = HistopathologyPromptStrategy(answer_first=True)
|
|
297
|
+
prompt_af = p_af.build_prompt({'text': 'Test'})
|
|
298
|
+
assert 'First' in prompt_af, 'Answer-first should ask for rating first'
|
|
299
|
+
print('answer_first mode works')
|
|
300
|
+
|
|
301
|
+
result = p.parse_response('{\"quality_score\": 2, \"reasoning\": \"Good report\"}')
|
|
302
|
+
assert result['quality_score'] == 2, 'Should parse quality_score'
|
|
303
|
+
assert result['parse_success'], 'Should mark as success'
|
|
304
|
+
print('parse_response works')
|
|
305
|
+
|
|
306
|
+
print('All histopathology tests passed!')
|
|
307
|
+
"
|
|
308
|
+
|
|
309
|
+
notebook-test:
|
|
310
|
+
runs-on: ${{ matrix.os }}
|
|
311
|
+
env:
|
|
312
|
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
|
313
|
+
HUGGING_FACE_HUB_TOKEN: ${{ secrets.HF_TOKEN }}
|
|
314
|
+
strategy:
|
|
315
|
+
matrix:
|
|
316
|
+
os: [ubuntu-latest, macos-latest]
|
|
317
|
+
steps:
|
|
318
|
+
- uses: actions/checkout@v6
|
|
319
|
+
|
|
320
|
+
- name: Free disk space (Linux)
|
|
321
|
+
if: runner.os == 'Linux'
|
|
322
|
+
run: |
|
|
323
|
+
sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /opt/hostedtoolcache/CodeQL
|
|
324
|
+
|
|
325
|
+
- name: Set up Python
|
|
326
|
+
uses: actions/setup-python@v6
|
|
327
|
+
with:
|
|
328
|
+
python-version: "3.11"
|
|
329
|
+
|
|
330
|
+
- name: Install uv
|
|
331
|
+
uses: astral-sh/setup-uv@v7
|
|
332
|
+
with:
|
|
333
|
+
enable-cache: true
|
|
334
|
+
cache-dependency-glob: "pyproject.toml"
|
|
335
|
+
|
|
336
|
+
- name: Install dependencies
|
|
337
|
+
run: |
|
|
338
|
+
uv pip install --system -e ".[dev]"
|
|
339
|
+
pip install papermill ipykernel
|
|
340
|
+
python -m ipykernel install --user --name cotlab --display-name "CoTLab"
|
|
341
|
+
|
|
342
|
+
- name: Check Hugging Face token
|
|
343
|
+
run: |
|
|
344
|
+
if [ -z "${HF_TOKEN}" ]; then
|
|
345
|
+
echo "HF_TOKEN secret is required for private dataset access."
|
|
346
|
+
exit 1
|
|
347
|
+
fi
|
|
348
|
+
|
|
349
|
+
- name: Authenticate Hugging Face
|
|
350
|
+
run: |
|
|
351
|
+
python - <<'PY'
|
|
352
|
+
import os
|
|
353
|
+
from huggingface_hub import login
|
|
354
|
+
|
|
355
|
+
login(token=os.environ["HF_TOKEN"], add_to_git_credential=False)
|
|
356
|
+
print("Authenticated to Hugging Face Hub.")
|
|
357
|
+
PY
|
|
358
|
+
|
|
359
|
+
- name: Execute tutorial notebook
|
|
360
|
+
run: |
|
|
361
|
+
echo "Running notebook: cotlab_tutorial.ipynb (CI mode: 5 samples, 64 max_tokens)"
|
|
362
|
+
cd notebooks
|
|
363
|
+
papermill cotlab_tutorial.ipynb cotlab_tutorial_executed.ipynb -k cotlab --progress-bar -p NUM_SAMPLES 5 -p MAX_TOKENS 64
|
|
364
|
+
echo "Notebook executed successfully on ${{ matrix.os }}!"
|
cotlab-0.8.0/.gitignore
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Virtual environments
|
|
2
|
+
/cot/
|
|
3
|
+
/cotlab/
|
|
4
|
+
/.venv/
|
|
5
|
+
/venv/
|
|
6
|
+
/env/
|
|
7
|
+
|
|
8
|
+
# Experiment outputs
|
|
9
|
+
outputs/
|
|
10
|
+
multirun/
|
|
11
|
+
|
|
12
|
+
# Model configs (local only — never commit)
|
|
13
|
+
conf/model/
|
|
14
|
+
|
|
15
|
+
# Python
|
|
16
|
+
__pycache__/
|
|
17
|
+
*.py[cod]
|
|
18
|
+
*$py.class
|
|
19
|
+
*.so
|
|
20
|
+
.Python
|
|
21
|
+
build/
|
|
22
|
+
develop-eggs/
|
|
23
|
+
dist/
|
|
24
|
+
downloads/
|
|
25
|
+
eggs/
|
|
26
|
+
.eggs/
|
|
27
|
+
lib/
|
|
28
|
+
lib64/
|
|
29
|
+
parts/
|
|
30
|
+
sdist/
|
|
31
|
+
var/
|
|
32
|
+
wheels/
|
|
33
|
+
*.egg-info/
|
|
34
|
+
.installed.cfg
|
|
35
|
+
*.egg
|
|
36
|
+
|
|
37
|
+
# IDE
|
|
38
|
+
.idea/
|
|
39
|
+
.vscode/
|
|
40
|
+
*.swp
|
|
41
|
+
*.swo
|
|
42
|
+
*~
|
|
43
|
+
|
|
44
|
+
# Environment
|
|
45
|
+
.env
|
|
46
|
+
.envrc
|
|
47
|
+
|
|
48
|
+
# Testing
|
|
49
|
+
.pytest_cache/
|
|
50
|
+
.coverage
|
|
51
|
+
htmlcov/
|
|
52
|
+
.tox/
|
|
53
|
+
.nox/
|
|
54
|
+
|
|
55
|
+
# Jupyter
|
|
56
|
+
.ipynb_checkpoints/
|
|
57
|
+
|
|
58
|
+
# OS
|
|
59
|
+
.DS_Store
|
|
60
|
+
Thumbs.db
|
|
61
|
+
|
|
62
|
+
# Logs
|
|
63
|
+
*.log
|
|
64
|
+
uv.lock
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
# Ruff - linting and formatting
|
|
3
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
4
|
+
rev: v0.14.10
|
|
5
|
+
hooks:
|
|
6
|
+
- id: ruff
|
|
7
|
+
args: [--fix]
|
|
8
|
+
- id: ruff-format
|
|
9
|
+
|
|
10
|
+
# Basic file checks
|
|
11
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
12
|
+
rev: v6.0.0
|
|
13
|
+
hooks:
|
|
14
|
+
- id: trailing-whitespace
|
|
15
|
+
- id: end-of-file-fixer
|
|
16
|
+
- id: check-yaml
|
|
17
|
+
- id: check-added-large-files
|
|
18
|
+
args: ['--maxkb=600']
|
cotlab-0.8.0/CLAUDE.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# CoTLab — Claude Instructions
|
|
2
|
+
|
|
3
|
+
## Commit style
|
|
4
|
+
- Always prefix with a conventional type: `feat`, `fix`, `refactor`, `chore`, `docs`, `test`
|
|
5
|
+
- One line only — no body, no co-author trailer, no bullet points
|
|
6
|
+
- Example: `feat: add composite shift detector experiment`
|
|
7
|
+
|
|
8
|
+
## Model names
|
|
9
|
+
Always use HF repo ID format directly — never safe-name format:
|
|
10
|
+
- `model=google/gemma-3-270m-it` (local smoke test)
|
|
11
|
+
- `model=google/medgemma-4b-it` (local full test)
|
|
12
|
+
- `model=google/medgemma-27b-text-it` (H100)
|
|
13
|
+
|
|
14
|
+
## Git — never commit model config files
|
|
15
|
+
Never stage or commit anything under `conf/model/`. These are local only.
|
|
16
|
+
Always exclude: `conf/model/google/`, `conf/model/*.yaml`
|
|
17
|
+
|
|
18
|
+
## Run commands
|
|
19
|
+
Always use `\` line continuation with **one arg per line** — this is the working pattern.
|
|
20
|
+
Always include `backend.device=` explicitly.
|
|
21
|
+
|
|
22
|
+
Local (Mac, MPS):
|
|
23
|
+
```bash
|
|
24
|
+
python -m cotlab.main \
|
|
25
|
+
experiment=residual_norm_ood \
|
|
26
|
+
dataset=afrimedqa \
|
|
27
|
+
backend=transformers \
|
|
28
|
+
backend.device=mps \
|
|
29
|
+
model=google/gemma-3-270m-it \
|
|
30
|
+
experiment.num_samples=10
|
|
31
|
+
|
|
32
|
+
python -m cotlab.main \
|
|
33
|
+
experiment=composite_shift_detector \
|
|
34
|
+
dataset=afrimedqa \
|
|
35
|
+
backend=transformers \
|
|
36
|
+
backend.device=mps \
|
|
37
|
+
model=google/gemma-3-270m-it \
|
|
38
|
+
experiment.num_samples=10
|
|
39
|
+
|
|
40
|
+
python -m cotlab.main \
|
|
41
|
+
experiment=activation_patching \
|
|
42
|
+
dataset=afrimedqa \
|
|
43
|
+
backend=transformers \
|
|
44
|
+
backend.device=mps \
|
|
45
|
+
model=google/gemma-3-270m-it \
|
|
46
|
+
experiment.patching_mode=cot_contrast \
|
|
47
|
+
experiment.num_samples=10
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
H100 (use `backend.device=cuda` and `model=google/medgemma-27b-text-it`, drop `experiment.num_samples`):
|
|
51
|
+
```bash
|
|
52
|
+
python -m cotlab.main \
|
|
53
|
+
experiment=residual_norm_ood \
|
|
54
|
+
dataset=afrimedqa \
|
|
55
|
+
backend=transformers \
|
|
56
|
+
backend.device=cuda \
|
|
57
|
+
model=google/medgemma-27b-text-it
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Multirun (H100 batch across datasets)
|
|
61
|
+
Define DS variable with `\` line continuation, then quote `"dataset=${DS}"`:
|
|
62
|
+
```bash
|
|
63
|
+
DS="afrimedqa,medbullets,medmcqa,medqa,medxpertqa,mmlu_medical,cardiology, \
|
|
64
|
+
histopathology,neurology,oncology,pediatrics,plab"
|
|
65
|
+
|
|
66
|
+
python -m cotlab.main --multirun \
|
|
67
|
+
experiment=residual_norm_ood \
|
|
68
|
+
backend=transformers \
|
|
69
|
+
backend.device=cuda \
|
|
70
|
+
model=google/medgemma-27b-text-it \
|
|
71
|
+
"dataset=${DS}"
|
|
72
|
+
```
|
|
73
|
+
Set DS once, reuse for all experiments. The `\` inside the double-quoted string joins the lines so Hydra sees one clean comma-separated value.
|
|
74
|
+
|
|
75
|
+
Always test locally with 270m + 10 samples before running on H100.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
FROM rocm/vllm-dev:rocm7.1.1_navi_ubuntu24.04_py3.12_pytorch_2.8_vllm_0.10.2rc1
|
|
2
|
+
|
|
3
|
+
# Ensure cache directory exists (base image already has user with UID 1000)
|
|
4
|
+
RUN mkdir -p /home/user/.cache/huggingface && \
|
|
5
|
+
chown -R 1000:1000 /home/user 2>/dev/null || true
|
|
6
|
+
|
|
7
|
+
WORKDIR /app
|
|
8
|
+
|
|
9
|
+
# Copy project files
|
|
10
|
+
COPY pyproject.toml README.md ./
|
|
11
|
+
COPY src/ ./src/
|
|
12
|
+
COPY conf/ ./conf/
|
|
13
|
+
COPY data/ ./data/
|
|
14
|
+
# Install CoTLab (without vllm since it's already in the base image)
|
|
15
|
+
RUN pip install -e .
|
|
16
|
+
|
|
17
|
+
# Default entrypoint
|
|
18
|
+
ENTRYPOINT ["python", "-m", "cotlab.main"]
|
cotlab-0.8.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Hüseyin Çavuş
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|