本文简要介绍python语言中 torch.distributed.rpc.functions.async_execution
的用法。
用法:
torch.distributed.rpc.functions.async_execution(fn)
函数的装饰器,指示函数的返回值保证是
Future
对象,并且该函数可以在 RPC 被调用者上异步运行。更具体地说,被调用者提取由包装函数返回的Future
并将后续处理步骤安装为该Future
的回调。完成后,安装的回调将从Future
读取值,并将该值作为 RPC 响应发送返回。这也意味着返回的Future
仅存在于被调用方,并且永远不会通过RPC发送。当包装函数(fn
)的执行由于包含rpc_async()
或等待其他信号而需要暂停和恢复时,此装饰器非常有用。注意
要启用异步执行,应用程序必须将此装饰器返回的函数对象传递给 RPC API。如果 RPC 检测到此装饰器安装的属性,它知道此函数返回一个
Future
对象并将相应地处理它。然而,这并不意味着在定义一个函数时这个装饰器必须是最外层的。例如,当与@staticmethod
或@classmethod
结合使用时,@rpc.functions.async_execution
需要作为内部装饰器,以允许将目标函数识别为静态函数或类函数。此目标函数仍然可以异步执行,因为在访问时,静态或类方法会保留由@rpc.functions.async_execution
安装的属性。返回的
Future
对象可以来自rpc_async()
、then()
或Future
构造函数。下面的示例显示直接使用then()
返回的Future
。>>> from torch.distributed import rpc >>> >>> # omitting setup and shutdown RPC >>> >>> # On all workers >>> @rpc.functions.async_execution >>> def async_add_chained(to, x, y, z): >>> # This function runs on "worker1" and returns immediately when >>> # the callback is installed through the `then(cb)` API. In the >>> # mean time, the `rpc_async` to "worker2" can run concurrently. >>> # When the return value of that `rpc_async` arrives at >>> # "worker1", "worker1" will run the lambda function accordingly >>> # and set the value for the previously returned `Future`, which >>> # will then trigger RPC to send the result back to "worker0". >>> return rpc.rpc_async(to, torch.add, args=(x, y)).then( >>> lambda fut: fut.wait() + z >>> ) >>> >>> # On worker0 >>> ret = rpc.rpc_sync( >>> "worker1", >>> async_add_chained, >>> args=("worker2", torch.ones(2), 1, 1) >>> ) >>> print(ret) # prints tensor([3., 3.])
当与TorchScript 装饰器结合使用时,这个装饰器必须是最外层的。
>>> from torch import Tensor >>> from torch.futures import Future >>> from torch.distributed import rpc >>> >>> # omitting setup and shutdown RPC >>> >>> # On all workers >>> @torch.jit.script >>> def script_add(x: Tensor, y: Tensor) -> Tensor: >>> return x + y >>> >>> @rpc.functions.async_execution >>> @torch.jit.script >>> def async_add(to: str, x: Tensor, y: Tensor) -> Future[Tensor]: >>> return rpc.rpc_async(to, script_add, (x, y)) >>> >>> # On worker0 >>> ret = rpc.rpc_sync( >>> "worker1", >>> async_add, >>> args=("worker2", torch.ones(2), 1) >>> ) >>> print(ret) # prints tensor([2., 2.])
与静态或类方法结合使用时,此装饰器必须是内部装饰器。
>>> from torch.distributed import rpc >>> >>> # omitting setup and shutdown RPC >>> >>> # On all workers >>> class AsyncExecutionClass: >>> >>> @staticmethod >>> @rpc.functions.async_execution >>> def static_async_add(to, x, y, z): >>> return rpc.rpc_async(to, torch.add, args=(x, y)).then( >>> lambda fut: fut.wait() + z >>> ) >>> >>> @classmethod >>> @rpc.functions.async_execution >>> def class_async_add(cls, to, x, y, z): >>> ret_fut = torch.futures.Future() >>> rpc.rpc_async(to, torch.add, args=(x, y)).then( >>> lambda fut: ret_fut.set_result(fut.wait() + z) >>> ) >>> return ret_fut >>> >>> @rpc.functions.async_execution >>> def bound_async_add(self, to, x, y, z): >>> return rpc.rpc_async(to, torch.add, args=(x, y)).then( >>> lambda fut: fut.wait() + z >>> ) >>> >>> # On worker0 >>> ret = rpc.rpc_sync( >>> "worker1", >>> AsyncExecutionClass.static_async_add, >>> args=("worker2", torch.ones(2), 1, 2) >>> ) >>> print(ret) # prints tensor([4., 4.]) >>> >>> ret = rpc.rpc_sync( >>> "worker1", >>> AsyncExecutionClass.class_async_add, >>> args=("worker2", torch.ones(2), 1, 2) >>> ) >>> print(ret) # prints tensor([4., 4.])
该装饰器还可以与 RRef 助手一起使用,即 .
torch.distributed.rpc.RRef.rpc_sync()
、torch.distributed.rpc.RRef.rpc_async()
和torch.distributed.rpc.RRef.remote()
。>>> from torch.distributed import rpc >>> >>> # reuse the AsyncExecutionClass class above >>> rref = rpc.remote("worker1", AsyncExecutionClass) >>> ret = rref.rpc_sync().static_async_add("worker2", torch.ones(2), 1, 2) >>> print(ret) # prints tensor([4., 4.]) >>> >>> rref = rpc.remote("worker1", AsyncExecutionClass) >>> ret = rref.rpc_async().static_async_add("worker2", torch.ones(2), 1, 2).wait() >>> print(ret) # prints tensor([4., 4.]) >>> >>> rref = rpc.remote("worker1", AsyncExecutionClass) >>> ret = rref.remote().static_async_add("worker2", torch.ones(2), 1, 2).to_here() >>> print(ret) # prints tensor([4., 4.])
例子:
相关用法
- Python PyTorch assert_close用法及代码示例
- Python PyTorch as_strided用法及代码示例
- Python PyTorch asin用法及代码示例
- Python PyTorch asinh用法及代码示例
- Python PyTorch as_tensor用法及代码示例
- Python PyTorch argsort用法及代码示例
- Python PyTorch addmm用法及代码示例
- Python PyTorch addmv用法及代码示例
- Python PyTorch apply_effects_tensor用法及代码示例
- Python PyTorch angle用法及代码示例
- Python PyTorch all_reduce用法及代码示例
- Python PyTorch atanh用法及代码示例
- Python PyTorch annotate用法及代码示例
- Python PyTorch argmax用法及代码示例
- Python PyTorch atan用法及代码示例
- Python PyTorch acos用法及代码示例
- Python PyTorch all_gather用法及代码示例
- Python PyTorch avg_pool1d用法及代码示例
- Python PyTorch allreduce_hook用法及代码示例
- Python PyTorch argmin用法及代码示例
- Python PyTorch any用法及代码示例
- Python PyTorch all_to_all用法及代码示例
- Python PyTorch add用法及代码示例
- Python PyTorch addcdiv用法及代码示例
- Python PyTorch acosh用法及代码示例
注:本文由纯净天空筛选整理自pytorch.org大神的英文原创作品 torch.distributed.rpc.functions.async_execution。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。