MCPcopy Create free account
hub / github.com/pybind/pybind11 / load

Function load

include/pybind11/eigen/tensor.h:178–219  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

176 PYBIND11_TYPE_CASTER(Type, temp_name);
177
178 bool load(handle src, bool convert) {
179 if (!convert) {
180 if (!isinstance<array>(src)) {
181 return false;
182 }
183 array temp = array::ensure(src);
184 if (!temp) {
185 return false;
186 }
187
188 if (!temp.dtype().is(dtype::of<typename Type::Scalar>())) {
189 return false;
190 }
191 }
192
193 array_t<typename Type::Scalar, compute_array_flag_from_tensor<Type>()> arr(
194 reinterpret_borrow<object>(src));
195
196 if (arr.ndim() != Type::NumIndices) {
197 return false;
198 }
199 auto shape = get_shape_for_array<typename Type::Index, Type::NumIndices>(arr);
200
201 if (!Helper::is_correct_shape(shape)) {
202 return false;
203 }
204
205#if EIGEN_VERSION_AT_LEAST(3, 4, 0)
206 auto data_pointer = arr.data();
207#else
208 // Handle Eigen bug
209 auto data_pointer = const_cast<typename Type::Scalar *>(arr.data());
210#endif
211
212 if (is_tensor_aligned(arr.data())) {
213 value = Eigen::TensorMap<const Type, Eigen::Aligned>(data_pointer, shape);
214 } else {
215 value = Eigen::TensorMap<const Type>(data_pointer, shape);
216 }
217
218 return true;
219 }
220
221 static handle cast(Type &&src, return_value_policy policy, handle parent) {
222 if (policy == return_value_policy::reference

Callers

nothing calls this directly

Calls 5

ensureFunction · 0.85
is_tensor_alignedFunction · 0.85
dtypeMethod · 0.45
ndimMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected