Skip to content

enable llama3.1-8B on xpu #9434

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 2 commits into
base: main
Choose a base branch
from

Conversation

huaiyuzh
Copy link

This PR is to enable meta-llama/Llama-3.1-8B-Instruct with datatype BF16 on XPU.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @huaiyuzh, 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 introduces support for the meta-llama/Llama-3.1-8B-Instruct model with BF16 datatype on XPU platforms. The changes primarily involve integrating XPU detection, implementing XPU-specific kernel dispatches for custom operations and activation functions, and updating system-wide checks to ensure compatibility and optimal performance on XPU hardware. This enhancement broadens the hardware compatibility for the specified Llama model, enabling its use on Intel's XPU architecture.

Highlights

  • XPU Device Integration: Introduced detection for XPU devices and integrated XPU-specific dispatching logic for custom operations, allowing the system to leverage XPU hardware for optimized performance.
  • XPU-Optimized Activation Functions: Added XPU-specific implementations for key activation functions like SiLU and GELU, utilizing sgl_kernel for efficient computation on XPU platforms.
  • Expanded sgl-kernel Support: Extended the availability checks for sgl-kernel implementations across activation and layernorm layers to explicitly include XPU, ensuring proper kernel selection and fallback mechanisms.
  • Memory Pool Host Adjustments: Adjusted the memory pool host logic to correctly account for XPU, modifying conditional imports for sgl_kernel.kvcacheio functions to align with XPU compatibility.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 adds support for XPU devices by introducing is_xpu checks and forward_xpu implementations in several layers. The changes are generally well-structured. My review includes a critical fix for a potential bug in SiluAndMul's XPU forward pass, along with several suggestions to improve code maintainability by reducing duplication and enhancing readability.

d = x.shape[-1] // 2
output_shape = x.shape[:-1] + (d,)
out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
out = silu_and_mul(x, out)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The line out = silu_and_mul(x, out) is inconsistent with the forward_cuda implementation and may cause a bug. If silu_and_mul modifies out in-place and returns None (which is a common pattern for such functions for performance), this function will incorrectly return None.

To fix this, you should call silu_and_mul without assigning its return value, just like in forward_cuda.

Suggested change
out = silu_and_mul(x, out)
silu_and_mul(x, out)

Comment on lines 55 to 56
if _is_xpu:
from sgl_kernel import gelu_and_mul, gelu_tanh_and_mul, silu_and_mul
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This block duplicates the imports from the _is_cuda case. For better maintainability, you could consider combining these conditions in the future, for example: if _is_cuda or _is_xpu:.

Comment on lines 116 to 126
def forward_xpu(self, x: torch.Tensor) -> torch.Tensor:
d = x.shape[-1] // 2
output_shape = x.shape[:-1] + (d,)
out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
if self.approximate == "tanh":
gelu_tanh_and_mul(x, out)
elif self.approximate == "none":
gelu_and_mul(x, out)
else:
raise RuntimeError("GeluAndMul only support tanh or none")
return out
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The implementation of forward_xpu is identical to forward_cuda. To improve maintainability and reduce code duplication, consider creating a shared helper method that both forward_xpu and forward_cuda can call.

For example, you could create a _forward_impl method and have both forward_cuda and forward_xpu delegate to it. This would make the code cleaner and easier to maintain in the future.


_is_npu = is_npu()
if not _is_npu:
_is_xpu = is_xpu()
if (not _is_npu and not _is_xpu):
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The condition (not _is_npu and not _is_xpu) can be simplified using De Morgan's laws to not (_is_npu or _is_xpu) for better readability. The outer parentheses are also redundant.

Suggested change
if (not _is_npu and not _is_xpu):
if not (_is_npu or _is_xpu):

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