| 435 | } |
| 436 | |
| 437 | void AttachBuffer( |
| 438 | int num_samples, |
| 439 | int ndim, |
| 440 | const int64_t *shapes, |
| 441 | daliDataType_t dtype, |
| 442 | const char *layout, |
| 443 | void *data, |
| 444 | const ptrdiff_t *sample_offsets, |
| 445 | daliDeleter_t deleter) override { |
| 446 | ValidateShape(num_samples, ndim, shapes); |
| 447 | Validate(dtype); |
| 448 | |
| 449 | if (!data && num_samples > 0) { |
| 450 | for (int i = 0; i < num_samples; i++) { |
| 451 | auto sample_shape = make_cspan(&shapes[i*ndim], ndim); |
| 452 | |
| 453 | if (volume(sample_shape) > 0) |
| 454 | throw std::invalid_argument( |
| 455 | "The pointer to the data buffer must not be null for a non-empty tensor list."); |
| 456 | |
| 457 | if (sample_offsets && sample_offsets[i]) |
| 458 | throw std::invalid_argument( |
| 459 | "All sample_offsets must be zero when the data pointer is NULL."); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | TensorLayout new_layout = {}; |
| 464 | |
| 465 | if (!layout) { |
| 466 | if (ndim == tl_->sample_dim()) |
| 467 | new_layout = tl_->GetLayout(); |
| 468 | } else { |
| 469 | new_layout = layout; |
| 470 | Validate(new_layout, ndim); |
| 471 | } |
| 472 | |
| 473 | tl_->Reset(); |
| 474 | tl_->SetSize(num_samples); |
| 475 | tl_->set_sample_dim(ndim); |
| 476 | tl_->SetLayout(new_layout); |
| 477 | tl_->set_type(dtype); |
| 478 | ptrdiff_t next_offset = 0; |
| 479 | auto type_info = TypeTable::GetTypeInfo(dtype); |
| 480 | auto element_size = type_info.size(); |
| 481 | |
| 482 | std::shared_ptr<void> buffer; |
| 483 | if (!deleter.delete_buffer && !deleter.destroy_context) { |
| 484 | buffer = std::shared_ptr<void>(data, [](void *){}); |
| 485 | } else { |
| 486 | buffer = std::shared_ptr<void>(data, BufferDeleter{deleter, tl_->order()}); |
| 487 | } |
| 488 | |
| 489 | bool is_contiguous = true; |
| 490 | if (sample_offsets) { |
| 491 | for (int i = 0; i < num_samples; i++) { |
| 492 | if (sample_offsets[i] != next_offset) { |
| 493 | is_contiguous = false; |
| 494 | break; |
nothing calls this directly
no test coverage detected