# Sending a tensor (e.g. constant) to device

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsending-a-tensor-e-g-constant-to-device%2F15032%3E%5C
**Category:** 🤗Accelerate\
**Created:** [February 22, 2022, 3:09pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsending-a-tensor-e-g-constant-to-device%2F15032 "2022-02-22T15:09:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![borgr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/borgr/32/24353_2.png) [@borgr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fborgr)%5C
**Post date:** [February 22, 2022, 3:09pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsending-a-tensor-e-g-constant-to-device%2F15032%2F1 "2022-02-22T15:09:40Z")

</div>

Probably a beginner’s question:

I couldn’t find a simple way to send a tensor to the device. If I have a constant (e.g. 0, or frozen weights for l2 regularization) Do I have to add them to a model for the accelerator to send them to a device (through prepare)

I would expect for example:  
` const = accelerator.prepare(torch.tensor(0.))`

Currently, code that worked for me was (for future readers):  
` l2_reg = torch.tensor(0, dtype=loss.dtype).to(loss.device) # loss some parameter that was in the model hence prepared regularly`

---

<div class="post-metadata">

**Author:** ![muellerzr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/muellerzr/32/21583_2.png) [@muellerzr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmuellerzr)%5C
**Post date:** [March 7, 2022, 4:56pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsending-a-tensor-e-g-constant-to-device%2F15032%2F2 "2022-03-07T16:56:36Z")

</div>

> [@borgr](#):
>
> Currently, code that worked for me was (for future readers):  
> ` l2_reg = torch.tensor(0, dtype=loss.dtype).to(loss.device) # loss some parameter that was in the model hence prepared regularly`

Great question! Your recommendation here is actually quite close. `prepare` doesn’t work with raw tensors, so you do need to change the device on that one manually. You can ensure it will always work by setting it to `accelerator.device` rather than `loss.device` 🙂
