Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pip install ninja
pip install lark-parser

# Install Pytorch
patch -p1 < xla/pytorch.patch
xla/scripts/apply_patches.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't kokoro/ubuntu/common.sh need the same change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah

python setup.py build develop

# Bazel doesn't work with sccache gcc. https://github.com/bazelbuild/bazel/issues/3642
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ To build:
git checkout $(cat xla/.torch_commit_id)
```

* Apply the `pytorch.patch` to the current `xla` code. From within the _pytorch_ source folder:
* Apply PyTorch patches:

```
patch -p1 < xla/pytorch.patch
xla/scripts/apply_patches.sh
```

* Install the Lark parser used for automatic code generation:
Expand Down
2 changes: 1 addition & 1 deletion kokoro/ubuntu/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cd pytorch

# TODO(jysohn): remove following patching once pytorch JIT bug is fixed
git checkout $(cat xla/.torch_commit_id)
git apply xla/pytorch.patch
xla/scripts/apply_patches.sh
# Build and install torch wheel and collect artifact
export NO_CUDA=1
python setup.py bdist_wheel
Expand Down
9 changes: 9 additions & 0 deletions scripts/apply_patches.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

CDIR=$(dirname $0)
XDIR=$CDIR/..
PTDIR=$XDIR/..

python $CDIR/cond_patch.py \
$XDIR/torch_patches \
$PTDIR
66 changes: 66 additions & 0 deletions scripts/cond_patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python

from __future__ import print_function

import argparse
import glob
import os
import re
import subprocess
import sys


def get_log(repo_folder, depth):
return subprocess.check_output(
['git', '-C', repo_folder, 'log', '-{}'.format(depth)]).decode('utf-8')


def is_applied(log, revno):
revrx = 'Pull Request resolved: .*[/#]{}'.format(revno)
return re.search(revrx, log)


def select_patches(patch_folder, repo_folder, depth):
log = get_log(repo_folder, depth)
files = sorted(glob.glob(os.path.join(patch_folder, '*.diff')))
selected = []
for ppath in files:
revno = os.path.splitext(os.path.basename(ppath))[0]
# Patches which are not all digits (PR numbers) are always applied.
if not re.match(r'\d+$', revno) or not is_applied(log, revno):
selected.append(ppath)
return selected


def apply_patch(ppath, repo_folder, level):
return subprocess.call([
'patch', '-d', repo_folder, '-p{}'.format(level), '-i', ppath, '-E', '-l',
'-r', '-', '-s', '--no-backup-if-mismatch'
])


def patch_repo(args):
patches = select_patches(
os.path.normpath(args.patch_folder), os.path.normpath(args.repo_folder),
args.log_depth)
for ppath in patches:
print('Applying patch file: {}'.format(ppath), file=sys.stderr)
apply_patch(ppath, os.path.normpath(args.repo_folder), args.level)


if __name__ == '__main__':
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--level', type=int, default=1)
arg_parser.add_argument('--log_depth', type=int, default=1000)
arg_parser.add_argument(
'patch_folder',
type=str,
metavar='PATCH_FOLDER',
help='The path to the folder containing the patches')
arg_parser.add_argument(
'repo_folder',
type=str,
metavar='REPO_FOLDER',
help='The path to the root folder of the repo to be patched')
args, files = arg_parser.parse_known_args()
patch_repo(args)
Empty file added torch_patches/.gitignore
Empty file.
17 changes: 17 additions & 0 deletions torch_patches/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Guidelines For Patch File Names

The only files which are considered by the apply script are the ones with extension '.diff'.

A file for PyTorch PR _N_ needs to be named 'N.diff'.

Patch files which are not related to PyTorch PRs, should begin with an 'X' character,
followed by a two digit number, followed by a dash ('-'), a name, and '.diff'.
Example:

```
X10-optimizer.diff
```

Patch file are alphabetically ordered, so PyTorch PR patches are always applied
before the non PyTorch ones.

File renamed without changes.