Commit 7b0050f
authored
fix(jax): avoid device lookup during type embedding tracing (#5736)
## Summary
Fix a JAX/Flax NNX tracing failure in `TypeEmbedNet.call()` by avoiding
a hard requirement that traced values expose a readable `.device`
attribute.
## Background
JAX training can call `TypeEmbedNet.call()` from inside `nnx.jit` /
`nnx.grad`. In that context the type-embedding weights are represented
by NNX/JAX traced values such as `DynamicJaxprTracer`. The previous code
passed
```python
device=array_api_compat.device(sample_array)
```
to `xp.eye`, and did the same for the padding `xp.zeros`. For an NNX
traced parameter, `array_api_compat.device(...)` eventually tries to
read `.device`; the tracer does not provide that attribute during
tracing, so the training step fails with an error like:
```text
AttributeError: DynamicJaxprTracer has no attribute device
```
or, through JAX core wrapping:
```text
AttributeError: 'ShapedArray' object has no attribute 'device'
```
## Change
Add a small local helper in `deepmd/dpmodel/utils/type_embed.py` that
returns `array_api_compat.device(array)` when available, and falls back
to `None` when the backend value has no readable device during tracing.
Passing `device=None` lets JAX create the constants on its default
traced device, while preserving the existing explicit-device behavior
for backends that expose it normally.
This keeps the change limited to the type-embedding constants that
triggered the crash, rather than changing global array API device
handling.
## Tests
Added `source/tests/jax/test_type_embed.py` to cover both:
- `TypeEmbedNet.call()` under `nnx.jit`
- `TypeEmbedNet.call()` under `nnx.jit` + `nnx.grad`
The old implementation fails this test during tracing before producing
an output.
Validation run locally:
```bash
pytest source/tests/jax/test_type_embed.py -v
ruff check .
ruff format .
```
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved compatibility for model execution on array backends that do
not support device detection in all cases.
* Reduced failures when padding or initializing arrays during type
embedding operations.
* **Tests**
* Added JAX coverage to verify the type embedding model runs under JIT,
supports gradient tracing, returns the expected output shape, and
produces valid numeric results.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 26ad0fb commit 7b0050f
2 files changed
Lines changed: 53 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
27 | 34 | | |
28 | 35 | | |
29 | 36 | | |
| |||
104 | 111 | | |
105 | 112 | | |
106 | 113 | | |
107 | | - | |
| 114 | + | |
108 | 115 | | |
109 | 116 | | |
110 | 117 | | |
| |||
113 | 120 | | |
114 | 121 | | |
115 | 122 | | |
116 | | - | |
| 123 | + | |
117 | 124 | | |
118 | 125 | | |
119 | 126 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
0 commit comments