-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[WIP] adding batch server/client #702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mrshenli
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
mrshenli:server
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Batch RPC Server Example | ||
|
||
This example shows how to create a batch RPC server using `torch.distributed.rpc` | ||
package, where multiple clients uses RPC to run functions on the server and the | ||
server processes multiple RPC requests together in a batch. | ||
|
||
To try an example with three workers, try running the following three commands | ||
to create three processes. | ||
|
||
|
||
``` | ||
python server.py --name="s" --rank=0 --world_size=3 | ||
python client.py --name="c1" --rank=1 --world_size=3 --server_name="s" | ||
python client.py --name="c2" --rank=2 --world_size=3 --server_name="s" | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import argparse | ||
import threading | ||
import concurrent.futures as futures | ||
|
||
import torch.distributed.rpc as rpc | ||
from torch.distributed.rpc import rpc_sync, rpc_async, remote | ||
|
||
_server = None | ||
|
||
def _run_on_server(name, *args, **kwargs): | ||
return _server.call(name, *args, **kwargs) | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser( | ||
description="Batch RPC example", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
|
||
parser.add_argument('--name') | ||
parser.add_argument('--rank', type=int) | ||
parser.add_argument('--world_size', type=int) | ||
parser.add_argument('--server_name') | ||
return parser.parse_args() | ||
|
||
|
||
class BatchServer: | ||
def __init__(self, batch_size=2): | ||
self._batch_size = batch_size | ||
self._fns = {} | ||
self._inps = {} | ||
self._outs = {} | ||
self.lock = threading.Lock() | ||
global _server | ||
_server = self | ||
|
||
def bind(self, fn): | ||
name = fn.__name__ | ||
self._fns[name] = fn | ||
self._inps[name] = [] | ||
self._outs[name] = futures.Future() | ||
|
||
def call(self, fn_name, *args, **kwargs): | ||
with self.lock: | ||
inps = self._inps[fn_name] | ||
fut = self._outs[fn_name] | ||
idx = len(inps) | ||
inps.append((args, kwargs)) | ||
if idx + 1 >= self._batch_size: | ||
self._inps[fn_name] = [] | ||
self._outs[fn_name] = futures.Future() | ||
|
||
if idx + 1 >= self._batch_size: | ||
rets = [] | ||
for arg, kwargs in inps: | ||
rets.append(self._fns[fn_name](*arg, **kwargs)) | ||
fut.set_result(rets) | ||
|
||
return fut.result()[idx] | ||
|
||
|
||
class BatchClient: | ||
def __init__(self, server_name): | ||
self._server_info = rpc.get_worker_info(worker_name=server_name) | ||
|
||
def __getattr__(self, name): | ||
def fn(*args, **kwargs): | ||
return rpc_sync( | ||
self._server_info, | ||
_run_on_server, | ||
args=(name, *args), | ||
kwargs=kwargs | ||
) | ||
return fn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import os | ||
|
||
import torch.distributed.rpc as rpc | ||
from batch import BatchClient, parse_args | ||
|
||
|
||
if __name__ == "__main__": | ||
os.environ['MASTER_ADDR'] = 'localhost' | ||
os.environ['MASTER_PORT'] = '29500' | ||
args = parse_args() | ||
rpc.init_rpc(args.name, rank=args.rank, world_size=args.world_size) | ||
client = BatchClient(args.server_name) | ||
y = args.rank * 100 | ||
for x in range(5): | ||
print("Client {} got result {}".format(args.name, client.foo(x, y))) | ||
rpc.shutdown() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import os | ||
|
||
import torch.distributed.rpc as rpc | ||
from batch import BatchServer, parse_args | ||
|
||
|
||
|
||
def foo(x, y): | ||
return x + y | ||
|
||
|
||
if __name__ == "__main__": | ||
os.environ['MASTER_ADDR'] = 'localhost' | ||
os.environ['MASTER_PORT'] = '29500' | ||
args = parse_args() | ||
rpc.init_rpc(args.name, rank=args.rank, world_size=args.world_size) | ||
server = BatchServer() | ||
server.bind(foo) | ||
rpc.shutdown() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would this be clearer if we change this to rpc_async? then a single client can eventually trigger a batch, and we could increase the batch size. I suspect this example was created before async was available?