Feature request
We should add scan and remat (gradient checkpointing) to the most important Flax/JAX models (BERT, GPT2, OPT, T5, BART, Wav2Vec2).
Motivation
Scan allows for much faster compilation and memory savings and remat is the equivalent of gradient_checkpointing in PyTorch.
@sanchit-gandhi already uses both features in the Flax Seq2Seq Speech project - see: https://github.com/sanchit-gandhi/seq2seq-speech so it'd be quite trivial to get them working.
Implementation details:
Given that both scan and remat are not related to the model architecture, they should IMO not be in the model's config (We've done this mistake in PyTorch and don't want to repeat it here).
I would advocate for the following API:
model = FlaxBertForMaskedLM.from_pretrained("bert-base-cased")
model.scan() # or model.scan_enable()
model.unscan() # or model.scan_disable()
and
model = FlaxBertForMaskedLM.from_pretrained("bert-base-cased")
model.gradient_checkpoint_enable()
model.gradient_checkpoint_disable()
As can be seen here: https://github.com/sanchit-gandhi/seq2seq-speech/blob/b28d0c25c8fad0f9ffa6707f91f7aba320d44a4b/models/modeling_flax_wav2vec2.py#L504
We'll need to re-initialize the flax.linen.module inside the model. However this should be fine since it just means that we do
self.module = self.module_class(config=config, dtype=dtype, use_scan=True, **kwargs)
self. _is_scan_enabled = True
similar to this line:
|
module = self.module_class(config=config, dtype=dtype, **kwargs) |
We can see along the PR how much logic can reside in modeling_flax_utils.py and how much would go into the specific models, e.g. modeling_flax_wav2vec2.py.
The same API / logic could be used for the gradient_checkpointing.
Your contribution
Happy to give this implementation a shot with @sanchit-gandhi and @patil-suraj .
Also would love to hear feedback from @borisdayma @marcvanzee about the API
Feature request
We should add scan and remat (gradient checkpointing) to the most important Flax/JAX models (BERT, GPT2, OPT, T5, BART, Wav2Vec2).
Motivation
Scan allows for much faster compilation and memory savings and
rematis the equivalent ofgradient_checkpointingin PyTorch.@sanchit-gandhi already uses both features in the Flax Seq2Seq Speech project - see: https://github.com/sanchit-gandhi/seq2seq-speech so it'd be quite trivial to get them working.
Implementation details:
Given that both
scanandrematare not related to the model architecture, they should IMO not be in the model's config (We've done this mistake in PyTorch and don't want to repeat it here).I would advocate for the following API:
and
As can be seen here: https://github.com/sanchit-gandhi/seq2seq-speech/blob/b28d0c25c8fad0f9ffa6707f91f7aba320d44a4b/models/modeling_flax_wav2vec2.py#L504
We'll need to re-initialize the
flax.linen.moduleinside the model. However this should be fine since it just means that we dosimilar to this line:
transformers/src/transformers/models/wav2vec2/modeling_flax_wav2vec2.py
Line 868 in 71e6027
We can see along the PR how much logic can reside in
modeling_flax_utils.pyand how much would go into the specific models, e.g.modeling_flax_wav2vec2.py.The same API / logic could be used for the
gradient_checkpointing.Your contribution
Happy to give this implementation a shot with @sanchit-gandhi and @patil-suraj .
Also would love to hear feedback from @borisdayma @marcvanzee about the API