Source code for pyflink.dataframe.gpu
################################################################################
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################
import json
import os
from typing import List
[docs]
class GPUInfo:
"""GPU device information allocated to the current Python worker.
Attributes:
index: GPU device index (e.g., 0, 1).
type: GPU device type (e.g., "A10", "V100").
amount: Fractional GPU amount allocated (e.g., 0.5, 1.0).
"""
def __init__(self, index: int, type: str, amount: float):
self.index = index
self.type = type
self.amount = amount
def __repr__(self):
return f"GPUInfo(index={self.index}, type='{self.type}', amount={self.amount})"
def __eq__(self, other):
if not isinstance(other, GPUInfo):
return False
return self.index == other.index and self.type == other.type and self.amount == other.amount
def __hash__(self):
return hash((self.index, self.type, self.amount))
[docs]
def get_gpu_infos() -> List[GPUInfo]:
"""Get GPU information allocated to the current Python worker.
Reads the ``_PYFLINK_GPU_INFOS`` environment variable set by the Java operator
and returns a list of :class:`GPUInfo` objects.
Returns:
List of GPUInfo. Empty list if no GPU is allocated.
"""
gpu_infos_json = os.environ.get("_PYFLINK_GPU_INFOS")
if not gpu_infos_json:
return []
infos = json.loads(gpu_infos_json)
return [GPUInfo(index=info["index"], type=info["type"], amount=info["amount"])
for info in infos]