fxn 0.0.34__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.34.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.34.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 -301
  40. fxn/services/prediction/service.py +0 -512
  41. fxn/types/value.py +0 -22
  42. fxn-0.0.34.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.34.dist-info → fxn-0.0.36.dist-info}/LICENSE +0 -0
  47. {fxn-0.0.34.dist-info → fxn-0.0.36.dist-info}/entry_points.txt +0 -0
  48. {fxn-0.0.34.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.34"
6
+ __version__ = "0.0.36"
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fxn
3
- Version: 0.0.34
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,301 +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 FXNPredictor(Structure): pass
59
-
60
- FXNValueRef = POINTER(FXNValue)
61
- FXNValueMapRef = POINTER(FXNValueMap)
62
- FXNConfigurationRef = POINTER(FXNConfiguration)
63
- FXNPredictionRef = POINTER(FXNPrediction)
64
- FXNPredictorRef = POINTER(FXNPredictor)
65
-
66
- def load_fxnc (path: Path) -> CDLL:
67
- # Open
68
- fxnc = CDLL(str(path))
69
- # FXNValueRelease
70
- fxnc.FXNValueRelease.argtypes = [FXNValueRef]
71
- fxnc.FXNValueRelease.restype = FXNStatus
72
- # FXNValueGetData
73
- fxnc.FXNValueGetData.argtypes = [FXNValueRef, POINTER(c_void_p)]
74
- fxnc.FXNValueGetData.restype = FXNStatus
75
- # FXNValueGetType
76
- fxnc.FXNValueGetType.argtypes = [FXNValueRef, POINTER(FXNDtype)]
77
- fxnc.FXNValueGetType.restype = FXNStatus
78
- # FXNValueGetDimensions
79
- fxnc.FXNValueGetDimensions.argtypes = [FXNValueRef, POINTER(c_int32)]
80
- fxnc.FXNValueGetDimensions.restype = FXNStatus
81
- # FXNValueGetShape
82
- fxnc.FXNValueGetShape.argtypes = [FXNValueRef, POINTER(c_int32), c_int32]
83
- fxnc.FXNValueGetShape.restype = FXNStatus
84
- # FXNValueCreateArray
85
- fxnc.FXNValueCreateArray.argtypes = [c_void_p, POINTER(c_int32), c_int32, FXNDtype, FXNValueFlags, POINTER(FXNValueRef)]
86
- fxnc.FXNValueCreateArray.restype = FXNStatus
87
- # FXNValueCreateString
88
- fxnc.FXNValueCreateString.argtypes = [c_char_p, POINTER(FXNValueRef)]
89
- fxnc.FXNValueCreateString.restype = FXNStatus
90
- # FXNValueCreateList
91
- fxnc.FXNValueCreateList.argtypes = [c_char_p, POINTER(FXNValueRef)]
92
- fxnc.FXNValueCreateList.restype = FXNStatus
93
- # FXNValueCreateDict
94
- fxnc.FXNValueCreateDict.argtypes = [c_char_p, POINTER(FXNValueRef)]
95
- fxnc.FXNValueCreateDict.restype = FXNStatus
96
- # FXNValueCreateImage
97
- fxnc.FXNValueCreateImage.argtypes = [c_void_p, c_int32, c_int32, c_int32, FXNValueFlags, POINTER(FXNValueRef)]
98
- fxnc.FXNValueCreateImage.restype = FXNStatus
99
- # FXNValueMapCreate
100
- fxnc.FXNValueMapCreate.argtypes = [POINTER(FXNValueMapRef)]
101
- fxnc.FXNValueMapCreate.restype = FXNStatus
102
- # FXNValueMapRelease
103
- fxnc.FXNValueMapRelease.argtypes = [FXNValueMapRef]
104
- fxnc.FXNValueMapRelease.restype = FXNStatus
105
- # FXNValueMapGetSize
106
- fxnc.FXNValueMapGetSize.argtypes = [FXNValueMapRef, POINTER(c_int32)]
107
- fxnc.FXNValueMapGetSize.restype = FXNStatus
108
- # FXNValueMapGetKey
109
- fxnc.FXNValueMapGetKey.argtypes = [FXNValueMapRef, c_int32, c_char_p, c_int32]
110
- fxnc.FXNValueMapGetKey.restype = FXNStatus
111
- # FXNValueMapGetValue
112
- fxnc.FXNValueMapGetValue.argtypes = [FXNValueMapRef, c_char_p, POINTER(FXNValueRef)]
113
- fxnc.FXNValueMapGetValue.restype = FXNStatus
114
- # FXNValueMapSetValue
115
- fxnc.FXNValueMapSetValue.argtypes = [FXNValueMapRef, c_char_p, FXNValueRef]
116
- fxnc.FXNValueMapSetValue.restype = FXNStatus
117
- # FXNConfigurationGetUniqueID
118
- fxnc.FXNConfigurationGetUniqueID.argtypes = [c_char_p, c_int32]
119
- fxnc.FXNConfigurationGetUniqueID.restype = FXNStatus
120
- # FXNConfigurationCreate
121
- fxnc.FXNConfigurationCreate.argtypes = [POINTER(FXNConfigurationRef)]
122
- fxnc.FXNConfigurationCreate.restype = FXNStatus
123
- # FXNConfigurationRelease
124
- fxnc.FXNConfigurationRelease.argtypes = [FXNConfigurationRef]
125
- fxnc.FXNConfigurationRelease.restype = FXNStatus
126
- # FXNConfigurationGetTag
127
- fxnc.FXNConfigurationGetTag.argtypes = [FXNConfigurationRef, c_char_p, c_int32]
128
- fxnc.FXNConfigurationRelease.restype = FXNStatus
129
- # FXNConfigurationSetTag
130
- fxnc.FXNConfigurationSetTag.argtypes = [FXNConfigurationRef, c_char_p]
131
- fxnc.FXNConfigurationSetTag.restype = FXNStatus
132
- # FXNConfigurationGetToken
133
- fxnc.FXNConfigurationGetToken.argtypes = [FXNConfigurationRef, c_char_p, c_int32]
134
- fxnc.FXNConfigurationGetToken.restype = FXNStatus
135
- # FXNConfigurationSetToken
136
- fxnc.FXNConfigurationSetToken.argtypes = [FXNConfigurationRef, c_char_p]
137
- fxnc.FXNConfigurationSetToken.restype = FXNStatus
138
- # FXNConfigurationGetAcceleration
139
- fxnc.FXNConfigurationGetAcceleration.argtypes = [FXNConfigurationRef, POINTER(FXNAcceleration)]
140
- fxnc.FXNConfigurationGetAcceleration.restype = FXNStatus
141
- # FXNConfigurationSetAcceleration
142
- fxnc.FXNConfigurationSetAcceleration.argtypes = [FXNConfigurationRef, FXNAcceleration]
143
- fxnc.FXNConfigurationSetAcceleration.restype = FXNStatus
144
- # FXNConfigurationGetDevice
145
- fxnc.FXNConfigurationGetDevice.argtypes = [FXNConfigurationRef, POINTER(c_void_p)]
146
- fxnc.FXNConfigurationGetDevice.restype = FXNStatus
147
- # FXNConfigurationSetDevice
148
- fxnc.FXNConfigurationSetDevice.argtypes = [FXNConfigurationRef, c_void_p]
149
- fxnc.FXNConfigurationSetDevice.restype = FXNStatus
150
- # FXNConfigurationAddResource
151
- fxnc.FXNConfigurationAddResource.argtypes = [FXNConfigurationRef, c_char_p, c_char_p]
152
- fxnc.FXNConfigurationAddResource.restype = FXNStatus
153
- # FXNPredictionRelease
154
- fxnc.FXNPredictionRelease.argtypes = [FXNPredictionRef]
155
- fxnc.FXNPredictionRelease.restype = FXNStatus
156
- # FXNPredictionGetID
157
- fxnc.FXNPredictionGetID.argtypes = [FXNPredictionRef, c_char_p, c_int32]
158
- fxnc.FXNPredictionGetID.restype = FXNStatus
159
- # FXNPredictionGetLatency
160
- fxnc.FXNPredictionGetLatency.argtypes = [FXNPredictionRef, POINTER(c_double)]
161
- fxnc.FXNPredictionGetLatency.restype = FXNStatus
162
- # FXNPredictionGetResults
163
- fxnc.FXNPredictionGetResults.argtypes = [FXNPredictionRef, POINTER(FXNValueMapRef)]
164
- fxnc.FXNPredictionGetResults.restype = FXNStatus
165
- # FXNPredictionGetError
166
- fxnc.FXNPredictionGetError.argtypes = [FXNPredictionRef, c_char_p, c_int32]
167
- fxnc.FXNPredictionGetError.restype = FXNStatus
168
- # FXNPredictionGetLogs
169
- fxnc.FXNPredictionGetLogs.argtypes = [FXNPredictionRef, c_char_p, c_int32]
170
- fxnc.FXNPredictionGetLogs.restype = FXNStatus
171
- # FXNPredictionGetLogLength
172
- fxnc.FXNPredictionGetLogLength.argtypes = [FXNPredictionRef, POINTER(c_int32)]
173
- fxnc.FXNPredictionGetLogLength.restype = FXNStatus
174
- # FXNPredictorCreate
175
- fxnc.FXNPredictorCreate.argtypes = [FXNConfigurationRef, POINTER(FXNPredictorRef)]
176
- fxnc.FXNPredictorCreate.restype = FXNStatus
177
- # FXNPredictorRelease
178
- fxnc.FXNPredictorRelease.argtypes = [FXNPredictorRef]
179
- fxnc.FXNPredictorRelease.restype = FXNStatus
180
- # FXNPredictorPredict
181
- fxnc.FXNPredictorPredict.argtypes = [FXNPredictorRef, FXNValueMapRef, POINTER(FXNPredictionRef)]
182
- fxnc.FXNPredictorPredict.restype = FXNStatus
183
- # FXNGetVersion
184
- fxnc.FXNGetVersion.argtypes = []
185
- fxnc.FXNGetVersion.restype = c_char_p
186
- # Return
187
- return fxnc
188
-
189
- def to_fxn_value (
190
- fxnc: CDLL,
191
- value: Union[float, int, bool, str, NDArray, List[Any], Dict[str, Any], Image.Image, bytes, bytearray, memoryview, BytesIO, None],
192
- *,
193
- copy: bool=False
194
- ) -> type[FXNValueRef]:
195
- result = FXNValueRef()
196
- if result is None:
197
- fxnc.FXNValueCreateNull(byref(result))
198
- elif isinstance(value, bool):
199
- return to_fxn_value(fxnc, array(value, dtype="bool"))
200
- elif isinstance(value, int):
201
- return to_fxn_value(fxnc, array(value, dtype="int32"))
202
- elif isinstance(value, float):
203
- return to_fxn_value(fxnc, array(value, dtype="float32"))
204
- elif isinstance(value, ndarray):
205
- dtype = _NP_TO_FXN_DTYPE.get(value.dtype)
206
- assert dtype is not None, f"Failed to convert numpy array to Function value because array data type is not supported: {value.dtype}"
207
- fxnc.FXNValueCreateArray(
208
- value.ctypes.data_as(c_void_p),
209
- value.ctypes.shape_as(c_int32),
210
- len(value.shape),
211
- dtype,
212
- FXNValueFlags.COPY_DATA if copy else FXNValueFlags.NONE,
213
- byref(result)
214
- )
215
- elif isinstance(value, str):
216
- fxnc.FXNValueCreateString(value.encode(), byref(result))
217
- elif isinstance(value, list):
218
- fxnc.FXNValueCreateList(dumps(value).encode(), byref(result))
219
- elif isinstance(value, dict):
220
- fxnc.FXNValueCreateDict(dumps(value).encode(), byref(result))
221
- elif isinstance(value, Image.Image):
222
- value = array(value)
223
- status = fxnc.FXNValueCreateImage(
224
- value.ctypes.data_as(c_void_p),
225
- value.shape[1],
226
- value.shape[0],
227
- value.shape[2],
228
- FXNValueFlags.COPY_DATA,
229
- byref(result)
230
- )
231
- assert status.value == FXNStatus.OK, f"Failed to create image value with status: {status.value}"
232
- elif isinstance(value, (bytes, bytearray, memoryview, BytesIO)):
233
- view = memoryview(value.getvalue() if isinstance(value, BytesIO) else value) if not isinstance(value, memoryview) else value
234
- buffer = (c_uint8 * len(view)).from_buffer(view)
235
- fxnc.FXNValueCreateBinary(
236
- buffer,
237
- len(view),
238
- FXNValueFlags.COPY_DATA if copy else FXNValueFlags.NONE,
239
- byref(result)
240
- )
241
- else:
242
- raise RuntimeError(f"Failed to convert Python value to Function value because Python value has an unsupported type: {type(value)}")
243
- return result
244
-
245
- def to_py_value (
246
- fxnc: CDLL,
247
- value: type[FXNValueRef]
248
- ) -> Union[float, int, bool, str, NDArray, List[Any], Dict[str, Any], Image.Image, BytesIO, None]:
249
- # Type
250
- dtype = FXNDtype()
251
- status = fxnc.FXNValueGetType(value, byref(dtype))
252
- assert status.value == FXNStatus.OK, f"Failed to get value data type with status: {status.value}"
253
- dtype = dtype.value
254
- # Get data
255
- data = c_void_p()
256
- status = fxnc.FXNValueGetData(value, byref(data))
257
- assert status.value == FXNStatus.OK, f"Failed to get value data with status: {status.value}"
258
- # Get shape
259
- dims = c_int32()
260
- status = fxnc.FXNValueGetDimensions(value, byref(dims))
261
- assert status.value == FXNStatus.OK, f"Failed to get value dimensions with status: {status.value}"
262
- shape = zeros(dims.value, dtype=int32)
263
- status = fxnc.FXNValueGetShape(value, shape.ctypes.data_as(POINTER(c_int32)), dims)
264
- assert status.value == FXNStatus.OK, f"Failed to get value shape with status: {status.value}"
265
- # Switch
266
- if dtype == FXNDtype.NULL:
267
- return None
268
- elif dtype in _FXN_TO_NP_DTYPE:
269
- dtype_c = as_ctypes_type(_FXN_TO_NP_DTYPE[dtype])
270
- tensor = as_array(cast(data, POINTER(dtype_c)), shape)
271
- return tensor.item() if len(tensor.shape) == 0 else tensor.copy()
272
- elif dtype == FXNDtype.STRING:
273
- return cast(data, c_char_p).value.decode()
274
- elif dtype == FXNDtype.LIST:
275
- return loads(cast(data, c_char_p).value.decode())
276
- elif dtype == FXNDtype.DICT:
277
- return loads(cast(data, c_char_p).value.decode())
278
- elif dtype == FXNDtype.IMAGE:
279
- pixel_buffer = as_array(cast(data, POINTER(c_uint8)), shape)
280
- return Image.fromarray(pixel_buffer)
281
- elif dtype == FXNDtype.BINARY:
282
- return BytesIO(string_at(data, shape[0]))
283
- else:
284
- raise RuntimeError(f"Failed to convert Function value to Python value because Function value has unsupported type: {dtype}")
285
-
286
- _FXN_TO_NP_DTYPE = {
287
- FXNDtype.FLOAT16: dtype("float16"),
288
- FXNDtype.FLOAT32: dtype("float32"),
289
- FXNDtype.FLOAT64: dtype("float64"),
290
- FXNDtype.INT8: dtype("int8"),
291
- FXNDtype.INT16: dtype("int16"),
292
- FXNDtype.INT32: dtype("int32"),
293
- FXNDtype.INT64: dtype("int64"),
294
- FXNDtype.UINT8: dtype("uint8"),
295
- FXNDtype.UINT16: dtype("uint16"),
296
- FXNDtype.UINT32: dtype("uint32"),
297
- FXNDtype.UINT64: dtype("uint64"),
298
- FXNDtype.BOOL: dtype("bool"),
299
- }
300
-
301
- _NP_TO_FXN_DTYPE = { value: key for key, value in _FXN_TO_NP_DTYPE.items() }