Skip to content

Commit 5beec9c

Browse files
authored
ENH: support int8 (#43)
* ENH: support int8 * add support for 8-bit signed integers with values that should exist on the interval `[-128, 127]` for conformance with the array API: https://data-apis.org/array-api/latest/API_specification/data_types.html kokkos/pykokkos#57 * add a regression test for kokkos<->NumPy type equivalencies that includes the new `int8` type; I was hoping to do some simple arithmetic testing as well, but not sure how practical that is within `pykokkos-base` proper (maybe defer that kind of thing to `pykokkos`?) * MAINT: PR 43 revisions * format with `black` because of a failing CI check * use `signed_char` as the shorthand for `int8`, for similarity with: https://numpy.org/doc/stable/user/basics.types.html
1 parent 3402d93 commit 5beec9c

File tree

6 files changed

+42
-3
lines changed

6 files changed

+42
-3
lines changed

include/fwd.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ struct Device;
123123
//----------------------------------------------------------------------------//
124124

125125
enum KokkosViewDataType {
126-
Int16 = 0,
126+
Int8 = 0,
127+
Int16,
127128
Int32,
128129
Int64,
129130
Uint16,

include/traits.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ VIEW_DATA_DIMS(8, T ********)
7575
// the first string identifier is the "canonical name" (i.e. what gets encoded)
7676
// and the remaining string entries are used to generate aliases
7777
//
78+
VIEW_DATA_TYPE(int8_t, Int8, "int8", "signed_char")
7879
VIEW_DATA_TYPE(int16_t, Int16, "int16", "short")
7980
VIEW_DATA_TYPE(int32_t, Int32, "int32", "int")
8081
VIEW_DATA_TYPE(int64_t, Int64, "int64", "long")

kokkos/__init__.py.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ try:
147147
"read_dtype",
148148
"initialize", # bindings
149149
"finalize",
150-
"int16", # data types
150+
"int8", # data types
151+
"int16",
151152
"int32",
152153
"int64",
153154
"uint16",

kokkos/test/test_types.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import kokkos
2+
3+
import numpy as np
4+
import pytest
5+
6+
7+
@pytest.mark.parametrize(
8+
"type_val, expected_np_type",
9+
[
10+
(kokkos.int8, np.int8),
11+
(kokkos.int16, np.int16),
12+
(kokkos.int32, np.int32),
13+
(kokkos.int64, np.int64),
14+
(kokkos.uint16, np.uint16),
15+
(kokkos.uint32, np.uint32),
16+
(kokkos.uint64, np.uint64),
17+
(kokkos.float32, np.float32),
18+
(kokkos.float64, np.float64),
19+
(kokkos.float, np.float32),
20+
(kokkos.double, np.float64),
21+
(kokkos.short, np.int16),
22+
(kokkos.int, np.int32),
23+
(kokkos.long, np.int64),
24+
],
25+
)
26+
def test_basic_type_equiv(type_val, expected_np_type):
27+
# test some view to NumPy array type equivalencies
28+
kokkos.initialize()
29+
view = kokkos.array([2], dtype=type_val, space=kokkos.DefaultHostMemorySpace)
30+
31+
# NOTE: copy can still happen (attempt no copy,
32+
# not guarantee)
33+
arr = np.array(view, copy=False)
34+
assert arr.dtype == expected_np_type

kokkos/utility.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def read_dtype(_dtype):
8181
try:
8282
import numpy as np
8383

84+
if _dtype == np.int8:
85+
return lib.int8
8486
if _dtype == np.int16:
8587
return lib.int16
8688
elif _dtype == np.int32:

src/variants/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ TARGET_LINK_LIBRARIES(libpykokkos-variants PUBLIC
2626

2727
SET(_types concrete dynamic)
2828
SET(_variants layout memory_trait)
29-
SET(_data_types Int16 Int32 Int64 Uint16 Uint32 Uint64 Float32 Float64)
29+
SET(_data_types Int8 Int16 Int32 Int64 Uint16 Uint32 Uint64 Float32 Float64)
3030

3131
SET(layout_enums Right)
3232
SET(memory_trait_enums Managed)

0 commit comments

Comments
 (0)