Search code examples
pythoncupy

An error occurred when using the flatten() and reshape() functions of the cupy module


I originally used numpy to run my program, and then I rewritten the program to use cupy, but an error occurred when using the cupy.flatten() function

Here is the minimal example

import cupy
import cudf
df = cudf.DataFrame([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]], index=['a', 'b', 'c']).T
array = df.loc[df['a']%3==1, ['b', 'c']].to_cupy().flatten()

This is the error message it returns

Traceback (most recent call last):
  File "/home/julio/pyy/test/nick_test/test.py", line 4, in <module>
    array = df.loc[df['a']%3==1, ['b', 'c']].to_cupy().flatten()
  File "cupy/_core/core.pyx", line 789, in cupy._core.core._ndarray_base.flatten
  File "cupy/_core/core.pyx", line 808, in cupy._core.core._ndarray_base.flatten
  File "cupy/_core/_routines_manipulation.pyx", line 131, in cupy._core._routines_manipulation._ndarray_flatten
  File "cupy/_core/_routines_manipulation.pyx", line 140, in cupy._core._routines_manipulation._ndarray_flatten_order_c
  File "cupy/_core/core.pyx", line 616, in cupy._core.core._ndarray_base.copy
  File "cupy/_core/core.pyx", line 585, in cupy._core.core._ndarray_base.astype
  File "cupy/_core/_kernel.pyx", line 1310, in cupy._core._kernel.ufunc.__call__
  File "cupy/_core/_kernel.pyx", line 1337, in cupy._core._kernel.ufunc._get_ufunc_kernel
  File "cupy/_core/_kernel.pyx", line 1029, in cupy._core._kernel._get_ufunc_kernel
  File "cupy/_core/_kernel.pyx", line 72, in cupy._core._kernel._get_simple_elementwise_kernel
  File "cupy/_core/core.pyx", line 2232, in cupy._core.core.compile_with_cache
  File "/home/julio/miniconda3/envs/nick-rapids-22.10/lib/python3.9/site-packages/cupy/cuda/compiler.py", line 493, in _compile_module_with_cache
    return _compile_with_cache_cuda(
  File "/home/julio/miniconda3/envs/nick-rapids-22.10/lib/python3.9/site-packages/cupy/cuda/compiler.py", line 601, in _compile_with_cache_cuda
    with tempfile.NamedTemporaryFile(dir=cache_dir, delete=False) as tf:
  File "/home/julio/miniconda3/envs/nick-rapids-22.10/lib/python3.9/tempfile.py", line 545, in NamedTemporaryFile
    (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "/home/julio/miniconda3/envs/nick-rapids-22.10/lib/python3.9/tempfile.py", line 255, in _mkstemp_inner
    fd = _os.open(file, flags, 0o600)
PermissionError: [Errno 13] Permission denied: '/home/julio/.cupy/kernel_cache/tmp5m2yd92w'

I've tried using reshape(-1) but it returns a similar error, the only difference is that the last line is

PermissionError: [Errno 13] Permission denied: '/home/julio/.cupy/kernel_cache/tmpb9990pi6'

The system is ubuntu 22.04, cupy version is 11.3.0.

I encountered similar problems in the load() and full() functions, but I managed to avoid those problems. But the problem of flatten() and reshape() must be solved


Solution

  • It seems that you are having permission issues on CuPy's default cache directory. The simplest way you could try resolving that is simply to remove the directory and rerun your code to if it is recreated with correct permissions:

    rm -rf /home/julio/.cupy
    

    If that still does not work, you may have some system configuration (such as umask) managing permission in some unexpected way. Assuming your username is julio, you could attempt to correct permissions manually, e.g.:

    chmod 700 /home/julio/.cupy
    chown -R julio /home/julio/.cupy
    find /home/julio/.cupy -type d exec chmod 700 {} \;
    find /home/julio/.cupy -type f exec chmod 600 {} \;
    

    If it is intentional that you do not have permissions or do not want to write CuPy cache under /home/julio/.cupy, you are allowed to change that to a custom directory . You could then set that environment variable to a directory you are certain you have permissions to create directories/write files, for example CUPY_CACHE_DIR=/tmp/julio-cupy-cache. Please note it's advised you keep the cache in a persistent directory, that will prevent requiring to JIT-compile kernels after a reboot for example.