|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import importlib |
| 8 | + |
| 9 | +from typing import cast, Type, TypeVar |
| 10 | + |
| 11 | + |
| 12 | +T = TypeVar("T") |
| 13 | + |
| 14 | + |
| 15 | +class PatchRustClass: |
| 16 | + def __init__(self, rust_class: Type): |
| 17 | + self.rust_class = rust_class |
| 18 | + |
| 19 | + def __call__(self, python_class: Type[T]) -> Type[T]: |
| 20 | + assert self.rust_class.__module__ == python_class.__module__ |
| 21 | + assert self.rust_class.__name__ == python_class.__name__ |
| 22 | + for name, implementation in python_class.__dict__.items(): |
| 23 | + if hasattr(self.rust_class, name): |
| 24 | + # do not patch in the stub methods that |
| 25 | + # are already defined by the rust implementation |
| 26 | + continue |
| 27 | + if not callable(implementation): |
| 28 | + continue |
| 29 | + setattr(self.rust_class, name, implementation) |
| 30 | + return cast(Type[T], self.rust_class) |
| 31 | + |
| 32 | + |
| 33 | +def rust_struct(name: str) -> PatchRustClass: |
| 34 | + """ |
| 35 | + When we bind a rust struct into Python, it is sometimes faster to implement |
| 36 | + parts of the desired Python API in Python. It is also easier to understand |
| 37 | + what the class does in terms of these methods. |
| 38 | +
|
| 39 | + We also want to avoid having to wrap rust objects in another layer of python objects |
| 40 | + because: |
| 41 | + * wrappers double the python overhead |
| 42 | + * it is easy to confuse which level of wrappers and API takes, especially |
| 43 | + along the python<->rust boundary. |
| 44 | +
|
| 45 | +
|
| 46 | + To avoid wrappers we first define the class in pyo3. Lets say we add a class |
| 47 | + monarch_hyperactor::actor_mesh::TestClass which we will want to extend with python methods in |
| 48 | + the monarch/actor/_src/actor_mesh.py. In rust we will define the class as |
| 49 | +
|
| 50 | + #[pyclass(name = "TestClass", module = "monarch._src.actor_mesh")] |
| 51 | + struct TestClass {} |
| 52 | + #[pymethods] |
| 53 | + impl TestClass { |
| 54 | + fn hello(&self) { |
| 55 | + println!("hello"); |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + Then rather than writing typing stubs in a pyi file we write the stub code directly in |
| 60 | + monarch/actor/_src/actor_mesh.py along with any helper methods: |
| 61 | +
|
| 62 | + @rust_struct("monarch_hyperactor::actor_mesh::TestClass") |
| 63 | + class TestClass: |
| 64 | + def hello(self) -> None: |
| 65 | + ... |
| 66 | + def hello_world(self) -> None: |
| 67 | + self.hello() |
| 68 | + print("world") |
| 69 | +
|
| 70 | + This class annotation then merges the python extension methods with the rust |
| 71 | + class implementation. Any rust code that returns the TestClass will have the `hello_world` |
| 72 | + extension method attached. Python typechecking always things TestClass is the python code, |
| 73 | + so typing works. |
| 74 | +
|
| 75 | + It is ok to have the pyclass module not match where it is defined because (1) we patch it into the right place |
| 76 | + to make sure pickling works, and (2) the rust_struct annotation points directly to where to find the rust code, |
| 77 | + and will be discovered by goto line in the IDE. |
| 78 | + """ |
| 79 | + |
| 80 | + *modules, name = name.split("::") |
| 81 | + module_name = ".".join(modules) |
| 82 | + module = importlib.import_module(f"monarch._rust_bindings.{module_name}") |
| 83 | + |
| 84 | + rust_class = getattr(module, name) |
| 85 | + |
| 86 | + return PatchRustClass(rust_class) |
0 commit comments