Skip to content

test_lambdify.py unit test fails when using symengine compiled with tcmalloc #475

@iyanmv

Description

@iyanmv

Edited description (Jan 19, 2025)

Test test_lambdify__sympy() from test_lambdify.py fails when sympy is installed and symengine was compiled with WITH_TCMALLOC=ON.

Minimal example:

import symengine as se
import sympy as sp

cb = sp.lambdify
Mtx = sp.Matrix
x, y = se.symbols('x, y')
args = Mtx(2, 1, [x, y])
v = Mtx(2, 1, [x**3 * y, (x+1)*(y+1)])
jac = v.jacobian(args)
exprs = [jac, x+y, v, (x+1)*(y+1)]
lmb = cb(args, exprs)  # Crashes here

This crashes for me with:

src/tcmalloc.cc:309] Attempt to free invalid pointer 0x5d72bb4df8f0 
[1]    329398 illegal hardware instruction (core dumped)  python

Bug in Arch Linux package: https://gitlab.archlinux.org/archlinux/packaging/packages/python-symengine/-/issues/2

Original description (May 11, 2024)

Some tests from qiskit work fine when using symengine from PyPI but failed when using symengine and python-symengine from the Arch Linux repos. After using [the same PKGBUILD] to rebuild the packages but just disabling tcmalloc the tests don't crash anymore.

I've reported this in qiskit repo (Qiskit/qiskit#12359) but they think it's a symengine bug.

Here is a minimal example that can produce the crash affecting the unit tests:

from qiskit.circuit.parameter import Parameter
from qiskit.circuit.parameterexpression import ParameterExpression


theta = Parameter("theta")
phi = Parameter("phi")
sum_param = theta + phi

sum_param._symbol_expr.__reduce__()
# src/tcmalloc.cc:304] Attempt to free invalid pointer 0x645c8e3eaa30 
# [1]    987079 IOT instruction (core dumped)  python

This same example works fine when symegine is compiled without tcmalloc. Curiously, if one imports symengine in the same script, it also works (even with symengine with tcmalloc):

import symengine
from qiskit.circuit.parameter import Parameter
from qiskit.circuit.parameterexpression import ParameterExpression


theta = Parameter("theta")
phi = Parameter("phi")
sum_param = theta + phi

sum_param._symbol_expr.__reduce__()
# (<cyfunction load_basic at 0x784de9289970>, (b'\x01\x00\x00\x0b\x00\x01\x00\x00\x80\x10\x00\x00\x00\x02\x00\x00\x80\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x000\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x80\r\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00phi\x04\x00\x00\x80\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x001\x05\x00\x00\x80\r\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00theta\x06\x00\x00\x80\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x001',))

Perhaps some extra 👀 from here can help understand this issue better :) Also, can you clarify if the recommended way to create packages for distros is to use tcmalloc or not? Arch uses tcmalloc but also -DWITH_SYMENGINE_THREAD_SAFE=ON. I guess it's a mixture of the flags recommended for distros and the optimized build. Is this properly tested? I noticed that the official wheel packages for PyPI, for example, do not use tcmalloc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions