# Why does \`Flux.stack\` use splatting?

**URL:** <https://discourse.julialang.org/t/why-does-flux-stack-use-splatting/64923>\
**Category:** Machine Learning\
**Tags:** flux\
**Created:** [July 19, 2021, 12:45pm UTC](https://discourse.julialang.org/t/why-does-flux-stack-use-splatting/64923 "2021-07-19T12:45:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bad\_at\_math](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@bad\_at\_math](https://discourse.julialang.org/u/bad_at_math)\
**Post date:** [July 19, 2021, 12:45pm UTC](https://discourse.julialang.org/t/why-does-flux-stack-use-splatting/64923/1 "2021-07-19T12:45:41Z")

</div>

The `Flux.stack` function is defined here: [https://github.com/FluxML/Flux.jl/blob/b78a27b01c9629099adb059a98657b995760b617/src/utils.jl#L476](https://github.com/FluxML/Flux.jl/blob/b78a27b01c9629099adb059a98657b995760b617/src/utils.jl#L476), and it is very simple:  
`stack(xs, dim) = cat(unsqueeze.(xs, dim)..., dims=dim)`.

However, its implementation seems contrary to the “Flux Performance Tips” here: [Performance Tips · Flux](https://fluxml.ai/Flux.jl/stable/performance/#Evaluate-batches-as-Matrices-of-features), specifically " When doing this kind of concatenation use `reduce(hcat, xs)` rather than `hcat(xs...)` . This will avoid the splatting penalty, and will hit the optimised `reduce` method."

Is there a reason for why `Flux.stack` uses splatting rather than `reduce`?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 19, 2021, 1:12pm UTC](https://discourse.julialang.org/t/why-does-flux-stack-use-splatting/64923/2 "2021-07-19T13:12:49Z")

</div>

`hcat` fixes the dimension to 2, `stack` is generic w.r.t. the dimension.
