Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some cases of gradient/jacobian with StaticArrays #1875

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ExpandingMan
Copy link
Contributor

This fixes at least some cases of functions taking and return static arrays being returned as Array (and the associated allocations).

An example of a case this fixes is

f1(x) = @SVector Float64[x[1], 2*x[2]]

x = @SVector ones(2)

gradient(Forward, f1, x)

There were two things wrong with the tupstack function in current main. The first is that cat always returns an Array. This may be a missing method in StaticArrays, but I suspect that the reason it's missing is that it's hard to statically infer the resulting shape (and it would rely on static inference of the value of dims). Suffice it to say, I think it's safe to assume that cat can never be used with staticarrays.

The second issue is that when reshape is used with static arrays, one must use Size arguments to return a static array, to indicate that the size can be statically inferred.

This works in the cases I have tested but looks fragile. I still need to confirm more thoroughly that this does not break any of the higher rank cases.

@wsmoses
Copy link
Member

wsmoses commented Sep 21, 2024

Add a test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants