Exemplo n.º 1
0
def create_nncf_model_and_single_algo_builder(model: Module, config: NNCFConfig,
                                              dummy_forward_fn: Callable[[Module], Any] = None,
                                              wrap_inputs_fn: Callable[[Tuple, Dict], Tuple[Tuple, Dict]] = None) \
        -> Tuple[NNCFNetwork, PTCompressionAlgorithmController]:
    assert isinstance(config, NNCFConfig)
    NNCFConfig.validate(config)
    input_info_list = create_input_infos(config)
    scopes_without_shape_matching = config.get('scopes_without_shape_matching',
                                               [])
    ignored_scopes = config.get('ignored_scopes')
    target_scopes = config.get('target_scopes')

    compressed_model = NNCFNetwork(
        model,
        input_infos=input_info_list,
        dummy_forward_fn=dummy_forward_fn,
        wrap_inputs_fn=wrap_inputs_fn,
        ignored_scopes=ignored_scopes,
        target_scopes=target_scopes,
        scopes_without_shape_matching=scopes_without_shape_matching)

    algo_names = extract_algorithm_names(config)
    assert len(algo_names) == 1
    algo_name = next(iter(algo_names))
    builder_cls = PT_COMPRESSION_ALGORITHMS.get(algo_name)
    builder = builder_cls(config, should_init=True)
    return compressed_model, builder
Exemplo n.º 2
0
    def __init__(self, config: NNCFConfig, should_init: bool = True):

        super().__init__(config, should_init)

        algo_names = extract_algorithm_names(config)
        if len(algo_names) < 2:
            raise RuntimeError('Composite algorithm builder must be supplied with a config with more than one '
                               'compression algo specified!')
        for algo_name in algo_names:
            algo_builder = PT_COMPRESSION_ALGORITHMS.get(algo_name)
            self._child_builders.append(algo_builder(config, should_init=should_init))
Exemplo n.º 3
0
    def __init__(self, config: NNCFConfig, should_init: bool = True):
        super().__init__(config, should_init)

        algo_names = extract_algorithm_names(config)
        if len(algo_names) < 2:
            raise RuntimeError(
                "Composite algorithm builder must be supplied with a config with more than one "
                "compression algo specified!")
        for algo_name in algo_names:
            algo_builder_cls = get_compression_algorithm_builder(algo_name)
            self._child_builders.append(
                algo_builder_cls(config, should_init=should_init))
Exemplo n.º 4
0
def create_compression_algorithm_builder(
        config: NNCFConfig,
        should_init: bool = True) -> PTCompressionAlgorithmBuilder:
    algo_names = extract_algorithm_names(config)
    if not algo_names:
        algo_builder_classes = [NoCompressionAlgorithmBuilder]
    else:
        algo_builder_classes = [
            PT_COMPRESSION_ALGORITHMS.get(algo_name)
            for algo_name in algo_names
        ]

    if len(algo_builder_classes) == 1:
        builder = next(iter(algo_builder_classes))(config,
                                                   should_init=should_init)
    else:
        builder = PTCompositeCompressionAlgorithmBuilder(
            config, should_init=should_init)

    return builder
Exemplo n.º 5
0
def create_compression_algorithm_builder(
        config: NNCFConfig,
        should_init: bool) -> TFCompressionAlgorithmBuilder:
    """
    Factory to create an instance of the compression algorithm builder
    by NNCFConfig.

    :param config: An instance of NNCFConfig that defines compression methods.
    :param should_init: The flag indicates that the generated compression builder
        will initialize (True) or not (False) the training parameters of the model
        during model building.
    :return: An instance of the `CompressionAlgorithmBuilder`
    """
    algo_names = extract_algorithm_names(config)
    number_compression_algorithms = len(algo_names)
    if number_compression_algorithms == 0:
        return NoCompressionAlgorithmBuilder(config, should_init)
    if number_compression_algorithms == 1:
        algo_name = next(iter(algo_names))
        return get_compression_algorithm_builder(algo_name)(config,
                                                            should_init)

    return TFCompositeCompressionAlgorithmBuilder(config, should_init)