# scalar() on a zero-rank tensor on GPU

**URL:** <https://itensor.discourse.group/t/scalar-on-a-zero-rank-tensor-on-gpu/1863>\
**Category:** ITensor Julia Questions\
**Created:** [July 9, 2024, 4:17pm UTC](https://itensor.discourse.group/t/scalar-on-a-zero-rank-tensor-on-gpu/1863 "2024-07-09T16:17:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ste](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@ste](https://itensor.discourse.group/u/ste)\
**Post date:** [July 9, 2024, 4:17pm UTC](https://itensor.discourse.group/t/scalar-on-a-zero-rank-tensor-on-gpu/1863/1 "2024-07-09T16:17:27Z")

</div>

hi, I stumbled upon yet another (maybe) quirky behavior of GPU tensors,  
I wanted to compute the entanglement entropy of an MPS on GPU - in order to avoid scalar indexing issues, I was able to do it as (say `S` here is a `diag_itensor` of singular values)

```auto
using ITensors, Metal

    S = mtl(diag_itensor([0.9,0.05,0.05], Index(3), Index(3)))
    S2 = S.^2
    SvN = - S2 * log.(S2) # this gives a rank-0 tensor, so I'd normally 
    SvN = scalar(SvN) # but this gives a scalar ( :) ) indexing error 

```

now, of course I could `scalar(NDTensors.cpu(SvN))`, but I wonder if it’s the only (or at least the intended) way to do it (since I’d like my code to be architecture-agnostic, putting an extra `cpu()` there seems a bit overkill… )

As a matter of fact, I found out that one can cheat a bit and calculate a `sum()` of the GPU 0-dim tensor, which (albeit probably a bit slower) returns me directly a scalar without needing to go through the NDTensors.cpu() (I wonder if it simply does it under the hood…) - is this the best way to go about it?  
thanks!

---

<div class="post-metadata">

**Author:** ![kmp5](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/kmp5/32/251_2.png) [@kmp5](https://itensor.discourse.group/u/kmp5)\
**Post date:** [July 9, 2024, 5:04pm UTC](https://itensor.discourse.group/t/scalar-on-a-zero-rank-tensor-on-gpu/1863/2 "2024-07-09T17:04:42Z")

</div>

Hello,

I believe that this is actually a bug in NDTensors. I implemented the correct version and it is much more performant than the `scalar` function

```julia
julia> @time SvN[]
  0.000015 seconds (4 allocations: 208 bytes)
julia> @time scalar(SvN)
  0.000804 seconds (72 allocations: 1.688 KiB)

```

I will make a PR for this now. Thanks!

Karl

---

<div class="post-metadata">

**Author:** ![ste](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@ste](https://itensor.discourse.group/u/ste)\
**Post date:** [July 9, 2024, 6:41pm UTC](https://itensor.discourse.group/t/scalar-on-a-zero-rank-tensor-on-gpu/1863/3 "2024-07-09T18:41:21Z")

</div>

awesome, thanks Karl!
