Skip to content
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

Support cuda tensor in vineyard. #2019

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion python/vineyard/contrib/ml/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from vineyard.core import context
from vineyard.data.utils import build_buffer
from vineyard.data.utils import from_json
from vineyard.data.utils import normalize_cpptype

Check warning on line 47 in python/vineyard/contrib/ml/torch.py

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

Unknown word (cpptype)
from vineyard.data.utils import to_json

torch = lazy_import.lazy_module("torch")
Expand Down Expand Up @@ -97,11 +97,11 @@
return torch.float16
if dtype == 'torch.bfloat16':
return torch.bfloat16
if dtype in ['torch.complex32', 'torch.chalf']:

Check warning on line 100 in python/vineyard/contrib/ml/torch.py

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

Unknown word (chalf)
return torch.complex32
if dtype in ['torch.complex64', 'torch.cfloat']:

Check warning on line 102 in python/vineyard/contrib/ml/torch.py

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

Unknown word (cfloat)
return torch.complex64
if dtype in ['torch.complex128', 'torch.cdouble']:

Check warning on line 104 in python/vineyard/contrib/ml/torch.py

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

Unknown word (cdouble)
return torch.complex128
if dtype == 'torch.uint8':
return torch.uint8
Expand All @@ -123,10 +123,10 @@
return torch.bool
if dtype == 'torch.quint8':
return torch.quint8
if dtype == 'torch.qint8':

Check warning on line 126 in python/vineyard/contrib/ml/torch.py

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

Unknown word (qint)
return torch.qint8

Check warning on line 127 in python/vineyard/contrib/ml/torch.py

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

Unknown word (qint)
if dtype == 'torch.qint32':

Check warning on line 128 in python/vineyard/contrib/ml/torch.py

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

Unknown word (qint)
return torch.qint32

Check warning on line 129 in python/vineyard/contrib/ml/torch.py

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

Unknown word (qint)
if dtype == 'torch.quint4x2':
return torch.quint4x2
if dtype == 'torch.float8_e4m3fn':
Expand All @@ -145,7 +145,12 @@

meta['typename'] = 'vineyard::Tensor<%s>' % str(value.dtype)
meta['value_type_'] = str(value.dtype)
meta.add_member('buffer_', build_torch_buffer(client, value))
if value.is_cuda:
meta['device_'] = str(value.device)
value_in_cpu = value.to('cpu')
meta.add_member('buffer_', build_torch_buffer(client, value_in_cpu))
else:
meta.add_member('buffer_', build_torch_buffer(client, value))

return client.create_metadata(meta)

Expand All @@ -157,16 +162,20 @@
value_type = normalize_tensor_dtype(value_type_name)
shape = from_json(meta['shape_'])
order = from_json(meta.get('order_', 'C'))
device = meta.get('device_', 'cpu')

if np.prod(shape) == 0:
return torch.zeros(shape, dtype=value_type)

buffer = (ctypes.c_char * int(np.prod(shape)) * value_type.itemsize).from_address(

Check warning on line 170 in python/vineyard/contrib/ml/torch.py

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

Unknown word (itemsize)
(obj.member('buffer_').address)
)

c_tensor = torch.frombuffer(buffer, dtype=value_type).reshape(shape)

Check warning on line 174 in python/vineyard/contrib/ml/torch.py

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

Unknown word (frombuffer)
tensor = c_tensor if order == 'C' else c_tensor.contiguous()
if "cuda" in device:
cuda_device = torch.device(device)
tensor = c_tensor.to(cuda_device)

return tensor

Expand Down
Loading