# If EnzymeRules.inactive() is defined, does the Enzyme.jl operation still take place?

**URL:** <https://discourse.julialang.org/t/if-enzymerules-inactive-is-defined-does-the-enzyme-jl-operation-still-take-place/113369>\
**Category:** Numerics\
**Tags:** question, autodiff, enzyme\
**Created:** [April 23, 2024, 3:51am UTC](https://discourse.julialang.org/t/if-enzymerules-inactive-is-defined-does-the-enzyme-jl-operation-still-take-place/113369 "2024-04-23T03:51:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ToPo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/topo/32/205868_2.png) [@ToPo](https://discourse.julialang.org/u/ToPo)\
**Post date:** [April 23, 2024, 3:51am UTC](https://discourse.julialang.org/t/if-enzymerules-inactive-is-defined-does-the-enzyme-jl-operation-still-take-place/113369/1 "2024-04-23T03:51:07Z")

</div>

When performing automatic differentiation using Enzyme.jl, Errors and warnings occur in functions that define EnzymeRules.inactive().

I thought that the application of Enzyme.jl was ignored when EnzymeRules.inactive() was used.  
But is that not the case, and Enzyme.jl is applied and simply the return value of the function is treated as a constant?

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [April 23, 2024, 5:24am UTC](https://discourse.julialang.org/t/if-enzymerules-inactive-is-defined-does-the-enzyme-jl-operation-still-take-place/113369/2 "2024-04-23T05:24:05Z")

</div>

Marking a function as inactive in Enzyme means that it won’t be differentiated. The original function call will still be run (as its values may be needed/produce observable side effects/etc)

---

<div class="post-metadata">

**Author:** ![ToPo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/topo/32/205868_2.png) [@ToPo](https://discourse.julialang.org/u/ToPo)\
**Post date:** [April 23, 2024, 5:31am UTC](https://discourse.julialang.org/t/if-enzymerules-inactive-is-defined-does-the-enzyme-jl-operation-still-take-place/113369/3 "2024-04-23T05:31:58Z")

</div>

Does that mean that if I use a function marked inactive() but not supported by Enzyme.jl, I will get an error?

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [April 23, 2024, 5:41am UTC](https://discourse.julialang.org/t/if-enzymerules-inactive-is-defined-does-the-enzyme-jl-operation-still-take-place/113369/4 "2024-04-23T05:41:41Z")

</div>

> [@ToPo](#):
>
> ctive() was used.  
> But is that not the case, and [Enzyme.jl](https://juliahub.com/ui/Packages/General/Enzyme) is applied and simply the return value of

Generally marking it as inactive means most Enzyme errors will go away (since we don’t need to differentiate it).

Not all may go away, however. For example if Julia generates some weird GC we don’t tunderstand Enzyme will throw an error rather than risk a segfault.

So it depends – what error are you seeing?

---

<div class="post-metadata">

**Author:** ![ToPo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/topo/32/205868_2.png) [@ToPo](https://discourse.julialang.org/u/ToPo)\
**Post date:** [April 23, 2024, 6:50am UTC](https://discourse.julialang.org/t/if-enzymerules-inactive-is-defined-does-the-enzyme-jl-operation-still-take-place/113369/5 "2024-04-23T06:50:56Z")

</div>

Since we are running this in a larger program, it may not be common, but it should show the warnings and errors that are being output.

- Warning (This occurs often in Enzyme.jl.)

```julia
┌ Warning: Using fallback BLAS replacements for (["dsymv_64_", "dsyrk_64_"]), performance may be degraded
└ @ Enzyme.Compiler ~/.julia/packages/GPUCompiler/kqxyC/src/utils.jl:59

```

- Error Description  
The following error does not occur on macOS, but does occur on Linux (Ubuntu).  
Note that the function make\_boundary\_Ks\_and\_Fs(), which is in error, is also marked as inactive().  
The part of the vector that is in direct error is the part where the vector is zero-cleared.

```julia
 Fe = zeros(Float64, num_dof)

```

```julia
ERROR: Enzyme execution failed.
Mismatched activity for: %value_phi33 = phi {} addrspace(10)* [%62, %L101], [%66, %L104] 
const val: %62 = call fastcc nonnull {} addrspace(10)* @julia_make_dNdxi_16061({ i64, i64, {} addrspace(10)*, {} addrspace(10)*, i64, {} addrspace(10)*, {} addrspace(10)*, double, i64, {} addrspace(10)*, i64, {} addrspace(10)*, {} addrspace(10)* } addrspace(11)* noalias nocapture nofree nonnull readnone align 8 dereferenceable(104) undef, {} addrspace(10)* nofree noundef nonnull align 16 dereferenceable(40) %57) #128, !dbg !263
value=Unknown object of type Matrix{Float64}
You may be using a constant variable as temporary storage for active memory (https://enzyme.mit.edu/julia/stable/faq/#Activity-of-temporary-storage). If not, please open an issue, and either rewrite this variable to not be conditionally active or use Enzyme.API.runtimeActivity!(true) as a workaround for now

```

Stacktrace is as follows:

```julia
Stacktrace:
  [1] throwerr(cstr::Cstring)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:1285
  [2] iterate
    @ ./range.jl:901 [inlined]
  [3] fill!
    @ ./array.jl:396 [inlined]
  [4] zeros
    @ ./array.jl:637 [inlined]
  [5] zeros
    @ ./array.jl:632 [inlined]
  [6] make_boundary_Ke_and_Fe
    @ ~/my_programs/JuliaOptOS/src/EnzymeOptOS/solid_mechanics/element/structural_quad4_boundary.jl:54
  [7] make_boundary_Ks_and_Fs
    @ ~/my_programs/JuliaOptOS/src/EnzymeOptOS/solid_mechanics/boundary_condition/robin_bc.jl:31
  [8] dynamic_structural_compliance_pnorm
    @ ~/my_programs/JuliaOptOS/src/EnzymeOptOS/evaluate_functions/dynamic_linear_elastic_systems/dynamic_structural_compliance_pnorm.jl:81
  [9] f
    @ ~/my_programs/JuliaOptOS/src/EnzymeOptOS/evaluate_functions/dynamic_linear_elastic_systems/dynamic_structural_compliance_pnorm.jl:21 [inlined]
 [10] f
    @ ~/my_programs/JuliaOptOS/src/EnzymeOptOS/evaluate_functions/dynamic_linear_elastic_systems/dynamic_structural_compliance_pnorm.jl:0 [inlined]
 [11] diffejulia_f_15729_inner_12wrap
    @ ~/my_programs/JuliaOptOS/src/EnzymeOptOS/evaluate_functions/dynamic_linear_elastic_systems/dynamic_structural_compliance_pnorm.jl:0
 [12] macro expansion
    @ ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:5594 [inlined]
 [13] enzyme_call(::Val{…}, ::Ptr{…}, ::Type{…}, ::Type{…}, ::Val{…}, ::Type{…}, ::Type{…}, ::EnzymeCore.Const{…}, ::Type{…}, ::EnzymeCore.Duplicated{…}, ::Float64)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:5272
 [14] (::Enzyme.Compiler.CombinedAdjointThunk{…})(::EnzymeCore.Const{…}, ::EnzymeCore.Duplicated{…}, ::Vararg{…})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:5154
 [15] autodiff
    @ ~/.julia/packages/Enzyme/MIIMf/src/Enzyme.jl:275 [inlined]
 [16] autodiff
    @ ~/.julia/packages/Enzyme/MIIMf/src/Enzyme.jl:287 [inlined]
 [17] gradient
    @ ~/.julia/packages/Enzyme/MIIMf/src/Enzyme.jl:938 [inlined]
 [18] (::Main.SolidMechanicsTO.var"#df#114"{Main.SolidMechanicsTO.var"#f#113"{…}})(x::Vector{Float64})
    @ Main.SolidMechanicsTO ~/my_programs/JuliaOptOS/src/EnzymeOptOS/evaluate_functions/dynamic_linear_elastic_systems/dynamic_structural_compliance_pnorm.jl:24
 [19] (::Main.SolidMechanicsTO.var"#df_obj_min#58"{…})(x::Vector{…})
    @ Main.SolidMechanicsTO ~/my_programs/JuliaOptOS/src/EnzymeOptOS/optimization/optimizers/MMA_GCMMA/abstract_basic_MMA.jl:16
 [20] (::Main.SolidMechanicsTO.var"#54#60"{Main.SolidMechanicsTO.var"#df_obj_min#58"{…}})(x::Vector{Float64})
    @ Main.SolidMechanicsTO ~/my_programs/JuliaOptOS/src/EnzymeOptOS/optimization/optimizers/MMA_GCMMA/abstract_basic_MMA.jl:19
 [21] compute(opt::Main.SolidMechanicsTO.GCMMA, s::Vector{Float64})
    @ Main.SolidMechanicsTO ~/my_programs/JuliaOptOS/src/EnzymeOptOS/optimization/optimizers/MMA_GCMMA/abstract_basic_MMA.jl:57
 [22] my_optimize(opt::Main.SolidMechanicsTO.GCMMA, xval::Vector{…}, nodes::Vector{…}, elements::Vector{…}, filter::Main.SolidMechanicsTO.DensityFilter)
    @ Main.SolidMechanicsTO ~/my_programs/JuliaOptOS/src/EnzymeOptOS/optimization/optimizers/MMA_GCMMA/GCMMA_wrapper.jl:96
 [23] main()
    @ Main ~/my_programs/JuliaOptOS/examples/EnzymeOptOS/dynamic_structural_compliance_2norm_my_optimizer.jl:150
 [24] top-level scope
    @ ~/my_programs/JuliaOptOS/examples/EnzymeOptOS/dynamic_structural_compliance_2norm_my_optimizer.jl:257
Some type information was truncated. Use `show(err)` to see complete types.

```

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [April 28, 2024, 12:19am UTC](https://discourse.julialang.org/t/if-enzymerules-inactive-is-defined-does-the-enzyme-jl-operation-still-take-place/113369/6 "2024-04-28T00:19:09Z")

</div>

> [@ToPo](#):
>
> make\_boundary\_Ks\_and\_Fs

Can you share your entire erring application, perhaps as an issue on Enzyme.jl?

From the looks of it `make_boundary_Ks_and_Fs` was not recognized as inactive, so my guess is that your custom activity rule is messed up

---

<div class="post-metadata">

**Author:** ![ToPo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/topo/32/205868_2.png) [@ToPo](https://discourse.julialang.org/u/ToPo)\
**Post date:** [May 7, 2024, 2:17pm UTC](https://discourse.julialang.org/t/if-enzymerules-inactive-is-defined-does-the-enzyme-jl-operation-still-take-place/113369/7 "2024-05-07T14:17:18Z")

</div>

Sorry, the arguments were incorrectly set in my program and inactive() was recognised as a different function. However, the modifications have not eliminated the current errors.

I will review it a bit more on my end and add it to the Enzyme.jl issue if there is a problem.
