DepthTensor 2.0.1__tar.gz → 2.1.0__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 (27) hide show
  1. {depthtensor-2.0.1/src/DepthTensor.egg-info → depthtensor-2.1.0}/PKG-INFO +1 -1
  2. {depthtensor-2.0.1 → depthtensor-2.1.0}/pyproject.toml +1 -1
  3. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/__init__.py +1 -1
  4. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/ops/comparison.py +3 -8
  5. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/ops/creation.py +0 -4
  6. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/ops/diff.py +73 -43
  7. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/ops/elementwise.py +31 -37
  8. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/ops/reduction.py +6 -4
  9. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/random/generator.py +48 -34
  10. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/autodiff.py +17 -8
  11. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/tensor.py +31 -73
  12. {depthtensor-2.0.1 → depthtensor-2.1.0/src/DepthTensor.egg-info}/PKG-INFO +1 -1
  13. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor.egg-info/SOURCES.txt +1 -3
  14. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor.egg-info/top_level.txt +0 -1
  15. depthtensor-2.0.1/src/example/example.py +0 -39
  16. depthtensor-2.0.1/src/example/test.py +0 -10
  17. {depthtensor-2.0.1 → depthtensor-2.1.0}/LICENSE +0 -0
  18. {depthtensor-2.0.1 → depthtensor-2.1.0}/README.md +0 -0
  19. {depthtensor-2.0.1 → depthtensor-2.1.0}/setup.cfg +0 -0
  20. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/__init__.py +0 -0
  21. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/exceptions.py +0 -0
  22. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
  23. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/random/__init__.py +0 -0
  24. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/_core/utils.py +2 -2
  25. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor/typing.py +0 -0
  26. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
  27. {depthtensor-2.0.1 → depthtensor-2.1.0}/src/DepthTensor.egg-info/requires.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: DepthTensor
3
- Version: 2.0.1
3
+ Version: 2.1.0
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.1"
7
+ version = "2.1.0"
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.1"
9
+ __version__ = "2.1.0"
@@ -75,12 +75,7 @@ def where(
75
75
  if cp is None:
76
76
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
77
77
  result = cp.where(data)
78
- return tuple(
79
- [
80
- Tensor(array, device=device, requires_grad=requires_grad)
81
- for array in result
82
- ]
83
- )
78
+ return tuple([Tensor(array, requires_grad=requires_grad) for array in result])
84
79
  # * Two parameters overload
85
80
  elif x is not None and y is not None:
86
81
  if (
@@ -99,7 +94,7 @@ def where(
99
94
  if cp is None:
100
95
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
101
96
  result = cp.where(data, x_data, y_data)
102
- return Tensor(result, device=device, requires_grad=requires_grad)
97
+ return Tensor(result, requires_grad=requires_grad)
103
98
  else:
104
99
  raise ValueError("Both x and y parameters must be given.")
105
100
 
@@ -143,7 +138,7 @@ def wrapper_2in_1out(
143
138
  if cp is None:
144
139
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
145
140
  y = getattr(cp, func_name)(x1, x2, out=out, dtype=dtype, casting=casting)
146
- return Tensor(y, device=op_device)
141
+ return Tensor(y)
147
142
 
148
143
 
149
144
  def equal(
@@ -39,7 +39,6 @@ def zeros_like(
39
39
  if device_op == "cpu":
40
40
  return Tensor(
41
41
  np.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
42
- device=device_op,
43
42
  requires_grad=requires_grad,
44
43
  )
45
44
  else:
@@ -47,7 +46,6 @@ def zeros_like(
47
46
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
48
47
  return Tensor(
49
48
  cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
50
- device=device_op,
51
49
  requires_grad=requires_grad,
52
50
  )
53
51
 
@@ -73,7 +71,6 @@ def ones_like(
73
71
  if device_op == "cpu":
74
72
  return Tensor(
75
73
  np.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
76
- device=device_op,
77
74
  requires_grad=requires_grad,
78
75
  )
79
76
  else:
@@ -81,7 +78,6 @@ def ones_like(
81
78
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
82
79
  return Tensor(
83
80
  cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
84
- device=device_op,
85
81
  requires_grad=requires_grad,
86
82
  )
87
83
 
@@ -1,6 +1,6 @@
1
1
  from typing import Callable
2
2
 
3
- from ...typing import OperandLike, TensorLike
3
+ from ...typing import OperandLike, TensorLike, NDArrayLike
4
4
 
5
5
  from ..utils import sum_to_shape, to_xp_array, get_two_operand_op_device
6
6
  from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
@@ -29,13 +29,25 @@ def wrapper_2in_diff(
29
29
  from ...tensor import Tensor
30
30
 
31
31
  def backward() -> None:
32
+ if result.grad is None:
33
+ result.zeros_grad()
34
+
35
+ result_grad: NDArrayLike = result.grad
32
36
  device = get_two_operand_op_device(x1, x2, None)
33
37
  _x1, _x2 = to_xp_array(x1, device=device), to_xp_array(x2, device=device)
34
38
 
35
39
  if isinstance(x1, Tensor) and x1.requires_grad:
36
- x1.grad += callback_x1(x1.shape, device, _x1, _x2)
40
+ if x1.grad is None:
41
+ x1.zeros_grad()
42
+ x1.grad += callback_x1(result_grad, x1.shape, device, _x1, _x2).astype(
43
+ x1.dtype
44
+ )
37
45
  if isinstance(x2, Tensor) and x2.requires_grad:
38
- x2.grad += callback_x2(x2.shape, device, _x1, _x2)
46
+ if x2.grad is None:
47
+ x2.zeros_grad()
48
+ x2.grad += callback_x2(result_grad, x2.shape, device, _x1, _x2).astype(
49
+ x2.dtype
50
+ )
39
51
 
40
52
  result.backward = backward
41
53
  return result
@@ -49,9 +61,15 @@ def wrapper_1in_diff(
49
61
  from ...tensor import Tensor
50
62
 
51
63
  def backward() -> None:
64
+ if result.grad is None:
65
+ result.zeros_grad()
66
+
67
+ result_grad: NDArrayLike = result.grad
52
68
  _x = to_xp_array(x)
53
69
  if isinstance(x, Tensor) and x.requires_grad:
54
- x.grad += callback_x1(x.shape, x.device, _x)
70
+ if x.grad is None:
71
+ x.zeros_grad()
72
+ x.grad += callback_x1(result_grad, x.shape, x.device, _x)
55
73
 
56
74
  result.backward = backward
57
75
  return result
@@ -63,87 +81,95 @@ def wrapper_1in_diff(
63
81
 
64
82
 
65
83
  def add_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
66
- def callback_x1(shape, device, _x1, _x2):
67
- return sum_to_shape(result.grad, shape, device)
84
+ def callback_x1(result_grad, shape, device, _x1, _x2):
85
+ return sum_to_shape(result_grad, shape, device)
68
86
 
69
- def callback_x2(shape, device, _x1, _x2):
70
- return sum_to_shape(result.grad, shape, device)
87
+ def callback_x2(result_grad, shape, device, _x1, _x2):
88
+ return sum_to_shape(result_grad, shape, device)
71
89
 
72
90
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
73
91
 
74
92
 
75
93
  def subtract_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
76
- def callback_x1(shape, device, _x1, _x2):
77
- return sum_to_shape(result.grad, shape, device)
78
94
 
79
- def callback_x2(shape, device, _x1, _x2):
80
- return sum_to_shape(-result.grad, shape, device)
95
+ def callback_x1(result_grad, shape, device, _x1, _x2):
96
+ return sum_to_shape(result_grad, shape, device)
97
+
98
+ def callback_x2(result_grad, shape, device, _x1, _x2):
99
+ return sum_to_shape(-result_grad, shape, device)
81
100
 
82
101
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
83
102
 
84
103
 
85
104
  def multiply_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
86
- def callback_x1(shape, device, _x1, _x2):
87
- return sum_to_shape(result.grad * _x2, shape, device)
88
105
 
89
- def callback_x2(shape, device, _x1, _x2):
90
- return sum_to_shape(result.grad * _x1, shape, device)
106
+ def callback_x1(result_grad, shape, device, _x1, _x2):
107
+ return sum_to_shape(result_grad * _x2, shape, device)
108
+
109
+ def callback_x2(result_grad, shape, device, _x1, _x2):
110
+ return sum_to_shape(result_grad * _x1, shape, device)
91
111
 
92
112
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
93
113
 
94
114
 
95
115
  def matmul_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
96
- def callback_x1(shape, device, _x1, _x2):
97
- return sum_to_shape(result.grad @ _x2.swapaxes(-2, -1), shape, device)
98
116
 
99
- def callback_x2(shape, device, _x1, _x2):
100
- return sum_to_shape(_x1.swapaxes(-2, -1) @ result.grad, shape, device)
117
+ def callback_x1(result_grad, shape, device, _x1, _x2):
118
+ return sum_to_shape(result_grad @ _x2.swapaxes(-2, -1), shape, device)
119
+
120
+ def callback_x2(result_grad, shape, device, _x1, _x2):
121
+ return sum_to_shape(_x1.swapaxes(-2, -1) @ result_grad, shape, device)
101
122
 
102
123
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
103
124
 
104
125
 
105
126
  def divide_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
106
- def callback_x1(shape, device, _x1, _x2):
107
- return sum_to_shape(result.grad / _x2, shape, device)
108
127
 
109
- def callback_x2(shape, device, _x1, _x2):
110
- return sum_to_shape(result.grad * (_x1 * -(_x2**-2)), shape, device)
128
+ def callback_x1(result_grad, shape, device, _x1, _x2):
129
+ return sum_to_shape(result_grad / _x2, shape, device)
130
+
131
+ def callback_x2(result_grad, shape, device, _x1, _x2):
132
+ return sum_to_shape(result_grad * (_x1 * -(_x2**-2)), shape, device)
111
133
 
112
134
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
113
135
 
114
136
 
115
137
  def power_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
116
- def callback_x1(shape, device, _x1, _x2):
117
- return sum_to_shape(result.grad * _x2 * _x1 ** (_x2 - 1), shape, device)
118
138
 
119
- def callback_x2(shape, device, _x1, _x2):
139
+ def callback_x1(result_grad, shape, device, _x1, _x2):
140
+ return sum_to_shape(result_grad * _x2 * _x1 ** (_x2 - 1), shape, device)
141
+
142
+ def callback_x2(result_grad, shape, device, _x1, _x2):
120
143
  if device == "cpu":
121
- return sum_to_shape(result.grad * np.log(_x1) * _x1**_x2, shape, device)
144
+ return sum_to_shape(result_grad * np.log(_x1) * _x1**_x2, shape, device)
122
145
  else:
123
146
  if cp is None:
124
147
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
125
- return sum_to_shape(result.grad * cp.log(_x1) * _x1**_x2, shape, device)
148
+ return sum_to_shape(result_grad * cp.log(_x1) * _x1**_x2, shape, device)
126
149
 
127
150
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
128
151
 
129
152
 
130
153
  def negative_diff(result: TensorLike, x: TensorLike) -> TensorLike:
131
- def callback_x1(shape, device, _x):
132
- return sum_to_shape(-result.data, shape, device)
154
+
155
+ def callback_x1(result_grad, shape, device, _x):
156
+ return sum_to_shape(-result_grad, shape, device)
133
157
 
134
158
  return wrapper_1in_diff(result, x, callback_x1)
135
159
 
136
160
 
137
161
  def sign_diff(result: TensorLike, x: TensorLike) -> TensorLike:
138
- def callback_x1(shape, device, _x):
139
- return sum_to_shape(result.grad * 0, shape, device)
162
+
163
+ def callback_x1(result_grad, shape, device, _x):
164
+ return sum_to_shape(result_grad * 0, shape, device)
140
165
 
141
166
  return wrapper_1in_diff(result, x, callback_x1)
142
167
 
143
168
 
144
169
  def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
145
- def callback_x1(shape, device, _x):
146
- return sum_to_shape(result.grad * result.data / _x, shape, device)
170
+
171
+ def callback_x1(result_grad, shape, device, _x):
172
+ return sum_to_shape(result_grad * result.data / _x, shape, device)
147
173
 
148
174
  return wrapper_1in_diff(result, x, callback_x1)
149
175
 
@@ -154,29 +180,33 @@ def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
154
180
 
155
181
 
156
182
  def exp_diff(result: TensorLike, x: TensorLike) -> TensorLike:
157
- def callback_x1(shape, device, _x):
158
- return sum_to_shape(result.grad * result.data, shape, device)
183
+
184
+ def callback_x1(result_grad, shape, device, _x):
185
+ return sum_to_shape(result_grad * result.data, shape, device)
159
186
 
160
187
  return wrapper_1in_diff(result, x, callback_x1)
161
188
 
162
189
 
163
190
  def sqrt_diff(result: TensorLike, x: TensorLike) -> TensorLike:
164
- def callback_x1(shape, device, _x):
165
- return sum_to_shape(result.grad * (0.5 * result.data ** (-0.5)), shape, device)
191
+
192
+ def callback_x1(result_grad, shape, device, _x):
193
+ return sum_to_shape(result_grad * (0.5 * result.data ** (-0.5)), shape, device)
166
194
 
167
195
  return wrapper_1in_diff(result, x, callback_x1)
168
196
 
169
197
 
170
198
  def log_diff(result: TensorLike, x: TensorLike) -> TensorLike:
171
- def callback_x1(shape, device, _x):
172
- return sum_to_shape(result.grad / _x, shape, device)
199
+
200
+ def callback_x1(result_grad, shape, device, _x):
201
+ return sum_to_shape(result_grad / _x, shape, device)
173
202
 
174
203
  return wrapper_1in_diff(result, x, callback_x1)
175
204
 
176
205
 
177
206
  def square_diff(result: TensorLike, x: TensorLike) -> TensorLike:
178
- def callback_x1(shape, device, _x):
179
- return sum_to_shape(result.grad * 2 * _x, shape, device)
207
+
208
+ def callback_x1(result_grad, shape, device, _x):
209
+ return sum_to_shape(result_grad * 2 * _x, shape, device)
180
210
 
181
211
  return wrapper_1in_diff(result, x, callback_x1)
182
212
 
@@ -70,7 +70,6 @@ def create_2in_1out(
70
70
  from ...tensor import Tensor
71
71
 
72
72
  op_device = get_two_operand_op_device(x1, x2, device)
73
-
74
73
  a1, a2 = to_xp_array(x1, device=op_device), to_xp_array(x2, device=op_device)
75
74
  y = op(a1, a2, device=op_device, **kwds)
76
75
 
@@ -91,7 +90,6 @@ def create_2in_1out(
91
90
  else:
92
91
  y = Tensor(
93
92
  y,
94
- device=op_device,
95
93
  prev=prev,
96
94
  requires_grad=y_requires_grad and requires_grad,
97
95
  )
@@ -99,13 +97,19 @@ def create_2in_1out(
99
97
  return y
100
98
  else:
101
99
  if y.requires_grad:
100
+ if y.grad is None:
101
+ y.zeros_grad()
102
102
  dx1, dx2 = diff(y, a1, a2, **kwds)
103
103
 
104
104
  def backward() -> None:
105
105
  if isinstance(x1, Tensor) and x1.requires_grad:
106
- x1.grad += y.grad * dx1()
106
+ if x1.grad is None:
107
+ x1.zeros_grad()
108
+ x1.grad += y.grad * dx1() # type: ignore
107
109
  if isinstance(x2, Tensor) and x2.requires_grad:
108
- x2.grad += y.grad * dx2()
110
+ if x2.grad is None:
111
+ x2.zeros_grad()
112
+ x2.grad += y.grad * dx2() # type: ignore
109
113
 
110
114
  y.backward = backward
111
115
  return y
@@ -126,13 +130,10 @@ def create_1in_1out(
126
130
  ) -> TensorLike:
127
131
  from ...tensor import Tensor
128
132
 
129
- if isinstance(x, Tensor):
130
- device_op = x.device
133
+ if device is None:
134
+ device_op = get_device(x)
131
135
  else:
132
- if device is None:
133
- device_op = get_device(x)
134
- else:
135
- device_op = device
136
+ device_op = device
136
137
 
137
138
  a = to_xp_array(x, device=device_op)
138
139
  y = op(a, device=device_op, **kwds)
@@ -152,7 +153,6 @@ def create_1in_1out(
152
153
  else:
153
154
  y = Tensor(
154
155
  y,
155
- device=device_op,
156
156
  prev=(x,),
157
157
  requires_grad=y_requires_grad and requires_grad,
158
158
  )
@@ -160,11 +160,15 @@ def create_1in_1out(
160
160
  return y
161
161
  else:
162
162
  if y.requires_grad:
163
+ if y.grad is None:
164
+ y.zeros_grad()
163
165
  dx = diff(y, a, **kwds)
164
166
 
165
167
  def backward() -> None:
166
168
  if isinstance(x, Tensor) and x.requires_grad:
167
- x.grad += y.grad * dx()
169
+ if x.grad is None:
170
+ x.zeros_grad()
171
+ x.grad += y.grad * dx() # type: ignore
168
172
 
169
173
  y.backward = backward
170
174
  return y
@@ -195,27 +199,20 @@ def wrapper_2in_1out(
195
199
  from ...tensor import Tensor
196
200
 
197
201
  op_device = get_two_operand_op_device(x1, x2, device)
198
-
199
- if device is not None:
200
- op_device = device
201
202
  a1, a2 = to_xp_array(x1, op_device), to_xp_array(x2, op_device)
202
203
 
203
204
  if op_device == "cpu":
205
+ kwds = {
206
+ "out": out,
207
+ "dtype": dtype,
208
+ "where": where,
209
+ "casting": casting,
210
+ "order": order,
211
+ "subok": subok,
212
+ }
204
213
  if func_name == "matmul":
205
- y = getattr(np, func_name)(
206
- a1, a2, out=out, dtype=dtype, casting=casting, order=order, subok=subok
207
- )
208
- else:
209
- y = getattr(np, func_name)(
210
- a1,
211
- a2,
212
- out=out,
213
- dtype=dtype,
214
- where=where,
215
- casting=casting,
216
- order=order,
217
- subok=subok,
218
- )
214
+ del kwds["where"]
215
+ y = getattr(np, func_name)(a1, a2, **kwds)
219
216
  else:
220
217
  if cp is None:
221
218
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
@@ -226,7 +223,7 @@ def wrapper_2in_1out(
226
223
  return x1
227
224
 
228
225
  requires_grad, prev = get_requires_grad_and_prev(x1, x2)
229
- return Tensor(y, device=op_device, prev=prev, requires_grad=requires_grad)
226
+ return Tensor(y, prev=prev, requires_grad=requires_grad)
230
227
 
231
228
 
232
229
  def wrapper_1in_1out(
@@ -245,13 +242,10 @@ def wrapper_1in_1out(
245
242
  ) -> TensorLike:
246
243
  from ...tensor import Tensor
247
244
 
248
- if isinstance(x, Tensor):
249
- device_op = x.device
245
+ if device is None:
246
+ device_op = get_device(device)
250
247
  else:
251
- if device is None:
252
- device_op = get_device(device)
253
- else:
254
- device_op = device
248
+ device_op = device
255
249
 
256
250
  a = to_xp_array(x, device=device_op)
257
251
  if device_op == "cpu":
@@ -275,7 +269,7 @@ def wrapper_1in_1out(
275
269
  x.data = y
276
270
  return x
277
271
  requires_grad = x.requires_grad
278
- return Tensor(y, device=device_op, prev=(x,), requires_grad=requires_grad)
272
+ return Tensor(y, prev=(x,), requires_grad=requires_grad)
279
273
 
280
274
 
281
275
  ###
@@ -66,7 +66,7 @@ def sum(
66
66
  if cp is None:
67
67
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
68
68
  y = cp.sum(arr, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
69
- return Tensor(y, device=device_op, requires_grad=requires_grad)
69
+ return Tensor(y, requires_grad=requires_grad)
70
70
 
71
71
 
72
72
  def max(
@@ -97,7 +97,7 @@ def max(
97
97
  if cp is None:
98
98
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
99
99
  y = cp.max(arr, axis=axis, out=out, keepdims=keepdims)
100
- return Tensor(y, device=device_op, requires_grad=requires_grad)
100
+ return Tensor(y, requires_grad=requires_grad)
101
101
 
102
102
 
103
103
  def maximum(
@@ -133,8 +133,10 @@ def maximum(
133
133
  subok=subok,
134
134
  )
135
135
  else:
136
- y = np.maximum(_x1, _x2, out=out, dtype=dtype, casting=casting)
137
- return Tensor(y, device=device_op, requires_grad=requires_grad)
136
+ if cp is None:
137
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
138
+ y = cp.maximum(_x1, _x2, out=out, dtype=dtype, casting=casting)
139
+ return Tensor(y, requires_grad=requires_grad)
138
140
 
139
141
 
140
142
  ###
@@ -1,32 +1,25 @@
1
- from typing import (
2
- Optional,
3
- overload,
4
- Any
5
- )
1
+ from typing import Optional, overload, Any
6
2
 
7
3
  from numpy import random
8
4
 
9
- from ...typing import (
10
- DTypeLike,
11
- TensorLike,
12
- AxisLike,
13
- int64,
14
- DeviceLike
15
- )
5
+ from ...typing import DTypeLike, TensorLike, AxisLike, int64, DeviceLike
16
6
 
17
- from ..exceptions import (
18
- CuPyNotFound, CUPY_NOT_FOUND_MSG
19
- )
7
+ from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
8
+
9
+ import numpy as np
20
10
 
21
11
  try:
22
12
  import cupy as cp
23
13
  except (ImportError, ModuleNotFoundError):
24
14
  cp = None
25
15
 
16
+ _RNG = np.random.default_rng()
17
+
26
18
  ###
27
19
  ###
28
20
  ###
29
21
 
22
+
30
23
  @overload
31
24
  def rand(*, device: DeviceLike = "cpu", requires_grad: bool = False) -> TensorLike: ...
32
25
  @overload
@@ -35,7 +28,7 @@ def rand(
35
28
  dtype: Optional[DTypeLike] = None,
36
29
  device: DeviceLike = "cpu",
37
30
  requires_grad: bool = False
38
- ) -> TensorLike: ...
31
+ ) -> TensorLike: ...
39
32
  def rand(
40
33
  *d: int,
41
34
  dtype: Optional[DTypeLike] = None,
@@ -43,13 +36,23 @@ def rand(
43
36
  requires_grad: bool = False
44
37
  ) -> TensorLike:
45
38
  from ...tensor import Tensor
39
+
46
40
  if device == "cpu":
47
- y = random.rand(*d)
41
+ if dtype is None:
42
+ y = _RNG.random(size=d)
43
+ elif dtype is np.float32 or dtype is np.float64:
44
+ y = _RNG.random(size=d, dtype=dtype)
45
+ else:
46
+ raise RuntimeError(
47
+ "The 'dtype' argument must be a type either a float64 or a float32."
48
+ )
48
49
  else:
49
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
50
+ if cp is None:
51
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
50
52
  y = cp.random.rand(*d, dtype=dtype)
51
- return Tensor(y, dtype=dtype, requires_grad=requires_grad)
52
-
53
+ return Tensor(y, requires_grad=requires_grad)
54
+
55
+
53
56
  @overload
54
57
  def randn(*, device: DeviceLike = "cpu", requires_grad: bool = False) -> TensorLike: ...
55
58
  @overload
@@ -58,7 +61,7 @@ def randn(
58
61
  dtype: Optional[DTypeLike] = None,
59
62
  device: DeviceLike = "cpu",
60
63
  requires_grad: bool = False
61
- ) -> TensorLike: ...
64
+ ) -> TensorLike: ...
62
65
  def randn(
63
66
  *d: int,
64
67
  dtype: Optional[DTypeLike] = None,
@@ -66,29 +69,42 @@ def randn(
66
69
  requires_grad: bool = False
67
70
  ) -> TensorLike:
68
71
  from ...tensor import Tensor
72
+
69
73
  if device == "cpu":
70
- y = random.randn(*d)
74
+ if dtype is None:
75
+ y = _RNG.standard_normal(d)
76
+ elif dtype is np.float32 or dtype is np.float64:
77
+ y = _RNG.standard_normal(d, dtype=dtype)
78
+ else:
79
+ raise RuntimeError(
80
+ "The 'dtype' argument must be a type either a float64 or a float32."
81
+ )
71
82
  else:
72
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
83
+ if cp is None:
84
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
73
85
  y = cp.random.randn(*d, dtype=dtype)
74
- return Tensor(y, dtype=dtype, requires_grad=requires_grad)
75
-
86
+ return Tensor(y, requires_grad=requires_grad)
87
+
88
+
76
89
  def randint(
77
90
  low: int,
78
91
  high: Optional[int] = None,
79
92
  size: Optional[AxisLike] = None,
80
93
  dtype: Any = int64,
81
94
  device: DeviceLike = "cpu",
82
- requires_grad: bool = False
95
+ requires_grad: bool = False,
83
96
  ) -> TensorLike:
84
97
  from ...tensor import Tensor
98
+
85
99
  if device == "cpu":
86
100
  y = random.randint(low=low, high=high, size=size, dtype=dtype)
87
101
  else:
88
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
102
+ if cp is None:
103
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
89
104
  y = cp.random.randint(low=low, high=high, size=size, dtype=dtype)
90
105
  return Tensor(y, requires_grad=requires_grad)
91
106
 
107
+
92
108
  def uniform(
93
109
  low: float = 0.0,
94
110
  high: float = 1.0,
@@ -98,20 +114,18 @@ def uniform(
98
114
  requires_grad: bool = False
99
115
  ):
100
116
  from ...tensor import Tensor
117
+
101
118
  if device == "cpu":
102
119
  y = random.uniform(low=low, high=high, size=size)
103
120
  else:
104
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
121
+ if cp is None:
122
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
105
123
  y = cp.random.uniform(low=low, high=high, size=size)
106
124
  return Tensor(y, requires_grad=requires_grad)
107
125
 
126
+
108
127
  ###
109
128
  ###
110
129
  ###
111
130
 
112
- __all__ = [
113
- 'rand',
114
- 'randn',
115
- 'randint',
116
- 'uniform'
117
- ]
131
+ __all__ = ["rand", "randn", "randint", "uniform"]
@@ -2,16 +2,20 @@ from typing import List, Set
2
2
 
3
3
  from . import Tensor
4
4
  from ._core.exceptions import (
5
- GradientComputationError, GRADIENT_COMPUTATION_ERROR,
6
- CuPyNotFound, CUPY_NOT_FOUND_MSG
5
+ GradientComputationError,
6
+ GRADIENT_COMPUTATION_ERROR,
7
+ CuPyNotFound,
8
+ CUPY_NOT_FOUND_MSG,
7
9
  )
8
10
 
9
11
  import numpy as np
12
+
10
13
  try:
11
14
  import cupy as cp
12
15
  except (ModuleNotFoundError, ImportError):
13
16
  cp = None
14
17
 
18
+
15
19
  def differentiate(tensor: Tensor) -> List[Tensor]:
16
20
  topo: List[Tensor] = []
17
21
  visited: Set[Tensor] = set()
@@ -20,7 +24,7 @@ def differentiate(tensor: Tensor) -> List[Tensor]:
20
24
  if t in visited:
21
25
  return
22
26
  visited.add(t)
23
-
27
+
24
28
  for prev in t.prev:
25
29
  build(prev)
26
30
  topo.append(t)
@@ -28,7 +32,8 @@ def differentiate(tensor: Tensor) -> List[Tensor]:
28
32
  build(tensor)
29
33
 
30
34
  if tensor.device == "gpu":
31
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
35
+ if cp is None:
36
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
32
37
  tensor.grad = cp.ones(tensor.shape, tensor.dtype)
33
38
  elif tensor.device == "cpu":
34
39
  tensor.grad = np.ones(tensor.shape, tensor.dtype)
@@ -36,9 +41,13 @@ def differentiate(tensor: Tensor) -> List[Tensor]:
36
41
  for t in reversed(topo):
37
42
  if t.backward is None:
38
43
  if not t.requires_grad:
39
- raise GradientComputationError(f"Attempted to propagate backward off of a tensor ({t}), which is not differentiable.")
44
+ raise GradientComputationError(
45
+ f"Attempted to propagate backward off of a tensor ({t}), which is not differentiable."
46
+ )
40
47
  if len(t.prev) > 0:
41
- raise GradientComputationError(f"Tensor ({t})'s backward function is None.")
42
- continue #* t has no preceeding operations, so t is a leaf node, which cannot be propagated further.
48
+ raise GradientComputationError(
49
+ f"Tensor ({t})'s backward function is None."
50
+ )
51
+ continue
43
52
  t.backward()
44
- return topo
53
+ return topo
@@ -14,7 +14,6 @@ from .typing import (
14
14
  )
15
15
 
16
16
  from ._core import (
17
- xp_array_to_device,
18
17
  CuPyNotFound,
19
18
  CUPY_NOT_FOUND_MSG,
20
19
  # * elementwise
@@ -48,6 +47,8 @@ from ._core import (
48
47
  sum,
49
48
  )
50
49
 
50
+ from ._core.utils import get_device, to_xp_array, xp_array_to_device
51
+
51
52
  import numpy as np
52
53
 
53
54
  try:
@@ -84,15 +85,18 @@ def _wrapper_1in_1out(
84
85
  return y
85
86
 
86
87
 
88
+ allowed_dtype_kind = "uifb"
89
+
87
90
  ###
88
91
  ###
89
92
  ###
90
93
 
91
94
 
92
95
  class Tensor:
96
+ data: NDArrayLike
93
97
  device: DeviceLike
98
+ grad: Optional[NDArrayLike]
94
99
  backward: Optional[Callable[[], None]]
95
- data: NDArrayLike
96
100
 
97
101
  def __init__(
98
102
  self,
@@ -100,7 +104,7 @@ class Tensor:
100
104
  /,
101
105
  *,
102
106
  dtype: Optional[DTypeLike] = None,
103
- device: DeviceLike = "cpu",
107
+ device: Optional[DeviceLike] = None,
104
108
  prev: Tuple = (),
105
109
  requires_grad: bool = False,
106
110
  copy: bool = True,
@@ -109,94 +113,48 @@ class Tensor:
109
113
  ndmin: int = 0,
110
114
  blocking: bool = False,
111
115
  ) -> None:
112
- # * Convert to xp.ndarray
116
+ if device is None:
117
+ self.device = get_device(obj)
118
+ else:
119
+ self.device = device
120
+
113
121
  if isinstance(obj, np.ndarray):
114
- if obj.dtype.kind in "uifb":
115
- self.data = xp_array_to_device(obj, device)
116
- self.device = "cpu"
122
+ if obj.dtype.kind in allowed_dtype_kind:
123
+ self.data = xp_array_to_device(obj, self.device)
117
124
  else:
118
125
  raise TypeError("Expected a numerical NumPy array.")
119
126
  else:
120
127
  if cp is not None and isinstance(obj, cp.ndarray):
121
- if obj.dtype.kind in "uifb": # type: ignore
122
- self.data = xp_array_to_device(obj, device)
123
- self.device = "gpu"
128
+ if obj.dtype.kind in allowed_dtype_kind: # type: ignore
129
+ self.data = xp_array_to_device(obj, self.device)
124
130
  else:
125
131
  raise TypeError("Expected a numerical CuPy array.")
132
+ elif isinstance(obj, Tensor):
133
+ self.data = xp_array_to_device(obj.data, self.device)
126
134
  else:
127
- if device == "gpu":
128
- if cp is None:
129
- raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
130
- if isinstance(
131
- obj,
132
- (int, float, List, Tuple, cp.floating, cp.integer, cp.bool_),
133
- ):
134
- self.data = cp.array(
135
- obj,
136
- dtype=dtype,
137
- copy=copy,
138
- order=order,
139
- subok=subok,
140
- ndmin=ndmin,
141
- blocking=blocking,
142
- )
143
- self.device = "gpu"
144
- elif isinstance(obj, Tensor):
145
- self.data = cp.array(
146
- obj.data,
147
- dtype=dtype,
148
- copy=copy,
149
- order=order,
150
- subok=subok,
151
- ndmin=ndmin,
152
- blocking=blocking,
153
- )
154
- self.device = "gpu"
155
- else:
156
- raise RuntimeError(
157
- "Expected data of type: int, float, list, tuple, numpy.ndarray, cupy.ndarray."
158
- )
159
- else:
160
- if isinstance(
161
- obj, (int, float, List, Tuple, np.integer, np.floating, np.bool)
162
- ):
163
- self.data = np.array(
164
- obj,
165
- dtype=dtype,
166
- copy=copy,
167
- order=order,
168
- subok=subok,
169
- ndmin=ndmin,
170
- )
171
- self.device = "cpu"
172
- elif isinstance(obj, Tensor):
173
- self.data = np.array(
174
- obj.data,
175
- dtype=dtype,
176
- copy=copy,
177
- order=order,
178
- subok=subok,
179
- ndmin=ndmin,
180
- )
181
- self.device = "cpu"
182
- else:
183
- raise RuntimeError(
184
- "Expected data of type: int, float, list, tuple, numpy.ndarray, cupy.ndarray."
185
- )
135
+ self.data = xp_array_to_device(
136
+ to_xp_array(obj, self.device), self.device
137
+ )
138
+
139
+ # TODO: Use all the parameters.
186
140
 
187
141
  # * Convert to dtype (if provided)
188
142
  if dtype is not None and dtype != self.data.dtype:
189
143
  self.data = self.data.astype(dtype)
190
144
  self.prev = prev
191
145
  self.requires_grad = requires_grad
146
+ self.backward = None
147
+ self.grad = None
192
148
 
193
- if device == "cpu":
194
- self.grad = np.zeros_like(self.data)
149
+ def zeros_grad(self) -> NDArrayLike:
150
+ if self.device == "cpu":
151
+ grad = np.zeros_like(self.data)
195
152
  else:
196
153
  if cp is None:
197
154
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
198
- self.grad = cp.zeros_like(self.data)
199
- self.backward = None
155
+ grad = cp.zeros_like(self.data)
156
+ self.grad = grad
157
+ return grad
200
158
 
201
159
  ###
202
160
  ###
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: DepthTensor
3
- Version: 2.0.1
3
+ Version: 2.1.0
4
4
  Description-Content-Type: text/markdown
5
5
  License-File: LICENSE
6
6
  Requires-Dist: numpy
@@ -20,6 +20,4 @@ src/DepthTensor/_core/ops/diff.py
20
20
  src/DepthTensor/_core/ops/elementwise.py
21
21
  src/DepthTensor/_core/ops/reduction.py
22
22
  src/DepthTensor/_core/random/__init__.py
23
- src/DepthTensor/_core/random/generator.py
24
- src/example/example.py
25
- src/example/test.py
23
+ src/DepthTensor/_core/random/generator.py
@@ -1,39 +0,0 @@
1
- import DepthTensor as DTensor
2
- from ..DepthTensor import (
3
- Tensor,
4
- differentiate,
5
- create_1in_1out,
6
- NDArrayLike,
7
- DeviceLike,
8
- CuPyNotFound,
9
- CUPY_NOT_FOUND_MSG,
10
- )
11
-
12
- import numpy as np
13
-
14
- try:
15
- import cupy as cp
16
- except:
17
- cp = None
18
-
19
-
20
- def op(x: NDArrayLike, device: DeviceLike = "cpu", **kwds) -> NDArrayLike:
21
- print(type(x))
22
- return DTensor.add(x, x)
23
-
24
-
25
- def diff(result: Tensor, x: NDArrayLike, **kwds):
26
- def x_diff() -> NDArrayLike:
27
- return 1
28
-
29
- return x_diff
30
-
31
-
32
- func = create_1in_1out(op, diff)
33
- a = Tensor(6.0, device="gpu", requires_grad=True)
34
- b = func(a, device="gpu")
35
- print(b)
36
- print(b.requires_grad)
37
- print(b.prev)
38
- differentiate(b)
39
- print(a.grad)
@@ -1,10 +0,0 @@
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)
File without changes
File without changes
File without changes
@@ -164,11 +164,11 @@ def get_complement_device(device: DeviceLike) -> DeviceLike:
164
164
  def get_two_operand_op_device(
165
165
  x1: OperandLike, x2: OperandLike, device: Optional[DeviceLike]
166
166
  ) -> DeviceLike:
167
- from ..tensor import Tensor
168
-
169
167
  if device is not None:
170
168
  return device
171
169
 
170
+ from ..tensor import Tensor
171
+
172
172
  if isinstance(x1, Tensor) and isinstance(x2, Tensor):
173
173
  if x1.device != x2.device:
174
174
  raise DeviceMismatch(DEVICE_MISMATCH_MSG)