pymc-devs / pymc-devs/pytensor

Reconsider handlig of constants in numba backend

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

Nobody has claimed this yet.

backend compatibility numba
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

Description

In the numba backend (and all Jitted backends), we massage constants into global variables of the codegen function.

This poses has a couple of issues for the Numba backend:

  1. Functions with large arrays can't be cached
  2. The number of cached functions grows with small differences in the global arrays, but otherwise identical structure (not sure how common)
  3. Functions with more esoteric constant types can't be compiled: SparseVariables, RandomGenerator, TypedLists (see failures in #1676 and #811)

We could adopt the approach that the C-backend takes, which is to just use pointers to the storage list created by PyTensor function, for these constants. We only inline scalars in the C-backend codegen. This would work-around all the issues listed above.

On the downside, I'm not sure how hard it would make things for nutpie. CC @aseyboldt

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 by tracing how the Numba and other jitted backends turn constants into codegen-function globals, then compare that path with the C-backend's pointer-based handling. Review the failures referenced in #1676 and #811, and consider the stated interaction with nutpie. Done means large arrays can be cached without unnecessary cache growth and the listed constant types compile successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, performance
Issue type
Refactor
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.