[flax] unfreeze initial cache in gpt models #14535
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fix flax
generate
for GPT models when the initialseq_len
is 1.The issue is the init_cache method of flax GPT2 returns the cache as a
FrozenDict
, but the model’s forward returns cache as adict
.It works with seq_len > 1 because, when seq_len > 1, we call the body fun outside of the while loop -> body calls forward -> which returns cache as a
dict
.then we iterate over
body_fn
, usinglax.while_loop
, and it works as the type signature ofcache
is similar.It breaks for seq_len = 1 because, when it’s one we directly call the
body_fn
withlax.while_loop
, so here the initial type of cache isFrozenDict
but the forward inbody_fn
returnsdict
, which raises this errorbody_fun output and input must have same type structure, got PyTreeDe...
cc @Narsil