Skip to content

[RFC] Scan & Gradient checkpointing in Flax #17399

Description

@patrickvonplaten

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    WIPLabel your PR/Issue with WIP for some long outstanding Issues/PRs that are work in progress

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions