| 319 | } |
| 320 | |
| 321 | void MetaInfo::LoadBinary(dmlc::Stream* fi) { |
| 322 | auto version = Version::Load(fi); |
| 323 | auto major = std::get<0>(version); |
| 324 | auto minor = std::get<1>(version); |
| 325 | // MetaInfo is saved in `SparsePageSource'. So the version in MetaInfo represents the |
| 326 | // version of DMatrix. |
| 327 | std::stringstream msg; |
| 328 | msg << "Binary DMatrix generated by XGBoost: " << Version::String(version) |
| 329 | << " is no longer supported. " |
| 330 | << "Please process and save your data in current version: " |
| 331 | << Version::String(Version::Self()) << " again."; |
| 332 | CHECK_GE(major, 3) << msg.str(); |
| 333 | CHECK_GE(minor, 1) << msg.str(); |
| 334 | |
| 335 | const uint64_t expected_num_field = kNumField; |
| 336 | uint64_t num_field{0}; |
| 337 | CHECK(fi->Read(&num_field)) << "MetaInfo: invalid format"; |
| 338 | size_t expected = 0; |
| 339 | if (major == 1 && std::get<1>(version) < 2) { |
| 340 | // feature names and types are added in 1.2 |
| 341 | expected = expected_num_field - 2; |
| 342 | } else { |
| 343 | expected = expected_num_field; |
| 344 | } |
| 345 | CHECK_GE(num_field, expected) << "MetaInfo: insufficient number of fields (expected at least " |
| 346 | << expected << " fields, but the binary file only contains " |
| 347 | << num_field << "fields.)"; |
| 348 | if (num_field > expected_num_field) { |
| 349 | LOG(WARNING) << "MetaInfo: the given binary file contains extra fields " |
| 350 | "which will be ignored."; |
| 351 | } |
| 352 | |
| 353 | LoadScalarField(fi, u8"num_row", DataType::kUInt64, &num_row_); |
| 354 | LoadScalarField(fi, u8"num_col", DataType::kUInt64, &num_col_); |
| 355 | LoadScalarField(fi, u8"num_nonzero", DataType::kUInt64, &num_nonzero_); |
| 356 | LoadTensorField(fi, u8"labels", DataType::kFloat32, &labels); |
| 357 | LoadVectorField(fi, u8"group_ptr", DataType::kUInt32, &group_ptr_); |
| 358 | LoadVectorField(fi, u8"weights", DataType::kFloat32, &weights_); |
| 359 | LoadTensorField(fi, u8"base_margin", DataType::kFloat32, &base_margin_); |
| 360 | LoadVectorField(fi, u8"labels_lower_bound", DataType::kFloat32, &labels_lower_bound_); |
| 361 | LoadVectorField(fi, u8"labels_upper_bound", DataType::kFloat32, &labels_upper_bound_); |
| 362 | |
| 363 | LoadVectorField(fi, u8"feature_names", DataType::kStr, &feature_names); |
| 364 | LoadVectorField(fi, u8"feature_types", DataType::kStr, &feature_type_names); |
| 365 | LoadVectorField(fi, u8"feature_weights", DataType::kFloat32, &feature_weights); |
| 366 | |
| 367 | this->has_categorical_ = LoadFeatureType(feature_type_names, &feature_types.HostVector()); |
| 368 | |
| 369 | std::vector<char> values; |
| 370 | LoadVectorField(fi, u8"cats", DataType::kStr, &values); |
| 371 | auto jcats = Json::Load(StringView{values.data(), values.size()}, std::ios::binary); |
| 372 | this->cats_->Load(jcats); |
| 373 | } |
| 374 | |
| 375 | namespace { |
| 376 | template <typename T> |