Skip to content

Commit fcce71c

Browse files
eromomondvrogozh
andauthored
Restore default CI configuration for VAE and Siamese examples using accelerator API (#1342)
* Restore default CI configuration for VAE and Siamese examples using Accelerator API * Update Siamese Readme for consistency with accelerator argument * Update siamese_network/README.md Co-authored-by: Dmitry Rogozhkin <[email protected]> * Update siamese_network/README.md Co-authored-by: Dmitry Rogozhkin <[email protected]> * Update siamese_network/main.py Co-authored-by: Dmitry Rogozhkin <[email protected]> * Update vae/main.py Co-authored-by: Dmitry Rogozhkin <[email protected]> * Improve Readme files for clearer descriptions * Update Readme file structure to enhance organization --------- Co-authored-by: Dmitry Rogozhkin <[email protected]>
1 parent 5cc81aa commit fcce71c

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

siamese_network/README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,27 @@ This implementation varies from FaceNet as we use the `ResNet-18` model from
88
[Deep Residual Learning for Image Recognition](https://arxiv.org/pdf/1512.03385.pdf) as our feature extractor.
99
In addition, we aren't using `TripletLoss` as the MNIST dataset is simple, so `BCELoss` can do the trick.
1010

11+
### Usage
12+
13+
Install the required dependencies:
1114
```bash
1215
pip install -r requirements.txt
16+
```
17+
18+
To run the example, execute:
19+
```bahs
1320
python main.py
1421
# CUDA_VISIBLE_DEVICES=2 python main.py # to specify GPU id to ex. 2
1522
```
23+
24+
If a hardware accelerator device is detected, the example will execute on the accelerator; otherwise, it will run on the CPU.
25+
26+
To force execution on the CPU, use `--no-accel` command line argument:
27+
28+
```bash
29+
python main.py --no-accel
30+
```
31+
1632
Optionally, you can add the following arguments to customize your execution.
1733

1834
```bash
@@ -21,17 +37,9 @@ Optionally, you can add the following arguments to customize your execution.
2137
--epochs number of epochs to train (default: 14)
2238
--lr learning rate (default: 1.0)
2339
--gamma learning rate step gamma (default: 0.7)
24-
--accel use accelerator
40+
--no-accel disables accelerator
2541
--dry-run quickly check a single pass
2642
--seed random seed (default: 1)
2743
--log-interval how many batches to wait before logging training status
2844
--save-model Saving the current Model
2945
```
30-
31-
To execute in an GPU, add the --accel argument to the command. For example:
32-
33-
```bash
34-
python main.py --accel
35-
```
36-
37-
This command will execute the example on the detected GPU.

siamese_network/main.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ def main():
247247
help='learning rate (default: 1.0)')
248248
parser.add_argument('--gamma', type=float, default=0.7, metavar='M',
249249
help='Learning rate step gamma (default: 0.7)')
250-
parser.add_argument('--accel', action='store_true',
251-
help='use accelerator')
250+
parser.add_argument('--no-accel', action='store_true',
251+
help='disables accelerator')
252252
parser.add_argument('--dry-run', action='store_true', default=False,
253253
help='quickly check a single pass')
254254
parser.add_argument('--seed', type=int, default=1, metavar='S',
@@ -258,16 +258,13 @@ def main():
258258
parser.add_argument('--save-model', action='store_true', default=False,
259259
help='For Saving the current Model')
260260
args = parser.parse_args()
261+
262+
use_accel = not args.no_accel and torch.accelerator.is_available()
261263

262264
torch.manual_seed(args.seed)
263265

264-
if args.accel and not torch.accelerator.is_available():
265-
print("ERROR: accelerator is not available, try running on CPU")
266-
sys.exit(1)
267-
if not args.accel and torch.accelerator.is_available():
268-
print("WARNING: accelerator is available, run with --accel to enable it")
269266

270-
if args.accel:
267+
if use_accel:
271268
device = torch.accelerator.current_accelerator()
272269
else:
273270
device = torch.device("cpu")
@@ -276,12 +273,12 @@ def main():
276273

277274
train_kwargs = {'batch_size': args.batch_size}
278275
test_kwargs = {'batch_size': args.test_batch_size}
279-
if device=="cuda":
280-
cuda_kwargs = {'num_workers': 1,
276+
if use_accel:
277+
accel_kwargs = {'num_workers': 1,
281278
'pin_memory': True,
282279
'shuffle': True}
283-
train_kwargs.update(cuda_kwargs)
284-
test_kwargs.update(cuda_kwargs)
280+
train_kwargs.update(accel_kwargs)
281+
test_kwargs.update(accel_kwargs)
285282

286283
train_dataset = APP_MATCHER('../data', train=True, download=True)
287284
test_dataset = APP_MATCHER('../data', train=False)

vae/README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,32 @@
33
This is an improved implementation of the paper [Auto-Encoding Variational Bayes](http://arxiv.org/abs/1312.6114) by Kingma and Welling.
44
It uses ReLUs and the adam optimizer, instead of sigmoids and adagrad. These changes make the network converge much faster.
55

6+
### Usage
7+
Install the required dependencies:
68
```bash
79
pip install -r requirements.txt
10+
```
11+
12+
To run the example, execute:
13+
```bash
814
python main.py
915
```
1016

17+
If a hardware accelerator device is detected, the example will execute on the accelerator; otherwise, it will run on the CPU.
18+
19+
To force execution on the CPU, use `--no-accel` command line argument:
20+
21+
```bash
22+
python main.py --no-accel
23+
```
24+
1125
The main.py script accepts the following optional arguments:
1226

1327
```bash
1428
--batch-size input batch size for training (default: 128)
1529
--epochs number of epochs to train (default: 10)
16-
--accel use accelerator
30+
--no-accel disables accelerator
1731
--seed random seed (default: 1)
1832
--log-interval how many batches to wait before logging training status
19-
```
33+
```
34+

vae/main.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,27 @@
1313
help='input batch size for training (default: 128)')
1414
parser.add_argument('--epochs', type=int, default=10, metavar='N',
1515
help='number of epochs to train (default: 10)')
16-
parser.add_argument('--accel', action='store_true',
17-
help='use accelerator')
16+
parser.add_argument('--no-accel', action='store_true',
17+
help='disables accelerator')
1818
parser.add_argument('--seed', type=int, default=1, metavar='S',
1919
help='random seed (default: 1)')
2020
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
2121
help='how many batches to wait before logging training status')
2222
args = parser.parse_args()
2323

24+
use_accel = not args.no_accel and torch.accelerator.is_available()
2425

2526
torch.manual_seed(args.seed)
2627

27-
if args.accel and not torch.accelerator.is_available():
28-
print("ERROR: accelerator is not available, try running on CPU")
29-
sys.exit(1)
30-
if not args.accel and torch.accelerator.is_available():
31-
print("WARNING: accelerator is available, run with --accel to enable it")
3228

33-
if args.accel:
29+
if use_accel:
3430
device = torch.accelerator.current_accelerator()
3531
else:
3632
device = torch.device("cpu")
3733

3834
print(f"Using device: {device}")
3935

40-
kwargs = {'num_workers': 1, 'pin_memory': True} if device=="cuda" else {}
36+
kwargs = {'num_workers': 1, 'pin_memory': True} if use_accel else {}
4137
train_loader = torch.utils.data.DataLoader(
4238
datasets.MNIST('../data', train=True, download=True,
4339
transform=transforms.ToTensor()),

0 commit comments

Comments
 (0)