Skip to content

[Bugfix] Fix vllm_flash_attn rotary import #17247

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

Closed
wants to merge 2 commits into from

Conversation

jeejeelee
Copy link
Collaborator

@jeejeelee jeejeelee commented Apr 27, 2025

Using qwen2-vl can reproduce this issue.
FIX #17263

Co-authored-by: Aaron Pham [email protected]

Signed-off-by: Jee Jee Li <[email protected]>
Copy link

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

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

Copy link
Collaborator

@aarnphm aarnphm left a comment

Choose a reason for hiding this comment

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

One slight change

setup.py Outdated
Comment on lines 285 to 302
src_layers = os.path.join(self.build_lib, "vllm", "vllm_flash_attn",
"layers")
dst_layers = os.path.join("vllm", "vllm_flash_attn", "layers")
if os.path.exists(src_layers):
print(f"Copying directory {src_layers} to {dst_layers}")
if os.path.exists(dst_layers):
shutil.rmtree(dst_layers)
shutil.copytree(src_layers, dst_layers)

# copy vllm_flash_attn/ops dir
src_ops = os.path.join(self.build_lib, "vllm", "vllm_flash_attn",
"ops")
dst_ops = os.path.join("vllm", "vllm_flash_attn", "ops")
if os.path.exists(src_ops):
print(f"Copying directory {src_ops} to {dst_ops}")
if os.path.exists(dst_ops):
shutil.rmtree(dst_ops)
shutil.copytree(src_ops, dst_ops)
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should use self.copy_tree instead

Suggested change
src_layers = os.path.join(self.build_lib, "vllm", "vllm_flash_attn",
"layers")
dst_layers = os.path.join("vllm", "vllm_flash_attn", "layers")
if os.path.exists(src_layers):
print(f"Copying directory {src_layers} to {dst_layers}")
if os.path.exists(dst_layers):
shutil.rmtree(dst_layers)
shutil.copytree(src_layers, dst_layers)
# copy vllm_flash_attn/ops dir
src_ops = os.path.join(self.build_lib, "vllm", "vllm_flash_attn",
"ops")
dst_ops = os.path.join("vllm", "vllm_flash_attn", "ops")
if os.path.exists(src_ops):
print(f"Copying directory {src_ops} to {dst_ops}")
if os.path.exists(dst_ops):
shutil.rmtree(dst_ops)
shutil.copytree(src_ops, dst_ops)
for folder in ("layers", "ops"):
src = os.path.join(self.build_lib, "vllm", "vllm_flash_attn", folder)
out = os.path.join("vllm", "vllm_flash_attn", folder)
self.copy_tree(src, out)

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can use this patch here

diff --git a/setup.py b/setup.py
index 671ec7739..f5b3f5165 100755
--- a/setup.py
+++ b/setup.py
@@ -283,6 +283,13 @@ class cmake_build_ext(build_ext):
             dst_file = os.path.join("vllm/vllm_flash_attn", basename)
             print(f"Copying {file} to {dst_file}")
             self.copy_file(file, dst_file)
+        # We will need to copy these folders to use its ops for qwen-vl
+        for folder in ("layers", "ops"):
+            src = os.path.join(self.build_lib, 'vllm', 'vllm_flash_attn',
+                               folder)
+            out = os.path.join('vllm', 'vllm_flash_attn', folder)
+            print(f"Copying {folder} from vllm/vllm_flash_attn")
+            self.copy_tree(src, out)
 
 
 class repackage_wheel(build_ext):
@@ -402,6 +409,32 @@ class repackage_wheel(build_ext):
 
                 package_data[package_name].append(file_name)
 
+            # Extract and include the layers and ops folders for qwen-vl
+            folders_to_copy = {"layers", "ops"}
+            for folder in folders_to_copy:
+                folder_path = f"vllm/vllm_flash_attn/{folder}"
+                folder_files = [
+                    f for f in wheel.filelist
+                    if f.filename.startswith(folder_path)
+                ]
+
+                if folder_files:
+                    print(f"Include {folder} folder from vllm/vllm_flash_attn")
+                    for file in folder_files:
+                        wheel.extract(file)
+
+                        # Add the file to package_data if it's not a Python file
+                        rel_path = file.filename.split("/")
+                        # vllm/vllm_flash_attn/folder/file
+                        if len(rel_path) >= 4:
+                            package_name = "vllm.vllm_flash_attn." + folder
+                            file_name = rel_path[-1]
+
+                            if not file_name.endswith(".py"):
+                                if package_name not in package_data:
+                                    package_data[package_name] = []
+                                package_data[package_name].append(file_name)
+
 
 def _is_hpu() -> bool:
     # if VLLM_TARGET_DEVICE env var was set explicitly, skip HPU autodetection

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you

Signed-off-by: Jee Jee Li <[email protected]>
@jeejeelee jeejeelee requested a review from aarnphm April 27, 2025 14:47
@aarnphm aarnphm self-assigned this Apr 27, 2025
Copy link
Collaborator

@aarnphm aarnphm left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM

Can you also add a Co-authored-by: Aaron Pham <[email protected]> to the description.

@aarnphm
Copy link
Collaborator

aarnphm commented Apr 27, 2025

Hmm, @jeejeelee there is a issue with building on CPU here?

@aarnphm
Copy link
Collaborator

aarnphm commented Apr 27, 2025

Ah we need to gated the copy ovee in _is_cuda() only here.

27fdbeea7 - chore: only gated in CUDA (HEAD -> fix-flash-att-rotray)

Signed-off-by: Aaron Pham <[email protected]>
(G) Aaron Pham <[email protected]> (Sun Apr 27 14:57:44 2025 +0000)


diff --git a/setup.py b/setup.py
index 34be6a641..8e18c8215 100755
--- a/setup.py
+++ b/setup.py
@@ -280,13 +280,14 @@ class cmake_build_ext(build_ext):
             print(f"Copying {file} to {dst_file}")
             self.copy_file(file, dst_file)
 
-        # copy these folders to use the vllm_flash_attn rotary_kernel.
-        for folder in ("layers", "ops"):
-            src = os.path.join(self.build_lib, "vllm", "vllm_flash_attn",
-                               folder)
-            out = os.path.join("vllm", "vllm_flash_attn", folder)
-            print(f"Copying {folder} from vllm/vllm_flash_attn")
-            self.copy_tree(src, out)
+        if _is_cuda():
+            # copy these folders to use the vllm_flash_attn rotary_kernel.
+            for folder in ("layers", "ops"):
+                src = os.path.join(self.build_lib, "vllm", "vllm_flash_attn",
+                                   folder)
+                out = os.path.join("vllm", "vllm_flash_attn", folder)
+                print(f"Copying {folder} from vllm/vllm_flash_attn")
+                self.copy_tree(src, out)
 
 
 class repackage_wheel(build_ext):
@@ -409,29 +410,34 @@ class repackage_wheel(build_ext):
                 package_data[package_name].append(file_name)
 
             # Extract and include the layers and ops of rotary embedding.
-            folders_to_copy = {"layers", "ops"}
-            for folder in folders_to_copy:
-                folder_path = f"vllm/vllm_flash_attn/{folder}"
-                folder_files = [
-                    f for f in wheel.filelist
-                    if f.filename.startswith(folder_path)
-                ]
-
-                if folder_files:
-                    print(f"Include {folder} folder from vllm/vllm_flash_attn")
-                    for file in folder_files:
-                        wheel.extract(file)
-
-                        # Add the file to package_data if it's not a Python file
-                        rel_path = file.filename.split("/")
-                        # vllm/vllm_flash_attn/folder/file
-                        if len(rel_path) >= 4:
-                            package_name = "vllm.vllm_flash_attn." + folder
-                            file_name = rel_path[-1]
-                            if not file_name.endswith(".py"):
-                                if package_name not in package_data:
-                                    package_data[package_name] = []
-                                package_data[package_name].append(file_name)
+            if _is_cuda():
+                folders_to_copy = {"layers", "ops"}
+                for folder in folders_to_copy:
+                    folder_path = f"vllm/vllm_flash_attn/{folder}"
+                    folder_files = [
+                        f for f in wheel.filelist
+                        if f.filename.startswith(folder_path)
+                    ]
+
+                    if folder_files:
+                        print(
+                            f"Include {folder} folder from vllm/vllm_flash_attn"
+                        )
+                        for file in folder_files:
+                            wheel.extract(file)
+
+                            # Add the file to package_data if
+                            # it's not a Python file
+                            rel_path = file.filename.split("/")
+                            # vllm/vllm_flash_attn/folder/file
+                            if len(rel_path) >= 4:
+                                package_name = "vllm.vllm_flash_attn." + folder
+                                file_name = rel_path[-1]
+                                if not file_name.endswith(".py"):
+                                    if package_name not in package_data:
+                                        package_data[package_name] = []
+                                    package_data[package_name].append(
+                                        file_name)
 
 
 def _is_hpu() -> bool:

Copy link
Collaborator

@LucasWilkinson LucasWilkinson left a comment

Choose a reason for hiding this comment

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

Can you please add a description of the bug to the PR comment

@@ -280,6 +280,14 @@ def run(self):
print(f"Copying {file} to {dst_file}")
self.copy_file(file, dst_file)

# copy these folders to use the vllm_flash_attn rotary_kernel.
for folder in ("layers", "ops"):
Copy link
Collaborator

@LucasWilkinson LucasWilkinson Apr 27, 2025

Choose a reason for hiding this comment

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

this seems unnecessary, why not just do:

        files = glob.glob(
            os.path.join(self.build_lib, "vllm", "vllm_flash_attn", "**",  "*.py"))

on line 275-276 instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

this will only grab all of the python files, then we still need to check if the folder exists or not, then copy over here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Would this conflict with #17260?

@@ -400,6 +408,31 @@ def run(self) -> None:

package_data[package_name].append(file_name)

# Extract and include the layers and ops of rotary embedding.
folders_to_copy = {"layers", "ops"}
for folder in folders_to_copy:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This also seems unnecessarily messing / complicated, I think we can just use glob.translate (https://docs.python.org/3/library/glob.html#glob.translate) and grab the file list like use the same glob pattern mentioned above

Copy link
Collaborator

Choose a reason for hiding this comment

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

i agree, overlook on my end.

@jeejeelee
Copy link
Collaborator Author

Thanks. LGTM

Can you also add a Co-authored-by: Aaron Pham <[email protected]> to the description.

Done

@jeejeelee
Copy link
Collaborator Author

Ah we need to gated the copy ovee in _is_cuda() only here.

27fdbeea7 - chore: only gated in CUDA (HEAD -> fix-flash-att-rotray)

Signed-off-by: Aaron Pham <[email protected]>
(G) Aaron Pham <[email protected]> (Sun Apr 27 14:57:44 2025 +0000)


diff --git a/setup.py b/setup.py
index 34be6a641..8e18c8215 100755
--- a/setup.py
+++ b/setup.py
@@ -280,13 +280,14 @@ class cmake_build_ext(build_ext):
             print(f"Copying {file} to {dst_file}")
             self.copy_file(file, dst_file)
 
-        # copy these folders to use the vllm_flash_attn rotary_kernel.
-        for folder in ("layers", "ops"):
-            src = os.path.join(self.build_lib, "vllm", "vllm_flash_attn",
-                               folder)
-            out = os.path.join("vllm", "vllm_flash_attn", folder)
-            print(f"Copying {folder} from vllm/vllm_flash_attn")
-            self.copy_tree(src, out)
+        if _is_cuda():
+            # copy these folders to use the vllm_flash_attn rotary_kernel.
+            for folder in ("layers", "ops"):
+                src = os.path.join(self.build_lib, "vllm", "vllm_flash_attn",
+                                   folder)
+                out = os.path.join("vllm", "vllm_flash_attn", folder)
+                print(f"Copying {folder} from vllm/vllm_flash_attn")
+                self.copy_tree(src, out)
 
 
 class repackage_wheel(build_ext):
@@ -409,29 +410,34 @@ class repackage_wheel(build_ext):
                 package_data[package_name].append(file_name)
 
             # Extract and include the layers and ops of rotary embedding.
-            folders_to_copy = {"layers", "ops"}
-            for folder in folders_to_copy:
-                folder_path = f"vllm/vllm_flash_attn/{folder}"
-                folder_files = [
-                    f for f in wheel.filelist
-                    if f.filename.startswith(folder_path)
-                ]
-
-                if folder_files:
-                    print(f"Include {folder} folder from vllm/vllm_flash_attn")
-                    for file in folder_files:
-                        wheel.extract(file)
-
-                        # Add the file to package_data if it's not a Python file
-                        rel_path = file.filename.split("/")
-                        # vllm/vllm_flash_attn/folder/file
-                        if len(rel_path) >= 4:
-                            package_name = "vllm.vllm_flash_attn." + folder
-                            file_name = rel_path[-1]
-                            if not file_name.endswith(".py"):
-                                if package_name not in package_data:
-                                    package_data[package_name] = []
-                                package_data[package_name].append(file_name)
+            if _is_cuda():
+                folders_to_copy = {"layers", "ops"}
+                for folder in folders_to_copy:
+                    folder_path = f"vllm/vllm_flash_attn/{folder}"
+                    folder_files = [
+                        f for f in wheel.filelist
+                        if f.filename.startswith(folder_path)
+                    ]
+
+                    if folder_files:
+                        print(
+                            f"Include {folder} folder from vllm/vllm_flash_attn"
+                        )
+                        for file in folder_files:
+                            wheel.extract(file)
+
+                            # Add the file to package_data if
+                            # it's not a Python file
+                            rel_path = file.filename.split("/")
+                            # vllm/vllm_flash_attn/folder/file
+                            if len(rel_path) >= 4:
+                                package_name = "vllm.vllm_flash_attn." + folder
+                                file_name = rel_path[-1]
+                                if not file_name.endswith(".py"):
+                                    if package_name not in package_data:
+                                        package_data[package_name] = []
+                                    package_data[package_name].append(
+                                        file_name)
 
 
 def _is_hpu() -> bool:

IIUC, no need to add gate, these changes are just a supplement to vllm_flash_attn copy

@aarnphm
Copy link
Collaborator

aarnphm commented Apr 27, 2025

This is superceded by #17267

@aarnphm aarnphm closed this Apr 27, 2025
@jeejeelee jeejeelee deleted the fix-flash-att-rotray branch April 28, 2025 00:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci/build ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: nightly version: ModuleNotFoundError: No module named 'vllm.vllm_flash_attn.layers'
4 participants