# paddleslim.common package

class `paddleslim.common.``EvolutionaryController`

  Bases: `object`

  Abstract controller for all evolutionary searching method.

  `next_tokens`()

    Generate new tokens.

    Returns: | The next searched tokens.
    Return type: | list<list>

  `reset`(range_table, constrain_func=None)

    Reset the controller.

    Parameters:

    - range_table (list<int>) – It is used to define the searching space of controller. The tokens[i] generated by controller should be in [0, range_table[i]).
    - constrain_func (function) – It is used to check whether tokens meet the constraint. None means there is no constraint. Default: None.

  `update`(tokens, reward)

    Update the status of controller according current tokens and reward.

    Parameters:

    - tokens (list<int>) – A solution of searching task.
    - reward (list<int>) – The reward of tokens.

class `paddleslim.common.``SAController`(range_table=None, reduce_rate=0.85, init_temperature=None, max_try_times=300, init_tokens=None, reward=-1, max_reward=-1, iters=0, best_tokens=None, constrain_func=None, checkpoints=None, searched=None)

  Bases: `paddleslim.common.controller.EvolutionaryController`

  Simulated annealing controller.

  Parameters:

  - range_table (list<int>) – Range table.
  - reduce_rate (float) – The decay rate of temperature.
  - init_temperature (float) – Init temperature.
  - max_try_times (int) – max try times before get legal tokens. Default: 300.
  - init_tokens (list<int>) – The initial tokens. Default: None.
  - reward (float) – The reward of current tokens. Default: -1.
  - max_reward (float) – The max reward in the search of sanas, in general, best tokens get max reward. Default: -1.
  - iters (int) – The iteration of sa controller. Default: 0.
  - best_tokens (list<int>) – The best tokens in the search of sanas, in general, best tokens get max reward. Default: None.
  - constrain_func (function) – The callback function used to check whether the tokens meet constraint. None means there is no constraint. Default: None.
  - checkpoints (str) – if checkpoint is None, donnot save checkpoints, else save scene to checkpoints file.
  - searched (dict<list, float>) – remember tokens which are searched.

  `best_tokens`

    Get current best tokens.

    Returns: | The best tokens.
    Return type: | list<int>

  `current_tokens`

    Get tokens generated in current searching step.

    Returns: | The best tokens.
    Return type: | list<int>

  `max_reward`
  `next_tokens`(control_token=None)

    Get next tokens.

    Parameters: | control_token – The tokens used to generate next tokens.
    Returns: | The next tokens.
    Return type: | list<int>

  `update`(tokens, reward, iter, client_num)

    Update the controller according to latest tokens and reward.

    Parameters:

    - tokens (list<int>) – The tokens generated in current step.
    - reward (float) – The reward of tokens.
    - iter (int) – The current step of searching client.
    - client_num (int) – The total number of searching client.

`paddleslim.common.``get_logger`(name, level, fmt='%(asctime)s-%(levelname)s: %(message)s')

  Get logger from logging with given name, level and format without setting logging basicConfig. For setting basicConfig in paddle will disable basicConfig setting after import paddle.

  Parameters:

  - name (str) – The logger name.
  - level (logging.LEVEL) – The base level of the logger
  - fmt (str) – Format of logger output

  Returns: | logging logger with given setttings
  Return type: | logging.Logger

  Examples:

      logger = log_helper.get_logger(__name__, logging.INFO,
                           fmt='%(asctime)s-%(levelname)s: %(message)s')

class `paddleslim.common.``ControllerServer`(controller=None, address=('', 0), max_client_num=100, search_steps=None, key=None)

  Bases: `object`

  The controller wrapper with a socket server to handle the request of search agent. :param controller: The controller used to generate tokens. :type controller: slim.searcher.Controller :param address: The address of current server binding with format (ip, port). Default: (‘’, 0).

  > which means setting ip automatically

  Parameters:

  - max_client_num (int) – The maximum number of clients connecting to current server simultaneously. Default: 100.
  - search_steps (int|None) – The total steps of searching. None means never stopping. Default: None
  - key (str|None) – Config information. Default: None.

  `close`()

    Close the server.

  `ip`()

    Get the ip.

  `port`()

    Get the port.

  `run`()

    Start the server.

  `start`()

class `paddleslim.common.``ControllerClient`(server_ip=None, server_port=None, key=None, client_name=None)

  Bases: `object`

  Controller client. :param server_ip: The ip that controller server listens on. None means getting the ip automatically. Default: None. :type server_ip: str :param server_port: The port that controller server listens on. 0 means getting usable port automatically. Default: 0. :type server_port: int :param key: The key used to identify legal agent for controller server. Default: “light-nas” :type key: str :param client_name: Current client name, random generate for counting client number. Default: None. :type client_name: str

  `next_tokens`()

    Get next tokens.

  `request_current_info`()

    Request for current information.

  `update`(tokens, reward, iter)

    Update the controller according to latest tokens and reward.

    Parameters:

    - tokens (list<int>) – The tokens generated in last step.
    - reward (float) – The reward of tokens.
    - iter (int) – The iteration number of current client.

`paddleslim.common.``lock`(file)

  Lock the file in local file system.

`paddleslim.common.``unlock`(file)

  Unlock the file in local file system.

`paddleslim.common.``cached_reader`(reader, sampled_rate, cache_path, cached_id)

  Sample partial data from reader and cache them into local file system.

  Parameters:

  - reader – Iterative data source.
  - sampled_rate (float) – The sampled rate used to sample partial data for evaluation. None means using all data in eval_reader. default: None.
  - cache_path (str) – The path to cache the sampled data.
  - cached_id (int) – The id of dataset sampled. Evaluations with same cached_id use the same sampled dataset. default: 0.

## Submodules

## paddleslim.common.cached_reader module

`paddleslim.common.cached_reader.``cached_reader`(reader, sampled_rate, cache_path, cached_id)

  Sample partial data from reader and cache them into local file system.

  Parameters:

  - reader – Iterative data source.
  - sampled_rate (float) – The sampled rate used to sample partial data for evaluation. None means using all data in eval_reader. default: None.
  - cache_path (str) – The path to cache the sampled data.
  - cached_id (int) – The id of dataset sampled. Evaluations with same cached_id use the same sampled dataset. default: 0.

## paddleslim.common.controller module

The controller used to search hyperparameters or neural architecture

class `paddleslim.common.controller.``EvolutionaryController`

  Bases: `object`

  Abstract controller for all evolutionary searching method.

  `next_tokens`()

    Generate new tokens.

    Returns: | The next searched tokens.
    Return type: | list<list>

  `reset`(range_table, constrain_func=None)

    Reset the controller.

    Parameters:

    - range_table (list<int>) – It is used to define the searching space of controller. The tokens[i] generated by controller should be in [0, range_table[i]).
    - constrain_func (function) – It is used to check whether tokens meet the constraint. None means there is no constraint. Default: None.

  `update`(tokens, reward)

    Update the status of controller according current tokens and reward.

    Parameters:

    - tokens (list<int>) – A solution of searching task.
    - reward (list<int>) – The reward of tokens.

## paddleslim.common.controller_client module

class `paddleslim.common.controller_client.``ControllerClient`(server_ip=None, server_port=None, key=None, client_name=None)

  Bases: `object`

  Controller client. :param server_ip: The ip that controller server listens on. None means getting the ip automatically. Default: None. :type server_ip: str :param server_port: The port that controller server listens on. 0 means getting usable port automatically. Default: 0. :type server_port: int :param key: The key used to identify legal agent for controller server. Default: “light-nas” :type key: str :param client_name: Current client name, random generate for counting client number. Default: None. :type client_name: str

  `next_tokens`()

    Get next tokens.

  `request_current_info`()

    Request for current information.

  `update`(tokens, reward, iter)

    Update the controller according to latest tokens and reward.

    Parameters:

    - tokens (list<int>) – The tokens generated in last step.
    - reward (float) – The reward of tokens.
    - iter (int) – The iteration number of current client.

## paddleslim.common.controller_server module

class `paddleslim.common.controller_server.``ControllerServer`(controller=None, address=('', 0), max_client_num=100, search_steps=None, key=None)

  Bases: `object`

  The controller wrapper with a socket server to handle the request of search agent. :param controller: The controller used to generate tokens. :type controller: slim.searcher.Controller :param address: The address of current server binding with format (ip, port). Default: (‘’, 0).

  > which means setting ip automatically

  Parameters:

  - max_client_num (int) – The maximum number of clients connecting to current server simultaneously. Default: 100.
  - search_steps (int|None) – The total steps of searching. None means never stopping. Default: None
  - key (str|None) – Config information. Default: None.

  `close`()

    Close the server.

  `ip`()

    Get the ip.

  `port`()

    Get the port.

  `run`()

    Start the server.

  `start`()

## paddleslim.common.lock module

`paddleslim.common.lock.``lock`(file)

  Lock the file in local file system.

`paddleslim.common.lock.``unlock`(file)

  Unlock the file in local file system.

## paddleslim.common.log_helper module

`paddleslim.common.log_helper.``get_logger`(name, level, fmt='%(asctime)s-%(levelname)s: %(message)s')

  Get logger from logging with given name, level and format without setting logging basicConfig. For setting basicConfig in paddle will disable basicConfig setting after import paddle.

  Parameters:

  - name (str) – The logger name.
  - level (logging.LEVEL) – The base level of the logger
  - fmt (str) – Format of logger output

  Returns: | logging logger with given setttings
  Return type: | logging.Logger

  Examples:

      logger = log_helper.get_logger(__name__, logging.INFO,
                           fmt='%(asctime)s-%(levelname)s: %(message)s')

## paddleslim.common.sa_controller module

The controller used to search hyperparameters or neural architecture

class `paddleslim.common.sa_controller.``SAController`(range_table=None, reduce_rate=0.85, init_temperature=None, max_try_times=300, init_tokens=None, reward=-1, max_reward=-1, iters=0, best_tokens=None, constrain_func=None, checkpoints=None, searched=None)

  Bases: `paddleslim.common.controller.EvolutionaryController`

  Simulated annealing controller.

  Parameters:

  - range_table (list<int>) – Range table.
  - reduce_rate (float) – The decay rate of temperature.
  - init_temperature (float) – Init temperature.
  - max_try_times (int) – max try times before get legal tokens. Default: 300.
  - init_tokens (list<int>) – The initial tokens. Default: None.
  - reward (float) – The reward of current tokens. Default: -1.
  - max_reward (float) – The max reward in the search of sanas, in general, best tokens get max reward. Default: -1.
  - iters (int) – The iteration of sa controller. Default: 0.
  - best_tokens (list<int>) – The best tokens in the search of sanas, in general, best tokens get max reward. Default: None.
  - constrain_func (function) – The callback function used to check whether the tokens meet constraint. None means there is no constraint. Default: None.
  - checkpoints (str) – if checkpoint is None, donnot save checkpoints, else save scene to checkpoints file.
  - searched (dict<list, float>) – remember tokens which are searched.

  `best_tokens`

    Get current best tokens.

    Returns: | The best tokens.
    Return type: | list<int>

  `current_tokens`

    Get tokens generated in current searching step.

    Returns: | The best tokens.
    Return type: | list<int>

  `max_reward`
  `next_tokens`(control_token=None)

    Get next tokens.

    Parameters: | control_token – The tokens used to generate next tokens.
    Returns: | The next tokens.
    Return type: | list<int>

  `update`(tokens, reward, iter, client_num)

    Update the controller according to latest tokens and reward.

    Parameters:

    - tokens (list<int>) – The tokens generated in current step.
    - reward (float) – The reward of tokens.
    - iter (int) – The current step of searching client.
    - client_num (int) – The total number of searching client.