From eda4143d6916b364ee83a3f276baf14c3d01de20 Mon Sep 17 00:00:00 2001 From: Shuaiyi Zhang Date: Wed, 7 May 2025 20:23:05 +0800 Subject: [PATCH] python: use torch.set_default_device in blocking_send_recv_example.py Signed-off-by: Shuaiyi Zhang --- examples/python/blocking_send_recv_example.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/examples/python/blocking_send_recv_example.py b/examples/python/blocking_send_recv_example.py index b330c6dace..72dff6ce76 100755 --- a/examples/python/blocking_send_recv_example.py +++ b/examples/python/blocking_send_recv_example.py @@ -44,19 +44,14 @@ def parse_args(): if args.mode != "target": listen_port = 0 + if args.use_cuda: + torch.set_default_device("cuda:0") + config = nixl_agent_config(True, True, listen_port) # Allocate memory and register with NIXL agent = nixl_agent(args.mode, config) - if args.mode == "target" and args.use_cuda: - tensors = [ - torch.ones(10, dtype=torch.float32, device="cuda:0") for _ in range(2) - ] - elif args.use_cuda: - tensors = [ - torch.zeros(10, dtype=torch.float32, device="cuda:0") for _ in range(2) - ] - elif args.mode == "target" and not args.use_cuda: + if args.mode == "target": tensors = [torch.ones(10, dtype=torch.float32) for _ in range(2)] else: tensors = [torch.zeros(10, dtype=torch.float32) for _ in range(2)]