Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit 3d4bbce

Browse files
authored
Core: uncomment out debugging code (#652)
This uncomments the debug logging. Rather than leaving the debug logging commented out, enable it always. Make the logging be conditional, allowing the inliner to remove the call entirely. This should have no visible impact.
1 parent 229b5f2 commit 3d4bbce

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

Sources/TensorFlow/Core/PythonConversion.swift

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import Python
2020
// a Python import error until it is first used.
2121
private let np = Python.import("numpy")
2222

23-
// private func debugLogNumpyError(_ message: String) {
24-
// debugLog("NumPy conversion error: " + message)
25-
// }
23+
private func debugLogNumpyError(_ message: String) {
24+
#if ENABLE_NUMPY_LOGGING
25+
debugLog("NumPy conversion error: " + message)
26+
#endif
27+
}
2628

2729
extension ShapedArray: ConvertibleFromNumpyArray
2830
where Scalar: NumpyScalarCompatible {
@@ -35,25 +37,25 @@ extension ShapedArray: ConvertibleFromNumpyArray
3537
public init?(numpy numpyArray: PythonObject) {
3638
// Check if input is a `numpy.ndarray` instance.
3739
guard Python.isinstance(numpyArray, np.ndarray) == true else {
38-
// debugLogNumpyError("""
39-
// PythonObject input has type '\(Python.type(numpyArray))' and is not \
40-
// an instance of 'numpy.ndarray'.
41-
// """)
40+
debugLogNumpyError("""
41+
PythonObject input has type '\(Python.type(numpyArray))' and is not \
42+
an instance of 'numpy.ndarray'.
43+
""")
4244
return nil
4345
}
4446
// Check if the dtype of the `ndarray` is compatible with the `Scalar`
4547
// type.
4648
guard Scalar.numpyScalarTypes.contains(numpyArray.dtype) else {
47-
// debugLogNumpyError("""
48-
// 'numpy.ndarray' dtype '\(numpyArray.dtype)' is incompatible with \
49-
// Swift type '\(Scalar.self)'.
50-
// """)
49+
debugLogNumpyError("""
50+
'numpy.ndarray' dtype '\(numpyArray.dtype)' is incompatible with \
51+
Swift type '\(Scalar.self)'.
52+
""")
5153
return nil
5254
}
5355

5456
let pyShape = numpyArray.__array_interface__["shape"]
5557
guard let shape = [Int](pyShape) else {
56-
// debugLogNumpyError("cannot access shape of 'numpy.ndarray' instance.")
58+
debugLogNumpyError("cannot access shape of 'numpy.ndarray' instance.")
5759
return nil
5860
}
5961

@@ -63,7 +65,7 @@ extension ShapedArray: ConvertibleFromNumpyArray
6365

6466
guard let ptrVal =
6567
UInt(contiguousNumpyArray.__array_interface__["data"].tuple2.0) else {
66-
// debugLogNumpyError("cannot access data of 'numpy.ndarray' instance.")
68+
debugLogNumpyError("cannot access data of 'numpy.ndarray' instance.")
6769
return nil
6870
}
6971
// Note: `ptr` is not nil even if the `ndarray` is empty (i.e. has a shape
@@ -98,25 +100,25 @@ extension Tensor: ConvertibleFromNumpyArray where Scalar: NumpyScalarCompatible
98100
public init?(numpy numpyArray: PythonObject) {
99101
// Check if input is a `numpy.ndarray` instance.
100102
guard Python.isinstance(numpyArray, np.ndarray) == true else {
101-
// debugLogNumpyError("""
102-
// PythonObject input has type '\(Python.type(numpyArray))' and is not \
103-
// an instance of 'numpy.ndarray'.
104-
// """)
103+
debugLogNumpyError("""
104+
PythonObject input has type '\(Python.type(numpyArray))' and is not \
105+
an instance of 'numpy.ndarray'.
106+
""")
105107
return nil
106108
}
107109
// Check if the dtype of the `ndarray` is compatible with the `Scalar`
108110
// type.
109111
guard Scalar.numpyScalarTypes.contains(numpyArray.dtype) else {
110-
// debugLogNumpyError("""
111-
// 'numpy.ndarray' dtype '\(numpyArray.dtype)' is incompatible with \
112-
// Swift type '\(Scalar.self)'.
113-
// """)
112+
debugLogNumpyError("""
113+
'numpy.ndarray' dtype '\(numpyArray.dtype)' is incompatible with \
114+
Swift type '\(Scalar.self)'.
115+
""")
114116
return nil
115117
}
116118

117119
let pyShape = numpyArray.__array_interface__["shape"]
118120
guard let dimensions = [Int](pyShape) else {
119-
// debugLogNumpyError("cannot access shape of 'numpy.ndarray' instance.")
121+
debugLogNumpyError("cannot access shape of 'numpy.ndarray' instance.")
120122
return nil
121123
}
122124
let shape = TensorShape(dimensions)
@@ -126,7 +128,7 @@ extension Tensor: ConvertibleFromNumpyArray where Scalar: NumpyScalarCompatible
126128
let contiguousNumpyArray = np.ascontiguousarray(numpyArray)
127129

128130
guard let ptrVal = UInt(contiguousNumpyArray.__array_interface__["data"].tuple2.0) else {
129-
// debugLogNumpyError("cannot access data of 'numpy.ndarray' instance.")
131+
debugLogNumpyError("cannot access data of 'numpy.ndarray' instance.")
130132
return nil
131133
}
132134
// Note: `ptr` is not nil even if the `ndarray` is empty (i.e. has a shape

0 commit comments

Comments
 (0)