fxn 0.0.35__py3-none-any.whl → 0.0.37__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 (51) 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/misc.py +10 -4
  15. fxn/cli/predict.py +3 -4
  16. fxn/cli/predictors.py +1 -40
  17. fxn/function.py +4 -10
  18. fxn/lib/macos/arm64/Function.dylib +0 -0
  19. fxn/lib/macos/x86_64/Function.dylib +0 -0
  20. fxn/lib/windows/arm64/Function.dll +0 -0
  21. fxn/lib/windows/x86_64/Function.dll +0 -0
  22. fxn/services/__init__.py +1 -3
  23. fxn/services/prediction.py +456 -0
  24. fxn/services/predictor.py +4 -73
  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.37.dist-info}/METADATA +27 -29
  31. fxn-0.0.37.dist-info/RECORD +46 -0
  32. {fxn-0.0.35.dist-info → fxn-0.0.37.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/magic.py +0 -35
  39. fxn/services/environment.py +0 -111
  40. fxn/services/prediction/__init__.py +0 -6
  41. fxn/services/prediction/fxnc.py +0 -312
  42. fxn/services/prediction/service.py +0 -512
  43. fxn/services/storage.py +0 -160
  44. fxn/types/value.py +0 -22
  45. fxn-0.0.35.dist-info/RECORD +0 -42
  46. /fxn/{graph → api}/__init__.py +0 -0
  47. /fxn/{graph → api}/client.py +0 -0
  48. /fxn/{libs → lib}/__init__.py +0 -0
  49. {fxn-0.0.35.dist-info → fxn-0.0.37.dist-info}/LICENSE +0 -0
  50. {fxn-0.0.35.dist-info → fxn-0.0.37.dist-info}/entry_points.txt +0 -0
  51. {fxn-0.0.35.dist-info → fxn-0.0.37.dist-info}/top_level.txt +0 -0
@@ -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() }