API Reference
This page is auto-generated from the public API exported by slurptuna.
Public API
slurptuna
Functions
execution_mode(mode)
Validate and normalize an execution mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
One of the supported strings: |
required |
Returns:
| Type | Description |
|---|---|
ExecutionMode
|
The corresponding |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/slurptuna/api.py
loss(*, name=None, description=None, parameter_space=None, seed_start=0)
Decorator to define a loss function for hyperparameter optimization.
The decorated function should accept params (dict of hyperparameters), seed (int),
and optionally a context dict. It should return either:
- A scalar loss value (float)
- A dict of scalar losses (will be averaged across entries for shared optimization)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Unique identifier for this loss. Required. |
None
|
description
|
str | None
|
Human-readable description of what this loss represents. Required. |
None
|
parameter_space
|
dict[str, ParamSpec] | None
|
Dict mapping parameter names to search specs. Required. Specs can be tuples (min, max) for continuous ranges or SearchParam objects for more control over type (int/categorical) and bounds. Example: {"alpha": (0.0, 1.0), "lr": search_param(range=(1e-4, 1e-2), dtype="log")} |
None
|
seed_start
|
int
|
Starting seed number for this loss. Default 0. |
0
|
Returns:
| Type | Description |
|---|---|
|
The decorated function as a LossDefinition ready for optimize_run() or optimize_entries(). |
Example
@loss( name="my_model_loss", description="Fit model parameters to training data", parameter_space={"learning_rate": (1e-4, 1e-2), "batch_size": search_param(range=(8, 256), dtype="int")} ) def my_model_loss(params, seed, context): # ... model training logic ... return mean_squared_error
Source code in src/slurptuna/registry.py
optimize_entries(loss, *, entry_ids, n_trials=10, seeds=None, n_seeds=None, chunk_size=None, num_chunks=None, random_seed=123, direction='minimize', mode=ExecutionMode.SINGLE, run_root='runs', run_name_prefix=None, loss_module=None, slurm_poll_seconds=15, slurm_timeout_minutes=120, cpus_per_task=1, max_concurrent_trials=1, array_parallelism_limit=None, worker_parallelism=1, max_concurrent_entries=None, worker_time_limit=timedelta(hours=2), slurm_qos='short', trial_retry_attempts=1, fail_on_chunk_error=True, use_processes=False, mem_per_cpu='2G', forward_sys_argv_to_workers=True)
Optimize independent fits for each entry, returning per-entry best parameters.
Use this for participant-wise, condition-wise, or other per-entry fitting where each entry gets its own separate optimization study. The loss function receives the current entry_id in its context dict, allowing entry-specific behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss
|
LossDefinition
|
LossDefinition created with the @loss decorator. |
required |
entry_ids
|
Iterable[str]
|
Iterable of entry identifiers (e.g., participant IDs, condition names). Each entry gets its own optimization study. |
required |
n_trials
|
int
|
Number of optimization trials per entry. Default 10. |
10
|
seeds
|
Iterable[int] | None
|
Explicit iterable of seed IDs (takes priority over n_seeds). |
None
|
n_seeds
|
int | None
|
Number of contiguous seeds per entry. Default derived from chunk_size/num_chunks. |
None
|
chunk_size
|
int | None
|
Seeds per distributed task (for mode=DISTRIBUTED). Default from loss metadata. |
None
|
num_chunks
|
int | None
|
Number of chunks per trial (for mode=DISTRIBUTED). Default auto from n_seeds/chunk_size. |
None
|
random_seed
|
int
|
Base seed for TPESampler; each entry gets random_seed + entry_index. Default 123. |
123
|
direction
|
str
|
Optimization direction: "minimize" or "maximize". Default "minimize". |
'minimize'
|
mode
|
ExecutionMode
|
ExecutionMode.SINGLE (in-process) or ExecutionMode.DISTRIBUTED (Slurm arrays). Default SINGLE. |
SINGLE
|
run_root
|
str | Path
|
Root directory for all output. Default "runs". |
'runs'
|
run_name_prefix
|
str | None
|
Prefix for the parent directory containing all entry runs. Auto-generated if not provided. |
None
|
loss_module
|
str | None
|
Module path for loss function (required for DISTRIBUTED mode if loss not in main). |
None
|
slurm_poll_seconds
|
int
|
Polling interval for Slurm job status. Default 15. |
15
|
slurm_timeout_minutes
|
int
|
Maximum wait time for distributed jobs. Default 120. |
120
|
cpus_per_task
|
int
|
CPUs per Slurm task (DISTRIBUTED only). Default 1. |
1
|
max_concurrent_trials
|
int
|
Number of trials per entry to run in parallel. Default 1. |
1
|
array_parallelism_limit
|
int | None
|
Max concurrent Slurm array jobs across all entries (DISTRIBUTED). Default unlimited. |
None
|
worker_parallelism
|
int
|
Number of seeds in parallel per worker. Default 1. |
1
|
max_concurrent_entries
|
int | None
|
Number of entries to optimize in parallel. Default all entries. |
None
|
worker_time_limit
|
timedelta
|
Max wall time per distributed task. Default 2 hours. |
timedelta(hours=2)
|
slurm_qos
|
str | None
|
Optional Slurm QoS name passed to |
'short'
|
trial_retry_attempts
|
int
|
Retry failed trials this many times. Default 1 (no retries). |
1
|
fail_on_chunk_error
|
bool
|
Whether to fail immediately if any chunk fails in distributed mode. Default True. |
True
|
use_processes
|
bool
|
Use ProcessPoolExecutor instead of ThreadPoolExecutor for seed parallelism. Default False (threads). See optimize_run() for full details on when to prefer processes over threads. |
False
|
mem_per_cpu
|
str
|
Memory per CPU for Slurm chunk tasks (DISTRIBUTED only). Default "2G". |
'2G'
|
forward_sys_argv_to_workers
|
bool
|
Forward the launcher's |
True
|
Returns:
| Type | Description |
|---|---|
MultiOptimizeResult
|
MultiOptimizeResult containing: |
MultiOptimizeResult
|
|
MultiOptimizeResult
|
|
MultiOptimizeResult
|
|
MultiOptimizeResult
|
|
MultiOptimizeResult
|
|
Source code in src/slurptuna/api.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | |
optimize_run(loss, *, n_trials=10, seeds=None, n_seeds=None, chunk_size=None, num_chunks=None, random_seed=123, entry_id=None, direction='minimize', mode=ExecutionMode.SINGLE, run_root='runs', run_name=None, loss_module=None, slurm_poll_seconds=15, slurm_timeout_minutes=120, cpus_per_task=1, mem_per_cpu='2G', max_concurrent_trials=1, array_parallelism_limit=None, worker_parallelism=1, worker_time_limit=timedelta(hours=2), slurm_qos='short', trial_retry_attempts=1, fail_on_chunk_error=True, use_processes=False, forward_sys_argv_to_workers=True)
Optimize hyperparameters for a single shared fit.
Runs Bayesian optimization on a loss function. For averaging across multiple entries (participants, conditions, etc.), return a dict from your loss function. For per-entry optimization, use optimize_entries() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss
|
LossDefinition
|
LossDefinition created with the @loss decorator. |
required |
n_trials
|
int
|
Number of optimization trials to run. Default 10. |
10
|
seeds
|
Iterable[int] | None
|
Explicit iterable of seed integer IDs (takes priority over n_seeds). |
None
|
n_seeds
|
int | None
|
Number of contiguous seeds starting from loss.seed_start. Default derived from chunk_size/num_chunks. |
None
|
chunk_size
|
int | None
|
Seeds per distributed task (for mode=DISTRIBUTED). Default from loss metadata. |
None
|
num_chunks
|
int | None
|
Number of chunks per trial (for mode=DISTRIBUTED). Default auto from n_seeds/chunk_size. |
None
|
random_seed
|
int
|
Seed for TPESampler (Optuna). Default 123. |
123
|
entry_id
|
str | None
|
Optional entry/participant ID passed to loss context. Only used in optimize_entries(). |
None
|
direction
|
str
|
Optimization direction: "minimize" or "maximize". Default "minimize". |
'minimize'
|
mode
|
ExecutionMode
|
ExecutionMode.SINGLE (in-process) or ExecutionMode.DISTRIBUTED (Slurm arrays). Default SINGLE. |
SINGLE
|
run_root
|
str | Path
|
Root directory for output runs. Default "runs". |
'runs'
|
run_name
|
str | None
|
Name of this run directory. Auto-generated if not provided. |
None
|
loss_module
|
str | None
|
Module path for loss function (required for DISTRIBUTED mode if loss not in main). |
None
|
slurm_poll_seconds
|
int
|
Polling interval for Slurm job status. Default 15. |
15
|
slurm_timeout_minutes
|
int
|
Maximum wait time for distributed job. Default 120. |
120
|
cpus_per_task
|
int
|
CPUs per Slurm task (DISTRIBUTED only). Default 1. |
1
|
max_concurrent_trials
|
int
|
Number of trials to run in parallel. Default 1. |
1
|
array_parallelism_limit
|
int | None
|
Max concurrent Slurm array jobs (DISTRIBUTED). Default unlimited. |
None
|
worker_parallelism
|
int
|
Number of seeds in parallel per worker. Default 1. |
1
|
worker_time_limit
|
timedelta
|
Max wall time per distributed task. Default 2 hours. |
timedelta(hours=2)
|
slurm_qos
|
str | None
|
Optional Slurm QoS name passed to |
'short'
|
trial_retry_attempts
|
int
|
Retry failed trials this many times. Default 1 (no retries). |
1
|
fail_on_chunk_error
|
bool
|
Whether to fail immediately if any chunk fails in distributed mode. Default True. |
True
|
use_processes
|
bool
|
Use ProcessPoolExecutor instead of ThreadPoolExecutor for seed parallelism. Default False (threads). Processes bypass Python's GIL, giving true parallelism even for pure-Python loss functions. The trade-off is higher overhead per seed evaluation due to inter-process pickling of the loss function, params, and results — so this is only worth enabling when individual seed evaluations are slow enough that the pickling cost is negligible (roughly >10 ms per seed). For numpy/scipy-heavy losses, threads are usually sufficient because numpy already releases the GIL. For DISTRIBUTED mode, each Slurm task is already a separate process, so this controls parallelism within each task. |
False
|
forward_sys_argv_to_workers
|
bool
|
Forward the launcher's |
True
|
Returns:
| Type | Description |
|---|---|
OptimizeResult
|
OptimizeResult with best_value, best_params, study metadata, and run_dir path. |
Source code in src/slurptuna/api.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |