nshutils 0.22.2__tar.gz → 0.22.3__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 (23) hide show
  1. {nshutils-0.22.2 → nshutils-0.22.3}/PKG-INFO +1 -1
  2. {nshutils-0.22.2 → nshutils-0.22.3}/pyproject.toml +1 -1
  3. nshutils-0.22.3/src/nshutils/lovely/numpy_.py +109 -0
  4. nshutils-0.22.2/src/nshutils/lovely/numpy_.py +0 -72
  5. {nshutils-0.22.2 → nshutils-0.22.3}/README.md +0 -0
  6. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/__init__.py +0 -0
  7. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/__init__.pyi +0 -0
  8. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/actsave/__init__.py +0 -0
  9. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/actsave/_loader.py +0 -0
  10. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/actsave/_saver.py +0 -0
  11. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/collections.py +0 -0
  12. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/display.py +0 -0
  13. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/logging.py +0 -0
  14. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/lovely/__init__.py +0 -0
  15. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/lovely/_base.py +0 -0
  16. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/lovely/_monkey_patch_all.py +0 -0
  17. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/lovely/config.py +0 -0
  18. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/lovely/jax_.py +0 -0
  19. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/lovely/torch_.py +0 -0
  20. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/lovely/utils.py +0 -0
  21. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/snoop.py +0 -0
  22. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/typecheck.py +0 -0
  23. {nshutils-0.22.2 → nshutils-0.22.3}/src/nshutils/util.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: nshutils
3
- Version: 0.22.2
3
+ Version: 0.22.3
4
4
  Summary:
5
5
  Author: Nima Shoghi
6
6
  Author-email: nimashoghi@gmail.com
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nshutils"
3
- version = "0.22.2"
3
+ version = "0.22.3"
4
4
  description = ""
5
5
  authors = [{ name = "Nima Shoghi", email = "nimashoghi@gmail.com" }]
6
6
  requires-python = ">=3.9,<4.0"
@@ -0,0 +1,109 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+
5
+ import numpy as np
6
+
7
+ from ._base import lovely_repr, monkey_patch_contextmanager
8
+ from .utils import LovelyStats, array_stats
9
+
10
+
11
+ def _np_ge_2():
12
+ import importlib.metadata
13
+
14
+ from packaging.version import Version
15
+
16
+ try:
17
+ numpy_version = importlib.metadata.version("numpy")
18
+ return Version(numpy_version) >= Version("2.0")
19
+ except importlib.metadata.PackageNotFoundError:
20
+ return False
21
+
22
+
23
+ def _type_name(array: np.ndarray):
24
+ return (
25
+ "array"
26
+ if type(array) is np.ndarray
27
+ else type(array).__name__.rsplit(".", 1)[-1]
28
+ )
29
+
30
+
31
+ _DT_NAMES = {
32
+ "float16": "f16",
33
+ "float32": "f32",
34
+ "float64": "", # Default dtype in numpy
35
+ "uint8": "u8",
36
+ "uint16": "u16",
37
+ "uint32": "u32",
38
+ "uint64": "u64",
39
+ "int8": "i8",
40
+ "int16": "i16",
41
+ "int32": "i32",
42
+ "int64": "i64",
43
+ "complex64": "c64",
44
+ "complex128": "c128",
45
+ }
46
+
47
+
48
+ def _dtype_str(array: np.ndarray) -> str:
49
+ dtype_base = str(array.dtype).rsplit(".", 1)[-1]
50
+ dtype_base = _DT_NAMES.get(dtype_base, dtype_base)
51
+ return dtype_base
52
+
53
+
54
+ @lovely_repr(dependencies=["numpy"])
55
+ def numpy_repr(array: np.ndarray) -> LovelyStats:
56
+ return {
57
+ # Basic attributes
58
+ "shape": array.shape,
59
+ "size": array.size,
60
+ "nbytes": array.nbytes,
61
+ "type_name": _type_name(array),
62
+ # Dtype
63
+ "dtype_str": _dtype_str(array),
64
+ "is_complex": np.iscomplexobj(array),
65
+ # Depending of whether the tensor is complex or not, we will call the appropriate stats function
66
+ **array_stats(array),
67
+ }
68
+
69
+
70
+ # If numpy 2.0, use the new API override_repr.
71
+ if _np_ge_2():
72
+
73
+ @monkey_patch_contextmanager(dependencies=["numpy"])
74
+ def numpy_monkey_patch():
75
+ try:
76
+ np.set_printoptions(override_repr=numpy_repr)
77
+ logging.info(
78
+ f"Numpy monkey patching: using {numpy_repr.__name__} for numpy arrays. "
79
+ f"{np.get_printoptions()=}"
80
+ )
81
+ yield
82
+ finally:
83
+ np.set_printoptions(override_repr=None)
84
+ logging.info(
85
+ f"Numpy unmonkey patching: using {numpy_repr.__name__} for numpy arrays. "
86
+ f"{np.get_printoptions()=}"
87
+ )
88
+
89
+ else:
90
+
91
+ @monkey_patch_contextmanager(dependencies=["numpy"])
92
+ def numpy_monkey_patch():
93
+ try:
94
+ np.set_string_function(numpy_repr, True) # pyright: ignore[reportAttributeAccessIssue]
95
+ np.set_string_function(numpy_repr, False) # pyright: ignore[reportAttributeAccessIssue]
96
+
97
+ logging.info(
98
+ f"Numpy monkey patching: using {numpy_repr.__name__} for numpy arrays. "
99
+ f"{np.get_printoptions()=}"
100
+ )
101
+ yield
102
+ finally:
103
+ np.set_string_function(None, True) # pyright: ignore[reportAttributeAccessIssue]
104
+ np.set_string_function(None, False) # pyright: ignore[reportAttributeAccessIssue]
105
+
106
+ logging.info(
107
+ f"Numpy unmonkey patching: using {numpy_repr.__name__} for numpy arrays. "
108
+ f"{np.get_printoptions()=}"
109
+ )
@@ -1,72 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import logging
4
-
5
- import numpy as np
6
-
7
- from ._base import lovely_repr, monkey_patch_contextmanager
8
- from .utils import LovelyStats, array_stats
9
-
10
-
11
- def _type_name(array: np.ndarray):
12
- return (
13
- "array"
14
- if type(array) is np.ndarray
15
- else type(array).__name__.rsplit(".", 1)[-1]
16
- )
17
-
18
-
19
- _DT_NAMES = {
20
- "float16": "f16",
21
- "float32": "f32",
22
- "float64": "", # Default dtype in numpy
23
- "uint8": "u8",
24
- "uint16": "u16",
25
- "uint32": "u32",
26
- "uint64": "u64",
27
- "int8": "i8",
28
- "int16": "i16",
29
- "int32": "i32",
30
- "int64": "i64",
31
- "complex64": "c64",
32
- "complex128": "c128",
33
- }
34
-
35
-
36
- def _dtype_str(array: np.ndarray) -> str:
37
- dtype_base = str(array.dtype).rsplit(".", 1)[-1]
38
- dtype_base = _DT_NAMES.get(dtype_base, dtype_base)
39
- return dtype_base
40
-
41
-
42
- @lovely_repr(dependencies=["numpy"])
43
- def numpy_repr(array: np.ndarray) -> LovelyStats:
44
- return {
45
- # Basic attributes
46
- "shape": array.shape,
47
- "size": array.size,
48
- "nbytes": array.nbytes,
49
- "type_name": _type_name(array),
50
- # Dtype
51
- "dtype_str": _dtype_str(array),
52
- "is_complex": np.iscomplexobj(array),
53
- # Depending of whether the tensor is complex or not, we will call the appropriate stats function
54
- **array_stats(array),
55
- }
56
-
57
-
58
- @monkey_patch_contextmanager(dependencies=["numpy"])
59
- def numpy_monkey_patch():
60
- try:
61
- np.set_printoptions(override_repr=numpy_repr)
62
- logging.info(
63
- f"Numpy monkey patching: using {numpy_repr.__name__} for numpy arrays. "
64
- f"{np.get_printoptions()=}"
65
- )
66
- yield
67
- finally:
68
- np.set_printoptions(override_repr=None)
69
- logging.info(
70
- f"Numpy unmonkey patching: using {numpy_repr.__name__} for numpy arrays. "
71
- f"{np.get_printoptions()=}"
72
- )
File without changes