rref_proxy.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from functools import partial
  2. from . import functions
  3. from . import rpc_async
  4. import torch
  5. from .constants import UNSET_RPC_TIMEOUT
  6. from torch.futures import Future
  7. def _local_invoke(rref, func_name, args, kwargs):
  8. return getattr(rref.local_value(), func_name)(*args, **kwargs)
  9. @functions.async_execution
  10. def _local_invoke_async_execution(rref, func_name, args, kwargs):
  11. return getattr(rref.local_value(), func_name)(*args, **kwargs)
  12. def _invoke_rpc(rref, rpc_api, func_name, timeout, *args, **kwargs):
  13. def _rref_type_cont(rref_fut):
  14. rref_type = rref_fut.value()
  15. _invoke_func = _local_invoke
  16. # Bypass ScriptModules when checking for async function attribute.
  17. bypass_type = issubclass(rref_type, torch.jit.ScriptModule) or issubclass(
  18. rref_type, torch._C.ScriptModule
  19. )
  20. if not bypass_type:
  21. func = getattr(rref_type, func_name)
  22. if hasattr(func, "_wrapped_async_rpc_function"):
  23. _invoke_func = _local_invoke_async_execution
  24. return rpc_api(
  25. rref.owner(),
  26. _invoke_func,
  27. args=(rref, func_name, args, kwargs),
  28. timeout=timeout
  29. )
  30. rref_fut = rref._get_type(timeout=timeout, blocking=False)
  31. if rpc_api != rpc_async:
  32. rref_fut.wait()
  33. return _rref_type_cont(rref_fut)
  34. else:
  35. # A little explanation on this.
  36. # rpc_async returns a Future pointing to the return value of `func_name`, it returns a `Future[T]`
  37. # Calling _rref_type_cont from the `then` lambda causes Future wrapping. IOW, `then` returns a `Future[Future[T]]`
  38. # To address that, we return a Future that is completed with the result of the async call.
  39. result: Future = Future()
  40. def _wrap_rref_type_cont(fut):
  41. try:
  42. _rref_type_cont(fut).then(_complete_op)
  43. except BaseException as ex:
  44. result.set_exception(ex)
  45. def _complete_op(fut):
  46. try:
  47. result.set_result(fut.value())
  48. except BaseException as ex:
  49. result.set_exception(ex)
  50. rref_fut.then(lambda fut: _wrap_rref_type_cont(fut))
  51. return result
  52. # This class manages proxied RPC API calls for RRefs. It is entirely used from
  53. # C++ (see python_rpc_handler.cpp).
  54. class RRefProxy:
  55. def __init__(self, rref, rpc_api, timeout=UNSET_RPC_TIMEOUT):
  56. self.rref = rref
  57. self.rpc_api = rpc_api
  58. self.rpc_timeout = timeout
  59. def __getattr__(self, func_name):
  60. return partial(_invoke_rpc, self.rref, self.rpc_api, func_name, self.rpc_timeout)