Skip to content

Conversation

Blury233
Copy link
Contributor

PR Category

Operator

Type of Change

New Feature

Description

This PR adds:

  • bitwise_shift_left
  • bitwise_shift_right

Issue

Progress

  • Change is properly reviewed (1 reviewer required, 2 recommended).
  • Change is responded to an issue.
  • Change is fully covered by a UT.

Performance

Accuracy Test
acc
Performance Test
perf1
perf2

Copy link
Contributor

Summary of Changes

Hello @Blury233, 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 enhances the flag_gems library by adding two new bitwise shift operators: bitwise_shift_left and bitwise_shift_right. These operators are designed for high performance through Triton kernel implementations and are thoroughly validated with new accuracy and performance tests, ensuring their correctness and efficiency across different scenarios.

Highlights

  • New Bitwise Operators: Introduced bitwise_shift_left and bitwise_shift_right operators to the flag_gems library, expanding its functional capabilities.
  • Triton Kernel Implementation: Both new bitwise shift operators are implemented using Triton kernels, leveraging pointwise_dynamic for efficient execution.
  • Comprehensive Testing: Added extensive accuracy tests for both bitwise_left_shift and bitwise_right_shift, covering various input shapes, integer data types, and their inplace versions. Performance benchmarks were also included.
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.

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 introduces bitwise_shift_left and bitwise_shift_right operators, complete with Triton kernels, registration, and both accuracy and performance tests. The implementation leverages pointwise_dynamic for handling broadcasting, which is a good approach. The overall structure is clean and follows the existing patterns in the codebase. My review comments focus on improving the comprehensiveness of the tests by using a wider, more realistic range of shift values, and on maintaining consistency in logging across the new operator files.

Comment on lines +205 to +207
shift_amount = torch.randint(
0, 8, shape, dtype=cur_dtype, device=self.device
)
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 range of shift amounts [0, 8) is a bit narrow for integer types larger than 8 bits. To ensure the performance benchmark covers a more realistic range of inputs, consider using a range for the shift amount that is dependent on the cur_dtype, up to the number of bits in the type.

Suggested change
shift_amount = torch.randint(
0, 8, shape, dtype=cur_dtype, device=self.device
)
shift_amount = torch.randint(
0, torch.iinfo(cur_dtype).bits, shape, dtype=cur_dtype, device=self.device
)

Comment on lines +16 to +17
def bitwise_right_shift(self, other, *, out=None):
return bitwise_right_shift_kernel(self, other, out=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

For consistency with bitwise_left_shift and to aid in debugging, it would be good to add a debug log message here.

Suggested change
def bitwise_right_shift(self, other, *, out=None):
return bitwise_right_shift_kernel(self, other, out=out)
def bitwise_right_shift(self, other, *, out=None):
logger.debug("GEMS BITWISE_RIGHT_SHIFT")
return bitwise_right_shift_kernel(self, other, out=out)

def test_accuracy_bitwise_left_shift(shapes, dtype):
shape_a, shape_b = shapes
res_a = torch.randint(0, 100, shape_a, dtype=dtype, device=flag_gems.device)
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device)
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 range of shift amounts [0, 8) is a bit narrow for integer types larger than 8 bits. To improve test coverage, consider using a range that is dependent on the dtype, up to the number of bits in the type. This will ensure that shifts by larger amounts are also tested.

Suggested change
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device)
res_b = torch.randint(0, torch.iinfo(dtype).bits, shape_b, dtype=dtype, device=flag_gems.device)

def test_accuracy_bitwise_right_shift(shapes, dtype):
shape_a, shape_b = shapes
res_a = torch.randint(0, 100, shape_a, dtype=dtype, device=flag_gems.device)
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device)
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 range of shift amounts [0, 8) is a bit narrow for integer types larger than 8 bits. To improve test coverage, consider using a range that is dependent on the dtype, up to the number of bits in the type. This will ensure that shifts by larger amounts are also tested.

Suggested change
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device)
res_b = torch.randint(0, torch.iinfo(dtype).bits, shape_b, dtype=dtype, device=flag_gems.device)

def test_accuracy_bitwise_left_shift_(shapes, dtype):
shape_a, shape_b = shapes
res_a = torch.randint(0, 100, shape_a, dtype=dtype, device=flag_gems.device)
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device)
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 range of shift amounts [0, 8) is a bit narrow for integer types larger than 8 bits. To improve test coverage, consider using a range that is dependent on the dtype, up to the number of bits in the type. This will ensure that shifts by larger amounts are also tested for the in-place operation.

Suggested change
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device)
res_b = torch.randint(0, torch.iinfo(dtype).bits, shape_b, dtype=dtype, device=flag_gems.device)

def test_accuracy_bitwise_right_shift_(shapes, dtype):
shape_a, shape_b = shapes
res_a = torch.randint(0, 100, shape_a, dtype=dtype, device=flag_gems.device)
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device)
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 range of shift amounts [0, 8) is a bit narrow for integer types larger than 8 bits. To improve test coverage, consider using a range that is dependent on the dtype, up to the number of bits in the type. This will ensure that shifts by larger amounts are also tested for the in-place operation.

Suggested change
res_b = torch.randint(0, 8, shape_b, dtype=dtype, device=flag_gems.device)
res_b = torch.randint(0, torch.iinfo(dtype).bits, shape_b, dtype=dtype, device=flag_gems.device)

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