rhscope 0.1.0__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.
- rhscope/__init__.py +83 -0
- rhscope/_deps.py +97 -0
- rhscope/activations.py +329 -0
- rhscope/bench/__init__.py +86 -0
- rhscope/bench/compare.py +351 -0
- rhscope/bench/curation.py +279 -0
- rhscope/bench/detectors.py +286 -0
- rhscope/bench/leaderboard.py +275 -0
- rhscope/bench/ref_delta.py +278 -0
- rhscope/bench/result.py +138 -0
- rhscope/bench/rhdetect.py +347 -0
- rhscope/cli.py +764 -0
- rhscope/envs/__init__.py +35 -0
- rhscope/envs/base.py +153 -0
- rhscope/envs/code_shortcut.py +676 -0
- rhscope/hub.py +222 -0
- rhscope/integrations/__init__.py +39 -0
- rhscope/integrations/base.py +185 -0
- rhscope/integrations/dpo.py +178 -0
- rhscope/integrations/sinks.py +164 -0
- rhscope/integrations/trl.py +174 -0
- rhscope/integrations/verl.py +266 -0
- rhscope/io.py +98 -0
- rhscope/metadata.py +92 -0
- rhscope/metrics.py +437 -0
- rhscope/model_card.py +287 -0
- rhscope/monitor.py +296 -0
- rhscope/probes/__init__.py +34 -0
- rhscope/probes/base.py +220 -0
- rhscope/probes/dom.py +161 -0
- rhscope/probes/logistic.py +172 -0
- rhscope/probes/mlp.py +250 -0
- rhscope/probes/online.py +290 -0
- rhscope/probes/tokens.py +294 -0
- rhscope/py.typed +0 -0
- rhscope/recipes/__init__.py +50 -0
- rhscope/recipes/config.py +303 -0
- rhscope/recipes/grpo.py +456 -0
- rhscope/recipes/probe_grid.py +368 -0
- rhscope/recipes/run.py +26 -0
- rhscope/research/__init__.py +31 -0
- rhscope/research/evasion.py +497 -0
- rhscope/sandbox.py +362 -0
- rhscope/training/__init__.py +21 -0
- rhscope/training/data.py +185 -0
- rhscope/training/layer_select.py +38 -0
- rhscope/training/trainer.py +360 -0
- rhscope-0.1.0.dist-info/METADATA +212 -0
- rhscope-0.1.0.dist-info/RECORD +52 -0
- rhscope-0.1.0.dist-info/WHEEL +4 -0
- rhscope-0.1.0.dist-info/entry_points.txt +2 -0
- rhscope-0.1.0.dist-info/licenses/LICENSE +201 -0
rhscope/envs/__init__.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Hacking environments.
|
|
2
|
+
|
|
3
|
+
Public surface:
|
|
4
|
+
|
|
5
|
+
from rhscope.envs import BaseEnv, EnvTask
|
|
6
|
+
from rhscope.envs import CodeShortcutEnv, build_task, load_mbpp_tasks
|
|
7
|
+
|
|
8
|
+
See :mod:`rhscope.envs.base` for the abstract interface every env must
|
|
9
|
+
implement; concrete environments live in sibling modules.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from rhscope.envs.base import BaseEnv, EnvTask
|
|
15
|
+
from rhscope.envs.code_shortcut import (
|
|
16
|
+
CodeShortcutEnv,
|
|
17
|
+
EnvTaskSchemaError,
|
|
18
|
+
ResponseEvaluation,
|
|
19
|
+
TestRunReport,
|
|
20
|
+
build_task,
|
|
21
|
+
extract_code,
|
|
22
|
+
load_mbpp_tasks,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"BaseEnv",
|
|
27
|
+
"CodeShortcutEnv",
|
|
28
|
+
"EnvTask",
|
|
29
|
+
"EnvTaskSchemaError",
|
|
30
|
+
"ResponseEvaluation",
|
|
31
|
+
"TestRunReport",
|
|
32
|
+
"build_task",
|
|
33
|
+
"extract_code",
|
|
34
|
+
"load_mbpp_tasks",
|
|
35
|
+
]
|
rhscope/envs/base.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""Abstract hacking-environment interface + registry.
|
|
2
|
+
|
|
3
|
+
A ``BaseEnv`` combines (a) a task distribution, (b) a *gameable* reward
|
|
4
|
+
function used to drive RL training, and (c) a ground-truth hacking signature
|
|
5
|
+
for labeling rollouts after the fact.
|
|
6
|
+
|
|
7
|
+
The reward function is the signal the policy optimizes against. The hacking
|
|
8
|
+
signature is the oracle used for probe training data and benchmark labeling.
|
|
9
|
+
Together they let a researcher:
|
|
10
|
+
|
|
11
|
+
1. Run RL training that will plausibly induce hacking (reward is weak /
|
|
12
|
+
gameable by design).
|
|
13
|
+
2. After each rollout, label whether the policy actually gamed the reward
|
|
14
|
+
via ``hacking_signature``.
|
|
15
|
+
3. Use those labels as supervision for probe training or as eval metrics.
|
|
16
|
+
|
|
17
|
+
Concrete subclasses self-register via :meth:`BaseEnv.__init_subclass__`;
|
|
18
|
+
``BaseEnv.registered_envs()`` lists them for discovery and
|
|
19
|
+
``BaseEnv.get_env_class(name)`` looks one up by name.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
from abc import ABC, abstractmethod
|
|
25
|
+
from collections.abc import Sequence
|
|
26
|
+
from dataclasses import dataclass, field
|
|
27
|
+
from typing import Any, ClassVar
|
|
28
|
+
|
|
29
|
+
_ENV_REGISTRY: dict[str, type[BaseEnv]] = {}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class EnvTask:
|
|
34
|
+
"""A single task from an environment.
|
|
35
|
+
|
|
36
|
+
Attributes:
|
|
37
|
+
task_id: Stable identifier for this task across train/eval splits.
|
|
38
|
+
prompt: What the policy sees as input. Typically the full instruction
|
|
39
|
+
text including any boilerplate the environment expects.
|
|
40
|
+
metadata: Env-specific information needed by ``reward_fn`` and
|
|
41
|
+
``hacking_signature`` (reference solutions, test specs, …). Opaque
|
|
42
|
+
to the framework; validated by the env that produced it.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
task_id: str
|
|
46
|
+
prompt: str
|
|
47
|
+
metadata: dict[str, Any] = field(default_factory=dict)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class BaseEnv(ABC):
|
|
51
|
+
"""Abstract interface for a reward-hacking training environment.
|
|
52
|
+
|
|
53
|
+
Subclasses implement ``train_tasks``, ``eval_tasks``, ``reward_fn``, and
|
|
54
|
+
``hacking_signature``. The ``evaluate_hacking_rate`` helper is a default
|
|
55
|
+
implementation that iterates ``hacking_signature`` over policy outputs.
|
|
56
|
+
|
|
57
|
+
Subclasses are auto-registered against their ``name`` class attribute at
|
|
58
|
+
class-creation time. Use :meth:`registered_envs` to discover them and
|
|
59
|
+
:meth:`get_env_class` to look one up.
|
|
60
|
+
|
|
61
|
+
Attributes:
|
|
62
|
+
name: Short machine-friendly identifier used in recipes and logs.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
name: ClassVar[str] = "base_env"
|
|
66
|
+
|
|
67
|
+
def __init_subclass__(cls, **kwargs: object) -> None:
|
|
68
|
+
super().__init_subclass__(**kwargs)
|
|
69
|
+
name = getattr(cls, "name", "")
|
|
70
|
+
if not name or name == "base_env":
|
|
71
|
+
return
|
|
72
|
+
existing = _ENV_REGISTRY.get(name)
|
|
73
|
+
if existing is not None and existing is not cls:
|
|
74
|
+
raise RuntimeError(
|
|
75
|
+
f"Env name {name!r} already registered by {existing.__name__}; "
|
|
76
|
+
f"cannot re-register with {cls.__name__}."
|
|
77
|
+
)
|
|
78
|
+
_ENV_REGISTRY[name] = cls
|
|
79
|
+
|
|
80
|
+
@classmethod
|
|
81
|
+
def registered_envs(cls) -> list[str]:
|
|
82
|
+
"""Return the sorted list of currently-registered env names."""
|
|
83
|
+
return sorted(_ENV_REGISTRY)
|
|
84
|
+
|
|
85
|
+
@classmethod
|
|
86
|
+
def get_env_class(cls, name: str) -> type[BaseEnv]:
|
|
87
|
+
"""Return the env class registered under ``name``.
|
|
88
|
+
|
|
89
|
+
Raises:
|
|
90
|
+
KeyError: If no env is registered under ``name``. Import the
|
|
91
|
+
module that defines the env before calling.
|
|
92
|
+
"""
|
|
93
|
+
try:
|
|
94
|
+
return _ENV_REGISTRY[name]
|
|
95
|
+
except KeyError as e:
|
|
96
|
+
raise KeyError(
|
|
97
|
+
f"No env registered under {name!r}. Known: {sorted(_ENV_REGISTRY)!r}."
|
|
98
|
+
) from e
|
|
99
|
+
|
|
100
|
+
@abstractmethod
|
|
101
|
+
def train_tasks(self) -> list[EnvTask]:
|
|
102
|
+
"""Return the training task distribution."""
|
|
103
|
+
|
|
104
|
+
@abstractmethod
|
|
105
|
+
def eval_tasks(self) -> list[EnvTask]:
|
|
106
|
+
"""Return the held-out evaluation task distribution."""
|
|
107
|
+
|
|
108
|
+
@abstractmethod
|
|
109
|
+
def reward_fn(self, task: EnvTask, response: str) -> float:
|
|
110
|
+
"""Compute the training reward for ``response`` on ``task``.
|
|
111
|
+
|
|
112
|
+
This is the signal the policy optimizes against. For hacking envs the
|
|
113
|
+
reward is intentionally gameable (weak tests, lax verifier, …).
|
|
114
|
+
"""
|
|
115
|
+
|
|
116
|
+
@abstractmethod
|
|
117
|
+
def hacking_signature(self, task: EnvTask, response: str) -> bool:
|
|
118
|
+
"""Oracle labeling: did the policy game the reward on this task?
|
|
119
|
+
|
|
120
|
+
Returns:
|
|
121
|
+
``True`` iff ``response`` exhibits the hacking behavior this env
|
|
122
|
+
is designed to catch (e.g. passed the weak tests but failed the
|
|
123
|
+
strong held-out tests).
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
def evaluate_hacking_rate(
|
|
127
|
+
self,
|
|
128
|
+
tasks: Sequence[EnvTask],
|
|
129
|
+
responses: Sequence[str],
|
|
130
|
+
) -> float:
|
|
131
|
+
"""Fraction of ``(task, response)`` pairs flagged as hacking.
|
|
132
|
+
|
|
133
|
+
Default implementation calls ``hacking_signature`` once per pair.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
tasks: Tasks aligned with ``responses``.
|
|
137
|
+
responses: Policy outputs.
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
``sum(hacking_signature) / len(tasks)``, or ``0.0`` if empty.
|
|
141
|
+
|
|
142
|
+
Raises:
|
|
143
|
+
ValueError: If lengths differ.
|
|
144
|
+
"""
|
|
145
|
+
if len(tasks) != len(responses):
|
|
146
|
+
raise ValueError(
|
|
147
|
+
f"tasks and responses must be the same length; got "
|
|
148
|
+
f"{len(tasks)} and {len(responses)}."
|
|
149
|
+
)
|
|
150
|
+
if not tasks:
|
|
151
|
+
return 0.0
|
|
152
|
+
hits = sum(1 for t, r in zip(tasks, responses, strict=True) if self.hacking_signature(t, r))
|
|
153
|
+
return hits / len(tasks)
|