# Optimizing concatenation of tensors

**URL:** <https://discuss.pytorch.org/t/optimizing-concatenation-of-tensors/183438>\
**Category:** Uncategorized\
**Created:** [July 4, 2023, 5:57pm UTC](https://discuss.pytorch.org/t/optimizing-concatenation-of-tensors/183438 "2023-07-04T17:57:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PabloVD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pablovd/32/46511_2.png) [@PabloVD](https://discuss.pytorch.org/u/PabloVD)\
**Post date:** [July 4, 2023, 5:57pm UTC](https://discuss.pytorch.org/t/optimizing-concatenation-of-tensors/183438/1 "2023-07-04T17:57:42Z")

</div>

I’m dealing with concatenation of tensors in cpu and gpu, and I’m trying to optimize the performance of the process.

Which option should be faster/preferred (if any)?

```python
torch.cat([x.view(1,3) for x in array],dim=0).to(device)

```

or

```python
torch.cat([x.view(1,3).to(device) for x in array],dim=0)

```

where `device="cuda"`.

In other words, is faster to employ `torch.cat` with tensors in cuda, or should I rather move to cpu after the concatenation? Does it depend on the specific case?

Many thanks in advance!

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 4, 2023, 7:00pm UTC](https://discuss.pytorch.org/t/optimizing-concatenation-of-tensors/183438/2 "2023-07-04T19:00:19Z")

</div>

> [@PabloVD](#):
>
> Does it depend on the specific case?

Yes, as usual.

Triggering a single copy kernel should be faster than calling them in a loop. However, in your use case it would also depend on the available GPU memory as the `torch.cat` call would increase it assuming `x` resides on the GPU and `device` is set to `"cpu"`.

In the end it would also depend on the tensor shapes, number of tensors, etc., so you might want to profile your actual workload.

---

<div class="post-metadata">

**Author:** ![PabloVD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pablovd/32/46511_2.png) [@PabloVD](https://discuss.pytorch.org/u/PabloVD)\
**Post date:** [July 5, 2023, 2:33pm UTC](https://discuss.pytorch.org/t/optimizing-concatenation-of-tensors/183438/3 "2023-07-05T14:33:31Z")

</div>

Thanks for the reply, thus I will perform a detailed profiling to check which suits better to my use case.  
Many thanks!
