Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
296 changes: 113 additions & 183 deletions src/nncf/common/hardware/config.py

Large diffs are not rendered by default.

617 changes: 0 additions & 617 deletions src/nncf/common/hardware/configs/cpu.json

This file was deleted.

627 changes: 0 additions & 627 deletions src/nncf/common/hardware/configs/gpu.json

This file was deleted.

740 changes: 0 additions & 740 deletions src/nncf/common/hardware/configs/npu.json

This file was deleted.

78 changes: 0 additions & 78 deletions src/nncf/common/hardware/configs/template.md

This file was deleted.

86 changes: 86 additions & 0 deletions src/nncf/common/hardware/defines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright (c) 2025 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from dataclasses import dataclass
from dataclasses import field
from typing import Any

from nncf.common.quantization.structs import QuantizationScheme
from nncf.common.quantization.structs import QuantizerConfig
from nncf.parameters import StrEnum

SCALES = "scales"
UNIFIED = "unified"
ADJUST_PADDING = "adjust_padding"


class Granularity(StrEnum):
PER_CHANNEL = "per_channel"
PER_TENSOR = "per_tensor"


@dataclass(frozen=True, kw_only=True, slots=True)
class QConfigSpace:
"""
A class to represent the configuration space for quantization.

:param bits: Number of bits for quantization.
:param mode: Available quantization schemes.
:param granularity: Granularity options for quantization.
:param narrow_range: Indicates narrow range quantization.
:param signedness_to_force: Optional signedness enforcement.
"""

bits: int
mode: tuple[QuantizationScheme, ...]
granularity: tuple[Granularity, ...]
narrow_range: tuple[bool, ...]
signedness_to_force: bool | None = None

def get_all_qconfigs(self) -> list[QuantizerConfig]:
"""
Generate a list of all possible QuantizerConfig instances based on the current
settings of mode, granularity, narrow_range, and other parameters.

:return: A list of QuantizerConfig objects, each representing
a unique combination of the quantization parameters.
"""
ret = []
for mode in self.mode:
for granularity in self.granularity:
for narrow_range in self.narrow_range:
ret.append(
QuantizerConfig(
num_bits=self.bits,
mode=mode,
per_channel=granularity == Granularity.PER_CHANNEL,
narrow_range=narrow_range,
signedness_to_force=self.signedness_to_force,
)
)
return ret


@dataclass(frozen=True, kw_only=True, slots=True)
class OpDesc:
"""
Represents the description of an operation.

:param type: The type of the operation.
:param activations: A tuple containing the quantization configuration for the activations of the operation.
:param weights: A tuple containing the quantization configuration for the weights of the operation.
:param attributes: A dictionary of additional attributes.
"""

type: str
activations: tuple[QConfigSpace, ...] = field(default_factory=tuple)
weights: tuple[QConfigSpace, ...] = field(default_factory=tuple)
attributes: dict[str, Any] = field(default_factory=dict)
4 changes: 3 additions & 1 deletion src/nncf/common/hardware/opset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from nncf.parameters import StrEnum

class HWConfigOpName:

class HWConfigOpName(StrEnum):
CONVOLUTION = "Convolution"
DEPTHWISECONVOLUTION = "DepthWiseConvolution"
MATMUL = "MatMul"
Expand Down
135 changes: 135 additions & 0 deletions src/nncf/common/hardware/qspaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Copyright (c) 2025 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from nncf.common.hardware.defines import Granularity
from nncf.common.hardware.defines import QConfigSpace
from nncf.common.quantization.structs import QuantizationScheme

q8_a_sym = QConfigSpace(
bits=8,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_TENSOR,),
narrow_range=(False,),
)

q8_a = QConfigSpace(
bits=8,
mode=(QuantizationScheme.SYMMETRIC, QuantizationScheme.ASYMMETRIC),
granularity=(Granularity.PER_TENSOR,),
narrow_range=(False,),
)

q8_a_ch = QConfigSpace(
bits=8,
mode=(QuantizationScheme.SYMMETRIC, QuantizationScheme.ASYMMETRIC),
granularity=(Granularity.PER_CHANNEL, Granularity.PER_TENSOR),
narrow_range=(False,),
)

q8_w_sym = QConfigSpace(
bits=8,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_CHANNEL, Granularity.PER_TENSOR),
narrow_range=(True,),
signedness_to_force=True,
)

q8_w_sym_any_nr = QConfigSpace(
bits=8,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_CHANNEL, Granularity.PER_TENSOR),
narrow_range=(True, False),
signedness_to_force=True,
)

q8_w_asym = QConfigSpace(
bits=8,
mode=(QuantizationScheme.ASYMMETRIC,),
granularity=(Granularity.PER_CHANNEL, Granularity.PER_TENSOR),
narrow_range=(False,),
)

q16_a_sym = QConfigSpace(
bits=16,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_TENSOR,),
narrow_range=(False,),
)

q16_a = QConfigSpace(
bits=16,
mode=(QuantizationScheme.SYMMETRIC, QuantizationScheme.ASYMMETRIC),
granularity=(Granularity.PER_TENSOR,),
narrow_range=(False,),
)

q16_a_ch = QConfigSpace(
bits=16,
mode=(QuantizationScheme.SYMMETRIC, QuantizationScheme.ASYMMETRIC),
granularity=(Granularity.PER_CHANNEL, Granularity.PER_TENSOR),
narrow_range=(False,),
)

q16_w_sym = QConfigSpace(
bits=16,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_CHANNEL, Granularity.PER_TENSOR),
narrow_range=(True,),
signedness_to_force=True,
)

q16_w_sym_any_nr = QConfigSpace(
bits=16,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_CHANNEL, Granularity.PER_TENSOR),
narrow_range=(True, False),
signedness_to_force=True,
)

q16_w_asym = QConfigSpace(
bits=16,
mode=(QuantizationScheme.ASYMMETRIC,),
granularity=(Granularity.PER_CHANNEL, Granularity.PER_TENSOR),
narrow_range=(False,),
)

q4_tn = QConfigSpace(
bits=4,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_TENSOR,),
narrow_range=(False,),
)
q4_ch = QConfigSpace(
bits=4,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_CHANNEL,),
narrow_range=(False,),
)
q4_w = QConfigSpace(
bits=4,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_CHANNEL, Granularity.PER_TENSOR),
narrow_range=(False,),
)
q2_ch = QConfigSpace(
bits=2,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_CHANNEL,),
narrow_range=(False,),
)

q2_w = QConfigSpace(
bits=2,
mode=(QuantizationScheme.SYMMETRIC,),
granularity=(Granularity.PER_CHANNEL, Granularity.PER_TENSOR),
narrow_range=(False,),
)
10 changes: 10 additions & 0 deletions src/nncf/common/hardware/setups/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2025 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Loading