Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion python/infinicore/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ def strided_from_blob(data_ptr, size, strides, *, dtype=None, device=None):

def from_torch(torch_tensor) -> Tensor:
infini_type = to_infinicore_dtype(torch_tensor.dtype)
infini_device = infinicore.device(torch_tensor.device.type, 0)
device_index = torch_tensor.device.index
infini_device = infinicore.device(
torch_tensor.device.type, 0 if device_index is None else device_index
)
return Tensor(
_infinicore.from_blob(
torch_tensor.data_ptr(),
Expand Down
16 changes: 16 additions & 0 deletions src/infinicore/tensor/copy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ void TensorImpl::copy_from(Tensor src) {
"Cannot copy from tensor with different shape. Src: " + src->info() + " Dst: " + this->info());
}
if (this->device() == src->device()) {
context::setDevice(this->device());
op::rearrange_(Tensor(const_cast<TensorImpl *>(this)->shared_from_this()), src);
} else {
if (!src->is_contiguous()) {
context::setDevice(src->device());
src = src->contiguous();
}

Expand All @@ -50,6 +52,20 @@ void TensorImpl::copy_from(Tensor src) {
context::memcpyH2D(local_src->data(), src->data(), copy_size);
op::rearrange_(Tensor(const_cast<TensorImpl *>(this)->shared_from_this()), local_src);
}
} else {
if (this->device().getType() != src->device().getType()) {
throw std::runtime_error(
"Cannot copy directly between different accelerator types. Src: " + src->info() +
" Dst: " + this->info());
}
context::setDevice(this->device());
if (this->is_contiguous()) {
context::memcpyD2D(this->data(), src->data(), copy_size);
} else {
auto local_src = Tensor::empty(this->shape(), this->dtype(), this->device());
context::memcpyD2D(local_src->data(), src->data(), copy_size);
op::rearrange_(Tensor(const_cast<TensorImpl *>(this)->shared_from_this()), local_src);
}
}
}
}
Expand Down
Loading