Skip to content

Commit c025215

Browse files
authored
Merge branch 'main' into fix/verify-existing-optimizations-error-handling
2 parents 999e708 + 4e7b14e commit c025215

40 files changed

Lines changed: 7299 additions & 862 deletions
Binary file not shown.
Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,39 @@
11
from concurrent.futures import ThreadPoolExecutor
2-
from time import sleep
32

43

54
def funcA(number):
6-
number = number if number < 1000 else 1000
5+
number = number if number < 100 else 100
76
k = 0
8-
for i in range(number * 100):
7+
for i in range(number * 10):
98
k += i
10-
# Simplify the for loop by using sum with a range object
119
j = sum(range(number))
12-
13-
# Use a generator expression directly in join for more efficiency
1410
return " ".join(str(i) for i in range(number))
1511

1612

1713
def test_threadpool() -> None:
18-
pool = ThreadPoolExecutor(max_workers=3)
19-
args = list(range(10, 31, 10))
14+
pool = ThreadPoolExecutor(max_workers=2)
15+
args = [5, 10, 15]
2016
result = pool.map(funcA, args)
2117

2218
for r in result:
2319
print(r)
2420

2521
class AlexNet:
26-
def __init__(self, num_classes=1000):
22+
def __init__(self, num_classes=10):
2723
self.num_classes = num_classes
28-
self.features_size = 256 * 6 * 6
2924

3025
def forward(self, x):
31-
features = self._extract_features(x)
32-
33-
output = self._classify(features)
34-
return output
35-
36-
def _extract_features(self, x):
37-
result = []
38-
for i in range(len(x)):
39-
pass
40-
41-
return result
42-
43-
def _classify(self, features):
44-
total = sum(features)
45-
return [total % self.num_classes for _ in features]
46-
47-
class SimpleModel:
48-
@staticmethod
49-
def predict(data):
50-
result = []
51-
sleep(0.1) # can be optimized away
52-
for i in range(500):
53-
for x in data:
54-
computation = 0
55-
computation += x * i ** 2
56-
result.append(computation)
57-
return result
58-
59-
@classmethod
60-
def create_default(cls):
61-
return cls()
26+
result = 0
27+
for val in x:
28+
result += val * val
29+
return result % self.num_classes
6230

6331

6432
def test_models():
6533
model = AlexNet(num_classes=10)
6634
input_data = [1, 2, 3, 4, 5]
6735
result = model.forward(input_data)
6836

69-
model2 = SimpleModel.create_default()
70-
prediction = model2.predict(input_data)
71-
7237
if __name__ == "__main__":
7338
test_threadpool()
7439
test_models()
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
Utility functions used in CompEcon
3+
4+
Based routines found in the CompEcon toolbox by Miranda and Fackler.
5+
6+
References
7+
----------
8+
Miranda, Mario J, and Paul L Fackler. Applied Computational Economics
9+
and Finance, MIT Press, 2002.
10+
11+
"""
12+
from functools import reduce
13+
import numpy as np
14+
import torch
15+
16+
def _gridmake2(x1, x2):
17+
"""
18+
Expands two vectors (or matrices) into a matrix where rows span the
19+
cartesian product of combinations of the input arrays. Each column of the
20+
input arrays will correspond to one column of the output matrix.
21+
22+
Parameters
23+
----------
24+
x1 : np.ndarray
25+
First vector to be expanded.
26+
27+
x2 : np.ndarray
28+
Second vector to be expanded.
29+
30+
Returns
31+
-------
32+
out : np.ndarray
33+
The cartesian product of combinations of the input arrays.
34+
35+
Notes
36+
-----
37+
Based of original function ``gridmake2`` in CompEcon toolbox by
38+
Miranda and Fackler.
39+
40+
References
41+
----------
42+
Miranda, Mario J, and Paul L Fackler. Applied Computational Economics
43+
and Finance, MIT Press, 2002.
44+
45+
"""
46+
if x1.ndim == 1 and x2.ndim == 1:
47+
return np.column_stack([np.tile(x1, x2.shape[0]),
48+
np.repeat(x2, x1.shape[0])])
49+
elif x1.ndim > 1 and x2.ndim == 1:
50+
first = np.tile(x1, (x2.shape[0], 1))
51+
second = np.repeat(x2, x1.shape[0])
52+
return np.column_stack([first, second])
53+
else:
54+
raise NotImplementedError("Come back here")
55+
56+
57+
def _gridmake2_torch(x1: torch.Tensor, x2: torch.Tensor) -> torch.Tensor:
58+
"""
59+
PyTorch version of _gridmake2.
60+
61+
Expands two tensors into a matrix where rows span the cartesian product
62+
of combinations of the input tensors. Each column of the input tensors
63+
will correspond to one column of the output matrix.
64+
65+
Parameters
66+
----------
67+
x1 : torch.Tensor
68+
First tensor to be expanded.
69+
70+
x2 : torch.Tensor
71+
Second tensor to be expanded.
72+
73+
Returns
74+
-------
75+
out : torch.Tensor
76+
The cartesian product of combinations of the input tensors.
77+
78+
Notes
79+
-----
80+
Based on original function ``gridmake2`` in CompEcon toolbox by
81+
Miranda and Fackler.
82+
83+
References
84+
----------
85+
Miranda, Mario J, and Paul L Fackler. Applied Computational Economics
86+
and Finance, MIT Press, 2002.
87+
88+
"""
89+
if x1.dim() == 1 and x2.dim() == 1:
90+
# tile x1 by x2.shape[0] times, repeat_interleave x2 by x1.shape[0]
91+
first = x1.tile(x2.shape[0])
92+
second = x2.repeat_interleave(x1.shape[0])
93+
return torch.column_stack([first, second])
94+
elif x1.dim() > 1 and x2.dim() == 1:
95+
# tile x1 along first dimension
96+
first = x1.tile(x2.shape[0], 1)
97+
second = x2.repeat_interleave(x1.shape[0])
98+
return torch.column_stack([first, second])
99+
else:
100+
raise NotImplementedError("Come back here")

0 commit comments

Comments
 (0)