Skip to content

Commit

Permalink
[air] Deprecate some fields/classes that are supposed to be gone in 2…
Browse files Browse the repository at this point in the history
….6. (ray-project#38794)

Signed-off-by: xwjiang2010 <xwjiang2010@gmail.com>
Signed-off-by: e428265 <arvind.chandramouli@lmco.com>
  • Loading branch information
xwjiang2010 authored and arvind-chandra committed Aug 31, 2023
1 parent eba51bb commit d3c1a0a
Show file tree
Hide file tree
Showing 14 changed files with 17 additions and 94 deletions.
2 changes: 1 addition & 1 deletion doc/source/train/getting-started-pytorch-lightning.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ information about the training run, including the metrics and checkpoints report
result.metrics # The metrics reported during training.
result.checkpoint # The latest checkpoint reported during training.
result.log_dir # The path where logs are stored.
result.path # The path where logs are stored.
result.error # The exception that was raised, if training failed.
.. TODO: Add results guide
Expand Down
2 changes: 1 addition & 1 deletion doc/source/train/getting-started-pytorch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ information about the training run, including the metrics and checkpoints report
result.metrics # The metrics reported during training.
result.checkpoint # The latest checkpoint reported during training.
result.log_dir # The path where logs are stored.
result.path # The path where logs are stored.
result.error # The exception that was raised, if training failed.
.. TODO: Add results guide
Expand Down
2 changes: 1 addition & 1 deletion doc/source/train/getting-started-transformers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ information about the training run, including the metrics and checkpoints report
result.metrics # The metrics reported during training.
result.checkpoint # The latest checkpoint reported during training.
result.log_dir # The path where logs are stored.
result.path # The path where logs are stored.
result.error # The exception that was raised, if training failed.
.. TODO: Add results guide
Expand Down
2 changes: 1 addition & 1 deletion doc/source/tune/doc_code/key_concepts.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def load_checkpoint(self, checkpoint_dir):

best_result = results.get_best_result() # Get best result object
best_config = best_result.config # Get best trial's hyperparameters
best_logdir = best_result.log_dir # Get best trial's logdir
best_logdir = best_result.path # Get best trial's result directory
best_checkpoint = best_result.checkpoint # Get best trial's best checkpoint
best_metrics = best_result.metrics # Get best trial's last results
best_result_df = best_result.metrics_dataframe # Get best result as pandas dataframe
Expand Down
4 changes: 2 additions & 2 deletions doc/source/tune/examples/pbt_guide.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@
"# Get the best trial result\n",
"best_result = results_grid.get_best_result(metric=\"mean_accuracy\", mode=\"max\")\n",
"\n",
"# Print `log_dir` where checkpoints are stored\n",
"print(\"Best result logdir:\", best_result.log_dir)\n",
"# Print `path` where checkpoints are stored\n",
"print('Best result path:', best_result.path)\n",
"\n",
"# Print the best trial `config` reported at the last iteration\n",
"# NOTE: This config is just what the trial ended up with at the last iteration.\n",
Expand Down
50 changes: 1 addition & 49 deletions python/ray/air/integrations/keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ray
from ray.train._internal.storage import _use_storage_context
from ray.train.tensorflow import TensorflowCheckpoint, LegacyTensorflowCheckpoint
from ray.util.annotations import PublicAPI, Deprecated
from ray.util.annotations import PublicAPI


class _Callback(KerasCallback):
Expand Down Expand Up @@ -183,51 +183,3 @@ def _get_reported_metrics(self, logs: Dict) -> Dict:

assert isinstance(reported_metrics, dict)
return reported_metrics


@Deprecated
class Callback(_Callback):
"""
Keras callback for Ray AIR reporting and checkpointing.
You can use this in both TuneSession and TrainSession.
Example:
.. code-block: python
############# Using it in TrainSession ###############
from ray.air.integrations.keras import Callback
def train_loop_per_worker():
strategy = tf.distribute.MultiWorkerMirroredStrategy()
with strategy.scope():
model = build_model()
#model.compile(...)
model.fit(dataset_shard, callbacks=[Callback()])
Args:
metrics: Metrics to report. If this is a list, each item describes
the metric key reported to Keras, and it will reported under the
same name. If this is a dict, each key will be the name reported
and the respective value will be the metric key reported to Keras.
If this is None, all Keras logs will be reported.
on: When to report metrics. Must be one of
the Keras event hooks (less the ``on_``), e.g.
"train_start", or "predict_end". Defaults to "epoch_end".
frequency: Checkpoint frequency. If this is an integer `n`,
checkpoints are saved every `n` times each hook was called. If
this is a list, it specifies the checkpoint frequencies for each
hook individually.
"""

def __init__(
self,
metrics: Optional[Union[str, List[str], Dict[str, str]]] = None,
on: Union[str, List[str]] = "epoch_end",
frequency: Union[int, List[int]] = 1,
):
# TODO: Remove this class in 2.6.
raise DeprecationWarning(
"`ray.air.integrations.keras.Callback` is deprecated. Use "
"`ray.air.integrations.keras.ReportCheckpointCallback` instead.",
)
18 changes: 0 additions & 18 deletions python/ray/air/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import pandas as pd
import pyarrow
import warnings
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
Expand All @@ -13,7 +12,6 @@
EXPR_PROGRESS_FILE,
EXPR_ERROR_PICKLE_FILE,
)
from ray.util import log_once
from ray.util.annotations import PublicAPI

import logging
Expand Down Expand Up @@ -60,22 +58,6 @@ class Result:
_remote_path: Optional[str] = None
_storage_filesystem: Optional[pyarrow.fs.FileSystem] = None
_items_to_repr = ["error", "metrics", "path", "filesystem", "checkpoint"]
# Deprecate: raise in 2.5, remove in 2.6
log_dir: Optional[Path] = None

def __post_init__(self):
if self.log_dir and log_once("result_log_dir_deprecated"):
warnings.warn(
"The `Result.log_dir` property is deprecated. "
"Use `local_path` instead."
)
self._local_path = str(self.log_dir)

# Duplicate for retrieval
self.log_dir = Path(self._local_path) if self._local_path else None
# Backwards compatibility: Make sure to cast Path to string
# Deprecate: Remove this line after 2.6
self._local_path = str(self._local_path) if self._local_path else None

@property
def config(self) -> Optional[Dict[str, Any]]:
Expand Down
7 changes: 1 addition & 6 deletions python/ray/air/tests/test_keras_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import ray
from ray import train
from ray.air.integrations.keras import Callback, ReportCheckpointCallback
from ray.air.integrations.keras import ReportCheckpointCallback
from ray.train.constants import TRAIN_DATASET_KEY
from ray.train import ScalingConfig
from ray.train.tensorflow import (
Expand Down Expand Up @@ -207,11 +207,6 @@ def test_keras_callback_e2e():
predictor.predict(data=items)


def test_keras_callback_is_deprecated():
with pytest.raises(DeprecationWarning):
Callback()


if __name__ == "__main__":
import sys

Expand Down
3 changes: 2 additions & 1 deletion python/ray/train/examples/mlflow_simple_example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from ray import train
from ray.train import ScalingConfig, RunConfig
from ray.train.torch import TorchTrainer
Expand Down Expand Up @@ -41,7 +42,7 @@ def train_func():

# Print the latest run directory and keep note of it.
# For example: /home/ubuntu/ray_results/TorchTrainer_2022-06-13_20-31-06
print("Run directory:", result.log_dir.parent) # TensorBoard is saved in parent dir
print("Run directory:", Path(result.path).parent) # TensorBoard is saved in parent dir

# How to visualize the logs

Expand Down
8 changes: 0 additions & 8 deletions python/ray/train/tensorflow/tensorflow_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ def from_saved_model(
def get_model(
self,
model: Optional[Union[tf.keras.Model, Callable[[], tf.keras.Model]]] = None,
model_definition: Optional[Callable[[], tf.keras.Model]] = None,
) -> tf.keras.Model:
"""Retrieve the model stored in this checkpoint.
Expand All @@ -308,13 +307,6 @@ def get_model(
Returns:
The Tensorflow Keras model stored in the checkpoint.
"""
# TODO: Remove `model_definition` in 2.6.
if model_definition is not None:
raise DeprecationWarning(
"The `model_definition` parameter is deprecated. Use the `model` "
"parameter instead."
)

if model is not None and self._flavor is not self.Flavor.MODEL_WEIGHTS:
warnings.warn(
"TensorflowCheckpoint was created from "
Expand Down
2 changes: 1 addition & 1 deletion python/ray/train/tests/test_lightning_trainer_restore.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import numpy as np
from pathlib import Path
import pytest
import pytorch_lightning as pl
from pathlib import Path
from pytorch_lightning.callbacks import ModelCheckpoint

import ray
Expand Down
4 changes: 2 additions & 2 deletions python/ray/tune/tests/test_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def train_func(config):
results = tuner.fit()
assert not results.errors
for result in results:
artifact_data = open(result.log_dir / "write.txt", "r").read()
artifact_data = open(os.path.join(result.path, "write.txt"), "r").read()
assert artifact_data == f"{result.config['id']}"


Expand Down Expand Up @@ -467,7 +467,7 @@ def train_func(config):
results = tuner.fit()
assert not results.errors
for result in results:
artifact_data = open(result.log_dir / "write.txt", "r").read()
artifact_data = open(os.path.join(result.path, "write.txt"), "r").read()
assert artifact_data == f"{result.config['id']}"


Expand Down
1 change: 1 addition & 0 deletions python/ray/tune/tests/test_tuner_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ def failing_fn(config):
if path.startswith("experiment_state")
]
)

assert num_experiment_checkpoints == 2

num_trial_checkpoints = len(
Expand Down
6 changes: 3 additions & 3 deletions python/ray/tune/tests/tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def train_mnist(config):
# __eval_func_end__

# __plot_begin__
dfs = {result.log_dir: result.metrics_dataframe for result in results}
dfs = {result.path: result.metrics_dataframe for result in results}
[d.mean_accuracy.plot() for d in dfs.values()]
# __plot_end__

Expand All @@ -149,7 +149,7 @@ def train_mnist(config):
results = tuner.fit()

# Obtain a trial dataframe from all run trials of this `tune.run` call.
dfs = {result.log_dir: result.metrics_dataframe for result in results}
dfs = {result.path: result.metrics_dataframe for result in results}
# __run_scheduler_end__

# fmt: off
Expand Down Expand Up @@ -190,7 +190,7 @@ def train_mnist(config):
# __run_analysis_begin__
import os

logdir = results.get_best_result("mean_accuracy", mode="max").log_dir
logdir = results.get_best_result("mean_accuracy", mode="max").path
state_dict = torch.load(os.path.join(logdir, "model.pth"))

model = ConvNet()
Expand Down

0 comments on commit d3c1a0a

Please sign in to comment.