-
Notifications
You must be signed in to change notification settings - Fork 63
enh: reduce per-task overhead #858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
satra
wants to merge
9
commits into
main
Choose a base branch
from
enh/profiling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cab9af2
enh: reduce per-task overhead (~26x speedup for simple tasks)
satra 81dca64
fix: detect unstable hashes for custom __bytes_repr__ types
satra f95951e
refactor: encapsulate etelemetry sentinel access in Job.check_eteleme…
github-actions[bot] af6e003
refactor: move etelemetry import to top of job.py
github-actions[bot] 4859b70
refactor: simplify etelemetry check to boolean flag
github-actions[bot] 5906131
Apply suggestions from code review
satra 3dcca84
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9f63f48
Merge branch 'main' into enh/profiling
satra acd0b16
Apply suggestion from @effigies
effigies File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| # CLAUDE.md | ||
|
|
||
| This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. | ||
|
|
||
| ## Overview | ||
|
|
||
| Pydra is a dataflow engine for constructing and executing directed acyclic graphs (DAGs) of tasks. It is the core for Nipype 2.0. Requires Python 3.11+. Uses `attrs` extensively for dataclass-like definitions and `hatchling` + `hatch-vcs` as the build system (version derived from git tags). | ||
|
|
||
| ## Commands | ||
|
|
||
| ### Install | ||
|
|
||
| ```bash | ||
| pip install -e ".[dev]" # full dev install (includes test + lint deps) | ||
| pip install -e ".[test]" # test deps only | ||
| pip install -e ".[doc]" # doc deps only | ||
| ``` | ||
|
|
||
| ### Testing | ||
|
|
||
| ```bash | ||
| pytest pydra # full test suite (parallel, with coverage) | ||
| pytest pydra/engine/tests/test_job.py # single test file | ||
| pytest pydra/engine/tests/test_job.py::test_my_func # single test | ||
| pytest pydra --only-worker=debug # single-process worker (good for debugging) | ||
| pytest pydra --only-worker=cf # ConcurrentFutures worker | ||
| pytest pydra --only-worker=slurm # SLURM (requires sbatch) | ||
| pytest pydra --with-dask # Dask worker | ||
| ``` | ||
|
|
||
| Set `_PYTEST_RAISE=1` for IDE breakpoint-friendly exception propagation. | ||
|
|
||
| Tests include doctests (`--doctest-modules` is on). `xfail_strict = true` means unexpected passes fail. | ||
|
|
||
| ### Linting / Formatting | ||
|
|
||
| ```bash | ||
| tox -e style # check style (ruff) | ||
| tox -e style-fix # auto-fix style | ||
| tox -e spellcheck # codespell check | ||
| pre-commit run --all-files # black + flake8 + codespell + nbstripout | ||
| black pydra | ||
| flake8 # max-line-length=105, ignores E203/W503/F541 | ||
| ``` | ||
|
|
||
| ### Tox Environments | ||
|
|
||
| ```bash | ||
| tox -e py311-latest # Python 3.11, latest deps | ||
| tox -e py313-pre # Python 3.13, pre-release deps | ||
| tox -e py311-min # Python 3.11, minimum pinned deps | ||
| ``` | ||
|
|
||
| ### Docs | ||
|
|
||
| ```bash | ||
| make -C docs html | ||
| ``` | ||
|
|
||
| ### CLI | ||
|
|
||
| ```bash | ||
| pydracli crash <crashfile> # display crash file | ||
| pydracli crash <crashfile> --rerun # rerun crashed job | ||
| ``` | ||
|
|
||
| ## Architecture | ||
|
|
||
| ### Layer Overview | ||
|
|
||
| ``` | ||
| compose/ Task definition (decorators + task types) | ||
| engine/ Execution engine (graph, jobs, state, submitter) | ||
| workers/ Execution backends (cf, debug, slurm, sge, + plugins) | ||
| environments/ Execution environments (native, docker, singularity, lmod) | ||
| utils/ Hashing, typing helpers, plugin discovery, profiling | ||
| tasks/ Built-in reusable tasks | ||
| scripts/ CLI entry points | ||
| ``` | ||
|
|
||
| ### Task Definition (`compose/`) | ||
|
|
||
| Three task flavors, each defined by a decorator: | ||
|
|
||
| **Python tasks** — wrap a Python function: | ||
| ```python | ||
| @python.define | ||
| def Add(a: int, b: int) -> int: | ||
| return a + b | ||
| ``` | ||
|
|
||
| **Shell tasks** — wrap a CLI command: | ||
| ```python | ||
| @shell.define | ||
| class BET(shell.Task["BET.Outputs"]): | ||
| executable = "bet" | ||
| input_image: File = shell.arg(argstr="{input_image}", position=1) | ||
| ``` | ||
|
|
||
| **Workflow tasks** — compose other tasks into a DAG: | ||
| ```python | ||
| @workflow.define | ||
| def MyWorkflow(x: int) -> int: | ||
| node_a = workflow.add(Add(a=x, b=1)) | ||
| return node_a.out | ||
| ``` | ||
|
|
||
| The decorator machinery is in `compose/base/builder.py` (`build_task_class()`). It converts `Arg`/`Out` field specs into `attrs` fields and dynamically creates a `Task` class + a paired `Outputs` class. | ||
|
|
||
| Key base types: `compose.base.Field`, `Arg`, `Out`, `Task[OutputType]`, `Outputs`. | ||
|
|
||
| ### Execution Engine (`engine/`) | ||
|
|
||
| **Workflow construction** (`engine/workflow.py`): `Workflow.construct(task)` runs the workflow definition function to discover nodes and wire the `DiGraph`. Constructed workflows are cached by content hash. | ||
|
|
||
| **Node** (`engine/node.py`): Wraps a `Task` inside a workflow. Holds the task, its `State`, optional `Environment`, and `TaskHooks`. Exposes `lzout` — a lazy output proxy for wiring downstream nodes. | ||
|
|
||
| **LazyField** (`engine/lazy.py`): Promises between nodes. `node.lzout.x` returns a `LazyOutField`. Assigning it to another node's input creates a dataflow edge. | ||
|
|
||
| **State / Splitter / Combiner** (`engine/state.py`): Implements map-reduce semantics. A node can be split over an iterable input (producing parallel jobs) and combined (reducing results). Splitters can be scalar (zip) or outer (cartesian product), expressed in RPN. Each concrete state index corresponds to one `Job`. | ||
|
|
||
| **Job** (`engine/job.py`): The concrete unit of work submitted to a worker. Holds the fully-resolved `Task`, a `cache_dir` (from content hash of task inputs + definition), and uses `filelock.SoftFileLock` for safe parallel execution. | ||
|
|
||
| **Submitter** (`engine/submitter.py`): The async dispatch loop. Constructs the `DiGraph` of `NodeExecution` objects, drives an asyncio event loop to submit ready jobs to the configured `Worker`, handles caching (skips jobs with a valid cached result), and manages concurrency. | ||
|
|
||
| ```python | ||
| with Submitter(worker="cf", cache_root="/tmp/cache") as sub: | ||
| result = sub(my_task) | ||
| ``` | ||
|
|
||
| ### Workers (`workers/`) | ||
|
|
||
| | Class | Module | Description | | ||
| |---|---|---| | ||
| | `ConcurrentFuturesWorker` | `cf.py` | `ProcessPoolExecutor`-based (default) | | ||
| | `DebugWorker` | `debug.py` | Single-process, synchronous | | ||
| | `SlurmWorker` | `slurm.py` | Submits via `sbatch`, polls with `sacct` | | ||
| | `SGEWorker` | `sge.py` | SGE qsub | | ||
|
|
||
| Workers are discovered via a plugin system (`get_plugin_classes` in `utils/general.py`). External workers (`pydra-workers-psij`, `pydra-workers-dask`) are installable as separate packages. | ||
|
|
||
| ### Environments (`environments/`) | ||
|
|
||
| Control *how* shell tasks execute: `native.py` (bare OS), `docker.py`, `singularity.py`, `lmod.py` (load environment modules before executing). | ||
|
|
||
| ### Caching (`utils/hash.py`, `engine/job.py`) | ||
|
|
||
| Cache keys are content hashes of the `Task` (all inputs + task definition). Results are stored as cloudpickled files under `~/.cache/pydra/<version>/run-cache/` (via `platformdirs`). Lock files prevent race conditions. | ||
|
|
||
| ### Provenance Tracking (`engine/audit.py`) | ||
|
|
||
| Optional JSON-LD provenance tracking controlled via `AuditFlag` bits. Messengers: `PrintMessenger`, `FileMessenger`, `RemoteRESTMessenger`. Schema at `schema/context.jsonld`. | ||
|
|
||
| ### Data Flow Summary | ||
|
|
||
| ``` | ||
| User code | ||
| │ | ||
| ├─ @python.define / @shell.define / @workflow.define | ||
| │ └─> compose/base/builder.py: build_task_class() | ||
| │ creates Task(attrs) + Outputs(attrs) | ||
| │ | ||
| ├─ Submitter(worker="cf", cache_root=...) | ||
| │ ├─ Workflow.construct(task) → DiGraph of Nodes (engine/graph.py) | ||
| │ ├─ State resolution → list of state indices (engine/state.py) | ||
| │ ├─ per state-index: Job(task, cache_dir) (engine/job.py) | ||
| │ │ └─ if not cached → Worker.run(job) (workers/) | ||
| │ │ └─ Environment.execute(job) (environments/) | ||
| │ └─ Result(outputs, runtime, cache_dir) (engine/result.py) | ||
| │ | ||
| └─ LazyField wiring between Nodes (engine/lazy.py) | ||
| ``` | ||
|
|
||
| ## Key Files | ||
|
|
||
| | File | Purpose | | ||
| |---|---| | ||
| | `pyproject.toml` | Build, deps, pytest config, coverage config | | ||
| | `tox.ini` | tox envs: test, style, style-fix, spellcheck, build, publish | | ||
| | `.flake8` | Flake8: max-line-length=105 | | ||
| | `pydra/conftest.py` | `worker`/`any_worker` fixtures; `--only-worker`, `--with-dask` flags | | ||
| | `pydra/compose/base/builder.py` | Decorator machinery (`build_task_class`) | | ||
| | `pydra/compose/base/field.py` | `Field`, `Arg`, `Out`, `NO_DEFAULT`, `Requirement` | | ||
| | `pydra/compose/base/task.py` | `Task` and `Outputs` base classes | | ||
| | `pydra/compose/python.py` | `@python.define` | | ||
| | `pydra/compose/shell/task.py` | Shell `Task`, CLI construction | | ||
| | `pydra/compose/workflow.py` | `@workflow.define`, `workflow.add`, `workflow.this` | | ||
| | `pydra/engine/submitter.py` | Async dispatch loop | | ||
| | `pydra/engine/job.py` | Single unit of work, caching, locking | | ||
| | `pydra/engine/state.py` | Splitter/combiner map-reduce | | ||
| | `pydra/engine/lazy.py` | `LazyField` — dataflow wiring | | ||
| | `pydra/engine/workflow.py` | DAG construction and caching | | ||
| | `pydra/engine/node.py` | `Node` — task wrapper in workflow graph | | ||
| | `pydra/utils/hash.py` | Content hashing for cache keys | | ||
| | `pydra/utils/general.py` | Plugin discovery, cache root, platform utils | | ||
| | `pydra/utils/typing.py` | `StateArray`, `TypeParser`, type helpers | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.