# Need some help replicating the Python wheel for 2.0.0+cu117

**URL:** <https://dev-discuss.pytorch.org/t/need-some-help-replicating-the-python-wheel-for-2-0-0-cu117/1385>\
**Category:** release/packaging\
**Created:** [July 20, 2023, 7:49am UTC](https://dev-discuss.pytorch.org/t/need-some-help-replicating-the-python-wheel-for-2-0-0-cu117/1385 "2023-07-20T07:49:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sagostinho-ae](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/sagostinho-ae/32/1280_2.png) [@sagostinho-ae](https://dev-discuss.pytorch.org/u/sagostinho-ae)\
**Post date:** [July 20, 2023, 7:49am UTC](https://dev-discuss.pytorch.org/t/need-some-help-replicating-the-python-wheel-for-2-0-0-cu117/1385/1 "2023-07-20T07:49:43Z")

</div>

# Need some help replicating wheel building for 2.0.0+cu117

Hi everyone. I’m currently working on a project that is pinned to Pytorch 2.0.0+cu117. During the course of the project, I’ve run against some [problems](https://github.com/pytorch/pytorch/issues/104880), identified the root cause and came up with solution to patch the behavior.

I’m currently in the process of upstreaming the patch, but until that is done, reviewed, accepted and merged (🤞) it’s gonna take some time. Thus, I decided in the meantime to apply my patch on top of 2.0.0+cu117, rebuild the wheel and host it internally so that my other colleagues are able to benefit from the fix. This brings us to my problem.

Notwithstanding the changes made in the patch, I’m struggling to create a wheel package with the exact same contents as `torch-2.0.0+cu117-cp39-cp39-linux_x86_64.whl`, distributed from [https://download.pytorch.org/whl/torch/](https://download.pytorch.org/whl/torch/). I was wondering if someone with more CI/release knowledge would help identify what I’m missing.

So currently I’ve made the following changes to the `Dockerfile` in the root folder

```diff
diff --git Dockerfile Dockerfile
index e6ade308499..b50cbea3ea4 100644
--- Dockerfile
+++ Dockerfile
@@ -40,7 +40,7 @@ COPY requirements.txt .
 RUN chmod +x ~/miniconda.sh && \
     bash ~/miniconda.sh -b -p /opt/conda && \
     rm ~/miniconda.sh && \
- /opt/conda/bin/conda install -y python=${PYTHON_VERSION} cmake conda-build pyyaml numpy ipython && \
+ /opt/conda/bin/conda install -y python=${PYTHON_VERSION} cmake conda-build mkl-include pyyaml numpy ipython && \
     /opt/conda/bin/python -mpip install -r requirements.txt && \
     /opt/conda/bin/conda clean -ya
 
@@ -50,13 +50,18 @@ COPY . .
 RUN git submodule update --init --recursive
 
 FROM conda as build
+ARG PYTORCH_BUILD_VERSION
+ARG PYTORCH_BUILD_NUMBER="0"
+ENV PYTORCH_BUILD_VERSION=${PYTORCH_BUILD_VERSION}
+ENV PYTORCH_BUILD_NUMBER=${PYTORCH_BUILD_NUMBER}
 WORKDIR /opt/pytorch
 COPY --from=conda /opt/conda /opt/conda
 COPY --from=submodule-update /opt/pytorch /opt/pytorch
 RUN --mount=type=cache,target=/opt/ccache \
- TORCH_CUDA_ARCH_LIST="3.5 5.2 6.0 6.1 7.0+PTX 8.0" TORCH_NVCC_FLAGS="-Xfatbin -compress-all" \
+ TORCH_CUDA_ARCH_LIST="5.2 6.0 6.1 7.0+PTX 7.5 8.0" TORCH_NVCC_FLAGS="-Xfatbin -compress-all" \
     CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" \
- python setup.py install
+ BUILD_TEST=0 \
+ python setup.py install bdist_wheel
 
 FROM conda as conda-installs
 ARG PYTHON_VERSION=3.8
@@ -101,3 +106,4 @@ WORKDIR /workspace
 FROM official as dev
 # Should override the already installed version from the official-image stage
 COPY --from=build /opt/conda /opt/conda
+COPY --from=build /opt/pytorch /opt/pytorch

```

and then I’m kicking off the build process with

```auto
DOCKER_BUILDKIT=1 docker build . --build-arg PYTHON_VERSION=3.9 --build-arg PYTORCH_BUILD_VERSION=2.0.0r1+cu117 --build-arg BASE_IMAGE=nvidia/cuda:11.7.1-cudnn8-devel-ubuntu18.04 --tag pytorch:2.0.0r1-cu117

```

After 3 hours (🥲) I have a wheel inside `/opt/pytorch/dist` that I can pull out of the container.

I’ve compared the contents of `torch-2.0.0+cu117-cp39-cp39-linux_x86_64.whl` (original) with `torch-2.0.0r1+cu117-cp39-cp39-linux_x86_64.whl` (patched) and I’ve noticed I’m missing a couple of crucial libraries as you can see below (note that common .so objects between both folders are not displayed). Most of it are cuda libraries that I expected to be present and available in the base image `nvidia/cuda:11.7.1-cudnn8-devel-ubuntu18.04` and something related to `gomp`.

 ![Screenshot 2023-07-20 at 09.44.38](https://canada1.discourse-cdn.com/flex036/uploads/pytorch1/original/2X/b/b838b750ec6cf0646aab59d21d0c0637261e003c.jpeg)

Any idea what am I missing to achieve the same wheel as in production? I’ve skimmed through the repo’s GitHub workflows but I’m struggling to pinpoint the exact details of how the wheels are built for the project.

I appreciate any help,  
Sérgio

---

<div class="post-metadata">

**Author:** ![smth](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/smth/32/3_2.png) [@smth](https://dev-discuss.pytorch.org/u/smth)\
**Post date:** [August 9, 2023, 9:18am UTC](https://dev-discuss.pytorch.org/t/need-some-help-replicating-the-python-wheel-for-2-0-0-cu117/1385/2 "2023-08-09T09:18:17Z")

</div>

Hi Sergio,

Did you see our wheel build-scripts here: [GitHub - pytorch/builder: Continuous builder and binary build scripts for pytorch](https://github.com/pytorch/builder)  
These are the entry-point scripts to build our wheels: [https://github.com/pytorch/builder/tree/main/manywheel](https://github.com/pytorch/builder/tree/main/manywheel)  
See the README or the `build_all_docker.sh`

I think what you are missing is this logic which unzips the wheel, copies over a bunch of dependencies and then rezips the wheel + fixes the wheel RECORD file.

> <https://github.com/pytorch/builder/blob/main/manywheel/build_common.sh#L305-L429>

---

<div class="post-metadata">

**Author:** ![sagostinho-ae](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/sagostinho-ae/32/1280_2.png) [@sagostinho-ae](https://dev-discuss.pytorch.org/u/sagostinho-ae)\
**Post date:** [August 9, 2023, 11:46am UTC](https://dev-discuss.pytorch.org/t/need-some-help-replicating-the-python-wheel-for-2-0-0-cu117/1385/3 "2023-08-09T11:46:14Z")

</div>

I completely missed that entire repo. Thank you for the pointers. I’ll have a look.

---

<div class="post-metadata">

**Author:** ![sagostinho-ae](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/sagostinho-ae/32/1280_2.png) [@sagostinho-ae](https://dev-discuss.pytorch.org/u/sagostinho-ae)\
**Post date:** [September 14, 2023, 10:50am UTC](https://dev-discuss.pytorch.org/t/need-some-help-replicating-the-python-wheel-for-2-0-0-cu117/1385/4 "2023-09-14T10:50:51Z")

</div>

That did the trick @smth. I finally had some time to revisit the issue. I’ll just document the experience below in case someone else faces the same needs.

Context: in my case I wanted to build 2.0.0 with a custom patch. There are things that are specific to building  
this particular version, at this particular point in time. Expect those to change by the time you read this.

Steps:

1. After adding your patch to the PyTorch version you want to modify, you will need to tag it. Reason: the build script  
currently does not accept setting the build version manually (see [url](https://github.com/pytorch/builder/blob/9f0fad92f342b146272d0f99d3969c2c148e80ba/.circleci/scripts/binary_populate_env.sh#L67-L85)) and will unconditionally look for a tag corresponding to your current git `HEAD` (on your pytorch repo).

2. You need to build the docker image that will be used to build and package pytorch. To do that, clone [GitHub - pytorch/builder: Continuous builder and binary build scripts for pytorch](https://github.com/pytorch/builder) and from this repo, the image you will need will depend on the type of package you need. I was interested in generating a package for PyTorch 2.0.0, linux, with cuda 11.7 and for Python 3.9. At the moment this requires a couple of patches to work.

3. Checkout the correct branch/ref for your PyTorch version.

4. Patch the build process if needed. For 2.0, I had to make the following changes

5. We’re now ready to build the image. This is what I called for my particular needs.

6. The next step is to replicate what is being done by CI, namely in [here](https://github.com/pytorch/pytorch/blob/v2.0.0/.github/workflows/generated-linux-binary-manywheel-master.yml#L74-L89) and [here](https://github.com/pytorch/pytorch/blob/v2.0.0/.github/workflows/_binary-build-linux.yml#L175-L204). From the terminal call

7. Get your wheel from inside `/tmp/artifacts/`

That’s it.
