Skip to content

refactor: extract shared tabulate math, remove pt_expt→pt dependency#5339

Open
wanghan-iapcm wants to merge 11 commits intodeepmodeling:masterfrom
wanghan-iapcm:refact-tabulate
Open

refactor: extract shared tabulate math, remove pt_expt→pt dependency#5339
wanghan-iapcm wants to merge 11 commits intodeepmodeling:masterfrom
wanghan-iapcm:refact-tabulate

Conversation

@wanghan-iapcm
Copy link
Collaborator

@wanghan-iapcm wanghan-iapcm commented Mar 25, 2026

Summary

  • Extract tabulation math (activation derivatives, chain-rule propagation, embedding-net forward pass) from deepmd/pt/utils/tabulate.py into a shared numpy module deepmd/utils/tabulate_math.py
  • Both pt and pt_expt backends now inherit from the shared DPTabulate class
  • pt_expt no longer imports from deepmd.pt for tabulation or compression
  • Remove is_pt flag from BaseTabulate — normalize lower/upper bounds to per-type scalars, move _convert_numpy_float_to_int into subclass _convert_numpy_to_tensor

Changes

File Lines What
deepmd/utils/tabulate_math.py +582 New: numpy DPTabulate with all math
deepmd/pt/utils/tabulate.py -560 Thin wrapper: _get_descrpt_type + _convert_numpy_to_tensor
deepmd/pt_expt/utils/tabulate.py rewrite Inherit from tabulate_math, no pt imports
deepmd/pt_expt/descriptor/*.py -6 files Remove ActivationFn import, pass string
deepmd/utils/tabulate.py -12 Remove is_pt flag and branching
deepmd/tf/utils/tabulate.py -1 Remove is_pt=False arg

Test plan

  • PT compression tests pass (16 tests: se_a, se_atten, se_r, se_t, dpa2)
  • PT_EXPT model compression tests pass (6 tests)
  • PT_EXPT descriptor compression tests pass (16 tests)
  • No deepmd.pt imports in pt_expt tabulate or descriptors
  • No is_pt references remain

Summary by CodeRabbit

  • Refactor

    • Streamlined tabulation: removed backend-specific PT logic, normalized activation/exclude handling, and standardized numpy→tensor conversion into a thin runtime wrapper.
  • New Features

    • Added a backend‑neutral math tabulation component that provides activation derivatives and chain‑rule propagation for embedding computations.
  • Tests

    • Converted tests to NumPy inputs/outputs for backend independence and added an explicit linear‑activation derivative test.

Han Wang added 4 commits March 25, 2026 15:06
Backend-agnostic DPTabulate with numpy implementations of:
- Activation derivatives (grad, grad_grad)
- Chain-rule propagation (unaggregated_dy_dx_s, etc.)
- Embedding net forward pass (_make_data, _layer_0, _layer_1)
- Network variable extraction (_get_network_variable)
- Descriptor type detection from serialized data
Remove ~500 lines of duplicated math. PT DPTabulate now only overrides
_get_descrpt_type (isinstance checks) and _convert_numpy_to_tensor
(torch tensor conversion).
…dency

DPTabulate now inherits from deepmd.utils.tabulate_math.DPTabulate
instead of deepmd.pt.utils.tabulate.DPTabulate. Removed all imports
of ActivationFn and DPTabulatePT from deepmd.pt. Descriptors now pass
activation_fn_name (string) instead of ActivationFn (torch module).
Normalize lower/upper to per-type scalars after _get_env_mat_range
so build() no longer needs is_pt branching. Move
_convert_numpy_float_to_int into subclass _convert_numpy_to_tensor.
Remove is_pt parameter from BaseTabulate and all callers.
@wanghan-iapcm wanghan-iapcm requested a review from njzjz March 25, 2026 07:59
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 56bdcf77a9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 25, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Refactors tabulation math into a new numpy-only backend module and replaces PT-side math with a thin PyTorch wrapper that forwards activation names and tensor conversion; several PT call sites now pass raw activation names instead of PT ActivationFn wrappers.

Changes

Cohort / File(s) Summary
New numpy tabulation math
deepmd/utils/tabulate_math.py
Added a numpy-only tabulation math backend: activation->functype map, grad/grad_grad, unaggregated derivative helpers, and a NumPy DPTabulate that computes forward + first/second derivatives.
Base tabulation updates
deepmd/utils/tabulate.py
Removed is_pt flag and PT branches; normalized multi-dim lower/upper via np.min/np.max; simplified descriptor "A"/"T" handling and removed conditional float->int conversion.
PyTorch backend wrapper
deepmd/pt/utils/tabulate.py
Replaced in-file math with a thin wrapper DPTabulate that subclasses the new numpy DPTabulate; accepts activation_fn_name/normalizes exclude_types and delegates math to the numpy backend.
PT-expt wrapper & conversion
deepmd/pt_expt/utils/tabulate.py
Switched base to deepmd.utils.tabulate_math.DPTabulate; constructor now uses activation_fn_name: str; added _convert_numpy_to_tensor() to convert numpy outputs to torch.tensor(..., device=DEVICE) after integer conversion.
PT-expt descriptor call sites
deepmd/pt_expt/descriptor/dpa1.py, .../dpa2.py, .../se_e2_a.py, .../se_r.py, .../se_t.py, .../se_t_tebd.py
Removed local ActivationFn imports and now pass raw data["activation_function"]/repinit_data["activation_function"] (or activation_fn_name=) into DPTabulate instead of constructing ActivationFn(...).
TensorFlow minor adjustment
deepmd/tf/utils/tabulate.py
Removed a trailing False argument from super().__init__() to match updated base signature.
Tests (PT)
source/tests/pt/test_tabulate.py
Replaced torch tensor usage with NumPy inputs/outputs for derivative helpers; removed device moves and updated assertions to use np.asarray(...); added a linear-activation test case.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant Caller as enable_compression (descriptor)
participant PTWrapper as deepmd.pt.utils.DPTabulate
participant TabMath as deepmd.utils.tabulate_math.DPTabulate
participant NumPy as NumPy math
participant Torch as torch/device conversion

Caller->>PTWrapper: instantiate with activation_fn_name (string)
PTWrapper->>TabMath: super().__init__(..., activation_fn_name=...)
TabMath->>NumPy: load params, run forward + derivative math
TabMath-->>PTWrapper: return numpy arrays (vv, dd, d2)
PTWrapper->>PTWrapper: _convert_numpy_float_to_int()
PTWrapper->>Torch: convert numpy arrays -> torch.tensor(device=DEVICE)
PTWrapper-->>Caller: tensor-based tabulation object ready

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

breaking change

Suggested reviewers

  • njzjz
  • iProzd
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately summarizes the main objective: extracting shared tabulate math into a backend-agnostic module and removing the pt_expt→pt dependency.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@deepmd/utils/tabulate_math.py`:
- Around line 33-42: ACTIVATION_TO_FUNCTYPE currently omits aliases like
"linear" and "none" and fails to normalize prefixed names such as "silut*" and
"custom_silu*", causing lookups (where the code maps an activation name to
ACTIVATION_TO_FUNCTYPE) to raise RuntimeError; update ACTIVATION_TO_FUNCTYPE to
include "linear" and "none" entries and extend it with any derivative names you
need, and modify the lookup code to normalize inputs (e.g., lowercasing and
stripping/normalizing known prefixes like "silut", "custom_silu", "silu", etc.)
before dict lookup; also add matching derivative handling cases in the grad and
grad_grad implementations (ensure they accept the same normalized
aliases/prefixes) so the activation, grad, and grad_grad mappings are
consistent.
- Around line 438-440: The code currently assigns vv = zz.astype(self.data_type)
but zz is only defined for layer > 0, causing UnboundLocalError for single-layer
nets; change the assignment to use the current-layer output variable yy (i.e.,
set vv from yy.astype(self.data_type)) and ensure corresponding dd and d2 still
come from dy.astype(self.data_type) and dy2.astype(self.data_type) so both
one-layer and multi-layer cases work (look for vv, zz, yy, dy, dy2, and
data_type to locate the lines to update).
- Around line 246-259: The __init__ signature currently uses a mutable default
exclude_types: list[list[int]] = [], which causes a shared list across
instances; change the parameter to exclude_types: list[list[int]] | None = None
and inside DPTabulate.__init__ (before calling super().__init__) set
exclude_types = [] if exclude_types is None so each instance gets its own list,
then pass that local variable to super().__init__.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: fa001961-2e88-4010-890f-763ebb944a9c

📥 Commits

Reviewing files that changed from the base of the PR and between e97967b and 56bdcf7.

📒 Files selected for processing (11)
  • deepmd/pt/utils/tabulate.py
  • deepmd/pt_expt/descriptor/dpa1.py
  • deepmd/pt_expt/descriptor/dpa2.py
  • deepmd/pt_expt/descriptor/se_e2_a.py
  • deepmd/pt_expt/descriptor/se_r.py
  • deepmd/pt_expt/descriptor/se_t.py
  • deepmd/pt_expt/descriptor/se_t_tebd.py
  • deepmd/pt_expt/utils/tabulate.py
  • deepmd/tf/utils/tabulate.py
  • deepmd/utils/tabulate.py
  • deepmd/utils/tabulate_math.py
💤 Files with no reviewable changes (1)
  • deepmd/tf/utils/tabulate.py

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
deepmd/pt/utils/tabulate.py (1)

57-65: ⚠️ Potential issue | 🟡 Minor

Fix shared defaults and normalize activation input type in __init__.

Lines 57–58 use shared mutable default objects (a code smell), and the type signature at line 58 is misleading since the parameter can reasonably accept strings at call sites. Line 65's direct access to .activation assumes a specific type, which is fragile if the type contract isn't enforced.

The proposed fix correctly:

  • Replaces mutable defaults with None and creates fresh objects
  • Widens the type annotation to accept both str and ActivationFn
  • Normalizes the input before passing to the parent class

Apply the suggested diff to improve type safety and prevent accidental state sharing across instances.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@deepmd/pt/utils/tabulate.py` around lines 57 - 65, The __init__ currently
uses a shared mutable default for exclude_types and assumes activation_fn is an
ActivationFn instance; change the signature to accept exclude_types:
Optional[list[list[int]]] = None and activation_fn: Union[str, ActivationFn] =
"tanh", then inside __init__ set exclude_types = [] if exclude_types is None and
normalize activation_value = activation_fn.activation if
isinstance(activation_fn, ActivationFn) else str(activation_fn) before calling
super().__init__(..., exclude_types, activation_fn_name=activation_value) to
avoid shared state and handle string inputs safely.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@deepmd/pt/utils/tabulate.py`:
- Around line 2-7: The constructor currently uses mutable and call-in-defaults
which fail linters: change the parameter defaults in the DPTabulate (or relevant
__init__) signature from exclude_types: list[list[int]] = [] to exclude_types:
Optional[list[list[int]]] = None and from activation_fn: ActivationFn =
ActivationFn("tanh") to activation_fn: Optional[ActivationFn] = None; then
inside the constructor, if exclude_types is None set exclude_types = [] (or a
fresh list) and if activation_fn is None set activation_fn =
ActivationFn("tanh"). Update any type hints/imports to include Optional and
ensure the instance attribute assignments use these local variables instead of
the original defaults.

---

Outside diff comments:
In `@deepmd/pt/utils/tabulate.py`:
- Around line 57-65: The __init__ currently uses a shared mutable default for
exclude_types and assumes activation_fn is an ActivationFn instance; change the
signature to accept exclude_types: Optional[list[list[int]]] = None and
activation_fn: Union[str, ActivationFn] = "tanh", then inside __init__ set
exclude_types = [] if exclude_types is None and normalize activation_value =
activation_fn.activation if isinstance(activation_fn, ActivationFn) else
str(activation_fn) before calling super().__init__(..., exclude_types,
activation_fn_name=activation_value) to avoid shared state and handle string
inputs safely.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 61f3ebed-df39-41bc-bd39-aeaaf52ceaad

📥 Commits

Reviewing files that changed from the base of the PR and between 56bdcf7 and cb4a122.

📒 Files selected for processing (1)
  • deepmd/pt/utils/tabulate.py

… tensors

The derivative functions now return numpy arrays from tabulate_math.
Update test inputs/outputs accordingly. Remove unused torch/env imports.
@codecov
Copy link

codecov bot commented Mar 25, 2026

Codecov Report

❌ Patch coverage is 86.46865% with 41 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.27%. Comparing base (e97967b) to head (35f1777).

Files with missing lines Patch % Lines
deepmd/utils/tabulate_math.py 84.92% 41 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5339      +/-   ##
==========================================
- Coverage   82.28%   82.27%   -0.01%     
==========================================
  Files         797      798       +1     
  Lines       82100    82110      +10     
  Branches     4003     4003              
==========================================
+ Hits        67557    67558       +1     
- Misses      13336    13345       +9     
  Partials     1207     1207              

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

Han Wang added 2 commits March 25, 2026 21:57
- Fix UnboundLocalError: use yy instead of zz for single-layer nets
- Add linear/none (functype=0) to ACTIVATION_TO_FUNCTYPE and
  grad/grad_grad (identity: f'=1, f''=0)
- All 8 activation derivatives verified against numerical differentiation
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: 2

🧹 Nitpick comments (2)
source/tests/pt/test_tabulate.py (2)

30-34: Unused dictionary ACTIVATION_NAMES_NUMPY_ONLY.

This dictionary is defined but never referenced in the test code. Consider removing it or using it in test_linear_activation.

♻️ Option 1: Remove unused dict
-# functype=0 (linear/none) is not supported by TF custom ops,
-# so we test it separately against numerical derivatives.
-ACTIVATION_NAMES_NUMPY_ONLY = {
-    0: "linear",
-}
♻️ Option 2: Use it in test_linear_activation
     def test_linear_activation(self) -> None:
-        """Test functype=0 (linear/none) against numerical derivatives.
+        """Test numpy-only activations against numerical derivatives.

         TF custom ops don't support functype=0, so we validate against
         finite-difference derivatives instead.
         """
         from deepmd.utils.tabulate_math import (
             grad,
             grad_grad,
         )

-        fn = get_activation_fn("linear")
+        for functype, activation_name in ACTIVATION_NAMES_NUMPY_ONLY.items():
+            fn = get_activation_fn(activation_name)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@source/tests/pt/test_tabulate.py` around lines 30 - 34, The dictionary
ACTIVATION_NAMES_NUMPY_ONLY is defined but never used; either remove it or make
test_linear_activation consume it: update the test_linear_activation function to
iterate or parameterize over ACTIVATION_NAMES_NUMPY_ONLY (using its keys/values)
so the "linear" case is covered via that mapping, or simply delete the
ACTIVATION_NAMES_NUMPY_ONLY constant if you prefer to keep the test
hardcoded—ensure references to ACTIVATION_NAMES_NUMPY_ONLY are removed if
deleting.

205-207: Unused variable h.

The variable h = 1e-7 appears to be a step size for finite-difference derivatives, but the test uses analytical derivatives instead. Remove it or add the numerical verification it was intended for.

♻️ Proposed fix - remove unused variable
         fn = get_activation_fn("linear")
         y = fn(self.xbar)
-        h = 1e-7

         # grad: f'(x) = 1 for identity
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@source/tests/pt/test_tabulate.py` around lines 205 - 207, Remove the unused
numerical step variable h = 1e-7 from the test (or alternatively implement the
intended finite-difference check); specifically, edit the test around
get_activation_fn("linear") / fn and y = fn(self.xbar) to either delete the line
defining h or replace it with a numeric-derivative verification that uses h to
compute finite-difference gradients and compare them to the analytical
derivatives returned by the activation function.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@deepmd/utils/tabulate_math.py`:
- Around line 80-107: grad_grad currently returns -np.ones_like(xbar) for
unknown functype while grad raises a ValueError; update grad_grad so it mirrors
grad's behavior by raising a ValueError for unsupported functype values (include
the invalid functype in the exception message) instead of returning a sentinel
array; locate the grad_grad function and replace the final else branch to raise
the same style of ValueError used by grad.
- Around line 458-473: The function _get_descrpt_type builds a type_map that
lacks the "dpa2" => "Atten" entry, causing RuntimeError for DPA2 descriptors;
update the type_map inside _get_descrpt_type (symbol: type_map in
deepmd/utils/tabulate_math.py) to include "dpa2": "Atten" so DPA2 serializations
map to "Atten", and apply the identical change to the analogous mapping in
deepmd/pt_expt/utils/tabulate.py (around the mapping at line 65) to keep both
modules consistent.

---

Nitpick comments:
In `@source/tests/pt/test_tabulate.py`:
- Around line 30-34: The dictionary ACTIVATION_NAMES_NUMPY_ONLY is defined but
never used; either remove it or make test_linear_activation consume it: update
the test_linear_activation function to iterate or parameterize over
ACTIVATION_NAMES_NUMPY_ONLY (using its keys/values) so the "linear" case is
covered via that mapping, or simply delete the ACTIVATION_NAMES_NUMPY_ONLY
constant if you prefer to keep the test hardcoded—ensure references to
ACTIVATION_NAMES_NUMPY_ONLY are removed if deleting.
- Around line 205-207: Remove the unused numerical step variable h = 1e-7 from
the test (or alternatively implement the intended finite-difference check);
specifically, edit the test around get_activation_fn("linear") / fn and y =
fn(self.xbar) to either delete the line defining h or replace it with a
numeric-derivative verification that uses h to compute finite-difference
gradients and compare them to the analytical derivatives returned by the
activation function.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 07fbed44-6815-422f-8d2e-6b756257a489

📥 Commits

Reviewing files that changed from the base of the PR and between 1067ff0 and 9b3661a.

📒 Files selected for processing (2)
  • deepmd/utils/tabulate_math.py
  • source/tests/pt/test_tabulate.py

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.

♻️ Duplicate comments (2)
deepmd/utils/tabulate_math.py (2)

283-286: ⚠️ Potential issue | 🟠 Major

Make the DPA2 branch reachable.

Lines 283-286 explicitly handle DPA2 by switching to serialized["repinit_variable"], but _get_descrpt_type() never maps "dpa2" to "Atten", so serialization-driven callers fail before that branch can run. Add "dpa2": "Atten" to the type map.

🐛 Proposed fix
         type_map = {
             "se_e2_a": "A",
             "se_r": "R",
             "se_e3": "T",
             "se_e3_tebd": "T_TEBD",
             "dpa1": "Atten",
+            "dpa2": "Atten",
             "se_atten_v2": "Atten",
         }

Also applies to: 463-473

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@deepmd/utils/tabulate_math.py` around lines 283 - 286, The DPA2 branch isn't
reachable because _get_descrpt_type() does not map "dpa2" to "Atten", so the
later code that expects descrpt_type == "Atten" and uses
serialized["repinit_variable"] never runs; fix by adding the mapping "dpa2":
"Atten" to the type map inside _get_descrpt_type() (and mirror this change in
the other similar mapping block referenced around the other occurrence),
ensuring callers that serialize can hit the serialized["repinit_variable"]
branch.

33-44: ⚠️ Potential issue | 🟠 Major

Normalize activation aliases before the functype lookup.

The shared backend now accepts the forward activation via get_activation_fn(...), but the derivative side still only accepts exact keys from ACTIVATION_TO_FUNCTYPE. That leaves valid project aliases/prefixed SiLU names failing here with Unknown activation function ... even though the forward activation can still be constructed. Normalize once up front and use that normalized name for both get_activation_fn() and the functype map.

♻️ Proposed fix
+def _normalize_activation_name(name: str) -> str:
+    normalized = name.lower()
+    if normalized.startswith(("silut", "custom_silu")):
+        return "silu"
+    if normalized in {"none", "linear"}:
+        return "linear"
+    return normalized
+
 ...
-        self._activation_fn = get_activation_fn(activation_fn_name)
-        activation_fn_name = activation_fn_name.lower()
+        activation_fn_name = _normalize_activation_name(activation_fn_name)
+        self._activation_fn = get_activation_fn(activation_fn_name)
         if activation_fn_name not in ACTIVATION_TO_FUNCTYPE:
             raise RuntimeError(f"Unknown activation function: {activation_fn_name}")

Also applies to: 267-271

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@deepmd/utils/tabulate_math.py` around lines 33 - 44, The
ACTIVATION_TO_FUNCTYPE lookup fails for valid aliases because activation names
are not normalized first; normalize the activation string once (using the same
normalization logic as get_activation_fn or by calling a canonicalizer used by
get_activation_fn) and then use that normalized name for both calling
get_activation_fn(...) and for the ACTIVATION_TO_FUNCTYPE[...] lookup; apply the
same change at the other occurrence that also performs a functype lookup so both
sites use the same canonical activation key.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@deepmd/utils/tabulate_math.py`:
- Around line 283-286: The DPA2 branch isn't reachable because
_get_descrpt_type() does not map "dpa2" to "Atten", so the later code that
expects descrpt_type == "Atten" and uses serialized["repinit_variable"] never
runs; fix by adding the mapping "dpa2": "Atten" to the type map inside
_get_descrpt_type() (and mirror this change in the other similar mapping block
referenced around the other occurrence), ensuring callers that serialize can hit
the serialized["repinit_variable"] branch.
- Around line 33-44: The ACTIVATION_TO_FUNCTYPE lookup fails for valid aliases
because activation names are not normalized first; normalize the activation
string once (using the same normalization logic as get_activation_fn or by
calling a canonicalizer used by get_activation_fn) and then use that normalized
name for both calling get_activation_fn(...) and for the
ACTIVATION_TO_FUNCTYPE[...] lookup; apply the same change at the other
occurrence that also performs a functype lookup so both sites use the same
canonical activation key.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c5ea7a07-c5f4-489a-b10d-cbf14a56293f

📥 Commits

Reviewing files that changed from the base of the PR and between 9b3661a and 3686686.

📒 Files selected for processing (4)
  • deepmd/pt/utils/tabulate.py
  • deepmd/pt_expt/utils/tabulate.py
  • deepmd/utils/tabulate_math.py
  • source/tests/pt/test_tabulate.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • source/tests/pt/test_tabulate.py

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.

1 participant