fxn 0.0.35__py3-none-any.whl → 0.0.36__py3-none-any.whl

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.
Files changed (48) hide show
  1. fxn/c/__init__.py +16 -0
  2. fxn/c/configuration.py +60 -0
  3. fxn/c/dtype.py +26 -0
  4. fxn/c/fxnc.py +28 -0
  5. fxn/c/map.py +34 -0
  6. fxn/c/prediction.py +37 -0
  7. fxn/c/predictor.py +31 -0
  8. fxn/c/status.py +12 -0
  9. fxn/c/stream.py +22 -0
  10. fxn/c/value.py +50 -0
  11. fxn/c/version.py +13 -0
  12. fxn/cli/__init__.py +8 -8
  13. fxn/cli/auth.py +1 -1
  14. fxn/cli/predict.py +3 -4
  15. fxn/cli/predictors.py +1 -40
  16. fxn/function.py +10 -11
  17. fxn/lib/macos/arm64/Function.dylib +0 -0
  18. fxn/lib/macos/x86_64/Function.dylib +0 -0
  19. fxn/lib/windows/arm64/Function.dll +0 -0
  20. fxn/lib/windows/x86_64/Function.dll +0 -0
  21. fxn/services/environment.py +1 -1
  22. fxn/services/prediction.py +456 -0
  23. fxn/services/predictor.py +3 -70
  24. fxn/services/storage.py +3 -4
  25. fxn/services/user.py +1 -1
  26. fxn/types/__init__.py +2 -3
  27. fxn/types/prediction.py +0 -4
  28. fxn/types/predictor.py +15 -22
  29. fxn/version.py +1 -1
  30. {fxn-0.0.35.dist-info → fxn-0.0.36.dist-info}/METADATA +27 -29
  31. fxn-0.0.36.dist-info/RECORD +49 -0
  32. {fxn-0.0.35.dist-info → fxn-0.0.36.dist-info}/WHEEL +1 -1
  33. fxn/libs/linux/__init__.py +0 -4
  34. fxn/libs/macos/Function.dylib +0 -0
  35. fxn/libs/macos/__init__.py +0 -4
  36. fxn/libs/windows/Function.dll +0 -0
  37. fxn/libs/windows/__init__.py +0 -4
  38. fxn/services/prediction/__init__.py +0 -6
  39. fxn/services/prediction/fxnc.py +0 -312
  40. fxn/services/prediction/service.py +0 -512
  41. fxn/types/value.py +0 -22
  42. fxn-0.0.35.dist-info/RECORD +0 -42
  43. /fxn/{graph → api}/__init__.py +0 -0
  44. /fxn/{graph → api}/client.py +0 -0
  45. /fxn/{libs → lib}/__init__.py +0 -0
  46. {fxn-0.0.35.dist-info → fxn-0.0.36.dist-info}/LICENSE +0 -0
  47. {fxn-0.0.35.dist-info → fxn-0.0.36.dist-info}/entry_points.txt +0 -0
  48. {fxn-0.0.35.dist-info → fxn-0.0.36.dist-info}/top_level.txt +0 -0
fxn/types/predictor.py CHANGED
@@ -3,21 +3,24 @@
3
3
  # Copyright © 2024 NatML Inc. All Rights Reserved.
4
4
  #
5
5
 
6
- from enum import Enum
7
- from pydantic import AliasChoices, BaseModel, Field
8
- from typing import List, Optional, Tuple, Union
6
+ from enum import Enum, IntFlag
7
+ from io import BytesIO
8
+ from numpy.typing import NDArray
9
+ from PIL import Image
10
+ from pydantic import AliasChoices, BaseModel, ConfigDict, Field
11
+ from typing import Any, Dict, List, Optional, Tuple
9
12
 
10
13
  from .dtype import Dtype
11
14
  from .profile import Profile
12
- from .value import Value
13
15
 
14
- class Acceleration (str, Enum):
16
+ class Acceleration (IntFlag):
15
17
  """
16
18
  Predictor acceleration.
17
19
  """
18
- CPU = "CPU"
19
- A40 = "A40"
20
- A100 = "A100"
20
+ Default = 0,
21
+ CPU = 1 << 0,
22
+ GPU = 1 << 1,
23
+ NPU = 1 << 2
21
24
 
22
25
  class AccessMode (str, Enum):
23
26
  """
@@ -26,13 +29,6 @@ class AccessMode (str, Enum):
26
29
  Public = "PUBLIC"
27
30
  Private = "PRIVATE"
28
31
 
29
- class PredictorType (str, Enum):
30
- """
31
- Predictor type.
32
- """
33
- Cloud = "CLOUD"
34
- Edge = "EDGE"
35
-
36
32
  class PredictorStatus (str, Enum):
37
33
  """
38
34
  Predictor status.
@@ -51,7 +47,7 @@ class EnumerationMember (BaseModel):
51
47
  value (str | int): Enumeration member value.
52
48
  """
53
49
  name: str = Field(description="Enumeration member name.")
54
- value: Union[str, int] = Field(description="Enumeration member value.")
50
+ value: str | int = Field(description="Enumeration member value.")
55
51
 
56
52
  class Parameter (BaseModel):
57
53
  """
@@ -64,7 +60,7 @@ class Parameter (BaseModel):
64
60
  optional (bool): Whether the parameter is optional.
65
61
  range (tuple): Parameter value range for numeric parameters.
66
62
  enumeration (list): Parameter value choices for enumeration parameters.
67
- default_value (Value): Parameter default value.
63
+ default_value (str | float | int | bool | ndarray | list | dict | PIL.Image | BytesIO): Parameter default value.
68
64
  value_schema (dict): Parameter JSON schema. This is only populated for `list` and `dict` parameters.
69
65
  """
70
66
  name: str = Field(description="Parameter name.")
@@ -73,8 +69,9 @@ class Parameter (BaseModel):
73
69
  optional: Optional[bool] = Field(default=None, description="Whether the parameter is optional.")
74
70
  range: Optional[Tuple[float, float]] = Field(default=None, description="Parameter value range for numeric parameters.")
75
71
  enumeration: Optional[List[EnumerationMember]] = Field(default=None, description="Parameter value choices for enumeration parameters.")
76
- default_value: Optional[Value] = Field(default=None, description="Parameter default value.", serialization_alias="defaultValue", validation_alias=AliasChoices("default_value", "defaultValue"))
72
+ default_value: Optional[str | float | int | bool | NDArray | List[Any] | Dict[str, Any] | Image.Image | BytesIO] = Field(default=None, description="Parameter default value.", serialization_alias="defaultValue", validation_alias=AliasChoices("default_value", "defaultValue"))
77
73
  value_schema: Optional[dict] = Field(default=None, description="Parameter JSON schema. This is only populated for `list` and `dict` parameters.", serialization_alias="schema", validation_alias=AliasChoices("schema", "value_schema"))
74
+ model_config = ConfigDict(arbitrary_types_allowed=True)
78
75
 
79
76
  class Signature (BaseModel):
80
77
  """
@@ -95,7 +92,6 @@ class Predictor (BaseModel):
95
92
  tag (str): Predictor tag.
96
93
  owner (Profile): Predictor owner.
97
94
  name (str): Predictor name.
98
- type (PredictorType): Predictor type.
99
95
  status (PredictorStatus): Predictor status.
100
96
  access (AccessMode): Predictor access.
101
97
  signature (Signature): Predictor signature.
@@ -103,13 +99,11 @@ class Predictor (BaseModel):
103
99
  description (str): Predictor description.
104
100
  card (str): Predictor card.
105
101
  media (str): Predictor media URL.
106
- acceleration (Acceleration): Predictor acceleration. This only applies to cloud predictors.
107
102
  license (str): Predictor license URL.
108
103
  """
109
104
  tag: str = Field(description="Predictor tag.")
110
105
  owner: Profile = Field(description="Predictor owner.")
111
106
  name: str = Field(description="Predictor name.")
112
- type: PredictorType = Field(description="Predictor type.")
113
107
  status: PredictorStatus = Field(description="Predictor status.")
114
108
  access: AccessMode = Field(description="Predictor access.")
115
109
  signature: Signature = Field(description="Predictor signature.")
@@ -117,5 +111,4 @@ class Predictor (BaseModel):
117
111
  description: Optional[str] = Field(default=None, description="Predictor description.")
118
112
  card: Optional[str] = Field(default=None, description="Predictor card.")
119
113
  media: Optional[str] = Field(default=None, description="Predictor media URL.")
120
- acceleration: Optional[Acceleration] = Field(default=None, description="Predictor acceleration. This only applies to cloud predictors.")
121
114
  license: Optional[str] = Field(default=None, description="Predictor license URL.")
fxn/version.py CHANGED
@@ -3,4 +3,4 @@
3
3
  # Copyright © 2024 NatML Inc. All Rights Reserved.
4
4
  #
5
5
 
6
- __version__ = "0.0.35"
6
+ __version__ = "0.0.36"
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fxn
3
- Version: 0.0.35
4
- Summary: Run on-device and cloud AI prediction functions in Python. Register at https://fxn.ai.
3
+ Version: 0.0.36
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
7
7
  Version 2.0, January 2004
@@ -213,14 +213,12 @@ Classifier: License :: OSI Approved :: Apache Software License
213
213
  Classifier: Operating System :: OS Independent
214
214
  Classifier: Topic :: Scientific/Engineering :: Image Recognition
215
215
  Classifier: Topic :: Software Development :: Libraries
216
- Requires-Python: >=3.9
216
+ Requires-Python: >=3.10
217
217
  Description-Content-Type: text/markdown
218
218
  License-File: LICENSE
219
- Requires-Dist: aiohttp
220
- Requires-Dist: magika
221
219
  Requires-Dist: numpy
222
220
  Requires-Dist: pillow
223
- Requires-Dist: pydantic >=2.0
221
+ Requires-Dist: pydantic>=2.0
224
222
  Requires-Dist: requests
225
223
  Requires-Dist: rich
226
224
  Requires-Dist: typer
@@ -231,52 +229,52 @@ Requires-Dist: typer
231
229
 
232
230
  [![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fdiscord.com%2Fapi%2Finvites%2Fy5vwgXkz2f%3Fwith_counts%3Dtrue&query=%24.approximate_member_count&logo=discord&logoColor=white&label=Function%20community)](https://fxn.ai/community)
233
231
 
234
- Run AI prediction functions (a.k.a "predictors") in your Python apps. With Function, you can build AI-powered apps by creating and composing GPU-accelerated predictors that run in the cloud. In a few steps:
232
+ Run prediction functions (a.k.a "predictors") locally in your Python apps, with full GPU acceleration and zero dependencies. In a few steps:
235
233
 
236
234
  ## Installing Function
237
235
  Function is distributed on PyPi. This distribution contains both the Python client and the command line interface (CLI). To install, open a terminal and run the following command:
238
236
  ```sh
239
- pip install --upgrade fxn
237
+ # Install Function
238
+ $ pip install --upgrade fxn
240
239
  ```
241
240
 
242
241
  > [!NOTE]
243
242
  > Function requires Python 3.9+
244
243
 
245
- ## Making a Prediction
246
- Let's run the [`@samplefxn/stable-diffusion`](https://fxn.ai/@samplefxn/stable-diffusion) predictor which accepts a text `prompt` and generates a corresponding image.
244
+ ## Retrieving your Access Key
245
+ Head over to [fxn.ai](https://fxn.ai) to create an account by logging in. Once you do, generate an access key:
246
+
247
+ ![generate access key](https://raw.githubusercontent.com/fxnai/.github/main/access_key.gif)
247
248
 
248
- ### In Python
249
- Run the following Python script:
249
+ ## Making a Prediction
250
+ Let's run the [`@fxn/greeting`](https://fxn.ai/@fxn/greeting) predictor which accepts a `name` and returns a congenial greeting. Run the following Python script:
250
251
  ```py
251
252
  from fxn import Function
252
253
 
253
254
  # Create the Function client
254
- fxn = Function()
255
+ fxn = Function(access_key="<Function access key>")
255
256
  # Create a prediction
256
257
  prediction = fxn.predictions.create(
257
- tag="@samplefxn/stable-diffusion",
258
- inputs={
259
- "prompt": "An astronaut riding a horse on Mars"
260
- }
258
+ tag="@fxn/greeting",
259
+ inputs={ "name": "Peter" }
261
260
  )
262
- # Show the generated image
263
- image = prediction.results[0]
264
- image.show()
261
+ # Print the returned greeting
262
+ print(prediction.results[0])
265
263
  ```
266
264
 
267
- ### In the CLI
265
+ > [!TIP]
266
+ > Explore public predictors [on Function](https://fxn.ai/explore) or [create your own](https://fxn.ai/waitlist).
267
+ r
268
+ ## Using the Function CLI
268
269
  Open up a terminal and run the following command:
269
270
 
270
271
  ```sh
271
- fxn predict @samplefxn/stable-diffusion --prompt "An astronaut riding a horse on the moon"
272
- ```
272
+ # Login to Function
273
+ fxn auth login <ACCESS KEY>
273
274
 
274
- Within a few seconds, you should see a creepy-looking image pop up 😅:
275
-
276
- ![prediction](https://raw.githubusercontent.com/fxnai/.github/main/predict.gif)
277
-
278
- ## Creating a Predictor
279
- At some point, you might want to create your own predictor. With Function, you don't have to deal with GitHub repos, Dockerfiles, or weird YAMLs. All you need is a Jupyter Notebook with a `predict` function. See our [samples project](https://github.com/fxnai/samples) for more.
275
+ # Make a prediction using the Function CLI
276
+ fxn predict @fxn/greeting --name Peter
277
+ ```
280
278
 
281
279
  ___
282
280
 
@@ -0,0 +1,49 @@
1
+ fxn/__init__.py,sha256=tWk0-aCNHX_yCS-Dg90pYnniNka9MWFoNMk6xY7u4nI,157
2
+ fxn/function.py,sha256=NBEH37NX6GFj6pf3nBja8xeYb1IoKCPiE4Sxwpwj-mM,1452
3
+ fxn/magic.py,sha256=PQmXhO9EvJ5EZylioV-6gsCvqhVRYscKBSOBoN4VhTk,1041
4
+ fxn/version.py,sha256=BiY9-f6E3ZNjYc2X4h8op-HBbBeHAEepyx3H_ePXT58,95
5
+ fxn/api/__init__.py,sha256=rJIDBhYg5jcrWO4hT4-CpwPq6dSgmLTEHCfUYTLpVaI,103
6
+ fxn/api/client.py,sha256=WCNsebcuwIlP9W5k_8AQCpxOCcy7cpbengfu2rIkGmc,1192
7
+ fxn/c/__init__.py,sha256=qBnS4_eoBe5biKoyhW0Fsfr3cqJ9SAvzmm-XB56ev0A,438
8
+ fxn/c/configuration.py,sha256=BU6WRaYVCjdCSTESNqZX9ghvA19l1yLUCn5EhChQTpA,2707
9
+ fxn/c/dtype.py,sha256=jcbFpQSnpvMPwXQ3mVyTZRSlJxcaArM2cvgXwAqzx0Q,386
10
+ fxn/c/fxnc.py,sha256=kaEWw_bmDVj55IcSJR8sPmmEE2-DwTAt-MW6h3p8aIU,859
11
+ fxn/c/map.py,sha256=IXYPGp4KEmgoO7v-EXJNDBVDm8qmMEuDDYZR1SCKFCM,1234
12
+ fxn/c/prediction.py,sha256=foPETcJUGn8QxBbub0O7EBPA8G3i9XNdwV22iB1Rm-A,1476
13
+ fxn/c/predictor.py,sha256=sqJo7BMnl_tVUlZx5tNy5IwoxK8bx-sHJzgYtSJkQR0,1172
14
+ fxn/c/status.py,sha256=QodZjS0pVI5NIBw5x2OobJLV44VVNhB0UNKmOI9yuAk,225
15
+ fxn/c/stream.py,sha256=F2R9eUPW3fvUzb7KBfRCk9KiGFl_Yhdf0gqEzM06RKs,725
16
+ fxn/c/value.py,sha256=FZbEaeJYBxdOIVOPR4n0wJ1G_8I0RZ6dRFPcVsDLYbU,1931
17
+ fxn/c/version.py,sha256=fnV14LTAXSl6Q03YFIWOINOUrs0DJpyBOcwP5VZvLFc,282
18
+ fxn/cli/__init__.py,sha256=c9qBvRTHSWnO-8Ny431RlkZWznF7sShJfHYfjBazjRk,1481
19
+ fxn/cli/auth.py,sha256=tMjgNA6AQeom3I5AsgPvWvN62cM3nFGBcjjXB7x4s8U,1688
20
+ fxn/cli/env.py,sha256=shqoP4tUiXdOoil73oiUYpqGeVcR119HPYFKgnoF894,1553
21
+ fxn/cli/misc.py,sha256=J3WgNjrxRzm-_iKC3Cp0o4VHeBYBQ1ta_t2-ozD9roo,662
22
+ fxn/cli/predict.py,sha256=hpd1VZtw1OBNkZwePCFtyTHLSDOzc8fuSODGmvmmG1E,3026
23
+ fxn/cli/predictors.py,sha256=SSvxf5emr_upcU78vhdB2F9PM351YdP2a1bqf8FRaxE,2248
24
+ fxn/lib/__init__.py,sha256=c_q01PLV3Mi-qV0_HVbNRHOI2TIUr_cDIJHvCASsYZk,71
25
+ fxn/lib/macos/arm64/Function.dylib,sha256=N-ewU2AXqgVAsYPpEnlWU09uyxLAywtsL0ous4MG3Zg,268736
26
+ fxn/lib/macos/x86_64/Function.dylib,sha256=DfoGF8v-lvxo8a10QUZ0egbfQZo6B6tNct5NMV1kpYc,269744
27
+ fxn/lib/windows/arm64/Function.dll,sha256=kvixEsiKalAqMOyCRpcAcFgkXxK_9Vt-WDQgws5M6Xg,425472
28
+ fxn/lib/windows/x86_64/Function.dll,sha256=lEHVER_6PQUqJHDd6YWMqZ2X6PLnJTmVa68cUgp5lJ8,450560
29
+ fxn/services/__init__.py,sha256=OTBRL_wH94hc_scZgRd42VrJQfldNLjv4APN4YaWBAw,366
30
+ fxn/services/environment.py,sha256=ITodddygFR9Kw8cdSh3vtTcTpUmUIsTFGpHnqx2RMVo,3714
31
+ fxn/services/prediction.py,sha256=EfjnwF_GEKSifClzsBWBuKe-NeSk9CeAMQAYDT0lF9A,20228
32
+ fxn/services/predictor.py,sha256=yhrg5msrfVsIomPB1jlOa0z7jVGTDZIdvBnB-OYiQdk,5163
33
+ fxn/services/storage.py,sha256=qGijBsfdGWQAumNAOYL6-SfSkrvYDuCK12FevdJl4cM,5480
34
+ fxn/services/user.py,sha256=niExZwZFl_hlgLTz9lvrU3QrVR8oKdMcm623wyQK-cA,1217
35
+ fxn/types/__init__.py,sha256=GY7ST0Ue_sh4rB3a4MHkvql_Cn0p9qZ4wlf3luPOwk8,398
36
+ fxn/types/dtype.py,sha256=YpTnIG-yzrQwda27GzfGZcel-zF3gOMMoHhcWD915BY,617
37
+ fxn/types/environment.py,sha256=FbmfGjSb5yYMT9IyDj8zNUpsoP3RbzqM6tK8gn2TfDs,394
38
+ fxn/types/prediction.py,sha256=y54TA0w16MVW2jUwN6U-BtXARZgyk2WXy2mHDf9Hsjw,2028
39
+ fxn/types/predictor.py,sha256=bqpt3m616JloxkcIGxUKP1_-7ioLXepe8DjgfEPzj-E,4699
40
+ fxn/types/profile.py,sha256=1KWqPusKieCIcw4KSS2sScpwP0Z-mU4ThMYOZRxZ_68,1123
41
+ fxn/types/storage.py,sha256=AtVKR3CtHzvSWLiJS_bbUyIA2Of_IKZVeL5_1PqqrQ0,228
42
+ fxn/types/tag.py,sha256=hWzSDCo8VjRHjS5ZLuFi3xVo8cuCNaNeULQ2mHEuwzM,707
43
+ fxn/types/user.py,sha256=_hc1YQh0WydniAurywA70EDs4VCY5rnGRYSiRc97Ab0,150
44
+ fxn-0.0.36.dist-info/LICENSE,sha256=QwcOLU5TJoTeUhuIXzhdCEEDDvorGiC6-3YTOl4TecE,11356
45
+ fxn-0.0.36.dist-info/METADATA,sha256=Hk1xVRWsEy87RvJrvIFY9zSetdg_Ms5iTOfxYgh4Bho,16068
46
+ fxn-0.0.36.dist-info/WHEEL,sha256=Mdi9PDNwEZptOjTlUcAth7XJDFtKrHYaQMPulZeBCiQ,91
47
+ fxn-0.0.36.dist-info/entry_points.txt,sha256=O_AwD5dYaeB-YT1F9hPAPuDYCkw_W0tdNGYbc5RVR2k,45
48
+ fxn-0.0.36.dist-info/top_level.txt,sha256=1ULIEGrnMlhId8nYAkjmRn9g3KEFuHKboq193SEKQkA,4
49
+ fxn-0.0.36.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.43.0)
2
+ Generator: setuptools (73.0.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,4 +0,0 @@
1
- #
2
- # Function
3
- # Copyright © 2024 NatML Inc. All Rights Reserved.
4
- #
Binary file
@@ -1,4 +0,0 @@
1
- #
2
- # Function
3
- # Copyright © 2024 NatML Inc. All Rights Reserved.
4
- #
Binary file
@@ -1,4 +0,0 @@
1
- #
2
- # Function
3
- # Copyright © 2024 NatML Inc. All Rights Reserved.
4
- #
@@ -1,6 +0,0 @@
1
- #
2
- # Function
3
- # Copyright © 2024 NatML Inc. All Rights Reserved.
4
- #
5
-
6
- from .service import PredictionService, PREDICTION_FIELDS
@@ -1,312 +0,0 @@
1
- #
2
- # Function
3
- # Copyright © 2024 NatML Inc. All Rights Reserved.
4
- #
5
-
6
- from ctypes import byref, cast, c_bool, c_char_p, c_double, c_int, c_int32, c_uint8, c_void_p, string_at, CDLL, POINTER, Structure, _Pointer
7
- from io import BytesIO
8
- from json import dumps, loads
9
- from numpy import array, dtype, int32, ndarray, zeros
10
- from numpy.ctypeslib import as_array, as_ctypes_type
11
- from numpy.typing import NDArray
12
- from pathlib import Path
13
- from PIL import Image
14
- from typing import Any, Dict, List, Union
15
-
16
- # https://github.com/fxnai/fxnc
17
-
18
- class FXNStatus(c_int):
19
- OK = 0
20
- ERROR_INVALID_ARGUMENT = 1
21
- ERROR_INVALID_OPERATION = 2
22
- ERROR_NOT_IMPLEMENTED = 3
23
-
24
- class FXNDtype(c_int):
25
- NULL = 0
26
- FLOAT16 = 1
27
- FLOAT32 = 2
28
- FLOAT64 = 3
29
- INT8 = 4
30
- INT16 = 5
31
- INT32 = 6
32
- INT64 = 7
33
- UINT8 = 8
34
- UINT16 = 9
35
- UINT32 = 10
36
- UINT64 = 11
37
- BOOL = 12
38
- STRING = 13
39
- LIST = 14
40
- DICT = 15
41
- IMAGE = 16
42
- BINARY = 17
43
-
44
- class FXNValueFlags(c_int):
45
- NONE = 0
46
- COPY_DATA = 1
47
-
48
- class FXNAcceleration(c_int):
49
- FXN_ACCELERATION_DEFAULT = 0
50
- FXN_ACCELERATION_CPU = 1 << 0
51
- FXN_ACCELERATION_GPU = 1 << 1
52
- FXN_ACCELERATION_NPU = 1 << 2
53
-
54
- class FXNValue(Structure): pass
55
- class FXNValueMap(Structure): pass
56
- class FXNConfiguration(Structure): pass
57
- class FXNPrediction(Structure): pass
58
- class FXNPredictionStream(Structure): pass
59
- class FXNPredictor(Structure): pass
60
-
61
- FXNValueRef = POINTER(FXNValue)
62
- FXNValueMapRef = POINTER(FXNValueMap)
63
- FXNConfigurationRef = POINTER(FXNConfiguration)
64
- FXNPredictionRef = POINTER(FXNPrediction)
65
- FXNPredictionStreamRef = POINTER(FXNPredictionStream)
66
- FXNPredictorRef = POINTER(FXNPredictor)
67
-
68
- def load_fxnc (path: Path) -> CDLL:
69
- # Open
70
- fxnc = CDLL(str(path))
71
- # FXNValueRelease
72
- fxnc.FXNValueRelease.argtypes = [FXNValueRef]
73
- fxnc.FXNValueRelease.restype = FXNStatus
74
- # FXNValueGetData
75
- fxnc.FXNValueGetData.argtypes = [FXNValueRef, POINTER(c_void_p)]
76
- fxnc.FXNValueGetData.restype = FXNStatus
77
- # FXNValueGetType
78
- fxnc.FXNValueGetType.argtypes = [FXNValueRef, POINTER(FXNDtype)]
79
- fxnc.FXNValueGetType.restype = FXNStatus
80
- # FXNValueGetDimensions
81
- fxnc.FXNValueGetDimensions.argtypes = [FXNValueRef, POINTER(c_int32)]
82
- fxnc.FXNValueGetDimensions.restype = FXNStatus
83
- # FXNValueGetShape
84
- fxnc.FXNValueGetShape.argtypes = [FXNValueRef, POINTER(c_int32), c_int32]
85
- fxnc.FXNValueGetShape.restype = FXNStatus
86
- # FXNValueCreateArray
87
- fxnc.FXNValueCreateArray.argtypes = [c_void_p, POINTER(c_int32), c_int32, FXNDtype, FXNValueFlags, POINTER(FXNValueRef)]
88
- fxnc.FXNValueCreateArray.restype = FXNStatus
89
- # FXNValueCreateString
90
- fxnc.FXNValueCreateString.argtypes = [c_char_p, POINTER(FXNValueRef)]
91
- fxnc.FXNValueCreateString.restype = FXNStatus
92
- # FXNValueCreateList
93
- fxnc.FXNValueCreateList.argtypes = [c_char_p, POINTER(FXNValueRef)]
94
- fxnc.FXNValueCreateList.restype = FXNStatus
95
- # FXNValueCreateDict
96
- fxnc.FXNValueCreateDict.argtypes = [c_char_p, POINTER(FXNValueRef)]
97
- fxnc.FXNValueCreateDict.restype = FXNStatus
98
- # FXNValueCreateImage
99
- fxnc.FXNValueCreateImage.argtypes = [c_void_p, c_int32, c_int32, c_int32, FXNValueFlags, POINTER(FXNValueRef)]
100
- fxnc.FXNValueCreateImage.restype = FXNStatus
101
- # FXNValueMapCreate
102
- fxnc.FXNValueMapCreate.argtypes = [POINTER(FXNValueMapRef)]
103
- fxnc.FXNValueMapCreate.restype = FXNStatus
104
- # FXNValueMapRelease
105
- fxnc.FXNValueMapRelease.argtypes = [FXNValueMapRef]
106
- fxnc.FXNValueMapRelease.restype = FXNStatus
107
- # FXNValueMapGetSize
108
- fxnc.FXNValueMapGetSize.argtypes = [FXNValueMapRef, POINTER(c_int32)]
109
- fxnc.FXNValueMapGetSize.restype = FXNStatus
110
- # FXNValueMapGetKey
111
- fxnc.FXNValueMapGetKey.argtypes = [FXNValueMapRef, c_int32, c_char_p, c_int32]
112
- fxnc.FXNValueMapGetKey.restype = FXNStatus
113
- # FXNValueMapGetValue
114
- fxnc.FXNValueMapGetValue.argtypes = [FXNValueMapRef, c_char_p, POINTER(FXNValueRef)]
115
- fxnc.FXNValueMapGetValue.restype = FXNStatus
116
- # FXNValueMapSetValue
117
- fxnc.FXNValueMapSetValue.argtypes = [FXNValueMapRef, c_char_p, FXNValueRef]
118
- fxnc.FXNValueMapSetValue.restype = FXNStatus
119
- # FXNConfigurationGetUniqueID
120
- fxnc.FXNConfigurationGetUniqueID.argtypes = [c_char_p, c_int32]
121
- fxnc.FXNConfigurationGetUniqueID.restype = FXNStatus
122
- # FXNConfigurationCreate
123
- fxnc.FXNConfigurationCreate.argtypes = [POINTER(FXNConfigurationRef)]
124
- fxnc.FXNConfigurationCreate.restype = FXNStatus
125
- # FXNConfigurationRelease
126
- fxnc.FXNConfigurationRelease.argtypes = [FXNConfigurationRef]
127
- fxnc.FXNConfigurationRelease.restype = FXNStatus
128
- # FXNConfigurationGetTag
129
- fxnc.FXNConfigurationGetTag.argtypes = [FXNConfigurationRef, c_char_p, c_int32]
130
- fxnc.FXNConfigurationRelease.restype = FXNStatus
131
- # FXNConfigurationSetTag
132
- fxnc.FXNConfigurationSetTag.argtypes = [FXNConfigurationRef, c_char_p]
133
- fxnc.FXNConfigurationSetTag.restype = FXNStatus
134
- # FXNConfigurationGetToken
135
- fxnc.FXNConfigurationGetToken.argtypes = [FXNConfigurationRef, c_char_p, c_int32]
136
- fxnc.FXNConfigurationGetToken.restype = FXNStatus
137
- # FXNConfigurationSetToken
138
- fxnc.FXNConfigurationSetToken.argtypes = [FXNConfigurationRef, c_char_p]
139
- fxnc.FXNConfigurationSetToken.restype = FXNStatus
140
- # FXNConfigurationGetAcceleration
141
- fxnc.FXNConfigurationGetAcceleration.argtypes = [FXNConfigurationRef, POINTER(FXNAcceleration)]
142
- fxnc.FXNConfigurationGetAcceleration.restype = FXNStatus
143
- # FXNConfigurationSetAcceleration
144
- fxnc.FXNConfigurationSetAcceleration.argtypes = [FXNConfigurationRef, FXNAcceleration]
145
- fxnc.FXNConfigurationSetAcceleration.restype = FXNStatus
146
- # FXNConfigurationGetDevice
147
- fxnc.FXNConfigurationGetDevice.argtypes = [FXNConfigurationRef, POINTER(c_void_p)]
148
- fxnc.FXNConfigurationGetDevice.restype = FXNStatus
149
- # FXNConfigurationSetDevice
150
- fxnc.FXNConfigurationSetDevice.argtypes = [FXNConfigurationRef, c_void_p]
151
- fxnc.FXNConfigurationSetDevice.restype = FXNStatus
152
- # FXNConfigurationAddResource
153
- fxnc.FXNConfigurationAddResource.argtypes = [FXNConfigurationRef, c_char_p, c_char_p]
154
- fxnc.FXNConfigurationAddResource.restype = FXNStatus
155
- # FXNPredictionRelease
156
- fxnc.FXNPredictionRelease.argtypes = [FXNPredictionRef]
157
- fxnc.FXNPredictionRelease.restype = FXNStatus
158
- # FXNPredictionGetID
159
- fxnc.FXNPredictionGetID.argtypes = [FXNPredictionRef, c_char_p, c_int32]
160
- fxnc.FXNPredictionGetID.restype = FXNStatus
161
- # FXNPredictionGetLatency
162
- fxnc.FXNPredictionGetLatency.argtypes = [FXNPredictionRef, POINTER(c_double)]
163
- fxnc.FXNPredictionGetLatency.restype = FXNStatus
164
- # FXNPredictionGetResults
165
- fxnc.FXNPredictionGetResults.argtypes = [FXNPredictionRef, POINTER(FXNValueMapRef)]
166
- fxnc.FXNPredictionGetResults.restype = FXNStatus
167
- # FXNPredictionGetError
168
- fxnc.FXNPredictionGetError.argtypes = [FXNPredictionRef, c_char_p, c_int32]
169
- fxnc.FXNPredictionGetError.restype = FXNStatus
170
- # FXNPredictionGetLogs
171
- fxnc.FXNPredictionGetLogs.argtypes = [FXNPredictionRef, c_char_p, c_int32]
172
- fxnc.FXNPredictionGetLogs.restype = FXNStatus
173
- # FXNPredictionGetLogLength
174
- fxnc.FXNPredictionGetLogLength.argtypes = [FXNPredictionRef, POINTER(c_int32)]
175
- fxnc.FXNPredictionGetLogLength.restype = FXNStatus
176
- # FXNPredictionStreamRelease
177
- fxnc.FXNPredictionStreamRelease.argtypes = [FXNPredictionStreamRef]
178
- fxnc.FXNPredictionStreamRelease.restype = FXNStatus
179
- # FXNPredictionStreamReadNext
180
- fxnc.FXNPredictionStreamReadNext.argtypes = [FXNPredictionStreamRef, POINTER(FXNPredictionRef)]
181
- fxnc.FXNPredictionStreamReadNext.restype = FXNStatus
182
- # FXNPredictorCreate
183
- fxnc.FXNPredictorCreate.argtypes = [FXNConfigurationRef, POINTER(FXNPredictorRef)]
184
- fxnc.FXNPredictorCreate.restype = FXNStatus
185
- # FXNPredictorRelease
186
- fxnc.FXNPredictorRelease.argtypes = [FXNPredictorRef]
187
- fxnc.FXNPredictorRelease.restype = FXNStatus
188
- # FXNPredictorCreatePrediction
189
- fxnc.FXNPredictorCreatePrediction.argtypes = [FXNPredictorRef, FXNValueMapRef, POINTER(FXNPredictionRef)]
190
- fxnc.FXNPredictorCreatePrediction.restype = FXNStatus
191
- # FXNPredictorStreamPrediction
192
- fxnc.FXNPredictorStreamPrediction.argtypes = [FXNPredictionRef, FXNValueMapRef, POINTER(FXNPredictionStreamRef)]
193
- fxnc.FXNPredictorStreamPrediction.restype = FXNStatus
194
- # FXNGetVersion
195
- fxnc.FXNGetVersion.argtypes = []
196
- fxnc.FXNGetVersion.restype = c_char_p
197
- # Return
198
- return fxnc
199
-
200
- def to_fxn_value (
201
- fxnc: CDLL,
202
- value: Union[float, int, bool, str, NDArray, List[Any], Dict[str, Any], Image.Image, bytes, bytearray, memoryview, BytesIO, None],
203
- *,
204
- copy: bool=False
205
- ) -> type[FXNValueRef]:
206
- result = FXNValueRef()
207
- if result is None:
208
- fxnc.FXNValueCreateNull(byref(result))
209
- elif isinstance(value, bool):
210
- return to_fxn_value(fxnc, array(value, dtype="bool"))
211
- elif isinstance(value, int):
212
- return to_fxn_value(fxnc, array(value, dtype="int32"))
213
- elif isinstance(value, float):
214
- return to_fxn_value(fxnc, array(value, dtype="float32"))
215
- elif isinstance(value, ndarray):
216
- dtype = _NP_TO_FXN_DTYPE.get(value.dtype)
217
- assert dtype is not None, f"Failed to convert numpy array to Function value because array data type is not supported: {value.dtype}"
218
- fxnc.FXNValueCreateArray(
219
- value.ctypes.data_as(c_void_p),
220
- value.ctypes.shape_as(c_int32),
221
- len(value.shape),
222
- dtype,
223
- FXNValueFlags.COPY_DATA if copy else FXNValueFlags.NONE,
224
- byref(result)
225
- )
226
- elif isinstance(value, str):
227
- fxnc.FXNValueCreateString(value.encode(), byref(result))
228
- elif isinstance(value, list):
229
- fxnc.FXNValueCreateList(dumps(value).encode(), byref(result))
230
- elif isinstance(value, dict):
231
- fxnc.FXNValueCreateDict(dumps(value).encode(), byref(result))
232
- elif isinstance(value, Image.Image):
233
- value = array(value)
234
- status = fxnc.FXNValueCreateImage(
235
- value.ctypes.data_as(c_void_p),
236
- value.shape[1],
237
- value.shape[0],
238
- value.shape[2],
239
- FXNValueFlags.COPY_DATA,
240
- byref(result)
241
- )
242
- assert status.value == FXNStatus.OK, f"Failed to create image value with status: {status.value}"
243
- elif isinstance(value, (bytes, bytearray, memoryview, BytesIO)):
244
- view = memoryview(value.getvalue() if isinstance(value, BytesIO) else value) if not isinstance(value, memoryview) else value
245
- buffer = (c_uint8 * len(view)).from_buffer(view)
246
- fxnc.FXNValueCreateBinary(
247
- buffer,
248
- len(view),
249
- FXNValueFlags.COPY_DATA if copy else FXNValueFlags.NONE,
250
- byref(result)
251
- )
252
- else:
253
- raise RuntimeError(f"Failed to convert Python value to Function value because Python value has an unsupported type: {type(value)}")
254
- return result
255
-
256
- def to_py_value (
257
- fxnc: CDLL,
258
- value: type[FXNValueRef]
259
- ) -> Union[float, int, bool, str, NDArray, List[Any], Dict[str, Any], Image.Image, BytesIO, None]:
260
- # Type
261
- dtype = FXNDtype()
262
- status = fxnc.FXNValueGetType(value, byref(dtype))
263
- assert status.value == FXNStatus.OK, f"Failed to get value data type with status: {status.value}"
264
- dtype = dtype.value
265
- # Get data
266
- data = c_void_p()
267
- status = fxnc.FXNValueGetData(value, byref(data))
268
- assert status.value == FXNStatus.OK, f"Failed to get value data with status: {status.value}"
269
- # Get shape
270
- dims = c_int32()
271
- status = fxnc.FXNValueGetDimensions(value, byref(dims))
272
- assert status.value == FXNStatus.OK, f"Failed to get value dimensions with status: {status.value}"
273
- shape = zeros(dims.value, dtype=int32)
274
- status = fxnc.FXNValueGetShape(value, shape.ctypes.data_as(POINTER(c_int32)), dims)
275
- assert status.value == FXNStatus.OK, f"Failed to get value shape with status: {status.value}"
276
- # Switch
277
- if dtype == FXNDtype.NULL:
278
- return None
279
- elif dtype in _FXN_TO_NP_DTYPE:
280
- dtype_c = as_ctypes_type(_FXN_TO_NP_DTYPE[dtype])
281
- tensor = as_array(cast(data, POINTER(dtype_c)), shape)
282
- return tensor.item() if len(tensor.shape) == 0 else tensor.copy()
283
- elif dtype == FXNDtype.STRING:
284
- return cast(data, c_char_p).value.decode()
285
- elif dtype == FXNDtype.LIST:
286
- return loads(cast(data, c_char_p).value.decode())
287
- elif dtype == FXNDtype.DICT:
288
- return loads(cast(data, c_char_p).value.decode())
289
- elif dtype == FXNDtype.IMAGE:
290
- pixel_buffer = as_array(cast(data, POINTER(c_uint8)), shape)
291
- return Image.fromarray(pixel_buffer)
292
- elif dtype == FXNDtype.BINARY:
293
- return BytesIO(string_at(data, shape[0]))
294
- else:
295
- raise RuntimeError(f"Failed to convert Function value to Python value because Function value has unsupported type: {dtype}")
296
-
297
- _FXN_TO_NP_DTYPE = {
298
- FXNDtype.FLOAT16: dtype("float16"),
299
- FXNDtype.FLOAT32: dtype("float32"),
300
- FXNDtype.FLOAT64: dtype("float64"),
301
- FXNDtype.INT8: dtype("int8"),
302
- FXNDtype.INT16: dtype("int16"),
303
- FXNDtype.INT32: dtype("int32"),
304
- FXNDtype.INT64: dtype("int64"),
305
- FXNDtype.UINT8: dtype("uint8"),
306
- FXNDtype.UINT16: dtype("uint16"),
307
- FXNDtype.UINT32: dtype("uint32"),
308
- FXNDtype.UINT64: dtype("uint64"),
309
- FXNDtype.BOOL: dtype("bool"),
310
- }
311
-
312
- _NP_TO_FXN_DTYPE = { value: key for key, value in _FXN_TO_NP_DTYPE.items() }