|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""This file contains util functions for the sagemaker Defaults Config. |
| 14 | +
|
| 15 | +These utils may be used inside or outside the config module. |
| 16 | +""" |
| 17 | +from __future__ import absolute_import |
| 18 | + |
| 19 | +import logging |
| 20 | +import sys |
| 21 | + |
| 22 | + |
| 23 | +def get_sagemaker_config_logger(): |
| 24 | + """Return a logger with the name 'sagemaker.config' |
| 25 | +
|
| 26 | + If the logger to be returned has no level or handlers set, this will get level and handler |
| 27 | + attributes. (So if the SDK user has setup loggers in a certain way, that setup will not be |
| 28 | + changed by this function.) It is safe to make repeat calls to this function. |
| 29 | + """ |
| 30 | + sagemaker_config_logger = logging.getLogger("sagemaker.config") |
| 31 | + sagemaker_logger = logging.getLogger("sagemaker") |
| 32 | + |
| 33 | + if sagemaker_config_logger.level == logging.NOTSET: |
| 34 | + sagemaker_config_logger.setLevel(logging.INFO) |
| 35 | + |
| 36 | + # check sagemaker_logger here as well, so that if handlers were set for the parent logger |
| 37 | + # already, we dont change behavior for the child logger |
| 38 | + if len(sagemaker_config_logger.handlers) == 0 and len(sagemaker_logger.handlers) == 0: |
| 39 | + # use sys.stdout so logs dont show up with a red background in a notebook |
| 40 | + handler = logging.StreamHandler(sys.stdout) |
| 41 | + |
| 42 | + formatter = logging.Formatter("%(name)s %(levelname)-4s - %(message)s") |
| 43 | + handler.setFormatter(formatter) |
| 44 | + sagemaker_config_logger.addHandler(handler) |
| 45 | + |
| 46 | + # if a handler is being added, we dont want the root handler to also process the same events |
| 47 | + sagemaker_config_logger.propagate = False |
| 48 | + |
| 49 | + return sagemaker_config_logger |
| 50 | + |
| 51 | + |
| 52 | +def _log_sagemaker_config_single_substitution(source_value, config_value, config_key_path: str): |
| 53 | + """Informs the SDK user whether a config value was present and automatically substituted |
| 54 | +
|
| 55 | + Args: |
| 56 | + source_value: The value that will be used if it exists. Usually, this is user-provided |
| 57 | + input to a Class or to a session.py method, or None if no input was provided. |
| 58 | + config_value: The value fetched from sagemaker_config. If it exists, this is the value that |
| 59 | + will be used if direct_input is None. |
| 60 | + config_key_path: A string denoting the path of keys that point to the config value in the |
| 61 | + sagemaker_config. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + None. Logs information to the "sagemaker.config" logger. |
| 65 | + """ |
| 66 | + logger = get_sagemaker_config_logger() |
| 67 | + |
| 68 | + if config_value is not None: |
| 69 | + |
| 70 | + if source_value is None: |
| 71 | + # Sagemaker Config value is going to be used |
| 72 | + logger.info( |
| 73 | + "Applied value\n config key = %s\n config value that will be used = %s", |
| 74 | + config_key_path, |
| 75 | + config_value, |
| 76 | + ) |
| 77 | + elif source_value is not None and config_value != source_value: |
| 78 | + # Sagemaker Config had a value defined that is NOT going to be used |
| 79 | + # and the config value has not already been applied earlier |
| 80 | + logger.debug( |
| 81 | + ( |
| 82 | + "Skipped value\n" |
| 83 | + " config key = %s\n" |
| 84 | + " config value = %s\n" |
| 85 | + " source value that will be used = %s", |
| 86 | + ), |
| 87 | + config_key_path, |
| 88 | + config_value, |
| 89 | + source_value, |
| 90 | + ) |
| 91 | + elif source_value is not None and config_value == source_value: |
| 92 | + # Sagemaker Config had a value defined that is NOT going to be used here. |
| 93 | + # Either (1) the config value was already fetched and applied earlier, or |
| 94 | + # (2) the user happened to pass in the same value. |
| 95 | + # Logged as a debug statement because most users do not need to know this. |
| 96 | + logger.debug( |
| 97 | + ( |
| 98 | + "Skipped value\n" |
| 99 | + " config key = %s\n" |
| 100 | + " config value = %s\n" |
| 101 | + " source value that will be used = %s" |
| 102 | + ), |
| 103 | + config_key_path, |
| 104 | + config_value, |
| 105 | + source_value, |
| 106 | + ) |
| 107 | + else: |
| 108 | + # nothing was specified in the config and nothing is being automatically applied |
| 109 | + logger.debug("Skipped value because no value defined\n config key = %s", config_key_path) |
| 110 | + |
| 111 | + |
| 112 | +def _log_sagemaker_config_merge( |
| 113 | + source_value=None, |
| 114 | + config_value=None, |
| 115 | + merged_source_and_config_value=None, |
| 116 | + config_key_path: str = None, |
| 117 | +): |
| 118 | + """Informs the SDK user whether a config value was present and automatically substituted |
| 119 | +
|
| 120 | + Args: |
| 121 | + source_value: The dict or object that would be used if no default values existed. Usually, |
| 122 | + this is user-provided input to a Class or to a session.py method, or None if no input |
| 123 | + was provided. |
| 124 | + config_value: The dict or object fetched from sagemaker_config. If it exists, this is the |
| 125 | + value that will be used if source_value is None. |
| 126 | + merged_source_and_config_value: The value that results from the merging of source_value and |
| 127 | + original_config_value. This will be the value used. |
| 128 | + config_key_path: A string denoting the path of keys that point to the config value in the |
| 129 | + sagemaker_config. |
| 130 | +
|
| 131 | + Returns: |
| 132 | + None. Logs information to the "sagemaker.config" logger. |
| 133 | + """ |
| 134 | + logger = get_sagemaker_config_logger() |
| 135 | + |
| 136 | + if config_value: |
| 137 | + |
| 138 | + if source_value == merged_source_and_config_value: |
| 139 | + # Sagemaker Config had a value defined that is NOT going to be used here. |
| 140 | + # Either (1) the config value was already fetched and applied earlier, or |
| 141 | + # (2) the user happened to pass in the same values. |
| 142 | + # Logged as a debug statement because most users do not need to know this. |
| 143 | + logger.debug( |
| 144 | + ( |
| 145 | + "Skipped values\n" |
| 146 | + " config key = %s\n" |
| 147 | + " config value = %s\n" |
| 148 | + " source value that will be used = %s" |
| 149 | + ), |
| 150 | + config_key_path, |
| 151 | + config_value, |
| 152 | + merged_source_and_config_value, |
| 153 | + ) |
| 154 | + else: |
| 155 | + logger.info( |
| 156 | + ( |
| 157 | + "Applied values\n" |
| 158 | + " config key = %s\n" |
| 159 | + " config value = %s\n" |
| 160 | + " source value = %s\n" |
| 161 | + " combined value that will be used = %s" |
| 162 | + ), |
| 163 | + config_key_path, |
| 164 | + config_value, |
| 165 | + source_value, |
| 166 | + merged_source_and_config_value, |
| 167 | + ) |
| 168 | + else: |
| 169 | + # nothing was specified in the config and nothing is being automatically applied |
| 170 | + logger.debug("Skipped value because no value defined\n config key = %s", config_key_path) |
0 commit comments