Commit b73b3dd
authored
Add __array__ and DLPack protocols to OrtValue (#27980)
## Summary
- Add `__array__`, `__dlpack__`, `__dlpack_device__`, and `from_dlpack`
to the public `OrtValue` class
- Enable standard Python interoperability protocols (numpy array
protocol + DLPack) on `OrtValue`
- Auto-detect boolean dtype from source objects in `from_dlpack` to
avoid the uint8/bool ambiguity in older DLPack versions
## Motivation
Fixes #24071
The C-level `C.OrtValue` already supports `__dlpack__`,
`__dlpack_device__`, and `from_dlpack`, but the public Python wrapper
`OrtValue` class does not expose them. Users currently have to access
the private `_ortvalue` attribute (e.g.
`ortvalue._ortvalue.__dlpack__()`) for DLPack interop. Similarly,
`np.asarray(ortvalue)` doesn't work because `__array__` is not
implemented.
This makes `OrtValue` a well-behaved tensor type that works out of the
box with:
- `np.asarray(ortvalue)` / `np.array(ortvalue)` via `__array__`
- `torch.from_dlpack(ortvalue)` via `__dlpack__` / `__dlpack_device__`
- `OrtValue.from_dlpack(torch_tensor)` via the `from_dlpack` classmethod
## Changes
**`onnxruntime/python/onnxruntime_inference_collection.py`**:
- `__array__(dtype, copy)`: Delegates to `self.numpy()` with optional
dtype conversion. Supports numpy 2.0 `copy` semantics while remaining
compatible with older numpy versions.
- `__dlpack__(*, stream)`: Thin wrapper over the C-level `__dlpack__`.
- `__dlpack_device__()`: Thin wrapper over the C-level
`__dlpack_device__`.
- `from_dlpack(data)`: Classmethod that accepts any
`__dlpack__`-compatible object or raw DLPack capsule. Detects boolean
dtype from the source object's `dtype` attribute or `data_type()`
method, avoiding the uint8/bool false-positive that
`is_dlpack_uint8_tensor` would produce on genuine uint8 data.
**`onnxruntime/test/python/onnxruntime_test_python.py`**:
- `test_ort_value_array_protocol`: Tests `np.asarray`/`np.array` with
float32, int64, bool dtypes, and dtype conversion.
- `test_ort_value_dlpack_protocol`: Tests `__dlpack__` and
`__dlpack_device__` on the public class.
- `test_ort_value_from_dlpack_protocol_object`: Tests `from_dlpack` with
numpy arrays and OrtValue-to-OrtValue round-trip, verifying zero-copy
(shared memory).
- `test_ort_value_from_dlpack_bool`: Tests bool round-trip and verifies
uint8 is not falsely detected as bool.
## Test Plan
- [x] `ruff check` passes on both modified files
- [x] `ruff format --check` passes on both modified files
- [x] `lintrunner` reports no issues
- [x] Existing `test_ort_value_dlpack` test continues to pass
- [x] All logic paths verified against C-level bindings (bool detection,
dtype conversion, shared memory)
- [ ] CI: new tests pass against a full build with DLPack enabled1 parent cd48875 commit b73b3dd
2 files changed
Lines changed: 194 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1199 | 1199 | | |
1200 | 1200 | | |
1201 | 1201 | | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
1202 | 1305 | | |
1203 | 1306 | | |
1204 | 1307 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1475 | 1475 | | |
1476 | 1476 | | |
1477 | 1477 | | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 1481 | + | |
| 1482 | + | |
| 1483 | + | |
| 1484 | + | |
| 1485 | + | |
| 1486 | + | |
| 1487 | + | |
| 1488 | + | |
| 1489 | + | |
| 1490 | + | |
| 1491 | + | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
| 1498 | + | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
1478 | 1569 | | |
1479 | 1570 | | |
1480 | 1571 | | |
| |||
0 commit comments