MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / _Formatter

Class _Formatter

python/oneflow/framework/tensor_str.py:110–199  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

108
109
110class _Formatter(object):
111 def __init__(self, tensor):
112 self.floating_dtype = tensor.dtype.is_floating_point
113 self.int_mode = True
114 self.sci_mode = False
115 self.max_width = 1
116 self.random_sample_num = 50
117 tensor = _try_convert_to_local_tensor(tensor)
118
119 with flow.no_grad():
120 tensor_view = tensor.reshape(-1)
121
122 if not self.floating_dtype:
123 for value in tensor_view:
124 value_str = "{}".format(value)
125 self.max_width = max(self.max_width, len(value_str))
126
127 else:
128 nonzero_finite_vals = flow.masked_select(tensor_view, tensor_view.ne(0))
129 if nonzero_finite_vals.numel() == 0:
130 # no valid number, do nothing
131 return
132
133 nonzero_finite_abs = nonzero_finite_vals.abs()
134 nonzero_finite_min = nonzero_finite_abs.min().numpy().astype(np.float64)
135 nonzero_finite_max = nonzero_finite_abs.max().numpy().astype(np.float64)
136
137 for value in nonzero_finite_abs.numpy():
138 if value != np.ceil(value):
139 self.int_mode = False
140 break
141
142 if self.int_mode:
143 # Check if scientific representation should be used.
144 if (
145 nonzero_finite_max / nonzero_finite_min > 1000.0
146 or nonzero_finite_max > 1.0e8
147 ):
148 self.sci_mode = True
149 for value in nonzero_finite_vals:
150 value_str = (
151 ("{{:.{}e}}").format(PRINT_OPTS.precision).format(value)
152 )
153 self.max_width = max(self.max_width, len(value_str))
154 else:
155 for value in nonzero_finite_vals:
156 value_str = ("{:.0f}").format(value)
157 self.max_width = max(self.max_width, len(value_str) + 1)
158 else:
159 if (
160 nonzero_finite_max / nonzero_finite_min > 1000.0
161 or nonzero_finite_max > 1.0e8
162 or nonzero_finite_min < 1.0e-4
163 ):
164 self.sci_mode = True
165 for value in nonzero_finite_vals:
166 value_str = (
167 ("{{:.{}e}}").format(PRINT_OPTS.precision).format(value)

Callers 1

_tensor_strFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected