Skip to content

Add a generic multimodal runner #13166

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

Merged
merged 1 commit into from
Aug 19, 2025
Merged

Add a generic multimodal runner #13166

merged 1 commit into from
Aug 19, 2025

Conversation

larryliu0820
Copy link
Contributor

Summary:
This diff adds a generic multimodal runner for Executorch. It includes changes to the image_prefiller.h file, which adds a prefill method that takes an Image object and returns the next token of the LLM module after prefill. It also includes changes to the multimodal_runner.cpp file, which implements the MultimodalRunner class for multimodal input and text output LLMs. The MultimodalRunner class uses the ImagePrefiller, TextPrefiller classes to prefill the KV cache of the model, then uses TextTokenGenerator to run the autoregressive generation loop.

See diagram:

      ┌─────────────────┐
      │     IRunner     │
      │   <<interface>> │
      │                 │
      │ + is_loaded()   │
      │ + load()        │
      │ + generate()    │
      │ + stop()        │
      └─────────────────┘
             △
             │
             │ implements
             │
             │
             │
             │
      ┌──────┴──────────┐          ┌─────────────────┐
      │ TextLLMRunner   │          │MultimodalRunner │
      │                 │          │                 │
      │ - tokenizer_    │          │ - tokenizer_    ┼───────┐
┌─────┼ - module_       │          │ - module_       ┼─────┐ │
│ ┌───┼ - stats_        │          │ - stats_        ┼───┐ │ │
│ │ ┌─┼ - metadata_     │          │ - metadata_     ┼─┐ │ │ │
│ │ │ │ - temperature_  │          │ - pos_          │ │ │ │ │
│ │ │ └─────────────────┘          └─────────────────┘ │ │ │ │
│ │ │                                                  │ │ │ │
│ │ │                                                  │ │ │ │
│ │ │                                                  │ │ │ │
│ │ │               ┌─────────────────┐                │ │ │ │
│ │ │               │TextTokenGenerat-│                │ │ │ │
│ │ │               │or               │                │ │ │ │
│ │ │               │                 │                │ │ │ │
│ │ │               │ - tokenizer_*   │                │ │ │ │
│ │ │  consists     │ - text_decoder_ │    consists    │ │ │ │
│ │ └──────────────►│   runner_       │◄───────────────┘ │ │ │
│ │                 │ - eos_ids_      │                  │ │ │
│ │                 │ - use_kv_cache_ │                  │ │ │
│ │                 │ - stats_*       │                  │ │ │
│ │                 │                 │                  │ │ │
│ │consists         │ + generate()    │         consists │ │ │
│ │                 └────────┬────────┘                  │ │ │
│ │           ┌──────────────┴───────────────┐           │ │ │
│ │           ▼            uses              ▼           │ │ │
│ │   ┌─────────────────┐          ┌─────────────────┐   │ │ │
│ │   │TextDecoderRunner│          │MultimodalTextDe-│   │ │ │
│ │   │                 │          │coderRunner      │   │ │ │
│ │   │ - module_*      │ extends  │ - module_*      │   │ │ │
│ └──►│ - should_stop_  │◄─────────┼ - should_stop_  │◄──┘ │ │
│     │                 │          │                 │     │ │
│     │ + step()        │          │ + step()        │     │ │
│     │ + logits_to_    │          │ + logits_to_    │     │ │
│     │   token()       │          │   token()       │     │ │
│     └─────────────────┘          └─────────────────┘     │ │
│             ▲                             ▲              │ │
│             │           uses              │              │ │
│             └──────────────┬──────────────┘              │ │
│                    ┌───────┴─────────┐                   │ │
│                    │  TextPrefiller  │                   │ │
│                    │                 │                   │ │
│                    │ - text_decoder_ │                   │ │
│   consists         │   runner_       │      consists     │ │
└───────────────────►│ - use_kv_cache_ │◄──────────────────┘ │
                     │ - enable_       │                     │
                     │   parallel_     │                     │
                     │   prefill_      │                     │
                     │                 │                     │
                     │ + prefill()     │                     │
                     └─────────────────┘           consists  │
                                                             │
                                                             │
                                   ┌─────────────────┐       │
                                   │ ImagePrefiller  │       │
                                   │                 │       │
                                   │ - module_*      │       │
                                   │                 │◄──────┘
                                   │ + prefill()     │
                                   │ + logits_to_    │
                                   │   token()       │
                                   └─────────────────┘

Differential Revision: D79231625

Copy link

pytorch-bot bot commented Aug 6, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/13166

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 2 Pending

As of commit 5c0ad9b with merge base d757709 (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 6, 2025
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79231625

Copy link

github-actions bot commented Aug 6, 2025

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Copy link
Contributor

@jackzhxng jackzhxng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pr not synced with internal diff so ignore these PR comments, made some comments to address internally. Otherwise looks good

facebook-github-bot pushed a commit that referenced this pull request Aug 18, 2025
Summary:

This diff adds a generic multimodal runner for Executorch. It includes changes to the `multimodal_prefiller.h` file, which adds a `prefill` method that takes an `MultimodalInput` object and returns the next token of the LLM module after prefill. It also includes changes to the `multimodal_runner.cpp` file, which implements the `MultimodalRunner` class for multimodal input and text output LLMs. The `MultimodalRunner` class uses the `MultimodalRunner` class to prefill the KV cache of the model, then uses `TextTokenGenerator` to run the autoregressive generation loop.

See diagram:

```
                                                             
       ┌─────────────────┐                                   
       │     IRunner     │                                   
       │   <<interface>> │                                   
       │                 │                                   
       │ + is_loaded()   │                                   
       │ + load()        │                                   
       │ + generate()    │                                   
       │ + stop()        │                                   
       └─────────────────┘                                   
              △                                              
              │                                              
              │ implements                                   
              │                                              
              │                                              
              │                                              
              │                                              
       ┌──────┴──────────┐          ┌─────────────────┐      
       │ TextLLMRunner   │          │MultimodalRunner │      
       │                 │          │                 │      
       │ - tokenizer_    │          │ - tokenizer_    │      
 ┌─────┼ - module_       │          │ - module_       ┼─────┐
 │ ┌───┼ - stats_        │          │ - stats_        ┼───┐ │
 │ │ ┌─┼ - metadata_     │          │ - metadata_     ┼─┐ │ │
 │ │ │ │ - temperature_  │          │ - pos_          │ │ │ │
 │ │ │ └─────────────────┘          └─────────────────┘ │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │               ┌─────────────────┐                │ │ │
 │ │ │               │TextTokenGenerat-│                │ │ │
 │ │ │               │or               │                │ │ │
 │ │ │               │                 │                │ │ │
 │ │ │               │ - tokenizer_*   │                │ │ │
 │ │ │  consists     │ - text_decoder_ │    consists    │ │ │
 │ │ └──────────────►│   runner_       │◄───────────────┘ │ │
 │ │                 │ - eos_ids_      │                  │ │
 │ │                 │ - use_kv_cache_ │                  │ │
 │ │                 │ - stats_*       │                  │ │
 │ │                 │                 │                  │ │
 │ │consists         │ + generate()    │         consists │ │
 │ │                 └────────┬────────┘                  │ │
 │ │           ┌──────────────┴───────────────┐           │ │
 │ │           ▼            uses              ▼           │ │
 │ │   ┌─────────────────┐          ┌─────────────────┐   │ │
 │ │   │TextDecoderRunner│          │MultimodalDecode-│   │ │
 │ │   │                 │          │rRunner          │   │ │
 │ │   │ - module_*      │ extends  │ - module_*      │   │ │
 │ └──►│ - should_stop_  │◄─────────┼ - should_stop_  │◄──┘ │
 │     │                 │          │                 │     │
 │     │ + step()        │          │ + step()        │     │
 │     │ + logits_to_    │          │ + logits_to_    │     │
 │     │   token()       │          │   token()       │     │
 │     └─────────────────┘          └─────────────────┘     │
 │             ▲                             ▲              │
 │             │           uses              │              │
 │consists     ├─────────────────────────────┤              │
 │     ┌───────┴─────────┐                   │              │
 │     │  TextPrefiller  │                   │      consists│
 │     │                 │          ┌────────┴────────┐     │
 │     │ - text_decoder_ │          │ MultimodalPrefi-│     │
 │     │   runner_       │          │ller             │     │
 └────►│ - use_kv_cache_ │          │ - module_*      │     │
       │ - enable_       │          │                 │◄────┘
       │   parallel_     │          │ + prefill()     │      
       │   prefill_      │          │ + logits_to_    │      
       │                 │          │   token()       │      
       │ + prefill()     │          └─────────────────┘      
       ├─────────────────┘                                                  
```

Reviewed By: jackzhxng

Differential Revision: D79231625
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79231625

larryliu0820 pushed a commit that referenced this pull request Aug 19, 2025
Summary:

This diff adds a generic multimodal runner for Executorch. It includes changes to the `multimodal_prefiller.h` file, which adds a `prefill` method that takes an `MultimodalInput` object and returns the next token of the LLM module after prefill. It also includes changes to the `multimodal_runner.cpp` file, which implements the `MultimodalRunner` class for multimodal input and text output LLMs. The `MultimodalRunner` class uses the `MultimodalRunner` class to prefill the KV cache of the model, then uses `TextTokenGenerator` to run the autoregressive generation loop.

See diagram:

```
                                                             
       ┌─────────────────┐                                   
       │     IRunner     │                                   
       │   <<interface>> │                                   
       │                 │                                   
       │ + is_loaded()   │                                   
       │ + load()        │                                   
       │ + generate()    │                                   
       │ + stop()        │                                   
       └─────────────────┘                                   
              △                                              
              │                                              
              │ implements                                   
              │                                              
              │                                              
              │                                              
              │                                              
       ┌──────┴──────────┐          ┌─────────────────┐      
       │ TextLLMRunner   │          │MultimodalRunner │      
       │                 │          │                 │      
       │ - tokenizer_    │          │ - tokenizer_    │      
 ┌─────┼ - module_       │          │ - module_       ┼─────┐
 │ ┌───┼ - stats_        │          │ - stats_        ┼───┐ │
 │ │ ┌─┼ - metadata_     │          │ - metadata_     ┼─┐ │ │
 │ │ │ │ - temperature_  │          │ - pos_          │ │ │ │
 │ │ │ └─────────────────┘          └─────────────────┘ │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │               ┌─────────────────┐                │ │ │
 │ │ │               │TextTokenGenerat-│                │ │ │
 │ │ │               │or               │                │ │ │
 │ │ │               │                 │                │ │ │
 │ │ │               │ - tokenizer_*   │                │ │ │
 │ │ │  consists     │ - text_decoder_ │    consists    │ │ │
 │ │ └──────────────►│   runner_       │◄───────────────┘ │ │
 │ │                 │ - eos_ids_      │                  │ │
 │ │                 │ - use_kv_cache_ │                  │ │
 │ │                 │ - stats_*       │                  │ │
 │ │                 │                 │                  │ │
 │ │consists         │ + generate()    │         consists │ │
 │ │                 └────────┬────────┘                  │ │
 │ │           ┌──────────────┴───────────────┐           │ │
 │ │           ▼            uses              ▼           │ │
 │ │   ┌─────────────────┐          ┌─────────────────┐   │ │
 │ │   │TextDecoderRunner│          │MultimodalDecode-│   │ │
 │ │   │                 │          │rRunner          │   │ │
 │ │   │ - module_*      │ extends  │ - module_*      │   │ │
 │ └──►│ - should_stop_  │◄─────────┼ - should_stop_  │◄──┘ │
 │     │                 │          │                 │     │
 │     │ + step()        │          │ + step()        │     │
 │     │ + logits_to_    │          │ + logits_to_    │     │
 │     │   token()       │          │   token()       │     │
 │     └─────────────────┘          └─────────────────┘     │
 │             ▲                             ▲              │
 │             │           uses              │              │
 │consists     ├─────────────────────────────┤              │
 │     ┌───────┴─────────┐                   │              │
 │     │  TextPrefiller  │                   │      consists│
 │     │                 │          ┌────────┴────────┐     │
 │     │ - text_decoder_ │          │ MultimodalPrefi-│     │
 │     │   runner_       │          │ller             │     │
 └────►│ - use_kv_cache_ │          │ - module_*      │     │
       │ - enable_       │          │                 │◄────┘
       │   parallel_     │          │ + prefill()     │      
       │   prefill_      │          │ + logits_to_    │      
       │                 │          │   token()       │      
       │ + prefill()     │          └─────────────────┘      
       ├─────────────────┘                                                  
```

Differential Revision: D79231625
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79231625

larryliu0820 pushed a commit that referenced this pull request Aug 19, 2025
Summary:
Pull Request resolved: #13166

This diff adds a generic multimodal runner for Executorch. It includes changes to the `multimodal_prefiller.h` file, which adds a `prefill` method that takes an `MultimodalInput` object and returns the next token of the LLM module after prefill. It also includes changes to the `multimodal_runner.cpp` file, which implements the `MultimodalRunner` class for multimodal input and text output LLMs. The `MultimodalRunner` class uses the `MultimodalRunner` class to prefill the KV cache of the model, then uses `TextTokenGenerator` to run the autoregressive generation loop.

See diagram:

```

       ┌─────────────────┐
       │     IRunner     │
       │   <<interface>> │
       │                 │
       │ + is_loaded()   │
       │ + load()        │
       │ + generate()    │
       │ + stop()        │
       └─────────────────┘
              △
              │
              │ implements
              │
              │
              │
              │
       ┌──────┴──────────┐          ┌─────────────────┐
       │ TextLLMRunner   │          │MultimodalRunner │
       │                 │          │                 │
       │ - tokenizer_    │          │ - tokenizer_    │
 ┌─────┼ - module_       │          │ - module_       ┼─────┐
 │ ┌───┼ - stats_        │          │ - stats_        ┼───┐ │
 │ │ ┌─┼ - metadata_     │          │ - metadata_     ┼─┐ │ │
 │ │ │ │ - temperature_  │          │ - pos_          │ │ │ │
 │ │ │ └─────────────────┘          └─────────────────┘ │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │               ┌─────────────────┐                │ │ │
 │ │ │               │TextTokenGenerat-│                │ │ │
 │ │ │               │or               │                │ │ │
 │ │ │               │                 │                │ │ │
 │ │ │               │ - tokenizer_*   │                │ │ │
 │ │ │  consists     │ - text_decoder_ │    consists    │ │ │
 │ │ └──────────────►│   runner_       │◄───────────────┘ │ │
 │ │                 │ - eos_ids_      │                  │ │
 │ │                 │ - use_kv_cache_ │                  │ │
 │ │                 │ - stats_*       │                  │ │
 │ │                 │                 │                  │ │
 │ │consists         │ + generate()    │         consists │ │
 │ │                 └────────┬────────┘                  │ │
 │ │           ┌──────────────┴───────────────┐           │ │
 │ │           ▼            uses              ▼           │ │
 │ │   ┌─────────────────┐          ┌─────────────────┐   │ │
 │ │   │TextDecoderRunner│          │MultimodalDecode-│   │ │
 │ │   │                 │          │rRunner          │   │ │
 │ │   │ - module_*      │ extends  │ - module_*      │   │ │
 │ └──►│ - should_stop_  │◄─────────┼ - should_stop_  │◄──┘ │
 │     │                 │          │                 │     │
 │     │ + step()        │          │ + step()        │     │
 │     │ + logits_to_    │          │ + logits_to_    │     │
 │     │   token()       │          │   token()       │     │
 │     └─────────────────┘          └─────────────────┘     │
 │             ▲                             ▲              │
 │             │           uses              │              │
 │consists     ├─────────────────────────────┤              │
 │     ┌───────┴─────────┐                   │              │
 │     │  TextPrefiller  │                   │      consists│
 │     │                 │          ┌────────┴────────┐     │
 │     │ - text_decoder_ │          │ MultimodalPrefi-│     │
 │     │   runner_       │          │ller             │     │
 └────►│ - use_kv_cache_ │          │ - module_*      │     │
       │ - enable_       │          │                 │◄────┘
       │   parallel_     │          │ + prefill()     │
       │   prefill_      │          │ + logits_to_    │
       │                 │          │   token()       │
       │ + prefill()     │          └─────────────────┘
       ├─────────────────┘
```

Differential Revision: D79231625
facebook-github-bot pushed a commit that referenced this pull request Aug 19, 2025
Summary:

This diff adds a generic multimodal runner for Executorch. It includes changes to the `multimodal_prefiller.h` file, which adds a `prefill` method that takes an `MultimodalInput` object and returns the next token of the LLM module after prefill. It also includes changes to the `multimodal_runner.cpp` file, which implements the `MultimodalRunner` class for multimodal input and text output LLMs. The `MultimodalRunner` class uses the `MultimodalRunner` class to prefill the KV cache of the model, then uses `TextTokenGenerator` to run the autoregressive generation loop.

See diagram:

```

       ┌─────────────────┐
       │     IRunner     │
       │   <<interface>> │
       │                 │
       │ + is_loaded()   │
       │ + load()        │
       │ + generate()    │
       │ + stop()        │
       └─────────────────┘
              △
              │
              │ implements
              │
              │
              │
              │
       ┌──────┴──────────┐          ┌─────────────────┐
       │ TextLLMRunner   │          │MultimodalRunner │
       │                 │          │                 │
       │ - tokenizer_    │          │ - tokenizer_    │
 ┌─────┼ - module_       │          │ - module_       ┼─────┐
 │ ┌───┼ - stats_        │          │ - stats_        ┼───┐ │
 │ │ ┌─┼ - metadata_     │          │ - metadata_     ┼─┐ │ │
 │ │ │ │ - temperature_  │          │ - pos_          │ │ │ │
 │ │ │ └─────────────────┘          └─────────────────┘ │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │               ┌─────────────────┐                │ │ │
 │ │ │               │TextTokenGenerat-│                │ │ │
 │ │ │               │or               │                │ │ │
 │ │ │               │                 │                │ │ │
 │ │ │               │ - tokenizer_*   │                │ │ │
 │ │ │  consists     │ - text_decoder_ │    consists    │ │ │
 │ │ └──────────────►│   runner_       │◄───────────────┘ │ │
 │ │                 │ - eos_ids_      │                  │ │
 │ │                 │ - use_kv_cache_ │                  │ │
 │ │                 │ - stats_*       │                  │ │
 │ │                 │                 │                  │ │
 │ │consists         │ + generate()    │         consists │ │
 │ │                 └────────┬────────┘                  │ │
 │ │           ┌──────────────┴───────────────┐           │ │
 │ │           ▼            uses              ▼           │ │
 │ │   ┌─────────────────┐          ┌─────────────────┐   │ │
 │ │   │TextDecoderRunner│          │MultimodalDecode-│   │ │
 │ │   │                 │          │rRunner          │   │ │
 │ │   │ - module_*      │ extends  │ - module_*      │   │ │
 │ └──►│ - should_stop_  │◄─────────┼ - should_stop_  │◄──┘ │
 │     │                 │          │                 │     │
 │     │ + step()        │          │ + step()        │     │
 │     │ + logits_to_    │          │ + logits_to_    │     │
 │     │   token()       │          │   token()       │     │
 │     └─────────────────┘          └─────────────────┘     │
 │             ▲                             ▲              │
 │             │           uses              │              │
 │consists     ├─────────────────────────────┤              │
 │     ┌───────┴─────────┐                   │              │
 │     │  TextPrefiller  │                   │      consists│
 │     │                 │          ┌────────┴────────┐     │
 │     │ - text_decoder_ │          │ MultimodalPrefi-│     │
 │     │   runner_       │          │ller             │     │
 └────►│ - use_kv_cache_ │          │ - module_*      │     │
       │ - enable_       │          │                 │◄────┘
       │   parallel_     │          │ + prefill()     │
       │   prefill_      │          │ + logits_to_    │
       │                 │          │   token()       │
       │ + prefill()     │          └─────────────────┘
       ├─────────────────┘
```

Differential Revision: D79231625
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79231625

1 similar comment
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79231625

jackzhxng added a commit that referenced this pull request Aug 19, 2025
Summary:
Pull Request resolved: #13166

This diff adds a generic multimodal runner for Executorch. It includes changes to the `multimodal_prefiller.h` file, which adds a `prefill` method that takes an `MultimodalInput` object and returns the next token of the LLM module after prefill. It also includes changes to the `multimodal_runner.cpp` file, which implements the `MultimodalRunner` class for multimodal input and text output LLMs. The `MultimodalRunner` class uses the `MultimodalRunner` class to prefill the KV cache of the model, then uses `TextTokenGenerator` to run the autoregressive generation loop.

See diagram:

```

       ┌─────────────────┐
       │     IRunner     │
       │   <<interface>> │
       │                 │
       │ + is_loaded()   │
       │ + load()        │
       │ + generate()    │
       │ + stop()        │
       └─────────────────┘
              △
              │
              │ implements
              │
              │
              │
              │
       ┌──────┴──────────┐          ┌─────────────────┐
       │ TextLLMRunner   │          │MultimodalRunner │
       │                 │          │                 │
       │ - tokenizer_    │          │ - tokenizer_    │
 ┌─────┼ - module_       │          │ - module_       ┼─────┐
 │ ┌───┼ - stats_        │          │ - stats_        ┼───┐ │
 │ │ ┌─┼ - metadata_     │          │ - metadata_     ┼─┐ │ │
 │ │ │ │ - temperature_  │          │ - pos_          │ │ │ │
 │ │ │ └─────────────────┘          └─────────────────┘ │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │               ┌─────────────────┐                │ │ │
 │ │ │               │TextTokenGenerat-│                │ │ │
 │ │ │               │or               │                │ │ │
 │ │ │               │                 │                │ │ │
 │ │ │               │ - tokenizer_*   │                │ │ │
 │ │ │  consists     │ - text_decoder_ │    consists    │ │ │
 │ │ └──────────────►│   runner_       │◄───────────────┘ │ │
 │ │                 │ - eos_ids_      │                  │ │
 │ │                 │ - use_kv_cache_ │                  │ │
 │ │                 │ - stats_*       │                  │ │
 │ │                 │                 │                  │ │
 │ │consists         │ + generate()    │         consists │ │
 │ │                 └────────┬────────┘                  │ │
 │ │           ┌──────────────┴───────────────┐           │ │
 │ │           ▼            uses              ▼           │ │
 │ │   ┌─────────────────┐          ┌─────────────────┐   │ │
 │ │   │TextDecoderRunner│          │MultimodalDecode-│   │ │
 │ │   │                 │          │rRunner          │   │ │
 │ │   │ - module_*      │ extends  │ - module_*      │   │ │
 │ └──►│ - should_stop_  │◄─────────┼ - should_stop_  │◄──┘ │
 │     │                 │          │                 │     │
 │     │ + step()        │          │ + step()        │     │
 │     │ + logits_to_    │          │ + logits_to_    │     │
 │     │   token()       │          │   token()       │     │
 │     └─────────────────┘          └─────────────────┘     │
 │             ▲                             ▲              │
 │             │           uses              │              │
 │consists     ├─────────────────────────────┤              │
 │     ┌───────┴─────────┐                   │              │
 │     │  TextPrefiller  │                   │      consists│
 │     │                 │          ┌────────┴────────┐     │
 │     │ - text_decoder_ │          │ MultimodalPrefi-│     │
 │     │   runner_       │          │ller             │     │
 └────►│ - use_kv_cache_ │          │ - module_*      │     │
       │ - enable_       │          │                 │◄────┘
       │   parallel_     │          │ + prefill()     │
       │   prefill_      │          │ + logits_to_    │
       │                 │          │   token()       │
       │ + prefill()     │          └─────────────────┘
       ├─────────────────┘
```

Differential Revision: D79231625
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79231625

larryliu0820 pushed a commit that referenced this pull request Aug 19, 2025
Summary:
Pull Request resolved: #13166

This diff adds a generic multimodal runner for Executorch. It includes changes to the `multimodal_prefiller.h` file, which adds a `prefill` method that takes an `MultimodalInput` object and returns the next token of the LLM module after prefill. It also includes changes to the `multimodal_runner.cpp` file, which implements the `MultimodalRunner` class for multimodal input and text output LLMs. The `MultimodalRunner` class uses the `MultimodalRunner` class to prefill the KV cache of the model, then uses `TextTokenGenerator` to run the autoregressive generation loop.

See diagram:

```

       ┌─────────────────┐
       │     IRunner     │
       │   <<interface>> │
       │                 │
       │ + is_loaded()   │
       │ + load()        │
       │ + generate()    │
       │ + stop()        │
       └─────────────────┘
              △
              │
              │ implements
              │
              │
              │
              │
       ┌──────┴──────────┐          ┌─────────────────┐
       │ TextLLMRunner   │          │MultimodalRunner │
       │                 │          │                 │
       │ - tokenizer_    │          │ - tokenizer_    │
 ┌─────┼ - module_       │          │ - module_       ┼─────┐
 │ ┌───┼ - stats_        │          │ - stats_        ┼───┐ │
 │ │ ┌─┼ - metadata_     │          │ - metadata_     ┼─┐ │ │
 │ │ │ │ - temperature_  │          │ - pos_          │ │ │ │
 │ │ │ └─────────────────┘          └─────────────────┘ │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │                                                  │ │ │
 │ │ │               ┌─────────────────┐                │ │ │
 │ │ │               │TextTokenGenerat-│                │ │ │
 │ │ │               │or               │                │ │ │
 │ │ │               │                 │                │ │ │
 │ │ │               │ - tokenizer_*   │                │ │ │
 │ │ │  consists     │ - text_decoder_ │    consists    │ │ │
 │ │ └──────────────►│   runner_       │◄───────────────┘ │ │
 │ │                 │ - eos_ids_      │                  │ │
 │ │                 │ - use_kv_cache_ │                  │ │
 │ │                 │ - stats_*       │                  │ │
 │ │                 │                 │                  │ │
 │ │consists         │ + generate()    │         consists │ │
 │ │                 └────────┬────────┘                  │ │
 │ │           ┌──────────────┴───────────────┐           │ │
 │ │           ▼            uses              ▼           │ │
 │ │   ┌─────────────────┐          ┌─────────────────┐   │ │
 │ │   │TextDecoderRunner│          │MultimodalDecode-│   │ │
 │ │   │                 │          │rRunner          │   │ │
 │ │   │ - module_*      │ extends  │ - module_*      │   │ │
 │ └──►│ - should_stop_  │◄─────────┼ - should_stop_  │◄──┘ │
 │     │                 │          │                 │     │
 │     │ + step()        │          │ + step()        │     │
 │     │ + logits_to_    │          │ + logits_to_    │     │
 │     │   token()       │          │   token()       │     │
 │     └─────────────────┘          └─────────────────┘     │
 │             ▲                             ▲              │
 │             │           uses              │              │
 │consists     ├─────────────────────────────┤              │
 │     ┌───────┴─────────┐                   │              │
 │     │  TextPrefiller  │                   │      consists│
 │     │                 │          ┌────────┴────────┐     │
 │     │ - text_decoder_ │          │ MultimodalPrefi-│     │
 │     │   runner_       │          │ller             │     │
 └────►│ - use_kv_cache_ │          │ - module_*      │     │
       │ - enable_       │          │                 │◄────┘
       │   parallel_     │          │ + prefill()     │
       │   prefill_      │          │ + logits_to_    │
       │                 │          │   token()       │
       │ + prefill()     │          └─────────────────┘
       ├─────────────────┘
```

Differential Revision: D79231625
Copy link
Contributor

@mergennachin mergennachin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

@mergennachin
Copy link
Contributor

We should make Llava runner use this new API as next step

https://github.com/pytorch/executorch/tree/main/examples/models/llava/runner

@facebook-github-bot
Copy link
Contributor

@larryliu0820 has imported this pull request. If you are a Meta employee, you can view this in D79231625.

larryliu0820 added a commit that referenced this pull request Aug 19, 2025
Summary:
This diff adds a generic multimodal runner for Executorch. It includes changes to the `image_prefiller.h` file, which adds a `prefill` method that takes an `Image` object and returns the next token of the LLM module after prefill. It also includes changes to the `multimodal_runner.cpp` file, which implements the `MultimodalRunner` class for multimodal input and text output LLMs. The `MultimodalRunner` class uses the `ImagePrefiller`, `TextPrefiller` classes to prefill the KV cache of the model, then uses `TextTokenGenerator` to run the autoregressive generation loop.

See diagram:

```
      ┌─────────────────┐
      │     IRunner     │
      │   <<interface>> │
      │                 │
      │ + is_loaded()   │
      │ + load()        │
      │ + generate()    │
      │ + stop()        │
      └─────────────────┘
             △
             │
             │ implements
             │
             │
             │
             │
      ┌──────┴──────────┐          ┌─────────────────┐
      │ TextLLMRunner   │          │MultimodalRunner │
      │                 │          │                 │
      │ - tokenizer_    │          │ - tokenizer_    ┼───────┐
┌─────┼ - module_       │          │ - module_       ┼─────┐ │
│ ┌───┼ - stats_        │          │ - stats_        ┼───┐ │ │
│ │ ┌─┼ - metadata_     │          │ - metadata_     ┼─┐ │ │ │
│ │ │ │ - temperature_  │          │ - pos_          │ │ │ │ │
│ │ │ └─────────────────┘          └─────────────────┘ │ │ │ │
│ │ │                                                  │ │ │ │
│ │ │                                                  │ │ │ │
│ │ │                                                  │ │ │ │
│ │ │               ┌─────────────────┐                │ │ │ │
│ │ │               │TextTokenGenerat-│                │ │ │ │
│ │ │               │or               │                │ │ │ │
│ │ │               │                 │                │ │ │ │
│ │ │               │ - tokenizer_*   │                │ │ │ │
│ │ │  consists     │ - text_decoder_ │    consists    │ │ │ │
│ │ └──────────────►│   runner_       │◄───────────────┘ │ │ │
│ │                 │ - eos_ids_      │                  │ │ │
│ │                 │ - use_kv_cache_ │                  │ │ │
│ │                 │ - stats_*       │                  │ │ │
│ │                 │                 │                  │ │ │
│ │consists         │ + generate()    │         consists │ │ │
│ │                 └────────┬────────┘                  │ │ │
│ │           ┌──────────────┴───────────────┐           │ │ │
│ │           ▼            uses              ▼           │ │ │
│ │   ┌─────────────────┐          ┌─────────────────┐   │ │ │
│ │   │TextDecoderRunner│          │MultimodalTextDe-│   │ │ │
│ │   │                 │          │coderRunner      │   │ │ │
│ │   │ - module_*      │ extends  │ - module_*      │   │ │ │
│ └──►│ - should_stop_  │◄─────────┼ - should_stop_  │◄──┘ │ │
│     │                 │          │                 │     │ │
│     │ + step()        │          │ + step()        │     │ │
│     │ + logits_to_    │          │ + logits_to_    │     │ │
│     │   token()       │          │   token()       │     │ │
│     └─────────────────┘          └─────────────────┘     │ │
│             ▲                             ▲              │ │
│             │           uses              │              │ │
│             └──────────────┬──────────────┘              │ │
│                    ┌───────┴─────────┐                   │ │
│                    │  TextPrefiller  │                   │ │
│                    │                 │                   │ │
│                    │ - text_decoder_ │                   │ │
│   consists         │   runner_       │      consists     │ │
└───────────────────►│ - use_kv_cache_ │◄──────────────────┘ │
                     │ - enable_       │                     │
                     │   parallel_     │                     │
                     │   prefill_      │                     │
                     │                 │                     │
                     │ + prefill()     │                     │
                     └─────────────────┘           consists  │
                                                             │
                                                             │
                                   ┌─────────────────┐       │
                                   │ ImagePrefiller  │       │
                                   │                 │       │
                                   │ - module_*      │       │
                                   │                 │◄──────┘
                                   │ + prefill()     │
                                   │ + logits_to_    │
                                   │   token()       │
                                   └─────────────────┘
```


Test Plan:
Imported from GitHub, without a `Test Plan:` line.

Rollback Plan:

Reviewed By: mergennachin

Differential Revision: D79231625

Pulled By: larryliu0820
Summary:
This diff adds a generic multimodal runner for Executorch. It includes changes to the `image_prefiller.h` file, which adds a `prefill` method that takes an `Image` object and returns the next token of the LLM module after prefill. It also includes changes to the `multimodal_runner.cpp` file, which implements the `MultimodalRunner` class for multimodal input and text output LLMs. The `MultimodalRunner` class uses the `ImagePrefiller`, `TextPrefiller` classes to prefill the KV cache of the model, then uses `TextTokenGenerator` to run the autoregressive generation loop.

See diagram:

```
      ┌─────────────────┐
      │     IRunner     │
      │   <<interface>> │
      │                 │
      │ + is_loaded()   │
      │ + load()        │
      │ + generate()    │
      │ + stop()        │
      └─────────────────┘
             △
             │
             │ implements
             │
             │
             │
             │
      ┌──────┴──────────┐          ┌─────────────────┐
      │ TextLLMRunner   │          │MultimodalRunner │
      │                 │          │                 │
      │ - tokenizer_    │          │ - tokenizer_    ┼───────┐
┌─────┼ - module_       │          │ - module_       ┼─────┐ │
│ ┌───┼ - stats_        │          │ - stats_        ┼───┐ │ │
│ │ ┌─┼ - metadata_     │          │ - metadata_     ┼─┐ │ │ │
│ │ │ │ - temperature_  │          │ - pos_          │ │ │ │ │
│ │ │ └─────────────────┘          └─────────────────┘ │ │ │ │
│ │ │                                                  │ │ │ │
│ │ │                                                  │ │ │ │
│ │ │                                                  │ │ │ │
│ │ │               ┌─────────────────┐                │ │ │ │
│ │ │               │TextTokenGenerat-│                │ │ │ │
│ │ │               │or               │                │ │ │ │
│ │ │               │                 │                │ │ │ │
│ │ │               │ - tokenizer_*   │                │ │ │ │
│ │ │  consists     │ - text_decoder_ │    consists    │ │ │ │
│ │ └──────────────►│   runner_       │◄───────────────┘ │ │ │
│ │                 │ - eos_ids_      │                  │ │ │
│ │                 │ - use_kv_cache_ │                  │ │ │
│ │                 │ - stats_*       │                  │ │ │
│ │                 │                 │                  │ │ │
│ │consists         │ + generate()    │         consists │ │ │
│ │                 └────────┬────────┘                  │ │ │
│ │           ┌──────────────┴───────────────┐           │ │ │
│ │           ▼            uses              ▼           │ │ │
│ │   ┌─────────────────┐          ┌─────────────────┐   │ │ │
│ │   │TextDecoderRunner│          │MultimodalTextDe-│   │ │ │
│ │   │                 │          │coderRunner      │   │ │ │
│ │   │ - module_*      │ extends  │ - module_*      │   │ │ │
│ └──►│ - should_stop_  │◄─────────┼ - should_stop_  │◄──┘ │ │
│     │                 │          │                 │     │ │
│     │ + step()        │          │ + step()        │     │ │
│     │ + logits_to_    │          │ + logits_to_    │     │ │
│     │   token()       │          │   token()       │     │ │
│     └─────────────────┘          └─────────────────┘     │ │
│             ▲                             ▲              │ │
│             │           uses              │              │ │
│             └──────────────┬──────────────┘              │ │
│                    ┌───────┴─────────┐                   │ │
│                    │  TextPrefiller  │                   │ │
│                    │                 │                   │ │
│                    │ - text_decoder_ │                   │ │
│   consists         │   runner_       │      consists     │ │
└───────────────────►│ - use_kv_cache_ │◄──────────────────┘ │
                     │ - enable_       │                     │
                     │   parallel_     │                     │
                     │   prefill_      │                     │
                     │                 │                     │
                     │ + prefill()     │                     │
                     └─────────────────┘           consists  │
                                                             │
                                                             │
                                   ┌─────────────────┐       │
                                   │ ImagePrefiller  │       │
                                   │                 │       │
                                   │ - module_*      │       │
                                   │                 │◄──────┘
                                   │ + prefill()     │
                                   │ + logits_to_    │
                                   │   token()       │
                                   └─────────────────┘
```

Pull Request resolved: #13166

Test Plan:
Imported from GitHub, without a `Test Plan:` line.

Rollback Plan:

Reviewed By: mergennachin

Differential Revision: D79231625

Pulled By: larryliu0820
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79231625

@larryliu0820 larryliu0820 merged commit 83749ae into main Aug 19, 2025
104 of 106 checks passed
@larryliu0820 larryliu0820 deleted the export-D79231625 branch August 19, 2025 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants