API reference

Contents

API reference#

slurmise can be driven from Python as well as from the command line. The Slurmise class in slurmise.api is the entry point the CLI itself uses.

Job data#

slurmise.job_data.array_safe_eq(a, b)[source]#

Check if a and b are equal, even if they are numpy arrays. When a and be are dictionaries call recursively for all key, value pairs.

Return type:

bool

slurmise.job_data.dc_eq(dc1, dc2)[source]#

Checks if two dataclasses which hold numpy arrays are equal

Return type:

bool

class slurmise.job_data.JobData(job_name, slurm_id=None, categories=<factory>, numerics=<factory>, memory=None, runtime=None, cmd=None)[source]#

Bases: object

Jobdata class holds the information of a unique slurm job.

Arguments:
job_name:

The unique command name to execute under slurm.

slurm_id:

The slurm job id assigned by the sceduler for a job run.

categories:

the CLI parameters of this job. This has parameter that affect the performance of the job and are fit seperately.

numerics:

These are parameters that are used as the free variables for fits, such input size, number of iterations etc.

memory:

The maximum amount of memory in MBs this job used.

runtime:

The time this job needed to complete in minutes.

Parameters:
  • job_name (str)

  • slurm_id (str | None)

  • categories (dict)

  • numerics (dict)

  • memory (float | None)

  • runtime (float | None)

  • cmd (str | None)

job_name: str#
slurm_id: str | None = None#
categories: dict#
numerics: dict#
memory: float | None = None#
runtime: float | None = None#
cmd: str | None = None#
static from_dataset(job_name, slurm_id, dataset, categories)[source]#

This method creates a JobData object from a HDF5 dataset that describes a job. :arguments:

job_name:

The unique command name to execute under slurm.

slurm_id:

The slurm job id assigned by the sceduler for a job run.

dataset:

The HDF5 dataset used to populate numerics, memory and runtime information of the job.

Parameters:
  • job_name (str)

  • slurm_id (str)

  • dataset (Dataset)

  • categories (dict)

Return type:

JobData

class slurmise.job_database.JobDatabase(db_file, max_retries=10)[source]#

Bases: object

This class creates the database to store job information. It saves the database in HDF5 file.

Parameters:
  • db_file (str)

  • max_retries (int)

property db_file#
static get_database(db_file, max_retries=10)[source]#

Use in context manager to automatically open and close db file.

Arguments:
db_file:

HDF5 file to use as database

Yields:

JobDatabase with opened db file

Finally:

Closes h5py database

Parameters:
  • db_file (str)

  • max_retries (int)

Return type:

JobDatabase

record(job_data, ignore_existing_job=False)[source]#

It records JobData information in the database. A tree is created based on the job name, categories and slurm id. The leaves of the tree are the memory, runtime and numerics of the JobData.

Parameters:
  • job_data (JobData)

  • ignore_existing_job (bool)

Return type:

None

job_exists(job_data)[source]#
Parameters:

job_data (JobData)

Return type:

bool

update(**kargs)[source]#
query(job_data, update_missing=False)[source]#

Query returns a list of JobData objects based on the requested JobData. The returned jobs match the query JobData’s job name and categories. update_missing will try to get maxRSS and elapsed from sacct if not found in the DB.

Note: It does not descend into all child categories, only the highest matching leaves

Parameters:
  • job_data (JobData)

  • update_missing (bool)

Return type:

list[JobData]

count_records(job_data)[source]#

Count the jobs stored for a job name and category combination.

is_slurm_job only inspects a group’s first key, so no dataset is read.

Parameters:

job_data (JobData)

Return type:

int

delete(job_data, delete_all_children=False)[source]#

Delete jobs with matching job name and categories.

Arguments:
job_data:

JobData object with name and categories which should be removed.

delete_all_children:

When true, will delete recursively any matching jobs

Parameters:
  • job_data (JobData)

  • delete_all_children (bool)

Return type:

None

clear()[source]#
record_fit(fit)[source]#
query_fit(fit)[source]#
update_missing_data(jobs)[source]#

Update missing mem and runtime for jobs with incomplete data in the db. Takes a list of JobData which was queried from the db, updates the db, and returns the updated job list.

TODO: gather slurm_ids of jobs that need updating and do it in one call

Parameters:

jobs (list[JobData])

Return type:

list[JobData]

static get_table_name(job_data)[source]#
Parameters:

job_data (JobData)

Return type:

str

static get_group_name(job_data)[source]#
Parameters:

job_data (JobData)

Return type:

str

static is_dataset(f)[source]#

Test if object is an h5py Dataset

Parameters:

f (Any)

Return type:

bool

static is_slurm_job(f)[source]#

Test if object is non-empty or its first element is a Dataset. This is consistent with a slurm job

Parameters:

f (Any)

Return type:

bool

print()[source]#
iterate_database(update_missing=False, job_name=None)[source]#

Yield key (query job) value (list of jobs) pairs of the entire database, or of a single job_name when given.

Parameters:
  • update_missing (bool)

  • job_name (str | None)

Return type:

Generator[tuple[JobData, list[JobData]]]

static iterate_jobs(h5py_obj, categories=None)[source]#

Helper function to recursively iterate through the database and yield job groups with their categories as tuples. Note, jobs are NOT yielded ordred by slurm-id.

Arguments:
h5py_obj:

the current h5py object to check for jobs

categories:

the categories found on the way to the current h5py object as a tuple of strings

Yields:

Tuple of categories and dict of slurm_id to h5py dataset for each job in the database For example two jobs that have the same name and categories: ((“test_job”, “option1=value1”, “option2=value2”), {“123”: <h5py dataset>, “456”: <h5py dataset>}).

Return type:

Generator[tuple[tuple[str, …], dict[str, Group]]]

static print_hdf5(h5py_obj, level=-1, print_full_name=False, print_attrs=True)[source]#

Prints the name and shape of datasets in a H5py HDF5 file.

Parameters:
  • h5py_obj ([h5py.File, h5py.Group]) – the h5py.File or h5py.Group object

  • level (int) – What level of the file tree you are in

  • print_full_name (bool) – If True, the full tree will be printed as the name, e.g. /group0/group1/group2/dataset: … If False, only the current node will be printed, e.g. dataset:

  • print_attrs (bool) – If True: print all attributes in the file

Return type:

None

Configuration#

class slurmise.config.SlurmiseConfiguration(toml_file, create_dirs=True)[source]#

Bases: object

SlurmiseConfiguration class parses and stores TOML configuration files for slurmise.

Parameters:
  • toml_file (Path)

  • create_dirs (bool)

get_runtime_corrector(job_name)[source]#

Return the ResourceCorrector for runtime, using per-job override or global config.

Parameters:

job_name (str)

Return type:

ResourceCorrector

get_memory_corrector(job_name)[source]#

Return the ResourceCorrector for memory, using per-job override or global config.

Parameters:

job_name (str)

Return type:

ResourceCorrector

parse_job_cmd(cmd, job_name=None, slurm_id=None, step_id=None)[source]#

Parse a job data dataset into a JobData object.

Parameters:
  • cmd (str)

  • job_name (str | None)

  • slurm_id (str | None)

  • step_id (str | None)

Return type:

JobData

parse_job_from_dict(variables, job_name, slurm_id=None, step_id=None)[source]#

Parse a job data dataset into a JobData object.

Parameters:
  • variables (dict)

  • job_name (str)

  • slurm_id (str | None)

  • step_id (str | None)

Return type:

JobData

dry_parse(cmd, job_name=None)[source]#
Parameters:
  • cmd (str)

  • job_name (str | None)

add_defaults(job_data)[source]#

Set default resource values on a JobData object before prediction.

Parameters:

job_data (JobData)

Return type:

JobData

get_model_class(job_name)[source]#

Returns the model class a job is using.

Parameters:

job_name (str)

get_sources(job_name)[source]#
Parameters:

job_name (str)

Return type:

dict

slurmise.config.find_config_file()[source]#

Search for a config file if it is not provided. Search first in the current working directory, and if the file does not exist search in the default slurmise base directory.

Return type:

Path

SLURM#

slurmise.slurm.get_current_job_id()[source]#

Return the job ID of the current SLURM job, or None when not inside a SLURM job.

Return type:

str | None

slurmise.slurm.resolve_job_id(slurm_id=None, step_id=None)[source]#

Resolve a job ID, falling back to the current SLURM job’s environment.

Parameters:
  • slurm_id (str | int | None) – The SLURM job ID. If None, the ID is read from the SLURM_JOB_ID environment variable.

  • step_id (str | None) – The SLURM step ID. If provided, it is appended to the job ID as “<slurm_id>.<step_id>”.

Returns:

The resolved job ID, with the step ID appended if provided.

Return type:

str

slurmise.slurm.split_job_id(slurm_id)[source]#

Split a combined “<slurm_id>.<step_id>” string into its parts.

Parameters:

slurm_id (str)

Return type:

tuple[str, str | None]

slurmise.slurm.parse_slurm_job_metadata(slurm_id=None, step_id=None)[source]#

Return a dictionary of metadata for the current SLURM job.

Parameters:
  • slurm_id (str | None) – The SLURM job ID. If None, the function will attempt to retrieve the job ID from the SLURM_JOB_ID environment variable.

  • step_id (str | None) – The SLURM step ID. If None, the function defaults to the last step of the job. If provided, it specifies which step’s metadata to return.

Returns:

A dictionary containing metadata for the specified SLURM job and step.

Return type:

dict

slurmise.slurm.get_slurm_job_sacct(slurm_id)[source]#

Return the JSON output of the sacct command for the given SLURM job.

Parameters:

slurm_id (str)

Return type:

dict

Fit models#

class slurmise.fit.resource_fit.ResourceFit(*, query: 'JobData', last_fit_dsize: 'int' = 0, last_fit_records: 'int' = 0, fit_timestamp: 'datetime.datetime' = <factory>, model_metrics: 'dict' = <factory>, path: 'pathlib.Path | None' = None)[source]#

Bases: object

Parameters:
  • query (JobData)

  • last_fit_dsize (int)

  • last_fit_records (int)

  • fit_timestamp (datetime)

  • model_metrics (dict)

  • path (Path | None)

query: JobData#
last_fit_dsize: int = 0#
last_fit_records: int = 0#
fit_timestamp: datetime#
model_metrics: dict#
path: Path | None = None#
save(model_params=None)[source]#

This method saves the basic information of the model, such as its query, when it was last fit, the dataset size of the latest fit, and the type of the model.

Parameters:

model_params (dict | None)

classmethod load(query=None, path=None, **kwargs)[source]#

This method loads a model from a file. The model is loaded from the path provided, or from the path generated from the query.

Parameters:
  • query (JobData) – The query used to generate the model

  • path (str) – The path to the model

  • kwargs – Additional keyword arguments to pass to the model

Returns:

The model

Return type:

ResourceFit

classmethod mean_percent_error(y_true, y_pred)[source]#

Mean percent error, skipping records whose true value is zero.

Return type:

float

fit(jobs, random_state, **kwargs)[source]#
Parameters:
  • jobs (list[JobData])

  • random_state (RandomState | None)

predict(job, runtime_corrector, memory_corrector, attempt=1)[source]#
Parameters:
  • job (JobData)

  • runtime_corrector (ResourceCorrector)

  • memory_corrector (ResourceCorrector)

  • attempt (int)

Return type:

tuple[JobData, list[str]]

class slurmise.fit.poly_fit.PolynomialFit(*, query: 'JobData', last_fit_dsize: 'int' = 0, last_fit_records: 'int' = 0, fit_timestamp: 'datetime.datetime' = <factory>, model_metrics: 'dict' = <factory>, path: 'pathlib.Path | None' = None, degree: 'int' = 2, runtime_model: 'InitVar[Pipeline | None]' = None, memory_model: 'InitVar[Pipeline | None]' = None)[source]#

Bases: ResourceFit

Parameters:
  • query (JobData)

  • last_fit_dsize (int)

  • last_fit_records (int)

  • fit_timestamp (datetime)

  • model_metrics (dict)

  • path (Path | None)

  • degree (int)

  • runtime_model (dataclasses.InitVar[sklearn.pipeline.Pipeline | None])

  • memory_model (dataclasses.InitVar[sklearn.pipeline.Pipeline | None])

degree: int = 2#
runtime_model: InitVar[Pipeline | None] = None#
memory_model: InitVar[Pipeline | None] = None#
save()[source]#

This method saves the basic information of the model, such as its query, when it was last fit, the dataset size of the latest fit, and the type of the model.

classmethod load(query=None, path=None)[source]#

This method loads a model from a file. The model is loaded from the path provided, or from the path generated from the query.

Parameters:
  • query (JobData) – The query used to generate the model

  • path (str) – The path to the model

  • kwargs – Additional keyword arguments to pass to the model

Returns:

The model

Return type:

ResourceFit

class slurmise.fit.kneighbors_fit.KNNFit(*, query: 'JobData', last_fit_dsize: 'int' = 0, last_fit_records: 'int' = 0, fit_timestamp: 'datetime.datetime' = <factory>, model_metrics: 'dict' = <factory>, path: 'pathlib.Path | None' = None, nneighbors: 'int' = 5, runtime_model: 'InitVar[Pipeline | None]' = None, memory_model: 'InitVar[Pipeline | None]' = None)[source]#

Bases: ResourceFit

Parameters:
  • query (JobData)

  • last_fit_dsize (int)

  • last_fit_records (int)

  • fit_timestamp (datetime)

  • model_metrics (dict)

  • path (Path | None)

  • nneighbors (int)

  • runtime_model (dataclasses.InitVar[sklearn.pipeline.Pipeline | None])

  • memory_model (dataclasses.InitVar[sklearn.pipeline.Pipeline | None])

nneighbors: int = 5#
runtime_model: InitVar[Pipeline | None] = None#
memory_model: InitVar[Pipeline | None] = None#
classmethod load(query=None, path=None)[source]#

This method loads a model from a file. The model is loaded from the path provided, or from the path generated from the query.

Parameters:
  • query (JobData) – The query used to generate the model

  • path (str) – The path to the model

  • kwargs – Additional keyword arguments to pass to the model

Returns:

The model

Return type:

ResourceFit