# Tullio broadcasting issue

**URL:** <https://discourse.julialang.org/t/tullio-broadcasting-issue/107321>\
**Category:** New to Julia\
**Tags:** broadcasting, tullio, tensoroperations\
**Created:** [December 8, 2023, 10:02am UTC](https://discourse.julialang.org/t/tullio-broadcasting-issue/107321 "2023-12-08T10:02:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![swanchristmas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swanchristmas/32/202550_2.png) [@swanchristmas](https://discourse.julialang.org/u/swanchristmas)\
**Post date:** [December 8, 2023, 10:02am UTC](https://discourse.julialang.org/t/tullio-broadcasting-issue/107321/1 "2023-12-08T10:02:31Z")

</div>

```julia
@tensor tmp1[m,s,n,p] := -λ[l,m,s]*ω[l,k]*λ[k,n,p] + λ[l,m,s]*g[l,n]*ones(2)[p])
@tullio tmp2[m,s,n,p] := -λ[l,m,s]*ω[l,k]*λ[k,n,p] + λ[l,m,s]*g[l,n]

```

`tmp1` turns out to be different from `tmp2`. It seems that `@tullio` performs no broadcasting in the p-index, or is there something I missed?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 8, 2023, 11:52am UTC](https://discourse.julialang.org/t/tullio-broadcasting-issue/107321/2 "2023-12-08T11:52:35Z")

</div>

Hi @swanchristmas, can you provide a minimal reproducable example? i.e. [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

For what it’s worth, if I try to construct such an example, I see no difference here:

```julia
julia> using Tullio, TensorOperations

julia> let 
           N, P = 2,3
           A = rand(N)
           B = rand(P)
           C = rand(N)
           @tullio out1[n, p] := A[n] * B[p] + C[n]
           @tullio out2[n, p] := A[n] * B[p] + C[n] * ones(P)[p]
           @tensor out3[n, p] := A[n] * B[p] + C[n] * ones(P)[p]
          
           @info "" out1 out2 out3
       end
┌ Info: 
│ out1 =
│ 2×3 Matrix{Float64}:
│ 0.670981 0.719456 0.856811
│ 0.577745 0.641696 0.822899
│ out2 =
│ 2×3 Matrix{Float64}:
│ 0.670981 0.719456 0.856811
│ 0.577745 0.641696 0.822899
│ out3 =
│ 2×3 Matrix{Float64}:
│ 0.670981 0.719456 0.856811
└ 0.577745 0.641696 0.822899

```

Is it possible there’s an error or typo somewhere in your code that’s causing the discrepancy you’re seeing?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 8, 2023, 11:55am UTC](https://discourse.julialang.org/t/tullio-broadcasting-issue/107321/3 "2023-12-08T11:55:16Z")

</div>

Ah okay, nevermind, I can reproduce the discrepancy you’re seeing by introducing a contracted index:

```julia-repl
julia> let 
           N, M, P = 2,4, 3
           A = rand(N, M)
           B = rand(M, P)
           C = rand(N)
           @tullio out1[n, p] := A[n, m] * B[m, p] + C[n]
           @tullio out2[n, p] := A[n, m] * B[m, p] + C[n] * ones(P)[p]
           @tensor out3[n, p] := A[n, m] * B[m, p] + C[n] * ones(P)[p]
          
           @info "" out1 out2 out3
       end
┌ Info: 
│ out1 =
│ 2×3 Matrix{Float64}:
│ 2.68322 2.8603 2.62903
│ 4.80266 4.25346 4.36135
│ out2 =
│ 2×3 Matrix{Float64}:
│ 2.68322 2.8603 2.62903
│ 4.80266 4.25346 4.36135
│ out3 =
│ 2×3 Matrix{Float64}:
│ 1.31961 1.49669 1.26541
└ 2.12742 1.57822 1.68612

```

It seems like what’s happening here is that instead of calculating

```julia
map(Iterators.product(1:N, 1:P)) do (n, p)
    sum(1:M) do m
        A[n,m] * B[m,p]
    end + C[n]
end

```

Tullio is instead calculating

```julia
map(Iterators.product(1:N, 1:P)) do (n, p)
    sum(1:M) do m
        A[n,m] * B[m,p] + C[n]
    end
end

```

That’s rather unexpected, I didn’t know it did this. cc @mcabbott

I guess this behaviour makes some sense, since Tullio is meant to represent general reductions, but there should maybe be some warnings about this in the README.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [December 8, 2023, 12:42pm UTC](https://discourse.julialang.org/t/tullio-broadcasting-issue/107321/4 "2023-12-08T12:42:51Z")

</div>

This is the correct diagnosis, Tullio always sums the entire RHS. It does not think that `+` is special. [This issue](https://github.com/mcabbott/Tullio.jl/issues/133) has a longer description.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 9, 2023, 11:46am UTC](https://discourse.julialang.org/t/tullio-broadcasting-issue/107321/5 "2023-12-09T11:46:28Z")

</div>

I’ve opened a PR trying to document this: [Add note about reduction scope by MasonProtter · Pull Request #183 · mcabbott/Tullio.jl · GitHub](https://github.com/mcabbott/Tullio.jl/pull/183)
