pymc-devs / pymc-devs/pytensor

Numpy 2.x C implementation for AdvancedIncSubtensor1

Open
#1,325 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

In our bump to numpy 2.0 we had to stop supporting the C implementation of this Op https://github.com/pymc-devs/pytensor/pull/1194

This was because we were using this PyArrayMapIter functionality that numpy removed. The Op needs to be able to iterate over the first dimension of an array to increment the values provided in y. This increment may need to broadcast with the selected entries.

Example:

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

x = pt.tensor("x", shape=(6, 2, 3))
y = pt.vector("y", shape=(3,))
idxs = pt.vector("idxs", shape=(7,), dtype=int)
out = pt.subtensor.advanced_inc_subtensor1(x, y, idxs)

x_test = np.zeros(x.type.shape)
y_test = np.ones(y.type.shape)
idxs_test = [0, 0, 0, 1, 2, 3, 5]
res = out.eval({x: x_test, y: y_test, idxs: idxs_test})
print(res)
# [[[3. 3. 3.]
#   [3. 3. 3.]]
#  [[1. 1. 1.]
#   [1. 1. 1.]]
#  [[1. 1. 1.]
#   [1. 1. 1.]]
#  [[1. 1. 1.]
#   [1. 1. 1.]]
#  [[0. 0. 0.]
#   [0. 0. 0.]]
#  [[1. 1. 1.]
#   [1. 1. 1.]]]

It's a restricted version of numpy.add.at, where indices must be a single integer vector, whereas numpy.add.at supports arbitrary advanced indexing.

res_np = x_test.copy()
np.add.at(res_np, idxs_test, y_test)
np.testing.assert_allclose(res, res_np)

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 at the pytensor.tensor advanced_inc_subtensor1 entry point and review the NumPy 2.0 limitation described in PR #1194. Use the provided evaluation and np.add.at comparison as acceptance checks; done means the C path works on NumPy 2.x with repeated indices and broadcasting.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, numpy, python
Domain
backend, performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.