Hyeontaek Lim 96b7dbabdc [JAX] Implement an initial object API for colocated Python
Colocated Python adds `colocated_python_class`. This API wraps a user-defined
class for automatic remoting of object construction/destruction and method calls:

* An object will be initialized on the backend. At least for now,
initialization is deferred until the first method is called; at this point,
colocated Python knows what devices the objects should be accessible and thus
it can construct the object(s).

* When an object method is called, the method call runs as a colocated Python
function call on the backend.

* When the object is destroyed (either by reaching a zero reference count or
through Python GC), destruction also runs as a colocated Python function call
and destroys all objects from the backend.

This change provides an intial API implementation. Main limitations are as
follows:

* The methods of a colocated Python class does not support specialization.
Calling it requires at least one argument.

* Colocated Python objects cannot reference or interact with each other on the
controller or on the colocated Python backend.

These limitations will be lifted as the object API implementation is improved.

PiperOrigin-RevId: 729629265
2025-02-21 12:58:25 -08:00

62 lines
2.3 KiB
Python

# Copyright 2024 The JAX Authors.
#
# Licensed 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
#
# https://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.
"""Colocated Python top-level API."""
from __future__ import annotations
import collections
from typing import Any, Callable, Sequence, Type
import jax
from jax._src import api_util
from jax.experimental.colocated_python.func import make_callable
from jax.experimental.colocated_python.obj import wrap_class
def colocated_cpu_devices(
devices: Sequence[jax.Device],
) -> Sequence[jax.Device]:
"""Finds CPU devices colocated with the given devices."""
cpu_devices_by_colocation_id = collections.defaultdict(list)
for device in devices[0].client._get_all_devices(): # pylint: disable=protected-access
if device.device_kind == "cpu":
cpu_devices_by_colocation_id[device.colocation_id].append(device)
if not cpu_devices_by_colocation_id:
raise ValueError("No CPU devices found")
colocated_cpu_devices = []
for device in devices:
matches = cpu_devices_by_colocation_id[device.colocation_id]
if not matches:
raise ValueError(f"Device {device} has no colocated devices")
elif len(matches) > 1:
raise ValueError(
f"Ambiguous colocated devices; device {device} has"
f" {len(matches)} colocated devices: f{matches}"
)
colocated_cpu_devices.append(matches[0])
return colocated_cpu_devices
def colocated_python(fun: Callable[..., Any]) -> Callable[..., Any]:
"""Executes the given Python function on the same devices as the arguments."""
return make_callable(
fun, api_util.fun_sourceinfo(fun), api_util.fun_signature(fun)
)
def colocated_python_class(cls: Type[object]) -> Type[object]:
"""Executes the given Python class methods on the same devices as the arguments."""
return wrap_class(cls, api_util.fun_sourceinfo(cls))