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
35 changes: 27 additions & 8 deletions ignition/preparation/tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ def tune(self, *, progress=None, show_progress=False, console=None):
requests = tuple(self._recorded_requests.values())
partial_shards = False
shard_rank = None
shard_ranks = None
if self.tuning_replica is not None:
import torch.distributed as dist
from .tuning_replicas import ReplicaPreparation
Expand All @@ -427,12 +428,27 @@ def tune(self, *, progress=None, show_progress=False, console=None):
ranks = (0,) if self._coordinator is None else self._coordinator.ranks
rank = 0 if self._coordinator is None else ranks.index(dist.get_rank())
self._replica_coordinator = ReplicaPreparation(self.tuning_replica, rank, ranks=ranks)
identities = self._replica_coordinator.gather(self.device.identity)
reports = self._replica_coordinator.gather(
(self.device.identity, _local_tuning_catalog(requests))
)
identities = [item[0] for item in reports]
corresponding = identities[rank::self.tuning_replica.tensor_parallel]
if any(identity != corresponding[0] for identity in corresponding[1:]):
raise ValueError("corresponding tuning GPUs must have identical hardware geometry")
if self.autotune:
self.configure_tuning_shard(self.tuning_replica.index, tuple(range(self.tuning_replica.replicas)))
share_across_ranks = (
self.tuning_replica.tensor_parallel > 1
and all(item == reports[0] for item in reports[1:])
)
self._replica_coordinator.share_across_ranks = share_across_ranks
if share_across_ranks:
self._sharded = True
shard_rank = self.tuning_replica.index * self.tuning_replica.tensor_parallel + rank
shard_ranks = tuple(range(self.tuning_replica.processes))
else:
shard_rank = self.tuning_replica.index
shard_ranks = tuple(range(self.tuning_replica.replicas))
self.configure_tuning_shard(shard_rank, shard_ranks)
elif self.autotune and self._coordinator is not None:
import torch.distributed as dist

Expand All @@ -446,7 +462,8 @@ def tune(self, *, progress=None, show_progress=False, console=None):
self._coordinator.share_selections(self.selection_cache())
if self._sharded:
shard_rank = dist.get_rank()
self.configure_tuning_shard(shard_rank, self._coordinator.ranks)
shard_ranks = self._coordinator.ranks
self.configure_tuning_shard(shard_rank, shard_ranks)
else:
partial_shards = True
scratch_arena = self._reserve_scratch(requests)
Expand Down Expand Up @@ -502,10 +519,10 @@ def update(status):
if self.is_cancelled():
raise RuntimeError("b12x tuning was cancelled before serving preparation completed")
if (self._coordinator is not None and len(self._coordinator.ranks) > 1
and self._replica_coordinator is None):
and (self.tuning_replica is None or self.tuning_replica.index == 0)):
configs, changed = self._coordinator.reconcile_selections(requests)
if changed:
if self._sharded:
if shard_ranks is not None:
self.configure_tuning_shard(0, (0,))
try:
for job in self._jobs(requests):
Expand All @@ -514,8 +531,8 @@ def update(status):
autotune=self.autotune,
selected_configs={plan: config for plan, config in configs.items() if plan in plans}))
finally:
if self._sharded:
self.configure_tuning_shard(shard_rank, self._coordinator.ranks)
if shard_ranks is not None:
self.configure_tuning_shard(shard_rank, shard_ranks)
selections = []
coverage = {name: value for result in results for name, value in result.coverage.items()}
for request in requests:
Expand All @@ -526,7 +543,9 @@ def update(status):
"source": selection.source,
"config": request.plan.contract.config_payload(selection.config).to_dict(),
"coverage": FrozenMapping(coverage.get(request.name, {})).to_dict()})
if self._coordinator is not None and (self._sharded or self._coordinator.spans_hosts):
if (self._coordinator is not None
and (self.tuning_replica is None or self.tuning_replica.index == 0)
and (self._sharded or self._coordinator.spans_hosts)):
self._coordinator.agree_selections(selections)
self._tune_status = "finalizing"
if self._scratch_arena is not None:
Expand Down
24 changes: 17 additions & 7 deletions ignition/preparation/tuning_replicas.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,29 @@ class TuningComplete(BaseException):
"""A preparation-only replica finished tuning and may release its model."""


def authorization(states, *, replica, rank, tensor_parallel, ranks):
"""Release replica-local collectives and corresponding-rank candidate exchanges."""
peers = states[rank::tensor_parallel]
def authorization(states, *, replica, rank, tensor_parallel, ranks, share_across_ranks=False):
"""Release replica-local collectives and local candidate exchanges across replicas or all ranks."""
peers = states if share_across_ranks else states[rank::tensor_parallel]
if all(item[3] is not None for item in peers):
return {"cache": tuple(item[3] for item in peers)}
if all(item[4] for item in peers):
from .collectives import _tuning_results

keys = [sorted(value.key for value in item[4]) for item in peers]
if any(value != keys[0] for value in keys[1:]):
raise RuntimeError("corresponding tuning ranks declared different candidate races")
raise RuntimeError(
"tuning ranks declared different candidate races"
if share_across_ranks
else "corresponding tuning ranks declared different candidate races"
)
results = _tuning_results(value for item in peers for value in item[4])
return {"tuning": results[0] if len(results) == 1 else results}
if any(item[0] for item in peers) and any(item[3] is not None or item[4] for item in peers):
raise RuntimeError("a tuning replica finished before its corresponding rank")
raise RuntimeError(
"a tuning rank finished before its peers"
if share_across_ranks
else "a tuning replica finished before its corresponding rank"
)
local = states[replica * tensor_parallel:(replica + 1) * tensor_parallel]
if all(item[5] is not None for item in local):
if any(item[5].ranks != tuple(ranks) for item in local):
Expand All @@ -63,10 +71,11 @@ def authorization(states, *, replica, rank, tensor_parallel, ranks):


class ReplicaPreparation:
def __init__(self, role, rank, *, ranks):
def __init__(self, role, rank, *, ranks, share_across_ranks=False):
import torch.distributed as dist

self.role, self.rank, self.ranks = role, rank, tuple(ranks)
self.share_across_ranks = share_across_ranks
self.store = dist.FileStore(role.rendezvous, role.processes)
# This group is independent of torch.distributed's default TP group.
self.group = dist.ProcessGroupGloo(self.store, role.index * role.tensor_parallel + rank,
Expand Down Expand Up @@ -112,7 +121,8 @@ def prepare(self, session, requests, *, progress, autotune):
if all(item[0] for item in states):
return job.result()
allowed = authorization(states, replica=self.role.index, rank=self.rank,
tensor_parallel=self.role.tensor_parallel, ranks=self.ranks)
tensor_parallel=self.role.tensor_parallel, ranks=self.ranks,
share_across_ranks=self.share_across_ranks)
except BaseException as failure:
if job is not None:
try:
Expand Down
Loading
Loading