Lightning-AI / Lightning-AI/lightning-thunder
unexpected cache hit with symbolic values cache
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 121
- PR merge metrics
- No merged PRs in 30d
Description
## 🐛 Bug
shape check logic in broadcast currently isn't translated properly to prologue trace. In the example below, the logic checks should be plumbed to prologue to ensure we reject cache hit when it's not appropriate to do so.
I think the root cause is the limited support in constraints we have today. Right now [propagate_constraints](https://github.com/Lightning-AI/lightning-thunder/blob/75ba590708178bfe61b7ec2ed2d579d9edb7daa9/thunder/core/jit_ext.py#L1226) only insert contraints on NumberProxy fed through prologue trace to compute trace, and TensorProxy.shape isn't explicit that.
### To Reproduce
On a script like this
```
jfoo = thunder.jit(foo, cache="symbolic values")
a = torch.randn(2, 2, device="cuda")
b = torch.randn(1, device="cuda")
out = jfoo(a, b)
print(out.shape)
print("====================")
print("--- run1 ---")
print("\n\tprologue:\n", thunder.last_prologue_traces(jfoo)[0])
print("\n\tcompute:\n", thunder.last_traces(jfoo)[0])
a = torch.randn(2, 1, device="cuda")
b = torch.randn(8, device="cuda")
out = jfoo(a, b)
print(out.shape)
print("cache_hit: ", thunder.cache_hits(jfoo))
```
e.g. 0
```
def foo(a, b):
if tuple(a.shape) == tuple((2, 2)):
return a
else:
return b
```
We have a trace like this:
```
@torch.no_grad()
@no_autocast
def computation(a):
# a: "cuda:0 f32[[IntegerProxy name=i0, value=2, static=CONSTRAINT.CONSTRAINABLE], [IntegerProxy name=i1, value=2, static=CONSTRAINT.CONSTRAINABLE]]"
# /volume/thunder_dynamic/bool2.py:6: if tuple(a.shape) == tuple((2, 2)):
(i0, i1) = prims.shape(a)
b3 = prims.eq(i0, 2) # b3: "bool True"
b4 = prims.eq(i1, 2) # b4: "bool True"
return a
```
It shows the comparison of each shape element, but that check is not propagated to prologue check, so even though the condition changes in the second iteration, we still hit the cache and return the wrong result.
Similarly, there are lots of checks like that in broadcast handling in thunder, so a simple binary add with the same input from the example would also show a cache hit, even though the broadcast in the second iteration changes.
e.g. 1
```
def foo(a, b):
return a + b
```
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with thunder/core/jit_ext.py, especially propagate_constraints, and run the provided thunder.jit example with cache="symbolic values" to inspect the prologue and compute traces. Trace how TensorProxy.shape comparisons and broadcast checks reach the prologue; done means the second input pair does not reuse an invalid cache entry and returns the correct result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- compilers, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100