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

[BUG]: Converting Complex non-contiguous ND Arrays fails #709

Closed
awni opened this issue Aug 30, 2024 · 2 comments
Closed

[BUG]: Converting Complex non-contiguous ND Arrays fails #709

awni opened this issue Aug 30, 2024 · 2 comments

Comments

@awni
Copy link

awni commented Aug 30, 2024

Problem description

Implictly converting non-contiguous nd arrays to contiguous ones works fine except for complex64 which fails with a type error. My understanding is nanobind should implicitly cast the non-contiguous array to a contiguous one (or at least the behavior should be consistent across types).

TypeError: convert(): incompatible function arguments. The following argument types are supported:
    1. convert(arg: ndarray[writable=False, order='C', device='cpu']) -> None

Invoked with types: ndarray

Reproducible example code

The extension (my_ext.cpp)

#include <iostream>                                  
#include <nanobind/nanobind.h>                       
#include <nanobind/ndarray.h>                        
                                                     
namespace nb = nanobind;                             
                                                     
NB_MODULE(my_ext, m) {                               
    m.def(                                           
        "convert",                                   
        [](nb::ndarray<nb::ro, nb::c_contig, nb::device::cpu>) {
          std::cout << "Worked" << std::endl;        
        },
        nb::arg()                                    
    );  
}  

The Python code:

import my_ext
import numpy as np

c_f32 = np.random.rand(10, 10)
c_c64 = c_f32.astype(np.complex64)

my_ext.convert(c_f32)
my_ext.convert(c_c64)

nc_f32 = c_f32.T
nc_c64 = c_c64.T

my_ext.convert(nc_f32)
my_ext.convert(nc_c64) # <-- Fails
@wjakob
Copy link
Owner

wjakob commented Sep 3, 2024

Thank you for catching this and posting a nice reproducer!

@awni
Copy link
Author

awni commented Sep 3, 2024

Thanks for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants