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.

Buffer Protocol Support in ChainerX ndarray

See original GitHub issue

I want to use mpi4py allreduce in ChainerX ndarray in order to support use of MultiNodeEvaluator in ChainerX network model. However, ChainerX doesn’t support buffer protocol so mpi4py couldn’t handle ChainerX object.

(Actually, I found ChainerX.ndarray wants to support bufferprotocol (https://github.com/chainer/chainer/blob/master/chainerx_cc/chainerx/python/array.cc#L160), but there is no def_buffer)

You can try below simple script.

import chainerx as chx
import numpy as np

from mpi4py import MPI

def check_bp(array):
    try:
        memoryview(array)

        return True
    except BufferError:
        return False


if __name__ == '__main__':
    comm = MPI.COMM_WORLD
    print("MPI_WORLD_SIZE: ", comm.size)

    # NumPy
    rank_np = np.asarray([comm.rank])
    print("NumPy: ", rank_np)
    print("Buffer Protocol Support of NumPy: ", check_bp(rank_np))

    rank_np = comm.allreduce(rank_np)

    print("NumPy AllReduce: ", rank_np)

    # ChainerX
    rank_chx = chx.asarray([comm.rank])
    print("ChainerX: ", rank_chx)
    print("Buffer Protocol Support of ChainerX: ", check_bp(rank_chx))

    rank_chx = comm.allreduce(rank_chx)

    print("ChainerX AllReduce: ", rank_chx)
$ mpiexec -N 2 python test.py
MPI_WORLD_SIZE:  2
MPI_WORLD_SIZE:  2
NumPy:  [1]
Buffer Protocol Support of NumPy:  True
NumPy:  [0]
Buffer Protocol Support of NumPy:  True
NumPy AllReduce:  [1]
NumPy AllReduce:  [1]
ChainerX:  array([0], shape=(1,), dtype=int64, device='native:0')
ChainerX:  array([1], shape=(1,), dtype=int64, device='native:0')
Buffer Protocol Support of ChainerX:  False
Buffer Protocol Support of ChainerX:  False
Traceback (most recent call last):
  File "test.py", line 33, in <module>
    rank_chx = comm.allreduce(rank_chx)
  File "mpi4py/MPI/Comm.pyx", line 1289, in mpi4py.MPI.Comm.allreduce
  File "mpi4py/MPI/msgpickle.pxi", line 1216, in mpi4py.MPI.PyMPI_allreduce
  File "mpi4py/MPI/msgpickle.pxi", line 1168, in mpi4py.MPI.PyMPI_allreduce_intra
  File "mpi4py/MPI/msgpickle.pxi", line 1018, in mpi4py.MPI.PyMPI_reduce_p2p
  File "mpi4py/MPI/msgpickle.pxi", line 950, in mpi4py.MPI.PyMPI_copy
  File "mpi4py/MPI/msgpickle.pxi", line 104, in mpi4py.MPI.Pickle.dump
  File "mpi4py/MPI/msgpickle.pxi", line 91, in mpi4py.MPI.Pickle.cdumps
RuntimeError: return_value_policy = copy, but the object is non-copyable!
Traceback (most recent call last):
  File "test.py", line 33, in <module>
    rank_chx = comm.allreduce(rank_chx)
  File "mpi4py/MPI/Comm.pyx", line 1289, in mpi4py.MPI.Comm.allreduce
  File "mpi4py/MPI/msgpickle.pxi", line 1216, in mpi4py.MPI.PyMPI_allreduce
  File "mpi4py/MPI/msgpickle.pxi", line 1168, in mpi4py.MPI.PyMPI_allreduce_intra
  File "mpi4py/MPI/msgpickle.pxi", line 1018, in mpi4py.MPI.PyMPI_reduce_p2p
  File "mpi4py/MPI/msgpickle.pxi", line 950, in mpi4py.MPI.PyMPI_copy
  File "mpi4py/MPI/msgpickle.pxi", line 104, in mpi4py.MPI.Pickle.dump
  File "mpi4py/MPI/msgpickle.pxi", line 91, in mpi4py.MPI.Pickle.cdumps
RuntimeError: return_value_policy = copy, but the object is non-copyable!
-------------------------------------------------------
Primary job  terminated normally, but 1 process returned
a non-zero exit code.. Per user-direction, the job has been aborted.
-------------------------------------------------------
--------------------------------------------------------------------------
mpiexec detected that one or more processes exited with non-zero status, thus causing
the job to be terminated. The first process to do so was:

  Process name: [[3381,1],1]
  Exit code:    1
--------------------------------------------------------------------------

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
keisukefukudacommented, Jul 18, 2019

@leofang thanks for the info! We’ll consider using it.

1reaction
emcastillocommented, Jun 13, 2019

Hello, We were just discussing this issue, we dropped BufferProtocol support due to conflicts with numpy array conversion. We thought that the most appropriate way is to work on the support for ChainerMN.

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NumPy - pybind11 documentation
Buffer protocol #. Python supports an extremely general and convenient approach for exchanging data between plugin libraries. Types can expose a buffer view...
Read more >
ChainerX Tutorial — Chainer 7.8.1 documentation
The module chainerx aims to support a NumPy compatible interface with additional operations specific to neural networks. It for instance provides chainerx.conv ...
Read more >
New style python buffer protocol and numpy arrays
The np.array function expects an array-like object, not a buffer: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0). Create an array.
Read more >
Chainer Documentation - Read the Docs
Chainer is a powerful, flexible and intuitive deep learning framework. • Chainer supports CUDA computation. It only requires a few lines of ...
Read more >
Buffer Protocol — Python 3.11.1 documentation
Buffer Protocol ¶. Certain objects available in Python wrap access to an underlying memory array or buffer. Such objects include the built-in bytes...
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