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

cuda tensor will raise an exception in np.asanyarray #152

Closed
Trinkle23897 opened this issue Jul 20, 2020 · 3 comments · Fixed by #147
Closed

cuda tensor will raise an exception in np.asanyarray #152

Trinkle23897 opened this issue Jul 20, 2020 · 3 comments · Fixed by #147
Labels
bug Something isn't working

Comments

@Trinkle23897
Copy link
Collaborator

Trinkle23897 commented Jul 20, 2020

In [2]: a=Batch(a={})

In [3]: b=Batch(a=torch.tensor([3,4], device='cuda'))

In [4]: Batch.cat([a,b])

will raise an exception. Same as Batch.stack:

~/github/tianshou-new/tianshou/data/batch.py in _is_scalar(value)
     37     # the check of dict / Batch is omitted because this only checks a value.
     38     # a dict / Batch will eventually check their values
---> 39     value = np.asanyarray(value)
     40     return value.size == 1 and not value.shape
     41 

~/.local/lib/python3.6/site-packages/numpy/core/_asarray.py in asanyarray(a, dtype, order)
    136 
    137     """
--> 138     return array(a, dtype, copy=False, order=order, subok=True)
    139 
    140 

~/.local/lib/python3.6/site-packages/torch/tensor.py in __array__(self, dtype)
    490     def __array__(self, dtype=None):
    491         if dtype is None:
--> 492             return self.numpy()
    493         else:
    494             return self.numpy().astype(dtype, copy=False)

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
@Trinkle23897 Trinkle23897 added the bug Something isn't working label Jul 20, 2020
@Trinkle23897 Trinkle23897 changed the title CUDA test cuda tensor will raise an exception in np.asanyarray Jul 20, 2020
@youkaichao
Copy link
Collaborator

youkaichao commented Jul 20, 2020

Should modify _is_scalar .

def _is_scalar(value: Any) -> bool:
    # check if the value is a scalar
    # 1. python bool object, number object: isinstance(value, Number)
    # 2. numpy scalar: isinstance(value, np.generic)
    # 3. python object rather than dict / Batch / tensor
    # the check of dict / Batch is omitted because this only checks a value.
    # a dict / Batch will eventually check their values
    if isinstance(value, (dict, Batch)):
        return False
    #=================== add these two lines
    if isinstance(value, torch.Tensor):
        return value.numel() == 1 and not value.shape
    value = np.asanyarray(value)
    return value.size == 1 and not value.shape

@duburcqa
Copy link
Collaborator

It could be added to #147

@duburcqa
Copy link
Collaborator

Fixed by #147

@duburcqa duburcqa linked a pull request Jul 21, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants