jax icon indicating copy to clipboard operation
jax copied to clipboard

Fixed lapack kernel null pointer issue

Open Ma-gi-cian opened this issue 1 year ago • 3 comments

Closes #29610 - Fixes segfault when exporting choleksy and svd functions

Example Code

test.py

import jax
import jax.numpy as jnp

with open("tmp.bin", "rb") as file:
    exported_fn = bytearray(file.read())
    rehydrated_fn = jax.export.deserialize(exported_fn)
    data = jnp.array([[2.0, 1.0], [1.0, 2.0]])
    out = rehydrated_fn.call(data)  # seg-fault
    print(out)

main.py

import jax.export
import jax
import jax.numpy as jnp

chol = jax.jit(jax.lax.linalg.cholesky)
data = jnp.array([[2.0, 1.0], [1.0, 2.0]])
exported_main = jax.export.export(chol)(data)
with open("tmp.bin", "wb") as file:
    file.write(exported_main.serialize())

Working Output

(jax-dev) jha@MAGICIAN:~/workspace/opensource/reports/jax-bug-choskley$ python test.py
WARNING:2025-11-25 10:56:18,160:jax._src.xla_bridge:854: An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
[[1.4142135  0.        ]
 [0.70710677 1.2247449 ]]
(jax-dev) jha@MAGICIAN:~/workspace/opensource/reports/jax-bug-choskley$ 

Reason behind the error:

  • These functions ( choleksy , svd , and about in total 15 functions in that particular file where they are defined ) use ffi handlers to get the kernels to perform the operation.
  • These kernel functions are initialized as nullptr and are statically loaded when calling these functions - during normal computation, hence no error.
  • When we export we also correctly embeed the information regarding where they should be.
  • But when we deseralize them again - like load them from the tmp.bin file - the mlir script ( subscript of mlir ) - we do not initialize the lapack kernels ( backends ) and they keep on pointing at the nullptr and hence fail.
  • This does not happen in GPU because they are not statically linked but dynamically so they are always initialized at runtime.
  • The trace also causes them to be initialized hence running the trace functions on them causes them to not give segfault

Ma-gi-cian avatar Nov 25 '25 11:11 Ma-gi-cian

Summary of Changes

Hello @Ma-gi-cian, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical segmentation fault that occurred when deserializing and executing certain exported JAX functions, such as cholesky and svd, specifically on CPU. The issue was traced to uninitialized LAPACK kernels, which are now explicitly initialized during the deserialization process to ensure proper function execution and stability.

Highlights

  • LAPACK Kernel Initialization: Introduced a new helper function, _ensure_backends_initialized, to explicitly initialize LAPACK kernels for CPU platforms when deserializing exported JAX functions, preventing null pointer dereferences.
  • Exported Function Lowering: Modified the _call_exported_lowering function to invoke _ensure_backends_initialized before processing the MLIR module, ensuring that necessary backends are ready for execution of deserialized functions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

gemini-code-assist avatar Nov 25 '25 11:11 gemini-code-assist

Here are some extra debug info, if you run gdb on the previous code without the fix it gives this output :

Command: gdb --args python test.py

[Thread 0x7fffab7fe6c0 (LWP 1994) exited]
[New Thread 0x7fffab7fe6c0 (LWP 1995)]
[New Thread 0x7fffd46df6c0 (LWP 1996)]
[New Thread 0x7fffc47f86c0 (LWP 1997)]
[New Thread 0x7fffaaffd6c0 (LWP 1998)]
[New Thread 0x7fffaabfc6c0 (LWP 1999)]
[New Thread 0x7fffaa7fb6c0 (LWP 2000)]
[New Thread 0x7fffaa3fa6c0 (LWP 2001)]
[New Thread 0x7fffa9ff96c0 (LWP 2002)]
[New Thread 0x7fffa97f86c0 (LWP 2003)]

Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007fffd8a433e7 in jax::CholeskyFactorization<(xla::ffi::DataType)11>::Kernel(xla::ffi::Buffer<(xla::ffi::DataType)11, 18446744073709551615ul>, jax::MatrixParams::UpLo, xla::ffi::Result<xla::ffi::Buffer<(xla::ffi::DataType)11, 18446744073709551615ul> >, xla::ffi::Result<xla::ffi::Buffer<(xla::ffi::DataType)4, 18446744073709551615ul> >) ()
   from /home/jha/miniforge3/envs/jax-dev/lib/python3.12/site-packages/jaxlib/cpu/_lapack.so
#2  0x00007fffd8a8eac1 in xla::ffi::Handler<(xla::ffi::ExecutionStage)3, xla::ffi::Error (*)(xla::ffi::Buffer<(xla::ffi::DataType)11, 18446744073709551615ul>, jax::MatrixParams::UpLo, xla::ffi::Result<xla::ffi::Buffer<(xla::ffi::DataType)11, 18446744073709551615ul> >, xla::ffi::Result<xla::ffi::Buffer<(xla::ffi::DataType)4, 18446744073709551615ul> >), xla::ffi::internal::ArgTag<xla::ffi::Buffer<(xla::ffi::DataType)11, 18446744073709551615ul> >, xla::ffi::internal::AttrTag<jax::MatrixParams::UpLo>, xla::ffi::internal::RetTag<xla::ffi::Buffer<(xla::ffi::DataType)11, 18446744073709551615ul> >, xla::ffi::internal::RetTag<xla::ffi::Buffer<(xla::ffi::DataType)4, 18446744073709551615ul> > >::Call(XLA_FFI_CallFrame*) const () from /home/jha/miniforge3/envs/jax-dev/lib/python3.12/site-packages/jaxlib/cpu/_lapack.so
#3  0x00007fffd8a5ccb9 in lapack_spotrf_ffi ()
   from /home/jha/miniforge3/envs/jax-dev/lib/python3.12/site-packages/jaxlib/cpu/_lapack.so
#4  0x00007fffed00a8c6 in absl::lts_20250814::StatusOr<XLA_FFI_Future*> xla::ffi::Call<XLA_FFI_Error* (*)(XLA_FFI_CallFrame*)>(XLA_FFI_Error* (*&)(XLA_FFI_CallFrame*), xla::ffi::CallFrame&, xla::ffi::CallOptions const&, xla::ffi::ExecutionStage) () from /home/jha/miniforge3/envs/jax-dev/lib/python3.12/site-packages/jaxlib/libjax_common.so
#5  0x00007fffed00aa91 in xla::ffi::CallAsync(XLA_FFI_Error* (*)(XLA_FFI_CallFrame*), xla::ffi::CallFrame&, xla::ffi::CallOptions const&, XLA_FFI_ExecutionStage) ()
   from /home/jha/miniforge3/envs/jax-dev/lib/python3.12/site-packages/jaxlib/libjax_common.so
#6  0x00007ffff1cda1ff in xla::cpu::CustomCallThunk::CallTypedFFI(xla::cpu::Thunk::ExecuteParams const&) ()
   from /home/jha/miniforge3/envs/jax-dev/lib/python3.12/site-packages/jaxlib/libjax_common.so
#7  0x00007ffff1cd930e in xla::cpu::CustomCallThunk::Execute(xla::cpu::Thunk::ExecuteParams const&) ()
   from /home/jha/miniforge3/envs/jax-dev/lib/python3.12/site-packages/jaxlib/libjax_common.so
#8  0x00007ffff6612dcb in xla::cpu::ThunkExecutor::TracedExecute(xla::cpu::Thunk&, xla::cpu::Thunk::ExecuteParams const&) () from /home/jha/miniforge3/envs/jax-dev/lib/python3.12/site-packages/jaxlib/libjax_common.so
#9  0x00007ffff6613d73 in xla::cpu::ThunkExecutor::ExecuteSequential(xla::cpu::Thunk::ExecuteParams const&) ()
   from /home/jha/miniforge3/envs/jax-dev/lib/python3.12/site-packages/jaxlib/libjax_common.so
#10 0x00007ffff6613239 in xla::cpu::ThunkExecutor::Execute(xla::cpu::Thunk::ExecuteParams const&) ()
--Type <RET> for more, q to quit, c to continue without paging--

Now there is no segment fault after the fix. Open to any suggestions by the maintainers

Ma-gi-cian avatar Nov 26 '25 04:11 Ma-gi-cian

Hi @hawkinsp @jakevdp , I was working on adding a test case to this pr but I have encountered some issues. The bug only manifests when we serialize and deserialize from two different files. The following code does not produce this bug :

import jax.export
import jax
import jax.numpy as jnp

chol = jax.jit(jax.lax.linalg.cholesky)
data = jnp.eye(1)
exported_main = jax.export.export(chol)(data).serialize()
rehydrated_fn = jax.export.deserialize(exported_main)
out = rehydrated_fn.call(data)
print(out)

Because the kernels - are still initialized after the export process. We can only produce this bug - we do deserialization from a different file. So would be helpful if you could give any guidance regarding this. Thanks

Ma-gi-cian avatar Dec 10 '25 11:12 Ma-gi-cian

Hi @hawkinsp @jakevdp any progress on this. No hurry from my side. Would appreciate any input on how to proceed with this pr.

Ma-gi-cian avatar Dec 24 '25 09:12 Ma-gi-cian