Lightning-AI / Lightning-AI/pytorch-lightning

Support for torch.func

Open
#17,820 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

Description & Motivation

Torch.func offers lots of flexibility. It's however not supported by lightning.
A few bugs I've noticed so far when trying to use it:

  • When we init our model and then use a trainer, if we have used model.to("meta") to only keep the function version, it then fails as the trainers try to send it to device
  • Params are not sent to the device, which then create device issues

Minimal example:

class Module(L.LightningModule):
    def __init__(self, cfg):
        super().__init__()
        models = [torch.nn.Linear(in_features, out_features) for i in range(num_models)]
        self.fmodel = copy.deepcopy(models[0])
        self.model_params, self.model_buffers = torch.func.stack_module_state(models)

   def training_step(self, batch, batch_idx):
       # It is possible to vmap directly over torch.func.functional_call,
      # but wrapping it in a function makes it clearer what is going on.
      def call_single_model(params, buffers, data):
          return torch.func.functional_call(self.fmodel, (params, buffers), (data,))
      output = torch.vmap(call_single_model, (0, 0, None))(self.model_params,  self.model_buffers, batch)
      return output.mean()
Pitch

No response

Alternatives

No response

Additional context

No response

cc @borda

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal example using torch.func.stack_module_state, functional_call, vmap, and model.to("meta"), then trace how Trainer moves models and parameters to a device. Done means the example runs through training without device errors while preserving the torch.func setup for model parameters and buffers.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.