|
| 1 | +# Copyright 2025 IBM, Red Hat |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from codeflare_sdk.common.utils.constants import ( |
| 16 | + SUPPORTED_PYTHON_VERSIONS, |
| 17 | + CUDA_PY312_RUNTIME_IMAGE, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def get_ray_image_for_python_version(python_version=None, warn_on_unsupported=True): |
| 22 | + """ |
| 23 | + Get the appropriate Ray image for a given Python version. |
| 24 | + If no version is provided, uses the current runtime Python version. |
| 25 | + This prevents us needing to hard code image versions for tests. |
| 26 | +
|
| 27 | + Args: |
| 28 | + python_version: Python version string (e.g. "3.11"). If None, detects current version. |
| 29 | + warn_on_unsupported: If True, warns and returns None for unsupported versions. |
| 30 | + If False, silently falls back to Python 3.12 image. |
| 31 | + """ |
| 32 | + if python_version is None: |
| 33 | + import sys |
| 34 | + |
| 35 | + python_version = f"{sys.version_info.major}.{sys.version_info.minor}" |
| 36 | + |
| 37 | + if python_version in SUPPORTED_PYTHON_VERSIONS: |
| 38 | + return SUPPORTED_PYTHON_VERSIONS[python_version] |
| 39 | + elif warn_on_unsupported: |
| 40 | + import warnings |
| 41 | + |
| 42 | + warnings.warn( |
| 43 | + f"No default Ray image defined for {python_version}. Please provide your own image or use one of the following python versions: {', '.join(SUPPORTED_PYTHON_VERSIONS.keys())}." |
| 44 | + ) |
| 45 | + return None |
| 46 | + else: |
| 47 | + return CUDA_PY312_RUNTIME_IMAGE |
0 commit comments