# Questions regarding a few ForwardDiff.jl limitations

**URL:** <https://discourse.julialang.org/t/questions-regarding-a-few-forwarddiff-jl-limitations/4218>\
**Category:** General Usage\
**Tags:** package\
**Created:** [June 12, 2017, 7:56am UTC](https://discourse.julialang.org/t/questions-regarding-a-few-forwarddiff-jl-limitations/4218 "2017-06-12T07:56:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcovalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcovalt/32/1722_2.png) [@mcovalt](https://discourse.julialang.org/u/mcovalt)\
**Post date:** [June 12, 2017, 7:56am UTC](https://discourse.julialang.org/t/questions-regarding-a-few-forwarddiff-jl-limitations/4218/1 "2017-06-12T07:56:24Z")

</div>

I have a project I’m wanting to use ForwardDiff.jl on. I have a few question:

**Chunk Size**

- If I’m reading correctly, ForwardDiff.jl evaluates partial derivatives on chunks of the input vector. What if computations depend on the whole input vector? A trivial example:

```julia
function f(x)
  output = 0.0
  if sum(x) > 5.0
    for i = 1:length(x)
      output += sin(x[i])
    end
  else
    for i = 1:length(x)
      output += cos(x[i])
    end
  end
end

```

- According to the docs, “each differentiation of a chunk requires […] additional memory proportional to the _square of the chunk’s size_.” Does this mean if it’s chunked into 10 64-bit elements, the memory allocated is `(10*10)^2*64` bits or `(10*64)^2` bits?

**External C-Calls** : I understand why these aren’t permitted for calculations, but are they permitted for non-calculation purposes (e.g. producing index ordering for a permuted vector)?

**Types** : What exactly has to be typed to accept `Real`? Just everything the input vector “touches” or absolutely everything within the function?

**Skipping Operations** : I have an iterative solver within my objective function. I see no reason why I’d need to track all differentiations within this. Is there a way to skip this section of the objective function?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 12, 2017, 8:20am UTC](https://discourse.julialang.org/t/questions-regarding-a-few-forwarddiff-jl-limitations/4218/2 "2017-06-12T08:20:47Z")

</div>

> [@mcovalt](#):
>
> External C-Calls: I understand why these aren’t permitted for calculations, but are they permitted for non-calculation purposes (e.g. producing index ordering for a permuted vector)?

If no dual number does a ccall you’re fine. It sounds like your examples will be fine.

> [@mcovalt](#):
>
> Types: What exactly has to be typed to accept Real? Just everything the input vector “touches” or absolutely everything within the function?

Everything that gets touched by a Dual number. It needs to be able to, well, accept a Dual number.

> [@mcovalt](#):
>
> Skipping Operations: I have an iterative solver within my objective function. I see no reason why I’d need to track all differentiations within this. Is there a way to skip this section of the objective function?

Just do a type-check for Dual numbers, but you should be sure that it won’t effect the derivative.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 12, 2017, 9:42am UTC](https://discourse.julialang.org/t/questions-regarding-a-few-forwarddiff-jl-limitations/4218/3 "2017-06-12T09:42:14Z")

</div>

Regarding chunking, the entire input vector is always passed into the function, regardless of the chunk size. The chunk size is just the number of partial derivatives which are evaluated during each call to the function. So the size of the input will be the number of elements in x times the chunk size.
