|
19 | 19 | ) |
20 | 20 |
|
21 | 21 | from delphi import logger |
22 | | -from delphi.clients import Offline, OpenRouter |
| 22 | +from delphi.clients import OpenRouter, ServerClient |
23 | 23 | from delphi.config import RunConfig |
24 | 24 | from delphi.explainers import ContrastiveExplainer, DefaultExplainer, NoOpExplainer |
25 | 25 | from delphi.explainers.explainer import ExplainerResult |
@@ -85,14 +85,12 @@ def create_neighbours( |
85 | 85 | ) |
86 | 86 |
|
87 | 87 | for hookpoint in hookpoints: |
88 | | - |
89 | 88 | if constructor_cfg.neighbours_type == "co-occurrence": |
90 | 89 | neighbour_calculator = NeighbourCalculator( |
91 | 90 | cache_dir=latents_path / hookpoint, number_of_neighbours=250 |
92 | 91 | ) |
93 | 92 |
|
94 | 93 | elif constructor_cfg.neighbours_type == "decoder_similarity": |
95 | | - |
96 | 94 | neighbour_calculator = NeighbourCalculator( |
97 | 95 | autoencoder=saes[hookpoint].to("cuda"), number_of_neighbours=250 |
98 | 96 | ) |
@@ -142,7 +140,21 @@ async def process_cache( |
142 | 140 | tokenizer=tokenizer, |
143 | 141 | ) |
144 | 142 |
|
145 | | - if run_cfg.explainer_provider == "offline": |
| 143 | + if run_cfg.server_url and run_cfg.server_port is not None: |
| 144 | + raise ValueError("Set only one of server_url and server_port") |
| 145 | + if run_cfg.server_url or run_cfg.server_port is not None: |
| 146 | + if run_cfg.explainer_provider != "offline": |
| 147 | + raise ValueError("Server options require explainer_provider=offline") |
| 148 | + if run_cfg.server_port is not None and not 1 <= run_cfg.server_port <= 65535: |
| 149 | + raise ValueError("server_port must be between 1 and 65535") |
| 150 | + llm_client = ServerClient( |
| 151 | + run_cfg.explainer_model, |
| 152 | + run_cfg.server_url or f"http://localhost:{run_cfg.server_port}/v1/", |
| 153 | + api_key=os.environ.get("SERVER_API_KEY"), |
| 154 | + ) |
| 155 | + elif run_cfg.explainer_provider == "offline": |
| 156 | + from .clients.offline import Offline |
| 157 | + |
146 | 158 | llm_client = Offline( |
147 | 159 | run_cfg.explainer_model, |
148 | 160 | max_memory=run_cfg.max_memory, |
@@ -171,124 +183,128 @@ async def process_cache( |
171 | 183 | f"Explainer provider {run_cfg.explainer_provider} not supported" |
172 | 184 | ) |
173 | 185 |
|
174 | | - if not run_cfg.explainer == "none": |
175 | | - |
176 | | - def explainer_postprocess(result): |
177 | | - with open(explanations_path / f"{result.record.latent}.txt", "wb") as f: |
178 | | - f.write(orjson.dumps(result.explanation)) |
| 186 | + try: |
| 187 | + if not run_cfg.explainer == "none": |
179 | 188 |
|
180 | | - return result |
181 | | - |
182 | | - if run_cfg.constructor_cfg.non_activating_source == "FAISS": |
183 | | - explainer = ContrastiveExplainer( |
184 | | - llm_client, |
185 | | - threshold=0.3, |
186 | | - verbose=run_cfg.verbose, |
187 | | - ) |
188 | | - else: |
189 | | - explainer = DefaultExplainer( |
190 | | - llm_client, |
191 | | - threshold=0.3, |
192 | | - verbose=run_cfg.verbose, |
193 | | - ) |
| 189 | + def explainer_postprocess(result): |
| 190 | + with open(explanations_path / f"{result.record.latent}.txt", "wb") as f: |
| 191 | + f.write(orjson.dumps(result.explanation)) |
194 | 192 |
|
195 | | - explainer_pipe = Pipe( |
196 | | - process_wrapper(explainer, postprocess=explainer_postprocess) |
197 | | - ) |
198 | | - else: |
| 193 | + return result |
199 | 194 |
|
200 | | - def none_postprocessor(result): |
201 | | - # Load the explanation from disk |
202 | | - explanation_path = explanations_path / f"{result.record.latent}.txt" |
203 | | - if not explanation_path.exists(): |
204 | | - raise FileNotFoundError( |
205 | | - f"Explanation file {explanation_path} does not exist. " |
206 | | - "Make sure to run an explainer pipeline first." |
| 195 | + if run_cfg.constructor_cfg.non_activating_source == "FAISS": |
| 196 | + explainer = ContrastiveExplainer( |
| 197 | + llm_client, |
| 198 | + threshold=0.3, |
| 199 | + verbose=run_cfg.verbose, |
207 | 200 | ) |
208 | | - |
209 | | - with open(explanation_path, "rb") as f: |
210 | | - return ExplainerResult( |
211 | | - record=result.record, |
212 | | - explanation=orjson.loads(f.read()), |
| 201 | + else: |
| 202 | + explainer = DefaultExplainer( |
| 203 | + llm_client, |
| 204 | + threshold=0.3, |
| 205 | + verbose=run_cfg.verbose, |
213 | 206 | ) |
214 | 207 |
|
215 | | - explainer_pipe = Pipe( |
216 | | - process_wrapper( |
217 | | - NoOpExplainer(), |
218 | | - postprocess=none_postprocessor, |
| 208 | + explainer_pipe = Pipe( |
| 209 | + process_wrapper(explainer, postprocess=explainer_postprocess) |
219 | 210 | ) |
220 | | - ) |
221 | | - |
222 | | - # Builds the record from result returned by the pipeline |
223 | | - def scorer_preprocess(result): |
224 | | - if isinstance(result, list): |
225 | | - result = result[0] |
226 | | - |
227 | | - record = result.record |
228 | | - record.explanation = result.explanation |
229 | | - record.extra_examples = record.not_active |
230 | | - return record |
231 | | - |
232 | | - # Saves the score to a file |
233 | | - def scorer_postprocess(result, score_dir): |
234 | | - safe_latent_name = str(result.record.latent).replace("/", "--") |
235 | | - |
236 | | - with open(score_dir / f"{safe_latent_name}.txt", "wb") as f: |
237 | | - f.write(orjson.dumps(result.score)) |
| 211 | + else: |
238 | 212 |
|
239 | | - scorers = [] |
240 | | - for scorer_name in run_cfg.scorers: |
241 | | - scorer_path = scores_path / scorer_name |
242 | | - scorer_path.mkdir(parents=True, exist_ok=True) |
| 213 | + def none_postprocessor(result): |
| 214 | + # Load the explanation from disk |
| 215 | + explanation_path = explanations_path / f"{result.record.latent}.txt" |
| 216 | + if not explanation_path.exists(): |
| 217 | + raise FileNotFoundError( |
| 218 | + f"Explanation file {explanation_path} does not exist. " |
| 219 | + "Make sure to run an explainer pipeline first." |
| 220 | + ) |
| 221 | + |
| 222 | + with open(explanation_path, "rb") as f: |
| 223 | + return ExplainerResult( |
| 224 | + record=result.record, |
| 225 | + explanation=orjson.loads(f.read()), |
| 226 | + ) |
| 227 | + |
| 228 | + explainer_pipe = Pipe( |
| 229 | + process_wrapper( |
| 230 | + NoOpExplainer(), |
| 231 | + postprocess=none_postprocessor, |
| 232 | + ) |
| 233 | + ) |
243 | 234 |
|
244 | | - if scorer_name == "simulation": |
245 | | - if isinstance(llm_client, Offline): |
246 | | - scorer = OpenAISimulator( |
247 | | - llm_client, tokenizer=tokenizer, all_at_once=True |
| 235 | + # Builds the record from result returned by the pipeline |
| 236 | + def scorer_preprocess(result): |
| 237 | + if isinstance(result, list): |
| 238 | + result = result[0] |
| 239 | + |
| 240 | + record = result.record |
| 241 | + record.explanation = result.explanation |
| 242 | + record.extra_examples = record.not_active |
| 243 | + return record |
| 244 | + |
| 245 | + # Saves the score to a file |
| 246 | + def scorer_postprocess(result, score_dir): |
| 247 | + safe_latent_name = str(result.record.latent).replace("/", "--") |
| 248 | + |
| 249 | + with open(score_dir / f"{safe_latent_name}.txt", "wb") as f: |
| 250 | + f.write(orjson.dumps(result.score)) |
| 251 | + |
| 252 | + scorers = [] |
| 253 | + for scorer_name in run_cfg.scorers: |
| 254 | + scorer_path = scores_path / scorer_name |
| 255 | + scorer_path.mkdir(parents=True, exist_ok=True) |
| 256 | + |
| 257 | + if scorer_name == "simulation": |
| 258 | + if llm_client.supports_prompt_logprobs: |
| 259 | + scorer = OpenAISimulator( |
| 260 | + llm_client, tokenizer=tokenizer, all_at_once=True |
| 261 | + ) |
| 262 | + else: |
| 263 | + scorer = OpenAISimulator( |
| 264 | + llm_client, tokenizer=tokenizer, all_at_once=False |
| 265 | + ) |
| 266 | + elif scorer_name == "fuzz": |
| 267 | + scorer = FuzzingScorer( |
| 268 | + llm_client, |
| 269 | + n_examples_shown=run_cfg.num_examples_per_scorer_prompt, |
| 270 | + verbose=run_cfg.verbose, |
| 271 | + log_prob=run_cfg.log_probs, |
| 272 | + fuzz_type=run_cfg.fuzz_type, |
248 | 273 | ) |
249 | | - else: |
250 | | - scorer = OpenAISimulator( |
251 | | - llm_client, tokenizer=tokenizer, all_at_once=False |
| 274 | + elif scorer_name == "detection": |
| 275 | + scorer = DetectionScorer( |
| 276 | + llm_client, |
| 277 | + n_examples_shown=run_cfg.num_examples_per_scorer_prompt, |
| 278 | + verbose=run_cfg.verbose, |
| 279 | + log_prob=run_cfg.log_probs, |
252 | 280 | ) |
253 | | - elif scorer_name == "fuzz": |
254 | | - scorer = FuzzingScorer( |
255 | | - llm_client, |
256 | | - n_examples_shown=run_cfg.num_examples_per_scorer_prompt, |
257 | | - verbose=run_cfg.verbose, |
258 | | - log_prob=run_cfg.log_probs, |
259 | | - fuzz_type=run_cfg.fuzz_type, |
260 | | - ) |
261 | | - elif scorer_name == "detection": |
262 | | - scorer = DetectionScorer( |
263 | | - llm_client, |
264 | | - n_examples_shown=run_cfg.num_examples_per_scorer_prompt, |
265 | | - verbose=run_cfg.verbose, |
266 | | - log_prob=run_cfg.log_probs, |
| 281 | + else: |
| 282 | + raise ValueError(f"Scorer {scorer_name} not supported") |
| 283 | + |
| 284 | + wrapped_scorer = process_wrapper( |
| 285 | + scorer, |
| 286 | + preprocess=scorer_preprocess, |
| 287 | + postprocess=partial(scorer_postprocess, score_dir=scorer_path), |
267 | 288 | ) |
268 | | - else: |
269 | | - raise ValueError(f"Scorer {scorer_name} not supported") |
| 289 | + scorers.append(wrapped_scorer) |
270 | 290 |
|
271 | | - wrapped_scorer = process_wrapper( |
272 | | - scorer, |
273 | | - preprocess=scorer_preprocess, |
274 | | - postprocess=partial(scorer_postprocess, score_dir=scorer_path), |
| 291 | + pipeline = Pipeline( |
| 292 | + dataset, |
| 293 | + explainer_pipe, |
| 294 | + Pipe(*scorers), |
275 | 295 | ) |
276 | | - scorers.append(wrapped_scorer) |
277 | 296 |
|
278 | | - pipeline = Pipeline( |
279 | | - dataset, |
280 | | - explainer_pipe, |
281 | | - Pipe(*scorers), |
282 | | - ) |
283 | | - |
284 | | - if run_cfg.pipeline_num_proc > 1 and run_cfg.explainer_provider == "openrouter": |
285 | | - print( |
286 | | - "OpenRouter does not support multiprocessing," |
287 | | - " setting pipeline_num_proc to 1" |
288 | | - ) |
289 | | - run_cfg.pipeline_num_proc = 1 |
| 297 | + if run_cfg.pipeline_num_proc > 1 and run_cfg.explainer_provider == "openrouter": |
| 298 | + print( |
| 299 | + "OpenRouter does not support multiprocessing," |
| 300 | + " setting pipeline_num_proc to 1" |
| 301 | + ) |
| 302 | + run_cfg.pipeline_num_proc = 1 |
290 | 303 |
|
291 | | - await pipeline.run(run_cfg.pipeline_num_proc) |
| 304 | + await pipeline.run(run_cfg.pipeline_num_proc) |
| 305 | + finally: |
| 306 | + if isinstance(llm_client, ServerClient): |
| 307 | + await llm_client.close() |
292 | 308 |
|
293 | 309 |
|
294 | 310 | def populate_cache( |
|
0 commit comments