17
17
import os
18
18
19
19
import pytest
20
- from mock import patch , Mock
20
+ from mock import patch , Mock , MagicMock
21
21
22
- from sagemaker .fw_utils import create_image_uri , UploadedCode
22
+ from sagemaker .fw_utils import create_image_uri
23
23
from sagemaker .model import MODEL_SERVER_WORKERS_PARAM_NAME
24
24
from sagemaker .session import s3_input
25
25
from sagemaker .tensorflow import defaults , TensorFlow , TensorFlowModel , TensorFlowPredictor
@@ -162,6 +162,7 @@ def test_tf_support_gpu_instances(sagemaker_session, tf_version):
162
162
assert tf .train_image () == _get_full_gpu_image_uri (tf_version )
163
163
164
164
165
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
165
166
def test_tf_deploy_model_server_workers (sagemaker_session ):
166
167
tf = _build_tf (sagemaker_session )
167
168
tf .fit (inputs = s3_input ('s3://mybucket/train' ))
@@ -172,6 +173,7 @@ def test_tf_deploy_model_server_workers(sagemaker_session):
172
173
MODEL_SERVER_WORKERS_PARAM_NAME .upper ()]
173
174
174
175
176
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
175
177
def test_tf_deploy_model_server_workers_unset (sagemaker_session ):
176
178
tf = _build_tf (sagemaker_session )
177
179
tf .fit (inputs = s3_input ('s3://mybucket/train' ))
@@ -259,20 +261,16 @@ def test_create_model_with_custom_image(sagemaker_session):
259
261
assert model .image == custom_image
260
262
261
263
262
- @patch ('time.strftime' , return_value = TIMESTAMP )
263
- @patch ('time.time' , return_value = TIME )
264
- @patch ('sagemaker.estimator.tar_and_upload_dir' )
265
- @patch ('sagemaker.model.tar_and_upload_dir' )
266
- def test_tf (m_tar , e_tar , time , strftime , sagemaker_session , tf_version ):
264
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
265
+ @patch ('time.strftime' , MagicMock (return_value = TIMESTAMP ))
266
+ @patch ('time.time' , MagicMock (return_value = TIME ))
267
+ def test_tf (sagemaker_session , tf_version ):
267
268
tf = TensorFlow (entry_point = SCRIPT_FILE , role = ROLE , sagemaker_session = sagemaker_session , training_steps = 1000 ,
268
269
evaluation_steps = 10 , train_instance_count = INSTANCE_COUNT , train_instance_type = INSTANCE_TYPE ,
269
270
framework_version = tf_version , requirements_file = REQUIREMENTS_FILE , source_dir = DATA_DIR )
270
271
271
272
inputs = 's3://mybucket/train'
272
- s3_prefix = 's3://{}/{}/source/sourcedir.tar.gz' .format (BUCKET_NAME , JOB_NAME )
273
- e_tar .return_value = UploadedCode (s3_prefix = s3_prefix , script_name = SCRIPT_FILE )
274
- s3_prefix = 's3://{}/{}/sourcedir.tar.gz' .format (BUCKET_NAME , JOB_NAME )
275
- m_tar .return_value = UploadedCode (s3_prefix = s3_prefix , script_name = SCRIPT_FILE )
273
+
276
274
tf .fit (inputs = inputs )
277
275
278
276
call_names = [c [0 ] for c in sagemaker_session .method_calls ]
@@ -288,7 +286,8 @@ def test_tf(m_tar, e_tar, time, strftime, sagemaker_session, tf_version):
288
286
289
287
environment = {
290
288
'Environment' : {
291
- 'SAGEMAKER_SUBMIT_DIRECTORY' : 's3://{}/{}/sourcedir.tar.gz' .format (BUCKET_NAME , JOB_NAME ),
289
+ 'SAGEMAKER_SUBMIT_DIRECTORY' :
290
+ 's3://mybucket/sagemaker-tensorflow-2017-11-06-14:14:15.673/source/sourcedir.tar.gz' ,
292
291
'SAGEMAKER_PROGRAM' : 'dummy_script.py' , 'SAGEMAKER_REQUIREMENTS' : 'dummy_requirements.txt' ,
293
292
'SAGEMAKER_ENABLE_CLOUDWATCH_METRICS' : 'false' , 'SAGEMAKER_REGION' : 'us-west-2' ,
294
293
'SAGEMAKER_CONTAINER_LOG_LEVEL' : '20'
@@ -318,6 +317,7 @@ def test_run_tensorboard_locally_without_tensorboard_binary(time, strftime, pope
318
317
'following command: \n pip install tensorboard'
319
318
320
319
320
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
321
321
def test_model (sagemaker_session , tf_version ):
322
322
model = TensorFlowModel ("s3://some/data.tar.gz" , role = ROLE , entry_point = SCRIPT_PATH ,
323
323
sagemaker_session = sagemaker_session )
@@ -340,6 +340,7 @@ def test_run_tensorboard_locally_without_awscli_binary(time, strftime, popen, ca
340
340
'following command: \n pip install awscli'
341
341
342
342
343
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
343
344
@patch ('sagemaker.tensorflow.estimator.Tensorboard._sync_directories' )
344
345
@patch ('tempfile.mkdtemp' , return_value = '/my/temp/folder' )
345
346
@patch ('shutil.rmtree' )
@@ -362,6 +363,7 @@ def test_run_tensorboard_locally(sleep, time, strftime, popen, call, access, rmt
362
363
stdout = - 1 )
363
364
364
365
366
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
365
367
@patch ('sagemaker.tensorflow.estimator.Tensorboard._sync_directories' )
366
368
@patch ('tempfile.mkdtemp' , return_value = '/my/temp/folder' )
367
369
@patch ('shutil.rmtree' )
@@ -388,6 +390,7 @@ def test_run_tensorboard_locally_port_in_use(sleep, time, strftime, popen, call,
388
390
stderr = - 1 , stdout = - 1 )
389
391
390
392
393
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
391
394
def test_tf_checkpoint_not_set (sagemaker_session ):
392
395
job_name = "sagemaker-tensorflow-py2-gpu-2017-10-24-14-12-09"
393
396
tf = _build_tf (sagemaker_session , checkpoint_path = None , base_job_name = job_name ,
@@ -398,6 +401,7 @@ def test_tf_checkpoint_not_set(sagemaker_session):
398
401
assert tf .hyperparameters ()['checkpoint_path' ] == expected_result
399
402
400
403
404
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
401
405
def test_tf_training_and_evaluation_steps_not_set (sagemaker_session ):
402
406
job_name = "sagemaker-tensorflow-py2-gpu-2017-10-24-14-12-09"
403
407
output_path = "s3://{}/output/{}/" .format (sagemaker_session .default_bucket (), job_name )
@@ -408,6 +412,7 @@ def test_tf_training_and_evaluation_steps_not_set(sagemaker_session):
408
412
assert tf .hyperparameters ()['evaluation_steps' ] == 'null'
409
413
410
414
415
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
411
416
def test_tf_training_and_evaluation_steps (sagemaker_session ):
412
417
job_name = "sagemaker-tensorflow-py2-gpu-2017-10-24-14-12-09"
413
418
output_path = "s3://{}/output/{}/" .format (sagemaker_session .default_bucket (), job_name )
@@ -418,11 +423,13 @@ def test_tf_training_and_evaluation_steps(sagemaker_session):
418
423
assert tf .hyperparameters ()['evaluation_steps' ] == '456'
419
424
420
425
426
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
421
427
def test_tf_checkpoint_set (sagemaker_session ):
422
428
tf = _build_tf (sagemaker_session , checkpoint_path = 's3://my_checkpoint_bucket' )
423
429
assert tf .hyperparameters ()['checkpoint_path' ] == json .dumps ("s3://my_checkpoint_bucket" )
424
430
425
431
432
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
426
433
def test_train_image_default (sagemaker_session ):
427
434
tf = TensorFlow (entry_point = SCRIPT_PATH ,
428
435
role = ROLE ,
@@ -433,6 +440,7 @@ def test_train_image_default(sagemaker_session):
433
440
assert _get_full_cpu_image_uri (defaults .TF_VERSION ) in tf .train_image ()
434
441
435
442
443
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
436
444
def test_attach (sagemaker_session , tf_version ):
437
445
training_image = '1.dkr.ecr.us-west-2.amazonaws.com/sagemaker-tensorflow-py2-cpu:{}-cpu-py2' .format (tf_version )
438
446
rjd = {
@@ -483,6 +491,7 @@ def test_attach(sagemaker_session, tf_version):
483
491
assert estimator .checkpoint_path == 's3://other/1508872349'
484
492
485
493
494
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
486
495
def test_attach_new_repo_name (sagemaker_session , tf_version ):
487
496
training_image = '520713654638.dkr.ecr.us-west-2.amazonaws.com/sagemaker-tensorflow:{}-cpu-py2' .format (tf_version )
488
497
rjd = {
@@ -531,6 +540,7 @@ def test_attach_new_repo_name(sagemaker_session, tf_version):
531
540
assert estimator .train_image () == training_image
532
541
533
542
543
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
534
544
def test_attach_old_container (sagemaker_session ):
535
545
training_image = '1.dkr.ecr.us-west-2.amazonaws.com/sagemaker-tensorflow-py2-cpu:1.0'
536
546
rjd = {
@@ -707,18 +717,16 @@ def test_script_mode_create_model(create_tfs_model, sagemaker_session):
707
717
create_tfs_model .assert_called_once ()
708
718
709
719
720
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
710
721
@patch ('sagemaker.tensorflow.estimator.Tensorboard._sync_directories' )
711
722
@patch ('sagemaker.tensorflow.estimator.Tensorboard.start' )
712
- @patch ('tempfile.mkdtemp' , return_value = '/my/temp/folder' )
713
- @patch ('shutil.rmtree' )
714
723
@patch ('os.access' , return_value = True )
715
724
@patch ('subprocess.call' )
716
725
@patch ('subprocess.Popen' )
717
726
@patch ('time.strftime' , return_value = TIMESTAMP )
718
727
@patch ('time.time' , return_value = TIME )
719
728
@patch ('time.sleep' )
720
- def test_script_mode_tensorboard (sleep , time , strftime , popen , call , access , rmtree , mkdtemp ,
721
- start , sync , sagemaker_session ):
729
+ def test_script_mode_tensorboard (sleep , time , strftime , popen , call , access , start , sync , sagemaker_session ):
722
730
tf = TensorFlow (entry_point = SCRIPT_PATH , role = ROLE , sagemaker_session = sagemaker_session ,
723
731
train_instance_count = INSTANCE_COUNT , train_instance_type = INSTANCE_TYPE ,
724
732
framework_version = 'some_version' , script_mode = True )
@@ -729,18 +737,13 @@ def test_script_mode_tensorboard(sleep, time, strftime, popen, call, access, rmt
729
737
730
738
@patch ('time.strftime' , return_value = TIMESTAMP )
731
739
@patch ('time.time' , return_value = TIME )
732
- @patch ('sagemaker.estimator.tar_and_upload_dir' )
733
- @patch ('sagemaker.model.tar_and_upload_dir' )
734
- def test_tf_script_mode (m_tar , e_tar , time , strftime , sagemaker_session ):
740
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
741
+ def test_tf_script_mode (time , strftime , sagemaker_session ):
735
742
tf = TensorFlow (entry_point = SCRIPT_FILE , role = ROLE , sagemaker_session = sagemaker_session , py_version = 'py3' ,
736
743
train_instance_type = INSTANCE_TYPE , train_instance_count = 1 , framework_version = '1.11' ,
737
744
source_dir = DATA_DIR )
738
745
739
746
inputs = 's3://mybucket/train'
740
- s3_prefix = 's3://{}/{}/source/sourcedir.tar.gz' .format (BUCKET_NAME , SM_JOB_NAME )
741
- e_tar .return_value = UploadedCode (s3_prefix = s3_prefix , script_name = SCRIPT_FILE )
742
- s3_prefix = 's3://{}/{}/sourcedir.tar.gz' .format (BUCKET_NAME , SM_JOB_NAME )
743
- m_tar .return_value = UploadedCode (s3_prefix = s3_prefix , script_name = SCRIPT_FILE )
744
747
tf .fit (inputs = inputs )
745
748
746
749
call_names = [c [0 ] for c in sagemaker_session .method_calls ]
@@ -755,18 +758,13 @@ def test_tf_script_mode(m_tar, e_tar, time, strftime, sagemaker_session):
755
758
756
759
@patch ('time.strftime' , return_value = TIMESTAMP )
757
760
@patch ('time.time' , return_value = TIME )
758
- @patch ('sagemaker.estimator.tar_and_upload_dir' )
759
- @patch ('sagemaker.model.tar_and_upload_dir' )
760
- def test_tf_script_mode_ps (m_tar , e_tar , time , strftime , sagemaker_session ):
761
+ @patch ('sagemaker.utils.create_tar_file' , MagicMock ())
762
+ def test_tf_script_mode_ps (time , strftime , sagemaker_session ):
761
763
tf = TensorFlow (entry_point = SCRIPT_FILE , role = ROLE , sagemaker_session = sagemaker_session , py_version = 'py3' ,
762
764
train_instance_type = INSTANCE_TYPE , train_instance_count = 1 , framework_version = '1.11' ,
763
765
source_dir = DATA_DIR , distributions = DISTRIBUTION_ENABLED )
764
766
765
767
inputs = 's3://mybucket/train'
766
- s3_prefix = 's3://{}/{}/source/sourcedir.tar.gz' .format (BUCKET_NAME , SM_JOB_NAME )
767
- e_tar .return_value = UploadedCode (s3_prefix = s3_prefix , script_name = SCRIPT_FILE )
768
- s3_prefix = 's3://{}/{}/sourcedir.tar.gz' .format (BUCKET_NAME , SM_JOB_NAME )
769
- m_tar .return_value = UploadedCode (s3_prefix = s3_prefix , script_name = SCRIPT_FILE )
770
768
tf .fit (inputs = inputs )
771
769
772
770
call_names = [c [0 ] for c in sagemaker_session .method_calls ]
0 commit comments