Skip to content

Conversation

@LeiWang1999
Copy link
Member

@LeiWang1999 LeiWang1999 commented Oct 21, 2025

This pull request updates the build and import structure for the Cython extension in the tilelang package to improve packaging consistency and compatibility. The most important changes include renaming and relocating the Cython extension, updating the CMake build process, and ensuring the correct import paths are set at runtime.

Build system and packaging updates:

  • The Cython extension module has been renamed from cython_wrapper to tilelang_cython_wrapper, and its output location and installation path have been changed to reside within the tilelang package directory for better packaging and import consistency. (CMakeLists.txt)

Python import logic improvements:

  • The import logic in adapter.py has been updated to prefer importing CythonKernelWrapper from tilelang.tilelang_cython_wrapper, with a fallback to the root-level tilelang_cython_wrapper for development builds, improving robustness across different build environments. (tilelang/jit/adapter/cython/adapter.py)
  • The env.py script now ensures all entries in TL_LIBS are added to sys.path, guaranteeing the Cython extension and other dependencies can be imported regardless of their install location. (tilelang/env.py)

Summary by CodeRabbit

  • Chores
    • Updated build and installation to produce and install the Python extension under a dedicated package path for clearer distribution.
  • Bug Fixes
    • Ensured package library directories are prepended to Python’s import search path so bundled libraries are found before system packages.
    • Adjusted extension import targets so runtime imports reference the newly packaged extension name.

@github-actions
Copy link

👋 Hi! Thank you for contributing to the TileLang project.

Please remember to run pre-commit run --all-files in the root directory of the project to ensure your changes are properly linted and formatted. This will help ensure your contribution passes the format check.

We appreciate you taking this step! Our team will review your contribution, and we look forward to your awesome work! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 21, 2025

Warning

Rate limit exceeded

@LeiWang1999 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 54 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 24cbf0c and 9c0b757.

📒 Files selected for processing (1)
  • tilelang/env.py (2 hunks)

Walkthrough

Rename Cython wrapper artifact and module to tilelang_cython_wrapper, update CMake install targets/paths, prepend each TL_LIBS entry to Python sys.path, and change the adapter import to load tilelang_cython_wrapper (raising ImportError on failure).

Changes

Cohort / File(s) Summary
Build configuration
CMakeLists.txt
Changed generated Cython output name from cython_wrapper.cpp to tilelang_cython_wrapper.cpp; use --module-name tilelang_cython_wrapper; create tilelang_cython_wrapper MODULE target; update installation destinations to tilelang with RUNTIME/ARCHIVE/LIBRARY rules.
Import path management
tilelang/env.py
Prepend each entry of TL_LIBS to sys.path if not already present (loop inserting at front), ensuring tilelang library paths are prioritized in import resolution.
Adapter import
tilelang/jit/adapter/cython/adapter.py
Replace direct import from cython_wrapper with import from tilelang_cython_wrapper; on import failure raise ImportError (no additional fallback imports added).

Sequence Diagram(s)

sequenceDiagram
    participant Env as tilelang/env.py
    participant Py as Python runtime
    participant Adapter as adapter.py
    participant Module as tilelang_cython_wrapper

    Env->>Py: for each lib in TL_LIBS\nif lib not in sys.path: sys.path.insert(0, lib)
    Note over Py: sys.path now preferrs TL_LIBS entries

    Adapter->>Py: import tilelang_cython_wrapper
    alt import succeeds
        Py->>Adapter: CythonKernelWrapper loaded
    else import fails
        Py-->>Adapter: ImportError raised
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 A new wrapper hops into its spot,
Paths lined up neatly, preferring our lot.
Imports now leap where the rabbit has led,
If the module won't show — an ImportError said.
Carrots and Cython, tidy and bright. 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "[Refactor] Rename cython output to tilelang_cython and relocate its path" accurately captures the main objectives of the changeset. The actual changes involve renaming the Cython wrapper from cython_wrapper to tilelang_cython_wrapper and relocating its installation path to the tilelang package directory, which directly aligns with what the title describes. While the title uses tilelang_cython rather than the full name tilelang_cython_wrapper, this is a reasonable abbreviation and doesn't fundamentally mislead about the primary refactoring work. The title is concise, specific, and clearly communicates the core change—a teammate reviewing the history would understand this PR is about restructuring the Cython module naming and location. The supporting changes to env.py and adapter.py are enabling work for this main refactoring objective.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
tilelang/jit/adapter/cython/adapter.py (1)

24-33: Fallback import logic is sound; consider improving error messaging.

The nested try-except correctly implements the fallback from the packaged module to the dev build location. The static analysis hint (TRY203) is a false positive—the structure is appropriate for this use case.

However, the final raise will only re-raise the second ImportError (from tilelang_cython_wrapper), potentially hiding issues with the preferred import path. Consider providing a more informative error message that mentions both import attempts.

Optional improvement:

 try:
     # Prefer packaged extension under tilelang/
     from tilelang.tilelang_cython_wrapper import CythonKernelWrapper
 except ImportError:
     try:
         # Fallback for dev builds where the module sits at the root
         from tilelang_cython_wrapper import CythonKernelWrapper
     except ImportError:
-        # TODO: tolerance a build without cython backend
-        raise
+        raise ImportError(
+            "Failed to import CythonKernelWrapper. "
+            "Tried: 'tilelang.tilelang_cython_wrapper' and 'tilelang_cython_wrapper'. "
+            "Ensure the Cython extension is built and installed."
+        )
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1d4b718 and 2bfde5a.

📒 Files selected for processing (3)
  • CMakeLists.txt (1 hunks)
  • tilelang/env.py (1 hunks)
  • tilelang/jit/adapter/cython/adapter.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.14.1)
tilelang/jit/adapter/cython/adapter.py

31-33: Remove exception handler; error is immediately re-raised

(TRY203)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Build wheels for Python 3.8 on ubuntu-24.04-arm with CUDA-12.8
  • GitHub Check: Build wheels for Python 3.8 on ubuntu-latest with CUDA-12.1
  • GitHub Check: Build wheels for Python 3.8 on macos-latest with Metal
  • GitHub Check: Quick Lint
🔇 Additional comments (3)
tilelang/env.py (1)

40-42: LGTM! Import path enhancement supports the refactored Cython module.

The loop ensures all library paths in TL_LIBS are available for imports by prepending them to sys.path. This change directly supports the new tilelang_cython_wrapper module location and complements the fallback import logic in adapter.py.

CMakeLists.txt (2)

161-161: LGTM! Build artifact naming is consistent with the new module structure.

The Cython compilation output and module name have been updated to tilelang_cython_wrapper, aligning with the package structure changes and the import logic in adapter.py.

Also applies to: 166-167


175-180: LGTM! Installation path correctly places the extension within the package.

Installing the Cython wrapper into the tilelang directory ensures the packaged extension can be imported as tilelang.tilelang_cython_wrapper, which matches the preferred import path in adapter.py. The fallback import handles development builds where the module may be at the root level.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2bfde5a and 24cbf0c.

📒 Files selected for processing (1)
  • tilelang/jit/adapter/cython/adapter.py (1 hunks)

Comment on lines 24 to 27
try:
# Load cython_wrapper.api3.so in env.py
from cython_wrapper import CythonKernelWrapper
from tilelang_cython_wrapper import CythonKernelWrapper
except ImportError:
# TODO: tolerance a build without cython backend
raise
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Inconsistent with PR objectives: Missing fallback import mechanism.

The PR description states this change should "prefer importing CythonKernelWrapper from tilelang.tilelang_cython_wrapper, with a fallback to the root-level tilelang_cython_wrapper for development builds and varied build environments." However, the code only attempts a single import from tilelang_cython_wrapper and immediately re-raises on failure.

Consider implementing the fallback mechanism for improved robustness across different build configurations:

 try:
-    from tilelang_cython_wrapper import CythonKernelWrapper
+    from tilelang.tilelang_cython_wrapper import CythonKernelWrapper
 except ImportError:
-    raise
+    # Fallback for development builds or varied build environments
+    from tilelang_cython_wrapper import CythonKernelWrapper

Alternatively, if the fallback is unnecessary and the description is outdated, remove the redundant try-except block:

-try:
-    from tilelang_cython_wrapper import CythonKernelWrapper
-except ImportError:
-    raise
+from tilelang_cython_wrapper import CythonKernelWrapper

Please verify whether the fallback mechanism is required for the intended build environments.


🏁 Script executed:

#!/bin/bash
# Verify how tilelang_cython_wrapper is installed and whether it's accessible both ways

# Check CMakeLists.txt for module naming and installation paths
rg -n -A5 -B5 'tilelang_cython_wrapper' --type=cmake

# Check if there are other imports of this module in the codebase
rg -n -C3 'from.*tilelang_cython_wrapper.*import|import.*tilelang_cython_wrapper'

# Check env.py to understand how sys.path is modified
fd -e py -x grep -l 'TL_LIBS' {} \; | head -5 | xargs rg -n -C5 'TL_LIBS.*sys.path|sys.path.*TL_LIBS'

Length of output: 2074


</shell_scripts_instructions>


🏁 Script executed:

#!/bin/bash
# Search for sys.path manipulation and environment setup

# Check for setup.py or pyproject.toml
fd -e toml -o -e setup.py | head -10

# Search for sys.path additions anywhere in the codebase
rg -n 'sys\.path' --type=py -B2 -A2

# Check tilelang/__init__.py for any path setup
fd tilelang/__init__.py -x cat {}

# Look for env.py files
fd -n env.py -x cat {}

Length of output: 5779


🏁 Script executed:

#!/bin/bash
# Get context around TL_LIBS definition
rg -n 'TL_LIBS' tilelang/env.py -B5 -A10

# Check if adapter.py imports env or if there's a dependency chain
rg -n 'import.*env|from.*env' tilelang/jit/adapter/cython/adapter.py -B2 -A2

# Check what imports adapter.py and if they import env
rg -n 'from.*adapter.*import|import.*adapter' --type=py | head -20

# Get the full context of tilelang/__init__.py
cat tilelang/__init__.py 2>/dev/null || echo "tilelang/__init__.py not found"

Length of output: 2204


🏁 Script executed:

#!/bin/bash
# Search with correct git/ prefix
grep -rn 'TL_LIBS' git/tilelang/env.py -B5 -A10 2>/dev/null || echo "env.py search failed"

# Check adapter.py imports
grep -n 'import' git/tilelang/jit/adapter/cython/adapter.py | head -20

# Check if tilelang __init__.py exists and what it imports
cat git/tilelang/__init__.py 2>/dev/null | head -50 || echo "__init__.py not found"

# Check cython adapter __init__.py
cat git/tilelang/jit/adapter/cython/__init__.py

# Search for where CythonKernelAdapter is actually used
grep -rn 'CythonKernelAdapter' git/tilelang --include='*.py' | grep -v test | head -10

Length of output: 4956


Inconsistent with PR objectives: Missing fallback import mechanism.

The PR description states this change should "prefer importing CythonKernelWrapper from tilelang.tilelang_cython_wrapper, with a fallback to the root-level tilelang_cython_wrapper for development builds and varied build environments." However, the code only attempts a single import from tilelang_cython_wrapper and immediately re-raises on failure.

Based on the codebase analysis:

  • CMakeLists.txt installs the module to DESTINATION tilelang (resulting in tilelang/tilelang_cython_wrapper.so)
  • Development mode adds the build/ directory to sys.path via env.py, making the top-level import work temporarily
  • Production installations place the module in the tilelang package, requiring tilelang.tilelang_cython_wrapper import path
  • Current code lacks fallback and will fail in standard installed environments

Implement the described fallback mechanism:

 try:
-    from tilelang_cython_wrapper import CythonKernelWrapper
+    from tilelang.tilelang_cython_wrapper import CythonKernelWrapper
 except ImportError:
-    raise
+    # Fallback for development builds with top-level module in build directory
+    from tilelang_cython_wrapper import CythonKernelWrapper
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try:
# Load cython_wrapper.api3.so in env.py
from cython_wrapper import CythonKernelWrapper
from tilelang_cython_wrapper import CythonKernelWrapper
except ImportError:
# TODO: tolerance a build without cython backend
raise
try:
from tilelang.tilelang_cython_wrapper import CythonKernelWrapper
except ImportError:
# Fallback for development builds with top-level module in build directory
from tilelang_cython_wrapper import CythonKernelWrapper
🧰 Tools
🪛 Ruff (0.14.1)

26-27: Remove exception handler; error is immediately re-raised

(TRY203)

🤖 Prompt for AI Agents
In tilelang/jit/adapter/cython/adapter.py around lines 24 to 27, replace the
single top-level import with a two-step import: first try "from
tilelang.tilelang_cython_wrapper import CythonKernelWrapper", and if that raises
ImportError fall back to "from tilelang_cython_wrapper import
CythonKernelWrapper"; if both fail re-raise the ImportError (or raise a new
ImportError with a clear message) so both production-installed
(tilelang.tilelang_cython_wrapper) and development/build-time
(tilelang_cython_wrapper) import layouts are supported.

@LeiWang1999 LeiWang1999 merged commit 60e9c7e into tile-ai:main Oct 21, 2025
8 of 9 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.

1 participant