Skip to content
Snippets Groups Projects

[Fix] Producer clone and Tensor copy

Merged Maxence Naud requested to merge fix_207-producer-clone into dev
1 unresolved thread
2 files
+ 33
31
Compare changes
  • Side-by-side
  • Inline
Files
2
  • 77d70664
    - Add move assignment operator
    - Fix 'Tensor::clone()' if original Tensor has no implementation
    - Change behaviour to always perform a shallow copy in case of copy constructor/assignment operator calls
@@ -212,14 +212,13 @@ class Tensor : public Data,
/**
* @brief Copy dimensions, datatype and data from another Tensor.
* If current Tensor already has an implementation, data is copied to the
* existing implementation. Tensor backend/device remain untouched.
* If current Tensor does not have an implementation, only a shallow copy
* is performed and the Tensor will share data with t.
* Tensor backend/device are also copied and only a shallow copy
* is performed for data. Implementation will be shared with original Tensor.
* @param other other Tensor object.
* @return Tensor&
*/
Tensor &operator=(const Tensor& other);
Tensor &operator=(const Tensor& other) = default;
Tensor &operator=(Tensor&& other) = default;
template <typename T>
constexpr Tensor &operator=(Vector<T> &&arr) {
@@ -332,14 +331,17 @@ public:
* @brief Perform a deep copy of the tensor.
*/
Tensor clone() const {
Tensor newTensor(*this);
if (!newTensor.isContiguous()) {
newTensor.makeContiguous();
}
else {
std::shared_ptr<TensorImpl> newImpl = Registrar<Tensor>::create({mImpl->backend(), mDataType})(mImpl->device().second, mDims);
newImpl->copy(mImpl->rawPtr(mImplOffset), mSize);
newTensor.setImpl(newImpl);
Tensor newTensor(*this); // shallow copy
// handle deepcopy of implementation if any
if (newTensor.hasImpl()) {
if (!newTensor.isContiguous()) {
newTensor.makeContiguous();
}
else {
std::shared_ptr<TensorImpl> newImpl = Registrar<Tensor>::create({mImpl->backend(), mDataType})(mImpl->device().second, mDims);
newImpl->copy(mImpl->rawPtr(mImplOffset), mSize);
newTensor.setImpl(newImpl);
}
}
return newTensor;
}
Loading