Skip to content

Fix: PY_SSIZE_T_CLEAN macro must be defined for '#' formats #6928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AdityaMayukhSom
Copy link

New contributor declaration

  • I am not making a trivial change, such as fixing a typo in a comment.

  • I have written a PR description following these rules.

Fixes #5919.

For python 3.12 and older, the macro PY_SSIZE_T_CLEAN must be defined before including Python.h to use all # variants of formats (s#, y#, etc.).

This PR defines PY_SSIZE_T_CLEAN in the first line for both AMD and NVIDIA backend C and python modules. This fixes SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats on python 3.12 and below when driver.c file is dynamically loaded and converted into module.

This change is necessary because as of the date of this PR, fine tuning models with unsloth and vLLM only works with python 3.12 and below.

  • I have run pre-commit run --from-ref origin/main --to-ref HEAD.

  • Select one of the following.

    • I have added tests.
      • /test for lit tests
      • /unittest for C++ tests
      • /python/test for end-to-end tests
    • This PR does not need a test because existing builds should continue to succeed .
  • Select one of the following.

    • I have not added any lit tests.
    • The lit tests I have added follow these best practices,
      including the "tests should be minimal" section. (Usually running Python code
      and using the instructions it generates is not minimal.)

@AdityaMayukhSom AdityaMayukhSom changed the title fixes definition of PY_SSIZE_T_CLEAN macro Fix: PY_SSIZE_T_CLEAN macro must be defined for '#' formats May 24, 2025
@@ -1,10 +1,10 @@
#define PY_SSIZE_T_CLEAN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you only want to fix this file, why trigger other changes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't understand

For python 3.12 and older, the macro PY_SSIZE_T_CLEAN must be defined before including Python.h to use all # variants of formats (s#, y#, etc.).

Isn't is already defined before Python.h here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do clean up your PR

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I unintentionally formatted the python files by mistake, I have removed the unwanted changes.

Regarding your second question, yes it was defined before Python.h, but the error was still present (unsloth version '2025.5.7').

My understanding is that the error was due #include statements written before PY_SSIZE_T_CLEAN was defined, as per the system error message PY_SSIZE_T_CLEAN macro must be defined for '#' forma.

Defining it on top of the file removed the error for me (on python 3.12.10).

Copy link
Contributor

@Jokeren Jokeren May 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you did is not consistent with your comment

For python 3.12 and older, the macro PY_SSIZE_T_CLEAN must be defined before including Python.h to use all # variants of formats (s#, y#, etc.).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug seems to still exist. The macro must also be defined in the embedded C source code defined in driver.py, in the make_lanucher function, line 229:

` params.append("&global_scratch")
src = f"""
#include "cuda.h"
#include <stdbool.h>
#include <Python.h>
#include <dlfcn.h>

`
PY_SSIZE_T_CLEAN is not defined before Python.h is included.

@debduthira
Copy link

@AdityaMayukhSom thanks for the fix. I was facing the same issue while training using SFT Trainer. Shifting #define PY_SSIZE_T_CLEAN above other #include statements fixed the issue.

@@ -227,6 +227,7 @@ def format_of(ty):
params = [f"&arg{i}" for i, ty in signature.items() if ty != "constexpr"]
params.append("&global_scratch")
src = f"""
#define PY_SSIZE_T_CLEAN
#include \"cuda.h\"
#include <stdbool.h>
#include <Python.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like the only change that is likely to make any difference. All the other cases already defined the macro before including Python.h.

@hongbo-miao
Copy link

(Came from Unsloth unslothai/unsloth#2760, met this issue for Unsloth notebook on RTX 5090. Hopefully this can fix soon, thank you! ☺️)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats
6 participants