Possibility to add source_directory to Sagemaker.processing.ScriptProcessor
Describe the feature you'd like
Currently there is no option to add source_dir to Sagemaker.processing.ScriptProcessor, which makes it difficult to have modular code.
How would this feature be used? Please describe. This will help using ScriptProcessor to be used for running complex processing logic. Especially where the project has different modules and each module perform different processing step.
Describe alternatives you've considered running it directly on docker ?
Almost seems like the best way to handle this might be to package the code and pip install it. But...
One solution might be to submit a script that runs the code along with a src dir as a processing input. Command would be something like ["sh", "run-package.sh"]. You might even get away without a local dir pip install if you cd in to the src dir before running (kinda hacky tho). I've done something similar for installing deps on the fly then running the script, worked like a charm.
run-package.sh
# Local dir pip install wherever this was mounted to as a processing input -- need a setup.py or pyproject.toml
# Or maybe just cd /opt/ml/input/package?
pip install /opt/ml/input/package/
# Have an entry point that kicks everything off
# Careful with the "$@" here, it might have the script name as the first arg, but you might want that.
python /opt/ml/input/package/main.py "${@:2}"
@jmahlik : I understand the context but for better understanding, can you point me to a git repo or any example which I can refer. Thank you
@jmahlik : I understand the context but for better understanding, can you point me to a git repo or any example which I can refer. Thank you
I don't have a repo, but the code might look something like this. There's a hundred different ways one could go about it for starting the script, but this example should give you some inspiration.
Tad hacky, but it allows you to do basically anything you want ex. installing dependencies on the fly.
inputs = [
# If you mount data to the instance.
ProcessingInput(
source="s3://path-to-data",
destination="/opt/ml/processing/input/data/",
),
# Mount the src dir with your code here.
ProcessingInput(
source=f"s3://path-to-src-dir",
destination="/opt/ml/processing/input/src/",
),
]
script_processor = ScriptProcessor(
# Your script that kicks off the program.
command=["sh", "run-code.sh"],
image_uri=image_name,
role=role,
instance_count=1,
instance_type=instance_type,
)
script_processor.run(
inputs=inputs,
# Wherever your process writes data to on the instance.
# Maybe it doesn't and you don't need this
outputs=[
ProcessingOutput(
source="/opt/ml/output/data/",
)
],
# This is where you upload the run-code.sh to
code=code_uri,
)
run-code.sh
# ignore the first arg, it's usually the script name, you might want that
# could be python3 depending on the image
python /opt/ml/processing/input/src/main.py "${@:2}"
very interesting 👍
Is this request still in needed?
Yes, it would be highly desirable to provide some additional python files that automatically become importable by your main script. I've had >10 peers express interest in this feature.
They are working around it by altering the sys.path and mounting code as a ProcessingInput which doesn't feel great. There's another workaround around using this importlib recipe to load mounted source files directly. That's even more difficult for a typical user to impalement correctly.
It'd be great if one could just tell the processor about a directory containing python files/modules and they became automatically importable.
Thanks for update, will triage this request to related team
Hey is there any update about this one? it's a crucial feature that makes coding much easier than all the workarounds I'm thinking about. This feature makes Sagemaker a modular product for complex model pipelines, which currently unfortunately isn't right now.
Is this request still in needed?
Yes.
Hi everyone,
I was working on something similar and found an alternative approach. It may not be simpler than the one suggested by @jmahlik here https://github.com/aws/sagemaker-python-sdk/issues/2117#issuecomment-797193878, but I figured it might still be useful to share.
All classes that inherit from FrameworkProcessor accept a source_dir parameter in their run(...) method. For example, XGBoostProcessor supports both source_dir and image_uri, which gives some flexibility. Here’s what my setup looks like:
Dockerfile
(I’m using Poetry as the package manager)
FROM python:3.13-slim-bookworm AS builder
# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libpq-dev \
gettext \
curl \
python3-dev \
gcc \
&& rm -rf /var/lib/apt/lists/*
# Install Poetry
ENV PIP_NO_CACHE_DIR=1
RUN pip install --no-cache-dir poetry==1.7.1
# Configure Poetry to install dependencies system-wide
RUN poetry config virtualenvs.create false
# Copy dependency files
WORKDIR /build
COPY pyproject.toml poetry.lock ./
# Install Python dependencies system-wide
RUN poetry install --no-root --only main \
&& find /usr/local \
\( -type d -a -name test -o -name tests \) \
-o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
-exec rm -rf '{}' + \
&& rm -rf $(poetry config cache-dir)
ENV PYTHONUNBUFFERED=TRUE
ENTRYPOINT ["python3"]
Running the Processor
xgboost_processor = XGBoostProcessor(
image_uri=<ecr_image_uri>,
framework_version='1.7-1', # Placeholder, will be ignored
role=<your_role>,
instance_type=<your_instance>,
instance_count=<number_of_instances>,
)
xgboost_processor.run(
code="preprocessing.py",
source_dir="predictions",
inputs=[
ProcessingInput(
input_name='input_dataset',
source="s3://path-to-data",
destination="/opt/ml/processing/input/data"
),
],
outputs=[
ProcessingOutput(
output_name='processed_dataset',
source='/opt/ml/processing/output/',
destination="s3://path-to-data"
)
],
)
My preprocessing.py script is located in the root of the predictions module in my project.
Hope this helps! Let me know if there are any potential drawbacks to using XGBoostProcessor in this way.
+1 would be a great feature rather to do it in hacky way
It is essential feature for me. Is no one writing modular code anymore? I remember Azure ML had this feature 7 years ago!!