added missing inputs and outputs to run_test hook
Browse files- run_test.py +16 -9
run_test.py
CHANGED
|
@@ -65,32 +65,39 @@ SafeTensors = WritingSafeTensors(
|
|
| 65 |
)
|
| 66 |
|
| 67 |
IDX = 0 # IDX is unused
|
| 68 |
-
|
| 69 |
tensors = {}
|
| 70 |
-
def hook(module, inputs, outputs):
|
| 71 |
global IDX
|
| 72 |
-
|
| 73 |
HAS_HF_HOOK = hasattr(module, '_hf_hook')
|
| 74 |
if HAS_HF_HOOK:
|
| 75 |
-
inputs = module._hf_hook.pre_forward(module, *inputs)
|
| 76 |
for idx, input in enumerate(inputs):
|
| 77 |
if isinstance(input, torch.Tensor):
|
| 78 |
-
SafeTensors.add(f'{
|
|
|
|
|
|
|
|
|
|
| 79 |
if STORE_WEIGHTS and not list(module.children()):
|
| 80 |
for wtname, wt in list(module.named_parameters()) + list(module.named_buffers()):
|
| 81 |
-
SafeTensors.add(f'{
|
| 82 |
if HAS_HF_HOOK:
|
| 83 |
outputs = module._hf_hook.post_forward(module, outputs)
|
| 84 |
if isinstance(outputs, torch.Tensor):
|
| 85 |
-
SafeTensors.add(f'{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
for idx, output in enumerate(outputs):
|
| 88 |
if isinstance(output, torch.Tensor):
|
| 89 |
-
SafeTensors.add(f'{
|
| 90 |
IDX += 1
|
| 91 |
|
| 92 |
for module in pipe.model.modules():
|
| 93 |
-
module.register_forward_hook(hook)
|
| 94 |
|
| 95 |
with SafeTensors:
|
| 96 |
output = pipe(prompt, do_sample=False, max_new_tokens=1, temperature=1.0, top_p=1.0)
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
IDX = 0 # IDX is unused
|
| 68 |
+
module_prefixes = {mod : name + '.' if name else '' for name, mod in pipe.model.named_modules()}
|
| 69 |
tensors = {}
|
| 70 |
+
def hook(module, inputs, kwinputs, outputs):
|
| 71 |
global IDX
|
| 72 |
+
prefix = module_prefixes[module]
|
| 73 |
HAS_HF_HOOK = hasattr(module, '_hf_hook')
|
| 74 |
if HAS_HF_HOOK:
|
| 75 |
+
inputs, kwinputs = module._hf_hook.pre_forward(module, *inputs, **kwinputs)
|
| 76 |
for idx, input in enumerate(inputs):
|
| 77 |
if isinstance(input, torch.Tensor):
|
| 78 |
+
SafeTensors.add(f'{prefix}input.{idx}', input);
|
| 79 |
+
for key, input in kwinputs.items():
|
| 80 |
+
if isinstance(input, torch.Tensor):
|
| 81 |
+
SafeTensors.add(f'{prefix}input.{key}', input);
|
| 82 |
if STORE_WEIGHTS and not list(module.children()):
|
| 83 |
for wtname, wt in list(module.named_parameters()) + list(module.named_buffers()):
|
| 84 |
+
SafeTensors.add(f'{prefix}{wtname}', wt)
|
| 85 |
if HAS_HF_HOOK:
|
| 86 |
outputs = module._hf_hook.post_forward(module, outputs)
|
| 87 |
if isinstance(outputs, torch.Tensor):
|
| 88 |
+
SafeTensors.add(f'{prefix}output', outputs);
|
| 89 |
+
elif isinstance(outputs, dict):
|
| 90 |
+
for key, output in outputs.items():
|
| 91 |
+
if isinstance(output, torch.Tensor):
|
| 92 |
+
SafeTensors.add(f'{prefix}output.{key}', output);
|
| 93 |
else:
|
| 94 |
for idx, output in enumerate(outputs):
|
| 95 |
if isinstance(output, torch.Tensor):
|
| 96 |
+
SafeTensors.add(f'{prefix}output.{idx}', output);
|
| 97 |
IDX += 1
|
| 98 |
|
| 99 |
for module in pipe.model.modules():
|
| 100 |
+
module.register_forward_hook(hook, with_kwargs=True)
|
| 101 |
|
| 102 |
with SafeTensors:
|
| 103 |
output = pipe(prompt, do_sample=False, max_new_tokens=1, temperature=1.0, top_p=1.0)
|