cohere-core 4.3__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.
@@ -0,0 +1,393 @@
1
+ # #########################################################################
2
+ # Copyright (c) , UChicago Argonne, LLC. All rights reserved. #
3
+ # #
4
+ # See LICENSE file. #
5
+ # #########################################################################
6
+
7
+ from cohere_core.lib.cohlib import cohlib
8
+ import arrayfire as af
9
+ import math
10
+ import numpy as np
11
+
12
+
13
+ class aflib(cohlib):
14
+ def array(obj):
15
+ print('not implemented')
16
+
17
+ def dot(arr1, arr2):
18
+ print('not implemented')
19
+
20
+ def set_device(dev_id):
21
+ af.device.set_device(dev_id)
22
+
23
+ def set_backend(proc):
24
+ af.set_backend(proc)
25
+
26
+ def to_numpy(arr):
27
+ return arr.to_ndarray().T
28
+
29
+ def from_numpy(arr):
30
+ return af.np_to_af_array(arr.T)
31
+
32
+ def save(filename, arr):
33
+ np.save(filename, arr.to_ndarray().T)
34
+
35
+ def load(filename):
36
+ arr = np.load(filename)
37
+ return af.np_to_af_array(arr.T)
38
+
39
+ def dtype(arr):
40
+ return arr.dtype()
41
+
42
+ def size(arr):
43
+ return arr.elements()
44
+
45
+ def hasnan(arr):
46
+ return af.any_true(af.isnan(arr))
47
+
48
+ def copy(arr):
49
+ return arr.copy()
50
+
51
+ def random(shape, **kwargs):
52
+ import time
53
+ import os
54
+
55
+ dims = [None, None, None, None]
56
+ for i in range(len(shape)):
57
+ dims[i] = shape[i]
58
+
59
+ eng = af.random.Random_Engine(engine_type=af.RANDOM_ENGINE.DEFAULT,
60
+ seed=int(time.time() * 1000000) * os.getpid() + os.getpid())
61
+ return af.random.randn(dims[0], dims[1], dims[2], dims[3], dtype=af.Dtype.c32, engine=eng)
62
+
63
+ def fftshift(arr):
64
+ raise NotImplementedError
65
+
66
+ def ifftshift(arr):
67
+ raise NotImplementedError
68
+
69
+ def shift(arr, sft):
70
+ raise NotImplementedError
71
+
72
+ def fft(arr):
73
+ raise NotImplementedError
74
+
75
+ def ifft(arr):
76
+ raise NotImplementedError
77
+
78
+ def fftconvolve(arr1, arr2):
79
+ return af.fft_convolve(arr1, arr2)
80
+
81
+ def where(cond, x, y):
82
+ return af.select(cond, x, y)
83
+
84
+ def dims(arr):
85
+ # get array dimensions
86
+ return arr.dims()
87
+
88
+ def absolute(arr):
89
+ return af.abs(arr)
90
+
91
+ def sqrt(arr):
92
+ return af.sqrt(arr)
93
+
94
+ def square(arr):
95
+ return af.pow(arr, 2)
96
+
97
+ def sum(arr, axis=None):
98
+ return af.sum(arr)
99
+
100
+ def real(arr):
101
+ return af.real(arr)
102
+
103
+ def imag(arr):
104
+ return af.imag(arr)
105
+
106
+ def amax(arr):
107
+ return af.max(arr)
108
+
109
+ def maximum(arr1, arr2):
110
+ return af.select((arr1 > arr2), arr1, arr2)
111
+
112
+ def argmax(arr, axis=None):
113
+ val, idx = af.imax(arr, axis)
114
+ return idx
115
+
116
+ def unravel_index(indices, shape):
117
+ raise NotImplementedError
118
+
119
+ def ceil(arr):
120
+ return af.ceil(arr)
121
+
122
+ def fix(arr):
123
+ return af.trunc(arr)
124
+
125
+ def round(val):
126
+ print('not implemented')
127
+
128
+ def print(arr, **kwargs):
129
+ af.display(arr)
130
+
131
+ def angle(arr):
132
+ return af.atan2(af.imag(arr), af.real(arr))
133
+
134
+ def flip(arr, axis=None):
135
+ if axis is None:
136
+ raise NotImplementedError
137
+ else:
138
+ return af.flip(arr, axis)
139
+
140
+ def tile(arr, rep):
141
+ print('not implemented')
142
+
143
+ def full(shape, fill_value, **kwargs):
144
+ dims = [None, None, None, None]
145
+ for i in range(len(shape)):
146
+ dims[i] = shape[i]
147
+ return af.constant(fill_value, dims[0], dims[1], dims[2], dims[3])
148
+
149
+ def expand_dims(arr, axis):
150
+ return arr
151
+
152
+ def squeeze(arr):
153
+ return arr
154
+
155
+ def gaussian(shape, sigmas, **kwargs):
156
+ raise NotImplementedError
157
+
158
+ def gaussian_flter(shape, sigma, **kwargs):
159
+ raise NotImplementedError
160
+
161
+ def center_of_mass(inarr):
162
+ arr = af.abs(inarr)
163
+ normalizer = af.sum(arr)
164
+ t_dims = list(arr.dims())
165
+ mod_dims = [None, None, None, None]
166
+ for i in range(len(t_dims)):
167
+ mod_dims[i] = 1
168
+ com = []
169
+
170
+ for dim in range(len(t_dims)):
171
+ # swap
172
+ mod_dims[dim] = t_dims[dim]
173
+ t_dims[dim] = 1
174
+ grid = af.iota(mod_dims[0], mod_dims[1], mod_dims[2], mod_dims[3], tile_dims=t_dims)
175
+ # print(grid)
176
+ com.append(af.sum(grid * arr) / normalizer)
177
+ # swap back
178
+ t_dims[dim] = mod_dims[dim]
179
+ mod_dims[dim] = 1
180
+
181
+ return com
182
+
183
+ def meshgrid(*xi):
184
+ print('not implemented')
185
+
186
+ def exp(arr):
187
+ return af.exp(arr)
188
+
189
+ def cong(arr):
190
+ return af.conjg(arr)
191
+
192
+ def save(file, arr):
193
+ arr = af.Array(arr)
194
+ nparr = arr.to_ndarray().T
195
+ np.save(file, nparr)
196
+
197
+
198
+ class aflib1(aflib):
199
+ def fftshift(arr):
200
+ return af.shift(arr, math.ceil(arr.dims()[0] / 2) - 1)
201
+
202
+ def ifftshift(arr):
203
+ return af.shift(arr, math.ceil(arr.dims()[0] / 2))
204
+
205
+ def shift(arr, sft):
206
+ return af.shift(arr, math.ceil(sft[0]))
207
+
208
+ def fft(arr):
209
+ return af.fft(arr)
210
+
211
+ def ifft(arr):
212
+ return af.ifft(arr)
213
+
214
+ def unravel_index(indices, shape):
215
+ return [indices]
216
+
217
+ def flip(arr, axis=None):
218
+ if axis is None:
219
+ return af.flip(arr, 0)
220
+ else:
221
+ return af.flip(arr, axis)
222
+
223
+ def gaussian(dims, sigmas, **kwargs):
224
+ alpha = 1.0
225
+ grid = af.constant(1.0, dims[0])
226
+ multiplier = - 0.5 * alpha / pow(sigmas[0], 2)
227
+ exponent = af.pow((af.range(dims[0], dim=0) - (dims[0] - 1) / 2.0), 2) * multiplier
228
+ grid = grid * af.arith.exp(exponent)
229
+ return grid / af.sum(grid)
230
+
231
+ def gaussian_filter(arr, sigma, **kwargs):
232
+ dims = arr.dims()
233
+ sigmas = [dim / (2.0 * np.pi * sigma) for dim in dims]
234
+ dist = aflib2.gaussian(dims, sigmas)
235
+ arr_f = aflib1.ifftshift(af.fft(aflib1.ifftshift(arr)))
236
+ filter = arr_f * dist
237
+ filter = aflib1.ifftshift(af.ifft(aflib1.ifftshift(filter)))
238
+ filter = af.real(filter)
239
+ filter = af.select(filter >= 0, filter, 0.0)
240
+ return filter
241
+
242
+
243
+ class aflib2(aflib):
244
+ def fftshift(arr):
245
+ return af.shift(arr, math.ceil(arr.dims()[0] / 2) - 1, math.ceil(arr.dims()[1] / 2) - 1)
246
+
247
+ def ifftshift(arr):
248
+ return af.shift(arr, math.ceil(arr.dims()[0] / 2), math.ceil(arr.dims()[1] / 2))
249
+
250
+ def shift(arr, sft):
251
+ return af.shift(arr, math.ceil(sft[0]), math.ceil(sft[1]))
252
+
253
+ def fft(arr):
254
+ return af.fft2(arr)
255
+
256
+ def ifft(arr):
257
+ return af.ifft2(arr)
258
+
259
+ def unravel_index(indices, shape):
260
+ return [indices % shape[0], int(indices / shape[0] % shape[1])]
261
+
262
+ def flip(arr, axis=None):
263
+ if axis is None:
264
+ return af.flip(af.flip(arr, 0), 1)
265
+ else:
266
+ return af.flip(arr, axis)
267
+
268
+ def gaussian(dims, sigmas, **kwargs):
269
+ alpha = 1.0
270
+ grid = af.constant(1.0, dims[0], dims[1])
271
+ for i in range(len(sigmas)):
272
+ multiplier = - 0.5 * alpha / pow(sigmas[i], 2)
273
+ exponent = af.pow((af.range(dims[0], dims[1], dim=i) - (dims[i] - 1) / 2.0), 2) * multiplier
274
+ grid = grid * af.arith.exp(exponent)
275
+ return grid / af.sum(grid)
276
+
277
+ def gaussian_filter(arr, sigma, **kwargs):
278
+ dims = arr.dims()
279
+ sigmas = [dim / (2.0 * np.pi * sigma) for dim in dims]
280
+ dist = aflib2.gaussian(dims, sigmas)
281
+ arr_f = aflib2.ifftshift(af.fft2(aflib2.ifftshift(arr)))
282
+ filter = arr_f * dist
283
+ filter = aflib2.ifftshift(af.ifft2(aflib2.ifftshift(filter)))
284
+ filter = af.real(filter)
285
+ filter = af.select(filter >= 0, filter, 0.0)
286
+ return filter
287
+
288
+
289
+ class aflib3(aflib):
290
+ def fftshift(arr):
291
+ return af.shift(arr, math.ceil(arr.dims()[0] / 2) - 1, math.ceil(arr.dims()[1] / 2) - 1,
292
+ math.ceil(arr.dims()[2] / 2) - 1)
293
+
294
+ def ifftshift(arr):
295
+ return af.shift(arr, math.ceil(arr.dims()[0] / 2), math.ceil(arr.dims()[1] / 2), math.ceil(arr.dims()[2] / 2))
296
+
297
+ def shift(arr, sft):
298
+ return af.shift(arr, math.ceil(sft[0]), math.ceil(sft[1]), math.ceil(sft[2]))
299
+
300
+ def fft(arr):
301
+ return af.fft3(arr)
302
+
303
+ def ifft(arr):
304
+ return af.ifft3(arr)
305
+
306
+ def unravel_index(indices, shape):
307
+ return [indices % shape[0], int(indices / shape[0] % shape[1]), int(indices / (shape[0] * shape[1]))]
308
+
309
+ def flip(arr, axis=None):
310
+ if axis is None:
311
+ return af.flip(af.flip(af.flip(arr, 0), 1), 2)
312
+ else:
313
+ return af.flip(arr, axis)
314
+
315
+ def gaussian(dims, sigmas, **kwargs):
316
+ alpha = 1.0
317
+ grid = af.constant(1.0, dims[0], dims[1], dims[2])
318
+ for i in range(len(sigmas)):
319
+ multiplier = - 0.5 * alpha / pow(sigmas[i], 2)
320
+ exponent = af.pow((af.range(dims[0], dims[1], dims[2], dim=i) - (dims[i] - 1) / 2.0), 2) * multiplier
321
+ grid = grid * af.arith.exp(exponent)
322
+ return grid / af.sum(grid)
323
+
324
+ def gaussian_filter(arr, sigma, **kwargs):
325
+ dims = arr.dims()
326
+ if type(sigma) == int or type(sigma) == float:
327
+ sigmas = [dim / (2.0 * math.pi * sigma) for dim in dims]
328
+ else:
329
+ sigmas = sigma
330
+
331
+ dist = aflib3.gaussian(dims, sigmas)
332
+
333
+ arr_sum = af.sum(arr)
334
+ arr_f = aflib3.ifftshift(aflib3.fft(aflib3.ifftshift(arr)))
335
+ convag = arr_f * dist
336
+ convag = aflib3.ifftshift(aflib3.ifft(aflib3.ifftshift(convag)))
337
+ convag = af.real(convag)
338
+ convag = aflib3.where(convag > 0, convag, 0.0)
339
+ correction = arr_sum / af.sum(convag)
340
+ convag *= correction
341
+ return convag
342
+
343
+ def diff(arr, axis=None, prepend=0):
344
+ raise NotImplementedError
345
+
346
+ def gradient(arr, dx=1):
347
+ raise NotImplementedError
348
+
349
+ def argmin(arr, axis=None):
350
+ raise NotImplementedError
351
+
352
+ def take_along_axis(a, indices, axis):
353
+ raise NotImplementedError
354
+
355
+ def moveaxis(arr, source, dest):
356
+ raise NotImplementedError
357
+
358
+ def lstsq(A, B):
359
+ raise NotImplementedError
360
+
361
+ def zeros(shape):
362
+ raise NotImplementedError
363
+
364
+ def indices(dims):
365
+ raise NotImplementedError
366
+
367
+ def concatenate(tup, axis=0):
368
+ raise NotImplementedError
369
+
370
+ def stack(tup):
371
+ raise NotImplementedError
372
+
373
+ def amin(arr):
374
+ raise NotImplementedError
375
+
376
+ def affine_transform(arr, matrix, order=3, offset=0):
377
+ raise NotImplementedError
378
+
379
+ def pad(arr, padding):
380
+ raise NotImplementedError
381
+
382
+ def histogram2d(meas, rec, n_bins=100, log=False):
383
+ raise NotImplementedError
384
+
385
+ def calc_nmi(hgram):
386
+ raise NotImplementedError
387
+
388
+ def calc_ehd(hgram):
389
+ raise NotImplementedError
390
+
391
+ def clean_default_mem():
392
+ pass
393
+
@@ -0,0 +1,50 @@
1
+ # #########################################################################
2
+ # Copyright (c) , UChicago Argonne, LLC. All rights reserved. #
3
+ # #
4
+ # See LICENSE file. #
5
+ # #########################################################################
6
+
7
+ from pathlib import Path
8
+ import re
9
+
10
+
11
+ def main():
12
+ lib_dir = Path(__file__).parent.as_posix()
13
+ cplib = Path(f"{lib_dir}/cplib.py").read_text()
14
+ cohlib = Path(f"{lib_dir}/cohlib.py").read_text()
15
+ nplib = Path(f"{lib_dir}/nplib.py").read_text()
16
+ torchlib = Path(f"{lib_dir}/torchlib.py").read_text()
17
+
18
+ p0 = re.compile(r" def (\S+)\(")
19
+
20
+ needs_subclasshook = []
21
+ needs_abstractmethod = []
22
+ needs_npfunction = []
23
+ needs_torchfunction = []
24
+ for funk in re.findall(p0, cplib):
25
+ p1 = re.compile(rf"callable\(subclass.{funk}\)")
26
+ p2 = re.compile(rf"hasattr\(subclass, '{funk}'\)")
27
+ p3 = re.compile(rf"def {funk}\(")
28
+ if not (re.search(p1, cohlib) and re.search(p2, cohlib)):
29
+ needs_subclasshook.append(funk)
30
+ if not re.search(p3, cohlib):
31
+ needs_abstractmethod.append(funk)
32
+ if not re.search(p3, nplib):
33
+ needs_npfunction.append(funk)
34
+ if not re.search(p3, torchlib):
35
+ needs_torchfunction.append(funk)
36
+
37
+ names = ["cohlib", "cohlib.__subclasshook__", "nplib", "torchlib"]
38
+ libs = [needs_subclasshook, needs_abstractmethod, needs_npfunction, needs_torchfunction]
39
+ if not any([len(lib) for lib in libs]):
40
+ print("All devlibs are up to date!")
41
+ return
42
+ for name, lib in zip(names, libs):
43
+ if len(lib):
44
+ print(f"\nMissing from {name}:")
45
+ for s in lib:
46
+ print(f"\t{s}")
47
+
48
+
49
+ if __name__ == "__main__":
50
+ main()