# Best way to forcibly passify a result using ForwardDiff?

**URL:** <https://discourse.julialang.org/t/best-way-to-forcibly-passify-a-result-using-forwarddiff/135283>\
**Category:** Specific Domains\
**Tags:** question, forwarddiff, autodiff\
**Created:** [January 27, 2026, 8:58am UTC](https://discourse.julialang.org/t/best-way-to-forcibly-passify-a-result-using-forwarddiff/135283 "2026-01-27T08:58:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![afleming](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/afleming/32/208443_2.png) [@afleming](https://discourse.julialang.org/u/afleming)\
**Post date:** [January 27, 2026, 8:58am UTC](https://discourse.julialang.org/t/best-way-to-forcibly-passify-a-result-using-forwarddiff/135283/1 "2026-01-27T08:58:51Z")

</div>

I’m working on the implementation of a time-stepping method and would like to force that part of the computation _always_ be a passive value (in the AD sense).

The function in question is a simple broadcasted computation, followed by a weighted average:

```julia
const _WENO_OPTIMAL_STENCIL_WEIGHTS = @SMatrix [
    0.0 0.0 0.0
    1/3 2/3 0.0
    1/10 6/10 3/10
]

# ISK is guaranteed to be a an array of length R
function _weno_weights(ISk, ::Val{R}; ε = 1.0e-6, p = 2) where {R}
    C_kR = _WENO_OPTIMAL_STENCIL_WEIGHTS[:, R]
    alpha_k = map((C, IS) -> C*inv((ε+IS)^p), C_kR, ISk)
    w_k = alpha_k / sum(alpha_k)
    return w_k
end

```

And what I would like to do, if `ISk` is an `AbstractArray` of `ForwardDiff.Dual{...}`, is set the `partials` to zero, but I’m not sure what the recommended way to do this is. Here’s my solution so far, but I don’t think this will work with chunking.

```julia
# We want to enforce that w_k is always passive
# (to avoid information moving downwind)
function _weno_weights(
    ISk::AbstractArray{<:Dual{T}},
    order::Val{R};
    ε = 1.0e-6,
    p = 2,
) where {T,R}
    C_kR = _WENO_OPTIMAL_STENCIL_WEIGHTS[:, R]
    alpha_k = map((C, IS) -> C * inv((ε + value(IS))^p), C_kR, ISk)
    tot = sum(alpha_k)
    w_k = map(alpha_k) do a
        Dual{T}(a / tot, zero(a))
    end
    return w_k
end

```

Is there a recommended way to do this? Is there a convenience function/macro/example I could look at anywhere?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [January 27, 2026, 10:07am UTC](https://discourse.julialang.org/t/best-way-to-forcibly-passify-a-result-using-forwarddiff/135283/2 "2026-01-27T10:07:31Z")

</div>

There’s `ForwardDiff.value` but it’s worth asking yourself whether you actually want to do that and why. In particular, it may screw up higher-order stuff if someone ever tries to differentiate through your own code

---

<div class="post-metadata">

**Author:** ![afleming](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/afleming/32/208443_2.png) [@afleming](https://discourse.julialang.org/u/afleming)\
**Post date:** [January 28, 2026, 9:41am UTC](https://discourse.julialang.org/t/best-way-to-forcibly-passify-a-result-using-forwarddiff/135283/3 "2026-01-28T09:41:30Z")

</div>

The reason why I’d like to do this is that the choice of w\_k can allow derivative information to “leak” in the wrong direction, since each w\_k must be normalized, which includes information we’d like to throw away.

It seems like a reasonable choice to force w\_k to always be a passive scalar (or at least always have value-of-derivative component equal to zero).

I ultimately settled on

```julia
tot = sum(alpha_k)
w_k = map(alpha_k) do a
    # N is the number of partials (Dual{T, V, N})
    Dual{T}(a / tot, ntuple(Returns(zero(a)), N))
end

```

which shouldn’t break type stability for higher-order differentiation. (fingers crossed).

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [January 28, 2026, 10:30am UTC](https://discourse.julialang.org/t/best-way-to-forcibly-passify-a-result-using-forwarddiff/135283/4 "2026-01-28T10:30:33Z")

</div>

It’s not a question of type stability, more of “what if someone tries to differentiate through my code with respect to parameters which I thought were constant”?
