Skip to content

Numba obj fallback fails for Blockwise with scalar outputs #1760

@ricardoV94

Description

@ricardoV94

Description

import numpy as np
import pytensor
import pytensor.tensor as pt
from pytensor.graph.basic import Apply
from pytensor.scalar import ScalarOp
from pytensor.tensor.elemwise import Elemwise
from pytensor.tensor.blockwise import Blockwise


class ScalarOp(ScalarOp):
    def make_node(self, x):
        return Apply(self, [x], [x.type()])

    def perform(self, node, inputs, outputs):
        [x] = inputs
        outputs[0][0] = x + 1

x = pt.vector("x")
y = Elemwise(ScalarOp())(x)
fn = pytensor.function([x], y, mode="NUMBA")
# UserWarning: Numba will use object mode to run ScalarOp's perform method
np.testing.assert_allclose(fn(np.zeros((3,))), [1, 1, 1])

x = pt.vector("x")
z = Blockwise(ScalarOp(), signature="()->()")(x)
# UserWarning: Numba will use object mode to run ScalarOp's perform method
fn = pytensor.function([x], z, mode="NUMBA")
fn(np.zeros((3,)))  # TypeError: can't unbox array from PyObject into native value.  The object maybe of a different type

Blockwise is different than Elemwise in that it gets wrapped in a BlockwiseWithCoreShape, which is an OpFromGraph. So the failure may be more general to OpFromGraph

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions