Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,20 @@ With that set, `pymatching_only` writes:
The directory is created lazily on the first write, so it is safe to point at
a path that does not yet exist.

**Multiple GPUs.** `local_run.sh` uses every visible GPU, and every rank
reads the same files. Each rank decodes its own contiguous block of shots, in
rank order, and the reported counts and LER are summed over all ranks:

* A positive `test.num_samples` limits the total number of shots per file.
Each rank decodes an equal share of the selected prefix, with one extra
shot on each of the first ranks when the total is uneven.
* The selected prefix must have at least one shot per rank. Use fewer GPUs
for smaller samples.

The `PREDECODER_DECODE_OUTPUT_DIR` arrays are gathered on rank 0 in rank
order before they are written, so each array has one row per decoded shot, in
file order, and covers the same shots as the reported counts.

#### Smoke script

```bash
Expand Down Expand Up @@ -1195,7 +1209,7 @@ Key scalars (as shown in TensorBoard):
- **`Metrics/LER`**: Logical Error Rate on the evaluation target (computed during training-time evaluation). Lower is better.
- Averaging: computed over `cfg.test.num_samples` Monte Carlo shots **per basis** (X and Z).
- Default: `cfg.test.num_samples = 262144` (hardcoded for the current public release).
- Distributed: each rank uses `cfg.test.num_samples // world_size` shots per basis (any remainder is dropped).
- Distributed on generated samples: each rank uses `cfg.test.num_samples // world_size` shots per basis (any remainder is dropped).
- **`Metrics/LER_Reduction_Factor`**: Ratio of post-predecoder LER to baseline LER (a “relative improvement” factor). `>1` means improvement. If both are 0, we log `1.0`.
- Averaging: derived from the same LER evaluation run (same shot count as `Metrics/LER`).
- **`Metrics/PyMatching_Speedup`**: Average PyMatching speedup from the pre-decoder: `latency_baseline / latency_after`. `>1` means faster decoding of PyMatching after pre-decoding.
Expand Down
41 changes: 39 additions & 2 deletions code/data/datapipe_stim.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,31 @@ def __getitem__(self, idx):
)


def _stim_file_shard(
*, file_shots: int, requested_total: int, rank: int, world_size: int, samples_path
) -> tuple:
"""Return the ``[start, stop)`` rows of a Stim sample file that ``rank`` decodes.

Apply the global shot limit before splitting the selected prefix of the file.
The first ranks take one extra row when the total is uneven.
"""
world_size = max(int(world_size), 1)
rank = int(rank)
if not 0 <= rank < world_size:
raise ValueError(f"rank={rank} is out of range for world_size={world_size}")
total = min(requested_total, file_shots) if requested_total > 0 else file_shots
if world_size > 1 and total < world_size:
raise ValueError(
f"Stim sample file {samples_path} has {total} selected shots, "
f"too few to give each of {world_size} ranks at least one shot"
)
if rank == 0 and world_size > 1 and total < file_shots:
print(f"[Stim samples] Decoding {total} of {file_shots} shots in {samples_path}")
count, remainder = divmod(total, world_size)
start = rank * count + min(rank, remainder)
return start, start + count + int(rank < remainder)


class QCDataPipePreDecoder_Memory_from_stim_file(Dataset):
"""
Datapipe for offline inference from Stim detector-sample files.
Expand All @@ -240,6 +265,10 @@ class QCDataPipePreDecoder_Memory_from_stim_file(Dataset):
``sha256()`` is compared against ``metadata['noise_model_sha256']``.
strict_noise: ``True`` (default) raises on noise-fingerprint drift;
``False`` downgrades the failure to a :class:`UserWarning`.
rank, world_size: Distributed shard of the file to expose. Every rank
reads the same file and keeps its own contiguous block, in rank
order. A positive ``num_samples`` limits the total across all ranks.
The first ranks receive one extra row when the split is uneven.
"""

def __init__(
Expand All @@ -254,6 +283,8 @@ def __init__(
code_rotation='XV',
noise_model=None,
strict_noise: bool = True,
rank: int = 0,
world_size: int = 1,
):
self.distance = int(distance)
self.n_rounds = max(int(n_rounds), 1)
Expand Down Expand Up @@ -310,8 +341,14 @@ def __init__(
noise_model_label=noise_label,
strict_noise=bool(strict_noise),
)
if self.requested_num_samples > 0:
dets_and_obs = dets_and_obs[:self.requested_num_samples]
start, stop = _stim_file_shard(
file_shots=int(dets_and_obs.shape[0]),
requested_total=self.requested_num_samples,
rank=rank,
world_size=world_size,
samples_path=samples_path,
)
dets_and_obs = dets_and_obs[start:stop]

self.samples_path = samples_path
self.metadata_path = metadata_path
Expand Down
16 changes: 12 additions & 4 deletions code/data/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def create_datapipe(cfg):
raise ValueError("Invalid datapipe code")

@staticmethod
def create_datapipe_inference(cfg):
def create_datapipe_inference(cfg, *, file_shot_limit=None):
"""Create datapipe for inference using Stim."""
if cfg.code == "surface":
return DatapipeFactory._create_surface_datapipe_inference(cfg)
return DatapipeFactory._create_surface_datapipe_inference(cfg, file_shot_limit)
elif cfg.code == "color":
return DatapipeFactory._create_color_datapipe_inference(cfg)
else:
Expand Down Expand Up @@ -74,7 +74,7 @@ def _create_color_datapipe(cfg):
raise ValueError(f"Datapipe not implemented: {cfg.datapipe}")

@staticmethod
def _create_surface_datapipe_inference(cfg):
def _create_surface_datapipe_inference(cfg, file_shot_limit=None):
"""
Datapipe for inference using Stim.
"""
Expand All @@ -93,8 +93,10 @@ def _create_surface_datapipe_inference(cfg):
try:
import torch.distributed as dist
rank = dist.get_rank() if dist.is_initialized() else 0
world_size = dist.get_world_size() if dist.is_initialized() else 1
except:
rank = 0
world_size = 1

if rank == 0:
print(
Expand All @@ -117,10 +119,16 @@ def _create_surface_datapipe_inference(cfg):
# shapes because they only depend on (distance, n_rounds).
measure_basis = "X"

num_samples = cfg.test.num_samples
dataset_kwargs: dict = {}
if stim_samples_dir:
if file_shot_limit is not None:
num_samples = file_shot_limit
dataset_cls = QCDataPipePreDecoder_Memory_from_stim_file
dataset_kwargs["stim_samples_dir"] = stim_samples_dir
# Every rank reads the same file; shard it so ranks decode disjoint shots.
dataset_kwargs["rank"] = rank
dataset_kwargs["world_size"] = world_size
# PREDECODER_STIM_STRICT_NOISE=0 downgrades the noise-fingerprint
# mismatch from an error to a UserWarning. Default is strict.
strict_env = os.environ.get("PREDECODER_STIM_STRICT_NOISE", "").strip().lower()
Expand All @@ -132,7 +140,7 @@ def _create_surface_datapipe_inference(cfg):
test_dataset = dataset_cls(
distance=cfg.distance,
n_rounds=cfg.n_rounds,
num_samples=cfg.test.num_samples,
num_samples=num_samples,
error_mode=error_mode_value,
p_error=cfg.test.p_error,
measure_basis=measure_basis,
Expand Down
4 changes: 3 additions & 1 deletion code/evaluation/failure_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,9 @@ def decoder_ablation_study(model, device, dist, cfg):
cfg_copy = deepcopy(cfg)
cfg_copy.test.num_samples = samples_per_gpu
cfg_copy.test.meas_basis_test = basis
test_dataset = DatapipeFactory.create_datapipe_inference(cfg_copy)
test_dataset = DatapipeFactory.create_datapipe_inference(
cfg_copy, file_shot_limit=total_samples
)
finally:
torch.set_rng_state(torch_state)
if cuda_state is not None:
Expand Down
93 changes: 69 additions & 24 deletions code/evaluation/logical_error_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,25 @@ def _save_decode_array(output_dir: Optional[Path], basis: str, name: str, arr) -
np.save(output_dir / f"{basis}_{name}.npy", np.asarray(arr))


def _gather_decode_rows(dist, arr):
"""Collect a per-rank decode-output array on rank 0, in rank order.

Every rank decodes its own block of shots and the counts are all-reduced, so
the saved arrays must cover all ranks' shots too. ``arr`` may be ``None`` on a
rank that decoded nothing. This is a collective: call it on every rank. Returns
the concatenated rows on rank 0 (``None`` if no rank had any) and ``None`` on
the other ranks.
"""
if dist.world_size <= 1:
return arr
parts = [None] * dist.world_size if dist.rank == 0 else None
torch.distributed.gather_object(arr, parts, dst=0)
if dist.rank != 0:
return None
parts = [part for part in parts if part is not None]
return np.concatenate(parts, axis=0) if parts else None


def _parse_quant_format(rank: int = 0) -> str:
"""Read and validate the QUANT_FORMAT environment variable.

Expand Down Expand Up @@ -1009,7 +1028,9 @@ def run_inference_and_decode_pre_decoder_memory(model, device, dist, cfg) -> dic
# Create datapipe with reduced sample count per GPU
cfg_copy = deepcopy(cfg)
cfg_copy.test.num_samples = samples_per_gpu
test_dataset = DatapipeFactory.create_datapipe_inference(cfg_copy)
test_dataset = DatapipeFactory.create_datapipe_inference(
cfg_copy, file_shot_limit=total_samples
)
finally:
# Restore random states
torch.set_rng_state(torch_state)
Expand Down Expand Up @@ -1058,11 +1079,24 @@ def run_inference_and_decode_pre_decoder_memory(model, device, dist, cfg) -> dic

baseline_pred = np.asarray(baseline_pred, dtype=np.uint8).reshape(-1, circuit.num_observables)
num_pymatch_errors = int((baseline_pred != stim_obs).sum())
if dist.rank == 0:
_save_decode_array(decode_output_dir, basis, "observables", stim_obs)
_save_decode_array(decode_output_dir, basis, "pymatching_predictions", baseline_pred)
if decode_output_dir is not None:
all_obs = _gather_decode_rows(dist, stim_obs)
all_baseline_pred = _gather_decode_rows(dist, baseline_pred)
if dist.rank == 0:
_save_decode_array(decode_output_dir, basis, "observables", all_obs)
_save_decode_array(
decode_output_dir, basis, "pymatching_predictions", all_baseline_pred
)

if decode_mode == "pymatching_only":
num_shots = int(stim_obs.shape[0])
if dist.world_size > 1:
t_pymatch = torch.tensor(num_pymatch_errors, device=device, dtype=torch.long)
t_n = torch.tensor(num_shots, device=device, dtype=torch.long)
torch.distributed.all_reduce(t_pymatch, op=torch.distributed.ReduceOp.SUM)
torch.distributed.all_reduce(t_n, op=torch.distributed.ReduceOp.SUM)
num_pymatch_errors = int(t_pymatch.item())
num_shots = int(t_n.item())
if dist.rank == 0:
LAST_DEM_TIMING.update(
{
Expand All @@ -1076,8 +1110,7 @@ def run_inference_and_decode_pre_decoder_memory(model, device, dist, cfg) -> dic
f"[DEM Timing] build={dem_build_s:.2f}s decode={dem_decode_s:.2f}s "
f"(basis={basis}, decode_mode=pymatching_only)"
)
return num_pymatch_errors, int(stim_obs.shape[0]
), num_pymatch_errors, float("nan"), float("nan")
return num_pymatch_errors, num_shots, num_pymatch_errors, float("nan"), float("nan")

# --- DataLoader: NO DistributedSampler - each GPU processes ALL of its own samples ---
test_loader_kwargs = dict(cfg.test.dataloader)
Expand Down Expand Up @@ -1381,8 +1414,9 @@ def run_inference_and_decode_pre_decoder_memory(model, device, dist, cfg) -> dic
) / stim_dets.size if stim_dets.size > 0 else 0.0
floor_time_per_round = None
detector_shape = None
final_prediction_rows = [] if dist.rank == 0 and decode_output_dir is not None else None
residual_rows = [] if dist.rank == 0 and decode_output_dir is not None else None
# Collected on every rank; gathered on rank 0 in rank order before saving.
final_prediction_rows = [] if decode_output_dir is not None else None
residual_rows = [] if decode_output_dir is not None else None

t_start = time.perf_counter()
t_model_time = 0.0
Expand Down Expand Up @@ -1576,21 +1610,30 @@ def run_inference_and_decode_pre_decoder_memory(model, device, dist, cfg) -> dic
total_samples = int(t_n.item())
num_pymatch_errors = int(t_pymatch.item())

if dist.rank == 0:
if final_prediction_rows:
_save_decode_array(
decode_output_dir,
basis,
"ising_decoding_pymatching_predictions",
np.concatenate(final_prediction_rows, axis=0),
)
if residual_rows:
_save_decode_array(
decode_output_dir,
basis,
"predecoder_residual_detectors",
np.concatenate(residual_rows, axis=0),
)
if decode_output_dir is not None:
all_final_predictions = _gather_decode_rows(
dist,
np.concatenate(final_prediction_rows, axis=0) if final_prediction_rows else None,
)
all_residuals = _gather_decode_rows(
dist,
np.concatenate(residual_rows, axis=0) if residual_rows else None,
)
if dist.rank == 0:
if all_final_predictions is not None:
_save_decode_array(
decode_output_dir,
basis,
"ising_decoding_pymatching_predictions",
all_final_predictions,
)
if all_residuals is not None:
_save_decode_array(
decode_output_dir,
basis,
"predecoder_residual_detectors",
all_residuals,
)

# Latency: single-shot (batch_size=1, matcher.decode) on a small subset on rank 0 only,
# timed after the main loop for a clean CPU state.
Expand Down Expand Up @@ -1700,7 +1743,9 @@ def compute_syndrome_density_reduction(model, device, dist, cfg) -> dict:
from copy import deepcopy
cfg_copy = deepcopy(cfg)
cfg_copy.test.num_samples = samples_per_gpu
test_dataset = DatapipeFactory.create_datapipe_inference(cfg_copy)
test_dataset = DatapipeFactory.create_datapipe_inference(
cfg_copy, file_shot_limit=total_samples
)

if rank == 0:
print(f"[Syndrome Density] Each GPU processes {len(test_dataset)} samples")
Expand Down
Loading
Loading