Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 4, 2025
1 parent c833b07 commit 2d68da5
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 37 deletions.
12 changes: 6 additions & 6 deletions propulate/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def __init__(

def _send_emigrants(self) -> None:
"""Perform migration, i.e. island sends individuals out to other islands."""
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} " f"Generation {self.generation}: EMIGRATION\n"
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: EMIGRATION\n"
# Determine relevant line of migration topology.
assert self.migration_topology is not None
to_migrate = self.migration_topology[self.island_idx, :]
Expand Down Expand Up @@ -160,7 +160,7 @@ def _send_emigrants(self) -> None:
if r == self.island_comm.rank:
continue # No self-talk.
self.island_comm.send(copy.deepcopy(emigrants), dest=r, tag=SYNCHRONIZATION_TAG)
log_string += f"Sent {len(emigrants)} individual(s) {emigrants} to " f"intra-island worker {r} to deactivate.\n"
log_string += f"Sent {len(emigrants)} individual(s) {emigrants} to intra-island worker {r} to deactivate.\n"

# Send emigrants to target island.
departing = copy.deepcopy(emigrants)
Expand All @@ -170,7 +170,7 @@ def _send_emigrants(self) -> None:
for r in dest_island: # Loop over self.propulate_comm destination ranks.
self.propulate_comm.send(copy.deepcopy(departing), dest=r, tag=MIGRATION_TAG)
log_string += (
f"Sent {len(departing)} individual(s) to worker {r-self.island_displs[target_island]} "
f"Sent {len(departing)} individual(s) to worker {r - self.island_displs[target_island]} "
+ f"on target island {target_island}.\n"
)

Expand Down Expand Up @@ -212,15 +212,15 @@ def _receive_immigrants(self) -> None:
RuntimeError
If identical immigrant is already active on target island for real migration.
"""
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} " f"Generation {self.generation}: IMMIGRATION\n"
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: IMMIGRATION\n"
probe_migrants = True
while probe_migrants:
stat = MPI.Status()
probe_migrants = self.propulate_comm.iprobe(source=MPI.ANY_SOURCE, tag=MIGRATION_TAG, status=stat)
log_string += f"Immigrant(s) to receive?...{probe_migrants}\n"
if probe_migrants:
immigrants = self.propulate_comm.recv(source=stat.Get_source(), tag=MIGRATION_TAG)
log_string += f"Received {len(immigrants)} immigrant(s) from global " f"worker {stat.Get_source()}: {immigrants}\n"
log_string += f"Received {len(immigrants)} immigrant(s) from global worker {stat.Get_source()}: {immigrants}\n"
for immigrant in immigrants:
immigrant.migration_steps += 1
assert immigrant.active is True
Expand Down Expand Up @@ -291,7 +291,7 @@ def _check_emigrants_to_deactivate(self) -> bool:

def _deactivate_emigrants(self) -> None:
"""Check for and possibly receive emigrants from other intra-island workers to be deactivated."""
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} " f"Generation {self.generation}: DEACTIVATION\n"
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: DEACTIVATION\n"
probe_sync = True
while probe_sync:
stat = MPI.Status()
Expand Down
17 changes: 8 additions & 9 deletions propulate/pollinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __init__(

def _send_emigrants(self) -> None:
"""Perform migration, i.e. island sends individuals out to other islands."""
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} " f"Generation {self.generation}: EMIGRATION\n"
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: EMIGRATION\n"
# Determine relevant line of migration topology.
assert self.migration_topology is not None
to_migrate = self.migration_topology[self.island_idx, :]
Expand Down Expand Up @@ -174,7 +174,7 @@ def _send_emigrants(self) -> None:
for r in dest_island: # Loop through Propulate world destination ranks.
self.propulate_comm.send(copy.deepcopy(departing), dest=r, tag=MIGRATION_TAG)
log_string += (
f"Sent {len(departing)} individual(s) to worker {r-self.island_displs[target_island]} "
f"Sent {len(departing)} individual(s) to worker {r - self.island_displs[target_island]} "
f"on target island {target_island}.\n"
)

Expand All @@ -192,15 +192,15 @@ def _send_emigrants(self) -> None:
def _receive_immigrants(self) -> None:
"""Check for and possibly receive immigrants send by other islands."""
replace_num = 0
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} " f"Generation {self.generation}: IMMIGRATION\n"
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: IMMIGRATION\n"
probe_migrants = True
while probe_migrants:
stat = MPI.Status()
probe_migrants = self.propulate_comm.iprobe(source=MPI.ANY_SOURCE, tag=MIGRATION_TAG, status=stat)
log_string += f"Immigrant(s) to receive?...{probe_migrants}\n"
if probe_migrants:
immigrants = self.propulate_comm.recv(source=stat.Get_source(), tag=MIGRATION_TAG)
log_string += f"Received {len(immigrants)} immigrant(s) from global " f"worker {stat.Get_source()}: {immigrants}\n"
log_string += f"Received {len(immigrants)} immigrant(s) from global worker {stat.Get_source()}: {immigrants}\n"

# Add immigrants to own population.
for immigrant in immigrants:
Expand All @@ -211,7 +211,7 @@ def _receive_immigrants(self) -> None:
replace_num = 0
if self.island_comm.rank == immigrant.current:
replace_num += 1
log_string += f"Responsible for choosing {replace_num} individual(s) " f"to be replaced by immigrants.\n"
log_string += f"Responsible for choosing {replace_num} individual(s) to be replaced by immigrants.\n"

# Check whether rank equals responsible worker's rank so different intra-island workers
# cannot choose the same individual independently for replacement and thus deactivation.
Expand All @@ -230,7 +230,7 @@ def _receive_immigrants(self) -> None:
continue # No self-talk.
self.island_comm.send(copy.deepcopy(to_replace), dest=r, tag=SYNCHRONIZATION_TAG)
log_string += (
f"Sent {len(to_replace)} individual(s) {to_replace} to " f"intra-island worker {r} for replacement.\n"
f"Sent {len(to_replace)} individual(s) {to_replace} to intra-island worker {r} for replacement.\n"
)

# Deactivate individuals to be replaced in own population.
Expand All @@ -245,7 +245,7 @@ def _receive_immigrants(self) -> None:

def _deactivate_replaced_individuals(self) -> None:
"""Check for and receive individuals from other intra-island workers to be deactivated due to immigration."""
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} " f"Generation {self.generation}: REPLACEMENT\n"
log_string = f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: REPLACEMENT\n"
probe_sync = True
while probe_sync:
stat = MPI.Status()
Expand Down Expand Up @@ -286,8 +286,7 @@ def _deactivate_replaced_individuals(self) -> None:
)
_, num_active = self._get_active_individuals()
log_string += (
f"After synchronization: {num_active}/{len(self.population)} active.\n"
f"{len(self.replaced)} individuals in replaced.\n"
f"After synchronization: {num_active}/{len(self.population)} active.\n{len(self.replaced)} individuals in replaced.\n"
)
log.debug(log_string)

Expand Down
2 changes: 1 addition & 1 deletion propulate/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def __repr__(self) -> str:
loss_str = f"{self.loss}"
else:
loss_str = f"{Decimal(float(self.loss)):.2E}"
return f"[{rep}, loss " + loss_str + f", island {self.island}, worker {self.rank}, " f"generation {self.generation}]"
return f"[{rep}, loss " + loss_str + f", island {self.island}, worker {self.rank}, generation {self.generation}]"

def __iter__(self) -> Generator[str, None, None]:
"""Return standard iterator."""
Expand Down
4 changes: 2 additions & 2 deletions propulate/propagators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def __init__(self, propagators: List[Propagator]) -> None:
ind = inp.parents

raise ValueError(
f"Incompatible combination of {outd} output individuals " f"of {outp} and {ind} input individuals of {inp}."
f"Incompatible combination of {outd} output individuals of {outp} and {ind} input individuals of {inp}."
)
self.propagators = propagators

Expand Down Expand Up @@ -524,7 +524,7 @@ def __call__(self, *inds: Individual) -> Individual: # type: ignore[override]
position[limit] = str(self.rng.choice(self.limits[limit]))
else:
raise ValueError(
"Unknown type of limits. Has to be float for interval, " "int for ordinal, or string for categorical."
"Unknown type of limits. Has to be float for interval, int for ordinal, or string for categorical."
)
ind = Individual(position, self.limits) # Instantiate new individual.
else: # Return first input individual w/o changes otherwise.
Expand Down
20 changes: 7 additions & 13 deletions propulate/propulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ def __init__(
max([x.generation for x in self.population if x.rank == self.island_comm.rank]) + 1
) # Determine generation to be evaluated next from population checkpoint.
if self.island_comm.rank == 0:
log.info(
"Valid checkpoint file found. " f"Resuming from generation {self.generation} of loaded population..."
)
log.info(f"Valid checkpoint file found. Resuming from generation {self.generation} of loaded population...")
except OSError:
self.population = []
if self.island_comm.rank == 0:
Expand Down Expand Up @@ -270,8 +268,7 @@ def loss_gen(individual: Individual) -> Generator[float, None, None]:
if self.surrogate is not None:
if self.surrogate.cancel(last): # Check cancel for each yield.
log.debug(
f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: PRUNING\n"
f"{ind}"
f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: PRUNING\n{ind}"
)
break
ind.loss = float(last) # Set final loss as individual's loss.
Expand Down Expand Up @@ -318,8 +315,7 @@ def loss_fn(individual: Individual) -> float:
def _receive_intra_island_individuals(self) -> None:
"""Check for and possibly receive incoming individuals evaluated by other workers within own island."""
log_string = (
f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: "
f"INTRA-ISLAND SYNCHRONIZATION\n"
f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: INTRA-ISLAND SYNCHRONIZATION\n"
)
probe_ind = True
while probe_ind:
Expand Down Expand Up @@ -412,7 +408,7 @@ def _check_intra_island_synchronization(self, populations: List[List[Individual]
difference = deepdiff.DeepDiff(population, populations[0], ignore_order=True)
if len(difference) == 0:
continue
log.info(f"Island {self.island_idx} Worker {self.island_comm.rank}: Population not synchronized:\n" f"{difference}")
log.info(f"Island {self.island_idx} Worker {self.island_comm.rank}: Population not synchronized:\n{difference}")
synchronized = False
return synchronized

Expand Down Expand Up @@ -501,9 +497,7 @@ def _intra_send_cleanup(self) -> None:

def _dump_checkpoint(self) -> None:
"""Dump checkpoint to file."""
log.debug(
f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: " f"Dumping checkpoint..."
)
log.debug(f"Island {self.island_idx} Worker {self.island_comm.rank} Generation {self.generation}: Dumping checkpoint...")
save_ckpt_file = self.checkpoint_path / f"island_{self.island_idx}_ckpt.pickle"
if os.path.isfile(save_ckpt_file):
try:
Expand Down Expand Up @@ -611,7 +605,7 @@ def summarize(self, top_n: int = 1, debug: int = 1) -> Union[List[Union[List[Ind
log.info(
"###########\n# SUMMARY #\n###########\n"
f"Number of currently active individuals is {num_active}.\n"
f"Expected overall number of evaluations is {self.generations*self.propulate_comm.size}."
f"Expected overall number of evaluations is {self.generations * self.propulate_comm.size}."
)
# Only double-check number of occurrences of each individual for DEBUG level 2.
if debug == 2:
Expand All @@ -638,6 +632,6 @@ def summarize(self, top_n: int = 1, debug: int = 1) -> Union[List[Union[List[Ind
if self.island_comm.rank == 0:
res_str = f"Top {top_n} result(s) on island {self.island_idx}:\n"
for i in range(top_n):
res_str += f"({i+1}): {unique_pop[i]}\n"
res_str += f"({i + 1}): {unique_pop[i]}\n"
log.info(res_str)
return self.propulate_comm.allgather(best)
4 changes: 2 additions & 2 deletions tutorials/surrogate/cifar10_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def ind_loss(
total_train_loss += loss.item()

avg_train_loss = total_train_loss / len(train_loader)
log.info(f"Epoch {epoch+1}: Avg Training Loss: {avg_train_loss}")
log.info(f"Epoch {epoch + 1}: Avg Training Loss: {avg_train_loss}")

# Validation loop
model.eval()
Expand All @@ -402,7 +402,7 @@ def ind_loss(
total_val_loss += loss.item()

avg_val_loss = total_val_loss / len(val_loader)
log.info(f"Epoch {epoch+1}: Avg Validation Loss: {avg_val_loss}")
log.info(f"Epoch {epoch + 1}: Avg Validation Loss: {avg_val_loss}")

yield avg_val_loss

Expand Down
4 changes: 2 additions & 2 deletions tutorials/surrogate/mnist_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def ind_loss(
total_train_loss += loss.item()

avg_train_loss = total_train_loss / len(train_loader)
log.info(f"Epoch {epoch+1}: Avg Training Loss: {avg_train_loss}")
log.info(f"Epoch {epoch + 1}: Avg Training Loss: {avg_train_loss}")

# Validation loop
model.eval()
Expand All @@ -257,7 +257,7 @@ def ind_loss(
total_val_loss += loss.item()

avg_val_loss = total_val_loss / len(val_loader)
log.info(f"Epoch {epoch+1}: Avg Validation Loss: {avg_val_loss}")
log.info(f"Epoch {epoch + 1}: Avg Validation Loss: {avg_val_loss}")

yield avg_val_loss

Expand Down
4 changes: 2 additions & 2 deletions tutorials/torch_ddp_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def ind_loss(params: Dict[str, Union[int, float, str]], subgroup_comm: MPI.Comm)
if batch_idx % log_interval == 0 or batch_idx == len(train_loader) - 1:
log.info(
f"Train Epoch: {epoch} [{batch_idx}/{len(train_loader)} "
f"({100. * batch_idx / len(train_loader):.0f}%)]\tLoss: {loss.item():.6f}"
f"({100.0 * batch_idx / len(train_loader):.0f}%)]\tLoss: {loss.item():.6f}"
)
# ------------ Validation loop ------------
model.eval()
Expand Down Expand Up @@ -370,7 +370,7 @@ def ind_loss(params: Dict[str, Union[int, float, str]], subgroup_comm: MPI.Comm)
val_acc = correct_tensor.item() / num_val_samples_tensor.item()
val_acc_history.append(val_acc)

log.info(f"\nValidation set: Average loss: {val_loss_tensor.item():.4f}, " f"Accuracy: {100. * val_acc:.0f} %)\n")
log.info(f"\nValidation set: Average loss: {val_loss_tensor.item():.4f}, Accuracy: {100.0 * val_acc:.0f} %)\n")

if not set_new_best:
early_stopping_count += 1
Expand Down

0 comments on commit 2d68da5

Please sign in to comment.