LEIP Train¶
- leip.train(options: TrainOptions) None ¶
Generates a Quantization Guided Training script.
- Parameters
options (TrainOptions) – Options that configure the training.
- Return type
None
- class leip.TrainOptions(*, input_path: Path, output_path: CreatedDirectoryPath = './outputTrain', training_config_file: Path = PosixPath('leip_train_config.json'), training_script_path: Path = PosixPath('leip_train.py'), invoke_training: bool = True, step: TrainSteps = TrainSteps.TRAIN)¶
Options for quantization guided training.
- Parameters
input_path (Path) –
output_path (CreatedDirectoryPath) –
training_config_file (Path) –
training_script_path (Path) –
invoke_training (bool) –
step (TrainSteps) –
- Return type
None
Quantization Guided Callback¶
- class leip.core.train.quantization_guided_callback.QGMonitoringCallback(epoch_begin=False, epoch_end=False, batch_begin=False, batch_end=False)¶
This class implements automatic monitoring of quantization guided regularization of the model weights.
- on_batch_begin(batch, logs=None)¶
Called on the beginning of the training batch with the batch number.
- on_batch_end(batch, logs=None)¶
Called on the end of the training batch with the batch number.
- on_epoch_begin(epoch, logs=None)¶
Called on the beginning of the training epoch with the epoch number.
- on_epoch_end(epoch, logs=None)¶
Called on the end of the training epoch with the epoch number.
- class leip.core.train.quantization_guided_callback.QuantizationGuidedCallback(callbacks_dict, epoch_begin=False, epoch_end=False, batch_begin=False, batch_end=False)¶
Callback class to monitor Quantization Guided Training. Subclass of tf.keras.callbacks.Callback.
This class can be subclassed to provide more fine grained control of the updating of lambda parameters during training.
- on_batch_begin(batch, logs=None)¶
Called on the beginning of the training batch with the batch number.
- on_batch_end(batch, logs=None)¶
Called on the end of the training batch with the batch number.
- on_epoch_begin(epoch, logs=None)¶
Called on the beginning of the training epoch with the epoch number.
- on_epoch_end(epoch, logs=None)¶
Called on the end of the training epoch with the epoch number.
Quantization Guided Config Help¶
- leip.core.train.quantization_guided.config_help.config_to_help_string()¶
config_help needs to be kept in sync with the config[“help”] dict in ./train_template.py.template
Quantization Guided Constraint¶
- class leip.core.train.constraints.quantization_guided_constraint.QuantizationGuidedConstraint(num_bits=8, tolerance=1e-05, quantizer_name='asymmetric')¶
Quantization Guided Constraint class. Subclass of tf.keras.constraints.Constraint. Constraint to restrict weights to a given range during training.
- asymmetric(w)¶
Implements the asymmetric quantization of the weight tensor w
- get_config()¶
Returns a python dict of the configuration parameters for this constraint instance
- get_serialized()¶
Returns a serialized representation of the constraint as a python dict
- quantizer(w)¶
Calls the appropriate quantizer implementation pointed to by the self.quantizer_name on the weight tensor w passed in
- to_json()¶
Returns a JSON string representation of the constraint
- update(num_bits=None, tolerance=None)¶
Updates the attributes of the instance. New values for either num_bits or tolerance or both can be supplied. This update may be called by the training framework either between epochs or even between batches.
Quantization Guided Regularizer¶
- class leip.core.train.regularizers.quantization_guided_regularizer.QuantizationGuidedCWConv2DRegularizer(*args, **kwargs)¶
A regularizer class for Conv2D layers (channel_axis=-1) based on QAChannelwiseRegularizer.
- class leip.core.train.regularizers.quantization_guided_regularizer.QuantizationGuidedCWDepthwiseConv2DRegularizer(*args, **kwargs)¶
A regularizer class for DepthwiseConv2D layers (channel_axis=-2) based on QAChannelwiseRegularizer.
- class leip.core.train.regularizers.quantization_guided_regularizer.QuantizationGuidedCWRegularizer(*args, **kwargs)¶
A class for channel-wise regularization.
- asymmetric(w)¶
Channel-wise asymmetric quantizer. Overrides the parent class’s asymmetric quantizer.
- symmetric(w)¶
Channel-wise symmetric quantizer.Overrides the parent class’s symmetric quantizer.
Quantization Guided Utilities¶
Script containing utilities for attaching regularizer
- class leip.core.train.utilities.QuantizationGuidedCWConv2DRegularizer(*args, **kwargs)¶
A regularizer class for Conv2D layers (channel_axis=-1) based on QAChannelwiseRegularizer.
- class leip.core.train.utilities.QuantizationGuidedCWDepthwiseConv2DRegularizer(*args, **kwargs)¶
A regularizer class for DepthwiseConv2D layers (channel_axis=-2) based on QAChannelwiseRegularizer.
- class leip.core.train.utilities.QuantizationGuidedConstraint(num_bits=8, tolerance=1e-05, quantizer_name='asymmetric')¶
Quantization Guided Constraint class. Subclass of tf.keras.constraints.Constraint. Constraint to restrict weights to a given range during training.
- asymmetric(w)¶
Implements the asymmetric quantization of the weight tensor w
- get_config()¶
Returns a python dict of the configuration parameters for this constraint instance
- get_serialized()¶
Returns a serialized representation of the constraint as a python dict
- quantizer(w)¶
Calls the appropriate quantizer implementation pointed to by the self.quantizer_name on the weight tensor w passed in
- to_json()¶
Returns a JSON string representation of the constraint
- update(num_bits=None, tolerance=None)¶
Updates the attributes of the instance. New values for either num_bits or tolerance or both can be supplied. This update may be called by the training framework either between epochs or even between batches.
- class leip.core.train.utilities.TrainOptions(*, input_path: Path, output_path: CreatedDirectoryPath = './outputTrain', training_config_file: Path = PosixPath('leip_train_config.json'), training_script_path: Path = PosixPath('leip_train.py'), invoke_training: bool = True, step: TrainSteps = TrainSteps.TRAIN)¶
Options for quantization guided training.
- Parameters
input_path (Path) –
output_path (CreatedDirectoryPath) –
training_config_file (Path) –
training_script_path (Path) –
invoke_training (bool) –
step (TrainSteps) –
- Return type
None
- leip.core.train.utilities.apply_quantization(model)¶
A Helper function that applies the quantization based on the regularizer. The idea is to use the regularizer’s own quantizer to change the weights.
Note that this function assumes that “X_regularizer” is the regularizer for “X”. As long as this convention is followed, custom layers can be accommodated.
T.B.D.: This method may add extra nods to the graph.
- leip.core.train.utilities.asymmetric_quantizer_error(num_bits, w)¶
Computes the quantization error for asymmetric quantization. Note that this quantizer performs zero-nudging.
- Parameters
num_bits (int) – Number of bits.
w (np.ndarray) – Array to measure it quantization
error. –
- Returns
float
- leip.core.train.utilities.attach_regularizers(model_or_path, attch_json_scheme, target_keras_h5_file=None, backend_session_reset=True, use_weights=False, strategy=None, custom_objects={})¶
Attaches regularizer to model_or_path
- Parameters
model_or_path (str or tf.keras.Model) –
attch_json_scheme (str or dict) –
target_keras_h5_file (str or None) – If provided, will save the
file. (new TF Keras model to this) –
backend_session_reset (bool) – If True will reset the backend session.
use_weights (bool) – If True, will use the weights from model_or_path.
strategy (None or tf.distributed) – If not None, with deserialize the
scope. (model under this strategy's) –
custom_objects (dict) – Custom objects of the model.
- Returns
The new tf keras model instance.
- leip.core.train.utilities.copy_and_invoke(training_script_path, config_path=None, invoke=True)¶
Copy the template script to the specified location.
If a config_path is specified, edit the config dict in the script to read that in and apply it over the default config values.
If invoke is True, run the script.
- leip.core.train.utilities.list_tf_keras_model(model_or_path, return_json=False, **kwargs)¶
Lists the contents of the TF Keras model. This is a CLI utility.
- Parameters
model_or_path (str or tf.keras.Model) –
return_json (bool) – If True, will return attach json.
contains (**kwargs) – custom_objects (dict): Dictionary of custom layer names and their signatures. print_model_json (bool): Set to True to print model json print_variables (bool): Set to True to list model parameters. save_model_json (str): save_regularizers_json (str): attach_regularizer_to_all (str): If not None, and an interpretable json string, will be used for all kernels and biases.
- leip.core.train.utilities.strip_model_for_inference(model, custom_objects=None)¶
Returns the model stripped of any ops not needed for inference.
- leip.core.train.utilities.validate_regularizer_attachment_scheme(scheme)¶
Ensure the value for the regularizer_attachment_scheme is either a string (in which case it is a path to a JSON file), or the scheme itself, which is a list of items, each of which are validated.