MCPcopy
hub / github.com/OpenPPL/ppq / dummy_forward

Method dummy_forward

ppq/executor/torch.py:615–652  ·  view source on GitHub ↗

This function allows you to execute entire graph without feeding any data. This feature is required for operation parameter quantization. See also: ppq.quantization.optim.ParameterQuantizePass. This function fakes some input tensors via operation metadata. ATTENT

(self, hooks: Dict[str, RuntimeHook] = None)

Source from the content-addressed store, hash-verified

613 else: return self._default_quant_fn(tensor, config)
614
615 def dummy_forward(self, hooks: Dict[str, RuntimeHook] = None) -> None:
616 """This function allows you to execute entire graph without feeding any
617 data. This feature is required for operation parameter quantization.
618 See also: ppq.quantization.optim.ParameterQuantizePass.
619
620 This function fakes some input tensors via operation metadata.
621 ATTENTION: operation must have metadata before invoking this function.
622
623 Args:
624 hooks (Dict[str, RuntimeHook], optional):
625 A hook table for customizing operation behaviour and collate data during executing.
626 All hooks should inherit from class RuntimeHook, with all necessary methods implemented.
627 See also: ppq.executor.base.RuntimeHook
628
629 Executor calls hook.pre_forward_hook(operation, input_data) before dispatching operation,
630 by using this feature, you can dynamically dispatch operation during executing,
631 or processing input data as you want.(remember to return processed input data)
632
633 Executor calls hook.post_forward_hook(operation, output_data) after the execution,
634 you are supposed to gather all necessary data from execution via this feature.
635
636 For Quantable Operation, a much more powerful class:
637 ppq.executor.base.QuantOpRuntimeHook is provided.
638 see also: ppq.executor.base.QuantOpRuntimeHook
639
640 Defaults to None.
641 """
642 # build dummy input based on meta data
643 feed_dict = {}
644 for var_name, input_var in self._graph.inputs.items():
645 if len(input_var.dest_ops) == 0: continue
646
647 assert input_var.shape is not None, (
648 f'Can not generate dummy input for input variable {input_var.name}, input shape is not specified.')
649
650 feed_dict[var_name] = torch.Tensor(size=input_var.shape, device='cpu').fill_(
651 0).type(dtype=DataType.to_torch(input_var.dtype)).to(self._device)
652 self.forward(inputs=feed_dict, hooks=hooks)
653
654 def partial_graph_forward(
655 self, operations: List[Operation],

Callers 1

optimizeMethod · 0.80

Calls 4

forwardMethod · 0.95
toMethod · 0.80
typeMethod · 0.80
to_torchMethod · 0.80

Tested by

no test coverage detected