Skip to content

Commit 140fc2c

Browse files
print verbose debug info in Hex format
1 parent bf51fdc commit 140fc2c

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

test/NeuralEngineFunctionalModel.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
import torch
44
import torch.nn.functional as F
5+
import numpy as np
56

67
from TestClasses import IntegerType, Padding, Stride
78

89

910
class NeuralEngineFunctionalModel:
1011
ACCUMULATOR_TYPE = IntegerType(name="int32")
1112

13+
@staticmethod
14+
def _tensor_to_hex(tensor):
15+
int_tensor = np.asarray(torch.floor(tensor).to(torch.int64))
16+
int_tensor[int_tensor < 0] = 0xffffffff + (int_tensor[int_tensor < 0]+1)
17+
hex_tensor = np.empty(int_tensor.shape, dtype=object)
18+
for idx in np.ndindex(int_tensor.shape):
19+
hex_tensor[idx] = hex(int_tensor[idx].item())
20+
return hex_tensor
21+
1222
@staticmethod
1323
def _cast(
1424
tensor: torch.Tensor, _type: IntegerType, saturate: bool = False
@@ -36,7 +46,10 @@ def _norm_quant(
3646

3747
if verbose:
3848
print("INTERMEDIATE RESULTS (after scale):")
39-
print(tensor)
49+
current_threshold = np.get_printoptions()['threshold']
50+
np.set_printoptions(threshold=np.inf)
51+
print(NeuralEngineFunctionalModel._tensor_to_hex(tensor))
52+
np.set_printoptions(threshold=current_threshold)
4053

4154
if has_bias:
4255
assert bias is not None
@@ -54,7 +67,10 @@ def _norm_quant(
5467

5568
if verbose:
5669
print("INTERMEDIATE RESULTS (after bias):")
57-
print(tensor)
70+
current_threshold = np.get_printoptions()['threshold']
71+
np.set_printoptions(threshold=np.inf)
72+
print(NeuralEngineFunctionalModel._tensor_to_hex(tensor))
73+
np.set_printoptions(threshold=current_threshold)
5874

5975
if has_relu:
6076
tensor = F.relu(tensor)
@@ -63,7 +79,10 @@ def _norm_quant(
6379

6480
if verbose:
6581
print("INTERMEDIATE RESULTS (after shift):")
66-
print(tensor)
82+
current_threshold = np.get_printoptions()['threshold']
83+
np.set_printoptions(threshold=np.inf)
84+
print(NeuralEngineFunctionalModel._tensor_to_hex(tensor))
85+
np.set_printoptions(threshold=current_threshold)
6786

6887
# Saturate into out_type
6988
tensor = NeuralEngineFunctionalModel._cast(tensor, out_type, saturate=True)
@@ -102,6 +121,15 @@ def convolution(
102121
0,
103122
)
104123

124+
if verbose:
125+
print("INPUTS (padded):")
126+
current_threshold = np.get_printoptions()['threshold']
127+
np.set_printoptions(threshold=np.inf)
128+
print(NeuralEngineFunctionalModel._tensor_to_hex(input_padded))
129+
print("WEIGHTS (padded):")
130+
print(NeuralEngineFunctionalModel._tensor_to_hex(weight))
131+
np.set_printoptions(threshold=current_threshold)
132+
105133
# Accumulators are 32bit non-saturating.
106134
# Calculate in higher precision (int64)
107135
output = F.conv2d(
@@ -118,7 +146,10 @@ def convolution(
118146

119147
if verbose:
120148
print("INTERMEDIATE RESULTS (pre-normalization/requant):")
121-
print(output)
149+
current_threshold = np.get_printoptions()['threshold']
150+
np.set_printoptions(threshold=np.inf)
151+
print(NeuralEngineFunctionalModel._tensor_to_hex(output))
152+
np.set_printoptions(threshold=current_threshold)
122153

123154
if has_norm_quant:
124155
assert scale is not None

0 commit comments

Comments
 (0)