pymc-devs / pymc-devs/pytensor

Add specialized float32 numba dispatches for transcendental functions

Open
#2,314 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

tanh, cos, sin, exp, and erfc admit polynomial approximations that offer nice speedups over what we currently have. Numbers from a very quick claude session:

  float32, 12.6M elements, single-threaded, min of 15                                                       
                                                                                                            
  ┌──────┬──────────────────┬─────────────────┬─────────┬─────────┬─────────────┐                           
  │  op  │ now (numba libm) │ with polynomial │ speedup │  saved  │    error    │                           
  ├──────┼──────────────────┼─────────────────┼─────────┼─────────┼─────────────┤                           
  │ cos  │ 67.50 ms         │ 7.79 ms         │ 8.66x   │ 59.7 ms │ 5.5 ULP abs │                           
  ├──────┼──────────────────┼─────────────────┼─────────┼─────────┼─────────────┤                           
  │ sin  │ 61.25 ms         │ 7.21 ms         │ 8.50x   │ 54.0 ms │ 5.9 ULP abs │                           
  ├──────┼──────────────────┼─────────────────┼─────────┼─────────┼─────────────┤                           
  │ erfc │ 102.58 ms        │ 45.42 ms        │ 2.26x   │ 57.2 ms │ 4.0 ULP rel │                           
  ├──────┼──────────────────┼─────────────────┼─────────┼─────────┼─────────────┤                           
  │ tanh │ 48.67 ms         │ 12.61 ms        │ 3.86x   │ 36.1 ms │ 2.7 ULP rel │                           
  ├──────┼──────────────────┼─────────────────┼─────────┼─────────┼─────────────┤                           
  │ exp  │ 17.70 ms         │ 5.69 ms         │ 3.23x   │ 12.0 ms │ 2.3 ULP rel │                           
  └──────┴──────────────────┴─────────────────┴─────────┴─────────┴─────────────┘

These functions show up in e.g. machine learning where we want to do float32, so it might be nice to implement these. On the other hand, they are less accurate, especially erfc/exp in the tails. I could do them as specialty dispatches in pytensor-ml, but I thought they might be more broadly interesting hence this issue first.

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 locating the existing Numba/libm dispatch path for tanh, cos, sin, exp, and erfc and review how float32 inputs are handled. Define acceptable accuracy, tail behavior, and benchmark expectations for each polynomial implementation; done means the specialized dispatches are integrated and their speed and error are validated.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.