question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dictionary keys cannot be CuPy element

See original GitHub issue

A more answerable question would be, can keys of a dictionary be an element stored in GPU?

  • Code to reproduce
import numpy as np
import cupy as cp

arr = [1, 2, 3]
np_dict = dict()
cp_dict = dict()
np_arr = np.asarray(arr)
cp_arr = cp.asarray(arr)

for i in np_arr:
     np_dict[i] = "okay"

print(np_dict)                                # {1: 'okay', 2: 'okay', 3: 'okay'}

for i in cp_arr:
     cp_dict[i] = "okay"

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: unhashable type: 'cupy.core.core.ndarray'
  • Error messages, stack traces, or logs Conditions (you can just paste the output of python -c 'import cupy; cupy.show_config()')
CuPy Version          : 6.0.0
CUDA Root             : /usr/local/cuda
CUDA Build Version    : 10000
CUDA Driver Version   : 11010
CUDA Runtime Version  : 10000
cuDNN Build Version   : 7301
cuDNN Version         : 7605
NCCL Build Version    : 1000
NCCL Runtime Version  : (unknown)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
aitikguptacommented, Jan 13, 2021

Thanks! 👍🏼

1reaction
kmaehashicommented, Jan 13, 2021

Unlike NumPy, CuPy returns a ndarray when iterating over a ndarray, even if the resulting value is a scalar. This is because the scalar value is on GPU and we don’t want to implicitly transfer it to the host (CPU) memory. You can use ndarray.item() to explicly convert the ndarray (cupy or numpy) into a scalar, e.g., cp_dict[i.item()] = "okay" instead of cp_dict[i] = "okay". https://docs.cupy.dev/en/stable/reference/generated/cupy.ndarray.html#cupy.ndarray.item

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why I can't get dictionary keys by index? - Stack Overflow
So keys is a dictionary itself, but whose key values directly correspond to the "index" at which the keys of d are located....
Read more >
Dictionary<TKey,TValue>.Add(TKey, TValue) Method
An element with the same key already exists in the Dictionary<TKey,TValue>. ... A key cannot be null , but a value can be,...
Read more >
Dictionary | Apple Developer Documentation
When the dictionary's Key and Value types are neither classes nor @objc protocols, any required bridging of elements occurs at the first access...
Read more >
Collections library | Robocorp documentation
First the equality of dictionaries' keys is checked and after that all the key value pairs. If there are differences between the values,...
Read more >
Built-in Types — Python 3.11.1 documentation
Some collection classes are mutable. The methods that add, subtract, or rearrange their members in place, and don't return a specific item, never...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found