MCPcopy Create free account
hub / github.com/dmlc/xgboost / Initialize

Method Initialize

src/data/array_interface.h:406–446  ·  view source on GitHub ↗

* \brief Initialize the object, by extracting shape, stride and type. * * The function also perform some basic validation for input array. Lastly it will * also remove trivial dimensions like converting a matrix with shape (n_samples, 1) * to a vector of size n_samples. For for inputs like weights, this should be a 1 * dimension column vector even though user might provide

Source from the content-addressed store, hash-verified

404 * dimension column vector even though user might provide a matrix.
405 */
406 void Initialize(Object::Map const &array) {
407 ArrayInterfaceHandler::Validate(array);
408
409 auto typestr = get<String const>(array.at("typestr"));
410 this->AssignType(StringView{typestr});
411 ArrayInterfaceHandler::ExtractShape(array, shape);
412 std::size_t itemsize = typestr[2] - '0';
413 is_contiguous = ArrayInterfaceHandler::ExtractStride(array, itemsize, shape, strides);
414 n = linalg::detail::CalcSize(shape);
415
416 data = ArrayInterfaceHandler::ExtractData(array, n);
417 static_assert(allow_mask ? D == 1 : D >= 1, "Masked ndarray is not supported.");
418
419 auto alignment = this->ElementAlignment();
420 auto ptr = reinterpret_cast<uintptr_t>(this->data);
421 if (!std::all_of(this->shape, this->shape + D, [](auto v) { return v == 0; })) {
422 CHECK_EQ(ptr % alignment, 0) << "Input pointer misalignment.";
423 }
424
425 if (allow_mask) {
426 common::Span<RBitField8::value_type> s_mask;
427 size_t n_bits = ArrayInterfaceHandler::ExtractMask(array, &s_mask);
428
429 valid = RBitField8(s_mask);
430
431 if (s_mask.data()) {
432 CHECK_EQ(n_bits, n) << "Shape of bit mask doesn't match data shape. "
433 << "XGBoost doesn't support internal broadcasting.";
434 }
435 } else {
436 auto mask_it = array.find("mask");
437 CHECK(mask_it == array.cend() || IsA<Null>(mask_it->second))
438 << "Masked array is not yet supported.";
439 }
440
441 auto stream_it = array.find("stream");
442 if (stream_it != array.cend() && !IsA<Null>(stream_it->second)) {
443 int64_t stream = get<Integer const>(stream_it->second);
444 ArrayInterfaceHandler::SyncCudaStream(stream);
445 }
446 }
447
448 public:
449 ArrayInterface() = default;

Callers 1

ArrayInterfaceMethod · 0.95

Calls 5

AssignTypeMethod · 0.95
CalcSizeFunction · 0.85
ValidateFunction · 0.50
dataMethod · 0.45
cendMethod · 0.45

Tested by

no test coverage detected