-
Notifications
You must be signed in to change notification settings - Fork 152
[KUNLUNXIN] Speed Up ge/gt/le/lt/ne with TRITONXPU_COMPARE_FUSION and TRITONXPU_FP16_FAST #1032
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
Conversation
Summary of ChangesHello @Ason93, 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 focuses on enhancing the performance of various comparison operations (greater than or equal to, greater than, less than or equal to, less than, and not equal to) within the Kunlunxin backend. The improvement is achieved by programmatically enabling a specific Triton XPU fusion optimization flag ( Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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 aims to speed up comparison operations on Kunlunxin hardware by enabling TRITONXPU_COMPARE_FUSION. The approach of setting an environment variable is sound, but the implementation has several issues. There is a critical bug in ne.py where os.environ is modified inside a Triton kernel, which is not allowed. Additionally, the method used to set and unset the environment variable is not robust; it's not exception-safe and doesn't correctly restore the previous state of the environment variable. This could lead to hard-to-debug issues. The repeated logic also makes the code harder to maintain. My review includes suggestions to fix these problems by using try...finally blocks for safe cleanup and state restoration, and I recommend abstracting this logic into a reusable utility to improve code quality.
| def ne_func_scalar(x, y): | ||
| return x.to(tl.float32) != y.to(tl.float32) | ||
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | ||
| res = x.to(tl.float32) != y.to(tl.float32) | ||
| del os.environ["TRITONXPU_COMPARE_FUSION"] | ||
| return res |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is incorrect. The os module cannot be used inside a Triton JIT-compiled kernel (@triton.jit). This will cause a runtime error. The logic for setting and unsetting the environment variable should be in the host-side Python wrapper function, ne_scalar, not inside the kernel ne_func_scalar.
Please revert the changes in ne_func_scalar. The environment variable logic should be applied in the ne_scalar function, which is currently not modified in this PR. It should be changed to mirror the pattern in other files.
| def ne_func_scalar(x, y): | |
| return x.to(tl.float32) != y.to(tl.float32) | |
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | |
| res = x.to(tl.float32) != y.to(tl.float32) | |
| del os.environ["TRITONXPU_COMPARE_FUSION"] | |
| return res | |
| def ne_func_scalar(x, y): | |
| return x.to(tl.float32) != y.to(tl.float32) |
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | ||
| res = ge_func(A, B) | ||
| del os.environ["TRITONXPU_COMPARE_FUSION"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modifying os.environ directly like this has a few issues:
- Exception Safety: If
ge_func(A, B)raises an exception, thedel os.environ[...]line will not be executed, leaving the environment variable set. This could affect other parts of the program. This should be wrapped in atry...finallyblock. - State Restoration: The code deletes the environment variable, but it might have been set to a different value before this function was called. The correct behavior is to restore its original state.
- Concurrency:
os.environis a global, process-wide dictionary. Modifying it without locks is not thread-safe and can lead to race conditions if these functions are called from multiple threads.
To address these issues and also reduce code duplication across files, I recommend creating a thread-safe context manager. As a direct fix, you can use a try...finally block and store/restore the previous value.
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | |
| res = ge_func(A, B) | |
| del os.environ["TRITONXPU_COMPARE_FUSION"] | |
| key = "TRITONXPU_COMPARE_FUSION" | |
| original_value = os.environ.get(key) | |
| os.environ[key] = "1" | |
| try: | |
| res = ge_func(A, B) | |
| finally: | |
| if original_value is None: | |
| os.environ.pop(key, None) | |
| else: | |
| os.environ[key] = original_value |
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | ||
| res = ge_func_scalar(A, B) | ||
| del os.environ["TRITONXPU_COMPARE_FUSION"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the comment above, this logic should be made robust against exceptions and should restore the original state of the environment variable. Using a try...finally block is recommended to ensure correctness and prevent side effects.
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | |
| res = ge_func_scalar(A, B) | |
| del os.environ["TRITONXPU_COMPARE_FUSION"] | |
| key = "TRITONXPU_COMPARE_FUSION" | |
| original_value = os.environ.get(key) | |
| os.environ[key] = "1" | |
| try: | |
| res = ge_func_scalar(A, B) | |
| finally: | |
| if original_value is None: | |
| os.environ.pop(key, None) | |
| else: | |
| os.environ[key] = original_value |
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | ||
| res = gt_func(A, B) | ||
| del os.environ["TRITONXPU_COMPARE_FUSION"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic for modifying os.environ is not exception-safe and does not properly restore the previous state. Please see my comment in ge.py for a detailed explanation and a suggested fix. The same robust approach should be applied here.
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | |
| res = gt_func(A, B) | |
| del os.environ["TRITONXPU_COMPARE_FUSION"] | |
| key = "TRITONXPU_COMPARE_FUSION" | |
| original_value = os.environ.get(key) | |
| os.environ[key] = "1" | |
| try: | |
| res = gt_func(A, B) | |
| finally: | |
| if original_value is None: | |
| os.environ.pop(key, None) | |
| else: | |
| os.environ[key] = original_value |
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | ||
| res = gt_func_scalar(A, B) | ||
| del os.environ["TRITONXPU_COMPARE_FUSION"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic for modifying os.environ is not exception-safe and does not properly restore the previous state. The same robust approach suggested for other functions in this PR should be applied here.
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | |
| res = gt_func_scalar(A, B) | |
| del os.environ["TRITONXPU_COMPARE_FUSION"] | |
| key = "TRITONXPU_COMPARE_FUSION" | |
| original_value = os.environ.get(key) | |
| os.environ[key] = "1" | |
| try: | |
| res = gt_func_scalar(A, B) | |
| finally: | |
| if original_value is None: | |
| os.environ.pop(key, None) | |
| else: | |
| os.environ[key] = original_value |
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | ||
| res = le_func(A, B) | ||
| del os.environ["TRITONXPU_COMPARE_FUSION"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic for modifying os.environ is not exception-safe and does not properly restore the previous state. Please see my comment in ge.py for a detailed explanation and a suggested fix. The same robust approach should be applied here.
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | |
| res = le_func(A, B) | |
| del os.environ["TRITONXPU_COMPARE_FUSION"] | |
| key = "TRITONXPU_COMPARE_FUSION" | |
| original_value = os.environ.get(key) | |
| os.environ[key] = "1" | |
| try: | |
| res = le_func(A, B) | |
| finally: | |
| if original_value is None: | |
| os.environ.pop(key, None) | |
| else: | |
| os.environ[key] = original_value |
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | ||
| res = le_func_scalar(A, B) | ||
| del os.environ["TRITONXPU_COMPARE_FUSION"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic for modifying os.environ is not exception-safe and does not properly restore the previous state. The same robust approach suggested for other functions in this PR should be applied here.
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | |
| res = le_func_scalar(A, B) | |
| del os.environ["TRITONXPU_COMPARE_FUSION"] | |
| key = "TRITONXPU_COMPARE_FUSION" | |
| original_value = os.environ.get(key) | |
| os.environ[key] = "1" | |
| try: | |
| res = le_func_scalar(A, B) | |
| finally: | |
| if original_value is None: | |
| os.environ.pop(key, None) | |
| else: | |
| os.environ[key] = original_value |
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | ||
| res = lt_func(A, B) | ||
| del os.environ["TRITONXPU_COMPARE_FUSION"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic for modifying os.environ is not exception-safe and does not properly restore the previous state. Please see my comment in ge.py for a detailed explanation and a suggested fix. The same robust approach should be applied here.
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | |
| res = lt_func(A, B) | |
| del os.environ["TRITONXPU_COMPARE_FUSION"] | |
| key = "TRITONXPU_COMPARE_FUSION" | |
| original_value = os.environ.get(key) | |
| os.environ[key] = "1" | |
| try: | |
| res = lt_func(A, B) | |
| finally: | |
| if original_value is None: | |
| os.environ.pop(key, None) | |
| else: | |
| os.environ[key] = original_value |
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | ||
| res = lt_func_scalar(A, B) | ||
| del os.environ["TRITONXPU_COMPARE_FUSION"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic for modifying os.environ is not exception-safe and does not properly restore the previous state. The same robust approach suggested for other functions in this PR should be applied here.
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | |
| res = lt_func_scalar(A, B) | |
| del os.environ["TRITONXPU_COMPARE_FUSION"] | |
| key = "TRITONXPU_COMPARE_FUSION" | |
| original_value = os.environ.get(key) | |
| os.environ[key] = "1" | |
| try: | |
| res = lt_func_scalar(A, B) | |
| finally: | |
| if original_value is None: | |
| os.environ.pop(key, None) | |
| else: | |
| os.environ[key] = original_value |
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | ||
| res = ne_func(A, B) | ||
| del os.environ["TRITONXPU_COMPARE_FUSION"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic for modifying os.environ is not exception-safe and does not properly restore the previous state. Please see my comment in ge.py for a detailed explanation and a suggested fix. The same robust approach should be applied here.
| os.environ["TRITONXPU_COMPARE_FUSION"] = "1" | |
| res = ne_func(A, B) | |
| del os.environ["TRITONXPU_COMPARE_FUSION"] | |
| key = "TRITONXPU_COMPARE_FUSION" | |
| original_value = os.environ.get(key) | |
| os.environ[key] = "1" | |
| try: | |
| res = ne_func(A, B) | |
| finally: | |
| if original_value is None: | |
| os.environ.pop(key, None) | |
| else: | |
| os.environ[key] = original_value |
PR Category
Type of Change
Description
Issue
Progress
Performance