pymc-devs / pymc-devs/pytensor

Reconsider checking for input alias during function calls

Open
#1,026 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

maintenance performance
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

Description

In trying to simplify Function.__call__, (see #1024 and #222), I noticed some complicated logic to check if inputs marked as mutable (or borrowable) are not aliasing to the same memory of each other.

https://github.com/pymc-devs/pytensor/blob/be358ed223d41c47c176992db48ad4abd1c347ee/pytensor/compile/function/types.py#L888-L933

To avoid erroneous computation, __call__ tries to copy aliased inputs. However this logic is wrong because it assumes only variables with the same type can be aliased which doesn't make sense. See the example below where a matrix and a vector are aliased, which fails the check and return wrong values and corrupted input y which was not marked as mutable

import pytensor
import pytensor.tensor as pt
from pytensor import In
import numpy as np

x = pt.vector()
y = pt.matrix()

fn = pytensor.function([In(x, mutable=True), In(y, mutable=False)], [x * 2, y * 2])
fn.dprint(print_destroy_map=True)
# Mul [id A] d={0: [1]} 0
#  ├─ [2.] [id B]
#  └─ <Vector(float64, shape=(?,))> [id C]
# Mul [id D] d={0: [1]} 1
#  ├─ [[2.]] [id E]
#  └─ <Matrix(float64, shape=(?, ?))> [id F]

y_val = np.ones((2, 5))
x_val = y_val[0]  # x is an alias of y
res1, res2 = fn(x_val, y_val)
print(res1)
# [2. 2. 2. 2. 2.]

print(res2)  # Wrong
# [[4. 4. 4. 4. 4.]
#  [2. 2. 2. 2. 2.]]

print(y_val)  # Corrupted
# [[2. 2. 2. 2. 2.]
#  [1. 1. 1. 1. 1.]]

My suggestion is not to make the check for alias more robust (and therefore increase the Function call overhead), but instead to forego it completely. If users indicated that an input is mutable it shouldn't be too surprising that views of that input (or other variables sharing the same underlying memory) would also be corrupted.

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 in pytensor/compile/function/types.py around lines 888-933 and reproduce the NumPy aliasing example from the issue. Trace how Function.call handles mutable and borrowable inputs; done means the incorrect same-type assumption no longer causes the aliased matrix input to be corrupted or produce the wrong result.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.