Skip to content

Commit

Permalink
Reduce the PR size.
Browse files Browse the repository at this point in the history
Signed-off-by: Michał Zientkiewicz <[email protected]>
  • Loading branch information
mzient committed Sep 18, 2024
1 parent 7610ac0 commit df16976
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions dali/pipeline/data/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,35 @@ class Tensor : public Buffer<Backend> {
* individually. The device_id describes the location of the memory and the order can describe
* the dependency on the work that is happening on another device.
*/
void ShareData(shared_ptr<void> ptr, size_t bytes, bool pinned,
const TensorShape<> &shape, DALIDataType type, int device_id,
AccessOrder order = {});
inline void ShareData(shared_ptr<void> ptr, size_t bytes, bool pinned,
const TensorShape<> &shape, DALIDataType type, int device_id,
AccessOrder order = {}) {
Index new_size = volume(shape);
DALI_ENFORCE(new_size == 0 || type != DALI_NO_TYPE,
"Only empty tensors can be shared without specifying a type.");

// Free the underlying storage.
if (!same_managed_object(data_, ptr))
free_storage();

// Set the new order, if provided.
if (order)
this->set_order(order);

// Save our new pointer and bytes. Reset our type, shape, and size
type_ = TypeTable::GetTypeInfo(type);
data_ = std::move(ptr);
size_ = new_size;
num_bytes_ = bytes;
device_ = device_id;

// If the input pointer stores a non-zero size allocation, mark
// that we are sharing our underlying data
shares_data_ = num_bytes_ > 0 ? true : false;
pinned_ = pinned;

shape_ = shape;
}

/**
* @brief Interprets a raw allocation as a tensor with given shape.
Expand Down Expand Up @@ -433,39 +459,6 @@ class Tensor : public Buffer<Backend> {
friend class TensorList;
};


template <typename Backend>
void Tensor<Backend>::ShareData(shared_ptr<void> ptr, size_t bytes, bool pinned,
const TensorShape<> &shape, DALIDataType type, int device_id,
AccessOrder order) {
Index new_size = volume(shape);
DALI_ENFORCE(new_size == 0 || type != DALI_NO_TYPE,
"Only empty tensors can be shared without specifying a type.");

// Free the underlying storage.
if (!same_managed_object(data_, ptr))
free_storage();

// Set the new order, if provided.
if (order)
this->set_order(order);

// Save our new pointer and bytes. Reset our type, shape, and size
type_ = TypeTable::GetTypeInfo(type);
data_ = std::move(ptr);
size_ = new_size;
num_bytes_ = bytes;
device_ = device_id;

// If the input pointer stores a non-zero size allocation, mark
// that we are sharing our underlying data
shares_data_ = num_bytes_ > 0 ? true : false;
pinned_ = pinned;

shape_ = shape;
}


} // namespace dali

#endif // DALI_PIPELINE_DATA_TENSOR_H_

0 comments on commit df16976

Please sign in to comment.