Skip to content

Conversation

@YanisLalou
Copy link

@YanisLalou YanisLalou commented Jan 23, 2026

Summary

  • Fix HyperparameterTuner._upload_source_code_and_configure_hyperparameters() to respect ignore_patterns from SourceCode
  • Add fnmatch-based filtering for both files and directories during tarball creation
  • Add comprehensive unit tests for the new ignore pattern functionality

Problem

The HyperparameterTuner class does not respect the ignore_patterns parameter from SourceCode when uploading source code to S3. This causes massive unnecessary uploads (656MB+ instead of ~5MB observed) because files like .venv/, __pycache__/, *.pt, etc. are included in the tarball.

Affected file: sagemaker/train/tuner.py

Affected method: _upload_source_code_and_configure_hyperparameters() (around lines 503-508)

Before (buggy code)

# Create tar.gz archive
with tarfile.open(tar_path, 'w:gz') as tar:
    for root, dirs, files in os.walk(source_dir):
        for file in files:
            file_path = os.path.join(root, file)
            arcname = os.path.relpath(file_path, source_dir)
            tar.add(file_path, arcname=arcname)

The code uses os.walk() to traverse the source directory but completely ignores the ignore_patterns attribute from SourceCode, which is properly respected by ModelTrainer.train().

After (fix)

# Create tar.gz archive
with tarfile.open(tar_path, 'w:gz') as tar:
    # Get ignore patterns from source_code (default to empty list if None)
    ignore_pats = source_code.ignore_patterns or []

    # Helper to check if a name matches any ignore pattern
    def should_ignore(name):
        return any(fnmatch.fnmatch(name, pat) for pat in ignore_pats)

    # Add files from source_dir, respecting ignore_patterns
    for root, dirs, files in os.walk(source_dir):
        # Filter directories in-place to skip ignored dirs
        dirs[:] = [d for d in dirs if not should_ignore(d)]

        for file in files:
            if should_ignore(file):
                continue
            file_path = os.path.join(root, file)
            arcname = os.path.relpath(file_path, source_dir)
            tar.add(file_path, arcname=arcname)

Why This Matters

  1. Performance: Upload times go from seconds to 10+ minutes for projects with virtual environments or large data directories
  2. Cost: Unnecessary S3 storage and data transfer costs
  3. Consistency: ModelTrainer.train() respects ignore_patterns, but HyperparameterTuner.tune() doesn't, creating inconsistent behavior in the same SDK
  4. User expectation: Users configure ignore_patterns in SourceCode expecting it to work everywhere

Test plan

  • Added unit tests for ignore pattern filtering
  • Test that files matching ignore patterns are excluded
  • Test that directories matching ignore patterns are completely skipped
  • Test backward compatibility with None/empty ignore_patterns
  • Test with default ignore patterns from SourceCode class

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

  • I have read the CONTRIBUTING doc
  • I certify that the changes I am introducing will be backward compatible, and I have discussed concerns about this, if any, with the Python SDK team
  • I used the commit message format described in CONTRIBUTING
  • I have passed the region in to all S3 and STS clients that I've initialized as part of this change.
  • I have updated any necessary documentation, including READMEs and API docs (if appropriate)

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have added unit and/or integration tests as appropriate to ensure backward compatibility of the changes
  • I have checked that my tests are not configured for a specific region or account (if appropriate)
  • I have used unique_name_from_base to create resource names in integ tests (if appropriate)
  • If adding any dependency in requirements.txt files, I have spell checked and ensured they exist in PyPi

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@YanisLalou YanisLalou force-pushed the fix-hyperparameter-tuner-ignore-patterns branch from d52140e to 5dd55b6 Compare January 23, 2026 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant