Skip to content

Commit aeeace7

Browse files
committed
add documentation regarding limitations of log_metric method
1 parent 11499bf commit aeeace7

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/smexperiments/tracker.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ def load(
8282
):
8383
"""Create a new ``Tracker`` by loading an existing trial component.
8484
85+
Note that `log_metric` will only work from a training job host.
86+
8587
Examples:
8688
.. code-block:: python
8789
@@ -142,7 +144,7 @@ def load(
142144
else:
143145
raise ValueError('Could not load TrialComponent. Specify a trial_component_name or invoke "create"')
144146

145-
# if running in a SageMaker context write metrics to file
147+
# metrics require the metrics agent running on training job hosts
146148
if not trial_component_name and tce.environment_type == _environment.EnvironmentType.SageMakerTrainingJob:
147149
metrics_writer = metrics.SageMakerFileMetricsWriter()
148150
else:
@@ -168,6 +170,8 @@ def create(
168170
):
169171
"""Create a new ``Tracker`` by creating a new trial component.
170172
173+
Note that `log_metric` will _not_ work when tracker is created this way.
174+
171175
Examples
172176
.. code-block:: python
173177
@@ -197,7 +201,10 @@ def create(
197201
sagemaker_boto_client=sagemaker_boto_client,
198202
)
199203

200-
metrics_writer = metrics.SageMakerFileMetricsWriter()
204+
# metrics require the metrics agent running on training job hosts and in which case the load
205+
# method should be used because it loads the trial component associated with the currently
206+
# running training job
207+
metrics_writer = None
201208

202209
return cls(
203210
tc,
@@ -364,7 +371,10 @@ def log_input_artifact(self, file_path, name=None, media_type=None):
364371
self._lineage_artifact_tracker.add_input_artifact(name, s3_uri, etag, media_type)
365372

366373
def log_metric(self, metric_name, value, timestamp=None, iteration_number=None):
367-
"""Record a scalar metric value for this TrialComponent to file, not SageMaker.
374+
"""Record a scalar metric value for this TrialComponent.
375+
376+
Note that metrics logged with this method will only appear in SageMaker when this method
377+
is called from a training job host.
368378
369379
Examples
370380
.. code-block:: python

tests/unit/test_tracker.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,7 @@ def test_create(boto3_session, sagemaker_boto_client):
151151
)
152152
assert trial_component_name == tracker_created.trial_component.trial_component_name
153153

154-
assert tracker_created._metrics_writer is not None
155-
156-
tracker_created._metrics_writer = unittest.mock.Mock()
157-
now = datetime.datetime.now()
158-
tracker_created.log_metric("foo", 1.0, 1, now)
159-
tracker_created._metrics_writer.log_metric.assert_called_with("foo", 1.0, 1, now)
154+
assert tracker_created._metrics_writer is None
160155

161156

162157
@pytest.fixture

0 commit comments

Comments
 (0)