Skip to content

Commit 6351471

Browse files
[Train] Replace Checkpoint Manager pydantic v2 APIs with v1 APIs (#57147)
Replace `CheckpointManager`'s usage of pydantic v2 APIs with v1 APIs instead. --------- Signed-off-by: JasonLi1909 <jasli1909@gmail.com> Signed-off-by: Jason Li <57246540+JasonLi1909@users.noreply.github.com> Co-authored-by: Justin Yu <justinvyu@anyscale.com>
1 parent ffcefe4 commit 6351471

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

python/ray/train/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
from ray.train.v2._internal.constants import is_v2_enabled
2929

3030
if is_v2_enabled():
31+
try:
32+
import pydantic # noqa: F401
33+
except (ImportError, ModuleNotFoundError) as exc:
34+
raise ImportError(
35+
"`ray.train.v2` requires the pydantic package, which is missing. "
36+
"Run the following command to fix this: `pip install pydantic`"
37+
) from exc
3138
from ray.train.v2.api.callback import UserCallback # noqa: F811
3239
from ray.train.v2.api.config import ( # noqa: F811
3340
FailureConfig,

python/ray/train/v2/_internal/execution/checkpoint/checkpoint_manager.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import asyncio
2+
import json
23
import logging
34
from typing import Any, Dict, List, Optional
45

6+
from ray._common.pydantic_compat import BaseModel
57
from ray.air.config import CheckpointConfig
68
from ray.train._checkpoint import Checkpoint
79
from ray.train._internal.checkpoint_manager import (
@@ -20,16 +22,6 @@
2022
from ray.train.v2._internal.execution.worker_group import Worker
2123
from ray.train.v2.api.reported_checkpoint import ReportedCheckpoint
2224

23-
try:
24-
from pydantic import BaseModel
25-
from pydantic_core import from_json
26-
except (ImportError, ModuleNotFoundError) as exc:
27-
raise ImportError(
28-
"`ray.train.v2` requires the pydantic package, which is missing. "
29-
"Run the following command to fix this: `pip install pydantic`"
30-
) from exc
31-
32-
3325
logger = logging.getLogger(__name__)
3426

3527

@@ -235,14 +227,13 @@ def _save_state(self) -> str:
235227
checkpoint_results=checkpoint_results,
236228
latest_checkpoint_result=latest_checkpoint_result,
237229
)
238-
return manager_snapshot.model_dump_json()
230+
return manager_snapshot.json()
239231

240232
def _load_state(self, json_state: str):
241233
"""Load the checkpoint manager state from a JSON str."""
242234
try:
243-
manager_snapshot = _CheckpointManagerState.model_validate(
244-
from_json(json_state)
245-
)
235+
json_dict = json.loads(json_state)
236+
manager_snapshot = _CheckpointManagerState.parse_obj(json_dict)
246237
except Exception as e:
247238
raise CheckpointManagerInitializationError(repr(e)) from e
248239
self._assert_checkpoints_exist()

0 commit comments

Comments
 (0)