neural_compressor.strategy.bayesian

The Bayesian tuning strategy.

Module Contents

Classes

BayesianTuneStrategy

The Bayesian tuning strategy.

TargetSpace

Holds the param-space coordinates (X) and target values (Y).

BayesianOptimization

The class for bayesian optimization.

Functions

acq_max(ac, gp, y_max, bounds, random_seed[, ...])

Find the maximum of the acquisition function parameters.

class neural_compressor.strategy.bayesian.BayesianTuneStrategy(model, conf, q_dataloader, q_func=None, eval_dataloader=None, eval_func=None, dicts=None, q_hooks=None)

Bases: neural_compressor.strategy.strategy.TuneStrategy

The Bayesian tuning strategy.

next_tune_cfg()

Generate the next tuning config according to bayesian search algorithm.

This strategy comes from the Bayesian optimization package and changed it to a discrete version. It uses Gaussian processes to define the prior/posterior distribution over the black-box function with the tuning history and then finds the tuning configuration that maximizes the expected improvement.

Yields:

tune_config (dict) – A dict containing the tuning configuration for quantization.

neural_compressor.strategy.bayesian.acq_max(ac, gp, y_max, bounds, random_seed, n_warmup=10000, n_iter=10)

Find the maximum of the acquisition function parameters.

Parameters:
  • ac – The acquisition function object that return its point-wise value.

  • gp – A gaussian process fitted to the relevant data.

  • y_max – The current maximum known value of the target function.

  • bounds – The variables bounds to limit the search of the acq max.

  • random_seed – instance of np.RandomState random number generator

  • n_warmup – number of times to randomly sample the acquisition function

  • n_iter – number of times to run scipy.minimize

Returns:

The arg max of the acquisition function.

Return type:

x_max

class neural_compressor.strategy.bayesian.TargetSpace(pbounds, random_seed=9527)

Bases: object

Holds the param-space coordinates (X) and target values (Y).

Allows for constant-time appends while ensuring no duplicates are added.

property empty

Check if the space is empty.

property params

Get all params stored in this space.

property target

Get all target values in this space.

property dim

Get the dimension of this space.

property keys

Get all keys of this space.

property bounds

Get the bounds of this space.

params_to_array(params)

Generate an array from params.

Parameters:

params (Dict) – The dict contains keys in self.keys, and corresponding param.

Returns:

An array contains all params.

Return type:

np.array

array_to_params(x)

Generate an params’ dict from array.

Parameters:

x (np.array) – The array contains all params.

Returns:

the dict contains keys and the params corresponding to it.

Return type:

dict

register(params, target)

Append a point and its target value to the known data.

Runs in amortized constant time.

Parameters:
  • params (ndarray) – a single point, with len(params) == self.dim

  • target (float) – target function value

Raises:

KeyError – if the point is not unique

get_target(params)

Get the target value of params.

Parameters:

params (ndarray) – a single point, with len(params) == self.dim

Returns:

target function value.

Return type:

target (float)

random_sample()

Create random points within the bounds of the space.

Returns:

[num x dim] array points with dimensions corresponding to self._keys

Return type:

data (ndarray)

max()

Get maximum target value found and corresponding parametes.

res()

Get all target values found and corresponding parametes.

class neural_compressor.strategy.bayesian.BayesianOptimization(pbounds, random_seed=9527, verbose=2)

The class for bayesian optimization.

This class takes the parameters bounds in order to find which values for the parameters yield the maximum value using bayesian optimization.

property space

Get the target space.

property max

Get the maximum value of target space.

property res

Get the minimum value of target space.

suggest()

Suggest the most promising points.

gen_next_params()

Get the next parameter.