forked from hw-native-sys/simpler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtorch_interop.py
More file actions
84 lines (66 loc) · 3.35 KB
/
Copy pathtorch_interop.py
File metadata and controls
84 lines (66 loc) · 3.35 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright (c) PyPTO Contributors.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------
# ruff: noqa: PLW0603, PLC0415
"""Torch integration helpers.
Canonical home for torch-aware helpers that convert ``torch.Tensor`` and
``torch.dtype`` values into the runtime's ``ContinuousTensor`` / ``DataType``
types. These helpers live in ``simpler_setup`` (not ``simpler``) so that the
stable ``simpler`` runtime API can remain torch-free; torch integration is a
setup-time/test-framework concern.
Callers:
from simpler_setup.torch_interop import make_tensor_arg, torch_dtype_to_datatype
torch is imported lazily inside ``_ensure_torch_map`` so that importing this
module does not force torch onto users who only touch ``simpler_setup`` for
other reasons (e.g. ``RuntimeBuilder``). ``simpler.task_interface`` is also
imported lazily because ``simpler_setup/__init__.py`` is executed during
``pip install`` (via ``build_runtimes.py``), before the ``_task_interface``
nanobind extension is built.
Requires torch >= 2.3 (for ``torch.uint16`` / ``torch.uint32``).
"""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from simpler.task_interface import ContinuousTensor, DataType
_TORCH_DTYPE_MAP = None
def _ensure_torch_map():
global _TORCH_DTYPE_MAP
if _TORCH_DTYPE_MAP is not None:
return
import torch # pyright: ignore[reportMissingImports]
from simpler.task_interface import DataType
_TORCH_DTYPE_MAP = {
torch.float32: DataType.FLOAT32,
torch.float16: DataType.FLOAT16,
torch.int32: DataType.INT32,
torch.int16: DataType.INT16,
torch.int8: DataType.INT8,
torch.uint8: DataType.UINT8,
torch.bfloat16: DataType.BFLOAT16,
torch.int64: DataType.INT64,
torch.uint16: DataType.UINT16,
torch.uint32: DataType.UINT32,
}
def torch_dtype_to_datatype(dt) -> DataType:
"""Convert a ``torch.dtype`` to a ``DataType`` enum value.
Raises ``KeyError`` for unsupported dtypes.
"""
_ensure_torch_map()
return _TORCH_DTYPE_MAP[dt] # pyright: ignore[reportOptionalSubscript]
def make_tensor_arg(tensor) -> ContinuousTensor:
"""Create a ``ContinuousTensor`` from a torch.Tensor.
The tensor must be CPU-contiguous. Its ``data_ptr()``, shape, and dtype
are read and stored in the returned ``ContinuousTensor``.
"""
from simpler.task_interface import ContinuousTensor
_ensure_torch_map()
dt = _TORCH_DTYPE_MAP.get(tensor.dtype) # pyright: ignore[reportOptionalMemberAccess]
if dt is None:
raise ValueError(f"Unsupported tensor dtype for ContinuousTensor: {tensor.dtype}")
shapes = tuple(int(s) for s in tensor.shape)
return ContinuousTensor.make(tensor.data_ptr(), shapes, dt)