DepthTensor 2.0.0__tar.gz → 2.0.1__tar.gz

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 (28) hide show
  1. {depthtensor-2.0.0/src/DepthTensor.egg-info → depthtensor-2.0.1}/PKG-INFO +1 -1
  2. {depthtensor-2.0.0 → depthtensor-2.0.1}/pyproject.toml +1 -1
  3. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/__init__.py +1 -1
  4. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/ops/diff.py +87 -39
  5. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/utils.py +2 -0
  6. {depthtensor-2.0.0 → depthtensor-2.0.1/src/DepthTensor.egg-info}/PKG-INFO +1 -1
  7. depthtensor-2.0.1/src/example/test.py +10 -0
  8. depthtensor-2.0.0/src/example/test.py +0 -7
  9. {depthtensor-2.0.0 → depthtensor-2.0.1}/LICENSE +0 -0
  10. {depthtensor-2.0.0 → depthtensor-2.0.1}/README.md +0 -0
  11. {depthtensor-2.0.0 → depthtensor-2.0.1}/setup.cfg +0 -0
  12. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/__init__.py +0 -0
  13. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/exceptions.py +0 -0
  14. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/ops/__init__.py +0 -0
  15. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/ops/comparison.py +0 -0
  16. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/ops/creation.py +0 -0
  17. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/ops/elementwise.py +0 -0
  18. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/ops/reduction.py +0 -0
  19. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/random/__init__.py +0 -0
  20. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/_core/random/generator.py +0 -0
  21. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/autodiff.py +0 -0
  22. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/tensor.py +0 -0
  23. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor/typing.py +0 -0
  24. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor.egg-info/SOURCES.txt +0 -0
  25. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
  26. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor.egg-info/requires.txt +0 -0
  27. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/DepthTensor.egg-info/top_level.txt +0 -0
  28. {depthtensor-2.0.0 → depthtensor-2.0.1}/src/example/example.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: DepthTensor
3
- Version: 2.0.0
3
+ Version: 2.0.1
4
4
  Description-Content-Type: text/markdown
5
5
  License-File: LICENSE
6
6
  Requires-Dist: numpy
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "DepthTensor"
7
- version = "2.0.0"
7
+ version = "2.0.1"
8
8
  readme = "README.md"
9
9
  dependencies = [
10
10
  "numpy"
@@ -6,4 +6,4 @@ from ._core.ops import *
6
6
  from ._core.exceptions import *
7
7
  from ._core import random
8
8
 
9
- __version__ = "2.0.0"
9
+ __version__ = "2.0.1"
@@ -1,18 +1,12 @@
1
- from typing import (
2
- Callable
3
- )
1
+ from typing import Callable
4
2
 
5
- from ...typing import (
6
- OperandLike,
7
- TensorLike
8
- )
3
+ from ...typing import OperandLike, TensorLike
9
4
 
10
- from ..utils import sum_to_shape, to_xp_array
11
- from ..exceptions import (
12
- CuPyNotFound, CUPY_NOT_FOUND_MSG
13
- )
5
+ from ..utils import sum_to_shape, to_xp_array, get_two_operand_op_device
6
+ from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
14
7
 
15
8
  import numpy as np
9
+
16
10
  try:
17
11
  import cupy as cp
18
12
  except (ImportError, ModuleNotFoundError):
@@ -22,133 +16,187 @@ except (ImportError, ModuleNotFoundError):
22
16
  ###
23
17
  ###
24
18
 
25
- def wrapper_2in_diff(result: TensorLike, x1: OperandLike, x2: OperandLike, callback_x1: Callable, callback_x2: Callable) -> TensorLike:
26
- if not result.requires_grad: return result
19
+
20
+ def wrapper_2in_diff(
21
+ result: TensorLike,
22
+ x1: OperandLike,
23
+ x2: OperandLike,
24
+ callback_x1: Callable,
25
+ callback_x2: Callable,
26
+ ) -> TensorLike:
27
+ if not result.requires_grad:
28
+ return result
27
29
  from ...tensor import Tensor
30
+
28
31
  def backward() -> None:
29
- _x1, _x2 = to_xp_array(x1), to_xp_array(x2)
32
+ device = get_two_operand_op_device(x1, x2, None)
33
+ _x1, _x2 = to_xp_array(x1, device=device), to_xp_array(x2, device=device)
34
+
30
35
  if isinstance(x1, Tensor) and x1.requires_grad:
31
- x1.grad += callback_x1(x1.shape, x1.device, _x1, _x2)
36
+ x1.grad += callback_x1(x1.shape, device, _x1, _x2)
32
37
  if isinstance(x2, Tensor) and x2.requires_grad:
33
- x2.grad += callback_x2(x2.shape, x2.device, _x1, _x2)
38
+ x2.grad += callback_x2(x2.shape, device, _x1, _x2)
39
+
34
40
  result.backward = backward
35
41
  return result
36
42
 
37
- def wrapper_1in_diff(result: TensorLike, x: OperandLike, callback_x1: Callable) -> TensorLike:
38
- if not result.requires_grad: return result
43
+
44
+ def wrapper_1in_diff(
45
+ result: TensorLike, x: OperandLike, callback_x1: Callable
46
+ ) -> TensorLike:
47
+ if not result.requires_grad:
48
+ return result
39
49
  from ...tensor import Tensor
50
+
40
51
  def backward() -> None:
41
52
  _x = to_xp_array(x)
42
53
  if isinstance(x, Tensor) and x.requires_grad:
43
54
  x.grad += callback_x1(x.shape, x.device, _x)
55
+
44
56
  result.backward = backward
45
57
  return result
46
58
 
59
+
47
60
  ###
48
61
  ### Arithmetics
49
62
  ###
50
63
 
64
+
51
65
  def add_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
52
66
  def callback_x1(shape, device, _x1, _x2):
53
67
  return sum_to_shape(result.grad, shape, device)
68
+
54
69
  def callback_x2(shape, device, _x1, _x2):
55
70
  return sum_to_shape(result.grad, shape, device)
71
+
56
72
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
57
73
 
74
+
58
75
  def subtract_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
59
76
  def callback_x1(shape, device, _x1, _x2):
60
77
  return sum_to_shape(result.grad, shape, device)
78
+
61
79
  def callback_x2(shape, device, _x1, _x2):
62
80
  return sum_to_shape(-result.grad, shape, device)
81
+
63
82
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
64
83
 
84
+
65
85
  def multiply_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
66
86
  def callback_x1(shape, device, _x1, _x2):
67
87
  return sum_to_shape(result.grad * _x2, shape, device)
88
+
68
89
  def callback_x2(shape, device, _x1, _x2):
69
90
  return sum_to_shape(result.grad * _x1, shape, device)
91
+
70
92
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
71
93
 
94
+
72
95
  def matmul_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
73
96
  def callback_x1(shape, device, _x1, _x2):
74
- return sum_to_shape(result.grad @ _x2.swapaxes(-2,-1), shape, device)
97
+ return sum_to_shape(result.grad @ _x2.swapaxes(-2, -1), shape, device)
98
+
75
99
  def callback_x2(shape, device, _x1, _x2):
76
100
  return sum_to_shape(_x1.swapaxes(-2, -1) @ result.grad, shape, device)
101
+
77
102
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
78
103
 
104
+
79
105
  def divide_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
80
106
  def callback_x1(shape, device, _x1, _x2):
81
107
  return sum_to_shape(result.grad / _x2, shape, device)
108
+
82
109
  def callback_x2(shape, device, _x1, _x2):
83
- return sum_to_shape(result.grad * (_x1 * -_x2**-2), shape, device)
110
+ return sum_to_shape(result.grad * (_x1 * -(_x2**-2)), shape, device)
111
+
84
112
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
85
113
 
114
+
86
115
  def power_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
87
116
  def callback_x1(shape, device, _x1, _x2):
88
- return sum_to_shape(result.grad * _x2*_x1**(_x2-1), shape, device)
117
+ return sum_to_shape(result.grad * _x2 * _x1 ** (_x2 - 1), shape, device)
118
+
89
119
  def callback_x2(shape, device, _x1, _x2):
90
120
  if device == "cpu":
91
- return sum_to_shape(result.grad * np.log(_x1)*_x1**_x2, shape, device)
121
+ return sum_to_shape(result.grad * np.log(_x1) * _x1**_x2, shape, device)
92
122
  else:
93
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
94
- return sum_to_shape(result.grad * cp.log(_x1)*_x1**_x2, shape, device)
123
+ if cp is None:
124
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
125
+ return sum_to_shape(result.grad * cp.log(_x1) * _x1**_x2, shape, device)
126
+
95
127
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
96
128
 
129
+
97
130
  def negative_diff(result: TensorLike, x: TensorLike) -> TensorLike:
98
131
  def callback_x1(shape, device, _x):
99
132
  return sum_to_shape(-result.data, shape, device)
133
+
100
134
  return wrapper_1in_diff(result, x, callback_x1)
101
135
 
136
+
102
137
  def sign_diff(result: TensorLike, x: TensorLike) -> TensorLike:
103
138
  def callback_x1(shape, device, _x):
104
139
  return sum_to_shape(result.grad * 0, shape, device)
140
+
105
141
  return wrapper_1in_diff(result, x, callback_x1)
106
142
 
143
+
107
144
  def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
108
145
  def callback_x1(shape, device, _x):
109
146
  return sum_to_shape(result.grad * result.data / _x, shape, device)
147
+
110
148
  return wrapper_1in_diff(result, x, callback_x1)
111
149
 
150
+
112
151
  ###
113
152
  ### Exponents/Logarithms
114
153
  ###
115
154
 
155
+
116
156
  def exp_diff(result: TensorLike, x: TensorLike) -> TensorLike:
117
157
  def callback_x1(shape, device, _x):
118
158
  return sum_to_shape(result.grad * result.data, shape, device)
159
+
119
160
  return wrapper_1in_diff(result, x, callback_x1)
120
161
 
162
+
121
163
  def sqrt_diff(result: TensorLike, x: TensorLike) -> TensorLike:
122
164
  def callback_x1(shape, device, _x):
123
- return sum_to_shape(result.grad * (0.5*result.data**(-0.5)), shape, device)
165
+ return sum_to_shape(result.grad * (0.5 * result.data ** (-0.5)), shape, device)
166
+
124
167
  return wrapper_1in_diff(result, x, callback_x1)
125
168
 
169
+
126
170
  def log_diff(result: TensorLike, x: TensorLike) -> TensorLike:
127
171
  def callback_x1(shape, device, _x):
128
172
  return sum_to_shape(result.grad / _x, shape, device)
173
+
129
174
  return wrapper_1in_diff(result, x, callback_x1)
130
175
 
176
+
131
177
  def square_diff(result: TensorLike, x: TensorLike) -> TensorLike:
132
178
  def callback_x1(shape, device, _x):
133
179
  return sum_to_shape(result.grad * 2 * _x, shape, device)
180
+
134
181
  return wrapper_1in_diff(result, x, callback_x1)
135
182
 
183
+
136
184
  ###
137
185
  ###
138
186
  ###
139
187
 
140
188
  __all__ = [
141
- 'add_diff',
142
- 'subtract_diff',
143
- 'multiply_diff',
144
- 'matmul_diff',
145
- 'divide_diff',
146
- 'power_diff',
147
- 'negative_diff',
148
- 'sign_diff',
149
- 'abs_diff',
150
- 'exp_diff',
151
- 'sqrt_diff',
152
- 'log_diff',
153
- 'square_diff'
154
- ]
189
+ "add_diff",
190
+ "subtract_diff",
191
+ "multiply_diff",
192
+ "matmul_diff",
193
+ "divide_diff",
194
+ "power_diff",
195
+ "negative_diff",
196
+ "sign_diff",
197
+ "abs_diff",
198
+ "exp_diff",
199
+ "sqrt_diff",
200
+ "log_diff",
201
+ "square_diff",
202
+ ]
@@ -185,6 +185,7 @@ def get_two_operand_op_device(
185
185
  tuple,
186
186
  ), # * Universal datatypes regardless of devices
187
187
  ):
188
+ # * This leaves x2 to be either np.array or cp.array.
188
189
  raise DeviceMismatch(
189
190
  f"There is a incompatibility in datatypes between the two operands of types Tensor and {type(x2)}. Expected datatype of device: {x1.device} {"(CuPy arrays, GPU Tensors)" if x1.device == "gpu" else "(NumPy arrays, CPU Tensors, ints, floats, lists, tuples)"}, got datatype of type: {type(x2)}."
190
191
  )
@@ -199,6 +200,7 @@ def get_two_operand_op_device(
199
200
  tuple,
200
201
  ), # * Universal datatypes regardless of devices
201
202
  ):
203
+ # * This leaves x1 to be either np.array or cp.array.
202
204
  raise DeviceMismatch(
203
205
  f"There is a incompatibility in datatypes between the two operands of types {type(x1)} and Tensor. Expected datatype of device: {x2.device} {"(CuPy arrays, GPU Tensors)" if x2.device == "gpu" else "(NumPy arrays, CPU Tensors, ints, floats, lists, tuples)"}, got datatype of type: {type(x1)}."
204
206
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: DepthTensor
3
- Version: 2.0.0
3
+ Version: 2.0.1
4
4
  Description-Content-Type: text/markdown
5
5
  License-File: LICENSE
6
6
  Requires-Dist: numpy
@@ -0,0 +1,10 @@
1
+ from .. import DepthTensor as DTensor
2
+
3
+ import numpy as np
4
+ import cupy as cp
5
+
6
+ a = DTensor.Tensor(0.1, requires_grad=True, device="gpu")
7
+ b = a**2
8
+ DTensor.differentiate(b)
9
+ print(b)
10
+ print(a.grad)
@@ -1,7 +0,0 @@
1
- from .. import DepthTensor as DTensor
2
-
3
- import numpy as np
4
- import cupy as cp
5
-
6
- a = DTensor.add(cp.array(1.0), np.array(2.0))
7
- print(a)
File without changes
File without changes
File without changes