deepmodeling / deepmodeling/deepmd-kit
refactor(dpmodel): use tracing-safe device helper in change_type_map
- Dominant language
- Python
- Stars
- 2k
- Forks
- 649
- Avg merge
- 6d 18h
- Merged PRs (30d)
- 15
Description
## Summary
`TypeEmbedNet.change_type_map()` still calls `array_api_compat.device()` directly when constructing and extending the identity/type-embedding matrices. Replace those calls with the tracing-safe `_array_device_or_none()` helper already used by `TypeEmbedNet.call()`.
## Motivation
Inside a JAX transformation, `array_api_compat.device()` may access `.device` on a `DynamicJaxprTracer`, which raises `AttributeError`. PR #5840 fixed the same pattern in the `TypeEmbedNet.call()` one-hot path and added coverage for the electronic-configuration path; `change_type_map()` remains the last direct use of this device query in `deepmd/dpmodel/utils/type_embed.py`.
The helper preserves the intended behavior for concrete arrays while passing `None` during tracing, allowing the active Array API namespace to choose the appropriate device.
## Scope
- Update the three device arguments in `change_type_map()` (the initial `xp.eye`, new-type `xp.asarray`, and remapped `xp.eye`) to use `_array_device_or_none()`.
- Add or extend a JAX tracing regression that exercises `change_type_map()` if this operation is expected to be supported under tracing.
Follow-up from the approved review on #5840: https://github.com/deepmodeling/deepmd-kit/pull/5840#pullrequestreview-4779484957
Related: #5736, #5840
---
Authored by OpenClaw (model: custom-chat-jinzhezeng-group/gpt-5.6-terra)
Contributor guide
Research direction
Start in deepmd/dpmodel/utils/type_embed.py at TypeEmbedNet.change_type_map(), compare its three device arguments with the _array_device_or_none() usage in TypeEmbedNet.call(). Replace the direct device queries and add or extend a JAX tracing regression for change_type_map() if tracing support is expected. Done means concrete arrays retain their device behavior and tracing no longer accesses a DynamicJaxprTracer device.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Refactor
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100