froog 0.4.2__py3-none-any.whl → 0.5.1__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.
froog/__init__.py CHANGED
@@ -1,3 +1,36 @@
1
1
  import froog.optim
2
2
  import froog.tensor
3
- import froog.utils
3
+ import froog.utils
4
+
5
+ # Import GPU packages
6
+ import froog.gpu.cl.cl_utils
7
+
8
+ # Try to import Metal utils if available
9
+ try:
10
+ import froog.gpu.metal.metal_utils
11
+ except ImportError:
12
+ pass
13
+
14
+ # Import device management functions
15
+ from froog.gpu import (
16
+ Device, OpenCLDevice, get_device, set_device,
17
+ upload_tensor, download_tensor, is_buffer,
18
+ allocate_buffer, synchronize, get_available_devices
19
+ )
20
+
21
+ # Try to import Metal device if available
22
+ try:
23
+ from froog.gpu.metal import MetalDevice
24
+ __all__ = [
25
+ 'Device', 'OpenCLDevice', 'MetalDevice',
26
+ 'get_device', 'set_device', 'upload_tensor',
27
+ 'download_tensor', 'is_buffer',
28
+ 'allocate_buffer', 'synchronize', 'get_available_devices'
29
+ ]
30
+ except ImportError:
31
+ __all__ = [
32
+ 'Device', 'OpenCLDevice',
33
+ 'get_device', 'set_device', 'upload_tensor',
34
+ 'download_tensor', 'is_buffer',
35
+ 'allocate_buffer', 'synchronize', 'get_available_devices'
36
+ ]
@@ -7,16 +7,15 @@
7
7
  # |___| |___| |_||_______||_______||_______|
8
8
 
9
9
  import numpy as np
10
+ from typing import Callable, Union, Any, Tuple
10
11
  from froog.tensor import Tensor
11
12
  from froog.utils import mask_like
12
13
 
13
- def jacobian(model, input):
14
+ def jacobian(model: Callable[[Tensor], Tensor], input: Tensor) -> np.ndarray:
14
15
  output = model(input)
15
-
16
16
  ji = input.data.reshape(-1).shape[-1] # jacobian of input
17
17
  jo = output.data.reshape(-1).shape[-1] # jacobian of output
18
18
  J = np.zeros((jo, ji), dtype=np.float32)
19
-
20
19
  for o in range(jo):
21
20
  o_scalar = Tensor(mask_like(output.data, o, 1.)).mul(output).sum()
22
21
  o_scalar.backward()
@@ -24,7 +23,7 @@ def jacobian(model, input):
24
23
  J[o,i] = grad
25
24
  return J
26
25
 
27
- def numerical_jacobian(model, input, eps = 1e-6):
26
+ def numerical_jacobian(model: Callable[[Tensor], Tensor], input: Tensor, eps: float = 1e-6) -> np.ndarray:
28
27
  # """
29
28
  # https://timvieira.github.io/blog/post/2017/04/21/how-to-test-gradient-implementations/
30
29
  # Computes :
@@ -37,24 +36,18 @@ def numerical_jacobian(model, input, eps = 1e-6):
37
36
  # NJ : an approx. of the Jacobian
38
37
  # """
39
38
  output = model(input)
40
-
41
39
  ji = input.data.reshape(-1).shape[-1]
42
40
  jo = output.data.reshape(-1).shape[-1]
43
41
  NJ = np.zeros((jo, ji), dtype=np.float32)
44
-
45
42
  for i in range(ji):
46
43
  eps_perturb = mask_like(input.data, i, mask_value = eps)
47
-
48
44
  output_perturb_add = model(Tensor(input.data + eps_perturb)).data.reshape(-1)
49
45
  output_perturb_sub = model(Tensor(input.data - eps_perturb)).data.reshape(-1)
50
-
51
46
  grad_approx = ((output_perturb_add) - (output_perturb_sub)) / (2*eps) # CDM: (f(x + h) - f(x - h)) / (2 * h)
52
-
53
47
  NJ[:,i] = grad_approx
54
-
55
48
  return NJ
56
49
 
57
- def gradcheck(model, input, eps = 1e-06, atol = 1e-5, rtol = 0.001):
50
+ def gradcheck(model: Callable[[Tensor], Tensor], input: Tensor, eps: float = 1e-06, atol: float = 1e-5, rtol: float = 0.001) -> bool:
58
51
  """
59
52
  Checks whether computed gradient is close to numerical approximation of the Jacobian
60
53
  Params: