MCPcopy Create free account
hub / github.com/Meshcapade/difflocks / repr

Function repr

utils/general_util.py:194–264  ·  view source on GitHub ↗
(model)

Source from the content-addressed store, hash-verified

192
193def summary(model,file=sys.stderr):
194 def repr(model):
195 # We treat the extra repr like the sub-module, one item per line
196 extra_lines = []
197 extra_repr = model.extra_repr()
198 # empty string will be split into list ['']
199 if extra_repr:
200 extra_lines = extra_repr.split('\n')
201 child_lines = []
202 total_params = 0
203 for key, module in model._modules.items():
204 mod_str, num_params = repr(module)
205 mod_str = _addindent(mod_str, 2)
206 child_lines.append('(' + key + '): ' + mod_str)
207 total_params += num_params
208 lines = extra_lines + child_lines
209
210 for name, p in model._parameters.items():
211 # print("name is ", name)
212 if p is not None:
213 # print("parameter with shape ", p.shape)
214 # print("parameter has dim ", p.dim())
215 if p.dim()==0: #is just a scalar parameter
216 total_params+=1
217 else:
218 total_params += reduce(lambda x, y: x * y, p.shape)
219 # if(p.grad==None):
220 # print("p has no grad", name)
221 # else:
222 # print("p has gradnorm ", name ,p.grad.norm() )
223
224 main_str = model._get_name() + '('
225 if lines:
226 # simple one-liner info, which most builtin Modules will use
227 if len(extra_lines) == 1 and not child_lines:
228 main_str += extra_lines[0]
229 else:
230 main_str += '\n ' + '\n '.join(lines) + '\n'
231
232 main_str += ')'
233 if file is sys.stderr:
234 main_str += ', \033[92m{:,}\033[0m params'.format(total_params)
235 for name, p in model._parameters.items():
236 if hasattr(p, 'grad'):
237 if(p.grad==None):
238 print("p has no grad", name)
239 main_str+="p no grad"
240 else:
241 # print("p has gradnorm ", name ,p.grad.norm() )
242 main_str+= "\n" + name + " p has grad norm, min, max" + str(p.grad.norm()) + " " + str(p.grad.min()) + " " + str(p.grad.max())
243 main_str+= "\n" + name + " p has grad type" + str(p.grad.type())
244
245 #check for nans
246 if torch.isnan(p.grad).any():
247 print("NAN detected in grad of ", name)
248 print("main_str is ", main_str)
249 exit(1)
250
251 #show also the parameter itself, an not only the gradient

Callers 1

summaryFunction · 0.85

Calls 4

formatMethod · 0.80
extra_reprMethod · 0.45
minMethod · 0.45
maxMethod · 0.45

Tested by

no test coverage detected