Skip to content

pd: support different label_dict in CINN #4795

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

Merged
merged 5 commits into from
Jun 19, 2025

Conversation

HydrogenSulfate
Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate commented Jun 10, 2025

pop unnecessary item when wrapping model with jit.to_static, so we can support se_e2_a/dpa2/dpa3 without extra modification.

@njzjz can you give some suggestions for better code improvements? The current approach of fetching data via self.get_data isn't very concise.

Summary by CodeRabbit

  • Bug Fixes
    • Improved compatibility by dynamically matching label input specifications to available label keys during model compilation when CINN is enabled. This prevents errors caused by mismatched label keys at runtime.

@Copilot Copilot AI review requested due to automatic review settings June 10, 2025 13:38
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the CINN jit.to_static wrapper by dynamically constructing the input_spec based on the keys present in label_dict, thereby removing unnecessary specs when certain labels aren’t used.

  • Fetches a sample label_dict via self.get_data to drive spec construction
  • Defines a full label_dict_spec and pops entries not present in label_dict
  • Applies the filtered label_dict_spec to jit.to_static

Copy link
Contributor

coderabbitai bot commented Jun 10, 2025

📝 Walkthrough

Walkthrough

The update modifies the way the input specification for model compilation is constructed in the training module. Instead of using a hardcoded label dictionary, the code now dynamically builds the input specification based on the actual keys present in sampled training data label dictionaries, ensuring alignment with runtime label structures.

Changes

File(s) Change Summary
deepmd/pd/train/training.py Replaces hardcoded label input specification with a dynamically constructed dictionary reflecting actual training data label keys for CINN-enabled jit.to_static compilation.

Sequence Diagram(s)

Loading
sequenceDiagram
    participant Trainer
    participant DataSource
    participant ModelCompiler

    Trainer->>DataSource: get_data(is_train=True, task_key="Default" or first model key)
    DataSource-->>Trainer: sample label_dict
    Trainer->>Trainer: build label_dict_spec based on sample keys
    Trainer->>ModelCompiler: compile model with input_spec (using label_dict_spec)

Possibly related PRs

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Pylint (3.3.7)
deepmd/pd/train/training.py

No files to lint: exiting.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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
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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c46dc7d and 8118231.

📒 Files selected for processing (1)
  • deepmd/pd/train/training.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (28)
  • GitHub Check: Build C library (2.18, libdeepmd_c.tar.gz)
  • GitHub Check: Build C library (2.14, >=2.5.0rc0,<2.15, libdeepmd_c_cu11.tar.gz)
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Test Python (1, 3.9)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Build C++ (clang, clang)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Analyze (python)
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build C++ (rocm, rocm)
  • GitHub Check: Build C++ (cpu, cpu)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Test C++ (false)
  • GitHub Check: Test C++ (true)
🔇 Additional comments (1)
deepmd/pd/train/training.py (1)

647-647: LGTM! Correct usage of the dynamically constructed specification.

The usage of label_dict_spec in the input_spec list correctly replaces the previously hardcoded label dictionary specification, ensuring alignment between static compilation and runtime data structures.

Copy link

codecov bot commented Jun 10, 2025

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.

Project coverage is 84.78%. Comparing base (8368b26) to head (3b09f93).
Report is 5 commits behind head on devel.

Files with missing lines Patch % Lines
deepmd/pd/train/training.py 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #4795      +/-   ##
==========================================
- Coverage   84.79%   84.78%   -0.01%     
==========================================
  Files         698      698              
  Lines       67816    67820       +4     
  Branches     3540     3540              
==========================================
- Hits        57505    57504       -1     
- Misses       9177     9181       +4     
- Partials     1134     1135       +1     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@njzjz
Copy link
Member

njzjz commented Jun 11, 2025

@njzjz can you give some suggestions for better code improvements? The current approach of fetching data via self.get_data isn't very concise.

Maybe @iProzd or @caic99 is more familiar with the get_data method.

@njzjz njzjz added this to the v3.1.1 milestone Jun 11, 2025
@njzjz njzjz requested review from caic99 and iProzd June 12, 2025 16:57
Copy link
Member

@caic99 caic99 left a comment

Choose a reason for hiding this comment

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

LGTM. @HydrogenSulfate The suggested changes by AI might help.

HydrogenSulfate and others added 2 commits June 13, 2025 11:18
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: HydrogenSulfate <490868991@qq.com>
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

🧹 Nitpick comments (1)
deepmd/pd/train/training.py (1)

630-632: Use membership on the dict directly & drop the redundant .keys()

dict.keys() materialises a view; membership works directly on the dict and is clearer:

-label_dict_spec = {
-    k: spec_templates[k] for k in label_dict.keys() if k in spec_templates
-}
+label_dict_spec = {k: spec_templates[k] for k in label_dict if k in spec_templates}

Minor, but it avoids the SIM118 lint warning and trims a few cycles.

🧰 Tools
🪛 Ruff (0.11.9)

631-631: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8118231 and 6528a85.

📒 Files selected for processing (1)
  • deepmd/pd/train/training.py (2 hunks)
🧰 Additional context used
🪛 Ruff (0.11.9)
deepmd/pd/train/training.py

631-631: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

⏰ Context from checks skipped due to timeout of 90000ms (23)
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Test Python (1, 3.9)
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Test C++ (false)
  • GitHub Check: Build C library (2.18, libdeepmd_c.tar.gz)
  • GitHub Check: Build C library (2.14, >=2.5.0rc0,<2.15, libdeepmd_c_cu11.tar.gz)
  • GitHub Check: Analyze (c-cpp)

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

♻️ Duplicate comments (1)
deepmd/pd/train/training.py (1)

610-613: ⚠️ Potential issue

Avoid consuming a real training batch when inferring input_spec

Calling self.get_data(is_train=True) here advances the primary training iterator and permanently drops one batch before training even starts.
This silently skews epoch statistics and is especially painful when the dataset is small.

A side-effect-free “peek” is safer:

-            # NOTE: This is a trick to decide the right input_spec for wrapper.forward
-            _, label_dict, _ = self.get_data(is_train=True)
+            # NOTE: do **not** advance the real iterator; just peek one sample
+            loader = self.training_dataloader
+            if self.multi_task:
+                # fall back to the first branch – adjust if you need something smarter
+                loader = self.training_dataloader[self.model_keys[0]]
+            raw_sample = next(iter(loader))
+            _, label_dict, _ = self._split_inputs_labels(raw_sample)  # reuse existing helper

(The helper _split_inputs_labels/similar already exists further down in the file; reuse it to keep the parsing logic in one place.)

🧹 Nitpick comments (1)
deepmd/pd/train/training.py (1)

628-630: Drop the redundant .keys() call

label_dict is already iterable; using .keys() is unnecessary and flagged by Ruff (SIM118).

-            label_dict_spec = {
-                k: spec_templates[k] for k in label_dict.keys() if k in spec_templates
-            }
+            label_dict_spec = {
+                k: spec_templates[k] for k in label_dict if k in spec_templates
+            }
🧰 Tools
🪛 Ruff (0.11.9)

629-629: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6528a85 and 3b09f93.

📒 Files selected for processing (1)
  • deepmd/pd/train/training.py (2 hunks)
🧰 Additional context used
🪛 Ruff (0.11.9)
deepmd/pd/train/training.py

629-629: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

⏰ Context from checks skipped due to timeout of 90000ms (25)
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build C library (2.14, >=2.5.0rc0,<2.15, libdeepmd_c_cu11.tar.gz)
  • GitHub Check: Build C library (2.18, libdeepmd_c.tar.gz)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Test Python (1, 3.9)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Test C++ (false)

@iProzd iProzd enabled auto-merge June 18, 2025 06:53
@njzjz
Copy link
Member

njzjz commented Jun 19, 2025

@coderabbitai resolve

Copy link
Contributor

coderabbitai bot commented Jun 19, 2025

✅ Actions performed

Comments resolved.

@iProzd iProzd added this pull request to the merge queue Jun 19, 2025
Merged via the queue into deepmodeling:devel with commit 617d3e2 Jun 19, 2025
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants