Skip to content
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

pt: add 4 tabulate_fusion op #3877

Merged
merged 35 commits into from
Jun 21, 2024
Merged

Conversation

cherryWangY
Copy link
Collaborator

@cherryWangY cherryWangY commented Jun 13, 2024

OP: tabulate_multi_device.cc -> tabulate_fusion_se_a & atten & r & t
Compile: add compile file
Test: source/tests, se_t has no test, and all other ops pass the forward and backward tests.

Summary by CodeRabbit

  • New Features

    • Introduced support for tabulating fusion operations for deep learning models using PyTorch tensors.
    • Added functions for forward, backward, and gradient computations for various fusion operations, handling different data types and device types.
    • Registered new operations with PyTorch libraries for seamless integration.
  • Chores

    • Updated build configuration to include new source files for fusion operations.

Copy link
Contributor

coderabbitai bot commented Jun 13, 2024

Walkthrough

Walkthrough

This update introduces a new file, tabulate_multi_device.cc, which adds functionality to perform and register fusion operations for deep learning models using PyTorch tensors. This involves defining new classes and functions for forward, backward, and gradient computations across multiple devices, and integrating these operations into the existing build system via updates to CMakeLists.txt.

Changes

File Changes Summary
source/op/pt/CMakeLists.txt Added tabulate_multi_device.cc to the OP_SRC variable.
source/op/pt/tabulate_multi_device.cc Introduced functions and classes for forward, backward, and gradient computations for fusion operations on multiple devices and registered new operations with PyTorch libraries.

Sequence Diagram(s)

sequenceDiagram
  participant Developer as Developer
  participant BuildSys as Build System
  participant PyTorch as PyTorch Library
  participant Model as Deep Learning Model

  Developer->>BuildSys: Modify CMakeLists.txt
  BuildSys->>tabulate_multi_device.cc: Add to OP_SRC
  Note over tabulate_multi_device.cc: New functions and classes added for operations
  
  Model->>tabulate_multi_device.cc: Request fusion operation A (forward)
  tabulate_multi_device.cc->>PyTorch: Register TabulateFusionSeAOp
  PyTorch->>Model: Perform fusion operation A
  
  Model->>tabulate_multi_device.cc: Request gradient of fusion operation A (forward)
  tabulate_multi_device.cc->>PyTorch: Register TabulateFusionSeAGradOp
  PyTorch->>Model: Compute gradient of fusion operation A
  
  Model->>tabulate_multi_device.cc: Request gradient of gradient of fusion operation A (forward)
  tabulate_multi_device.cc->>PyTorch: Register TabulateFusionSeAGradGradOp
  PyTorch->>Model: Perform gradient of gradient computation
  
  Model->>tabulate_multi_device.cc: Request fusion operation T (forward)
  tabulate_multi_device.cc->>PyTorch: Register TabulateFusionSeTOp
  PyTorch->>Model: Perform fusion operation T
  
  Model->>tabulate_multi_device.cc: Request fusion operation R (forward)
  tabulate_multi_device.cc->>PyTorch: Register TabulateFusionSeROp
  PyTorch->>Model: Perform fusion operation R
  
  Model->>tabulate_multi_device.cc: Request attention-based fusion operation (forward)
  tabulate_multi_device.cc->>PyTorch: Register TabulateFusionSeAttenOp
  PyTorch->>Model: Perform attention-based fusion operation
Loading

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9920e57 and 1523196.

Files selected for processing (2)
  • source/op/pt/CMakeLists.txt (1 hunks)
  • source/tests/pt/test_tabulate_fusion_se_t.py (1 hunks)
Files not summarized due to errors (1)
  • source/tests/pt/test_tabulate_fusion_se_t.py: Error: Message exceeds token limit
Files skipped from review as they are similar to previous changes (1)
  • source/op/pt/CMakeLists.txt
Additional comments not posted (6)
source/tests/pt/test_tabulate_fusion_se_t.py (6)

1681-1702: Ensure correctness of forward function testing.

The forward function test checks the shape and value closeness of the result tensor. Confirm that the assertions are robust enough to catch potential discrepancies in the operation's output.


1704-1749: Review backward function testing for completeness.

The backward function test checks the gradients' shapes and values. Ensure that these tests are comprehensive and cover all scenarios that might affect gradient computation.


436-1507: Validate tensor reshaping and gradient settings.

The tensors are reshaped and their gradient settings are explicitly defined. Confirm that the reshaping dimensions and gradient requirements are as expected for the operation being tested.

Verification successful

The gradient settings for the tensors in the provided code snippet are consistent with other tests in the repository. Specifically, the requires_grad attribute is set correctly for the tensors used in the tests.

  • self.table_info_tensor.requires_grad = False
  • self.table_tensor.requires_grad = False
  • self.em_x_tensor.requires_grad = True

These settings align with the expected behavior for gradient computations in the tests.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify tensor reshaping and gradient settings.

# Test: Check for gradient settings consistency in other tests.
rg --type python 'requires_grad =' 'source/tests'

Length of output: 80



Script:

#!/bin/bash
# Description: Verify tensor reshaping and gradient settings.

# Test: Check for gradient settings consistency in other tests.
rg --type py 'requires_grad =' source/tests

Length of output: 1550


23-27: Check precision setting for floating point comparisons.

The precision settings are different for float64 and float32 types. Ensure that these precision levels are appropriate and consistent with other tests in the project.


18-19: Check conditional test skipping logic.

The test is skipped if ENABLE_CUSTOMIZED_OP is false. Ensure that this is the intended behavior, especially if this operation is crucial and should be tested regardless of the configuration.

Verification successful

The ENABLE_CUSTOMIZED_OP conditional test skipping logic is consistently used across multiple test files, indicating that it is an intended behavior.

  • source/tests/pt/test_tabulate_fusion_se_t.py
  • source/tests/pt/test_tabulate_fusion_se_r.py
  • source/tests/pt/test_tabulate_fusion_se_a.py
  • source/tests/pt/test_tabulate_fusion_se_atten.py

This confirms that the approach to skip tests when ENABLE_CUSTOMIZED_OP is not enabled is consistent and likely intentional.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the condition for skipping tests.

# Test: Search for `ENABLE_CUSTOMIZED_OP` usage in other test files. Expect: Consistent usage.
rg --type python 'ENABLE_CUSTOMIZED_OP'

Length of output: 70



Script:

#!/bin/bash
# Description: Verify the condition for skipping tests.

# Test: Search for `ENABLE_CUSTOMIZED_OP` usage in other test files. Expect: Consistent usage.
rg --type py 'ENABLE_CUSTOMIZED_OP'

Length of output: 1197


28-424: Check tensor initialization and reshape operations.

The tensor is reshaped to (8, 174), resulting in a total of 1392 elements. Ensure that the tensor dimensions and the total number of elements are correctly calculated and aligned with the expected use in tests.

Verification successful

The search results show numerous instances of tensor reshaping logic across various test files, including the file in question (source/tests/pt/test_tabulate_fusion_se_t.py). The specific line of interest, where the tensor is reshaped to (8, 174), is explicitly found in the search results:

source/tests/pt/test_tabulate_fusion_se_t.py:        ).reshape(8, 174)  # 1392

Given this, we can verify that the tensor reshaping logic is consistent and the dimensions align with the expected number of elements (8 * 174 = 1392). There is no evidence of a mismatch or inconsistency in the tensor dimensions or the total number of elements.

  • File: source/tests/pt/test_tabulate_fusion_se_t.py
  • Line: 424
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify tensor reshaping logic.

# Test: Check for similar tensor reshaping logic in other tests or in the operation implementation.
rg --type python 'reshape' 'source/tests'

Length of output: 70



Script:

#!/bin/bash
# Description: Verify tensor reshaping logic.

# Test: Check for similar tensor reshaping logic in other tests or in the operation implementation.
rg 'reshape' --type py source/tests

Length of output: 117785


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Jun 13, 2024

Codecov Report

Attention: Patch coverage is 84.42029% with 43 lines in your changes missing coverage. Please review.

Project coverage is 82.72%. Comparing base (f9d5f56) to head (7dd7f6a).

Current head 7dd7f6a differs from pull request most recent head 1523196

Please upload reports for the commit 1523196 to get more accurate results.

Files Patch % Lines
source/op/pt/tabulate_multi_device.cc 84.42% 16 Missing and 27 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #3877      +/-   ##
==========================================
- Coverage   82.74%   82.72%   -0.02%     
==========================================
  Files         518      518              
  Lines       50215    50414     +199     
  Branches     2982     3018      +36     
==========================================
+ Hits        41548    41705     +157     
- Misses       7757     7773      +16     
- Partials      910      936      +26     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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: 7

source/tests/pt/test_tabulate_fusion_se_a.py Outdated Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_r.py Outdated Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Outdated Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Outdated Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Fixed Show fixed Hide fixed
source/op/pt/tabulate_multi_device.cc Fixed Show fixed Hide fixed
source/op/pt/tabulate_multi_device.cc Fixed Show fixed Hide fixed
source/op/pt/tabulate_multi_device.cc Fixed Show fixed Hide fixed
source/op/pt/tabulate_multi_device.cc Fixed Show fixed Hide fixed
source/op/pt/tabulate_multi_device.cc Fixed Show fixed Hide fixed
source/op/pt/tabulate_multi_device.cc Fixed Show fixed Hide fixed
source/op/pt/tabulate_multi_device.cc Fixed Show fixed Hide fixed
source/op/pt/tabulate_multi_device.cc Fixed Show fixed Hide fixed
source/op/pt/tabulate_multi_device.cc Fixed Show resolved Hide resolved
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

source/tests/pt/test_tabulate_fusion_se_atten.py Outdated Show resolved Hide resolved
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: 3

source/tests/pt/test_tabulate_fusion_se_r.py Outdated Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_r.py Outdated Show resolved Hide resolved
TORCH_LIBRARY_FRAGMENT(deepmd, m) {
m.def("tabulate_fusion_se_t", tabulate_fusion_se_t);
}
TORCH_LIBRARY_FRAGMENT(deepmd, m) {

Check notice

Code scanning / CodeQL

Unused static function Note

Static function TORCH_LIBRARY_FRAGMENT_init_deepmd_5 is unreachable (
TORCH_LIBRARY_FRAGMENT_static_init_deepmd_5
must be removed at the same time)
TORCH_LIBRARY_FRAGMENT(deepmd, m) {
m.def("tabulate_fusion_se_atten", tabulate_fusion_se_atten);
}
TORCH_LIBRARY_FRAGMENT(deepmd, m) {

Check notice

Code scanning / CodeQL

Unused static function Note

Static function TORCH_LIBRARY_FRAGMENT_init_deepmd_4 is unreachable (
TORCH_LIBRARY_FRAGMENT_static_init_deepmd_4
must be removed at the same time)
TORCH_LIBRARY_FRAGMENT(deepmd, m) {
m.def("tabulate_fusion_se_a", tabulate_fusion_se_a);
}
TORCH_LIBRARY_FRAGMENT(deepmd, m) {

Check notice

Code scanning / CodeQL

Unused static function Note

Static function TORCH_LIBRARY_FRAGMENT_init_deepmd_3 is unreachable (
TORCH_LIBRARY_FRAGMENT_static_init_deepmd_3
must be removed at the same time)
last_layer_size);
}

TORCH_LIBRARY_FRAGMENT(deepmd, m) {

Check notice

Code scanning / CodeQL

Unused static function Note

Static function TORCH_LIBRARY_FRAGMENT_init_deepmd_2 is unreachable (
TORCH_LIBRARY_FRAGMENT_static_init_deepmd_2
must be removed at the same time)
source/tests/pt/test_tabulate_fusion_se_a.py Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_a.py Outdated Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Fixed Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
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: 10

Outside diff range and nitpick comments (1)
source/op/pt/tabulate_multi_device.cc (1)

440-476: Consider adding detailed logging for debugging purposes in TabulateFusionSeRGradGradForward, which could help trace computation values step-by-step.

source/tests/pt/test_tabulate_fusion_se_a.py Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_r.py Outdated Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_r.py Outdated Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Outdated Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
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: 5

Outside diff range and nitpick comments (3)
source/tests/pt/test_tabulate_fusion_se_r.py (2)

1-1: Add a module docstring to describe the purpose and functionality of this test module.


11-1100: Ensure proper documentation for the test class and its methods to improve maintainability and understandability.

source/tests/pt/test_tabulate_fusion_se_a.py (1)

1072-1073: Consider adding comments describing the purpose and contents of these tensors to improve code readability and maintainability.

Also applies to: 1076-1076, 1079-1098, 1099-1167

source/tests/pt/test_tabulate_fusion_se_atten.py Outdated Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_r.py Outdated Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_a.py Outdated Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_a.py Outdated Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_a.py Show resolved Hide resolved
@cherryWangY cherryWangY requested a review from njzjz June 17, 2024 06:18
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: 9

Outside diff range and nitpick comments (3)
source/op/pt/tabulate_multi_device.cc (3)

21-71: Ensure that the input validation messages in the TabulateFusionSeAForward function are clear and precise to help users understand the expected input dimensions.


73-121: In TabulateFusionSeAGradForward, consider adding detailed logging or comments explaining the gradient computation logic for better maintainability.


182-222: The function TabulateFusionSeTForward performs tensor computations but lacks inline comments explaining the steps. Adding comments would enhance understandability.

source/tests/pt/test_tabulate_fusion_se_r.py Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
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: 12

Outside diff range and nitpick comments (2)
source/tests/pt/test_tabulate_fusion_se_a.py (2)

1413-1413: Consider adding a docstring to the test_forward method to describe its purpose and the specific conditions it tests.
Adding a docstring would improve the maintainability of the test suite by making it easier for other developers to understand the purpose and functionality of each test case.


1436-1436: Consider adding a docstring to the test_backward method to describe its purpose and the specific conditions it tests.
Adding a docstring would enhance the readability and maintainability of the test suite, providing clarity on what each test case is designed to verify.

source/tests/pt/test_tabulate_fusion_se_atten.py Outdated Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_r.py Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_r.py Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_r.py Outdated Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_a.py Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
source/op/pt/tabulate_multi_device.cc Show resolved Hide resolved
@njzjz
Copy link
Member

njzjz commented Jun 17, 2024

@coderabbitai resolve

njzjz added 2 commits June 17, 2024 17:37
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
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: 3

source/tests/pt/test_tabulate_fusion_se_a.py Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_a.py Show resolved Hide resolved
source/tests/pt/test_tabulate_fusion_se_a.py Show resolved Hide resolved
@njzjz njzjz requested a review from wanghan-iapcm June 21, 2024 06:34
@wanghan-iapcm wanghan-iapcm enabled auto-merge June 21, 2024 08:18
@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue Jun 21, 2024
Merged via the queue into deepmodeling:devel with commit 54aff95 Jun 21, 2024
51 checks passed
mtaillefumier pushed a commit to mtaillefumier/deepmd-kit that referenced this pull request Sep 18, 2024
**OP**: tabulate_multi_device.cc -> tabulate_fusion_se_a & atten & r & t
**Compile**: add compile file
**Test**: source/tests, se_t has no test, and all other ops pass the
forward and backward tests.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced support for tabulating fusion operations for deep learning
models using PyTorch tensors.
- Added functions for forward, backward, and gradient computations for
various fusion operations, handling different data types and device
types.
- Registered new operations with PyTorch libraries for seamless
integration.

- **Chores**
- Updated build configuration to include new source files for fusion
operations.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants