Slurm job workflow management
Project description
srunx
A modern Python library for SLURM workload manager integration with workflow orchestration capabilities.
Features
- 🧩 Workflow Orchestration: YAML-based workflow definitions with Prefect integration
- ⚡ Fine-Grained Parallel Execution: Jobs execute immediately when their specific dependencies complete, not entire workflow phases
- 🔗 Branched Dependency Control: Independent branches in dependency graphs run simultaneously without false dependencies
- 📝 Template System: Customizable Jinja2 templates for SLURM scripts
- 🛡️ Type Safe: Full type hints and mypy compatibility
- 🖥️ CLI Tools: Command-line interfaces for both job management and workflows
- 🚀 Simple Job Submission: Easy-to-use API for submitting SLURM jobs
- ⚙️ Flexible Configuration: Support for various environments (conda, venv, sqsh)
- 📋 Job Management: Submit, monitor, cancel, and list jobs
Installation
Using uv (Recommended)
uv add srunx
Using pip
pip install srunx
Development Installation
git clone https://github.com/ksterx/srunx.git
cd srunx
uv sync --dev
Quick Start
You can try the workflow example:
cd examples
srunx flow run sample_workflow.yaml
graph TD
A["Job A"]
B1["Job B1"]
B2["Job B2"]
C["Job C"]
D["Job D"]
A --> B1
A --> C
B1 --> B2
B2 --> D
C --> D
The workflow engine provides fine-grained dependency control: when Job A completes, B1 and C start immediately in parallel. As soon as B1 finishes, B2 starts regardless of C's status. Job D waits only for both B2 and C to complete, enabling maximum parallelization.
Workflow Orchestration
Create a workflow YAML file:
# workflow.yaml
name: ml_pipeline
tasks:
- name: preprocess
command: ["python", "preprocess.py"]
nodes: 1
memory_per_node: "16GB"
- name: train
command: ["python", "train.py"]
depends_on: [preprocess]
nodes: 1
gpus_per_node: 2
memory_per_node: "32GB"
time_limit: "8:00:00"
conda: ml_env
- name: evaluate
command: ["python", "evaluate.py"]
depends_on: [train]
nodes: 1
- name: notify
command: ["python", "notify.py"]
depends_on: [train, evaluate]
Execute the workflow:
# Run workflow
srunx flow run workflow.yaml
# Validate workflow without execution
srunx flow validate workflow.yaml
# Show execution plan
srunx flow run workflow.yaml --dry-run
Advanced Usage
Custom Templates
Create a custom SLURM template:
#!/bin/bash
#SBATCH --job-name={{ job_name }}
#SBATCH --nodes={{ nodes }}
{% if gpus_per_node > 0 -%}
#SBATCH --gpus-per-node={{ gpus_per_node }}
{% endif -%}
#SBATCH --time={{ time_limit }}
#SBATCH --output={{ log_dir }}/%x_%j.out
{{ environment_setup }}
srun {{ command }}
Use it with your job:
job = client.run(job, template_path="custom_template.slurm.jinja")
Environment Configuration
Conda Environment
environment = JobEnvironment(
conda="my_env",
env_vars={"CUDA_VISIBLE_DEVICES": "0,1"}
)
Programmatic Workflow Execution
from srunx.workflows import WorkflowRunner
runner = WorkflowRunner.from_yaml("workflow.yaml")
results = runner.run()
print("Job IDs:")
for task_name, job_id in results.items():
print(f" {task_name}: {job_id}")
Job Submission
# Submit job without waiting
job = client.submit(job)
# Later, wait for completion
completed_job = client.monitor(job, poll_interval=30)
print(f"Job completed with status: {completed_job.status}")
# Subit and wait for completion
completed_job = client.run(job)
print(f"Job completed with status: {completed_job.status}")
API Reference
Core Classes
Job
Main job configuration class with resources and environment settings.
JobResource
Resource allocation specification (nodes, GPUs, memory, time).
JobEnvironment
Environment setup (conda, venv, sqsh, environment variables).
Slurm
Main interface for SLURM operations (submit, status, cancel, list).
WorkflowRunner
Workflow execution engine with YAML support.
CLI Commands
Main CLI (srunx)
submit- Submit SLURM jobsstatus- Check job statusqueue- List jobscancel- Cancel jobs
Workflow CLI (srunx flow)
- Execute YAML-defined workflows
- Validate workflow files
- Show execution plans
Configuration
Environment Variables
SLURM_LOG_DIR: Default directory for SLURM logs (default:logs)
Template Locations
srunx includes built-in templates:
base.slurm.jinja: Basic job templateadvanced.slurm.jinja: Full-featured template with all options
Development
Setup Development Environment
git clone https://github.com/ksterx/srunx.git
cd srunx
uv sync --dev
Run Tests
uv run pytest
Type Checking
uv run mypy .
Code Formatting
uv run ruff check .
uv run ruff format .
Examples
Machine Learning Pipeline
# Complete ML pipeline example
from srunx import Job, JobResource, JobEnvironment, Slurm
def create_ml_job(script: str, **kwargs) -> Job:
return Job(
name=f"ml_{script.replace('.py', '')}",
command=["python", script] + [f"--{k}={v}" for k, v in kwargs.items()],
resources=JobResource(
nodes=1,
gpus_per_node=1,
memory_per_node="32GB",
time_limit="4:00:00"
),
environment=JobEnvironment(conda="pytorch")
)
client = Slurm()
# Submit preprocessing job
prep_job = create_ml_job("preprocess.py", data_path="/data", output_path="/processed")
prep_job = client.run(prep_job)
# Wait for preprocessing to complete
client.monitor(prep_job)
# Submit training job
train_job = create_ml_job("train.py", data_path="/processed", model_path="/models")
train_job = client.run(train_job)
print(f"Training job {train_job.job_id} submitted")
Distributed Computing
# Multi-node distributed job
distributed_job = Job(
name="distributed_training",
command=[
"mpirun", "-np", "16",
"python", "distributed_train.py"
],
resources=JobResource(
nodes=4,
ntasks_per_node=4,
cpus_per_task=8,
gpus_per_node=2,
memory_per_node="128GB",
time_limit="12:00:00"
),
environment=JobEnvironment(
conda="distributed_ml"
)
)
job = client.run(distributed_job)
Development Workflow
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Run type checking and tests
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- 🐞 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions
Acknowledgments
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file srunx-0.2.0.tar.gz.
File metadata
- Download URL: srunx-0.2.0.tar.gz
- Upload date:
- Size: 179.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
682c6c95a448a2553def1bac309861b1f8c75cdcf6c892c5a5da5c55f345ec16
|
|
| MD5 |
f582253ce912b7cf7ee0ecfa69b84635
|
|
| BLAKE2b-256 |
aeaa92dee158f79c0e4edb476696c9cfe62c89c926120f64437892e25ee312b9
|
File details
Details for the file srunx-0.2.0-py3-none-any.whl.
File metadata
- Download URL: srunx-0.2.0-py3-none-any.whl
- Upload date:
- Size: 24.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94187c2b776d9b858f327bc3bacc41cca6d70c40372412ba5435c12ed2bb6df6
|
|
| MD5 |
a288cfd53f68e2dfc953e15eeb7ba4ea
|
|
| BLAKE2b-256 |
f89511e35e93fa8e2452b84f1c89959cb831c000158b40fb620d58dc3bd25fa9
|