-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
session.py
1119 lines (880 loc) · 36.9 KB
/
session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import functools
import logging
import os
import platform
import queue
import sys
import threading
import time
import warnings
from dataclasses import dataclass
from datetime import datetime
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Set, Type
import ray
from ray.air._internal.session import _get_session
from ray.air._internal.util import RunnerThread, StartTraceback
from ray.air.constants import (
_ERROR_FETCH_TIMEOUT,
_RESULT_FETCH_TIMEOUT,
SESSION_MISUSE_LOG_ONCE_KEY,
TIME_THIS_ITER_S,
TIMESTAMP,
)
from ray.data import Dataset
from ray.train import Checkpoint
from ray.train._internal.accelerator import Accelerator
from ray.train._internal.storage import StorageContext
from ray.train.constants import (
CHECKPOINT_DIR_NAME,
DETAILED_AUTOFILLED_KEYS,
RAY_CHDIR_TO_TRIAL_DIR,
TIME_TOTAL_S,
WORKER_HOSTNAME,
WORKER_NODE_IP,
WORKER_PID,
)
from ray.train.error import SessionMisuseError
from ray.util.annotations import DeveloperAPI, PublicAPI
from ray.util.debug import log_once
from ray.util.placement_group import _valid_resource_shape
from ray.util.scheduling_strategies import (
PlacementGroupSchedulingStrategy,
SchedulingStrategyT,
)
if TYPE_CHECKING:
from ray.data import DataIterator
from ray.tune.execution.placement_groups import PlacementGroupFactory
logger = logging.getLogger(__name__)
@dataclass
class TrialInfo:
"""The trial information to propagate to TrainSession."""
name: str
id: str
resources: Dict[str, float]
logdir: str
driver_ip: str
experiment_name: Optional[str] = None
class _FutureTrainingResult:
"""A future that will be resolved to a `_TrainingResult`.
This is needed for specific schedulers such as PBT that schedule saves.
This wrapper should be removed after refactoring PBT to not schedule saves anymore.
"""
def __init__(self, future: ray.ObjectRef):
self.future = future
def resolve(self, block: bool = True) -> Optional["_TrainingResult"]:
"""Resolve into ``_TrainingResult``.
This will return None for function trainables if no checkpoint has been
saved before.
"""
if block:
timeout = None
else:
timeout = 1e-9
try:
return ray.get(self.future, timeout=timeout)
except TimeoutError:
# Not ready, yet
pass
except Exception as exc:
logger.error(f"Error resolving result: {exc}")
class _TrainingResult:
"""A (checkpoint, metrics) result reported by the user."""
def __init__(self, checkpoint: Optional[Checkpoint], metrics: Dict[str, Any]):
self.checkpoint = checkpoint
self.metrics = metrics
def __repr__(self) -> str:
return f"TrainingResult(checkpoint={self.checkpoint}, metrics={self.metrics})"
# TODO(xwjiang): This needs a better name.
@DeveloperAPI
class _TrainSession:
"""Holds information for training on each worker."""
def __init__(
self,
training_func: Callable,
world_rank: int,
local_rank: int,
node_rank: int,
local_world_size: int,
world_size: int,
trial_info: Optional[TrialInfo] = None,
dataset_shard: Optional[Dict[str, Dataset]] = None,
metadata: Dict[str, Any] = None,
checkpoint: Optional[Checkpoint] = None,
detailed_autofilled_metrics: bool = False,
storage: Optional[StorageContext] = None,
synchronous_result_reporting: bool = False,
):
# `synchronous_result_reporting` refers to whether or not the
# training function is immediately unblocked to continue running
# after the main thread receives its result.
# Ex 1: For 2 Ray Train workers with synchronous_result_reporting=True,
# the worker that produces a result first will immediately will continue
# onto the next iteration.
# Ex 2: For a Tune function Trainable with `synchronous_result_reporting=False`,
# training will only continue with an explicit call to `session.get_next`.
# Synchronous reporting in example 2 is needed for Tune schedulers to
# be able to stop the execution of the training function at will,
# for advanced pausing schedulers (PBT, BOHB) and actor reuse.
self.synchronous_result_reporting = synchronous_result_reporting
# Ray Train worker properties
self.dataset_shard = dataset_shard
self.metadata = metadata
self.world_rank = world_rank
self.local_rank = local_rank
self.node_rank = node_rank
self.local_world_size = local_world_size
self.world_size = world_size
assert storage
logger.debug(f"StorageContext on SESSION (rank={world_rank}):\n{storage}")
# NOTE: `reset` will initialize many properties needed to start running the
# training_func as a thread.
self.reset(
training_func=training_func,
trial_info=trial_info,
storage=storage,
loaded_checkpoint=checkpoint,
)
# Autofilled metrics attributes.
self.detailed_autofilled_metrics = detailed_autofilled_metrics
self.last_report_time = time.time()
self.iteration = 0
self.time_total = 0.0
self.local_ip = self.get_current_ip()
self.accelerator = None
self._state = {}
def get_state(self, key: str) -> Any:
return self._state.get(key)
def set_state(self, key: str, value: Any):
self._state[key] = value
def get_current_ip(self):
self.local_ip = ray.util.get_node_ip_address()
return self.local_ip
def start(self):
"""Starts the training thread."""
self.training_started = True
self.training_thread.start()
def reset(
self,
training_func: Callable,
trial_info: TrialInfo,
storage: StorageContext,
loaded_checkpoint=None,
):
# This lock is used to control the execution of the training thread.
self.continue_lock = threading.Semaphore(0)
# This event is used to signal the training thread to stop.
self.stop_event = threading.Event()
# Queue for sending results across threads.
self.result_queue = queue.Queue(1)
# Queue for raising exceptions from runner thread to main thread.
# The error queue has a max size of one to prevent stacking error and force
# error reporting to block until finished.
self.error_queue = queue.Queue(1)
# The Thread object that is running the training function.
self.training_thread = RunnerThread(
target=training_func, daemon=True, error_queue=self.error_queue
)
# Possibly override with new state
self.trial_info = trial_info
self.storage = storage
self.loaded_checkpoint = loaded_checkpoint
# Reset state
self._state = {}
self.ignore_report = False
self.training_started = False
self._first_report = True
# Change the working directory to a special trial folder.
# This is to ensure that all Ray Train workers have a common working directory.
os.makedirs(storage.trial_working_directory, exist_ok=True)
if bool(int(os.environ.get(RAY_CHDIR_TO_TRIAL_DIR, "1"))):
logger.debug(
f"Changing the working directory to: {storage.trial_working_directory}"
)
os.chdir(storage.trial_working_directory)
def pause_reporting(self):
"""Ignore all future ``session.report()`` calls."""
self.ignore_report = True
def finish(self, timeout: Optional[float] = None) -> Optional[Any]:
"""Finishes the training thread.
Raises any Exception from training.
"""
# Set the stop event for the training thread to gracefully exit.
self.stop_event.set()
# Release the lock so that training thread can process this event.
self.continue_lock.release()
# Force a final (blocking) sync of artifacts in the trial path to storage.
self.storage.persist_artifacts(force=True)
# Wait for training to finish.
# This will raise any errors that occur during training, including SystemError
# This returns the result of the training function.
output = None
if self.training_started:
output = self.training_thread.join(timeout=timeout)
return output
def get_next(self) -> Optional[_TrainingResult]:
"""Gets the next ``_TrainingResult`` from the result queue.
If the result queue is empty, then this function returns ``None``.
"""
if not self.training_started:
raise RuntimeError("Please call start before calling get_next.")
if self.synchronous_result_reporting:
# There's no need to release the lock on the first report
# since `start` already started the training thread.
if not self._first_report:
# Release the lock to trigger training to continue,
# until the next call to report.
self.continue_lock.release()
self._first_report = False
result = None
# While training is still ongoing, attempt to get the result.
while result is None and self.training_thread.is_alive():
try:
result = self.result_queue.get(
block=True, timeout=_RESULT_FETCH_TIMEOUT
)
except queue.Empty:
pass
# If no result was found, then the runner must no longer be alive.
if result is None:
# Try one last time to fetch results in case results were
# reported in between the time of the last check and the
# termination of the thread runner.
try:
result = self.result_queue.get(
block=False, timeout=_RESULT_FETCH_TIMEOUT
)
except queue.Empty:
pass
# check if error occurred inside the thread runner.
if result is None:
# only raise an error from the runner if all results are consumed
self._report_thread_runner_error(block=True)
else:
if not self.error_queue.empty():
logger.debug(
(
"Runner error waiting to be raised in main thread. "
"Logging all available results first."
)
)
if not self.synchronous_result_reporting:
# At this point, the training thread has reached
# the `train.report` and is blocked there.
# If performing asynchronous result reporting,
# release the lock to allow each worker to keep training
# immediately after the coordinator fetches their result.
self.continue_lock.release()
# Return None if there are no more results to fetch.
return result
def _auto_fill_metrics(self, result: dict) -> dict:
"""Add autofilled metrics and update attributes."""
current_time = time.time()
current_datetime = datetime.now()
if TIME_THIS_ITER_S in result:
time_this_iter = result[TIME_THIS_ITER_S]
else:
time_this_iter = current_time - self.last_report_time
self.iteration += 1
self.time_total += time_this_iter
self.last_report_time = current_time
auto_filled_metrics = {
TIMESTAMP: int(time.mktime(current_datetime.timetuple())),
TIME_TOTAL_S: self.time_total,
WORKER_PID: os.getpid(),
WORKER_HOSTNAME: platform.node(),
WORKER_NODE_IP: self.local_ip,
}
if not self.detailed_autofilled_metrics:
auto_filled_metrics = {
k: v
for k, v in auto_filled_metrics.items()
if k not in DETAILED_AUTOFILLED_KEYS
}
result = result.copy()
result.update(auto_filled_metrics)
return result
def _auto_fill_checkpoint_metrics(self, result: dict) -> dict:
"""Add autofilled metrics and update attributes."""
current_datetime = datetime.now()
auto_filled_metrics = {
TIMESTAMP: int(time.mktime(current_datetime.timetuple()))
}
result = result.copy()
result.update(auto_filled_metrics)
return result
def _report_thread_runner_error(self, block=False):
try:
e = self.error_queue.get(block=block, timeout=_ERROR_FETCH_TIMEOUT)
raise StartTraceback from e
except queue.Empty:
pass
def _report_training_result(self, training_result: _TrainingResult) -> None:
"""Place a training result on the result queue for the main thread to process,
then block until the main thread signals that training should continue.
NOTE: This is used internally to report results from Train to Tune
without persisting checkpoints to storage 2 times.
`report` is the public API that directly persists to storage, which
should only be called by user code.
"""
if training_result.checkpoint:
# NOTE: This populates `train.get_checkpoint`
self.loaded_checkpoint = training_result.checkpoint
# Add result to a thread-safe queue.
self.result_queue.put(training_result, block=True)
# Acquire lock to stop the training thread until main thread
# triggers resume.
self.continue_lock.acquire()
# If the trial should be terminated, exit gracefully.
# NOTE: This is only really useful if `synchronous_result_reporting=True`.
# Otherwise, the lock is immediately released on reporting, and this
# check is skipped before the main thread decides to set the stop event.
if self.stop_event.is_set():
self.stop_event.clear()
sys.exit(0)
def report(self, metrics: Dict, checkpoint: Optional[Checkpoint] = None) -> None:
# Special case: early fail for Torch tensors
if "torch" in sys.modules:
from ray.air._internal.torch_utils import contains_tensor
if contains_tensor(metrics):
raise ValueError(
"Passing objects containg Torch tensors as metrics "
"is not supported as it will throw an exception on "
"deserialization. You can either convert the tensors "
"to Python objects or report a `train.Checkpoint` "
"with `ray.train.report` to store your Torch objects."
)
if self.ignore_report:
return
metrics = self._auto_fill_metrics(metrics)
persisted_checkpoint = None
if checkpoint:
self.storage._update_checkpoint_index(metrics)
# Persist the reported checkpoint files to storage.
persisted_checkpoint = self.storage.persist_current_checkpoint(checkpoint)
metrics[CHECKPOINT_DIR_NAME] = self.storage.checkpoint_dir_name
else:
metrics[CHECKPOINT_DIR_NAME] = None
# Persist trial artifacts to storage.
force_artifact_sync = (
persisted_checkpoint
and self.storage.sync_config.sync_artifacts_on_checkpoint
)
self.storage.persist_artifacts(force=force_artifact_sync)
# Set additional user metadata from the Trainer.
if persisted_checkpoint and self.metadata:
user_metadata = persisted_checkpoint.get_metadata()
for k, v in self.metadata.items():
# Update keys not already set by the user. This gives user-set keys
# precedence over keys set at the Trainer level.
if k not in user_metadata:
user_metadata[k] = v
persisted_checkpoint.set_metadata(user_metadata)
result = _TrainingResult(checkpoint=persisted_checkpoint, metrics=metrics)
self._report_training_result(result)
@property
def experiment_name(self) -> str:
return self.trial_info.experiment_name
@property
def trial_name(self) -> str:
return self.trial_info.name
@property
def trial_id(self) -> str:
return self.trial_info.id
@property
def trial_resources(self) -> "PlacementGroupFactory":
return self.trial_info.resources
@property
def trial_dir(self) -> str:
return self.trial_info.logdir
def get_dataset_shard(
self,
dataset_name: Optional[str] = None,
) -> Optional["DataIterator"]:
shard = self.dataset_shard
if shard is None:
warnings.warn(
"No dataset passed in. Returning None. Make sure to "
"pass in a Dataset to Trainer.run to use this "
"function."
)
elif isinstance(shard, dict):
if not dataset_name:
raise RuntimeError(
"Multiple datasets were passed into ``Trainer``, "
"but no ``dataset_name`` is passed into "
"``get_dataset_shard``. Please specify which "
"dataset shard to retrieve."
)
return shard.get(dataset_name)
return shard
# Cache of resource dicts that have been checked by the launch hook already.
_checked_resources: Set[frozenset] = set()
_session: Optional[_TrainSession] = None
def _tune_task_and_actor_launch_hook(
fn, resources: Dict[str, float], strategy: Optional[SchedulingStrategyT]
):
"""Launch hook to catch nested tasks that can't fit in the placement group.
This gives users a nice warning in case they launch a nested task in a Tune trial
without reserving resources in the trial placement group to fit it.
"""
# Already checked, skip for performance reasons.
key = frozenset({(k, v) for k, v in resources.items() if v > 0})
if not key or key in _checked_resources:
return
# No need to check if placement group is None.
if (
not isinstance(strategy, PlacementGroupSchedulingStrategy)
or strategy.placement_group is None
):
return
# Check if the resource request is targeting the current placement group.
cur_pg = ray.util.get_current_placement_group()
if not cur_pg or strategy.placement_group.id != cur_pg.id:
return
_checked_resources.add(key)
# Check if the request can be fulfilled by the current placement group.
pgf = get_trial_resources()
if pgf.head_bundle_is_empty:
available_bundles = cur_pg.bundle_specs[0:]
else:
available_bundles = cur_pg.bundle_specs[1:]
# Check if the request can be fulfilled by the current placement group.
if _valid_resource_shape(resources, available_bundles):
return
if fn.class_name:
submitted = "actor"
name = fn.module_name + "." + fn.class_name + "." + fn.function_name
else:
submitted = "task"
name = fn.module_name + "." + fn.function_name
# Normalize the resource spec so it looks the same as the placement group bundle.
main_resources = cur_pg.bundle_specs[0]
resources = {k: float(v) for k, v in resources.items() if v > 0}
raise RuntimeError(
f"No trial resources are available for launching the {submitted} `{name}`. "
"To resolve this, specify the Tune option:\n\n"
"> resources_per_trial=tune.PlacementGroupFactory(\n"
f"> [{main_resources}] + [{resources}] * N\n"
"> )\n\n"
f"Where `N` is the number of slots to reserve for trial {submitted}s. "
"If you are using a Ray training library, there might be a utility function "
"to set this automatically for you. For more information, refer to "
"https://docs.ray.io/en/latest/tune/tutorials/tune-resources.html"
)
def init_session(*args, **kwargs) -> None:
global _session
if _session:
raise ValueError(
"A Train session is already in use. Do not call "
"`init_session()` manually."
)
# Setup hooks for generating placement group resource deadlock warnings.
from ray import actor, remote_function
if "TUNE_DISABLE_RESOURCE_CHECKS" not in os.environ:
actor._actor_launch_hook = _tune_task_and_actor_launch_hook
remote_function._task_launch_hook = _tune_task_and_actor_launch_hook
_session = _TrainSession(*args, **kwargs)
def get_session() -> Optional[_TrainSession]:
return _session
def shutdown_session():
"""Shuts down the initialized session."""
global _session
_session = None
def _raise_accelerator_session_misuse():
"""Raises a SessionMisuseError because a utility function was used improperly."""
raise SessionMisuseError(
"prepare/accelerate utility functions should be called inside a training "
"function executed by `Trainer.run`"
)
def get_accelerator(default_accelerator_cls: Type[Accelerator]) -> Accelerator:
"""The accelerator for this training session.
If an accelerator has not been set, then this method will construct an
accelerator using the provided accelerator class.
Raises:
SessionMisuseError: if the session is uninitialized.
"""
session = get_session()
if session is None:
_raise_accelerator_session_misuse()
if session.accelerator is None:
session.accelerator = default_accelerator_cls()
return session.accelerator
def set_accelerator(accelerator: Accelerator) -> None:
"""Sets the accelerator for this training session.
Args:
accelerator: The accelerator to use for training.
Raises:
SessionMisuseError: if the session is unitialized.
RuntimeError: if the accelerator has already been set.
"""
session = get_session()
if session is None:
_raise_accelerator_session_misuse()
if session.accelerator is not None:
raise RuntimeError("Cannot change accelerator once set.")
session.accelerator = accelerator
def _warn_session_misuse(default_value: Any = None):
"""Warns if fn is being used outside of session and returns ``default_value``."""
def inner(fn: Callable):
fn_name = fn.__name__
@functools.wraps(fn)
def wrapper(*args, **kwargs):
session = _get_session()
if not session:
if log_once(f"{SESSION_MISUSE_LOG_ONCE_KEY}-{fn_name}"):
warnings.warn(
f"`{fn_name}` is meant to only be "
"called inside a function that is executed by a Tuner"
f" or Trainer. Returning `{default_value}`."
)
return default_value
return fn(*args, **kwargs)
return wrapper
return inner
@PublicAPI(stability="stable")
@_warn_session_misuse()
def report(metrics: Dict, *, checkpoint: Optional[Checkpoint] = None) -> None:
"""Report metrics and optionally save a checkpoint.
If a checkpoint is provided, it will be
:ref:`persisted to storage <persistent-storage-guide>`.
If this is called in multiple distributed training workers:
- Only the metrics reported by the rank 0 worker will be tracked by Ray Train.
See :ref:`the metrics logging guide <train-monitoring-and-logging>`.
- A checkpoint will be registered as long as one or more workers reports
checkpoint that is not None.
See the :ref:`checkpointing guide <train-dl-saving-checkpoints>`.
- Checkpoints from multiple workers will be merged into one directory
in persistent storage.
See :ref:`the distributed checkpointing guide <train-distributed-checkpointing>`.
.. note::
Each invocation of this method will automatically increment the underlying
``training_iteration`` number. The physical meaning of this "iteration" is
defined by user depending on how often they call ``report``.
It does not necessarily map to one epoch.
.. warning::
All workers must call `ray.train.report` the same number of times
so that Ray Train can properly synchronize the training state across
workers. Otherwise, your training will hang.
.. warning::
This method does NOT act as a barrier for distributed training workers.
Workers will upload their checkpoint, then continue training immediately.
If you need to synchronize workers, you can use a framework-native barrier
such as `torch.distributed.barrier()`.
Example:
.. testcode::
import tempfile
from ray import train
from ray.train import Checkpoint
from ray.train.torch import TorchTrainer
def train_func(config):
start_epoch = 0
checkpoint = train.get_checkpoint()
if checkpoint:
with checkpoint.as_directory() as checkpoint_dir:
# Load back training state
...
for epoch in range(start_epoch, config.get("num_epochs", 10)):
# Do training...
metrics = {"loss": ...}
with tempfile.TemporaryDirectory() as temp_checkpoint_dir:
# Save the checkpoint...
# torch.save(...)
checkpoint = Checkpoint.from_directory(temp_checkpoint_dir)
# Example: Only the rank 0 worker uploads the checkpoint.
if ray.train.get_context().get_world_rank() == 0:
train.report(metrics, checkpoint=checkpoint)
else:
train.report(metrics, checkpoint=None)
trainer = TorchTrainer(
train_func, scaling_config=train.ScalingConfig(num_workers=2)
)
Args:
metrics: The metrics you want to report.
checkpoint: The optional checkpoint you want to report.
"""
_get_session().report(metrics, checkpoint=checkpoint)
@PublicAPI(stability="stable")
@_warn_session_misuse()
def get_checkpoint() -> Optional[Checkpoint]:
"""Access the session's last checkpoint to resume from if applicable.
Example:
.. testcode::
import tempfile
from ray import train
from ray.train import Checkpoint
from ray.train.torch import TorchTrainer
def train_func(config):
start_epoch = 0
checkpoint = train.get_checkpoint()
if checkpoint:
with checkpoint.as_directory() as checkpoint_dir:
# Load back training state
...
for epoch in range(start_epoch, config.get("num_epochs", 10)):
# Do training...
metrics = {"loss": ...}
with tempfile.TemporaryDirectory() as temp_checkpoint_dir:
# Save the checkpoint...
checkpoint = Checkpoint.from_directory(temp_checkpoint_dir)
train.report(metrics, checkpoint=checkpoint)
trainer = TorchTrainer(
train_func, scaling_config=train.ScalingConfig(num_workers=2)
)
Returns:
Checkpoint object if the session is currently being resumed.
Otherwise, return None.
"""
return _get_session().loaded_checkpoint
@PublicAPI(stability="beta")
@_warn_session_misuse()
def get_metadata() -> Dict[str, Any]:
"""User metadata dict passed to the Trainer constructor."""
return _get_session().metadata
@PublicAPI(stability="beta")
@_warn_session_misuse()
def get_experiment_name() -> str:
"""Experiment name for the corresponding trial."""
return _get_session().experiment_name
@PublicAPI(stability="beta")
@_warn_session_misuse()
def get_trial_name() -> str:
"""Trial name for the corresponding trial."""
return _get_session().trial_name
@PublicAPI(stability="beta")
@_warn_session_misuse()
def get_trial_id() -> str:
"""Trial id for the corresponding trial."""
return _get_session().trial_id
@PublicAPI(stability="beta")
@_warn_session_misuse()
def get_trial_resources() -> "PlacementGroupFactory":
"""Trial resources for the corresponding trial."""
return _get_session().trial_resources
@PublicAPI(stability="beta")
@_warn_session_misuse()
def get_trial_dir() -> str:
"""Log directory corresponding to the trial directory for a Tune session.
If calling from a Train session, this will give the trial directory of its parent
Tune session.
.. testcode::
from ray import train, tune
def train_func(config):
print(train.get_context().get_trial_dir())
tuner = tune.Tuner(train_func)
tuner.fit()
.. testoutput::
:options: +MOCK
/Users/root/ray_results/train_func_2023-07-19_15-01-37/train_func_d620c_00000_0_2023-07-19_15-01-40
"""
return _get_session().trial_dir
@PublicAPI(stability="beta")
@_warn_session_misuse(default_value=1)
def get_world_size() -> int:
"""Get the current world size (i.e. total number of workers) for this run.
.. testcode::
import ray
from ray import train
from ray.train import ScalingConfig
from ray.train.tensorflow import TensorflowTrainer
NUM_WORKERS = 2
def train_loop_per_worker(config):
assert train.get_context().get_world_size() == NUM_WORKERS
train_dataset = ray.data.read_csv("s3://anonymous@ray-example-data/iris.csv")
trainer = TensorflowTrainer(
train_loop_per_worker,
scaling_config=ScalingConfig(num_workers=NUM_WORKERS),
datasets={"train": train_dataset}
)
trainer.fit()
.. testoutput::
:hide:
...
"""
session = _get_session()
if not hasattr(session, "world_size"):
raise RuntimeError(
"`get_world_size` can only be called for TrainSession! "
"Make sure you only use that in `train_loop_per_worker` function"
"that is passed into `DataParallelTrainer`."
)
return session.world_size
@PublicAPI(stability="beta")
@_warn_session_misuse(default_value=0)
def get_world_rank() -> int:
"""Get the world rank of this worker.
.. testcode::
import ray
from ray import train
from ray.train import ScalingConfig
from ray.train.tensorflow import TensorflowTrainer
def train_loop_per_worker(config):
if train.get_context().get_world_rank() == 0:
print("Worker 0")
train_dataset = ray.data.read_csv("s3://anonymous@ray-example-data/iris.csv")
trainer = TensorflowTrainer(
train_loop_per_worker,
scaling_config=ScalingConfig(num_workers=2),
datasets={"train": train_dataset}
)
trainer.fit()
.. testoutput::
:hide:
...
"""
session = _get_session()
if not hasattr(session, "world_rank"):
raise RuntimeError(
"`get_world_rank` can only be called for TrainSession! "
"Make sure you only use that in `train_loop_per_worker` function"
"that is passed into `DataParallelTrainer`."
)
return session.world_rank
@PublicAPI(stability="beta")
@_warn_session_misuse(default_value=0)
def get_local_rank() -> int:
"""Get the local rank of this worker (rank of the worker on its node).
.. testcode::
import torch
import ray
from ray import train
from ray.train import ScalingConfig
from ray.train.torch import TorchTrainer
def train_loop_per_worker(config):
if torch.cuda.is_available():
torch.cuda.set_device(train.get_context().get_local_rank())
...
train_dataset = ray.data.read_csv("s3://anonymous@ray-example-data/iris.csv")
trainer = TorchTrainer(
train_loop_per_worker,
scaling_config=ScalingConfig(num_workers=2, use_gpu=True),
datasets={"train": train_dataset}
)
trainer.fit()
.. testoutput::
:hide:
...
"""
session = _get_session()
if not hasattr(session, "local_rank"):
raise RuntimeError(
"`get_local_rank` can only be called for TrainSession! "
"Make sure you only use that in `train_loop_per_worker` function"
"that is passed into `DataParallelTrainer`."
)
return session.local_rank
@PublicAPI(stability="beta")
@_warn_session_misuse(default_value=0)
def get_local_world_size() -> int:
"""Get the local world size of this node (i.e. number of workers on this node).
Example:
.. testcode::
import ray
from ray import train
from ray.train import ScalingConfig
from ray.train.torch import TorchTrainer
def train_loop_per_worker():
print(train.get_context().get_local_world_size())
train_dataset = ray.data.from_items(
[{"x": x, "y": x + 1} for x in range(32)])
trainer = TorchTrainer(train_loop_per_worker,
scaling_config=ScalingConfig(num_workers=1),
datasets={"train": train_dataset})
trainer.fit()
.. testoutput::
:hide: