fxn 0.0.40__tar.gz → 0.0.41__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fxn-0.0.40 → fxn-0.0.41}/PKG-INFO +3 -3
- {fxn-0.0.40 → fxn-0.0.41}/README.md +2 -2
- fxn-0.0.41/fxn/c/__init__.py +13 -0
- fxn-0.0.41/fxn/c/configuration.py +118 -0
- fxn-0.0.41/fxn/c/fxnc.py +48 -0
- fxn-0.0.41/fxn/c/map.py +64 -0
- fxn-0.0.41/fxn/c/prediction.py +76 -0
- fxn-0.0.41/fxn/c/predictor.py +60 -0
- fxn-0.0.41/fxn/c/stream.py +41 -0
- fxn-0.0.41/fxn/c/value.py +223 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/cli/__init__.py +6 -2
- fxn-0.0.40/fxn/cli/predict.py → fxn-0.0.41/fxn/cli/predictions.py +32 -35
- fxn-0.0.41/fxn/client.py +46 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/function.py +4 -4
- fxn-0.0.41/fxn/lib/linux/arm64/libFunction.so +0 -0
- fxn-0.0.41/fxn/lib/linux/x86_64/libFunction.so +0 -0
- fxn-0.0.41/fxn/lib/macos/arm64/Function.dylib +0 -0
- fxn-0.0.41/fxn/lib/macos/x86_64/Function.dylib +0 -0
- fxn-0.0.41/fxn/lib/windows/arm64/Function.dll +0 -0
- fxn-0.0.41/fxn/lib/windows/x86_64/Function.dll +0 -0
- fxn-0.0.41/fxn/services/__init__.py +8 -0
- fxn-0.0.41/fxn/services/prediction.py +278 -0
- fxn-0.0.41/fxn/services/predictor.py +30 -0
- fxn-0.0.41/fxn/services/user.py +27 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/types/__init__.py +1 -1
- {fxn-0.0.40 → fxn-0.0.41}/fxn/types/prediction.py +8 -8
- {fxn-0.0.40 → fxn-0.0.41}/fxn/types/predictor.py +18 -21
- fxn-0.0.41/fxn/types/user.py +29 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/version.py +1 -1
- {fxn-0.0.40 → fxn-0.0.41}/fxn.egg-info/PKG-INFO +3 -3
- {fxn-0.0.40 → fxn-0.0.41}/fxn.egg-info/SOURCES.txt +2 -6
- fxn-0.0.40/fxn/api/__init__.py +0 -6
- fxn-0.0.40/fxn/api/client.py +0 -43
- fxn-0.0.40/fxn/c/__init__.py +0 -16
- fxn-0.0.40/fxn/c/configuration.py +0 -60
- fxn-0.0.40/fxn/c/dtype.py +0 -26
- fxn-0.0.40/fxn/c/fxnc.py +0 -28
- fxn-0.0.40/fxn/c/map.py +0 -34
- fxn-0.0.40/fxn/c/prediction.py +0 -37
- fxn-0.0.40/fxn/c/predictor.py +0 -31
- fxn-0.0.40/fxn/c/status.py +0 -12
- fxn-0.0.40/fxn/c/stream.py +0 -22
- fxn-0.0.40/fxn/c/value.py +0 -50
- fxn-0.0.40/fxn/c/version.py +0 -13
- fxn-0.0.40/fxn/lib/linux/arm64/libFunction.so +0 -0
- fxn-0.0.40/fxn/lib/linux/x86_64/libFunction.so +0 -0
- fxn-0.0.40/fxn/lib/macos/arm64/Function.dylib +0 -0
- fxn-0.0.40/fxn/lib/macos/x86_64/Function.dylib +0 -0
- fxn-0.0.40/fxn/lib/windows/arm64/Function.dll +0 -0
- fxn-0.0.40/fxn/lib/windows/x86_64/Function.dll +0 -0
- fxn-0.0.40/fxn/services/__init__.py +0 -8
- fxn-0.0.40/fxn/services/prediction.py +0 -450
- fxn-0.0.40/fxn/services/predictor.py +0 -206
- fxn-0.0.40/fxn/services/user.py +0 -56
- fxn-0.0.40/fxn/types/user.py +0 -35
- {fxn-0.0.40 → fxn-0.0.41}/LICENSE +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/__init__.py +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/cli/auth.py +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/cli/env.py +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/cli/misc.py +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/cli/predictors.py +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/lib/__init__.py +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn/types/dtype.py +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn.egg-info/dependency_links.txt +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn.egg-info/entry_points.txt +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn.egg-info/requires.txt +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/fxn.egg-info/top_level.txt +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/pyproject.toml +0 -0
- {fxn-0.0.40 → fxn-0.0.41}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: fxn
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.41
|
4
4
|
Summary: Run prediction functions locally in Python. Register at https://fxn.ai.
|
5
5
|
Author-email: "NatML Inc." <hi@fxn.ai>
|
6
6
|
License: Apache License
|
@@ -227,7 +227,7 @@ Requires-Dist: typer
|
|
227
227
|
|
228
228
|

|
229
229
|
|
230
|
-
[](https://
|
230
|
+
[](https://discord.gg/fxn)
|
231
231
|
|
232
232
|
Run prediction functions (a.k.a "predictors") locally in your Python apps, with full GPU acceleration and zero dependencies.
|
233
233
|
|
@@ -286,7 +286,7 @@ ___
|
|
286
286
|
|
287
287
|
## Useful Links
|
288
288
|
- [Discover predictors to use in your apps](https://fxn.ai/explore).
|
289
|
-
- [Join our Discord community](https://
|
289
|
+
- [Join our Discord community](https://discord.gg/fxn).
|
290
290
|
- [Check out our docs](https://docs.fxn.ai).
|
291
291
|
- Learn more about us [on our blog](https://blog.fxn.ai).
|
292
292
|
- Reach out to us at [hi@fxn.ai](mailto:hi@fxn.ai).
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|

|
4
4
|
|
5
|
-
[](https://
|
5
|
+
[](https://discord.gg/fxn)
|
6
6
|
|
7
7
|
Run prediction functions (a.k.a "predictors") locally in your Python apps, with full GPU acceleration and zero dependencies.
|
8
8
|
|
@@ -61,7 +61,7 @@ ___
|
|
61
61
|
|
62
62
|
## Useful Links
|
63
63
|
- [Discover predictors to use in your apps](https://fxn.ai/explore).
|
64
|
-
- [Join our Discord community](https://
|
64
|
+
- [Join our Discord community](https://discord.gg/fxn).
|
65
65
|
- [Check out our docs](https://docs.fxn.ai).
|
66
66
|
- Learn more about us [on our blog](https://blog.fxn.ai).
|
67
67
|
- Reach out to us at [hi@fxn.ai](mailto:hi@fxn.ai).
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#
|
2
|
+
# Function
|
3
|
+
# Copyright © 2024 NatML Inc. All Rights Reserved.
|
4
|
+
#
|
5
|
+
|
6
|
+
# https://github.com/fxnai/fxnc
|
7
|
+
|
8
|
+
from .configuration import Configuration
|
9
|
+
from .map import ValueMap
|
10
|
+
from .prediction import Prediction
|
11
|
+
from .predictor import Predictor
|
12
|
+
from .stream import PredictionStream
|
13
|
+
from .value import Value, ValueFlags
|
@@ -0,0 +1,118 @@
|
|
1
|
+
#
|
2
|
+
# Function
|
3
|
+
# Copyright © 2024 NatML Inc. All Rights Reserved.
|
4
|
+
#
|
5
|
+
|
6
|
+
from ctypes import byref, c_int, c_void_p, create_string_buffer
|
7
|
+
from pathlib import Path
|
8
|
+
from typing import final
|
9
|
+
|
10
|
+
from ..types import Acceleration
|
11
|
+
from .fxnc import get_fxnc, status_to_error, FXNStatus
|
12
|
+
|
13
|
+
@final
|
14
|
+
class Configuration:
|
15
|
+
|
16
|
+
def __init__ (self):
|
17
|
+
configuration = c_void_p()
|
18
|
+
status = get_fxnc().FXNConfigurationCreate(byref(configuration))
|
19
|
+
if status == FXNStatus.OK:
|
20
|
+
self.__configuration = configuration
|
21
|
+
else:
|
22
|
+
raise RuntimeError(f"Failed to create configuration with error: {status_to_error(status)}")
|
23
|
+
|
24
|
+
@property
|
25
|
+
def tag (self) -> str:
|
26
|
+
buffer = create_string_buffer(2048)
|
27
|
+
status = get_fxnc().FXNConfigurationGetTag(self.__configuration, buffer, len(buffer))
|
28
|
+
if status != FXNStatus.OK:
|
29
|
+
raise RuntimeError(f"Failed to get configuration tag with error: {status_to_error(status)}")
|
30
|
+
tag = buffer.value.decode("utf-8")
|
31
|
+
return tag if tag else None
|
32
|
+
|
33
|
+
@tag.setter
|
34
|
+
def tag (self, tag: str):
|
35
|
+
tag = tag.encode() if tag is not None else None
|
36
|
+
status = get_fxnc().FXNConfigurationSetTag(self.__configuration, tag)
|
37
|
+
if status != FXNStatus.OK:
|
38
|
+
raise RuntimeError(f"Failed to set configuration tag with error: {status_to_error(status)}")
|
39
|
+
|
40
|
+
@property
|
41
|
+
def token (self) -> str:
|
42
|
+
buffer = create_string_buffer(2048)
|
43
|
+
status = get_fxnc().FXNConfigurationGetToken(self.__configuration, buffer, len(buffer))
|
44
|
+
if status != FXNStatus.OK:
|
45
|
+
raise RuntimeError(f"Failed to get configuration token with error: {status_to_error(status)}")
|
46
|
+
token = buffer.value.decode("utf-8")
|
47
|
+
return token if token else None
|
48
|
+
|
49
|
+
@token.setter
|
50
|
+
def token (self, token: str):
|
51
|
+
token = token.encode() if token is not None else None
|
52
|
+
status = get_fxnc().FXNConfigurationSetToken(self.__configuration, token)
|
53
|
+
if status != FXNStatus.OK:
|
54
|
+
raise RuntimeError(f"Failed to set configuration token with error: {status_to_error(status)}")
|
55
|
+
|
56
|
+
@property
|
57
|
+
def acceleration (self) -> Acceleration:
|
58
|
+
acceleration = c_int()
|
59
|
+
status = get_fxnc().FXNConfigurationGetAcceleration(self.__configuration, byref(acceleration))
|
60
|
+
if status == FXNStatus.OK:
|
61
|
+
return Acceleration(acceleration.value)
|
62
|
+
else:
|
63
|
+
raise RuntimeError(f"Failed to get configuration acceleration with error: {status_to_error(status)}")
|
64
|
+
|
65
|
+
@acceleration.setter
|
66
|
+
def acceleration (self, acceleration: Acceleration):
|
67
|
+
status = get_fxnc().FXNConfigurationSetAcceleration(self.__configuration, acceleration.value)
|
68
|
+
if status != FXNStatus.OK:
|
69
|
+
raise RuntimeError(f"Failed to set configuration acceleration with error: {status_to_error(status)}")
|
70
|
+
|
71
|
+
@property
|
72
|
+
def device (self):
|
73
|
+
device = c_void_p()
|
74
|
+
status = get_fxnc().FXNConfigurationGetDevice(self.__configuration, byref(device))
|
75
|
+
if status == FXNStatus.OK:
|
76
|
+
return device if device.value else None
|
77
|
+
else:
|
78
|
+
raise RuntimeError(f"Failed to get configuration device with error: {status_to_error(status)}")
|
79
|
+
|
80
|
+
@device.setter
|
81
|
+
def device (self, device):
|
82
|
+
status = get_fxnc().FXNConfigurationSetDevice(self.__configuration, device)
|
83
|
+
if status != FXNStatus.OK:
|
84
|
+
raise RuntimeError(f"Failed to set configuration device with error: {status_to_error(status)}")
|
85
|
+
|
86
|
+
def add_resource (self, type: str, path: Path):
|
87
|
+
status = get_fxnc().FXNConfigurationAddResource(self.__configuration, type.encode(), str(path).encode())
|
88
|
+
if status != FXNStatus.OK:
|
89
|
+
raise RuntimeError(f"Failed to add configuration resource with error: {status_to_error(status)}")
|
90
|
+
|
91
|
+
def __enter__ (self):
|
92
|
+
return self
|
93
|
+
|
94
|
+
def __exit__ (self, exc_type, exc_value, traceback):
|
95
|
+
self.__release()
|
96
|
+
|
97
|
+
def __release (self):
|
98
|
+
if self.__configuration:
|
99
|
+
get_fxnc().FXNConfigurationRelease(self.__configuration)
|
100
|
+
self.__configuration = None
|
101
|
+
|
102
|
+
@classmethod
|
103
|
+
def get_unique_id (cls) -> str:
|
104
|
+
buffer = create_string_buffer(2048)
|
105
|
+
status = get_fxnc().FXNConfigurationGetUniqueID(buffer, len(buffer))
|
106
|
+
if status == FXNStatus.OK:
|
107
|
+
return buffer.value.decode("utf-8")
|
108
|
+
else:
|
109
|
+
raise RuntimeError(f"Failed to retrieve configuration identifier with error: {status_to_error(status)}")
|
110
|
+
|
111
|
+
@classmethod
|
112
|
+
def get_client_id (cls) -> str:
|
113
|
+
buffer = create_string_buffer(64)
|
114
|
+
status = get_fxnc().FXNConfigurationGetClientID(buffer, len(buffer))
|
115
|
+
if status == FXNStatus.OK:
|
116
|
+
return buffer.value.decode("utf-8")
|
117
|
+
else:
|
118
|
+
raise RuntimeError(f"Failed to retrieve client identifier with error: {status_to_error(status)}")
|
fxn-0.0.41/fxn/c/fxnc.py
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#
|
2
|
+
# Function
|
3
|
+
# Copyright © 2024 NatML Inc. All Rights Reserved.
|
4
|
+
#
|
5
|
+
|
6
|
+
from ctypes import CDLL
|
7
|
+
from enum import IntEnum
|
8
|
+
from importlib import resources
|
9
|
+
from platform import machine, system
|
10
|
+
|
11
|
+
_fxnc: CDLL = None
|
12
|
+
|
13
|
+
class FXNStatus(IntEnum):
|
14
|
+
OK = 0
|
15
|
+
ERROR_INVALID_ARGUMENT = 1
|
16
|
+
ERROR_INVALID_OPERATION = 2
|
17
|
+
ERROR_NOT_IMPLEMENTED = 3
|
18
|
+
|
19
|
+
def get_fxnc () -> CDLL:
|
20
|
+
global _fxnc
|
21
|
+
_fxnc = _fxnc if _fxnc is not None else _load_fxnc()
|
22
|
+
return _fxnc
|
23
|
+
|
24
|
+
def set_fxnc (fxnc: CDLL):
|
25
|
+
global _fxnc
|
26
|
+
_fxnc = fxnc
|
27
|
+
|
28
|
+
def _load_fxnc () -> CDLL:
|
29
|
+
os = system().lower()
|
30
|
+
os = "macos" if os == "darwin" else os
|
31
|
+
arch = machine().lower()
|
32
|
+
arch = "arm64" if arch == "aarch64" else arch
|
33
|
+
arch = "x86_64" if arch in ["x64", "amd64"] else arch
|
34
|
+
package = f"fxn.lib.{os}.{arch}"
|
35
|
+
resource = "libFunction.so"
|
36
|
+
resource = "Function.dylib" if os == "macos" else resource
|
37
|
+
resource = "Function.dll" if os == "windows" else resource
|
38
|
+
with resources.path(package, resource) as path:
|
39
|
+
return CDLL(str(path))
|
40
|
+
|
41
|
+
def status_to_error (status: int) -> str:
|
42
|
+
if status == FXNStatus.ERROR_INVALID_ARGUMENT:
|
43
|
+
return "FXN_ERROR_INVALID_ARGUMENT"
|
44
|
+
elif status == FXNStatus.ERROR_INVALID_OPERATION:
|
45
|
+
return "FXN_ERROR_INVALID_OPERATION"
|
46
|
+
elif status == FXNStatus.ERROR_NOT_IMPLEMENTED:
|
47
|
+
return "FXN_ERROR_NOT_IMPLEMENTED"
|
48
|
+
return ""
|
fxn-0.0.41/fxn/c/map.py
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
#
|
2
|
+
# Function
|
3
|
+
# Copyright © 2024 NatML Inc. All Rights Reserved.
|
4
|
+
#
|
5
|
+
|
6
|
+
from ctypes import byref, c_int, c_int32, c_void_p, create_string_buffer
|
7
|
+
from pathlib import Path
|
8
|
+
from typing import final
|
9
|
+
|
10
|
+
from .fxnc import get_fxnc, status_to_error, FXNStatus
|
11
|
+
from .value import Value
|
12
|
+
|
13
|
+
@final
|
14
|
+
class ValueMap:
|
15
|
+
|
16
|
+
def __init__ (self, map=None, *, owner: bool=True):
|
17
|
+
if map is None:
|
18
|
+
map = c_void_p()
|
19
|
+
owner = True
|
20
|
+
status = get_fxnc().FXNValueMapCreate(byref(map))
|
21
|
+
if status != FXNStatus.OK:
|
22
|
+
raise RuntimeError(f"Failed to create value map with error: {status_to_error(status)}")
|
23
|
+
self.__map = map
|
24
|
+
self.__owner = owner
|
25
|
+
|
26
|
+
def key (self, index: int) -> str:
|
27
|
+
buffer = create_string_buffer(256)
|
28
|
+
status = get_fxnc().FXNValueMapGetKey(self.__map, index, buffer, len(buffer))
|
29
|
+
if status == FXNStatus.OK:
|
30
|
+
return buffer.value.decode("utf-8")
|
31
|
+
else:
|
32
|
+
raise RuntimeError(f"Failed to get value map key at index {index} with error: {status_to_error(status)}")
|
33
|
+
|
34
|
+
def __getitem__ (self, key: str) -> Value | None:
|
35
|
+
value = c_void_p()
|
36
|
+
status = get_fxnc().FXNValueMapGetValue(self.__map, key.encode(), byref(value))
|
37
|
+
if status == FXNStatus.OK:
|
38
|
+
return Value(value, owner=False)
|
39
|
+
else:
|
40
|
+
raise RuntimeError(f"Failed to get value map value for key '{key}' with error: {status_to_error(status)}")
|
41
|
+
|
42
|
+
def __setitem__ (self, key: str, value: Value):
|
43
|
+
status = get_fxnc().FXNValueMapSetValue(self.__map, key.encode(), value._Value__value)
|
44
|
+
if status != FXNStatus.OK:
|
45
|
+
raise RuntimeError(f"Failed to set value map value for key '{key}' with error: {status_to_error(status)}")
|
46
|
+
|
47
|
+
def __len__ (self) -> int:
|
48
|
+
count = c_int32()
|
49
|
+
status = get_fxnc().FXNValueMapGetSize(self.__map, byref(count))
|
50
|
+
if status == FXNStatus.OK:
|
51
|
+
return count.value
|
52
|
+
else:
|
53
|
+
raise RuntimeError(f"Failed to get value map size with error: {status_to_error(status)}")
|
54
|
+
|
55
|
+
def __enter__ (self):
|
56
|
+
return self
|
57
|
+
|
58
|
+
def __exit__ (self, exc_type, exc_value, traceback):
|
59
|
+
self.__release()
|
60
|
+
|
61
|
+
def __release (self):
|
62
|
+
if self.__map and self.__owner:
|
63
|
+
get_fxnc().FXNValueMapRelease(self.__map)
|
64
|
+
self.__map = None
|
@@ -0,0 +1,76 @@
|
|
1
|
+
#
|
2
|
+
# Function
|
3
|
+
# Copyright © 2024 NatML Inc. All Rights Reserved.
|
4
|
+
#
|
5
|
+
|
6
|
+
from ctypes import byref, c_double, c_int, c_int32, c_void_p, create_string_buffer
|
7
|
+
from pathlib import Path
|
8
|
+
from typing import final
|
9
|
+
|
10
|
+
from .fxnc import get_fxnc, status_to_error, FXNStatus
|
11
|
+
from .map import ValueMap
|
12
|
+
|
13
|
+
@final
|
14
|
+
class Prediction:
|
15
|
+
|
16
|
+
def __init__ (self, prediction):
|
17
|
+
self.__prediction = prediction
|
18
|
+
|
19
|
+
@property
|
20
|
+
def id (self) -> str:
|
21
|
+
id = create_string_buffer(256)
|
22
|
+
status = get_fxnc().FXNPredictionGetID(self.__prediction, id, len(id))
|
23
|
+
if status == FXNStatus.OK:
|
24
|
+
return id.value.decode("utf-8")
|
25
|
+
else:
|
26
|
+
raise RuntimeError(f"Failed to get prediction id with error: {status_to_error(status)}")
|
27
|
+
|
28
|
+
@property
|
29
|
+
def latency (self) -> float:
|
30
|
+
latency = c_double()
|
31
|
+
status = get_fxnc().FXNPredictionGetLatency(self.__prediction, byref(latency))
|
32
|
+
if status == FXNStatus.OK:
|
33
|
+
return latency.value
|
34
|
+
else:
|
35
|
+
raise RuntimeError(f"Failed to get prediction latency with error: {status_to_error(status)}")
|
36
|
+
|
37
|
+
@property
|
38
|
+
def results (self) -> ValueMap | None:
|
39
|
+
map = c_void_p()
|
40
|
+
status = get_fxnc().FXNPredictionGetResults(self.__prediction, byref(map))
|
41
|
+
if status != FXNStatus.OK:
|
42
|
+
raise RuntimeError(f"Failed to get prediction results with error: {status_to_error(status)}")
|
43
|
+
map = ValueMap(map, owner=False)
|
44
|
+
return map if len(map) > 0 else None
|
45
|
+
|
46
|
+
@property
|
47
|
+
def error (self) -> str | None:
|
48
|
+
error = create_string_buffer(2048)
|
49
|
+
get_fxnc().FXNPredictionGetError(self.__prediction, error, len(error))
|
50
|
+
error = error.value.decode("utf-8")
|
51
|
+
return error if error else None
|
52
|
+
|
53
|
+
@property
|
54
|
+
def logs (self) -> str:
|
55
|
+
fxnc = get_fxnc()
|
56
|
+
log_length = c_int32()
|
57
|
+
status = fxnc.FXNPredictionGetLogLength(self.__prediction, byref(log_length))
|
58
|
+
if status != FXNStatus.OK:
|
59
|
+
raise RuntimeError(f"Failed to get prediction log length with error: {status_to_error(status)}")
|
60
|
+
logs = create_string_buffer(log_length.value + 1)
|
61
|
+
status = fxnc.FXNPredictionGetLogs(self.__prediction, logs, len(logs))
|
62
|
+
if status == FXNStatus.OK:
|
63
|
+
return logs.value.decode("utf-8")
|
64
|
+
else:
|
65
|
+
raise RuntimeError(f"Failed to get prediction logs with error: {status_to_error(status)}")
|
66
|
+
|
67
|
+
def __enter__ (self):
|
68
|
+
return self
|
69
|
+
|
70
|
+
def __exit__ (self, exc_type, exc_value, traceback):
|
71
|
+
self.__release()
|
72
|
+
|
73
|
+
def __release (self):
|
74
|
+
if self.__prediction:
|
75
|
+
get_fxnc().FXNPredictionRelease(self.__prediction)
|
76
|
+
self.__prediction = None
|
@@ -0,0 +1,60 @@
|
|
1
|
+
#
|
2
|
+
# Function
|
3
|
+
# Copyright © 2024 NatML Inc. All Rights Reserved.
|
4
|
+
#
|
5
|
+
|
6
|
+
from ctypes import byref, c_int, c_int32, c_void_p, create_string_buffer
|
7
|
+
from pathlib import Path
|
8
|
+
from typing import final
|
9
|
+
|
10
|
+
from .configuration import Configuration
|
11
|
+
from .fxnc import get_fxnc, status_to_error, FXNStatus
|
12
|
+
from .map import ValueMap
|
13
|
+
from .prediction import Prediction
|
14
|
+
from .stream import PredictionStream
|
15
|
+
|
16
|
+
@final
|
17
|
+
class Predictor:
|
18
|
+
|
19
|
+
def __init__ (self, configuration: Configuration):
|
20
|
+
predictor = c_void_p()
|
21
|
+
status = get_fxnc().FXNPredictorCreate(configuration._Configuration__configuration, byref(predictor))
|
22
|
+
if status == FXNStatus.OK:
|
23
|
+
self.__predictor = predictor
|
24
|
+
else:
|
25
|
+
raise RuntimeError(f"Failed to create predictor with error: {status_to_error(status)}")
|
26
|
+
|
27
|
+
def create_prediction (self, inputs: ValueMap) -> Prediction:
|
28
|
+
prediction = c_void_p()
|
29
|
+
status = get_fxnc().FXNPredictorCreatePrediction(
|
30
|
+
self.__predictor,
|
31
|
+
inputs._ValueMap__map,
|
32
|
+
byref(prediction)
|
33
|
+
)
|
34
|
+
if status == FXNStatus.OK:
|
35
|
+
return Prediction(prediction)
|
36
|
+
else:
|
37
|
+
raise RuntimeError(f"Failed to create prediction with error: {status_to_error(status)}")
|
38
|
+
|
39
|
+
def stream_prediction (self, inputs: ValueMap) -> PredictionStream:
|
40
|
+
stream = c_void_p()
|
41
|
+
status = get_fxnc().FXNPredictorStreamPrediction(
|
42
|
+
self.__predictor,
|
43
|
+
inputs._ValueMap__map,
|
44
|
+
byref(stream)
|
45
|
+
)
|
46
|
+
if status == FXNStatus.OK:
|
47
|
+
return PredictionStream(stream)
|
48
|
+
else:
|
49
|
+
raise RuntimeError(f"Failed to stream prediction with error: {status_to_error(status)}")
|
50
|
+
|
51
|
+
def __enter__ (self):
|
52
|
+
return self
|
53
|
+
|
54
|
+
def __exit__ (self, exc_type, exc_value, traceback):
|
55
|
+
self.__release()
|
56
|
+
|
57
|
+
def __release (self):
|
58
|
+
if self.__predictor:
|
59
|
+
get_fxnc().FXNPredictorRelease(self.__predictor)
|
60
|
+
self.__predictor = None
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#
|
2
|
+
# Function
|
3
|
+
# Copyright © 2024 NatML Inc. All Rights Reserved.
|
4
|
+
#
|
5
|
+
|
6
|
+
from ctypes import byref, c_int, c_int32, c_void_p, create_string_buffer
|
7
|
+
from pathlib import Path
|
8
|
+
from typing import final
|
9
|
+
|
10
|
+
from .fxnc import get_fxnc, status_to_error, FXNStatus
|
11
|
+
from .prediction import Prediction
|
12
|
+
|
13
|
+
@final
|
14
|
+
class PredictionStream:
|
15
|
+
|
16
|
+
def __init__ (self, stream):
|
17
|
+
self.__stream = stream
|
18
|
+
|
19
|
+
def __iter__ (self):
|
20
|
+
return self
|
21
|
+
|
22
|
+
def __next__ (self) -> Prediction:
|
23
|
+
prediction = c_void_p()
|
24
|
+
status = get_fxnc().FXNPredictionStreamReadNext(self.__stream, byref(prediction))
|
25
|
+
if status == FXNStatus.ERROR_INVALID_OPERATION:
|
26
|
+
raise StopIteration()
|
27
|
+
elif status != FXNStatus.OK:
|
28
|
+
raise RuntimeError(f"Failed to read next prediction in stream with error: {status_to_error(status)}")
|
29
|
+
else:
|
30
|
+
return Prediction(prediction)
|
31
|
+
|
32
|
+
def __enter__ (self):
|
33
|
+
return self
|
34
|
+
|
35
|
+
def __exit__ (self, exc_type, exc_value, traceback):
|
36
|
+
self.__release()
|
37
|
+
|
38
|
+
def __release (self):
|
39
|
+
if self.__stream:
|
40
|
+
get_fxnc().FXNPredictionStreamRelease(self.__stream)
|
41
|
+
self.__stream = None
|
@@ -0,0 +1,223 @@
|
|
1
|
+
#
|
2
|
+
# Function
|
3
|
+
# Copyright © 2024 NatML Inc. All Rights Reserved.
|
4
|
+
#
|
5
|
+
|
6
|
+
from __future__ import annotations
|
7
|
+
from collections.abc import Iterable
|
8
|
+
from enum import IntFlag
|
9
|
+
from ctypes import byref, cast, c_char_p, c_int, c_int32, c_uint8, c_void_p, string_at, POINTER
|
10
|
+
from io import BytesIO
|
11
|
+
from json import dumps, loads
|
12
|
+
from numpy import array, dtype, int32, ndarray, zeros
|
13
|
+
from numpy.ctypeslib import as_array, as_ctypes_type
|
14
|
+
from PIL import Image
|
15
|
+
from typing import final, Any
|
16
|
+
|
17
|
+
from ..types import Dtype
|
18
|
+
from .fxnc import get_fxnc, status_to_error, FXNStatus
|
19
|
+
|
20
|
+
class ValueFlags (IntFlag):
|
21
|
+
NONE = 0
|
22
|
+
COPY_DATA = 1
|
23
|
+
|
24
|
+
@final
|
25
|
+
class Value:
|
26
|
+
|
27
|
+
def __init__ (self, value, *, owner: bool=True):
|
28
|
+
self.__value = value
|
29
|
+
self.__owner = owner
|
30
|
+
|
31
|
+
@property
|
32
|
+
def data (self):
|
33
|
+
data = c_void_p()
|
34
|
+
status = get_fxnc().FXNValueGetData(self.__value, byref(data))
|
35
|
+
if status == FXNStatus.OK:
|
36
|
+
return data
|
37
|
+
else:
|
38
|
+
raise RuntimeError(f"Failed to get value data with error: {status_to_error(status)}")
|
39
|
+
|
40
|
+
@property
|
41
|
+
def type (self) -> Dtype:
|
42
|
+
dtype = c_int()
|
43
|
+
status = get_fxnc().FXNValueGetType(self.__value, byref(dtype))
|
44
|
+
if status == FXNStatus.OK:
|
45
|
+
return _DTYPE_TO_STR.get(dtype.value)
|
46
|
+
else:
|
47
|
+
raise RuntimeError(f"Failed to get value data type with error: {status_to_error(status)}")
|
48
|
+
|
49
|
+
@property
|
50
|
+
def shape (self) -> list[int] | None:
|
51
|
+
if self.type not in _TENSOR_DTYPES:
|
52
|
+
return None
|
53
|
+
fxnc = get_fxnc()
|
54
|
+
dims = c_int32()
|
55
|
+
status = fxnc.FXNValueGetDimensions(self.__value, byref(dims))
|
56
|
+
if status != FXNStatus.OK:
|
57
|
+
raise RuntimeError(f"Failed to get value dimensions with error: {status_to_error(status)}")
|
58
|
+
shape = zeros(dims.value, dtype=int32)
|
59
|
+
status = fxnc.FXNValueGetShape(self.__value, shape.ctypes.data_as(POINTER(c_int32)), dims)
|
60
|
+
if status == FXNStatus.OK:
|
61
|
+
return shape.tolist()
|
62
|
+
else:
|
63
|
+
raise RuntimeError(f"Failed to get value shape with error: {status_to_error(status)}")
|
64
|
+
|
65
|
+
def to_object (self) -> Any:
|
66
|
+
type = self.type
|
67
|
+
if type == Dtype.null:
|
68
|
+
return None
|
69
|
+
elif type in _TENSOR_DTYPES:
|
70
|
+
ctype = as_ctypes_type(dtype(type))
|
71
|
+
tensor = as_array(cast(self.data, POINTER(ctype)), self.shape)
|
72
|
+
return tensor.item() if len(tensor.shape) == 0 else tensor.copy()
|
73
|
+
elif type == Dtype.string:
|
74
|
+
return cast(self.data, c_char_p).value.decode()
|
75
|
+
elif type in [Dtype.list, Dtype.dict]:
|
76
|
+
return loads(cast(self.data, c_char_p).value.decode())
|
77
|
+
elif type == Dtype.image:
|
78
|
+
pixel_buffer = as_array(cast(self.data, POINTER(c_uint8)), self.shape)
|
79
|
+
return Image.fromarray(pixel_buffer.squeeze()).copy()
|
80
|
+
elif type == Dtype.binary:
|
81
|
+
return BytesIO(string_at(self.data, self.shape[0]))
|
82
|
+
else:
|
83
|
+
raise RuntimeError(f"Failed to convert Function value to object because value has unsupported type: {type}")
|
84
|
+
|
85
|
+
def __enter__ (self):
|
86
|
+
return self
|
87
|
+
|
88
|
+
def __exit__ (self, exc_type, exc_value, traceback):
|
89
|
+
self.__release()
|
90
|
+
|
91
|
+
def __release (self):
|
92
|
+
if self.__value and self.__owner:
|
93
|
+
get_fxnc().FXNValueRelease(self.__value)
|
94
|
+
self.__value = None
|
95
|
+
|
96
|
+
@classmethod
|
97
|
+
def create_array (
|
98
|
+
cls,
|
99
|
+
data: ndarray,
|
100
|
+
*,
|
101
|
+
flags: ValueFlags=ValueFlags.NONE
|
102
|
+
) -> Value:
|
103
|
+
dtype = _STR_TO_DTYPE.get(data.dtype.name)
|
104
|
+
if dtype is None:
|
105
|
+
raise RuntimeError(f"Failed to create array value because data type is not supported: {data.dtype}")
|
106
|
+
value = c_void_p()
|
107
|
+
status = get_fxnc().FXNValueCreateArray(
|
108
|
+
data.ctypes.data_as(c_void_p),
|
109
|
+
data.ctypes.shape_as(c_int32),
|
110
|
+
len(data.shape),
|
111
|
+
dtype,
|
112
|
+
flags,
|
113
|
+
byref(value)
|
114
|
+
)
|
115
|
+
if status == FXNStatus.OK:
|
116
|
+
return Value(value)
|
117
|
+
else:
|
118
|
+
raise RuntimeError(f"Failed to create array value with error: {status_to_error(status)}")
|
119
|
+
|
120
|
+
@classmethod
|
121
|
+
def create_string (cls, data: str) -> Value:
|
122
|
+
value = c_void_p()
|
123
|
+
status = get_fxnc().FXNValueCreateString(data.encode(), byref(value))
|
124
|
+
if status == FXNStatus.OK:
|
125
|
+
return Value(value)
|
126
|
+
else:
|
127
|
+
raise RuntimeError(f"Failed to create string value with error: {status_to_error(status)}")
|
128
|
+
|
129
|
+
@classmethod
|
130
|
+
def create_list (cls, data: Iterable[Any]) -> Value:
|
131
|
+
value = c_void_p()
|
132
|
+
status = get_fxnc().FXNValueCreateList(dumps(data).encode(), byref(value))
|
133
|
+
if status == FXNStatus.OK:
|
134
|
+
return Value(value)
|
135
|
+
else:
|
136
|
+
raise RuntimeError(f"Failed to create list value with error: {status_to_error(status)}")
|
137
|
+
|
138
|
+
@classmethod
|
139
|
+
def create_dict (cls, data: dict[str, Any]) -> Value:
|
140
|
+
value = c_void_p()
|
141
|
+
status = get_fxnc().FXNValueCreateDict(dumps(data).encode(), byref(value))
|
142
|
+
if status == FXNStatus.OK:
|
143
|
+
return Value(value)
|
144
|
+
else:
|
145
|
+
raise RuntimeError(f"Failed to create dict value with error: {status_to_error(status)}")
|
146
|
+
|
147
|
+
@classmethod
|
148
|
+
def create_image (cls, image: Image.Image) -> Value:
|
149
|
+
value = c_void_p()
|
150
|
+
pixel_buffer = array(image)
|
151
|
+
status = get_fxnc().FXNValueCreateImage(
|
152
|
+
pixel_buffer.ctypes.data_as(c_void_p),
|
153
|
+
image.width,
|
154
|
+
image.height,
|
155
|
+
pixel_buffer.shape[2],
|
156
|
+
ValueFlags.COPY_DATA,
|
157
|
+
byref(value)
|
158
|
+
)
|
159
|
+
if status == FXNStatus.OK:
|
160
|
+
return Value(value)
|
161
|
+
else:
|
162
|
+
raise RuntimeError(f"Failed to create image value with error: {status_to_error(status)}")
|
163
|
+
|
164
|
+
@classmethod
|
165
|
+
def create_binary (
|
166
|
+
cls,
|
167
|
+
data: memoryview,
|
168
|
+
*,
|
169
|
+
flags: ValueFlags=ValueFlags.NONE
|
170
|
+
) -> Value:
|
171
|
+
buffer = (c_uint8 * len(data)).from_buffer(data)
|
172
|
+
value = c_void_p()
|
173
|
+
status = get_fxnc().FXNValueCreateBinary(buffer, len(data), flags, byref(value))
|
174
|
+
if status == FXNStatus.OK:
|
175
|
+
return Value(value)
|
176
|
+
else:
|
177
|
+
raise RuntimeError(f"Failed to create binary value with error: {status_to_error(status)}")
|
178
|
+
|
179
|
+
@classmethod
|
180
|
+
def create_null (cls) -> Value:
|
181
|
+
value = c_void_p()
|
182
|
+
status = get_fxnc().FXNValueCreateNull(byref(value))
|
183
|
+
if status == FXNStatus.OK:
|
184
|
+
return Value(value)
|
185
|
+
else:
|
186
|
+
raise RuntimeError(f"Failed to create null value with error: {status_to_error(status)}")
|
187
|
+
|
188
|
+
|
189
|
+
_STR_TO_DTYPE = {
|
190
|
+
Dtype.null: 0,
|
191
|
+
Dtype.float16: 1,
|
192
|
+
Dtype.float32: 2,
|
193
|
+
Dtype.float64: 3,
|
194
|
+
Dtype.int8: 4,
|
195
|
+
Dtype.int16: 5,
|
196
|
+
Dtype.int32: 6,
|
197
|
+
Dtype.int64: 7,
|
198
|
+
Dtype.uint8: 8,
|
199
|
+
Dtype.uint16: 9,
|
200
|
+
Dtype.uint32: 10,
|
201
|
+
Dtype.uint64: 11,
|
202
|
+
Dtype.bool: 12,
|
203
|
+
Dtype.string: 13,
|
204
|
+
Dtype.list: 14,
|
205
|
+
Dtype.dict: 15,
|
206
|
+
Dtype.image: 16,
|
207
|
+
Dtype.binary: 17,
|
208
|
+
}
|
209
|
+
_DTYPE_TO_STR = { value: key for key, value in _STR_TO_DTYPE.items() }
|
210
|
+
_TENSOR_DTYPES = {
|
211
|
+
Dtype.float16,
|
212
|
+
Dtype.float32,
|
213
|
+
Dtype.float64,
|
214
|
+
Dtype.int8,
|
215
|
+
Dtype.int16,
|
216
|
+
Dtype.int32,
|
217
|
+
Dtype.int64,
|
218
|
+
Dtype.uint8,
|
219
|
+
Dtype.uint16,
|
220
|
+
Dtype.uint32,
|
221
|
+
Dtype.uint64,
|
222
|
+
Dtype.bool,
|
223
|
+
}
|