Lightning-AI / Lightning-AI/lightning-thunder

Support `torch.where(condition)` with thunder.jit

Open
#124 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted operators
Dominant language
Python
Stars
1.5k
Forks
121
PR merge metrics
No merged PRs in 30d

Description

## 🚀 Feature

### Motivation

Mixtral uses it: https://github.com/mistralai/mistral-src/blob/8598cf582091a596671be31990448e0620017851/moe_one_file_ref.py#L215

Minimal Repro
```python
import thunder

def fn(cond):
return torch.where(cond)

thunder.jit(fn)(torch.randn(3) > 0)
```

### Pitch

Support
![image](https://github.com/Lightning-AI/lightning-thunder/assets/11067892/cce623f9-8790-4467-bbf6-4f2d3f9d752c)
from https://pytorch.org/docs/stable/generated/torch.where.html

### Additional context

We already support `torch.where(condition, input, other)`: https://github.com/search?q=repo%3ALightning-AI%2Flightning-thunder+%22def+where%22&type=code

cc @apaz-cli

Contributor guide

No contributing guide indexed for this repository

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 by locating the existing `def where` implementation referenced in the issue's repository search, then compare its supported three-argument path with the one-argument `torch.where(condition)` API shown in the minimal repro. Add coverage for the repro and verify that the returned indices match PyTorch's documented behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.