You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With numpy, it is possible to define a function that resizes a matrix, that has been passed in, via the use of refcheck=False (with
the usual warnings & caveats). That is, this code:
def resize_array(arr):
arr.resize((3, 4),refcheck=False) # This changes the shape and reallocates memory if necessary
mat = np.random.rand(2, 3)
print("Before resizing:\n", mat)
resize_array(mat)
print("After resizing:\n", mat)
I'm wondering if this is possible to emulate using nanobind. Right now, the following code will do the resize
on the C++ side, but once we return to python, the original matrix size is preserved.
void my_resize(nb::ndarray<double, nb::c_contig, nb::ndim<2>,nb::device::cpu>& nda) {
// resize nda to be 3x4 in all cases, we do not attempt to preserve any original values in this example.
double *new_data = new double[12];
nb::capsule owner(new_data, [](void *p) noexcept { delete[] (double *) p; });
auto new_nda = nb::ndarray< nb::numpy, double, nb::shape<-1, -1>>(
/* data = */ new_data,
/* ndim = */ 2,
/* shape */ new size_t[2] {3, 4},
/* owner = */ owner
);
nda = nb::ndarray<double, nb::c_contig, nb::ndim<2>,nb::device::cpu>(new_nda);
std::cout << "After resizing nda is now " << nda.shape(0) << "x" << nda.shape(1) << std::endl;
return;
}
NB_MODULE(my_ext, m) {
m.def("my_resize", &my_resize);
}
With the following python code,
import my_ext
print("-"*80)
print("testing resize")
def resize_array(arr):
arr.resize((3, 4),refcheck=False) # This changes the shape and reallocates memory if necessary
mat = np.random.rand(2, 3)
print("Before resizing:\n", mat)
resize_array(mat)
print(f"After resizing, shape is {mat.shape}:\n", mat)
print("-"*80)
print("testing nanobind resize")
mat = np.random.rand(2, 3)
print("Before resizing:\n", mat)
my_ext.my_resize(mat)
print(f"After resizing, shape is {mat.shape}:\n", mat)
the following is the output:
--------------------------------------------------------------------------------
testing resize
Before resizing:
[[0.99257607 0.30254559 0.10790966]
[0.3933591 0.29892738 0.30457502]]
After resizing, shape is (3, 4):
[[0.99257607 0.30254559 0.10790966 0.3933591 ]
[0.29892738 0.30457502 0. 0. ]
[0. 0. 0. 0. ]]
--------------------------------------------------------------------------------
testing nanobind resize
Before resizing:
[[0.43815661 0.72994475 0.37792948]
[0.96170208 0.28911954 0.25293229]]
After resizing nda is now 3x4
After resizing, shape is (2, 3):
[[0.43815661 0.72994475 0.37792948]
[0.96170208 0.28911954 0.25293229]]
If you are wondering why I would like to resize in place rather than having a function
that returns a new matrix new_mat = my_resize(old_mat), the reason is that the in-place resize has the opportunity
to minimize the total amount of memory used (when done in C++) and also, when necessary, it can
minimize the amount of time that both any old and new matrix needs to coexist simultaneously.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
With numpy, it is possible to define a function that resizes a matrix, that has been passed in, via the use of refcheck=False (with
the usual warnings & caveats). That is, this code:
will print out:
I'm wondering if this is possible to emulate using nanobind. Right now, the following code will do the resize
on the C++ side, but once we return to python, the original matrix size is preserved.
With the following python code,
the following is the output:
If you are wondering why I would like to resize in place rather than having a function
that returns a new matrix
new_mat = my_resize(old_mat)
, the reason is that the in-place resize has the opportunityto minimize the total amount of memory used (when done in C++) and also, when necessary, it can
minimize the amount of time that both any old and new matrix needs to coexist simultaneously.
Thanks very much for any ideas!
Beta Was this translation helpful? Give feedback.
All reactions