Shard Utilities¶
mmirage.shard_process¶
Main entry point for processing a single shard.
Main script for processing dataset shards with MMIRAGE.
Supports both text-only and multimodal (vision-language) processing.
- mmirage.shard_process.rewrite_batch(batch, mapper, renderer, image_base_path=None)[source]¶
Rewrite a batch of samples by applying transformations. :param batch: Dictionary mapping column names to lists of values. :param mapper: MMIRAGEMapper for processing transformations. :param renderer: TemplateRenderer for generating output. :param image_base_path: Optional base directory for resolving relative image paths.
- Returns:
Dictionary mapping output keys to lists of rendered values.
- Raises:
ValueError – If variables are not computable given the configuration.
- Parameters:
mapper (MMIRAGEMapper)
renderer (TemplateRenderer)
image_base_path (str | None)
- Return type:
mmirage.shard_utils¶
Low-level helpers for shard state management, atomic saves, and status markers.
Utility functions for shard and merge processing.
This module contains helper functions for dataset sharding, state management, and file operations used in the MMIRAGE shard processing pipeline.
- mmirage.shard_utils.format_duration(seconds)[source]¶
Format a duration given in seconds as a human-readable string.
- class mmirage.shard_utils.ShardStats(runtime_seconds=None, rows_processed=None, throughput_rows_per_sec=None, gpu_util_mean=None, gpu_util_min=None, gpu_util_max=None, gpu_util_samples=None, input_tokens=None, output_tokens=None, num_gpus=None, model_load_seconds=None)[source]¶
Bases:
objectPer-shard benchmark statistics recorded at completion.
- Parameters:
runtime_seconds (float | None)
rows_processed (int | None)
throughput_rows_per_sec (float | None)
gpu_util_mean (float | None)
gpu_util_min (float | None)
gpu_util_max (float | None)
gpu_util_samples (int | None)
input_tokens (int | None)
output_tokens (int | None)
num_gpus (int | None)
model_load_seconds (float | None)
- classmethod from_dict(data)[source]¶
Build a ShardStats from a JSON payload, or return None if data is missing.
- Parameters:
- Return type:
ShardStats | None
- class mmirage.shard_utils.GpuUtilizationPoller(interval_seconds=5.0, gpu_indices=None)[source]¶
Bases:
objectPolls
nvidia-smiin a background daemon thread.Usage:
poller = GpuUtilizationPoller() poller.start() # ... do work ... gpu_info = poller.stop() # {"mean": 85.2, "min": 70.0, "max": 98.0, "samples": 24}
If
nvidia-smiis unavailable all values areNoneand samples is 0.
- class mmirage.shard_utils.ShardStatus(status='unknown', retry_count=0, shard_id=None, started_at=None, finished_at=None, error=None, hostname=None, pid=None, slurm_job_id=None, slurm_array_task_id=None, datasets=None, stats=None)[source]¶
Bases:
objectTyped representation of the shard status.json payload.
- Parameters:
- stats: ShardStats | None = None¶
- class mmirage.shard_utils.MergeReport(dataset_name, input_dir, output_dir, used_shards, merged_rows, skipped_invalid_dirs, skipped_zero_rows)[source]¶
Bases:
objectSummary of a merge operation for one dataset directory.
- Parameters:
- mmirage.shard_utils.shard_state_dir(shard_idx, state_root)[source]¶
Get central state directory for a logical shard.
mmirage.merge_shards¶
Dataset shard merging logic.
Merge processed dataset shards.
- mmirage.merge_shards.merge_dataset_dir(dataset_dir, output_dir)[source]¶
Merge one dataset directory containing shard_* folders.
- Parameters:
- Returns:
MergeReport with summary details.
- Return type:
- mmirage.merge_shards.merge_input_dir(input_dir, output_dir)[source]¶
Merge all shard datasets found under an input directory.
The input can be either: - one dataset dir containing shard_* folders directly - a parent dir containing multiple dataset subdirectories, each with shard_*
- Parameters:
- Return type:
- mmirage.merge_shards.merge_from_config(cfg, output_root=None)[source]¶
Merge shard outputs described in config.loading_params.datasets.
- Parameters:
cfg (MMirageConfig) – Loaded MMIRAGE config.
output_root (str | None) – Optional destination root. If omitted, each dataset writes into <dataset.output_dir>/merged.
- Returns:
Merge reports for each dataset entry.
- Return type:
mmirage.cli_utils.status¶
Shard status checking and retry helpers.
Shard status and retry helpers for the MMIRAGE CLI.
- class mmirage.cli_utils.status.ShardSummary(total, successful, running, failed, max_retries_exceeded)[source]¶
Bases:
objectCompact status summary for shard execution.
- mmirage.cli_utils.status.max_allowed_attempts(max_retries)[source]¶
Return max allowed total attempts for a shard.
Total attempts = initial attempt + max_retries.
- mmirage.cli_utils.status.is_retry_budget_exceeded(attempt_count, max_retries)[source]¶
Return whether a shard has exceeded the retry budget.
- mmirage.cli_utils.status.get_shard_status(state_dir)[source]¶
Read the current status and attempt counter for a shard.
- mmirage.cli_utils.status.check_failed_shards(cfg)[source]¶
Return retryable failed shards and a compact summary.
- Parameters:
cfg (MMirageConfig)
- Return type:
Tuple[List[int], ShardSummary]
- mmirage.cli_utils.status.confirm_retry(count, confirm_mode)[source]¶
Return whether retry submission is confirmed.
Modes: - prompt: ask the user interactively - yes: submit without prompting
- mmirage.cli_utils.status.status_exit_code(failed_shards, summary)[source]¶
Map shard status to an exit code.
- Parameters:
summary (ShardSummary)
- Return type:
- mmirage.cli_utils.status.submit_failed_shards(cfg, config_path, failed_shards, confirm_mode, collect_stats=False)[source]¶
Submit retry jobs for failed shards when requested.
- mmirage.cli_utils.status.collect_bench_stats(cfg)[source]¶
Collect per-shard benchmark statistics and compute aggregate totals.
Returns a dict with two keys:
per_shard: list of dicts, one per shard, each containing the fullShardStatuspayload plus a flattenedstatssub-dict.aggregate: rolled-up totals across all completed shards.
Shards without
stats(e.g. still running or from older runs) are included inper_shardbut excluded from aggregate calculations.- Parameters:
cfg (MMirageConfig)
- Return type:
mmirage.cli_utils.slurm¶
SLURM submission and job monitoring helpers.
SLURM helpers for the MMIRAGE CLI.
- mmirage.cli_utils.slurm.build_sbatch_script(cfg, config_path, collect_stats=False, shard_ids=None)[source]¶
Build the sbatch payload for shard processing.
- mmirage.cli_utils.slurm.submit_slurm_job(cfg, config_path, shard_ids=None, collect_stats=False)[source]¶
Submit a SLURM shard-processing job and return its job ID.
- mmirage.cli_utils.slurm.wait_for_slurm_job(job_id, cfg)[source]¶
Wait for a SLURM job array to leave the queue.
- Parameters:
job_id (int)
cfg (MMirageConfig)
- Return type:
None
- mmirage.cli_utils.slurm.require_slurm(cfg, command_name)[source]¶
Ensure command can only run in SLURM mode.
- Parameters:
cfg (MMirageConfig)
command_name (str)
- Return type:
mmirage.cli_utils.runtime¶
Runtime environment setup helpers.
Runtime/path helpers for the MMIRAGE CLI.
- mmirage.cli_utils.runtime.non_empty_path(value)[source]¶
Reject a path argument given as an empty or blank string.
An empty value reads as “flag not passed” further down, which would turn a dry run into a real submission.
- mmirage.cli_utils.runtime.expand_path(path, project_root=None)[source]¶
Expand environment variables, user home and relative paths.
- mmirage.cli_utils.runtime.get_project_root(cfg)[source]¶
Return the configured project root, or the current working directory.
- Parameters:
cfg (MMirageConfig)
- Return type:
- mmirage.cli_utils.runtime.create_directories(paths)[source]¶
Create directories if they do not already exist.
- mmirage.cli_utils.runtime.validate_edf_env_path(cfg)[source]¶
Validate the optional EDF environment file path.
- Parameters:
cfg (MMirageConfig)
- Return type:
None
- mmirage.cli_utils.runtime.add_file_logging(log_file, level)[source]¶
Add a file handler so logs are also written to disk.
- mmirage.cli_utils.runtime.setup_runtime(cfg, log_level)[source]¶
Initialize runtime-level logging.
- Parameters:
cfg (MMirageConfig)
log_level (str)
- Return type:
None