mlxops-utils 0.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.
@@ -0,0 +1 @@
1
+ .DS_Store
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 youyinnn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,51 @@
1
+ Metadata-Version: 2.4
2
+ Name: mlxops-utils
3
+ Version: 0.1.0
4
+ Summary: Utilities for MLX operations
5
+ Project-URL: Homepage, https://github.com/youyinnn/mlxops-utils
6
+ Author-email: youyinnn <youyinnn@gmail.com>
7
+ License: MIT License
8
+
9
+ Copyright (c) 2026 youyinnn
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ License-File: LICENSE
29
+ Requires-Python: >=3.11.4
30
+ Requires-Dist: torch
31
+ Provides-Extra: dev
32
+ Requires-Dist: pytest; extra == 'dev'
33
+ Description-Content-Type: text/markdown
34
+
35
+ # mlxops-utils
36
+
37
+ Utilities for MLX operations.
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ pip install mlxops-utils
43
+ ```
44
+
45
+ ## Requirements
46
+
47
+ - Python >= 3.11.4
48
+
49
+ ## License
50
+
51
+ MIT
@@ -0,0 +1,17 @@
1
+ # mlxops-utils
2
+
3
+ Utilities for MLX operations.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install mlxops-utils
9
+ ```
10
+
11
+ ## Requirements
12
+
13
+ - Python >= 3.11.4
14
+
15
+ ## License
16
+
17
+ MIT
@@ -0,0 +1,26 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "mlxops-utils"
7
+ version = "0.1.0"
8
+ description = "Utilities for MLX operations"
9
+ readme = "README.md"
10
+ license = { file = "LICENSE" }
11
+ authors = [
12
+ { name = "youyinnn", email = "youyinnn@gmail.com" },
13
+ ]
14
+ requires-python = ">=3.11.4"
15
+ dependencies = [
16
+ "torch",
17
+ ]
18
+
19
+ [project.optional-dependencies]
20
+ dev = ["pytest"]
21
+
22
+ [project.urls]
23
+ Homepage = "https://github.com/youyinnn/mlxops-utils"
24
+
25
+ [tool.hatch.build.targets.wheel]
26
+ packages = ["src/mlxops_utils"]
@@ -0,0 +1,3 @@
1
+ from .data_utils import min_max_normalize, normalize, denormalize, compute_mean_std, IMAGENET_MEAN, IMAGENET_STD
2
+
3
+ __all__ = ["min_max_normalize", "normalize", "denormalize", "compute_mean_std", "IMAGENET_MEAN", "IMAGENET_STD"]
@@ -0,0 +1,68 @@
1
+ import torch
2
+
3
+
4
+ def min_max_normalize(u: torch.Tensor) -> torch.Tensor:
5
+ u = u.clone()
6
+ umin = u.min(dim=-1, keepdim=True).values.min(dim=-2, keepdim=True).values
7
+ u = u - umin
8
+ umax = u.max(dim=-1, keepdim=True).values.max(dim=-2, keepdim=True).values
9
+ u = u / umax
10
+ if torch.isnan(u).all():
11
+ u = torch.ones_like(u)
12
+ return torch.nan_to_num(u)
13
+
14
+
15
+ IMAGENET_MEAN = [0.485, 0.456, 0.406]
16
+ IMAGENET_STD = [0.229, 0.224, 0.225]
17
+
18
+
19
+ def _build_mean_std(tensors: torch.Tensor, mean: list[float], std: list[float]):
20
+ c = tensors.shape[1]
21
+ if len(mean) != c or len(std) != c:
22
+ raise ValueError(f"Expected mean and std of length {c}, got {len(mean)} and {len(std)}")
23
+ mean_t = torch.tensor(mean, dtype=tensors.dtype, device=tensors.device).view(1, c, 1, 1)
24
+ std_t = torch.tensor(std, dtype=tensors.dtype, device=tensors.device).view(1, c, 1, 1)
25
+ return mean_t, std_t
26
+
27
+
28
+ def compute_mean_std(dataloader) -> tuple[list[float], list[float]]:
29
+ """Compute per-channel mean and std over a dataset.
30
+
31
+ Args:
32
+ dataloader: yields tensors of shape (B, C, H, W), or (tensor, ...) tuples.
33
+
34
+ Returns:
35
+ (mean, std): each a list of floats with length C, ready for use with normalize().
36
+ """
37
+ channel_sum = None
38
+ channel_sq_sum = None
39
+ pixel_count = 0
40
+
41
+ for batch in dataloader:
42
+ if isinstance(batch, (list, tuple)):
43
+ batch = batch[0]
44
+ b, c, h, w = batch.shape
45
+ if channel_sum is None:
46
+ channel_sum = torch.zeros(c, dtype=torch.float64)
47
+ channel_sq_sum = torch.zeros(c, dtype=torch.float64)
48
+ batch = batch.to(torch.float64)
49
+ channel_sum += batch.sum(dim=[0, 2, 3])
50
+ channel_sq_sum += (batch ** 2).sum(dim=[0, 2, 3])
51
+ pixel_count += b * h * w
52
+
53
+ if pixel_count == 0:
54
+ raise ValueError("dataloader is empty")
55
+
56
+ mean = channel_sum / pixel_count
57
+ std = (channel_sq_sum / pixel_count - mean ** 2).sqrt()
58
+ return mean.tolist(), std.tolist()
59
+
60
+
61
+ def normalize(tensors: torch.Tensor, mean: list[float] = IMAGENET_MEAN, std: list[float] = IMAGENET_STD) -> torch.Tensor:
62
+ mean_t, std_t = _build_mean_std(tensors, mean, std)
63
+ return (tensors - mean_t) / std_t
64
+
65
+
66
+ def denormalize(tensors: torch.Tensor, mean: list[float] = IMAGENET_MEAN, std: list[float] = IMAGENET_STD) -> torch.Tensor:
67
+ mean_t, std_t = _build_mean_std(tensors, mean, std)
68
+ return tensors * std_t + mean_t
@@ -0,0 +1,143 @@
1
+ import pytest
2
+ import torch
3
+ from mlxops_utils.data_utils import min_max_normalize, normalize, denormalize, compute_mean_std
4
+
5
+
6
+ class TestMinMaxNormMatrix:
7
+ def test_basic_normalization(self):
8
+ u = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
9
+ result = min_max_normalize(u)
10
+ assert result.min().item() == pytest.approx(0.0)
11
+ assert result.max().item() == pytest.approx(1.0)
12
+
13
+ def test_output_range(self):
14
+ u = torch.rand(5, 5) * 100
15
+ result = min_max_normalize(u)
16
+ assert result.min().item() >= 0.0
17
+ assert result.max().item() <= 1.0
18
+
19
+ def test_does_not_modify_input(self):
20
+ u = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
21
+ original = u.clone()
22
+ min_max_normalize(u)
23
+ assert torch.equal(u, original)
24
+
25
+ def test_all_same_values_returns_ones(self):
26
+ # all same -> nan -> torch.isnan(u).all() -> ones_like
27
+ u = torch.full((3, 3), 5.0)
28
+ result = min_max_normalize(u)
29
+ assert torch.equal(result, torch.ones(3, 3))
30
+
31
+ def test_4d_batch_tensor(self):
32
+ # (B, C, H, W): each H×W map normalized independently
33
+ u = torch.rand(2, 3, 4, 4) * 100
34
+ result = min_max_normalize(u)
35
+ assert result.min().item() >= 0.0
36
+ assert result.max().item() <= 1.0
37
+ # each (b, c) slice should be in [0, 1]
38
+ for b in range(2):
39
+ for c in range(3):
40
+ s = result[b, c]
41
+ assert s.min().item() == pytest.approx(0.0)
42
+ assert s.max().item() == pytest.approx(1.0)
43
+
44
+ def test_negative_values(self):
45
+ u = torch.tensor([[-3.0, -1.0], [0.0, 2.0]])
46
+ result = min_max_normalize(u)
47
+ assert result.min().item() == pytest.approx(0.0)
48
+ assert result.max().item() == pytest.approx(1.0)
49
+
50
+ def test_returns_tensor(self):
51
+ u = torch.rand(3, 3)
52
+ result = min_max_normalize(u)
53
+ assert isinstance(result, torch.Tensor)
54
+
55
+
56
+ class TestNormalizeDenormalize:
57
+ def test_norm_output_shape(self):
58
+ u = torch.rand(2, 3, 64, 64)
59
+ assert normalize(u).shape == u.shape
60
+
61
+ def test_denorm_output_shape(self):
62
+ u = torch.rand(2, 3, 64, 64)
63
+ assert denormalize(u).shape == u.shape
64
+
65
+ def test_norm_denorm_roundtrip(self):
66
+ u = torch.rand(2, 3, 64, 64)
67
+ result = denormalize(normalize(u))
68
+ assert torch.allclose(result, u, atol=1e-6)
69
+
70
+ def test_norm_applies_imagenet_stats(self):
71
+ # single pixel all-ones image: norm should give (1 - mean) / std per channel
72
+ u = torch.ones(1, 3, 1, 1)
73
+ result = normalize(u)
74
+ expected_r = (1.0 - 0.485) / 0.229
75
+ assert result[0, 0, 0, 0].item() == pytest.approx(expected_r, rel=1e-5)
76
+
77
+ def test_invalid_mean_std_length_raises(self):
78
+ u = torch.rand(2, 3, 64, 64)
79
+ with pytest.raises(ValueError):
80
+ normalize(u, mean=[0.5, 0.5], std=[0.5, 0.5, 0.5])
81
+
82
+ def test_custom_mean_std(self):
83
+ mean = [0.5, 0.5, 0.5]
84
+ std = [0.5, 0.5, 0.5]
85
+ u = torch.ones(1, 3, 4, 4)
86
+ result = normalize(u, mean=mean, std=std)
87
+ assert result[0, 0, 0, 0].item() == pytest.approx((1.0 - 0.5) / 0.5)
88
+
89
+ def test_custom_mean_std_roundtrip(self):
90
+ mean = [0.3, 0.4, 0.5]
91
+ std = [0.1, 0.2, 0.3]
92
+ u = torch.rand(2, 3, 32, 32)
93
+ assert torch.allclose(denormalize(normalize(u, mean, std), mean, std), u, atol=1e-6)
94
+
95
+ def test_single_channel(self):
96
+ u = torch.rand(2, 1, 64, 64)
97
+ mean = [0.5]
98
+ std = [0.25]
99
+ result = normalize(u, mean=mean, std=std)
100
+ assert result.shape == u.shape
101
+
102
+ def test_does_not_modify_input(self):
103
+ u = torch.rand(2, 3, 32, 32)
104
+ original = u.clone()
105
+ normalize(u)
106
+ assert torch.equal(u, original)
107
+
108
+
109
+ class TestComputeMeanStd:
110
+ def _make_loader(self, batches):
111
+ return batches
112
+
113
+ def test_known_values(self):
114
+ # constant tensor: mean = value, std = 0
115
+ batch = torch.full((4, 3, 8, 8), 0.5)
116
+ mean, std = compute_mean_std([batch])
117
+ assert mean == pytest.approx([0.5, 0.5, 0.5], abs=1e-6)
118
+ assert std == pytest.approx([0.0, 0.0, 0.0], abs=1e-6)
119
+
120
+ def test_output_length_matches_channels(self):
121
+ batch = torch.rand(2, 4, 16, 16)
122
+ mean, std = compute_mean_std([batch])
123
+ assert len(mean) == 4
124
+ assert len(std) == 4
125
+
126
+ def test_tuple_batch(self):
127
+ # dataloader that yields (image, label) tuples
128
+ batch = (torch.rand(2, 3, 8, 8), torch.zeros(2))
129
+ mean, std = compute_mean_std([batch])
130
+ assert len(mean) == 3
131
+ assert len(std) == 3
132
+
133
+ def test_multiple_batches(self):
134
+ # two batches with same constant: result should match single batch
135
+ b1 = torch.full((2, 3, 4, 4), 0.3)
136
+ b2 = torch.full((2, 3, 4, 4), 0.3)
137
+ mean, std = compute_mean_std([b1, b2])
138
+ assert mean == pytest.approx([0.3, 0.3, 0.3], abs=1e-6)
139
+ assert std == pytest.approx([0.0, 0.0, 0.0], abs=1e-6)
140
+
141
+ def test_empty_dataloader_raises(self):
142
+ with pytest.raises(ValueError, match="empty"):
143
+ compute_mean_std([])