Skip to content

Add accelerator API to GCN example. #1365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2025
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
21 changes: 6 additions & 15 deletions gcn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ This repository contains an implementation of Graph Convolutional Networks (GCN)
## Overview
This project implements the GCN model proposed in the paper for semi-supervised node classification on graph-structured data. GCN leverages graph convolutions to aggregate information from neighboring nodes and learn node representations for downstream tasks. The implementation provides a flexible and efficient GCN model for graph-based machine learning tasks.

# Requirements
- Python 3.7 or higher
- PyTorch 2.0 or higher
- Requests 2.31 or higher
- NumPy 1.24 or higher


# Installation
## Requirements
```bash
pip install -r requirements.txt
python main.py
```

# Usage
```bash
python main.py --epochs 200 --lr 0.01 --l2 5e-4 --dropout-p 0.5 --hidden-dim 16 --val-every 20 --include-bias
```

# Dataset
Expand All @@ -24,12 +21,6 @@ The implementation includes support for the Cora dataset, a standard benchmark d
## Model Architecture
The GCN model architecture follows the details provided in the paper. It consists of multiple graph convolutional layers with ReLU activation, followed by a final softmax layer for classification. The implementation supports customizable hyperparameters such as the number of hidden units, the number of layers, and dropout rate.

## Usage
To train and evaluate the GCN model on the Cora dataset, use the following command:
```bash
python train.py --epochs 200 --lr 0.01 --l2 5e-4 --dropout-p 0.5 --hidden-dim 16 --val-every 20 --include-bias False --no-cuda False
```

# Results
The model achieves a classification accuracy of 82.5% on the test set of the Cora dataset after 200 epochs of training. This result is comparable to the performance reported in the original paper. However, the results can vary due to the randomness of the train/val/test split.

Expand Down
23 changes: 8 additions & 15 deletions gcn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ def load_cora(path='./cora', device='cpu'):

return features.to_sparse().to(device), labels.to(device), adj_mat.to_sparse().to(device)


def train_iter(epoch, model, optimizer, criterion, input, target, mask_train, mask_val, print_every=10):
start_t = time.time()
model.train()
Expand Down Expand Up @@ -199,8 +198,6 @@ def test(model, criterion, input, target, mask):


if __name__ == '__main__':
device = 'cuda' if torch.cuda.is_available() else 'cpu'

parser = argparse.ArgumentParser(description='PyTorch Graph Convolutional Network')
parser.add_argument('--epochs', type=int, default=200,
help='number of epochs to train (default: 200)')
Expand All @@ -214,29 +211,25 @@ def test(model, criterion, input, target, mask):
help='dimension of the hidden representation (default: 16)')
parser.add_argument('--val-every', type=int, default=20,
help='epochs to wait for print training and validation evaluation (default: 20)')
parser.add_argument('--include-bias', action='store_true', default=False,
parser.add_argument('--include-bias', action='store_true',
help='use bias term in convolutions (default: False)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--no-mps', action='store_true', default=False,
help='disables macOS GPU training')
parser.add_argument('--dry-run', action='store_true', default=False,
parser.add_argument('--no-accel', action='store_true',
help='disables accelerator')
parser.add_argument('--dry-run', action='store_true',
help='quickly check a single pass')
parser.add_argument('--seed', type=int, default=42, metavar='S',
help='random seed (default: 42)')
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_accel = not args.no_accel and torch.accelerator.is_available()

torch.manual_seed(args.seed)

if use_cuda:
device = torch.device('cuda')
elif use_mps:
device = torch.device('mps')
if use_accel:
device = torch.accelerator.current_accelerator()
else:
device = torch.device('cpu')

print(f'Using {device} device')

cora_url = 'https://linqs-data.soe.ucsc.edu/public/lbc/cora.tgz'
Expand Down
6 changes: 3 additions & 3 deletions gcn/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
torch
torchvision==0.20.0
torch>=2.6
torchvision
requests
numpy<2
numpy