Skip to content

fix the 8-bit vector loads/stores problem, which will solve the problem raised in the codegen test for cuda#18398

Merged
tqchen merged 2 commits into
apache:mainfrom
hantao-zhou:main
Oct 26, 2025
Merged

fix the 8-bit vector loads/stores problem, which will solve the problem raised in the codegen test for cuda#18398
tqchen merged 2 commits into
apache:mainfrom
hantao-zhou:main

Conversation

@hantao-zhou

Copy link
Copy Markdown
Contributor

reinterpret_cast byte indexing, instead of rolled bit packing, which will omit certain bits.

…nterpret_cast byte indexing, instead of rolled bit packing, which will omit certain bits.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @hantao-zhou, I'm Gemini Code Assist1! 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 refactors the CUDA code generation logic for 8-bit vector loads and stores. It replaces an error-prone bit-packing and shifting mechanism with a more robust reinterpret_cast approach, allowing for direct byte-level indexing. This change ensures accurate handling of 8-bit data within vector types, resolving existing issues in CUDA codegen tests and improving the reliability of generated code.

Highlights

  • Corrected 8-bit Vector Access: The mechanism for accessing individual 8-bit elements within vector types has been updated from bit-shifting and masking to using reinterpret_cast for direct byte-level indexing.
  • Improved CUDA Codegen: This change specifically addresses and resolves a problem in CUDA code generation related to 8-bit vector loads and stores, which was causing issues in codegen tests.
  • Explicit Signedness for Char Types: The char type used for integer representations has been explicitly changed to signed char to ensure correct signedness handling during vector element operations.
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 👍 and 👎 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.

Footnotes

  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 gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request provides a critical fix for 8-bit integer vector load and store operations in the CUDA code generator. The previous implementation, which relied on bit-shifting, was problematic due to implementation-defined behavior with signed integers and was logically incorrect for byte extraction.

The changes introduce a reinterpret_cast to signed char* or unsigned char* to directly access the bytes of the vector. This is a much safer, more portable, and more efficient approach. The explicit use of signed char instead of char also improves code clarity and correctness.

Overall, this is an excellent fix that improves the robustness and quality of the generated CUDA code. The changes are correct and well-implemented.

@hantao-zhou

Copy link
Copy Markdown
Contributor Author

(matopbench) hans@fedora:~/workspace/matopbench/external/tvm/build$ python /home/hans/workspace/matopbench/external/tvm/tests/python/codegen/test_target_codegen_cuda.py
enabled targets: llvm; cuda; nvptx
pytest marker:
===================================================================================================================================== test session starts ======================================================================================================================================
platform linux -- Python 3.14.0, pytest-8.4.2, pluggy-1.6.0 -- /home/hans/workspace/matopbench/.pixi/envs/default/bin/python
cachedir: .pytest_cache
rootdir: /home/hans/workspace/matopbench/external/tvm
configfile: pyproject.toml
collected 27 items

../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_vectorize_add PASSED [ 3%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_bf16_vectorize_add PASSED [ 7%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_multiply_add PASSED [ 11%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_vectorize_load PASSED [ 14%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_make_int8 FAILED [ 18%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_inf_nan PASSED [ 22%]
../tests/python/codegen/test_target_codegen_cuda.py::test_crossthread_reduction1[cuda] PASSED [ 25%]
../tests/python/codegen/test_target_codegen_cuda.py::test_crossthread_reduction1[rocm] SKIPPED (No device exists for target rocm) [ 29%]
../tests/python/codegen/test_target_codegen_cuda.py::test_crossthread_reduction2[cuda] PASSED [ 33%]
../tests/python/codegen/test_target_codegen_cuda.py::test_crossthread_reduction2[rocm] SKIPPED (No device exists for target rocm) [ 37%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_reduction_binding PASSED [ 40%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_const_float_to_half PASSED [ 44%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_floordiv_with_vectorization PASSED [ 48%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_floormod_with_vectorization PASSED [ 51%]
../tests/python/codegen/test_target_codegen_cuda.py::test_vectorized_casts PASSED [ 55%]
../tests/python/codegen/test_target_codegen_cuda.py::test_vectorized_intrin1 PASSED [ 59%]
../tests/python/codegen/test_target_codegen_cuda.py::test_vectorized_intrin2 PASSED [ 62%]
../tests/python/codegen/test_target_codegen_cuda.py::test_vectorized_popcount PASSED [ 66%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_vectorize_load_permute_pad PASSED [ 70%]
../tests/python/codegen/test_target_codegen_cuda.py::test_try_unaligned_vector_load PASSED [ 74%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_thread_sync_inside_condition PASSED [ 77%]
../tests/python/codegen/test_target_codegen_cuda.py::test_invalid_reinterpret PASSED [ 81%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_tensormap PASSED [ 85%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_device_func_call PASSED [ 88%]
../tests/python/codegen/test_target_codegen_cuda.py::test_cuda_float_const_hex_format PASSED [ 92%]
../tests/python/codegen/test_target_codegen_cuda.py::test_device_host_call_same_func PASSED [ 96%]
../tests/python/codegen/test_target_codegen_cuda.py::test_thread_return PASSED [100%]

=========================================================================================================================================== FAILURES ===========================================================================================================================================
_____________________________________________________________________________________________________________________________________ test_cuda_make_int8 ______________________________________________________________________________________________________________________________________
../tests/python/codegen/test_target_codegen_cuda.py:205: in test_cuda_make_int8
check_cuda(64, np.int8(0xAB), 4)
^^^^^^^^^^^^^
E OverflowError: Python integer 171 out of bounds for int8

This is the current result, as mentioned, i have mixed the error used to exist in the vectorized_casts part,
The current error is not related to the issue, it should be some kinda mismatch of numpy with the TVM

@tqchen tqchen merged commit 356cb57 into apache:main Oct 26, 2025
10 checks passed
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.

2 participants