Exemple #1
0
def parse_save_config_dict(params, mode=None) -> Dict:
    """Grab the relevant keys for a SaveConfig and return a dictionary."""
    if not isinstance(params, dict):
        raise ValueError("parameter must be dict")

    if mode is None:
        prefix = ""
    elif mode == ModeKeys.TRAIN:
        prefix = "train."
    elif mode == ModeKeys.EVAL:
        prefix = "eval."
    elif mode == ModeKeys.PREDICT:
        prefix = "predict."
    elif mode == ModeKeys.GLOBAL:
        prefix = "global."
    else:
        raise ValueError(f"Invalid mode={mode}.")

    # Only look at keys starting with prefix
    params = {
        key[len(prefix):]: value
        for key, value in params.items() if key.startswith(prefix)
    }

    # Parse relevant key-value pairs and place them in a new `ret` dictionary
    ret = {}
    if "save_interval" in params:
        ret["save_interval"] = params["save_interval"]
    if "save_steps" in params:
        ret["save_steps"] = [int(x) for x in split(params["save_steps"])]
    if "start_step" in params:
        ret["start_step"] = params["start_step"]
    if "end_step" in params:
        ret["end_step"] = params["end_step"]
    return ret
def add_collections_to_manager(collection_manager, params_dict, hook_params):
    """Read the config file from an environment variable and return a dictionary.

    Return a dictionary, example keys:
    dict_keys(['reduction_configs', 'save_configs', 'collections', 'out_dir', 'reduction_config', 'save_config',
    'include_regex', 'config_name', 's3_path'])
    """
    # For each collection configuration, also create the reduction config and save config.
    if (
        CONFIG_COLLECTION_CONFIG_KEY in params_dict
        and params_dict[CONFIG_COLLECTION_CONFIG_KEY] is not None
    ):
        for config in params_dict[CONFIG_COLLECTION_CONFIG_KEY]:
            # Require name and parameters for each collection.
            if CONFIG_COLLECTION_NAME_KEY not in config:
                raise ValueError(f"Must specify '{CONFIG_COLLECTION_NAME_KEY}' in JSON config.")

            name = config[CONFIG_COLLECTION_NAME_KEY]
            coll_params = config.get(CONFIG_COLLECTION_PARAMS_KEY, {})
            # If we have {"CollectionParameters": null}, replace null with {}.
            coll_params = {} if coll_params is None else coll_params
            coll = collection_manager.get(name)
            coll_config_modes = parse_save_config_modes_dict(
                params=coll_params, base_config_modes=hook_params["save_config_modes"]
            )
            mode_save_configs = {
                mode: SaveConfigMode.from_dict(val) for mode, val in coll_config_modes.items()
            }
            coll.save_config = mode_save_configs
            if "reductions" in coll_params:
                coll.reduction_config = ReductionConfig.from_dict(coll_params)
            if "include_regex" in coll_params:
                coll.include(split(coll_params["include_regex"]))
            if "save_histogram" in coll_params:
                coll.save_histogram = parse_bool(coll_params["save_histogram"], True)
Exemple #3
0
    def from_dict(cls, params: Dict[str, Any]) -> "ReductionConfig":
        """Parses a flattened dict with two keys: `save_raw_tensor` and `reductions`."""
        if params is None:
            return None
        if not isinstance(params, dict):
            raise ValueError(f"params={params} must be dict")
        save_shape = params.get("save_shape", False)
        save_raw_tensor = params.get("save_raw_tensor", False)
        # Parse comma-separated string into array
        all_reductions = split(params.get("reductions", ""))
        # Parse list of reductions into various types, e.g. convert "abs_l1_norm" into "l1"
        reductions, norms, abs_reductions, abs_norms = [], [], [], []
        for red in all_reductions:
            if red != "":  # possible artifact of using split()
                if red.startswith("abs_"):
                    if red.endswith("_norm"):
                        abs_norms.append(
                            red.split("_")[1])  # abs_l1_norm -> l1
                    else:
                        abs_reductions.append(
                            red.split("_")[1])  # abs_mean -> mean
                else:
                    if red.endswith("_norm"):
                        norms.append(red.split("_")[0])  # l1_norm -> l1
                    else:
                        reductions.append(red)  # mean -> mean

        return cls(
            reductions=reductions,
            abs_reductions=abs_reductions,
            norms=norms,
            abs_norms=abs_norms,
            save_raw_tensor=save_raw_tensor,
            save_shape=save_shape,
        )
Exemple #4
0
def collect_hook_config_params(params_dict) -> Dict:
    """Read the config file from an environment variable and return a dictionary.

    Return a dictionary, example keys:
    dict_keys(['reduction_configs', 'save_configs', 'collections', 'out_dir', 'reduction_config', 'save_config',
    'include_regex', 'config_name', 's3_path'])
    """
    # Build params dictionary from the json file

    # Declare defaults
    parsed_params_dict = {
        CONFIG_RDN_CFG_KEY: None,
        CONFIG_REDUCTION_CONFIGS_KEY: {},
        CONFIG_SAVE_CONFIGS_KEY: {},
        CONFIG_INCLUDE_REGEX_KEY: None,
    }
    # Set top-level path parameters
    # SageMaker doesn't have any way to specify this for now, so default to using their path
    parsed_params_dict["out_dir"] = params_dict.get(CONFIG_OUTDIR_KEY,
                                                    DEFAULT_SAGEMAKER_OUTDIR)

    # Get the main HookParameters; pass these as defaults
    hook_params = params_dict.get(CONFIG_HOOK_PARAMS_KEY, {})
    # If we have {"HookParameters": null}, replace null with {}.
    hook_params = {} if hook_params is None else hook_params
    base_config_modes = parse_save_config_modes_dict(params=hook_params)
    parsed_params_dict["save_config_modes"] = base_config_modes
    # If we pass reduction=None, then the full tensor is saved by default
    if "reductions" in hook_params:
        parsed_params_dict[CONFIG_RDN_CFG_KEY] = ReductionConfig.from_dict(
            hook_params)
    if "save_all" in hook_params:
        parsed_params_dict[CONFIG_SAVE_ALL_KEY] = parse_bool(
            hook_params["save_all"], False)
    if "include_regex" in hook_params:
        parsed_params_dict[CONFIG_INCLUDE_REGEX_KEY] = split(
            hook_params["include_regex"])
    if CONFIG_INCLUDE_WORKERS_KEY in hook_params:
        parsed_params_dict[CONFIG_INCLUDE_WORKERS_KEY] = hook_params[
            CONFIG_INCLUDE_WORKERS_KEY]
    parsed_params_dict[EXPORT_TENSORBOARD_KEY] = parse_bool(
        hook_params.get(EXPORT_TENSORBOARD_KEY, False), False)
    parsed_params_dict[TENSORBOARD_DIR_KEY] = hook_params.get(
        TENSORBOARD_DIR_KEY, None)
    return parsed_params_dict