From e37528268cb0f592af006efaa6bb593501ef86ff Mon Sep 17 00:00:00 2001 From: Jaime Fraustro Date: Fri, 21 Feb 2025 11:42:11 -0600 Subject: [PATCH] Add support for Intel GPU to MNIST examples * Add support for Intel GPU to MNIST example * Add support for Intel GPU to MNIST Forward-Forward example * Add support for Intel GPU to MNIST using RNN example and update README with optional arguments * Refactor argument parsing in MNIST examples. There is no need to use `default=False` with `store_true` Signed-off-by: jafraustro --- mnist/main.py | 13 +++++++++---- mnist_forward_forward/README.md | 1 + mnist_forward_forward/main.py | 12 ++++++++---- mnist_rnn/README.md | 17 +++++++++++++++++ mnist_rnn/main.py | 10 +++++++--- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/mnist/main.py b/mnist/main.py index 184dc4744f..09487639d4 100644 --- a/mnist/main.py +++ b/mnist/main.py @@ -82,21 +82,24 @@ def main(): help='learning rate (default: 1.0)') parser.add_argument('--gamma', type=float, default=0.7, metavar='M', help='Learning rate step gamma (default: 0.7)') - parser.add_argument('--no-cuda', action='store_true', default=False, + parser.add_argument('--no-cuda', action='store_true', help='disables CUDA training') - parser.add_argument('--no-mps', action='store_true', default=False, + parser.add_argument('--no-mps', action='store_true', help='disables macOS GPU training') - parser.add_argument('--dry-run', action='store_true', default=False, + parser.add_argument('--no-xpu', action='store_true', + help='disables Intel GPU training') + parser.add_argument('--dry-run', action='store_true', help='quickly check a single pass') parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)') parser.add_argument('--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') - parser.add_argument('--save-model', action='store_true', default=False, + parser.add_argument('--save-model', action='store_true', help='For Saving the current Model') args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() use_mps = not args.no_mps and torch.backends.mps.is_available() + use_xpu = not args.no_mps and torch.xpu.is_available() torch.manual_seed(args.seed) @@ -104,6 +107,8 @@ def main(): device = torch.device("cuda") elif use_mps: device = torch.device("mps") + elif use_xpu: + device = torch.device("xpu") else: device = torch.device("cpu") diff --git a/mnist_forward_forward/README.md b/mnist_forward_forward/README.md index f6ae12e56d..8857c9a6fb 100644 --- a/mnist_forward_forward/README.md +++ b/mnist_forward_forward/README.md @@ -18,6 +18,7 @@ optional arguments: --lr LR learning rate (default: 0.03) --no_cuda disables CUDA training --no_mps disables MPS training + --no_xpu disables XPU training --seed SEED random seed (default: 1) --save_model For saving the current Model --train_size TRAIN_SIZE diff --git a/mnist_forward_forward/main.py b/mnist_forward_forward/main.py index a175126067..e6c2902ed8 100644 --- a/mnist_forward_forward/main.py +++ b/mnist_forward_forward/main.py @@ -102,10 +102,13 @@ def train(self, x_pos, x_neg): help="learning rate (default: 0.03)", ) parser.add_argument( - "--no_cuda", action="store_true", default=False, help="disables CUDA training" + "--no_cuda", action="store_true", help="disables CUDA training" ) parser.add_argument( - "--no_mps", action="store_true", default=False, help="disables MPS training" + "--no_mps", action="store_true", help="disables MPS training" + ) + parser.add_argument( + "--no_xpu", action="store_true", help="disables XPU training" ) parser.add_argument( "--seed", type=int, default=1, metavar="S", help="random seed (default: 1)" @@ -113,7 +116,6 @@ def train(self, x_pos, x_neg): parser.add_argument( "--save_model", action="store_true", - default=False, help="For saving the current Model", ) parser.add_argument( @@ -126,7 +128,6 @@ def train(self, x_pos, x_neg): parser.add_argument( "--save-model", action="store_true", - default=False, help="For Saving the current Model", ) parser.add_argument( @@ -139,10 +140,13 @@ def train(self, x_pos, x_neg): args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() use_mps = not args.no_mps and torch.backends.mps.is_available() + use_xpu = not args.no_xpu and torch.xpu.is_available() if use_cuda: device = torch.device("cuda") elif use_mps: device = torch.device("mps") + elif use_xpu: + device = torch.device("xpu") else: device = torch.device("cpu") diff --git a/mnist_rnn/README.md b/mnist_rnn/README.md index c879cb367f..ba63513711 100644 --- a/mnist_rnn/README.md +++ b/mnist_rnn/README.md @@ -8,3 +8,20 @@ pip install -r requirements.txt python main.py # CUDA_VISIBLE_DEVICES=2 python main.py # to specify GPU id to ex. 2 ``` + +```bash +optional arguments: + -h, --help show this help message and exit + --batch_size input batch_size for training (default:64) + --testing_batch_size input batch size for testing (default: 1000) + --epochs EPOCHS number of epochs to train (default: 14) + --lr LR learning rate (default: 0.1) + --gamma learning rate step gamma (default: 0.7) + --cuda enables CUDA training + --xpu enables XPU training + --mps enables macos GPU training + --seed SEED random seed (default: 1) + --save_model For saving the current Model + --log_interval how many batches to wait before logging training status + --dry-run quickly check a single pass +``` \ No newline at end of file diff --git a/mnist_rnn/main.py b/mnist_rnn/main.py index 2fa64c00d6..f6c1ff3d48 100644 --- a/mnist_rnn/main.py +++ b/mnist_rnn/main.py @@ -93,15 +93,17 @@ def main(): help='learning rate step gamma (default: 0.7)') parser.add_argument('--cuda', action='store_true', default=False, help='enables CUDA training') - parser.add_argument('--mps', action="store_true", default=False, + parser.add_argument('--mps', action="store_true", help="enables MPS training") - parser.add_argument('--dry-run', action='store_true', default=False, + parser.add_argument('--xpu', action='store_true', + help='enables XPU training') + parser.add_argument('--dry-run', action='store_true', help='quickly check a single pass') parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)') parser.add_argument('--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') - parser.add_argument('--save-model', action='store_true', default=False, + parser.add_argument('--save-model', action='store_true', help='for Saving the current Model') args = parser.parse_args() @@ -109,6 +111,8 @@ def main(): device = "cuda" elif args.mps and not args.cuda: device = "mps" + elif args.xpu: + device = "xpu" else: device = "cpu"