GreenWaves-Technologies / bfloat16

bfloat16 dtype for numpy
Apache License 2.0
17 stars 13 forks source link

install fails with error: lvalue required as left operand of assignment #5

Open amuthl opened 1 year ago

amuthl commented 1 year ago

running setup.py install fails with this error:

bfloat16# python setup.py install
/workspaces/.venv/lib/python3.11/site-packages/setuptools/dist.py:775: UserWarning: Usage of dash-separated 'description-file' will not be supported in future versions. Please use the underscore name 'description_file' instead
  warnings.warn(
/workspaces/.venv/lib/python3.11/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
/workspaces/.venv/lib/python3.11/site-packages/setuptools/command/easy_install.py:146: EasyInstallDeprecationWarning: easy_install command is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
warning: no files found matching 'bfloat16.h'
bfloat16.cc: In member function ‘greenwaves::{anonymous}::bfloat16 greenwaves::{anonymous}::ufuncs::NextAfter::operator()(greenwaves::{anonymous}::bfloat16, greenwaves::{anonymous}::bfloat16)’:
bfloat16.cc:1663:63: warning: ‘void* memcpy(void*, const void*, size_t)’ copying an object of non-trivial type ‘using bfloat16 = struct Eigen::bfloat16’ {aka ‘struct Eigen::bfloat16’} from an array of ‘uint16_t’ {aka ‘short unsigned int’} [-Wclass-memaccess]
 1663 |                                                         memcpy(&out, &out_int, sizeof(bfloat16));
      |                                                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from eigen/Eigen/Core:170,
                 from bfloat16.cc:33:
eigen/Eigen/src/Core/arch/Default/BFloat16.h:58:8: note: ‘using bfloat16 = struct Eigen::bfloat16’ {aka ‘struct Eigen::bfloat16’} declared here
   58 | struct bfloat16 : public bfloat16_impl::bfloat16_base {
      |        ^~~~~~~~
bfloat16.cc:1675:47: warning: ‘void* memcpy(void*, const void*, size_t)’ copying an object of non-trivial type ‘using bfloat16 = struct Eigen::bfloat16’ {aka ‘struct Eigen::bfloat16’} from an array of ‘uint16_t’ {aka ‘short unsigned int’} [-Wclass-memaccess]
 1675 |                                         memcpy(&out, &out_int, sizeof(bfloat16));
      |                                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
eigen/Eigen/src/Core/arch/Default/BFloat16.h:58:8: note: ‘using bfloat16 = struct Eigen::bfloat16’ {aka ‘struct Eigen::bfloat16’} declared here
   58 | struct bfloat16 : public bfloat16_impl::bfloat16_base {
      |        ^~~~~~~~
In file included from /usr/include/python3.11/Python.h:44,
                 from bfloat16.cc:27:
bfloat16.cc: In function ‘bool greenwaves::Initialize()’:
/usr/include/python3.11/object.h:136:30: error: lvalue required as left operand of assignment
  136 | #  define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
      |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~
bfloat16.cc:1748:17: note: in expansion of macro ‘Py_TYPE’
 1748 |                 Py_TYPE(&NPyBfloat16_Descr) = &PyArrayDescr_Type;
      |                 ^~~~~~~
error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1

On Debian Bookworm, GCC 12, Python 3.11, Numpy 1.24.3.

The issue is that Py_TYPE is a function returning a PyTypeObject* so it can't be assigned a value. Maybe there is a missing dereference ?

amuthl commented 1 year ago

The fix is to use Py_SET_TYPE function which was added in python 3.9: Py_SET_TYPE(&NPyBfloat16_Descr, &PyArrayDescr_Type);

See also: https://github.com/python/steering-council/issues/79