python-examples 0.2.0__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.
- Algorithms/arrays/subsequence_conflict.py +35 -0
- Algorithms/matrices/utils.py +59 -0
- Algorithms/numerical_methods/coin_run.py +77 -0
- Algorithms/numerical_methods/euler/p0.py +9 -0
- Algorithms/numerical_methods/euler/p1.py +8 -0
- Algorithms/numerical_methods/euler/p2.py +16 -0
- Algorithms/numerical_methods/euler/p48.py +8 -0
- Algorithms/numerical_methods/euler/p49.py +100 -0
- Algorithms/numerical_methods/euler/p50.py +22 -0
- Algorithms/numerical_methods/euler/p51.py +45 -0
- Algorithms/numerical_methods/euler/p54.py +161 -0
- Algorithms/numerical_methods/euler/utils.py +16 -0
- Algorithms/slidingWindow/longestUnique.py +37 -0
- Algorithms/static_analysis/ast_tree.py +76 -0
- Algorithms/static_analysis/deduplicate_security_findings.py +22 -0
- Algorithms/static_analysis/package_dependency.py +70 -0
- Algorithms/static_analysis/rule_matcher.py +70 -0
- Algorithms/tensors/broadcast.py +98 -0
- Algorithms/tensors/kmeans.py +49 -0
- LLMs/torch_examples/batch_agg.py +78 -0
- LLMs/torch_examples/pooling.py +26 -0
- LLMs/torch_examples/shape_literacy.py +37 -0
- LLMs/xai.py +92 -0
- mixins/__init__.py +0 -0
- mixins/benchmark.py +16 -0
- mixins/logging.py +7 -0
- python_examples-0.2.0.dist-info/METADATA +32 -0
- python_examples-0.2.0.dist-info/RECORD +31 -0
- python_examples-0.2.0.dist-info/WHEEL +5 -0
- python_examples-0.2.0.dist-info/licenses/LICENSE +674 -0
- python_examples-0.2.0.dist-info/top_level.txt +3 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from typing import List, Dict
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def deduplicate(findings: List[Dict]) -> List[Dict]:
|
|
5
|
+
seen = set()
|
|
6
|
+
output = []
|
|
7
|
+
for finding in findings:
|
|
8
|
+
key = (finding["file"], finding["line"], finding["rule"])
|
|
9
|
+
if key not in seen:
|
|
10
|
+
seen.add(key)
|
|
11
|
+
output.append(finding)
|
|
12
|
+
|
|
13
|
+
return output
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
findings = [
|
|
17
|
+
{"file": "a.py", "line": 10, "rule": "SQL001"},
|
|
18
|
+
{"file": "a.py", "line": 10, "rule": "SQL001"},
|
|
19
|
+
{"file": "b.py", "line": 20, "rule": "XSS001"},
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
print(deduplicate(findings))
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from typing import Dict, List
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def find_dependency_cycle(deps: Dict[str, List[str]]) -> List[List[str]]:
|
|
5
|
+
"""
|
|
6
|
+
dfs back edges for cycle detection
|
|
7
|
+
"""
|
|
8
|
+
visited = set()
|
|
9
|
+
visiting = set()
|
|
10
|
+
# there may be more than one cycle
|
|
11
|
+
path = []
|
|
12
|
+
cycles = []
|
|
13
|
+
|
|
14
|
+
def dfs(node):
|
|
15
|
+
if node in visiting:
|
|
16
|
+
idx = path.index(node)
|
|
17
|
+
cycles.append(path[idx:] + [node])
|
|
18
|
+
return
|
|
19
|
+
|
|
20
|
+
if node in visited:
|
|
21
|
+
return
|
|
22
|
+
|
|
23
|
+
visiting.add(node)
|
|
24
|
+
visited.add(node)
|
|
25
|
+
path.append(node)
|
|
26
|
+
|
|
27
|
+
for neighbor in deps[node]:
|
|
28
|
+
dfs(neighbor)
|
|
29
|
+
|
|
30
|
+
path.pop()
|
|
31
|
+
visiting.remove(node)
|
|
32
|
+
|
|
33
|
+
for node in deps:
|
|
34
|
+
if node not in visited:
|
|
35
|
+
dfs(node)
|
|
36
|
+
|
|
37
|
+
return cycles
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def find_dependency_cycle_stack(deps: Dict[str, List[str]]) -> List[List[str]]:
|
|
41
|
+
"""
|
|
42
|
+
dfs back edges for cycle detection
|
|
43
|
+
"""
|
|
44
|
+
visited = set()
|
|
45
|
+
visiting = set()
|
|
46
|
+
# there may be more than one cycle
|
|
47
|
+
path = []
|
|
48
|
+
cycles = []
|
|
49
|
+
for start in deps:
|
|
50
|
+
if start not in visited:
|
|
51
|
+
stack = [(start, False)]
|
|
52
|
+
while stack:
|
|
53
|
+
node, exiting = stack.pop()
|
|
54
|
+
if exiting:
|
|
55
|
+
path.pop()
|
|
56
|
+
visiting.remove(node)
|
|
57
|
+
visited.add(node)
|
|
58
|
+
# back edge
|
|
59
|
+
if node in visiting:
|
|
60
|
+
idx = path.index(node)
|
|
61
|
+
cycles.append(path[idx:] + [node])
|
|
62
|
+
continue
|
|
63
|
+
if node in visited:
|
|
64
|
+
continue
|
|
65
|
+
visiting.add(node)
|
|
66
|
+
path.append(node)
|
|
67
|
+
stack.append((node, True))
|
|
68
|
+
for neighbor in reversed(deps[node]):
|
|
69
|
+
stack.append((neighbor, False))
|
|
70
|
+
return cycles
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from typing import List, Dict
|
|
2
|
+
import re
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def find_violations(code: List[str], rules: List[str]):
|
|
6
|
+
"""
|
|
7
|
+
This function takes a list of code lines and a list of rules, and returns a list of violations found in the code.
|
|
8
|
+
"""
|
|
9
|
+
violations = []
|
|
10
|
+
for rule in rules:
|
|
11
|
+
pattern = rule["pattern"]
|
|
12
|
+
severity = rule["severity"]
|
|
13
|
+
for line_number, line in enumerate(code):
|
|
14
|
+
if pattern in line:
|
|
15
|
+
violations.append(
|
|
16
|
+
{
|
|
17
|
+
"line_number": line_number + 1,
|
|
18
|
+
"line": line,
|
|
19
|
+
"pattern": pattern,
|
|
20
|
+
"severity": severity,
|
|
21
|
+
}
|
|
22
|
+
)
|
|
23
|
+
return violations
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def compile_rule(pattern):
|
|
27
|
+
METAVAR = re.compile(r"\$([A-Za-z_][A-Za-z0-9_]*)")
|
|
28
|
+
captures = []
|
|
29
|
+
regex_parts = []
|
|
30
|
+
last = 0
|
|
31
|
+
for match in METAVAR.finditer(pattern):
|
|
32
|
+
regex_parts.append(re.escape(pattern[last : match.start()]))
|
|
33
|
+
name = match.group(1)
|
|
34
|
+
captures.append(name)
|
|
35
|
+
|
|
36
|
+
regex_parts.append(f"(?P<{name}>.*?)")
|
|
37
|
+
last = match.end()
|
|
38
|
+
regex_parts.append(re.escape(pattern[last:]))
|
|
39
|
+
return re.compile("^" + "".join(regex_parts) + "$"), captures
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def find_violations_v2(code: List[str], rules: List[Dict]) -> List[Dict]:
|
|
43
|
+
"""
|
|
44
|
+
Support wildcards
|
|
45
|
+
"""
|
|
46
|
+
compiled_rules = []
|
|
47
|
+
for rule in rules:
|
|
48
|
+
regex_parts, captures = compile_rule(rule["pattern"])
|
|
49
|
+
compiled_rules.append(
|
|
50
|
+
{
|
|
51
|
+
"regex": regex_parts,
|
|
52
|
+
"captures": captures,
|
|
53
|
+
"severity": rule["severity"],
|
|
54
|
+
"pattern": rule["pattern"],
|
|
55
|
+
}
|
|
56
|
+
)
|
|
57
|
+
violations = []
|
|
58
|
+
for line_number, line in enumerate(code):
|
|
59
|
+
for rule in compiled_rules:
|
|
60
|
+
match = rule["regex"].match(line)
|
|
61
|
+
if match:
|
|
62
|
+
violations.append(
|
|
63
|
+
{
|
|
64
|
+
"line_number": line_number + 1,
|
|
65
|
+
"pattern": rule["pattern"],
|
|
66
|
+
"severity": rule["severity"],
|
|
67
|
+
"captures": match.groupdict(),
|
|
68
|
+
}
|
|
69
|
+
)
|
|
70
|
+
return violations
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def normalize_l2(A: np.array, dim: int) -> np.array:
|
|
5
|
+
"""
|
|
6
|
+
input:
|
|
7
|
+
A: [D1,D2,...]
|
|
8
|
+
operations:
|
|
9
|
+
keepdims preserves the dimension. For instance:
|
|
10
|
+
A [B, N, D]
|
|
11
|
+
norm [B, N, 1]
|
|
12
|
+
output:
|
|
13
|
+
A/norm: [D1,D2,...] same shape but normalize along given axis/dim
|
|
14
|
+
"""
|
|
15
|
+
norm = np.sqrt((A**2).sum(axis=dim, keepdims=True))
|
|
16
|
+
return A / norm
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def layer_norm(A: np.array) -> np.array:
|
|
20
|
+
"""
|
|
21
|
+
input:
|
|
22
|
+
A: [B, N, D] (batch, seq len, feature dimension)
|
|
23
|
+
operations: keepdims used to retain original dimensions for broadcasting
|
|
24
|
+
"""
|
|
25
|
+
if len(A.shape) != 3:
|
|
26
|
+
raise ValueError("Input must be a 3D array with shape [B, N, D]")
|
|
27
|
+
means = A.mean(axis=2, keepdims=True)
|
|
28
|
+
vars = A.var(axis=2, keepdims=True)
|
|
29
|
+
|
|
30
|
+
return (A - means) / np.sqrt(vars + 1e-5)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def batch_norm(A: np.array) -> np.array:
|
|
34
|
+
"""
|
|
35
|
+
input:
|
|
36
|
+
A: [B, N, D] (batch, seq len, feature dimension)
|
|
37
|
+
operations:
|
|
38
|
+
pool batch and sequence length so we normalize each feature independently
|
|
39
|
+
using the pooled batch statistics. keepdims used to retain original dimensions for broadcasting
|
|
40
|
+
"""
|
|
41
|
+
if len(A.shape) != 3:
|
|
42
|
+
raise ValueError("Input must be a 3D array with shape [B, N, D]")
|
|
43
|
+
means = A.mean(axis=(0, 1), keepdims=True)
|
|
44
|
+
vars = A.var(axis=(0, 1), keepdims=True)
|
|
45
|
+
|
|
46
|
+
return (A - means) / np.sqrt(vars + 1e-5)
|
|
47
|
+
|
|
48
|
+
def pairwise_cosine(A: np.array) -> np.array:
|
|
49
|
+
"""
|
|
50
|
+
input:
|
|
51
|
+
A: [N, D]
|
|
52
|
+
|
|
53
|
+
output:
|
|
54
|
+
[N, N]: each entry in matrix is cosine similarity
|
|
55
|
+
"""
|
|
56
|
+
norm = np.sqrt((A**2).sum(axis=1, keepdims=True))
|
|
57
|
+
normalized = A / norm
|
|
58
|
+
return normalized @ normalized.T
|
|
59
|
+
|
|
60
|
+
def pairwise_cosine_broadcast(A: np.array) -> np.array:
|
|
61
|
+
"""
|
|
62
|
+
input:
|
|
63
|
+
A: [N, D]
|
|
64
|
+
|
|
65
|
+
output:
|
|
66
|
+
[N, N]: each entry in matrix is cosine similarity
|
|
67
|
+
"""
|
|
68
|
+
norm = np.sqrt((A**2).sum(axis=1, keepdims=True))
|
|
69
|
+
normalized = A / norm
|
|
70
|
+
return (normalized[:, None, :] * normalized[None, :, :]).sum(axis=-1)
|
|
71
|
+
|
|
72
|
+
def feature_thresholds(A: np.array, thresholds: np.array) -> np.array:
|
|
73
|
+
"""
|
|
74
|
+
input:
|
|
75
|
+
A: [B, N, D]
|
|
76
|
+
thresholds: [D] for each dimension
|
|
77
|
+
|
|
78
|
+
output:
|
|
79
|
+
[B, N, D] with features below threshold masked out
|
|
80
|
+
"""
|
|
81
|
+
mask = A > thresholds
|
|
82
|
+
return A * mask
|
|
83
|
+
|
|
84
|
+
def closest_pairwise(A: np.array) -> np.array:
|
|
85
|
+
"""
|
|
86
|
+
input:
|
|
87
|
+
A: [N, D]
|
|
88
|
+
|
|
89
|
+
operations:
|
|
90
|
+
broadcast pairwise distances, filling diagonal self distances with inf
|
|
91
|
+
as these are trivially 0.
|
|
92
|
+
|
|
93
|
+
output:
|
|
94
|
+
result: [N], the index closest to each point
|
|
95
|
+
"""
|
|
96
|
+
pairwise_dists = np.sum((A[:, None, :] - A[None, :, :])**2, axis=2)
|
|
97
|
+
np.fill_diagonal(pairwise_dists, np.inf)
|
|
98
|
+
return np.argmin(pairwise_dists, axis=1)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
def generate_centers(k: int,
|
|
4
|
+
dim: int,
|
|
5
|
+
mn: np.array,
|
|
6
|
+
mx: np.array):
|
|
7
|
+
return mn + np.random.rand(k, dim)*(mx-mn)
|
|
8
|
+
|
|
9
|
+
def k_means(k: int, data: np.array, iterations: int):
|
|
10
|
+
"""
|
|
11
|
+
given a numpy dataset, apply vector quantization to model probability
|
|
12
|
+
density functions by distribution of prototype functions.
|
|
13
|
+
|
|
14
|
+
array of tuples to list "tolist()" for the standard matrix format.
|
|
15
|
+
Or np.vstack
|
|
16
|
+
|
|
17
|
+
## Annotate tensor sizes ##
|
|
18
|
+
data: [num_points, feature_dimension]
|
|
19
|
+
output:
|
|
20
|
+
centers: [k, feature_dimension]
|
|
21
|
+
labels: [num_points, feature_dimension]
|
|
22
|
+
"""
|
|
23
|
+
# data = np.array(data.tolist())
|
|
24
|
+
data = np.vstack(data)
|
|
25
|
+
# axis is column 0 rows 1.
|
|
26
|
+
mn, mx = data.min(axis=0), data.max(axis=0)
|
|
27
|
+
# randomly generate centers
|
|
28
|
+
centers = generate_centers(k=k,
|
|
29
|
+
dim=data.shape[-1],
|
|
30
|
+
mn=mn,
|
|
31
|
+
mx=mx)
|
|
32
|
+
for _ in range(iterations):
|
|
33
|
+
# distances: [num_points, num_centers (k), feature_dimension]
|
|
34
|
+
# squared norm, at least avoids extra square root (monotonic increasing
|
|
35
|
+
# function)
|
|
36
|
+
distances = np.sum((data[:, None, :] - centers[None, :, :]) ** 2,
|
|
37
|
+
axis=2)
|
|
38
|
+
|
|
39
|
+
# Which center is closest to each point?
|
|
40
|
+
labels = np.argmin(distances, axis=1)
|
|
41
|
+
|
|
42
|
+
# Recompute centers
|
|
43
|
+
for j in range(k):
|
|
44
|
+
points = data[labels == j]
|
|
45
|
+
|
|
46
|
+
if len(points) > 0:
|
|
47
|
+
centers[j] = points.mean(axis=0)
|
|
48
|
+
|
|
49
|
+
return centers, labels
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def standardize(M: torch.Tensor) -> torch.Tensor:
|
|
5
|
+
"""
|
|
6
|
+
standardize standardizes each row of the input matrix M to have mean 0 and std 1.
|
|
7
|
+
Arguments:
|
|
8
|
+
M: input tensor of shape (n, p)
|
|
9
|
+
Returns:
|
|
10
|
+
A tensor of shape (n, p) where each row has mean 0 and std 1.
|
|
11
|
+
"""
|
|
12
|
+
return (M - M.mean(axis=1).unsqueeze(1)) / M.std(axis=1).unsqueeze(1)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def normalize_per_batch(p: int, num_samples: int = 100, batch_size: int = 10):
|
|
16
|
+
"""
|
|
17
|
+
normalize_per_batch generates num_samples random p-dim vectors,
|
|
18
|
+
splits them into batches of size batch_size (last batch may be smaller),
|
|
19
|
+
and standardizes each batch to have mean 0 and std 1 per dimension.
|
|
20
|
+
Arguments:
|
|
21
|
+
p: dimension of the vectors
|
|
22
|
+
num_samples: total number of samples to generate
|
|
23
|
+
batch_size: size of each batch
|
|
24
|
+
Returns:
|
|
25
|
+
A tensor of shape (num_samples, p) containing the standardized vectors.
|
|
26
|
+
"""
|
|
27
|
+
if batch_size > num_samples:
|
|
28
|
+
raise ValueError("batch_size must be at most number of samples.")
|
|
29
|
+
mat = torch.randn((num_samples, p), dtype=torch.float32)
|
|
30
|
+
num_batches = num_samples // batch_size
|
|
31
|
+
remainder = num_samples % batch_size
|
|
32
|
+
num_batches += int(remainder != 0)
|
|
33
|
+
if remainder == 0:
|
|
34
|
+
# evenly split
|
|
35
|
+
batch_mat = mat.view(num_batches, batch_size, p)
|
|
36
|
+
means = batch_mat.mean(dim=1, keepdim=True) # (num_batches, 1, p)
|
|
37
|
+
stds = batch_mat.std(dim=1, keepdim=True) # (num_batches, 1, p)
|
|
38
|
+
standardized = (batch_mat - means) / (stds + 1e-8)
|
|
39
|
+
return standardized.view(num_samples, p)
|
|
40
|
+
else:
|
|
41
|
+
# scattered
|
|
42
|
+
batch_ids = torch.randint(0, num_batches, (num_samples,))
|
|
43
|
+
# count per group
|
|
44
|
+
counts = torch.bincount(batch_ids, minlength=num_batches).unsqueeze(1)
|
|
45
|
+
batch_ids_processed = batch_ids.unsqueeze(1).expand(-1, p)
|
|
46
|
+
sum_per_group = torch.zeros((num_batches, p)).scatter_add_(
|
|
47
|
+
0, batch_ids_processed, mat
|
|
48
|
+
)
|
|
49
|
+
mean_per_group = sum_per_group / counts.clamp(min=1)
|
|
50
|
+
|
|
51
|
+
sum_sq = (mat - mean_per_group[batch_ids]) ** 2
|
|
52
|
+
sum_sq_per_group = torch.zeros((num_batches, p)).scatter_add_(
|
|
53
|
+
0, batch_ids_processed, sum_sq
|
|
54
|
+
)
|
|
55
|
+
var_per_group = sum_sq_per_group / counts.clamp(min=1)
|
|
56
|
+
std_per_group = torch.sqrt(var_per_group + 1e-8)
|
|
57
|
+
return (mat - mean_per_group[batch_ids]) / (std_per_group[batch_ids])
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def sample_gaussian_pairs(p: int, num_samples: int = 10000, eps: float = 1e-8):
|
|
61
|
+
"""
|
|
62
|
+
sanple_gaussian_pairs generates num_samples pairs of p-dimensional vectors
|
|
63
|
+
from standard normal distribution, and computes their normalized inner products.
|
|
64
|
+
Highlights that high-dimensional random vectors are almost orthogonal.
|
|
65
|
+
Arguments:
|
|
66
|
+
p: dimension of the vectors
|
|
67
|
+
num_samples: number of pairs to sample
|
|
68
|
+
eps: small value to avoid division by zero
|
|
69
|
+
Returns:
|
|
70
|
+
A tensor of shape (num_samples,) containing the normalized inner products.
|
|
71
|
+
"""
|
|
72
|
+
pair_mat = torch.randn((2 * num_samples, p), dtype=torch.float32)
|
|
73
|
+
pair_mat = pair_mat.view(num_samples, 2, p)
|
|
74
|
+
norm_first = torch.norm(pair_mat[:, 0, :], dim=1)
|
|
75
|
+
norm_second = torch.norm(pair_mat[:, 1, :], dim=1)
|
|
76
|
+
dot_prod = (pair_mat[:, 0, :] * pair_mat[:, 1, :]).sum(dim=1)
|
|
77
|
+
norm_inner_prods = dot_prod / (norm_first * norm_second + eps)
|
|
78
|
+
return norm_inner_prods
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class VariableSortedHistoryPooling(torch.nn.Module):
|
|
5
|
+
def __init__(self, n_samples: int, emb_dim: int):
|
|
6
|
+
super(VariableSortedHistoryPooling, self).__init__()
|
|
7
|
+
# n samples are n events, where it's consecutive events belonging to a given user
|
|
8
|
+
# The n samples can be segmented into B users.
|
|
9
|
+
self.emb = torch.nn.Embedding(n_samples, emb_dim)
|
|
10
|
+
|
|
11
|
+
def forward(
|
|
12
|
+
self, event_indices: torch.Tensor, offsets: torch.Tensor
|
|
13
|
+
) -> torch.Tensor:
|
|
14
|
+
event_embs = self.emb(event_indices)
|
|
15
|
+
# diffs of cumulative offsets gives user lengths (number of events in history per user)
|
|
16
|
+
user_lengths = offsets[1:] - offsets[:-1]
|
|
17
|
+
user_ids = torch.repeat_interleave(
|
|
18
|
+
torch.arange(len(user_lengths), device=offsets.device), user_lengths
|
|
19
|
+
)
|
|
20
|
+
target = torch.zeros(
|
|
21
|
+
len(user_lengths), event_embs.shape[1], device=event_embs.device
|
|
22
|
+
)
|
|
23
|
+
target = target.scatter_add(
|
|
24
|
+
dim=0, index=user_ids.unsqueeze(1).expand_as(event_embs), src=event_embs
|
|
25
|
+
)
|
|
26
|
+
return target / user_lengths.clamp(min=1).unsqueeze(1)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
x = torch.randn(10)
|
|
4
|
+
y = x.unsqueeze(1)
|
|
5
|
+
print(x.shape, y.shape)
|
|
6
|
+
|
|
7
|
+
x = torch.randn(4, 1, 8)
|
|
8
|
+
y = x.squeeze()
|
|
9
|
+
print(x.shape, y.shape)
|
|
10
|
+
|
|
11
|
+
x = torch.randn(2, 3, 4)
|
|
12
|
+
print(x, x.shape)
|
|
13
|
+
# reshape to same number of elements, but different shape
|
|
14
|
+
y = x.view(12, 2)
|
|
15
|
+
print(y, y.shape)
|
|
16
|
+
z = x.reshape(12, 2)
|
|
17
|
+
print(z, z.shape)
|
|
18
|
+
|
|
19
|
+
x = torch.randn(5, 1)
|
|
20
|
+
z = x.expand(5, 3)
|
|
21
|
+
print(x, y)
|
|
22
|
+
print(x.shape, z.shape)
|
|
23
|
+
|
|
24
|
+
# copies the data
|
|
25
|
+
y = x.repeat(1, 3)
|
|
26
|
+
print(y)
|
|
27
|
+
print(y.shape)
|
|
28
|
+
y[0, 0] = 10
|
|
29
|
+
print("Y", y)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
y2 = x.expand_as(torch.randn(5, 10))
|
|
33
|
+
# expanded size to match existing size at dim 0.
|
|
34
|
+
# expand doesn't allocate new memory, so changing y2 will change x
|
|
35
|
+
y3 = x.expand(5, 2)
|
|
36
|
+
y3[0, 0] = 20
|
|
37
|
+
print(y3)
|
LLMs/xai.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
import numpy as np
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
REQUEST_QUEUE = []
|
|
6
|
+
VOCAB_SIZE = 10
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def lm_batch(prev_tokens: List[List[int]]):
|
|
10
|
+
next_tokens = []
|
|
11
|
+
for sequence in prev_tokens:
|
|
12
|
+
next_tokens.append(hash(tuple(sequence)) % VOCAB_SIZE)
|
|
13
|
+
return next_tokens
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ReturnHandle:
|
|
17
|
+
RETURN = dict()
|
|
18
|
+
|
|
19
|
+
def __init__(self, key):
|
|
20
|
+
self.key = key
|
|
21
|
+
|
|
22
|
+
def return_result(self, sequence: List[int]):
|
|
23
|
+
self.__class__.RETURN[self.key] = sequence
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class Request:
|
|
28
|
+
prompt: List[int]
|
|
29
|
+
handle: ReturnHandle
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def process_loop(batch_size=8, max_len=20, stop_token=0):
|
|
33
|
+
active = batch_size * [None]
|
|
34
|
+
while True:
|
|
35
|
+
i = 0
|
|
36
|
+
while i < batch_size:
|
|
37
|
+
elem = dequeue()
|
|
38
|
+
if elem:
|
|
39
|
+
active[i] = [elem, np.array([])]
|
|
40
|
+
else:
|
|
41
|
+
break
|
|
42
|
+
i += 1
|
|
43
|
+
print(f"Batch Length: {i}")
|
|
44
|
+
|
|
45
|
+
if i == 0:
|
|
46
|
+
return
|
|
47
|
+
for iter in range(1, max_len + 1):
|
|
48
|
+
contexts = [
|
|
49
|
+
np.concatenate((val[0].prompt, val[1]))
|
|
50
|
+
for val in active
|
|
51
|
+
if val is not None
|
|
52
|
+
]
|
|
53
|
+
next_tokens = lm_batch(contexts)
|
|
54
|
+
if len(next_tokens) == 0:
|
|
55
|
+
break
|
|
56
|
+
for j in range(len(next_tokens)):
|
|
57
|
+
if active[j]:
|
|
58
|
+
if next_tokens[j] == stop_token or iter == max_len:
|
|
59
|
+
print(f"Iter: {iter}: ", next_tokens[j])
|
|
60
|
+
active[j][0].handle.return_result(active[j][1])
|
|
61
|
+
active[j] = None
|
|
62
|
+
else:
|
|
63
|
+
# print("Before", active[j][1])
|
|
64
|
+
active[j][1] = np.append(active[j][1], next_tokens[j])
|
|
65
|
+
# print("After", active[j][1])
|
|
66
|
+
|
|
67
|
+
if all([val is None for val in active]) and not REQUEST_QUEUE:
|
|
68
|
+
return
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _enqueue(num_entries=20):
|
|
72
|
+
np.random.seed(42)
|
|
73
|
+
for i in range(num_entries):
|
|
74
|
+
prompt: List[int] = np.random.randint(0, high=10, size=100)
|
|
75
|
+
handle = ReturnHandle(i)
|
|
76
|
+
REQUEST_QUEUE.append(Request(prompt, handle))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def dequeue():
|
|
80
|
+
if REQUEST_QUEUE:
|
|
81
|
+
return REQUEST_QUEUE.pop(0)
|
|
82
|
+
else:
|
|
83
|
+
return None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
if __name__ == "__main__":
|
|
87
|
+
_enqueue(num_entries=20)
|
|
88
|
+
assert len(REQUEST_QUEUE) == 20, "Not initialized correctly"
|
|
89
|
+
process_loop()
|
|
90
|
+
print("\n", "#" * 10 + " RESULT " + "#" * 10, "\n")
|
|
91
|
+
for k, v in ReturnHandle.RETURN.items():
|
|
92
|
+
print(f"{k}: {v}")
|
mixins/__init__.py
ADDED
|
File without changes
|
mixins/benchmark.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from typing import Callable
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class BenchmarkMixin:
|
|
6
|
+
def benchmark(self, fn: Callable, *args, **kwargs):
|
|
7
|
+
start = time.perf_counter()
|
|
8
|
+
|
|
9
|
+
result = fn(*args, **kwargs)
|
|
10
|
+
|
|
11
|
+
elapsed = time.perf_counter() - start
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
"result": result,
|
|
15
|
+
"elapsed_seconds": elapsed,
|
|
16
|
+
}
|
mixins/logging.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-examples
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Algorithms and systems examples repo
|
|
5
|
+
Requires-Python: >=3.8
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: numpy>=1.23.5
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# python examples
|
|
12
|
+
Practice with Jupyter Notebook, Python, and Algorithms
|
|
13
|
+
|
|
14
|
+
#### Review
|
|
15
|
+
* Python defines a default constructor in the class, which exists regardless of created constructor unlike Java.
|
|
16
|
+
* Python has duck typing so it doesn't enforce type hints at runtime. It only cares about if the object has existing methods. Java is statically typed and enforces strict inheritance rules at compile time, so a parent object passed into an argument expecting a child class will raise a compile time error.
|
|
17
|
+
* Python has mutable default arguments, so use None on the list instead.
|
|
18
|
+
* In Python, modifying a list cannot be done while iterating over it.
|
|
19
|
+
* Python has double equal sign that checks for value equality. `is` confirms if two objects/variables are the same, so:
|
|
20
|
+
```python
|
|
21
|
+
a = 256
|
|
22
|
+
b = 256
|
|
23
|
+
print(a is b)
|
|
24
|
+
```
|
|
25
|
+
* The print statement returns True because Python optimizes by caching small values -5 to 256 and short strings into memory. However, if the two variables were assigned to 257, they are not equal.
|
|
26
|
+
* Defining a mutable value in python class body updates the value to be shared by all instances of the class.
|
|
27
|
+
* In exception handling, we have a try finally statement that both contain `return 1` and `return 2` then finally return would hijack the execution. Finally should not contain return statements.
|
|
28
|
+
* For multithreaded work, CPU bounded work will run slower due to competition for a single lock. For I/O heavy work, GIL is released when waiting on network responses and threading works good for I/O tasks.
|
|
29
|
+
* For circular imports, we can avoid this by defining a third file or by doing a local import within a method or local scope.
|
|
30
|
+
* += mutates in place but `a = a + [1]` creates a new list.
|
|
31
|
+
* `__eq__` verifies object representation of the same value. When used in a set/dictionary, `__hash__` needs to be implemented.
|
|
32
|
+
* `@staticmethod` in python is a normal function that lives in the class's namespace.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Algorithms/arrays/subsequence_conflict.py,sha256=_a9bgsLQawSBR_7oYFE_25BAywREwZwdkT3S7cfTljY,863
|
|
2
|
+
Algorithms/matrices/utils.py,sha256=SSzK-kSYXvr18pHWvcutzwupNoCvbNH1EZIcp9T2UuI,1631
|
|
3
|
+
Algorithms/numerical_methods/coin_run.py,sha256=5IrUz76NDHcPNhiwqJOrMt1AKMpbkTl9aLRXubz1YW4,2214
|
|
4
|
+
Algorithms/numerical_methods/euler/p0.py,sha256=p8W86lxtzu_DB4Bd-8KNQGMujA5MlTQmzwXozKub9cY,224
|
|
5
|
+
Algorithms/numerical_methods/euler/p1.py,sha256=Zpshpb43gaJlOz6sPneY3TW9_2PpqZsYw82S2MKjGDM,142
|
|
6
|
+
Algorithms/numerical_methods/euler/p2.py,sha256=QDgctFowoFqkgI0axlqd2zLe0yLPKRiz7KmMZoFHV8E,326
|
|
7
|
+
Algorithms/numerical_methods/euler/p48.py,sha256=oN2xwCiruIpFbMC4ADUvIb6ykVo9Y_pDHl_Y2Sov0E4,205
|
|
8
|
+
Algorithms/numerical_methods/euler/p49.py,sha256=4xCNiQT32RMKKfYx1f15g-JjMIexcuC8mmW4BiD_C_s,2735
|
|
9
|
+
Algorithms/numerical_methods/euler/p50.py,sha256=yGZw2D91jPhZtWRPZJ3pFz4fBiqWF07wPo0CA-J_goE,782
|
|
10
|
+
Algorithms/numerical_methods/euler/p51.py,sha256=_UxNHlasdUwhr05w0b_JVVdPp8egjiNw9bSd_FcTLMM,1595
|
|
11
|
+
Algorithms/numerical_methods/euler/p54.py,sha256=TRf_aKKvKnIGvZutwoVGeeRj_kNIB9-TY7tApwhuJtM,4413
|
|
12
|
+
Algorithms/numerical_methods/euler/utils.py,sha256=Rdadl4mjiRiVQ2YfvD8G0OYrdCGH_lGFda3InErMgNk,358
|
|
13
|
+
Algorithms/slidingWindow/longestUnique.py,sha256=8_8GYIsdhsG0ClREbiLkgGqDOwVTb5vx6wASEnrKRjg,1104
|
|
14
|
+
Algorithms/static_analysis/ast_tree.py,sha256=CtyLr-YY_I3JODk9JqlLv9S-5aF3ySyO5-nqNLqMEmQ,2251
|
|
15
|
+
Algorithms/static_analysis/deduplicate_security_findings.py,sha256=PV1lw-XpH6BXESxzpbMIZSudhS5LSdTg4-rwGXK05gY,524
|
|
16
|
+
Algorithms/static_analysis/package_dependency.py,sha256=jd8eNuXVQrJ2oueIQL1baa7egg0l1zIT6Xt-qDAIT-g,1799
|
|
17
|
+
Algorithms/static_analysis/rule_matcher.py,sha256=5ynJZ-HyPVrREQb3yn_r79R1kQgiwW3ZdV6iVGfoavI,2195
|
|
18
|
+
Algorithms/tensors/broadcast.py,sha256=2P5vBEKDVBWkZA2drihMI7HS9EJqAtAyfmOsBfRqoYw,2675
|
|
19
|
+
Algorithms/tensors/kmeans.py,sha256=xPhjVhSW8v89KKazVkq18cML58bdJ7iSADQUQqV6kVA,1516
|
|
20
|
+
LLMs/xai.py,sha256=QkuXKtKNsEAvuZy1eUMvIRHifz_lIWTu7O9L3yrHUyQ,2522
|
|
21
|
+
LLMs/torch_examples/batch_agg.py,sha256=KHGZDfBQt6M5JJvrkGIfTeK1pz8jc-nUpo5u-qSJTl0,3384
|
|
22
|
+
LLMs/torch_examples/pooling.py,sha256=F6a89eRsmZ_iMjwUGtx-LyNkXO1yLF_k-g6M0rC9cNc,1130
|
|
23
|
+
LLMs/torch_examples/shape_literacy.py,sha256=KuQ0VJEJN-gztzZ3ZYJY2n2vIlGDMfss43RUpI3dPGY,686
|
|
24
|
+
mixins/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
25
|
+
mixins/benchmark.py,sha256=dTUcJPvtJGtsJ1YAyyuidA-HEl8xx4MRNU9Tc-ScFX4,338
|
|
26
|
+
mixins/logging.py,sha256=TWUb-gSCQsZyZDKMvl-wfL_yKQG13UdPd7ERjyrBOd8,130
|
|
27
|
+
python_examples-0.2.0.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
28
|
+
python_examples-0.2.0.dist-info/METADATA,sha256=iWSHKtN13_Kz8XsvVRSpxZieZBedrJr716xcCQAxO3g,2193
|
|
29
|
+
python_examples-0.2.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
30
|
+
python_examples-0.2.0.dist-info/top_level.txt,sha256=1qDk8V1ZB6dlQn_r62lGHhMUkVuKsyPqH1vJJor6Wrc,23
|
|
31
|
+
python_examples-0.2.0.dist-info/RECORD,,
|