<|start_filename|>docker/Dockerfile<|end_filename|>
FROM nvcr.io/nvidia/deepstream:5.0-20.07-triton

RUN apt-get update && apt install --no-install-recommends -y \
    ca-certificates \
    python-gst-1.0 \
    wget

# allow GObject to find typelibs
ENV GI_TYPELIB_PATH /usr/lib/x86_64-linux-gnu/girepository-1.0/

# use conda to simplify some dependency managemeny
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \
    /bin/bash ~/miniconda.sh -b -p /opt/conda && \
    rm ~/miniconda.sh && \
    ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh

ENV PATH /opt/conda/bin:${PATH}
ENV LD_LIBRARY_PATH /usr/local/cuda-10.2/compat:/opt/conda/lib:/opt/nvidia/deepstream/deepstream-5.0/lib:${LD_LIBRARY_PATH}

RUN conda install -y -c pytorch \
    cudatoolkit=10.2 \
    pytorch \
    torchvision

RUN conda install -y -c conda-forge \
    pygobject \
    scikit-image

# Nvidia Apex for mixed-precision inference
RUN git clone https://github.com/NVIDIA/apex.git /build/apex
WORKDIR /build/apex
RUN pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" .

RUN pip install --upgrade cython
RUN pip install --upgrade gil_load

# Gstreamer debug output location
env GST_DEBUG_DUMP_DOT_DIR=/app/logs

RUN python -c "import torch; torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_ssd', model_math='fp32')" 2>/dev/null | :
RUN python -c "import torch; torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_ssd', model_math='fp16')" 2>/dev/null | :

RUN rm -rf /var/lib/apt/lists/* && \
    conda clean -afy

WORKDIR /app


<|start_filename|>Makefile<|end_filename|>

DOCKER_CMD := docker run -it --rm --gpus=all --privileged=true --ipc=host -v $(shell pwd):/app
DOCKER_PY_CMD := ${DOCKER_CMD} --entrypoint=python
DOCKER_NSYS_CMD := ${DOCKER_CMD} --entrypoint=nsys
PROFILE_CMD := profile -t cuda,cublas,cudnn,nvtx,osrt --force-overwrite=true --delay=2 --duration=30

PROFILE_TARGETS = logs/tuning_baseline.qdrep logs/tuning_postprocess_1.qdrep

.PHONY: sleep 


build-container: docker/Dockerfile
	docker build -f $< -t pytorch-video-pipeline:latest .


run-container: build-container
	${DOCKER_CMD} pytorch-video-pipeline:latest


logs/cli.pipeline.dot:
	${DOCKER_CMD} --entrypoint=gst-launch-1.0 pytorch-video-pipeline:latest filesrc location=media/in.mp4 num-buffers=200 ! decodebin ! progressreport update-freq=1 ! fakesink sync=true


logs/%.pipeline.dot: %.py
	${DOCKER_PY_CMD} pytorch-video-pipeline:latest $<


logs/%.qdrep: %.py
	${DOCKER_NSYS_CMD} pytorch-video-pipeline:latest ${PROFILE_CMD} -o $@ python $<


%.pipeline.png: logs/%.pipeline.dot
	dot -Tpng -o$@ $< && rm -f $<


%.output.svg: %.rec
	cat $< | svg-term > $@
	
%.rec:
	asciinema rec $@ -c "$(MAKE) --no-print-directory logs/$*.pipeline.dot sleep"

sleep:
	@sleep 2
	@echo "---"


pipeline: cli.pipeline.png frames_into_python.pipeline.png frames_into_pytorch.pipeline.png

tuning: logs/tuning_baseline.qdrep logs/tuning_postprocess_1.qdrep logs/tuning_postprocess_2.qdrep logs/tuning_batch.qdrep logs/tuning_fp16.qdrep logs/tuning_dtod.qdrep logs/tuning_concurrency.qdrep

