diff --git a/tutorials/notebooks/mct_features_notebooks/keras/example_keras_qat.ipynb b/tutorials/notebooks/mct_features_notebooks/keras/example_keras_qat.ipynb index f504fcb4b..ef8614222 100644 --- a/tutorials/notebooks/mct_features_notebooks/keras/example_keras_qat.ipynb +++ b/tutorials/notebooks/mct_features_notebooks/keras/example_keras_qat.ipynb @@ -193,9 +193,9 @@ "from model_compression_toolkit import DefaultDict\n", "from model_compression_toolkit.constants import FLOAT_BITWIDTH\n", "from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_KERNEL, BIAS_ATTR, BIAS\n", - "\n", - "tp = mct.target_platform\n", - "\n", + "from mct_quantizers import QuantizationMethod\n", + "from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import schema, TargetPlatformCapabilities, Signedness, \\\n", + " AttributeQuantizationConfig, OpQuantizationConfig, QuantizationConfigOptions\n", "\n", "def get_tpc():\n", " \"\"\"\n", @@ -211,7 +211,7 @@ "\n", " # define a default quantization config for all non-specified weights attributes.\n", " default_weight_attr_config = AttributeQuantizationConfig(\n", - " weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,\n", + " weights_quantization_method=QuantizationMethod.POWER_OF_TWO,\n", " weights_n_bits=8,\n", " weights_per_channel_threshold=False,\n", " enable_weights_quantization=False,\n", @@ -219,7 +219,7 @@ "\n", " # define a quantization config to quantize the kernel (for layers where there is a kernel attribute).\n", " kernel_base_config = AttributeQuantizationConfig(\n", - " weights_quantization_method=tp.QuantizationMethod.SYMMETRIC,\n", + " weights_quantization_method=QuantizationMethod.SYMMETRIC,\n", " weights_n_bits=2,\n", " weights_per_channel_threshold=True,\n", " enable_weights_quantization=True,\n", @@ -227,7 +227,7 @@ "\n", " # define a quantization config to quantize the bias (for layers where there is a bias attribute).\n", " bias_config = AttributeQuantizationConfig(\n", - " weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,\n", + " weights_quantization_method=QuantizationMethod.POWER_OF_TWO,\n", " weights_n_bits=FLOAT_BITWIDTH,\n", " weights_per_channel_threshold=False,\n", " enable_weights_quantization=False,\n", @@ -237,11 +237,11 @@ " # AttributeQuantizationConfig for weights with no specific AttributeQuantizationConfig.\n", " # MCT will compress a layer's kernel and bias according to the configurations that are\n", " # set in KERNEL_ATTR and BIAS_ATTR that are passed in attr_weights_configs_mapping.\n", - " default_config = tp.OpQuantizationConfig(\n", + " default_config = OpQuantizationConfig(\n", " default_weight_attr_config=default_weight_attr_config,\n", " attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config,\n", " BIAS_ATTR: bias_config},\n", - " activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,\n", + " activation_quantization_method=QuantizationMethod.POWER_OF_TWO,\n", " activation_n_bits=3,\n", " supported_input_activation_n_bits=8,\n", " enable_activation_quantization=True,\n", @@ -253,29 +253,21 @@ "\n", " # Set default QuantizationConfigOptions in new TargetPlatformCapabilities to be used when no other\n", " # QuantizationConfigOptions is set for an OperatorsSet.\n", - " default_configuration_options = tp.QuantizationConfigOptions([default_config])\n", - " tp_model = tp.TargetPlatformCapabilities(default_configuration_options,\n", - " tpc_minor_version=1,\n", - " tpc_patch_version=0,\n", - " tpc_platform_type=\"custom_qat_notebook_tpc\")\n", - " with tp_model:\n", - " default_qco = tp.get_default_quantization_config_options()\n", - " # Group of OperatorsSets that should not be quantized.\n", - " tp.OperatorsSet(\"NoQuantization\",\n", - " default_qco.clone_and_edit(enable_activation_quantization=False)\n", - " .clone_and_edit_weight_attribute(enable_weights_quantization=False))\n", - " # Group of linear OperatorsSets such as convolution and matmul.\n", - " tp.OperatorsSet(\"LinearOp\")\n", + " default_configuration_options = QuantizationConfigOptions(quantization_configurations=[default_config])\n", + " no_quantization_config = (default_configuration_options.clone_and_edit(enable_activation_quantization=False)\n", + " .clone_and_edit_weight_attribute(enable_weights_quantization=False))\n", + "\n", + " operator_set = []\n", + "\n", + " operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.DROPOUT, qc_options=no_quantization_config))\n", + " operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.FLATTEN, qc_options=no_quantization_config))\n", + "\n", "\n", - " tpc = tp.FrameworkQuantizationCapabilities(tp_model)\n", - " with tpc:\n", - " # No need to quantize Flatten and Dropout layers\n", - " tp.OperationsSetToLayers(\"NoQuantization\", [layers.Flatten, layers.Dropout])\n", - " # Assign the framework layers' attributes to KERNEL_ATTR and BIAS_ATTR that were used during creation\n", - " # of the default OpQuantizationConfig.\n", - " tp.OperationsSetToLayers(\"LinearOp\", [layers.Dense, layers.Conv2D],\n", - " attr_mapping={KERNEL_ATTR: DefaultDict(default_value=KERAS_KERNEL),\n", - " BIAS_ATTR: DefaultDict(default_value=BIAS)})\n", + " tpc = TargetPlatformCapabilities(default_qco=default_configuration_options,\n", + " tpc_minor_version=1,\n", + " tpc_patch_version=0,\n", + " tpc_platform_type=\"custom_qat_notebook_tpc\",\n", + " operator_set=tuple(operator_set))\n", " return tpc\n" ] },